Memory error while using keras - memory-leaks

I am using keras for CNN but the problem is that there is memory leak. The error is
anushreej#cpusrv-gpu-109:~/12EC35005/MTP_Workspace/MTP$ python cnn_implement.py
Using Theano backend.
[INFO] compiling model...
Traceback (most recent call last):
File "cnn_implement.py", line 23, in <module>
model = CNNModel.build(width=150, height=150, depth=3)
File "/home/ms/anushreej/12EC35005/MTP_Workspace/MTP/cnn/networks/model_define.py", line 27, in build
model.add(Dense(depth*height*width))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/models.py", line 146, in add
output_tensor = layer(self.outputs[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 458, in __call__
self.build(input_shapes[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/layers/core.py", line 604, in build
name='{}_W'.format(self.name))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 61, in glorot_uniform
return uniform(shape, s, name=name)
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 32, in uniform
return K.variable(np.random.uniform(low=-scale, high=scale, size=shape),
File "mtrand.pyx", line 1255, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13575)
File "mtrand.pyx", line 220, in mtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2902)
MemoryError
Now I am unable to understand why is this happening. My training images are very small of the size 150*150*3.
The code is -:
# import the necessary packages
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
class CNNModel:
#staticmethod
def build(width, height, depth):
# initialize the model
model = Sequential()
# first set of CONV => RELU
model.add(Convolution2D(50, 5, 5, border_mode="same", batch_input_shape=(None, depth, height, width)))
model.add(Activation("relu"))
# second set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
# third set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(depth*height*width))
# if weightsPath is not None:
# model.load_weights(weightsPath)
return model

I faced the same problem, I think the issue is the number data points just before the Flattening layer are more than your system can handle(i tried in difference systems so one with high ram worked and with less ram gave this error). Just add more CNN layers to reduce the size and then add a flattening layer it works.
This gave me and error:
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax'))
This didnt give an error
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(Convolution2D(128, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax')
Hope it helps.

Related

Keras ConvLSTM InvalidArgumentError

I'm new enough to TensorFlow and Keras that I might be missing something obvious, but this is driving me nuts. I inherited an app that trains a custom convolutional LSTM, and I just spent the last two months or so straightening out some really atrocious data wrangling, only to discover I can't get the model to train properly.
Here's the model definition (with hard values substituted for the variables in my actual code):
inputs = layers.Input(shape = (2, 23, 23, 10))
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (5,5),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(inputs)
outputs = layers.BatchNormalization()(outputs)
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (3,3),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(outputs)
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (3,3),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(outputs)
outputs = layers.Conv3D(filters = 1,
kernel_size = (3, 3, 3),
padding = "same",
activation = "sigmoid")(outputs)
If something looks squirrely there, let me know--I didn't create this model (though I did change the input grid from 100 x 100 to 23 x 23, if that makes a difference). The idea is to predict the intensity of a particular weather phenomenon (that's a single number for each grid point at each time).
Here's the model summary produced after the model is defined:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 2, 23, 23, 10)] 0
conv_lstm2d (ConvLSTM2D) (None, 2, 23, 23, 32) 134528
batch_normalization (BatchN (None, 2, 23, 23, 32) 128
ormalization)
conv_lstm2d_1 (ConvLSTM2D) (None, 2, 23, 23, 32) 73856
conv_lstm2d_2 (ConvLSTM2D) (None, 2, 23, 23, 32) 73856
conv3d (Conv3D) (None, 2, 23, 23, 1) 865
=================================================================
Total params: 283,233
Trainable params: 283,169
Non-trainable params: 64
_________________________________________________________________
The model is fit using data from a custom Sequence sub-class, which produces X input of shape ([batch size], 2, 23, 23, 10) and Y input of shape ([batch size], 2, 23, 23, 1). The batch size is usually 8, but because of the way the data is lazily loaded, the last batch in a particular block of files may be smaller, which is why I don't specify a batch size in model definition. For the record, the original coders had a constant batch size, though, as with mine, it wasn't specified in the model definition.
When I try to fit the model, I get a crash pretty quickly, with this traceback:
Traceback (most recent call last):
File "C:/code/Python/edapts/ConvLSTM2D.py", line 175, in <module>
history = model.fit(training_data,
File "C:\Python\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Python\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'gradient_tape/model/conv_lstm2d/transpose_1/transpose' defined at (most recent call last):
File "C:/code/Python/edapts/ConvLSTM2D.py", line 175, in <module>
history = model.fit(training_data,
File "C:\Python\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1051, in train_function
return step_function(self, iterator)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1030, in run_step
outputs = model.train_step(data)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 893, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 537, in minimize
grads_and_vars = self._compute_gradients(
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 590, in _compute_gradients
grads_and_vars = self._get_gradients(tape, loss, var_list, grad_loss)
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 471, in _get_gradients
grads = tape.gradient(loss, var_list, grad_loss)
Node: 'gradient_tape/model/conv_lstm2d/transpose_1/transpose'
transpose expects a vector of size 4. But input(1) is a vector of size 5
[[{{node gradient_tape/model/conv_lstm2d/transpose_1/transpose}}]] [Op:__inference_train_function_9880]
I'm so lost. The only thing I can think of is that the original coders made the X and Y features the same, whereas I'm only trying to predict 1 of the 10 (there's not actually a reason to predict the others--not sure why they were trying to). If that's the problem, how do I redefine the model to take account of the different output shape?
EDIT: Well, that's interesting. I just compared the model definition from the original team with the model definition used by the co-worker I inherited the code from. Said co-worker seems to have inserted an extra layer in the outputs, duplicating the ConvLSTM2D with 32 filters and kernel size (3,3). When I remove one of those duplicates, everything runs just fine....
Great, so it's "fixed". But is there someone who can explain why it wasn't working in the first place? My level of understanding the issue at this point is to cross myself and throw salt over my shoulder.
EDIT #2: Does the problem result from having such a small grid (23 x 23)? So either a bigger grid or smaller kernels would solve the problem without deleting a layer? That seems intuitively likely, but I'd like to know the match to calculate how the outputs from each layer match up with the definition of the next layer.

Having a problem with input_shape on Conv1d Layer

I'm attempting to perform a sentiment classification using CNN. The error seems to be related to the input_shape parameters.
The x data consists of arrays of integers created using tokenizer.texts_to_sequences.
? x_train.shape
(4460, 20)
? x_trains.shape[0]
array([ 49, 472, 4436, 843, 756, 659, 64, 8, 1328, 87, 123,
352, 1329, 148, 2996, 1330, 67, 58, 4437, 144])
The y data consist of one hot encoded values for classification.
y_train.shape
(4460, 2)
y_train[0]
array([1., 0.], dtype=float32)
here is the model:
model.add(layers.Conv1D(filters=256, kernel_size=3, activation='relu', input_shape=(max_seqlen,)))
model.add(layers.SpatialDropout1D(0.2))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(num_classes, activation="softmax"))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=3, batch_size=512,
validation_data=(x_val, y_val), class_weight=label_weights)
An error is thrown adding the Conv1D layer. The message is:
"Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=2"
I have no idea what I'm doing wrong. Any help is greatly appreciated.
Conv1D takes a 2D input (I don't know why this is the case). As your input is only 1D, your dimensions don't match up. I'm afraid that you will probably have to stick to other keras layer types, or alternatively reshape your data so that it is (4460, 20, 1), allowing you to pass a conv1D over it.

Tensorflow 2.0 InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:]

i am training a mnist CNN. When i ran my code the problem is coming . I tried other answers but they do not work. I am a new to TensorFlow so can someone explain me this error. Here is my code. i am using Pycharm 2020.2. and Python 3.6 in anaconda. There is no help i could find.
import tensorflow as tf
from tensorflow.keras.models import Sequential
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_train, axis=1)
model = Sequential()
model.add(tf.keras.layers.Dense(256))
model.add(tf.keras.layers.Conv1D(kernel_size=4, strides=1, filters=4, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=3, strides=1, activation="relu", filters=3))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=2, filters=2, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=1, filters=1, strides=1, activation="relu"))
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.MaxPool1D(pool_size=2, strides=1))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=4, filters=4, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=3, filters=3, strides=1, activation="relu"))
model.add(tf.keras.layers.MaxPool1D(pool_size=2, strides=1))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=2, filters=2, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=1, filters=1, strides=1, activation="relu"))
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.Dense(16, activation="softmax"))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=x_train, y=y_train, batch_size=64, epochs=5, shuffle=True, validation_split=0.1)
model.summary()
it is giving the error:
Train on 54000 samples, validate on 6000 samples
Epoch 1/5
2020-09-09 15:16:16.953428: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-09-09 15:16:17.146701: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-09-09 15:16:17.741916: W tensorflow/stream_executor/gpu/redzone_allocator.cc:312] Internal: Invoking GPU asm compilation is supported on Cuda non-Windows platforms only
Relying on driver to perform ptx compilation. This message will be only logged once.
2020-09-09 15:16:18.085250: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: assertion failed: [Condition x == y did not hold element-wise:] [x (loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [64 1] [y (loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [64 14]
[[{{node loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert}}]]
64/54000 [..............................] - ETA: 39:34Traceback (most recent call last):
File "F:\anaconda\envs\tensorflow1\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-d2317d03e1c1>", line 1, in <module>
runfile('F:/Pycharm_projects/my_fun_project/Fake or real news/fake-or-real-news/bitcoin.py', wdir='F:/Pycharm_projects/my_fun_project/Fake or real news/fake-or-real-news')
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "F:/Pycharm_projects/my_fun_project/Fake or real news/fake-or-real-news/bitcoin.py", line 41, in <module>
model.fit(x=x_train, y=y_train, batch_size=64, epochs=5, shuffle=True, validation_split=0.1)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 342, in fit
total_epochs=epochs)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
batch_outs = execution_function(iterator)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
distributed_function(input_fn))
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 632, in _call
return self._stateless_fn(*args, **kwds)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\function.py", line 2363, in __call__
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_call
self.captured_inputs)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\function.py", line 1692, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\function.py", line 545, in call
ctx=ctx)
File "F:\anaconda\envs\tensorflow1\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:] [x (loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [64 1] [y (loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [64 14]
[[node loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at F:/Pycharm_projects/my_fun_project/Fake or real news/fake-or-real-news/bitcoin.py:41) ]] [Op:__inference_distributed_function_2970]
Function call stack:
distributed_function
The error is because your output_shape and label_shape don't match.
This is the architecture of the model you created:
.
As you can see, your model outputs (batch_size, 14, 16) but the labels you provide have a shape of (batch_size, 16).
In order to fix this try adding the Flatten layer before your final Dense layers.
Code:
model = Sequential()
model.add(tf.keras.layers.Dense(256, input_shape = (28,28)))
model.add(tf.keras.layers.Conv1D(kernel_size=4, strides=1, filters=4, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=3, strides=1, activation="relu", filters=3))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=2, filters=2, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=1, filters=1, strides=1, activation="relu"))
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.MaxPool1D(pool_size=2, strides=1))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=4, filters=4, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=3, filters=3, strides=1, activation="relu"))
model.add(tf.keras.layers.MaxPool1D(pool_size=2, strides=1))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=2, filters=2, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv1D(kernel_size=1, filters=1, strides=1, activation="relu"))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.Dense(16, activation="softmax"))
Now your model architecture looks like this:
Now, your model has matching shapes and will train without any issues.

