titu1994 / keras-coordconv

Keras implementation of CoordConv for all Convolution layers
MIT License
148 stars 33 forks source link

Error while using CoordinateChannel2D. #3

Closed ronakdedhia18 closed 6 years ago

ronakdedhia18 commented 6 years ago

Hi, I am using the following code ip=Input(shape=(3,128,128)) x = CoordinateChannel2D(data_format="channels_first")(ip) But i am getting some error. ValueError: Dimension 1 in both shapes must be equal, but are 3 and 1. Shapes are [128,3] and [?,1]. for 'coordinate_channel2d_5/concat' (op: 'ConcatV2') with input shapes: [?,128,128,3], [?,?,?,1], [?,?,?,1], [] and with computed input tensors: input[3] = <1>.

Shape of x_train is (892, 3, 128, 128) Shape of y_train is (892, 8) I have uploaded a text file of entire error which i am getting. Please let me know. Thankyou.

Error.txt

titu1994 commented 6 years ago

Yup found the bug. I was changing axis for data format channels first, but was then dim shuffling the data prior to computing the coordinates. Should probably be fixed now.

ronakdedhia18 commented 6 years ago

So should I expect an updated coord.py file from you ? Or should I make changes in my code?

titu1994 commented 6 years ago

Yeah there's an updated coord.py file. Use that and see if it works. If it doesn't, post the stacktrace here.

ronakdedhia18 commented 6 years ago

Hi, I am using this below code x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) x = Conv2D(3, (3, 3), padding='same', activation='linear')(x) x = Flatten()(x) x = Softmax()(x) I am getting some error. Also i want to add pooling layers and dense layer. My problem is a regression one. So I want to use model.add(Dense(8)) Please let me know. Attaching the error file. Error.txt

ronakdedhia18 commented 6 years ago

Hi, Can i write my model as below by passing x?

model = Sequential() ip=Input(shape=(3,128,128)) x = CoordinateChannel2D(data_format="channels_first")(ip)

model.add(Conv2D(32, (4,4), input_shape=(3,128,128),padding='same',data_format='channels_first'))(x) model.add(Activation('relu'))

model.add(Conv2D(32,(4,4),padding='same'))(x) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2)))(x) model.add(Dropout(0.25))

model.add(Conv2D(64, (4,4),padding='same'))(x) model.add(Activation('relu'))

model.add(Conv2D(64,(4,4),padding='same'))(x) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.25))

model.add(Dense(1024)) model.add(Activation('relu')) model.add(Dropout(0.25))

model.add(Dense(8))

@titu1994 please let me know your views on this as well.

Thankyou.

ronakdedhia18 commented 6 years ago

Hi, ip=Input(shape=(3,128,128)) x = CoordinateChannel2D(data_format="channels_first")(ip) print(x) gives below output Tensor("coordinate_channel2d_1/transpose_3:0", shape=(?, 5, 128, 128), dtype=float32) The shape should be rather (?,3,128,128) Please let me know.

Thank you.

titu1994 commented 6 years ago

CoordinateChannel adds indices, so the 5 in place of 3 is correct. Are you getting any error?

ronakdedhia18 commented 6 years ago

Yes i have already posted a error.txt file after using below model Error.txt

x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) x = Conv2D(3, (3, 3), padding='same', activation='linear')(x) x = Flatten()(x) x = Softmax()(x) I am getting some error. Also i want to add pooling layers and dense layer. My problem is a regression one. So I want to use model.add(Dense(8)) Please let me know. Attaching the error file.

titu1994 commented 6 years ago

Update your coord.py file and try it now.

ronakdedhia18 commented 6 years ago

Yes i will check. But can you tell me if the below model would work in this case. Hi, Can i write my model as below by passing x?

model = Sequential() ip=Input(shape=(3,128,128)) x = CoordinateChannel2D(data_format="channels_first")(ip)

model.add(Conv2D(32, (4,4), input_shape=(3,128,128),padding='same',data_format='channels_first'))(x) model.add(Activation('relu'))

model.add(Conv2D(32,(4,4),padding='same'))(x) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2)))(x) model.add(Dropout(0.25))

model.add(Conv2D(64, (4,4),padding='same'))(x) model.add(Activation('relu'))

model.add(Conv2D(64,(4,4),padding='same'))(x) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.25))

model.add(Dense(1024)) model.add(Activation('relu')) model.add(Dropout(0.25))

model.add(Dense(8))

ronakdedhia18 commented 6 years ago

Hi, I am not getting any error now. But i cant use your model. when i use in mine its showing uncallable object is called. Can you please suggest a fix @titu1994

ronakdedhia18 commented 6 years ago

Error still persists ValueError Traceback (most recent call last)

in () ----> 1 x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) 2 x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) 3 x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) 4 x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) 5 x = Flatten()(x) C:\Users\ronak.dedhia\AppData\Local\Anaconda3\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs) 573 # Raise exceptions in case the input is not compatible 574 # with the input_spec specified in the layer constructor. --> 575 self.assert_input_compatibility(inputs) 576 577 # Collect input shapes to build layer. C:\Users\ronak.dedhia\AppData\Local\Anaconda3\lib\site-packages\keras\engine\topology.py in assert_input_compatibility(self, inputs) 472 self.name + ': expected ndim=' + 473 str(spec.ndim) + ', found ndim=' + --> 474 str(K.ndim(x))) 475 if spec.max_ndim is not None: 476 ndim = K.ndim(x) ValueError: Input 0 is incompatible with layer conv2d_8: expected ndim=4, found ndim=2
titu1994 commented 6 years ago

This is not an issue with the script but with your model. Use he example model I've given to see the mistake. As far as I can see, you are mixing a Keras Model with Sequential, and this two don't work together like that. Sequential doesn't need an Input layer like Model, and instead wants the first layer to have the parameter input_shape defined there.

I don't use sequential models much, so I may be incorrect in that. Refer to the model I have in the experiments section.

ronakdedhia18 commented 6 years ago

Hi, I am getting this by appying your model @titu1994

ValueError Traceback (most recent call last)

in () ----> 1 x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) 2 x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) 3 x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) 4 x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) 5 x = Conv2D(8, (3, 3), padding='same', activation='relu')(x) C:\Users\ronak.dedhia\AppData\Local\Anaconda3\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs) 573 # Raise exceptions in case the input is not compatible 574 # with the input_spec specified in the layer constructor. --> 575 self.assert_input_compatibility(inputs) 576 577 # Collect input shapes to build layer. C:\Users\ronak.dedhia\AppData\Local\Anaconda3\lib\site-packages\keras\engine\topology.py in assert_input_compatibility(self, inputs) 472 self.name + ': expected ndim=' + 473 str(spec.ndim) + ', found ndim=' + --> 474 str(K.ndim(x))) 475 if spec.max_ndim is not None: 476 ndim = K.ndim(x) ValueError: Input 0 is incompatible with layer conv2d_7: expected ndim=4, found ndim=2