TensorFlow的代碼報錯,信息如下:
Traceback (most recent call last):
File "main3.py", line 120, in <module>
tf.app.run()
File "C:\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "main3.py", line 99, in main
data_file=FLAGS.imglist_file)
File "D:\trunk\model3.py", line 90, in __init__ self.build_model()
File "D:\trunk\model3.py", line 124, in build_model self.output_height, self.output_width, 3, self.is_crop)
File "D:\trunk\utils.py", line 30, in transform_with_tf return image/127.5 - 1
TypeError: unsupported operand type(s) for /: 'Tensor' and 'float'
查看報錯的消息可能來自于tensorflow\python\kernel_tests\matmul_op_test.py文件,看來其實是沒有實現這個除法,但真的是這樣的話。多次運行出錯的代碼,發現代碼有時候能順利運行,有時候不能運行,會上述錯誤,或者報錯如下:
Traceback (most recent call last):
File "main3.py", line 111, in <module>
tf.app.run()
File "C:\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "main3.py", line 92, in main
dcgan.train(FLAGS)
File "D: \trunk\model3.py", line 227, in train
is_grayscale=self.is_grayscale
File "D: \trunk\utils.py", line 37, in get_batch_image
img = transform_with_tf(img,input_height,input_width,resize_height,resize_wi
dth, depth, is_crop)
File "D: \trunk\utils.py", line 58, in transform_with_tf
return image/127.5 - 1.
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\math_o
ps.py", line 820, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
ops.py", line 639, in convert_to_tensor
as_ref=False)
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
ops.py", line 704, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
constant_op.py", line 113, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
constant_op.py", line 102, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=
verify_shape))
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
tensor_util.py", line 370, in make_tensor_proto
_AssertCompatible(values, dtype)
File "F:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\
tensor_util.py", line 302, in _AssertCompatible
(dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected uint8, got 127.5 of type 'float' instead.
檢查renturn image / 127.5 – 1的上一句是
image = tf.image.resize_images(image,[resize_height, resize_width])
查看tf.image.resize_images的實現源碼,找到下列這段:
if all(x is not None
for x in [new_width_const, width, new_height_const, height]) and (
width == new_width_const and height == new_height_const):
if not is_batch:
images = array_ops.squeeze(images, squeeze_dims=[0])
return images
if method == ResizeMethod.BILINEAR:
images = gen_image_ops.resize_bilinear(images,
size,
align_corners=align_corners)
意思就是如果resize設置的大小與原大小一致,就直接返回原來的數據了,而如果確實要改變圖片大小,則會根據設置的參數,調用對應的改變圖片大小的算法,在使用中發現,數據如果resize_images中改變了大小,那么不會報上述的錯誤,但是沒有改變的話,就會報上述的錯誤。檢查gen_image_ops.resize_bilinear對應源碼為ResizeBilinear的操作,在resize_bilinear_op.h文件中定義如下:
template <typename Device, typename T>
struct ResizeBilinear {
void operator()(const Device& d, typename TTypes<T, 4>::ConstTensor images,
const float height_scale, const float width_scale,
typename TTypes<float, 4>::Tensor resized_images);
};
很顯然,這里resized_images變量被轉換成了float的類型,而圖片一般的數據類型是uint8類型,這里在處理圖片的時候,進行了隱性轉換,這就導致了代碼同樣的數據,不同參數調用會出現報錯與不報錯的區別。
解決的辦法其實很簡單:
在return image/127.5 -1之前加上顯式的類型轉換tf.cast即可,例如
image = tf.cast(image, tf.float32)
最后吐槽下:
1.同樣的函數,對于數據沒有做統一的格式處理,這里是不是算一個BUG,因此要盡量的減少隱性的數據轉換。
2.報錯信息的問題,報錯是不支持的Tensor和float類型的除法操作,其實際上是不支持uint8類型的Tensor與float類型的數據的除法操作(將上述報錯的內容改成image/2-1是可以正常運行的),這里說明報錯的信息不全,導致報錯的信息容易造成誤導(以偏概全的情況)。