python3__深度学习:TensorFlow__数据的生成与读取(主要针对图像处理)

1.CSV文件(提供图像地址和标签)

1.1 创建

import os


path = "pic"
filenames = os.listdir(path=path)
strText = ""

with open(file="train_list.csv", mode="w") as fid:
    for a in range(len(filenames)):
        # 获取图片的地址和标签
        strText = path+os.sep+filenames[a]+","+filenames[a].split(sep="_")[0]+"\n"
        fid.write(strText)

1.1.1 os.listdir(path)

os.listdir(*args, **kwargs):返回指定路径下文件名称的列表

【注意】

①列表中并不包括“.”和“..”

②列表可以为字节,此时列表中的文件名也为字节;其他情况下,文件名为字符串

③path=None,则默认为当前程序所在的路径

④列表中的文件名为乱序排列

import os


def getfilenames(path):
    filenames = os.listdir(path)

    for name in filenames:
        print(name)


if "__main__" == __name__:
    getfilename(r"E:\个人项目\11 人脸识别\train")


# 打印结果
['s_0','s_1','s_10','s_11','s_12','s_13','s_14','s_15','s_16', 's_17', 's_18', 's_19', 's_2', 's_20', 's_21', 's_22', 's_23', 's_24', 's_25', 's_26', 's_27', 's_28', 's_29', 's_3', 's_30', 's_31', 's_32', 's_33', 's_34', 's_35', 's_36', 's_37', 's_38', 's_39', 's_4', 's_5', 's_6', 's_7', 's_8', 's_9']

1.1.2 os.walk(path)

os.walk(top, topdown=True, οnerrοr=None, followlinks=False):生成给定路径树结构的生成器。返回的是由三个列表元素的元组。([dirpath], [dirnames], [filenames])

top:路径字符串

onerror:指定错误(应当赋值为一个函数)

followlinks:是否支持符号链接

【注意】

①dirpath(目录路径的字符串)dirnames(路径下子目录名称的列表)filenames(对应子目录中的文件名列表)

②os.path.join(dirpath, filenames)可以生成对应文件的绝对路径

③os.path.splitext(filename)可以将文件名称划分为“根+扩展名(最后一个点及后边的部分)”

def getAllPath(dirpath, *suffix):
    """
    获取文件夹下各图片路径
    :param dirpath: 文件路径
    :param suffix:  图片格式
    :return:
    """
    PathArray = []

    # 遍历文件树
    for r, ds, fs in os.walk(dirpath):
        # 生成文件绝对路径
        for fn in fs:
            if os.path.splitext(fn)[1] in suffix:
                fname = os.path.join(r, fn)
                PathArray.append(fname)
    return PathArray

1.2 读取

import os
import cv2
import tensorflow as tf


# img_add_list = []
# img_label_list = []
# with open("train_list.csv") as f:
#     for img in f.readlines():
#         # strip:移出空格
#         img_add_list.append(img.strip().split(sep=",")[0])
#         img_label_list.append(img.strip().split(sep=",")[1])
#
# # tf.image.decode_jpeg:将jpeg编码的图片解码成jpg格式
# # tf.image.convert_image_dtype: 对图像进行转换,将图像矩阵转化成TensorFlow需要的张量格式
# img = tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.read_file("pic\\1_0.jpg"), channels=1), dtype=tf.float32)
# print(img)

img_add_list = []
img_label_list = []
with open("train_list.csv", "r") as f:
    for img in f.readlines():
        img_add_list.append(img.strip().split(",")[0])
        img_label_list.append(img.strip().split(sep=",")[1])


def get_img(img_path):
    # 颜色通道
    # channels=0,1,3 or 4
    return tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.read_file(img_path), channels=1), dtype=tf.float32)


with tf.Session() as sess:
    cv2Img = sess.run(get_img("pic\\1_1.jpg"))
    img2 = cv2.resize(cv2Img, (200, 200))
    cv2.imshow("img2", img2)
    cv2.waitKey()

2.TFRecords文件(提供图像特征与标签)

TFRecords是TensorFlow专用的数据文件格式。其中包含了tf.train.Example协议内存块,其是包含特征值和数据内容的一种数据格式。通过tf.python_io.TFRecordWriter类,可以获取相应的数据并将其填入Example协议内存块中,最终生成TFRecords文件。

