I am trying to implement a "Dilated Residual Network" as described in this Paper in TensorFlow (s. PyTorch Implementation here) to train it on the CityScapes Dataset and use it for Semantic Image Segmentation. Unfortunately I get an error when trying to train and can't seem to find a fix.
Since this type of network can be seen as an extension to ResNet, I used the official TensorFlow ResNet Model (Link) and modified the architecture by changing strides, adding dilation (as a parameter in the tf.layers.conv2d function) and removing residual connections.
To train this network I wanted to use the same approach as in the TensorFlow ResNet Model: tf.estimator in combination with input_fn (can be found at the end of this post).
Now when I want to train this network with the CityScapes Dataset I get following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-263240bbee7e> in <module>()
----> 1 main()
<ipython-input-16-b57cd9b52bc7> in main()
27 print('Starting a training cycle.')
28 drn_classifier.train(
---> 29 input_fn=lambda: input_fn(True, _BATCH_SIZE, _EPOCHS_PER_EVAL),hooks=[logging_hook])
30
31 print(2)
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\estimator\estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
300
301 saving_listeners = _check_listeners_type(saving_listeners)
--> 302 loss = self._train_model(input_fn, hooks, saving_listeners)
303 logging.info('Loss for final step: %s.', loss)
304 return self
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\estimator\estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
709 with ops.control_dependencies([global_step_read_tensor]):
710 estimator_spec = self._call_model_fn(
--> 711 features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
712 # Check if the user created a loss summary, and add one if they didn't.
713 # We assume here that the summary is called 'loss'. If it is not, we will
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\estimator\estimator.py in _call_model_fn(self, features, labels, mode, config)
692 if 'config' in model_fn_args:
693 kwargs['config'] = config
--> 694 model_fn_results = self._model_fn(features=features, **kwargs)
695
696 if not isinstance(model_fn_results, model_fn_lib.EstimatorSpec):
<ipython-input-15-797249462151> in drn_model_fn(features, labels, mode, params)
7 params['arch'], params['size'], _LABEL_CLASSES, params['data_format'])
8 print(4)
----> 9 logits = network(inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN))
10 print(12)
11 predictions = {
\Code\Semantic Image Segmentation\drn.py in model(inputs, is_training)
255 print(16)
256 inputs = conv2d_fixed_padding(
--> 257 inputs=inputs, filters=16, kernel_size=7, strides=2,
258 data_format=data_format,dilation_rate=1)
259 print(17)
\Code\Semantic Image Segmentation\drn.py in conv2d_fixed_padding(inputs, filters, kernel_size, strides, data_format, dilation_rate)
90 kernel_initializer=tf.variance_scaling_initializer(),
91 data_format=data_format,
---> 92 dilation_rate=dilation_rate)
93
94
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\layers\convolutional.py in conv2d(inputs, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, reuse)
606 _reuse=reuse,
607 _scope=name)
--> 608 return layer.apply(inputs)
609
610
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\layers\base.py in apply(self, inputs, *args, **kwargs)
669 Output tensor(s).
670 """
--> 671 return self.__call__(inputs, *args, **kwargs)
672
673 def _add_inbound_node(self,
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\layers\base.py in __call__(self, inputs, *args, **kwargs)
557 input_shapes = [x.get_shape() for x in input_list]
558 if len(input_shapes) == 1:
--> 559 self.build(input_shapes[0])
560 else:
561 self.build(input_shapes)
~\Anaconda3\envs\master-thesis\lib\site-packages\tensorflow\python\layers\convolutional.py in build(self, input_shape)
130 channel_axis = -1
131 if input_shape[channel_axis].value is None:
--> 132 raise ValueError('The channel dimension of the inputs '
133 'should be defined. Found `None`.')
134 input_dim = input_shape[channel_axis].value
ValueError: The channel dimension of the inputs should be defined. Found `None`.
I already searched the web this error but only found posts in correlation with Keras, when the Backend wasn't initialized properly (s. this).
I would be glad if anyone can point me in a direction to look for bugs.
This is my input_fn:
def input_fn(is_training, batch_size, num_epochs=1):
"""Input function which provides batches for train or eval."""
# Get list of paths belonging to training images and corresponding label images
filename_list = filenames(is_training)
filenames_train = []
filenames_labels = []
for i in range(len(filename_list)):
filenames_train.append(train_dataset_dir+filename_list[i])
filenames_labels.append(gt_dataset_dir+filename_list[i])
filenames_train = tf.convert_to_tensor(tf.constant(filenames_train, dtype=tf.string))
filenames_labels = tf.convert_to_tensor(tf.constant(filenames_labels, dtype=tf.string))
dataset = tf.data.Dataset.from_tensor_slices((filenames_train,filenames_labels))
if is_training:
dataset = dataset.shuffle(buffer_size=_FILE_SHUFFLE_BUFFER)
dataset = dataset.map(image_parser)
dataset = dataset.prefetch(batch_size)
if is_training:
# When choosing shuffle buffer sizes, larger sizes result in better
# randomness, while smaller sizes have better performance.
dataset = dataset.shuffle(buffer_size=_SHUFFLE_BUFFER)
# We call repeat after shuffling, rather than before, to prevent separate
# epochs from blending together.
dataset = dataset.repeat(num_epochs)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
images, labels = iterator.get_next()
return images, labels
And this is the image_parser function used in input_fn:
def image_parser(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_image(image_string,_NUM_CHANNELS)
image_decoded = tf.image.convert_image_dtype(image_decoded, dtype=tf.float32)
label_string = tf.read_file(label)
label_decoded = tf.image.decode_image(label)
return image_decoded, tf.one_hot(label_decoded, _LABEL_CLASSES)
Try this after tf.read_file
:
image_decoded = tf.image.decode_image(image_string, channels=3)
image_decoded.set_shape([None, None, 3])