训练CNN时DataType出错

时间:2018-06-19 10:09:04

标签: python tensorflow convolutional-neural-network

我对CNN的培训有问题。我基于可以在https://www.tensorflow.org/tutorials/layers找到的示例。我的网络与示例中的网络之间的区别在于我使用自己的数据而不是数据集。我把它们放在我之前创建并保存的numpy数组中。这就是我在代码中对待它们的方式:

train_data = np.load("train_data.npy")
train_labels = np.load("train_labels.npy")
eval_data = np.load("test_data.npy")
eval_labels = np.load("test_labels.npy")

这是我的主要开头,其余部分遵循示例(当然根据我的问题改变节点数)。 当我尝试跑步时,我得到一个很长的错误:

 python cnn_mnist.py/home/someone/tensorflow/local/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_train_distribute': None, '_is_chief':True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7fcd75bd6090>, '_evaluation_master': '', '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None,'_master': '', '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': '/tmp/mnist_convnet_model', '_global_id_in_cluster': 0, '_save_summary_steps': 100}
INFO:tensorflow:Calling model_fn.
Traceback (most recent call last):
  File "cnn_mnist.py", line 133, in <module>
    tf.app.run()
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 126, in run
    _sys.exit(main(argv))
  File "cnn_mnist.py", line 121, in main
    hooks=[logging_hook])
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 363, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 843, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 856, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 831, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "cnn_mnist.py", line 24, in cnn_model_fn
    activation=tf.nn.relu)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 621, in conv2d
    return layer.apply(inputs)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 828, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 717, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 168, in call
    outputs = self._convolution_op(inputs, self.kernel)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 868, in __call__
    return self.conv_op(inp, filter)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 520, in __call__
    return self.call(inp, filter)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 204, in __call__
    name=self.name)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 956, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 609, in _apply_op_helper
    param_name=input_name)
  File "/home/someone/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64.

我从中理解的是,某些内容的数据类型错误,但我无法理解错误。调用的函数太多,我无法追踪错误的位置。有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

即使日志中有太多的函数调用和错误,但始终可以在最后一条语句中找到错误的主要原因。在这种情况下,“ TypeError:传递给参数'input'的值的数据类型uint8不在允许的值列表中:float16,bfloat16,float32,float64。

正如错误明确指出的那样,“输入”(很可能是占位符)期望一个float对象(即,错误中允许值列表中提到的float16,bfloat16,float32,float64值之一),但是您输入的内容是整数格式(uint8)。通常,当您使用PIL或cv2读取图像时,会得到uint8格式的数组。因此,您需要转换numpy数组并将其传递给输入。

只需:

data = np.array(data).astype(np.float32)

此处,数据可以是您加载的数据的任何numpy数组。