flyyufelix / cnn_finetune

Fine-tune CNN in Keras
https://flyyufelix.github.io/2016/10/08/fine-tuning-in-keras-part2.html
MIT License
938 stars 413 forks source link

Negative dimension size ValueError #24

Open Kopsahlong opened 6 years ago

Kopsahlong commented 6 years ago

Hi Felix, thanks for the code you have provided here!

I just clone everything and am attempting to run things out of box before doing my own finetuning .. I'm running into this issue though, and am wondering if you have any insights.

I simply run 'python vgg16.py' and get this output:

Kristas-MacBook-Pro-2:cnn_finetune Krista$ python vgg19.py Using TensorFlow backend. 2017-11-10 12:30:18.227736: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA vgg19.py:29: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(64, (3, 3), activation="relu") model.add(Convolution2D(64, 3, 3, activation='relu')) vgg19.py:31: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(64, (3, 3), activation="relu") model.add(Convolution2D(64, 3, 3, activation='relu')) vgg19.py:35: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(128, (3, 3), activation="relu") model.add(Convolution2D(128, 3, 3, activation='relu')) vgg19.py:37: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(128, (3, 3), activation="relu") model.add(Convolution2D(128, 3, 3, activation='relu')) Traceback (most recent call last): File "vgg19.py", line 108, in model = vgg19_model(img_rows, img_cols, channel, num_classes) File "vgg19.py", line 38, in vgg19_model model.add(MaxPooling2D((2,2), strides=(2,2))) File "/usr/local/lib/python2.7/site-packages/keras/models.py", line 475, in add output_tensor = layer(self.outputs[0]) File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 603, in call output = self.call(inputs, **kwargs) File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 154, in call data_format=self.data_format) File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 217, in _pooling_function pool_mode='max') File "/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3456, in pool2d data_format=tf_data_format) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1958, in max_pool name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 2806, in _max_pool data_format=data_format, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2958, in create_op set_shapes_for_outputs(ret) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2209, in set_shapes_for_outputs shapes = shape_func(op) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2159, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn require_shape_fn) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,112,128].

I'm using python 2..7.14, keras 2.0.9, and tensorflow 1.4.0

Thanks in advance!

vaghelan commented 6 years ago

I am also getting the same error!!

Kopsahlong commented 6 years ago

@vaghelan I never actually got this error fixed, but I found that some of the other NN architectures (like the resnet_50 and inception_v4) worked for me instead! I'd still love to get the VGG going though, so let me know if you come up with anything that helps...

vaghelan commented 6 years ago

error for resnet_50 also

resnet_50.py:29: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(256, (1, 1), name="res4f_branch2a") x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) resnet_50.py:34: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(256, (3, 3), padding="same", name="res4f_branch2b") border_mode='same', name=conv_name_base + '2b')(x) resnet_50.py:38: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(1024, (1, 1), name="res4f_branch2c") x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) resnet_50.py:63: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (1, 1), strides=(2, 2), name="res5a_branch2a") name=conv_name_base + '2a')(input_tensor) resnet_50.py:68: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (3, 3), padding="same", name="res5a_branch2b") name=conv_name_base + '2b')(x) resnet_50.py:72: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(2048, (1, 1), name="res5a_branch2c") x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) resnet_50.py:76: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(2048, (1, 1), strides=(2, 2), name="res5a_branch1") name=conv_name_base + '1')(input_tensor) resnet_50.py:29: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (1, 1), name="res5b_branch2a") x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) resnet_50.py:34: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (3, 3), padding="same", name="res5b_branch2b") border_mode='same', name=conv_name_base + '2b')(x) resnet_50.py:38: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(2048, (1, 1), name="res5b_branch2c") x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) resnet_50.py:29: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (1, 1), name="res5c_branch2a") x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) resnet_50.py:34: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(512, (3, 3), padding="same", name="res5c_branch2b") border_mode='same', name=conv_name_base + '2b')(x) resnet_50.py:38: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(2048, (1, 1), name="res5c_branch2c") x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) Traceback (most recent call last): File "resnet_50.py", line 182, in model = resnet50_model(img_rows, img_cols, channel, num_classes) File "resnet_50.py", line 150, in resnet50_model model.load_weights(weights_path) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2532, in load_weights f = h5py.File(filepath, mode='r') File "/usr/local/lib/python2.7/dist-packages/h5py/_hl/files.py", line 271, in init fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr) File "/usr/local/lib/python2.7/dist-packages/h5py/_hl/files.py", line 101, in make_fid fid = h5f.open(name, flags, fapl=fapl) File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-nCYoKW-build/h5py/_objects.c:2840) File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-nCYoKW-build/h5py/_objects.c:2798) File "h5py/h5f.pyx", line 78, in h5py.h5f.open (/tmp/pip-nCYoKW-build/h5py/h5f.c:2117) IOError: Unable to open file (Unable to open file: name = 'imagenet_models/resnet50_weights_tf_dim_ordering_tf_kernels.h5', errno = 2, error mes sage = 'no such file or directory', flags = 0, o_flags = 0)

Kopsahlong commented 6 years ago

@vaghelan I know the fix to that problem! You just need to make sure you download the resnet50_weights_tf_dim_ordering_tf_kernels.h5 file () and place it in the imagenet_models directory. The download can be found here >> https://github.com/fchollet/deep-learning-models/releases.

vaghelan commented 6 years ago

New error :

Traceback (most recent call last): File "resnet_50.py", line 182, in model = resnet50_model(img_rows, img_cols, channel, num_classes) File "resnet_50.py", line 150, in resnet50_model model.load_weights(weights_path) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2538, in load_weights load_weights_from_hdf5_group(f, self.layers) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2970, in load_weights_from_hdf5_group K.batch_set_value(weight_value_tuples) File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2148, in batch_set_value assign_op = x.assign(assign_placeholder) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 512, in assign return state_ops.assign(self._variable, value, use_locking=use_locking) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/state_ops.py", line 270, in assign validate_shape=validate_shape) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_state_ops.py", line 47, in assign use_locking=use_locking, name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2338, in create_op set_shapes_for_outputs(ret) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1719, in set_shapes_for_outputs shapes = shape_func(op) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1669, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Dimension 2 in both shapes must be equal, but are 128 and 256 for 'Assign_78' (op: 'Assign') with input shapes: [1,1,128,512], [1,1, 256,512].

Kopsahlong commented 6 years ago

@vaghelan how many channels are you using? And is your input data still just the cifar data that was given by default?

vaghelan commented 6 years ago

I just typed : python resnet_50.py

So yes it uses same cifar data.

Kopsahlong commented 6 years ago

Not entirely sure on this issue, but given that the error is in load weights, my best guess is that the weight file is incorrect for some reason and you need to search for another one (the file is online in lots of different locations)

vaghelan commented 6 years ago

I think code is old way of doing fine tuning. With new version of keras we have to do from keras.applications

https://keras.io/applications/

Look at fine tuning section.

They have given example for Inceptionv3. They knock off top layer and add new layer with less classes.

Laicheng0830 commented 5 years ago

I am also getting the same error I'm using python 3.7, keras 2.2.4, and tensorflow 1.8.0 I modification code model.add(ZeroPadding2D((1, 1), input_shape=(channel,img_rows, img_cols))) == >> model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel))) channel last and Convolution2D modification model.add(Convolution2D(128, kernel_size=3, activation='relu',padding='same')) The code works