TensorFlow ValueError:应定义输入的通道维度。找到了“无”

时间:2018-01-15 14:08:06

标签: python tensorflow machine-learning deep-learning resnet

我正在尝试按照TensorFlow中的Paper(s.PyTorch Implementation here)中的描述实现“扩张的剩余网络”,以便在CityScapes Dataset上对其进行训练,并将其用于语义图像分割。不幸的是,我在尝试训练时遇到错误,似乎无法找到解决办法。

由于这种类型的网络可以看作是ResNet的扩展,我使用官方的TensorFlow ResNet模型(Link)并通过改变步幅,添加扩张(作为tf.layers中的参数)来修改架构.conv2d功能)并删除剩余连接。

为了训练这个网络,我想使用与TensorFlow ResNet模型中相同的方法:tf.estimator与input_fn结合使用(可以在本文末尾找到)。

现在,当我想使用CityScapes数据集训练此网络时,我收到以下错误:

---------------------------------------------------------------------------
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`.

我已经在网上搜索了这个错误,但只发现了与Keras相关的帖子,当Backend没有正确初始化时(s。this)。

如果有人能指出我寻找错误的方向,我会很高兴。

这是我的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

这是input_fn中使用的image_parser函数:

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)

2 个答案:

答案 0 :(得分:4)

tf.read_file

之后尝试此操作
image_decoded = tf.image.decode_image(image_string, channels=3)
image_decoded.set_shape([None, None, 3])

答案 1 :(得分:0)

问题在于tf.image.decode_image。不管怎么说,虽然你通过它,但它并没有设置频道。

如果您知道数据集中的图片类型,请使用tf.image.decode_image等适当的解码器替换tf.image.decode_png