一. 準備原始數據
首先,我們需要準備訓練的原始數據,本次訓練爲圖像分類識別,因而一開始,筆者從網上隨機的下載了Dog的四種類別:husky,jiwawa,poodle,qiutian。每種類別30種,一共120張圖片。在訓練之前,需要做的就是進行圖像的預處理,即將這些大小不一的原始圖片轉換成我們訓練需要的shape。
下載的原始圖片分別放到同一文件的不同文件夾下,如:
二. 編程實現
該部分包括:製作Tfrecords,讀取Tfrecords數據獲得iamge和label,打印驗證並保存生成的圖片。
#將原始圖片轉換成需要的大小,並將其保存
#========================================================================================
import os
import tensorflow as tf
from PIL import Image
#原始圖片的存儲位置
orig_picture = 'E:/train_test/train_data/generate_sample/'
#生成圖片的存儲位置
gen_picture = 'E:/Re_train/image_data/inputdata/'
#需要的識別類型
classes = {'husky','jiwawa','poodle','qiutian'}
#樣本總數
num_samples = 120
#製作TFRecords數據
def create_record():
writer = tf.python_io.TFRecordWriter("dog_train.tfrecords")
for index, name in enumerate(classes):
class_path = orig_picture +"/"+ name+"/"
for img_name in os.listdir(class_path):
img_path = class_path + img_name
img = Image.open(img_path)
img = img.resize((64, 64)) #設置需要轉換的圖片大小
img_raw = img.tobytes() #將圖片轉化爲原生bytes
print (index,img_raw)
example = tf.train.Example(
features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
writer.write(example.SerializeToString())
writer.close()
#=======================================================================================
def read_and_decode(filename):
# 創建文件隊列,不限讀取的數量
filename_queue = tf.train.string_input_producer([filename])
# create a reader from file queue
reader = tf.TFRecordReader()
# reader從文件隊列中讀入一個序列化的樣本
_, serialized_example = reader.read(filename_queue)
# get feature from serialized example
# 解析符號化的樣本
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw': tf.FixedLenFeature([], tf.string)
})
label = features['label']
img = features['img_raw']
img = tf.decode_raw(img, tf.uint8)
img = tf.reshape(img, [64, 64, 3])
#img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(label, tf.int32)
return img, label
#=======================================================================================
if __name__ == '__main__':
create_record()
batch = read_and_decode('dog_train.tfrecords')
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
with tf.Session() as sess: #開始一個會話
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
for i in range(num_samples):
example, lab = sess.run(batch)#在會話中取出image和label
img=Image.fromarray(example, 'RGB')#這裏Image是之前提到的
img.save(gen_picture+'/'+str(i)+'samples'+str(lab)+'.jpg')#存下圖片;注意cwd後邊加上‘/’
print(example, lab)
coord.request_stop()
coord.join(threads)
sess.close()
#========================================================================================
運行程序,得到的結果都保存在gen_picture文件中。一方面,我們可以通過生成圖片的命名,驗證label是否與圖片對應;另一方面,我們將生成的120張圖片按照圖片命名中的label,分別放到四個不同的文件夾下,作爲後續操作的inputdata數據,如下:
此處生成的四類圖片husky,jiwawa,poodle,qiutian;其shape = 64 x 64,大小一致,一共120張。