0
votes

I have written this code. My input shape is (100 x100 X3). I am new to deep learning. I have spent so much time on this, but couldn't resolve the issue. Any help is highly appreciated.

init = tf.random_normal_initializer(mean=0.0, stddev=0.05, seed=None)
input_image=Input(shape=image_shape)


# input: 100x100 images with 3 channels -> (3, 100, 100) tensors.
# this applies 32 convolution filters of size 3x3 each.
model=Sequential()
model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
                        padding='same', input_shape=(3,100,100)))
model.add(Activation('relu'))
model.add(Conv2D(filters=32,kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding="same"))
model.add(Dropout(0.25))

model.add(Conv2D(filters=32,kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(Conv2D(filters=32, kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding="same"))
model.add(Dropout(0.25))

model.add(Flatten())
# Note: Keras does automatic shape inference.
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
len(model.weights)
model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])

Error : In [15]: runfile('/user/Project/SM/src/ann_algo_keras.py', wdir='/user/Project/SM/src') Random starting synaptic weights: Model: "sequential_3"


Layer (type) Output Shape Param #

conv2d_12 (Conv2D) (None, 3, 100, 16) 14416


activation_18 (Activation) (None, 3, 100, 16) 0


conv2d_13 (Conv2D) (None, 3, 100, 32) 4640


activation_19 (Activation) (None, 3, 100, 32) 0


max_pooling2d_6 (MaxPooling2 (None, 2, 50, 32) 0


dropout_9 (Dropout) (None, 2, 50, 32) 0


conv2d_14 (Conv2D) (None, 2, 50, 32) 9248


activation_20 (Activation) (None, 2, 50, 32) 0


conv2d_15 (Conv2D) (None, 2, 50, 32) 9248


activation_21 (Activation) (None, 2, 50, 32) 0


max_pooling2d_7 (MaxPooling2 (None, 1, 25, 32) 0


dropout_10 (Dropout) (None, 1, 25, 32) 0


flatten_3 (Flatten) (None, 800) 0


dense_6 (Dense) (None, 256) 205056


activation_22 (Activation) (None, 256) 0


dropout_11 (Dropout) (None, 256) 0


dense_7 (Dense) (None, 10) 2570


activation_23 (Activation) (None, 10) 0

Total params: 245,178 Trainable params: 245,178 Non-trainable params: 0


Epoch 1/2000 Traceback (most recent call last):

File "/user/Project/SM/src/ann_algo_keras.py", line 272, in train(inputs,outputs,image_shape)

File "/user/Project/SM/src/ann_algo_keras.py", line 204, in train model.fit(X_train, y_train, batch_size, epochs, validation_data=(X_test, y_test), use_multiprocessing=True)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper return method(self, *args, **kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1098, in fit tmp_logs = train_function(iterator)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 780, in call result = self._call(*args, **kwds)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 823, in _call self._initialize(args, kwds, add_initializers_to=initializers)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 696, in _initialize self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2855, in _get_concrete_function_internal_garbage_collected graph_function, _, _ = self._maybe_define_function(args, kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3065, in _create_graph_function func_graph_module.func_graph_from_py_func(

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 600, in wrapped_fn return weak_wrapped_fn().wrapped(*args, **kwds)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 973, in wrapper raise e.ag_error_metadata.to_exception(e)

ValueError: in user code:

/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
    return step_function(self, iterator)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
    return fn(*args, **kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
    outputs = model.train_step(data)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:748 train_step
    loss = self.compiled_loss(
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:149 __call__
    losses = ag_call(y_true, y_pred)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:253 call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
    return target(*args, **kwargs)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:1195 mean_squared_error
    return K.mean(math_ops.squared_difference(y_pred, y_true), axis=-1)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py:10398 squared_difference
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:742 _apply_op_helper
    op = g._create_op_internal(op_type_name, inputs, dtypes=None,
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:591 _create_op_internal
    return super(FuncGraph, self)._create_op_internal(  # pylint: disable=protected-access
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:3477 _create_op_internal
    ret = Operation(
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1974 __init__
    self._c_op = _create_c_op(self._graph, node_def, inputs,
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
    raise ValueError(str(e))

ValueError: Dimensions must be equal, but are 10 and 10000 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](sequential_3/activation_23/Softmax, IteratorGetNext:1)' with input shapes: [?,10], [?,1,10000].
2

2 Answers

1
votes

Just a mix up with the position of the channels in the input shape. In Keras the input shape should be HxWxC and not CxHxW as in PyTorch.

model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
                        padding='same', input_shape=(100,100,3)))
1
votes

Your input is not in correct order, channels should be at last. So,

model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
                        padding='same', input_shape=(100,100,3)))

Also I assume you are trying to make a classification. Also some metrics are for regression, such as 'mae'. You can change them as:

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["acc"])