Keras Error: tensorflow.python.framework.errors_impl.ResourceExhaustedError: failed to allocate memory
I'm attempting to review a lot of famous DL architectures and implement them via Keras.
I'm following <PERSON> book & Github (https://github.com/moelgendy/deep_learning_for_vision_systems/blob/master/chapter_05/.ipynb_checkpoints/alexnet_implementation_in_keras-checkpoint.ipynb) pretty much verbatim, except I'm putting the code in a more object-oriented style so I can build easier to scale image processing pipelines:
Here is AlexNet architecture using Keras:
#References http://www.computervisionbook.com/
#References https://github.com/moelgendy/deep_learning_for_vision_systems/blob/master/chapter_05/.ipynb_checkpoints/alexnet_implementation_in_keras-checkpoint.ipynb
import tensorflow as tf
from keras.models import Sequential
from keras.regularizers import l2
from keras.layers import Conv2D, AveragePooling2D, Flatten, Dense, Activation, MaxPool2D, BatchNormalization, Dropout
from keras.datasets import cifar10
import numpy as np
import keras
from keras.models import load_model, Model
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras import backend as K
class AlexNet:
data: ((),())
datagen : ImageDataGenerator
checkpointer : ModelCheckpoint
batch_size = 2
epochs = 100
def __init__(self):
K.image_dim_ordering='th'
device = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(device[0], True)
#1) download and split the data
# (x_train, y_train), (x_test, y_test) = download()
def download(self):
return cifar10.load_data()
#preprocess 1.1 split
def split(self):
(x_train, y_train), (x_test, y_test) = self.data
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
print("training data = ", x_train.shape)
print("testing data = ", x_test.shape)
self.data = (x_train, y_train), (x_test, y_test)
# preprocess 1.2 Normalize the data to speed up training
def preprocess(self):
(x_train, y_train), (x_test, y_test) = self.data
mean = np.mean(x_train)
std = np.std(x_train)
x_train = (x_train-mean)/(std+1e-7)
x_test = (x_test-mean)/(std+1e-7)
# let's look at the normalized values of a sample image
x_train[0]
self.data = (x_train, y_train), (x_test, y_test)
# preprocess 1.3 one-hot encode the labels in train and test datasets
# we use “to_categorical” function in keras
def toCategorical(self):
(x_train, y_train), (x_test, y_test) = self.data
num_classes = 10
y_train = np_utils.to_categorical(y_train,num_classes)
y_test = np_utils.to_categorical(y_test,num_classes)
# let's display one of the one-hot encoded labels
y_train[0]
self.data = (x_train, y_train), (x_test, y_test)
#build model #2
def buildModel(self):
# Instantiate an empty sequential model
model = Sequential(name="Alexnet")
# 1st layer (conv + pool + batchnorm)
model. 695