换句话说,tf.train.Example包含着若干数据特征(Features),而Features中又包含Feature字典。更进一步的说明,任何一个Feature中又包含着FloatList,或者ByteList,或者Int64List,这三种数据格式之一。TFRecords就是通过一个包含着二进制文件的数据文件,将特征和标签进行保存,以便于TensorFlow读取

2.1 TFRecords文件写入

tf.python_io.TFRecordWriter(path, options):仅能写入二进制数据

import tensorflow as tf
import numpy as np


# 创建TFRecords类型文件写操作的对象
writer = tf.python_io.TFRecordWriter("trainArray.tfrecords", options="error")

for i in range(100):
    randomArray = np.random.random((1, 3))

    # 将数组转换成二进制
    array_raw = randomArray.tobytes()

    example = tf.train.Example(features=tf.train.Features(feature={"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[0])),
                                                                   "img_raw": tf.train.Feature(bytes_list=tf.train.BytesList(value=[array_raw]))}))

    # tf.train.Example().SerializeToString: 将协议消息数据转换成二进制字符串
    writer.write(example.SerializeToString())
writer.close()

2.2 TFRecords文件读取

import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"


# 将[文件名]输出到输入管道的队列
filename_queue = tf.train.string_input_producer(string_tensor=["trainArray.tfrecords"], num_epochs=None)

# 创建tfrecords文件读取对象
# reader是符号化的,只有在sess中才能执行
reader = tf.TFRecordReader()

key, serialized_example = reader.read(filename_queue)

# tf.parse_single_example: 解释单一内存块
# tf.FixedLenFeatures(): 解析固定长度的输入特征
features = tf.parse_single_example(serialized_example, features={"label": tf.FixedLenFeature([], tf.int64),
                                                                 "img_raw": tf.FixedLenFeature([], tf.string)})
label = features["label"]
img_raw = features["img_raw"]

# tf.decode_raw: 将string的字节重新解释为数字vector(数字向量)
img = tf.decode_raw(img_raw, tf.uint8)
img = tf.reshape(img, [3, 8])

# tf.train.shuffle: 通过随机填充张量创建批次
label_batch, img_batch = tf.train.shuffle_batch([label, img], batch_size=1, capacity=200, min_after_dequeue=100, num_threads=6)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

coord = tf.train.Coordinator()
# tf.train.start_queue_runners: 启动图中所有的队列运行程序
tf.train.start_queue_runners(sess=sess, coord=coord)

label_val, img_val = sess.run([label_batch, img_batch])
print(label_val)
print("*" * 100)
print(img_val)

3. 图片文件的创建与读取

图片存储和命名形式如下:

3.1 图片文件的创建

# 创建
import tensorflow as tf
import os
from PIL import Image


path = "pic"
filenames = os.listdir(path=path)
writer = tf.python_io.TFRecordWriter("train.tfrecords")

for name in filenames:
    class_path = path + os.sep + name
    for img_name in os.listdir(class_path):
        img_path = class_path + os.sep + img_name
        img = Image.open(img_path)
        img = img.resize((500, 500))
        img_raw = img.tobytes()
        example = tf.train.Example(features=tf.train.Features(feature={"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[int(name)])),
                                                                       "image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))}))
        writer.write(example.SerializeToString())
writer.close()

3.2 图片文件的循环读取

import tensorflow as tf
import cv2
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"


def read_and_decode(filename):
    filename_queue = tf.train.string_input_producer([filename])

    reader = tf.TFRecordReader()

    # tf.TFRecordReader().read(queue): 返回的是(index, value)
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example, features={"label": tf.FixedLenFeature([], tf.int64),
                                                                     "image": tf.FixedLenFeature([], tf.string)})
    label = features["label"]
    img_raw = features["image"]

    img = tf.decode_raw(img_raw, tf.uint8)
    img = tf.reshape(img, shape=[500, 500, 3])
    return img, label


if "__main__" == __name__:
    filename = "train.tfrecords"
    img, label = read_and_decode(filename=filename)

    # tf.train.shuffle_batch: 从队列当中随机采样
    img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=1, capacity=10, min_after_dequeue=1)

    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    for i in range(20):
        val = sess.run(img_batch)
        label = sess.run(label_batch)
        val.resize((500, 500, 3))
        cv2.imshow("cool", val)
        cv2.waitKey()
        print(label)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博士僧小星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值