1. 程式人生 > >TypeError: 'RGB' has type str, but expected one of: bytes

TypeError: 'RGB' has type str, but expected one of: bytes

在使用Python3+(本人Python3.5)執行21個專案玩轉深度學習第三個專案的時候。在執行資料轉換操作

python data_convert.py -t pic/ \
  --train-shards 2 \
  --validation-shards 2 \
  --num-threads 2 \
  --dataset-name satellite

出現了 ‘’TypeError: 'RGB' has type str, but expected one of: bytes‘’,原因是Python2+與Python3+使用格式不對。經以下修改成功執行資料轉換。

colorspace = 'RGB'.encode()
channels = 3
image_format = 'JPEG'.encode()

example = tf.train.Example(features=tf.train.Features(feature={
    'image/height': _int64_feature(height),
    'image/width': _int64_feature(width),
    'image/colorspace': _bytes_feature(colorspace),
    'image/channels': _int64_feature(channels),
    'image/class/label': _int64_feature(label),
    'image/class/text': _bytes_feature(text.encode()),
    'image/format': _bytes_feature(image_format),
    'image/filename': _bytes_feature(os.path.basename(filename.encode())),
    'image/encoded': _bytes_feature(image_buffer)}))
return example

INFO:root:2018-11-27 17:45:23.217677 [thread 0]: Processed 1000 of 2400 images in thread batch.
INFO:root:2018-11-27 17:45:23.219677 [thread 1]: Processed 1000 of 2400 images in thread batch.
INFO:root:2018-11-27 17:45:24.021429 [thread 0]: Processed 2000 of 2400 images in thread batch.
INFO:root:2018-11-27 17:45:24.025384 [thread 1]: Processed 2000 of 2400 images in thread batch.
INFO:root:2018-11-27 17:45:24.336195 [thread 0]: Wrote 2400 images to pic/satellite_train_00000-of-00002.tfrecord
INFO:root:2018-11-27 17:45:24.336302 [thread 0]: Wrote 2400 images to 2400 shards.
INFO:root:2018-11-27 17:45:24.348348 [thread 1]: Wrote 2400 images to pic/satellite_train_00001-of-00002.tfrecord
INFO:root:2018-11-27 17:45:24.348439 [thread 1]: Wrote 2400 images to 2400 shards.
INFO:root:2018-11-27 17:45:24.407238: Finished writing all 4800 images in data set.