Увеличение изображения Tensorflow путем переключения при создании примера - PullRequest
0 голосов
/ 25 сентября 2019

Поскольку у меня ограниченные тренировочные данные, я хочу дополнить свои существующие данные, чтобы получить лучшую и более надежную модель.Поскольку я не хочу сохранять каждое увеличенное изображение, я хочу увеличивать изображения при создании файлов записей tf.

Следующий код пытается решить эту проблему, но когда я запускаю код, созданные записи пусты

def create_tf_example(data,
                label_map_dict,
                image_dir,
                bucket_name,
                augmentation 
                ):
  width = int(data[['width']].values[0])
  height = int(data[['height']].values[0])
  filename = data[['filename']].values[0][0].split(".")[0].encode('utf-8')
  img_path =(image_dir + str((data[['filename']].values[0][0].split(".")[0] +".jpg"))).encode('utf-8')
  if augmentation == True:
      with tf.gfile.GFile(("images/" + data[['filename']].values[0][0].split(".")[0]+".jpg").encode('utf-8'), 'rb') as fid:
          encoded_png = tf.image.flip_left_right(fid.read())
  else:
      with tf.gfile.GFile(("images/" + data[['filename']].values[0][0].split(".")[0]+".jpg").encode('utf-8'), 'rb') as fid:
          encoded_png = fid.read()
  encoded_image_data = encoded_png # Encoded image bytes
  image_format = 'jpg'.encode('utf-8') 

  xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
  xmaxs = [] # List of normalized right x coordinates in bounding box
             # (1 per box)
  ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
  ymaxs = [] # List of normalized bottom y coordinates in bounding box
             # (1 per box)
  classes_text = [] # List of string class name of bounding box (1 per box)
  classes = [] # List of integer class id of bounding box (1 per box)
  difficult_obj = []
  for min in data['xmin']:
      difficult_obj.append(int(0))
  for min in data['xmin']:
      xmin = float(min)
      xmins.append(xmin / float(width))
  for max in data['xmax']:
      xmax = float(max)
      xmaxs.append(xmax / float(width))
  for min in data['ymin']:
      ymin = float(min)
      ymins.append(ymin / float(height))
  for max in data['ymax']:
      ymax = float(max)
      ymaxs.append(ymax / float(height))
  for name in data['class']:
      class_name = name
      classes_text.append(class_name.encode('utf8'))
      classes.append(label_map_dict[class_name])
  tf_example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(filename),
      'image/source_id': dataset_util.bytes_feature(filename),
      'image/encoded': dataset_util.bytes_feature(encoded_image_data),
      'image/format': dataset_util.bytes_feature(image_format), #
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
      'image/object/difficult': dataset_util.int64_list_feature(difficult_obj),#
  }))
  return tf_example
...