Guest User

Untitled

a guest
Jan 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. def convert(image_paths, labels, out_path):
  2.  
  3. num_images = len(image_paths)
  4.  
  5. with tf.python_io.TFRecordWriter(out_path) as writer:
  6. for i, (path, label) in enumerate(zip(image_paths, labels)):
  7.  
  8. print_progress(count=i, total=num_images-1)
  9. img = open(path, 'rb').read()
  10.  
  11. data ={'image': wrap_bytes(img),
  12. 'label': wrap_int64(label)}
  13.  
  14. feature = tf.train.Features(feature=data)
  15. example = tf.train.Example(features=feature)
  16. serialized = example.SerializeToString()
  17. writer.write(serialized)
  18.  
  19. {convert(image_paths=image_paths_train,
  20. labels=cls_train,
  21. out_path=path_tfrecords_train)}
  22.  
  23. def parse(serialized):
  24.  
  25. features =
  26. {
  27. 'image': tf.FixedLenFeature([], tf.string),
  28. 'label': tf.FixedLenFeature([], tf.int64)
  29. }
  30. parsed_example = tf.parse_single_example(serialized=serialized,
  31. features=features)
  32.  
  33. # Get the image as raw bytes.
  34. image_raw = parsed_example['image']
  35.  
  36. # Decode the raw bytes so it becomes a tensor with type.
  37. image = tf.image.decode_image(image_raw,channels=3)
  38.  
  39. #image = tf.decode_raw(image_raw, tf.uint8)
  40.  
  41. # The type is now uint8 but we need it to be float.
  42. image = tf.cast(image, tf.float32)
  43.  
  44. # Get the label associated with the image.
  45. label = parsed_example['label']
  46.  
  47. # The image and label are now correct TensorFlow types.
  48. return image, label
Add Comment
Please, Sign In to add comment