(metadata, paths, box_scale_ratio=1.2)
Generates MSCOCO examples.
Generates MSCOCO examples.
Generates MSCOCO examples .
def generate_mscoco_examples(metadata, paths, box_scale_ratio=1.2):
  """Generates MSCOCO examples."""
  if box_scale_ratio < 1.0:
    raise ValueError('Box scale ratio must be greater or equal to 1.0.')

  image_dir = paths['train2017'] / 'train2017'
  annotations = json.loads((
      paths['annotations_trainval2017'] /
      'annotations/instances_train2017.json'
  ).read_text())

  class_name_to_category_id = {
      category['name']: category['id']
      for category in annotations['categories']
  }
  coco_id_to_label = {
      class_name_to_category_id[class_name]: label
      for label, class_name in enumerate(metadata['class_names'])
  }

  label_to_annotations = collections.defaultdict(list)
  for annotation in annotations['annotations']:
    category_id = annotation['category_id']
    if category_id in coco_id_to_label:
      label_to_annotations[coco_id_to_label[category_id]].append(annotation)

  image_ids = itertools.count()

  for label, class_name in enumerate(metadata['class_names']):
    for annotation in label_to_annotations[label]:
      image_path = image_dir / f"{annotation['image_id']:012d}.jpg"

      # The bounding box is represented as (x_topleft, y_topleft, width, height)
      bbox = annotation['bbox']
      with image_path.open('rb') as f:
        image = tfds.core.lazy_imports.PIL_Image.open(f)

      # The image shape is [?, ?, 3] and the type is uint8.
      image = image.convert(mode='RGB')
      image_w, image_h = image.size

      x, y, w, h = bbox
      x = x - 0.5 * w * (box_scale_ratio - 1.0)
      y = y - 0.5 * h * (box_scale_ratio - 1.0)
      w = w * box_scale_ratio
      h = h * box_scale_ratio

      # Convert half-integer to full-integer representation.
      # The Python Imaging Library uses a Cartesian pixel coordinate system,
      # with (0,0) in the upper left corner. Note that the coordinates refer
      # to the implied pixel corners; the centre of a pixel addressed as
      # (0, 0) actually lies at (0.5, 0.5). Since COCO uses the later
      # convention and we use PIL to crop the image, we need to convert from
      # half-integer to full-integer representation.
      xmin = max(int(round(x - 0.5)), 0)
      ymin = max(int(round(y - 0.5)), 0)
      xmax = min(int(round(x + w - 0.5)) + 1, image_w)
      ymax = min(int(round(y + h - 0.5)) + 1, image_h)
      image = image.crop((xmin, ymin, xmax, ymax))
      crop_width, crop_height = image.size
      if crop_width <= 0 or crop_height <= 0:
        raise ValueError('crops are not valid.')

      buffer = io.BytesIO()
      image.save(buffer, format='JPEG')
      buffer.seek(0)
      image_bytes = buffer.getvalue()

      yield _image_key(next(image_ids), metadata['total_num_examples']), {
          'image': image_bytes,
          'format': 'JPEG',
          'filename': image_path.name,
          'label': label,
          'class_name': class_name,
      }
def generate_mscoco_examples ( metadata , paths , box_scale_ratio = 1.2 ) : if box_scale_ratio < 1.0 : raise ValueError ( 'Box scale ratio must be greater or equal to 1.0.' ) image_dir = paths [ 'train2017' ] / 'train2017' annotations = json . loads ( ( paths [ 'annotations_trainval2017' ] / 'annotations/instances_train2017.json' ) . read_text ( ) ) class_name_to_category_id = { category [ 'name' ] : category [ 'id' ] for category in annotations [ 'categories' ] } coco_id_to_label = { class_name_to_category_id [ class_name ] : label for label , class_name in enumerate ( metadata [ 'class_names' ] ) } label_to_annotations = collections . defaultdict ( list ) for annotation in annotations [ 'annotations' ] : category_id = annotation [ 'category_id' ] if category_id in coco_id_to_label : label_to_annotations [ coco_id_to_label [ category_id ] ] . append ( annotation ) image_ids = itertools . count ( ) for label , class_name in enumerate ( metadata [ 'class_names' ] ) : for annotation in label_to_annotations [ label ] : image_path = image_dir / f"{annotation['image_id']:012d}.jpg" # The bounding box is represented as (x_topleft, y_topleft, width, height) bbox = annotation [ 'bbox' ] with image_path . open ( 'rb' ) as f : image = tfds . core . lazy_imports . PIL_Image . open ( f ) # The image shape is [?, ?, 3] and the type is uint8. image = image . convert ( mode = 'RGB' ) image_w , image_h = image . size x , y , w , h = bbox x = x - 0.5 * w * ( box_scale_ratio - 1.0 ) y = y - 0.5 * h * ( box_scale_ratio - 1.0 ) w = w * box_scale_ratio h = h * box_scale_ratio # Convert half-integer to full-integer representation. # The Python Imaging Library uses a Cartesian pixel coordinate system, # with (0,0) in the upper left corner. Note that the coordinates refer # to the implied pixel corners; the centre of a pixel addressed as # (0, 0) actually lies at (0.5, 0.5). Since COCO uses the later # convention and we use PIL to crop the image, we need to convert from # half-integer to full-integer representation. xmin = max ( int ( round ( x - 0.5 ) ) , 0 ) ymin = max ( int ( round ( y - 0.5 ) ) , 0 ) xmax = min ( int ( round ( x + w - 0.5 ) ) + 1 , image_w ) ymax = min ( int ( round ( y + h - 0.5 ) ) + 1 , image_h ) image = image . crop ( ( xmin , ymin , xmax , ymax ) ) crop_width , crop_height = image . size if crop_width <= 0 or crop_height <= 0 : raise ValueError ( 'crops are not valid.' ) buffer = io . BytesIO ( ) image . save ( buffer , format = 'JPEG' ) buffer . seek ( 0 ) image_bytes = buffer . getvalue ( ) yield _image_key ( next ( image_ids ) , metadata [ 'total_num_examples' ] ) , { 'image' : image_bytes , 'format' : 'JPEG' , 'filename' : image_path . name , 'label' : label , 'class_name' : class_name , }