我就廢話不多說了,直接上代碼吧!
import tensorflow as tf from PIL import Image import matplotlib.pyplot as plt import numpy as np import os i = 0 j = 0 num_shards = 100#總共寫入的文件個數(shù) instances_per_shard = 2#每個文件中的數(shù)據(jù)個數(shù) sess=tf.InteractiveSession() cwd = "F:/寒假/google--data/新建文件夾/" #圖片數(shù)據(jù)所在目錄位置(讀者自己去改就好了) classes = {'daisy','rose'} #預先自己定義的類別,根據(jù)自己的需要修改 def _int64_feature(value):#生成整數(shù)型的屬性 return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes_feature(value):#生成字符串型的屬性 return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) for index, name in enumerate(classes):#枚舉函數(shù) class_path = cwd + name + "/"#選取具體數(shù)據(jù)目錄 for img_name in os.listdir(class_path):#遍歷文件列表 img_path = class_path + img_name#圖片路徑 img = Image.open(img_path) img = img.resize((299, 299)) #圖像reshape大小設置,根據(jù)自己的需要修改 img_raw = img.tobytes() example = tf.train.Example(features=tf.train.Features(feature={ 'label': _int64_feature(index), 'img_raw': _bytes_feature(img_raw), 'i': _int64_feature(i), 'j': _int64_feature(j) })) filename = ("F:/寒假/google--data/data.tfrecords-%.5d-of-%.5d"%(i,num_shards)) if j == instances_per_shard-1: i+=1 j+=1 if j == instances_per_shard: j=0 writer = tf.python_io.TFRecordWriter(filename) writer.write(example.SerializeToString())#將一個example寫入tfrecord文件 writer.close()
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。