jocicmarko / ultrasound-nerve-segmentation

Deep Learning Tutorial for Kaggle Ultrasound Nerve Segmentation competition, using Keras
MIT License
939 stars 328 forks source link

Error trying to run vanilla code: ValueError ("Shapes (?,) and (?,?) must have same rank") #1

Closed thomas-keller closed 8 years ago

thomas-keller commented 8 years ago

Hi, thanks so much for releasing this code, it's already helped a bunch in helping me go from very basic MNIST models to something more sophisticated.

I'm trying to run your code now but it dies when trying to compile the model. The nvidia card I'm using has very little memory at this point, do you think that's the actual error? The simpler auto-encoder model for the MNIST dataset from http://blog.keras.io/building-autoencoders-in-keras.html works, even when I substitute Adam.

From my reading of the trace, there's the main error, but that's ultimately coming off the dice_coeff functions, though I don't really understand why they would.

I tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 with properties: name: GeForce GT 755M major: 3 minor: 0 memoryClockRate (GHz) 1.0195 pciBusID 0000:01:00.0 Total memory: 2.00GiB Free memory: 1.65GiB I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0: Y I tensorflow/core/common_runtime/gpu/gpu_device.cc:806] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GT 755M, pci bus id: 0000:01:00.0)


Loading and preprocessing train data...


Creating and compiling model...


ValueError Traceback (most recent call last) /usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other) 562 try: --> 563 self.assert_same_rank(other) 564 new_dims = []

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other) 608 raise ValueError( --> 609 "Shapes %s and %s must have the same rank" % (self, other)) 610

ValueError: Shapes (?,) and (?, ?) must have the same rank

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) /usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank) 638 try: --> 639 return self.merge_with(unknown_shape(ndims=rank)) 640 except ValueError:

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other) 569 raise ValueError("Shapes %s and %s are not compatible" % --> 570 (self, other)) 571

ValueError: Shapes (?,) and (?, ?) are not compatible

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) /notebooks/ultrasound-nerve-segmentation/train.py in () 134 135 if name == 'main': --> 136 train_and_predict()

/notebooks/ultrasound-nerve-segmentation/train.py in train_and_predict() 102 print('Creating and compiling model...') 103 print('-'*30) --> 104 model = get_unet() 105 model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss', save_best_only=True) 106

/notebooks/ultrasound-nerve-segmentation/train.py in get_unet() 68 model = Model(input=inputs, output=conv10) 69 ---> 70 model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef]) 71 72 return model

/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, **kwargs) 587 loss_weight = loss_weights_list[i] 588 output_loss = weighted_loss(y_true, y_pred, --> 589 sample_weight, mask) 590 if len(self.outputs) > 1: 591 self.metrics.append(output_loss)

/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in weighted(y_true, y_pred, weights, mask) 309 def weighted(y_true, y_pred, weights, mask=None): 310 # score_array has ndim >= 2 --> 311 score_array = fn(y_true, y_pred) 312 if mask is not None: 313 # Cast the mask to floatX to avoid float64 upcasting in theano

/notebooks/ultrasound-nerve-segmentation/train.py in dice_coef_loss(y_true, y_pred) 24 25 def dice_coef_loss(y_true, y_pred): ---> 26 return -dice_coef(y_true, y_pred) 27 28

/notebooks/ultrasound-nerve-segmentation/train.py in dice_coef(y_true, y_pred) 20 y_true_f = K.flatten(y_true) 21 y_pred_f = K.flatten(y_pred) ---> 22 return (2. * K.dot(y_true_f, K.transpose(y_pred_f)) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) 23 24

/usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py in dot(x, y) 212 yt = tf.reshape(tf.transpose(y, perm=y_permute_dim), [y_shape[-2], -1]) 213 return tf.reshape(tf.matmul(xt, yt), x_shape[:-1] + y_shape[:-2] + y_shape[-1:]) --> 214 out = tf.matmul(x, y) 215 return out 216

/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, a_is_sparse, b_is_sparse, name) 1207 transpose_a=transpose_a, 1208 transpose_b=transpose_b, -> 1209 name=name) 1210 1211 sparse_matmul = gen_math_ops._sparse_mat_mul

/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/gen_math_ops.py in _mat_mul(a, b, transpose_a, transpose_b, name) 1176 """ 1177 result = _op_def_lib.apply_op("MatMul", a=a, b=b, transpose_a=transpose_a, -> 1178 transpose_b=transpose_b, name=name) 1179 return result 1180

/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/op_def_library.py in apply_op(self, op_type_name, name, **keywords) 702 op = g.create_op(op_type_name, inputs, output_types, name=scope, 703 input_types=input_types, attrs=attr_protos, --> 704 op_def=op_def) 705 outputs = op.outputs 706 return _Restructure(ops.convert_n_to_tensor(outputs),

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device) 2260 original_op=self._default_original_op, op_def=op_def) 2261 if compute_shapes: -> 2262 set_shapes_for_outputs(ret) 2263 self._add_op(ret) 2264 self._record_op_seen_by_control_dependencies(ret)

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op) 1700 raise RuntimeError("No shape function registered for standard op: %s" 1701 % op.type) -> 1702 shapes = shape_func(op) 1703 if shapes is None: 1704 raise RuntimeError(

/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/common_shapes.py in matmul_shape(op) 84 def matmul_shape(op): 85 """Shape function for a MatMul op.""" ---> 86 a_shape = op.inputs[0].get_shape().with_rank(2) 87 transpose_a = op.get_attr("transpose_a") 88 b_shape = op.inputs[1].get_shape().with_rank(2)

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank) 639 return self.merge_with(unknown_shape(ndims=rank)) 640 except ValueError: --> 641 raise ValueError("Shape %s must have rank %d" % (self, rank)) 642 643 def with_rank_at_least(self, rank):

ValueError: Shape (?,) must have rank 2

jocicmarko commented 8 years ago

Hi Thomas, thanks for reporting this - it seems to be the issue when running the code with TF backend (I'm still using Theano). Will check it out and get back to you.

jocicmarko commented 8 years ago

e1433994f38cbaeb44e261169bc5dc6885474787 should fix it. You can pull the repo and it should work now.