Vale
Vale

Reputation: 1

error with DataType when training CNN

I have a problem with the training of a CNN. I based it on the example that can be found at https://www.tensorflow.org/tutorials/layers. The difference between my network and the one in the example is that i am using my own data instead of a dataset. I have them in numpy arrays I created before and saved. This is how a treat them in my code:

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")

This is at the beginning of my main, for the rest it follows the example (of course changing the number of nodes according to my problem). When I try to run, I get a pretty long error:

 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.

What I understand from this is that something has the wrong data type, but I can't understand what is wrong. Too many functions are called and I can't track back where the error is. Can someone help me?

Upvotes: 0

Views: 1670

Answers (1)

End-2-End
End-2-End

Reputation: 921

Even though there are too many function calls and errors in the log, the main reason for the error can always be found in the last statement. In this case, "TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64."

As the error clearly mentions, the 'input' (most probably a placeholder), expects a float object (i.e., one of float16, bfloat16, float32, float64 values as mentioned in the list of allowed values in the error), but the input you're giving is in integer format (uint8). Generally, when you read an image using PIL or cv2, you'd get an array in uint8 format. So you need to convert your numpy arrays and pass it to the input.

Just do:

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

Here, data can be any numpy array of your data that you load.

Upvotes: 3

Related Questions