IndexError Using TFLearn and MNIST

I am having an Indexing Error when running the following code:
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
X, Y, test_x, test_y = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
test_x = X.reshape([-1, 28, 28, 1])
convnet = input_data(shape=[None, 28, 28, 1], name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.fit({'input':X},{'targets':Y}, n_epoch=10,
validation_set=({'input':test_x},{'targets':test_y}),
snapshot_step=500, show_metric=True, run_id='mnist')
model.save('tflearncnn.model')
I cannot figure out how to make the index larger than 0-9999 (10000) as i am not sure where the error is occurring.
here is the error in my Terminal:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Exception in thread Thread-5:oss: 0.13790 | time: 29.813s
Traceback (most recent call last):0 - acc: 0.9592 -- iter: 31936/55000
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
data = self.retrieve_data(batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 222, in retrieve_data
utils.slice_array(self.feed_dict[key], batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/utils.py", line 187, in slice_array
return X[start]
IndexError: index 10000 is out of bounds for axis 0 with size 10000
this happens when i reach the point where a new Epoch is supposed to start as shown when step 499 is reached:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Training Step: 499 | total loss: 0.12698 | time: 27.880s
| Adam | epoch: 001 | loss: 0.12698 - acc: 0.9616 -- iter: 31936/55000
I have tried the following:
-Changing size of snapshot_steps
-changing the size of n_units in fully_connected()
-changing the nb_filter in conv_2d
This is just your typo
test_x = X.reshape([-1, 28, 28, 1])
test_x = test_x.reshape([-1, 28, 28, 1])

AssertionError when I use deep learning library Keras

who can give me some notice when casue this error ? I am use VGG16-net to do face recongnition.
ERROR (theano.gof.opt): Optimization failure due to:
LocalOptGroup(local_abstractconv_gemm,local_abstractconv_gradinputs_gemm,local_abstractconv_gradweight_gemm,local_conv2d_cpu,local_conv2d_gradinputs_cpu,local_conv2d_gradweight_cpu)
ERROR (theano.gof.opt): node: AbstractConv2d{border_mode='valid',
subsample=(1, 1), filter_flip=True, imshp=(None, None, None, None),
kshp=(512, 512, 3, 3)}(IncSubtensor{Set;::, ::, int64:int64:,
int64:int64:}.0, convolution2d_26_W) ERROR (theano.gof.opt):
TRACEBACK: ERROR (theano.gof.opt): Traceback (most recent call last):
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 1772, in
process_node
replacements = lopt.transform(node) File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 1223, in
transform
repl = opt.transform(node) File "D:\Anaconda2\lib\site-packages\theano\tensor\nnet\opt.py", line 153,
in local_conv2d_cpu
subsample=node.op.subsample) File "D:\Anaconda2\lib\site-packages\theano\tensor\nnet\conv.py", line 132,
in conv2d
assert image_shape[1] == filter_shape[1] AssertionError
image [None, None, None, None] filters [512, 512, 3, 3] Traceback
(most recent call last):
File "", line 1, in
runfile('E:/Deep Learning/vgg.py', wdir='E:/Deep Learning')
File
"D:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 699, in runfile
execfile(filename, namespace)
File
"D:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "E:/Deep Learning/vgg.py", line 110, in
model.fit(data,label,batch_size=100,nb_epoch=10,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2)
File "D:\Anaconda2\lib\site-packages\keras\models.py", line 402, in
fit
sample_weight=sample_weight)
File "D:\Anaconda2\lib\site-packages\keras\engine\training.py", line
999, in fit
self._make_test_function()
File "D:\Anaconda2\lib\site-packages\keras\engine\training.py", line
666, in _make_test_function
**self._function_kwargs)
File
"D:\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line
503, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File
"D:\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line
489, in init
**kwargs)
File "D:\Anaconda2\lib\site-packages\theano\compile\function.py",
line 320, in function
output_keys=output_keys)
File "D:\Anaconda2\lib\site-packages\theano\compile\pfunc.py", line
479, in pfunc
output_keys=output_keys)
File
"D:\Anaconda2\lib\site-packages\theano\compile\function_module.py",
line 1776, in orig_function
output_keys=output_keys).create(
File
"D:\Anaconda2\lib\site-packages\theano\compile\function_module.py",
line 1456, in init
optimizer_profile = optimizer(fgraph)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 101,
in call
return self.optimize(fgraph)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 89, in
optimize
ret = self.apply(fgraph, *args, **kwargs)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 230,
in apply
sub_prof = optimizer.optimize(fgraph)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 89, in
optimize
ret = self.apply(fgraph, *args, **kwargs)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 2196,
in apply
lopt_change = self.process_node(fgraph, node, lopt)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 1777,
in process_node
lopt, node)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 1673,
in warn_inplace
return NavigatorOptimizer.warn(exc, nav, repl_pairs, local_opt, node)
File "D:\Anaconda2\lib\site-packages\theano\gof\opt.py", line 1659,
in warn
raise exc
AssertionError
this is my code
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,64,64)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
return model
if weights_path:
import h5py
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')
if __name__ == "__main__":
train_data = np.empty((5800,3,64,64),dtype='float32')
train_label = np.empty((5800,),dtype="uint8")
data,label = load_data(r'E:\test\face_64_64\target\train.csv',train_data,train_label)
# Test pretrained model
label = np_utils.to_categorical(label,58)
model = VGG_16()
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(58, activation='softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy',class_mode="categorical")
model.fit(data,label,batch_size=100,nb_epoch=10,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2)
I’ve faced the same issue with keras/theano. Error was solved by upgrade theano to version 0.8.2. Please check that your theano version >= 0.8.2 and try to upgrade theano to version 0.8.2 in the opposite case . For example, with using pip install:
pip install theano==0.8.2

Resources