tensorflow生成多個tfrecord文件實(shí)例
更新時間:2020年02月17日 10:07:02 作者:zhx_123987
今天小編就為大家分享一篇tensorflow生成多個tfrecord文件實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
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'} #預(yù)先自己定義的類別,根據(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大小設(shè)置,根據(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()
以上這篇tensorflow生成多個tfrecord文件實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中的enumerate() 函數(shù)用法詳解
enumerate()是python的內(nèi)置函數(shù),將一個可遍歷iterable數(shù)據(jù)對象(如list列表、tuple元組或str字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在for循環(huán)當(dāng)中,這篇文章主要介紹了Python中的enumerate() 函數(shù)用法詳解,需要的朋友可以參考下2024-01-01Python實(shí)現(xiàn)爬取房源信息的示例詳解
站在一個租房人的立場,租房平臺實(shí)在太多了,并且各平臺篩選和排序邏輯都不太一致。這篇文章將教教大家如何利用Python語言實(shí)現(xiàn)爬取房源信息,需要的可以參考一下2022-09-09