(class_npy_file, class_label,
                                           output_path)
return len(imgs)
Create and write a tf.record file for the data of a class.

  This assumes that the provided .npy file stores the data of a given class in
  an array of shape [num_images_of_given_class, side**2].
  In the case of the Quickdraw dataset for example, side = 28.
  Each row of that array is interpreted as a single-channel side x side image,
  read into a PIL.Image, converted to RGB and then written into a record.
  Args:
    class_npy_file: the .npy file of the images of class class_label.
    class_label: the label of the class that a Record is being made for.
    output_path: the location to write the Record.

  Returns:
    The number of images in the .npy file for class class_label.
Create and write a tf.record file for the data of a class.
Create and write a tf . record file for the data of a class .
def write_tfrecord_from_npy_single_channel(class_npy_file, class_label,
                                           output_path):
  """Create and write a tf.record file for the data of a class.

  This assumes that the provided .npy file stores the data of a given class in
  an array of shape [num_images_of_given_class, side**2].
  In the case of the Quickdraw dataset for example, side = 28.
  Each row of that array is interpreted as a single-channel side x side image,
  read into a PIL.Image, converted to RGB and then written into a record.
  Args:
    class_npy_file: the .npy file of the images of class class_label.
    class_label: the label of the class that a Record is being made for.
    output_path: the location to write the Record.

  Returns:
    The number of images in the .npy file for class class_label.
  """

  def load_image(img):
    """Load image img.

    Args:
      img: a 1D numpy array of shape [side**2]

    Returns:
      a PIL Image
    """
    # We make the assumption that the images are square.
    side = int(np.sqrt(img.shape[0]))
    # To load an array as a PIL.Image we must first reshape it to 2D.
    img = Image.fromarray(img.reshape((side, side)))
    img = img.convert('RGB')
    return img

  with tf.io.gfile.GFile(class_npy_file, 'rb') as f:
    imgs = np.load(f)

  # If the values are in the range 0-1, bring them to the range 0-255.
  if imgs.dtype == np.bool:
    imgs = imgs.astype(np.uint8)
    imgs *= 255

  writer = tf.python_io.TFRecordWriter(output_path)
  # Takes a row each time, i.e. a different image (of the same class_label).
  for image in imgs:
    img = load_image(image)
    # Compress to JPEG before writing
    buf = io.BytesIO()
    img.save(buf, format='JPEG')
    buf.seek(0)
    write_example(buf.getvalue(), class_label, writer)

  writer.close()
  return len(imgs)
def write_tfrecord_from_npy_single_channel ( class_npy_file , class_label , output_path ) : def load_image ( img ) : """Load image img.

    Args:
      img: a 1D numpy array of shape [side**2]

    Returns:
      a PIL Image
    """ # We make the assumption that the images are square. side = int ( np . sqrt ( img . shape [ 0 ] ) ) # To load an array as a PIL.Image we must first reshape it to 2D. img = Image . fromarray ( img . reshape ( ( side , side ) ) ) img = img . convert ( 'RGB' ) return img with tf . io . gfile . GFile ( class_npy_file , 'rb' ) as f : imgs = np . load ( f ) # If the values are in the range 0-1, bring them to the range 0-255. if imgs . dtype == np . bool : imgs = imgs . astype ( np . uint8 ) imgs *= 255 writer = tf . python_io . TFRecordWriter ( output_path ) # Takes a row each time, i.e. a different image (of the same class_label). for image in imgs : img = load_image ( image ) # Compress to JPEG before writing buf = io . BytesIO ( ) img . save ( buf , format = 'JPEG' ) buf . seek ( 0 ) write_example ( buf . getvalue ( ) , class_label , writer ) writer . close ( ) return len ( imgs )