All-Conv-Keras icon indicating copy to clipboard operation
All-Conv-Keras copied to clipboard

Code execution error?

Open ChouYuWei opened this issue 7 years ago • 9 comments

hi vibrantabhi19 :

I think your article is great, so I try to execute the code you've provided. But I encountered some errors when I executed the code.

The last error message is as follows: ValueError: Dimension 0 in both shapes must be equal, but are 1 and 3 From merging shape 0 with other shapes. for 'lambda_1/concat/concat_dim' (op: 'Pack') with input shapes: [1], [3].

So I would like to ask you may cause the wrong reason.You mentioned in the project environmental requirements are as follows: Requirements: keras with Tensorflow backend (keras version 1.0.4 or later) h5py (if you want to save your model) numpy pandas (if you want to save the logs of your model) cv2 (for image resizing)

And my operating environment is as follows: Python 3.5.2 :: Anaconda 4.2.0 (64-bit) TensorFlow Version:1.1.0 Keras Version:2.0.3 Using TensorFlow backend.

Is this the cause of the error? Please help me deal with this error. Thank you.

ChouYuWei avatar May 14 '17 20:05 ChouYuWei

Hii @hugh2017 Can you post the line number in which you are getting the error? And are you trying this on CIFAR10 or on your own dataset?

iabhi7 avatar May 16 '17 18:05 iabhi7

hi ibrantabhi19 :

Thank you for your reply. I think I really use CIFAR10, see below the picture. image

The following is the whole message: Using TensorFlow backend. X_train shape: (50000, 32, 32, 3) 50000 train samples 10000 test samples (32, 32, 3) allconv.py:78: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), padding="same", input_shape=(3, 32, 32...) model.add(Convolution2D(96, 3, 3, border_mode = 'same', input_shape=(3, 32, 32))) allconv.py:80: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), padding="same") model.add(Convolution2D(96, 3, 3,border_mode='same')) allconv.py:82: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), padding="same", strides=(2, 2)) model.add(Convolution2D(96, 3, 3, border_mode='same', subsample = (2,2))) allconv.py:85: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3, border_mode = 'same')) allconv.py:87: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3,border_mode='same')) allconv.py:89: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same", strides=(2, 2)) model.add(Convolution2D(192, 3, 3,border_mode='same', subsample = (2,2))) allconv.py:92: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3, border_mode = 'same')) allconv.py:94: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (1, 1), padding="valid") model.add(Convolution2D(192, 1, 1,border_mode='valid')) allconv.py:96: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(10, (1, 1), padding="valid") model.add(Convolution2D(10, 1, 1, border_mode='valid')) Traceback (most recent call last): File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl input_tensors_as_shapes, status) File "C:\Toolkits\Anaconda3\lib\contextlib.py", line 66, in exit next(self.gen) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 1 and 3 From merging shape 0 with other shapes. for 'tower_0/lambda_1/concat/concat_dim' (op: 'Pack') with input shapes: [1], [3].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "allconv.py", line 102, in model = make_parallel(model, 4) File "allconv.py", line 37, in make_parallel slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\engine\topology.py", line 578, in call output = self.call(inputs, **kwargs) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\layers\core.py", line 659, in call return self.function(inputs, **arguments) File "allconv.py", line 19, in get_slice size = tf.concat(0, [ shape[:1] // parts, shape[1:] ]) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1029, in concat dtype=dtypes.int32).get_shape( File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 639, in convert_to_tensor as_ref=False) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 704, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 905, in _autopacking_conversion_function return _autopacking_helper(v, inferred_dtype, name or "packed") File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 868, in _autopacking_helper return gen_array_ops._pack(elems_as_tensors, name=scope) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1889, in _pack result = _op_def_lib.apply_op("Pack", values=values, axis=axis, name=name) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op op_def=op_def) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_op set_shapes_for_outputs(ret) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputs shapes = shape_func(op) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Dimension 0 in both shapes must be equal, but are 1 and 3 From merging shape 0 with other shapes. for 'tower_0/lambda_1/concat/concat_dim' (op: 'Pack') with input shapes: [1], [3].


Hope to successfully handle the problem. Thank you.

ChouYuWei avatar May 17 '17 03:05 ChouYuWei

