tensorflow生成多個tfrecord文件實例
更新時間:2020年02月17日 10:07:02 作者:zhx_123987
今天小編就為大家分享一篇tensorflow生成多個tfrecord文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
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文件實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫的操作過程
這篇文章主要介紹了Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
python將字符串轉(zhuǎn)換成數(shù)組的方法
這篇文章主要介紹了python將字符串轉(zhuǎn)換成數(shù)組的方法,涉及Python操作字符串與數(shù)組的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
Python+unittest+requests+excel實現(xiàn)接口自動化測試框架
這篇文章主要介紹了Python+unittest+requests+excel實現(xiàn)接口自動化測試框架,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Pandas查詢數(shù)據(jù)df.query的使用
本文主要介紹了Pandas查詢數(shù)據(jù)df.query的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

