1
$\begingroup$

Hello I'm trying to use SegNet in my project with tensorflow, for educational purpose. And I'm surely following someone else's code on GitHub:

conv_14 = Conv2D(512, (kernel, kernel), padding="same")(unpool_1)

But it is giving me something like:

The channel dimension of the inputs should be defined. Found `None`.

How can resolve this? I checked, i've passed all the necessary params to it.

Unpooling instance is created somewhat like this:

pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)
print("Build enceder done..")

# decoder

unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

And when I tried a log in unpool_1 with simple print, it gave me:

KerasTensor(type_spec=TensorSpec(shape=(None, None, None, None), dtype=tf.float32, name=None), name='max_unpooling2d_3/max_unpooling2d_3/ScatterNd:0', description="created by layer 'max_unpooling2d_3'")

And un-pooling class looks like this:

class MaxUnpooling2D(Layer):
    def __init__(self, size=(2, 2), **kwargs):
        super(MaxUnpooling2D, self).__init__(**kwargs)
        self.size = size

    def call(self, inputs, output_shape=None):
        updates, mask = inputs[0], inputs[1]
        with K.tf.variable_scope(self.name):
            mask = K.cast(mask, "int32")
            input_shape = K.tf.shape(updates, out_type="int32")
            #  calculation new shape
            if output_shape is None:
                output_shape = (
                    input_shape[0],
                    input_shape[1] * self.size[0],
                    input_shape[2] * self.size[1],
                    input_shape[3],
                )
            self.output_shape1 = output_shape

            # calculation indices for batch, height, width and feature maps
            one_like_mask = K.ones_like(mask, dtype="int32")
            batch_shape = K.concatenate([[input_shape[0]], [1], [1], [1]], axis=0)
            batch_range = K.reshape(
                K.tf.range(output_shape[0], dtype="int32"), shape=batch_shape
            )
            b = one_like_mask * batch_range
            y = mask // (output_shape[2] * output_shape[3])
            x = (mask // output_shape[3]) % output_shape[2]
            feature_range = K.tf.range(output_shape[3], dtype="int32")
            f = one_like_mask * feature_range

            # transpose indices & reshape update values to one dimension
            updates_size = K.tf.size(updates)
            indices = K.transpose(K.reshape(K.stack([b, y, x, f]), [4, updates_size]))
            values = K.reshape(updates, [updates_size])
            ret = K.tf.scatter_nd(indices, values, output_shape)
            return ret

Reference:

Traceback looks like this:

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_36/2900577033.py in <module>
    212 
    213 
--> 214 segnet = SegNet(input_shape=(128, 128, 3), n_labels=2)

/tmp/ipykernel_36/2900577033.py in SegNet(input_shape, n_labels, kernel, pool_size, output_mode)
    148     unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])
    149 
--> 150     conv_14 = Conv2D(512, (kernel, kernel), padding="same")(unpool_1)
    151     conv_14 = BatchNormalization()(conv_14)
    152     conv_14 = Activation("relu")(conv_14)

/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    975     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    976       return self._functional_construction_call(inputs, args, kwargs,
--> 977                                                 input_list)
    978 
    979     # Maintains info about the `Layer.call` stack.

/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1113       # Check input assumptions set after layer building, e.g. input shape.
   1114       outputs = self._keras_tensor_symbolic_call(
-> 1115           inputs, input_masks, args, kwargs)
   1116 
   1117       if outputs is None:

/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
    846       return tf.nest.map_structure(keras_tensor.KerasTensor, output_signature)
    847     else:
--> 848       return self._infer_output_signature(inputs, args, kwargs, input_masks)
    849 
    850   def _infer_output_signature(self, inputs, args, kwargs, input_masks):

/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
    884           # overridden).
    885           # TODO(kaftan): do we maybe_build here, or have we already done it?
--> 886           self._maybe_build(inputs)
    887           inputs = self._maybe_cast_inputs(inputs)
    888           outputs = call_fn(inputs, *args, **kwargs)

/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2657         # operations.
   2658         with tf_utils.maybe_init_scope(self):
-> 2659           self.build(input_shapes)  # pylint:disable=not-callable
   2660       # We must set also ensure that the layer is marked as built, and the build
   2661       # shape is stored since user defined build functions may not be calling

/opt/conda/lib/python3.7/site-packages/keras/layers/convolutional.py in build(self, input_shape)
    185   def build(self, input_shape):
    186     input_shape = tf.TensorShape(input_shape)
--> 187     input_channel = self._get_input_channel(input_shape)
    188     if input_channel % self.groups != 0:
    189       raise ValueError(

/opt/conda/lib/python3.7/site-packages/keras/layers/convolutional.py in _get_input_channel(self, input_shape)
    364     channel_axis = self._get_channel_axis()
    365     if input_shape.dims[channel_axis].value is None:
--> 366       raise ValueError('The channel dimension of the inputs '
    367                        'should be defined. Found `None`.')
    368     return int(input_shape[channel_axis])

ValueError: The channel dimension of the inputs should be defined. Found `None`.
```
$\endgroup$

1 Answer 1

1
$\begingroup$

I do not see the kernel value set in your code. In Python, a variable has to have a value before being used. For example:

kernel = 3
segnet = SegNet(input_shape=(128, 128, 3), n_labels=2, kernel=kernel)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.