I assume that you are running this code on your personal computer so you are having only a single GPU to play with, so a temporary hack would be remove the make_parallel function (as this function is to distribute the data onto the different GPU available and make training easier). Alternatively, you can try switching back to a previous version of keras as there are few changes in the latest 2.0 which I have not accounted for. (Maybe a PR from anyone would be highly appreciated). Also check that the image_dim_ordering is set to "tf" as in "image_dim_ordering": "tf". You can check this in keras.json file. Let me know if this solves your problem

iabhi7 avatar May 17 '17 03:05 iabhi7

hi ibrantabhi19 :

I have set keras.json to "image_dim_ordering": "tf". And I will also line this #model = make_parallel (model, 0) annotation, then execute the code and get the following error message:

Using TensorFlow backend. X_train shape: (50000, 32, 32, 3) 50000 train samples 10000 test samples (32, 32, 3) allconv.py:78: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), input_shape=(3, 32, 32..., padding="same") model.add(Convolution2D(96, 3, 3, border_mode = 'same', input_shape=(3, 32, 32))) allconv.py:80: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), padding="same") model.add(Convolution2D(96, 3, 3,border_mode='same')) allconv.py:82: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (3, 3), strides=(2, 2), padding="same") model.add(Convolution2D(96, 3, 3, border_mode='same', subsample = (2,2))) allconv.py:85: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3, border_mode = 'same')) allconv.py:87: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3,border_mode='same')) allconv.py:89: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), strides=(2, 2), padding="same") model.add(Convolution2D(192, 3, 3,border_mode='same', subsample = (2,2))) allconv.py:92: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (3, 3), padding="same") model.add(Convolution2D(192, 3, 3, border_mode = 'same')) allconv.py:94: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(192, (1, 1), padding="valid") model.add(Convolution2D(192, 1, 1,border_mode='valid')) allconv.py:96: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(10, (1, 1), padding="valid") model.add(Convolution2D(10, 1, 1, border_mode='valid'))


Layer (type) Output Shape Param #

conv2d_1 (Conv2D) (None, 3, 32, 96) 27744


activation_1 (Activation) (None, 3, 32, 96) 0


conv2d_2 (Conv2D) (None, 3, 32, 96) 83040


activation_2 (Activation) (None, 3, 32, 96) 0


conv2d_3 (Conv2D) (None, 2, 16, 96) 83040


dropout_1 (Dropout) (None, 2, 16, 96) 0


conv2d_4 (Conv2D) (None, 2, 16, 192) 166080


activation_3 (Activation) (None, 2, 16, 192) 0


conv2d_5 (Conv2D) (None, 2, 16, 192) 331968


activation_4 (Activation) (None, 2, 16, 192) 0


conv2d_6 (Conv2D) (None, 1, 8, 192) 331968


dropout_2 (Dropout) (None, 1, 8, 192) 0


conv2d_7 (Conv2D) (None, 1, 8, 192) 331968


activation_5 (Activation) (None, 1, 8, 192) 0


conv2d_8 (Conv2D) (None, 1, 8, 192) 37056


activation_6 (Activation) (None, 1, 8, 192) 0


conv2d_9 (Conv2D) (None, 1, 8, 10) 1930


