Last active
November 22, 2016 02:56
-
-
Save nemo/a35e53343ea0d113b4ff87dae5a9a1af to your computer and use it in GitHub Desktop.
Keras memory error issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers.core import Flatten, Dense, Dropout, Lambda | |
from keras.layers.normalization import BatchNormalization | |
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D | |
from keras.optimizers import SGD, RMSprop, Adam | |
from theano.sandbox import cuda | |
from keras.preprocessing import image | |
cuda.use('gpu') | |
def get_batches(path, gen=image.ImageDataGenerator(), shuffle=True, batch_size=8, class_mode='categorical'): | |
return gen.flow_from_directory(path, target_size=(224,224), | |
class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) | |
batch_size = 8 | |
path = "./data/sample/"; | |
train_batches = get_batches(path + "train", batch_size=batch_size) | |
valid_batches = get_batches(path + "valid", batch_size=batch_size, shuffle=False) | |
test_batches = get_batches(path + "test", batch_size=batch_size, shuffle=False) | |
def get_model(p): | |
return Sequential([ | |
BatchNormalization(input_shape=(3, 244, 244)), | |
ZeroPadding2D((1, 1), input_shape=(3, 244, 244)), | |
Convolution2D(64, 3, 3, activation='relu'), | |
ZeroPadding2D((1, 1)), | |
Convolution2D(64, 3, 3, activation='relu'), | |
MaxPooling2D((2, 2), strides=(2, 2)), | |
ZeroPadding2D((1, 1)), | |
Convolution2D(128, 3, 3, activation='relu'), | |
ZeroPadding2D((1, 1)), | |
Convolution2D(128, 3, 3, activation='relu'), | |
MaxPooling2D((2, 2), strides=(2, 2)), | |
Flatten(), | |
Dense(4096, activation='relu'), | |
Dropout(p), | |
Dense(train_batches.nb_class, activation='softmax') | |
]) | |
model = get_model(0.5) | |
model.compile(optimizer=RMSprop(lr=1e-10), | |
loss='categorical_crossentropy', metrics=['accuracy']) | |
model.fit_generator(train_batches, samples_per_epoch=train_batches.nb_sample, nb_epoch=1, | |
validation_data=valid_batches, nb_val_samples=valid_batches.nb_sample, verbose=True) | |
# Error | |
# MemoryError: ('Error allocating 7803502592 bytes of device memory (out of memory).', "you might consider using 'theano.shared(..., borrow=True)'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment