1.将img转为tfrecord文件
import os
import tensorflow as tf
from PIL import Image
path = './dataset/'
classes = {'daisy','dandelion','roses','sunflowers','tulips'}
writer = tf.python_io.TFRecordWriter("flowers_train.tfrecords")
for index, name in enumerate(classes):
class_path = path + name + '/'
for img_name in os.listdir(class_path):
img_path = class_path + img_name
img = Image.open(img_path)
img = img.resize((224, 224))
img_raw = img.tobytes() # 将图片转化为二进制格式
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'data': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
writer.write(example.SerializeToString())
writer.close()
2.将tfrecord转为tensor
import tensorflow as tf
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'data': tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['data'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
print(img)
label = tf.cast(features['label'], tf.int32)
print(label)
return img, label
if __name__=='__main__':
read_and_decode('flowers_train.tfrecords')
result:
Tensor("sub:0", shape=(224, 224, 3), dtype=float32)
Tensor("Cast_1:0", shape=(), dtype=int32)
注:通过以上两步即可将img输入给网络模型
3. tfrecord转为img
import tensorflow as tf
from PIL import Image
path='./result/'
filename_queue = tf.train.string_input_producer(["flowers_train.tfrecords"])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,features={'label': tf.FixedLenFeature([], tf.int64),
'data' : tf.FixedLenFeature([], tf.string),})
image = tf.decode_raw(features['data'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess:
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
#todo:按照类别文件夹读取,并保存。
for i in range(3670):
example, l = sess.run([image,label])
img=Image.fromarray(example, 'RGB')
img.save(path+str(i)+'_''Label_'+str(l)+'.jpg')
print(example, l)
coord.request_stop()
coord.join(threads)
备注:数据集获取请点击这里。