Re: increase fit batch size: 1->2 => Incompatible shapes on model.compile(): [2,1] vs. [2,1,7,512]

1,133 views
Skip to first unread message
Message has been deleted

ro...@cgl.ucsf.edu

unread,
Aug 22, 2017, 2:37:33 AM8/22/17
to Keras-users, ro...@cgl.ucsf.edu
So much for trying to fix colors. Here again:

I'm hoping this is an obvious error. Training a siamese net, my data looks like

tr_pairs, tr_y = create_pairs(pic_dir, 'train', input_dim)

te_pairs, te_y = create_pairs(pic_dir, 'test', input_dim)


with dimensions

 TR_P (2038, 2, 224, 224, 3)

 TE_P (2266, 2, 224, 224, 3)


and

model = Model([input_a, input_b], distance)

rms = RMSprop()

model.compile(loss=contrastive_loss, optimizer=rms)

model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,

          batch_size=2,

          epochs=4,

          validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))


I get computation of 4 epochs with batch_size=1, but with batch_size=2, I get

  File "siamese.py", line 157, in <module>

    validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))

...

Caused by op u'loss/lambda_1_loss/mul', defined at:

  File "siamese.py", line 153, in <module>

    model.compile(loss=contrastive_loss, optimizer=rms)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/keras/engine/training.py", line 852, in compile

    sample_weight, mask)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/keras/engine/training.py", line 453, in weighted

    score_array = fn(y_true, y_pred)

  File "siamese.py", line 47, in contrastive_loss

    return K.mean(y_true * K.square(y_pred) +

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 865, in binary_op_wrapper

    return func(x, y, name=name)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1088, in _mul_dispatch

    return gen_math_ops._mul(x, y, name=name)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1449, in _mul

    result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op

    op_def=op_def)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op

    original_op=self._default_original_op, op_def=op_def)

  File "/Users/pp/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__

    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access


InvalidArgumentError (see above for traceback): Incompatible shapes: [2,1] vs. [2,1,7,512]

[[Node: loss/lambda_1_loss/mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_lambda_1_target_0_3, loss/lambda_1_loss/Square)]]


It seems odd that a fit parameter would cause a compile error?



ro...@cgl.ucsf.edu

unread,
Aug 23, 2017, 3:28:58 AM8/23/17
to Keras-users, ro...@cgl.ucsf.edu
Working answer: just use 1 for now.



ro...@cgl.ucsf.edu

unread,
Aug 27, 2017, 5:41:13 AM8/27/17
to Keras-users, ro...@cgl.ucsf.edu
Later on - solved dimensioning problems and this problem went away, not sure at what point. Main thing was I had to flatten the output of the inceptionv3 net to 1D to get a 1D euclidean distance.

ro...@cgl.ucsf.edu

unread,
Aug 27, 2017, 7:46:39 AM8/27/17
to Keras-users, ro...@cgl.ucsf.edu
The other thing I did is use a sigmoid on the distance, in an attempt to force the output of the net to 0/1:

processed_a = base_network(input_a)
processed_b = base_network(input_b)
flat_a = Flatten()(processed_a)
flat_b = Flatten()(processed_b) 

distance = Lambda(euclidean_distance,
                  output_shape=eucl_dist_output_shape)([flat_a, flat_b])
df = Dense(1, activation='sigmoid')(distance)
model = Model([input_a, input_b], df)


So far it only classifies as 1's, so likely I'm still doing something wrong.
Reply all
Reply to author
Forward
0 new messages