tensorflow生成多個tfrecord檔案

2021-09-10 12:40:41 字數 1372 閱讀 3625

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#總共寫入的檔案個數

instances_per_shard = 2#每個檔案中的資料個數

sess=tf.interactivesession()

cwd = "f:/寒假/google--data/新建資料夾/" #資料所在目錄位置(讀者自己去改就好了)

classes = #預先自己定義的類別,根據自己的需要修改

def _int64_feature(value):#生成整數型的屬性

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):#列舉函式

class_path = cwd + name + "/"#選取具體資料目錄

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大小設定,根據自己的需要修改

img_raw = img.tobytes()

example = tf.train.example(features=tf.train.features(feature=))

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=0writer = tf.python_io.tfrecordwriter(filename)

writer.write(example.serializetostring())#將乙個example寫入tfrecord檔案

writer.close()

tensorflow 張量生成

coding utf 8 import tensorflow as tf import numpy as np 建立張量 a tf.constant 1 5 dtype tf.int64 print a a print a.dtype a.dtype print a.shape a.shape a ...

tensorflow生成隨機數

tf.random normal shape,mean 0.0,stddev 1.0,dtype tf.float32,seed none,name none random normal 正態分佈隨機數,均值mean,標準差stddev tf.truncated normal shape,mean ...

HSSFWorkbook生成多個sheet頁

public static void exportexcel hssfworkbook workbook,int sheetnum,string sheettitle,string headers,listresult,outputstream out throws exception 遍歷集合資料...