欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

tensorflow生成多个tfrecord文件实例

时间:2021-12-14 10:42:33|栏目:Python代码|点击:

我就废话不多说了,直接上代码吧!

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 = {'daisy','rose'} #预先自己定义的类别,根据自己的需要修改


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={
          '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()

上一篇:Python实现双轴组合图表柱状图和折线图的具体流程

栏    目:Python代码

下一篇:python多线程并发及测试框架案例

本文标题:tensorflow生成多个tfrecord文件实例

本文地址:http://www.codeinn.net/misctech/186798.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有