daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import tensorflow as tf
  3. from PIL import Image
  4. import numpy as np
  5.  
  6. data_dir = 'train/'
  7. classes={'不导电','其他','凸粉','擦花','无瑕疵样本','桔皮','横条压凹','涂层开裂','漏底','碰伤','脏点','起坑'}
  8. writer= tf.python_io.TFRecordWriter("train.tfrecords")
  9.  
  10. for index,name in enumerate(classes):
  11.     class_path = data_dir + name
  12.     for img_name in os.listdir(class_path):
  13.         img_path = os.path.join(class_path, img_name)
  14.         img = Image.open(img_path)
  15.         img = img.resize((224,224))
  16.         img_raw = img.tobytes()
  17.         example = tf.train.Example(features=tf.train.Features(feature={
  18.             "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
  19.             'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
  20.         }))
  21.         writer.write(example.SerializeToString())
  22.  
  23. writer.close()
  24.  
  25.  
  26. '''
  27.  
  28. def _int64_feature(value):
  29.     return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  30.  
  31. def _bytes_feature(value):
  32.     return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  33.    
  34. for index,name in enumerate(classes):
  35.     class_path = data_dir + name
  36.     for img_name in os.listdir(class_path):
  37.         img_path = os.path.join(class_path, img_name)
  38.         img = Image.open(img_path)
  39.         img = img.resize((224,224))
  40.         img_raw = img.tobytes()
  41.         features=tf.train.Features(feature={
  42.             "label": _int64_feature(index),
  43.             'img_raw': _bytes_feature(img_raw)
  44.         })
  45.         example = tf.train.Example(features=feature)
  46.  
  47.         writer.write(example.SerializeToString())
  48.  
  49. writer.close()
  50. '''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top