global_average_pooling2d_1 ( (None, 10) 0


activation_7 (Activation) (None, 10) 0

Total params: 1,394,794 Trainable params: 1,394,794 Non-trainable params: 0


None allconv.py:135: UserWarning: Update your fit_generator call to the Keras 2 API: fit_generator(<keras.pre..., epochs=350, steps_per_epoch=1562, validation_data=(array([[[..., callbacks=[<keras.ca..., verbose=0) nb_epoch=nb_epoch, validation_data=(X_test, Y_test), callbacks=callbacks_list, verbose=0) Traceback (most recent call last): File "allconv.py", line 135, in nb_epoch=nb_epoch, validation_data=(X_test, Y_test), callbacks=callbacks_list, verbose=0) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 88, in wrapper return func(*args, **kwargs) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\models.py", line 1110, in fit_generator initial_epoch=initial_epoch) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 88, in wrapper return func(*args, **kwargs) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\engine\training.py", line 1838, in fit_generator val_x, val_y, val_sample_weight) File "C:\Toolkits\Anaconda3\lib\site-packages\keras\engine\training.py", line 1305, in _standardize_user_data exception_prefix='input') File "C:\Toolkits\Anaconda3\lib\site-packages\keras\engine\training.py", line 139, in _standardize_input_data str(array.shape)) ValueError: Error when checking input: expected conv2d_1_input to have shape (None, 3, 32, 32) but got array with shape (10000, 32, 32, 3)


Then I downgrade keras to 1.1.0 and execute python.code again, and I get the following error message: ... 2017-05-20 00:07:14.418492: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:908] DMA: 0 2017-05-20 00:07:14.422350: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:918] 0: Y 2017-05-20 00:07:14.423313: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1060 3GB, pci bus id: 0000:09:00.0) Traceback (most recent call last): File "allconv2t.py", line 83, in model.add(Dropout(0.5)) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\models.py", line 308, in add output_tensor = layer(self.outputs[0]) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\engine\topology.py", line 514, in call self.add_inbound_node(inbound_layers, node_indices, tensor_indices) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\engine\topology.py", line 572, in add_inbound_node Node.create_node(self, inbound_layers, node_indices, tensor_indices) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\engine\topology.py", line 149, in create_node output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\layers\core.py", line 90, in call x = K.in_train_phase(K.dropout(x, self.p, noise_shape), x) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.1.0-py3.5.egg\keras\backend\tensorflow_backend.py", line 1304, in in_train_phase x = tf.python.control_flow_ops.cond(tf.cast(_LEARNING_PHASE, 'bool'), AttributeError: module 'tensorflow' has no attribute 'python' Exception ignored in: <bound method BaseSession.del of <tensorflow.python.client.session.Session object at 0x0000029D2858C470>> Traceback (most recent call last): File "C:\Toolkits\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 587, in del AttributeError: 'NoneType' object has no attribute 'TF_NewStatus'

** What should I do next?

ChouYuWei avatar May 19 '17 16:05 ChouYuWei

hi vibrantabhi19 :

The latest progress, I downgrade the keras version to 1.2.1. And modify this line model.add(Convolution2D(96, 3, 3, border_mode = 'same', input_shape=(3, 32, 32))) to model.add(Convolution2D(96, 3, 3, border_mode = 'same', input_shape=(32, 32, 3)))


Then, i can use the code to perform the training. But in the end there are still small problems, Error message : Traceback (most recent call last): File "allconv.py", line 137, in im = cv2.resize(cv2.imread('image.jpg'), (224, 224)).astype(np.float32) NameError: name 'cv2' is not defined

ChouYuWei avatar May 21 '17 20:05 ChouYuWei

The above modification works because of the image_dim_ordering. Kindly refer to the keras docs for details. The NameError is because you have uninstalled dependencies. Make sure that you have openCV installed and the OpenCV library has been changed to cv2 from cv, so you have to import cv2 instead of import cv.

I just noticed that the import cv2 is missing from the code. Would really appreciate a PR with the changes made.

iabhi7 avatar May 22 '17 14:05 iabhi7

Yes, I found the CV2 problem.

At the same time, I also found that the need to import numpy as np, otherwise print (np.argmax (out)). This line will be a problem.

However, after I have finished modifying the 2 lines, execute the code again. There are new problems that appear as follows: Traceback (most recent call last): File "allconve1.py", line 13x, in out = model.predict(im) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.2.1-py3.5.egg\keras\models.py", line 724, in predict return self.model.predict(x, batch_size=batch_size, verbose=verbose) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.2.1-py3.5.egg\keras\engine\training.py", line 1250, in predict check_batch_axis=False) File "C:\Toolkits\Anaconda3\lib\site-packages\keras-1.2.1-py3.5.egg\keras\engine\training.py", line 112, in standardize_input_data str(array.shape)) ValueError: Error when checking : expected convolution2d_input_1 to have 4 dimensions, but got array with shape (224, 224, 3)

ChouYuWei avatar May 22 '17 15:05 ChouYuWei

You are training the model on CIFAR-10 and the images in CIFAR are of 3232 so you need to pass in the image with the exact same resolution. So replace the im = cv2.resize(cv2.imread('image.jpg'), (224, 224)).astype(np.float32) to im = cv2.resize(cv2.imread('image.jpg'), (image_dimensions)).astype(np.float32) where image_dimensions are the dimension of the image that you trained upon. For your particular problem, replace it with 3232 and you will be good to go.

iabhi7 avatar May 22 '17 16:05 iabhi7

I think the problem is in this line.

out = model.predict(im)

The error message is still as follows:

str(array.shape)) ValueError: Error when checking : expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

If I annotate that line, no error will occur.

ChouYuWei avatar May 22 '17 21:05 ChouYuWei