生成TFrecord
生活随笔
收集整理的這篇文章主要介紹了
生成TFrecord
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求:將圖片文件保存成Tfrecord的格式.
解決方法:基于tensorflow、cv2、numpy等庫完成該功能.
注:改編自網上代碼?
1)? 準備要訓練的手寫識別的圖片文件,并按照目錄結構存放。見下圖示意:
2)? 生成訓練圖片和標簽對應的文本文件,見下圖示意:
3) 編寫圖片生成TFrecord代碼,代碼見下:
import numpy as np import cv2 import tensorflow as tf import matplotlib.pyplot as plt from io import StringIO,BytesIO# 將value轉化成int64字節屬性 def _int64_feature(value):return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))# 將value轉化成bytes屬性 def _bytes_feature(value):return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))# 從訓練文本里讀取樣本、標簽。返回樣本、標簽列表以及行數 def load_file(examples_list_file):# type: (object) -> objectlines = np.genfromtxt(examples_list_file, delimiter=" ", dtype=[('col1', 'S120'), ('col2', 'i8')])examples = []labels = []for example, label in lines:examples.append(example)labels.append(label)return np.asarray(examples), np.asarray(labels), len(lines)def extract_image(filename, resize_height, resize_width):image = cv2.imread(filename)image = cv2.resize(image, (resize_height, resize_width))b, g, r = cv2.split(image)rgb_image = cv2.merge([r, g, b])rgb_image = rgb_image / 255.rgb_image = rgb_image.astype(np.float32)return rgb_imagedef Image2TFRecord(trainDir,trainLabelFile,tfFile):resize_height = 28resize_width = 28train_file_root = trainDirtrain_file = trainLabelFileexamples, labels, examples_num = load_file(train_file)writer = tf.python_io.TFRecordWriter(tfFile)for i, [example, label] in enumerate(zip(examples, labels)):print('No.%d' % (i))print(examples[i].decode(encoding="utf-8"))root = train_file_root + '/' + examples[i].decode(encoding="utf-8")print(root)image = extract_image(root, resize_height, resize_width)a = image.shapeprint(root)print('shape: %d, %d, %d, label: %d' % (image.shape[0], image.shape[1], image.shape[2], label))image_raw = image.tostring()? # 將Image轉化成字符example = tf.train.Example(features=tf.train.Features(feature={'image_raw': _bytes_feature(image_raw),'height': _int64_feature(image.shape[0]),'width': _int64_feature(image.shape[1]),'depth': _int64_feature(image.shape[2]),'label': _int64_feature(label)}))writer.write(example.SerializeToString())writer.close()if __name__ == '__main__':##Image2TFRecord('E:/Python/mnist_img_data4','E:/Python/mnist_img_data4/train.txt','E:/Python/mnist_img_output/a44.tfrecords')##TFrcords2Img('E:/Python/mnist_img_output/a44.tfrecords')#### 延展閱讀 https://docs.scipy.org/doc/numpy-1.10.1/user/basics.io.genfromtxt.html###指定按照定長3來分割data = "? 1? 2? 3\n? 4? 5 67\n890123? 4"print(np.genfromtxt(StringIO(data), delimiter=3))''' 結果: [[? 1.? ?2.? ?3.][? 4.? ?5.? 67.][890. 123.? ?4.]]'''### 指定按照3列來分割,第一列長度是4,第二列長度是3,第三列長度是2data = "123456789\n? ?4? 7 9\n? ?4567 9"print(np.genfromtxt(StringIO(data), delimiter=(4, 3, 2)))'''結果:[[1234.? 567.? ?89.][? ?4.? ? 7.? ? 9.][? ?4.? 567.? ? 9.]]'''data = "1, abc , 2\n 3, xxx, 4"### 默認是不自動替換空格的print(np.genfromtxt(StringIO(data), delimiter=",", dtype="|S5"))print(np.genfromtxt(StringIO(data), delimiter=",", dtype="|S5", autostrip=True)) ''' [[b'1' b' abc ' b' 2'][b'3' b' xxx' b' 4']][[b'1' b'abc' b'2'][b'3' b'xxx' b'4']]''' data = "1 2 3\n4 5 6" print(np.genfromtxt(StringIO(data), usecols=(0, -1))) ''' [[1. 3.][4. 6.]] ''' print(np.genfromtxt(StringIO(data),names="a, b, c", usecols=("b", "c"))) ''' [(2., 3.) (5., 6.)] ''' ##DataType的類型:
4)在中E:/Python/mnist_img_output目錄下生成a4.tfrecords
總結
以上是生活随笔為你收集整理的生成TFrecord的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10新系统没声音是怎么回事 win
- 下一篇: win10怎么创建管理员 Win10如何