All-Conv-Keras
All-Conv-Keras copied to clipboard
Code execution error?
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.
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?
hi ibrantabhi19 :
Thank you for your reply.
I think I really use CIFAR10, see below the picture.
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
Hope to successfully handle the problem. Thank you.
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
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
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
** What should I do next?
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
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.
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
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.
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.