激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取

tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取

2021-09-30 00:01wxsy024680 Python

這篇文章主要介紹了tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

訓(xùn)練模型時(shí),我們并不是直接將圖像送入模型,而是先將圖像轉(zhuǎn)換為tfrecord文件,再將tfrecord文件送入模型。為進(jìn)一步理解tfrecord文件,本例先將6幅圖像及其標(biāo)簽轉(zhuǎn)換為tfrecord文件,然后讀取tfrecord文件,重現(xiàn)6幅圖像及其標(biāo)簽。
1、生成tfrecord文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import numpy as np
import tensorflow as tf
from PIL import Image
 
filenames = [
'images/cat/1.jpg',
'images/cat/2.jpg',
'images/dog/1.jpg',
'images/dog/2.jpg',
'images/pig/1.jpg',
'images/pig/2.jpg',]
 
labels = {'cat':0, 'dog':1, 'pig':2}
 
def int64_feature(values):
    if not isinstance(values, (tuple, list)):
        values = [values]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=values))
 
def bytes_feature(values):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))
 
with tf.Session() as sess:
    output_filename = os.path.join('images/train.tfrecords')
    with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
        for filename in filenames:
            #讀取圖像
            image_data = Image.open(filename)
            #圖像灰度化
            image_data = np.array(image_data.convert('L'))
            #將圖像轉(zhuǎn)化為bytes
            image_data = image_data.tobytes()
            #讀取label
            label = labels[filename.split('/')[-2]]
            #生成protocol數(shù)據(jù)類型
            example = tf.train.Example(features=tf.train.Features(feature={'image': bytes_feature(image_data),
                                                                            'label': int64_feature(label)}))
            tfrecord_writer.write(example.SerializeToString())

2、讀取tfrecord文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
 
# 根據(jù)文件名生成一個(gè)隊(duì)列
filename_queue = tf.train.string_input_producer(['images/train.tfrecords'])
reader = tf.TFRecordReader()
# 返回文件名和文件
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
                                    features={'image': tf.FixedLenFeature([], tf.string),
                                                'label': tf.FixedLenFeature([], tf.int64)})
# 獲取圖像數(shù)據(jù)
image = tf.decode_raw(features['image'], tf.uint8)
# 恢復(fù)圖像原始尺寸[高,寬]
image = tf.reshape(image, [60, 160])
# 獲取label
label = tf.cast(features['label'], tf.int32)
 
with tf.Session() as sess:
    # 創(chuàng)建一個(gè)協(xié)調(diào)器,管理線程
    coord = tf.train.Coordinator()
    # 啟動(dòng)QueueRunner, 此時(shí)文件名隊(duì)列已經(jīng)進(jìn)隊(duì)
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
 
    for i in range(6):
        image_b, label_b = sess.run([image, label])
        img = Image.fromarray(image_b, 'L')
        plt.imshow(img)
        plt.axis('off')
        plt.show()
        print(label_b)
 
    # 通知其他線程關(guān)閉
    coord.request_stop()
    # 其他所有線程關(guān)閉之后,這一函數(shù)才能返回
    coord.join(threads)

到此這篇關(guān)于tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取的文章就介紹到這了,更多相關(guān)tfrecord文件的生成與讀取內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/wxsy024680/article/details/115291692

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人午夜影院 | 欧美成人精品一区二区男人小说 | 91看片王 | 国产成人综合在线观看 | 美女扒开腿让男生桶爽网站 | 91成人免费视频 | 欧美激情在线播放 | 亚洲性生活免费视频 | 国产美女的小嫩bbb图片 | 综合国产在线 | 特级毛片a级毛片100免费 | 成人啪啪18免费网站 | 欧美日韩亚洲另类 | 男女一边摸一边做羞羞视频免费 | 精品亚洲一区二区 | 欧洲成人一区二区 | 97porn| 久久久久99999 | av噜噜噜噜 | 亚洲视频综合网 | 红杏网站永久免费视频入口 | 在线看免费的a | 黄色网址在线视频 | 爱逼av | 欧美18videos性处按摩 | 国产成人在线网站 | 国内精品久久久久久久久久 | 日韩在线激情 | 国产深夜福利视频在线播放 | 精品在线免费播放 | 182tv成人福利视频免费看 | 成人午夜一区二区 | 精品69人人人人 | 久久精品国产清自在天天线 | 亚洲特黄a级毛片在线播放 激情视频免费看 | 午夜视频在线观看91 | 暖暖免费观看高清完整版电影 | 欧美一级毛片特黄黄 | 一色桃子av大全在线播放 | 欧美精品一级 | 免费a级网站 |