Как прочитать точку данных с несколькими метками из файла tfrecord - PullRequest
0 голосов
/ 04 января 2019

Я пишу данные с несколькими метками для каждого изображения, в этом случае ограничивающий прямоугольник и классификационные метки, и использую следующую функцию для записи данных в tfrecord:

   def tfr_write_sr(data_split_name,save_dir, label_array, data_array):

       filename = os.path.join(save_dir, data_split_name + '.tfrecords')
       writer = tf.python_io.TFRecordWriter(filename)
       for index in range(data_array.shape[0]):

       image = data_array[index].tostring()
       example = tf.train.Example(features=tf.train.Features(
        feature={
            'height': tf.train.Feature(
                int64_list=tf.train.Int64List(
                    value=[data_array.shape[1]])),
            'width': tf.train.Feature(
                int64_list=tf.train.Int64List(
                    value=[data_array.shape[2]])),
            'depth': tf.train.Feature(
                int64_list=tf.train.Int64List(
                    value=[data_array.shape[3]])),
            'shape_type': tf.train.Feature(
                    int64_list=tf.train.Int64List(
                        value=[int(label_array[index][3])])),
            'bbtl_x': tf.train.Feature(
                    int64_list=tf.train.Int64List(
                        value=[int(label_array[index][1][0])])),
            'bbtl_y': tf.train.Feature(
                    int64_list=tf.train.Int64List(
                        value=[int(label_array[index][1][1])])),
            'bbbr_x': tf.train.Feature(
                    int64_list=tf.train.Int64List(
                        value=[int(label_array[index][0][0])])),
            'bbbr_y': tf.train.Feature(
                    int64_list=tf.train.Int64List(
                        value=[int(label_array[index][0][1])])),                
            'image_raw': tf.train.Feature(
                bytes_list=tf.train.BytesList(
                    value=[image]))}))
         writer.write(example.SerializeToString())
       writer.close() 

Я проверил, чтозапись пишется правильно, но все примеры, которые я видел прежде, читают только одну метку на изображение, как мне читать несколько меток?

1 Ответ

0 голосов
/ 04 января 2019

Сначала мы читаем в нашей tfrecord и получаем его функции:

  reader = tf.TFRecordReader()
  _ , serialized_example = reader.read(filename_queue)

   features = tf.parse_single_example(serialized_example, 
        features={
            'image_raw': tf.FixedLenFeature([],tf.string),
            'shape_type' : tf.FixedLenFeature([], tf.int64),
            'bbtl_x' : tf.FixedLenFeature([], tf.int64),
            'bbtl_y' : tf.FixedLenFeature([], tf.int64),
            'bbbr_x' : tf.FixedLenFeature([], tf.int64),
            'bbbr_y' : tf.FixedLenFeature([], tf.int64)
    })

Теперь у нас есть наши функции, которые мы можем использовать tf.stack (), чтобы построить тензор для наших мультилаблов и добавить его в наш график:

     label  = tf.stack([features['shape_type'],
                        features['bbtl_x'],
                        features['bbtl_y'],
                        features['bbbr_x'],
                        features['bbbr_y'] ], axis=0 )


      image = tf.decode_raw(features['image_raw'], tf.uint8)

      images_batch, labels_batch = tf.train.shuffle_batch([image,label],
                                                 batch_size=128,
                                                 capacity=2000,
                                                 min_after_dequeue=1000) 
...