Ошибка формы при чтении tfrecords с tf.data.TFRecordDataset? - PullRequest
0 голосов
/ 29 ноября 2018

Я создал файл tfrecords со своими собственными изображениями, и когда я пытаюсь прочитать его с помощью tf.data.TFRecordDataset, появляется ошибка формы: enter image description here Я создал tfrecords с кодами:

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def img_to_tfrecord(data_path):
writer = tf.python_io.TFRecordWriter('test_imgs/test.tfrecords')

file = open('test_imgs/test.txt')
for line in file.readlines():
    img_name = line.split(' ')[0]
    label = int(line.split(' ')[1])
    img_path = data_path + '/test_imgs/' + img_name

    img = Image.open(img_path)
    img = img.resize((224, 224))
    img_bytes = img.tobytes()

    feature={'train_img': _bytes_feature(img_bytes),
             'train_label': _int64_feature(label)}
    example = tf.train.Example(features=tf.train.Features(feature=feature))
    writer.write(example.SerializeToString())

writer.close()

И прочитайте его с кодами:

def parser(record):
parsed = tf.parse_single_example(record, {'train_img': tf.FixedLenFeature((), tf.string),
                                          'train_label': tf.FixedLenFeature((), tf.int64)})

image = tf.decode_raw(parsed['train_img'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
label = tf.cast(parsed['train_label'], tf.int32)

return image, label

if __name__ == '__main__':
    os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    tf.reset_default_graph()

    dataset = tf.data.TFRecordDataset('test_imgs/test.tfrecords')
    dataset = dataset.map(parser)
    dataset = dataset.shuffle(buffer_size=6).batch(4).repeat(2)
    iterator = dataset.make_one_shot_iterator()
    img, label = iterator.get_next()

    with tf.Session() as sess:
       a,b=sess.run([img, label])
       print(a.shape)

Поскольку 150528 = 224 * 224 * 3, откуда взялся 200704? Я прочитал много уроков, но все еще могуне решить эту проблему.Я уже обратил внимание на тип синтаксического анализа кода: image = tf.decode_raw (parsed ['train_img'], tf.uint8).Любой может помочь мне, я собираюсь сломаться.Изображения, используемые для создания записей, выглядят так: enter image description here

1 Ответ

0 голосов
/ 29 ноября 2018

ОК, я решил это.О Господи.Это о самих картинах.Глубина в битах одного из изображений составляет 32. Тогда его форма будет (224 224,4).

enter image description here

...