Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert(image_paths, labels, out_path):
- num_images = len(image_paths)
- with tf.python_io.TFRecordWriter(out_path) as writer:
- for i, (path, label) in enumerate(zip(image_paths, labels)):
- print_progress(count=i, total=num_images-1)
- img = open(path, 'rb').read()
- data ={'image': wrap_bytes(img),
- 'label': wrap_int64(label)}
- feature = tf.train.Features(feature=data)
- example = tf.train.Example(features=feature)
- serialized = example.SerializeToString()
- writer.write(serialized)
- {convert(image_paths=image_paths_train,
- labels=cls_train,
- out_path=path_tfrecords_train)}
- def parse(serialized):
- features =
- {
- 'image': tf.FixedLenFeature([], tf.string),
- 'label': tf.FixedLenFeature([], tf.int64)
- }
- parsed_example = tf.parse_single_example(serialized=serialized,
- features=features)
- # Get the image as raw bytes.
- image_raw = parsed_example['image']
- # Decode the raw bytes so it becomes a tensor with type.
- image = tf.image.decode_image(image_raw,channels=3)
- #image = tf.decode_raw(image_raw, tf.uint8)
- # The type is now uint8 but we need it to be float.
- image = tf.cast(image, tf.float32)
- # Get the label associated with the image.
- label = parsed_example['label']
- # The image and label are now correct TensorFlow types.
- return image, label
Add Comment
Please, Sign In to add comment