TensorFlow文件读取图片数据案例(100张狗的图片)

目录

图像

图片三要素

张量形状

图片特征值处理

案例:狗图片读取

流程分析

代码:

结果分析:


图像

组成图像基本单位  ---像素

图片三要素

特征抽取  :图片   (三维数组  (图片长度、图片宽度、图片通道数))

       灰度图[长,宽,1]:每一个像素点[0,255]的图  数越大越白

       彩色图[长,宽,3]:每个像素点[0,255]  有3个通道

张量形状

Tensor(指令名称,shape,dtype)

一张图片  shape = (height,width,channels)

多张图片  shape = (,batch,height,width,channels)

图片特征值处理

1)一个样本数据量大

2)样本和样本形状不统一,没办法进行批量操作和运算

每个图片的特征量保持相同,大小转换

图片像素量太大,适当减少像素数量

缩放处理:tf.image.resize_images(images,size)

                 size:new_height  new_width

数据格式

存储:unint8(节约空间)

矩阵计算:float32(提高精度)

案例:狗图片读取

流程分析

1)构造文件名队列

2)读取与解码  ---使样本的形状和类型统一

3)批处理

代码:

import tensorflow as tf
import os
tf.compat.v1.disable_eager_execution()
def picture_read(file_list):
    """
    狗图片读取
    :return:
    """
    # 1、构造文件名队列
    file_queue = tf.compat.v1.train.string_input_producer(file_list)
    # 2、读取与解码
    # 读取
    reader = tf.compat.v1.WholeFileReader()
    # key 文件名 value一张图片的原始编码形式
    key,value = reader.read(file_queue)
    print("key:\n", key)
    print("value_\n", value)
    # 解码
    image = tf.compat.v1.image.decode_jpeg(value)
    print("image:\n",image)
    # 图像形状类型处理
    image_resized = tf.compat.v1.image.resize_images(image,[200,200])
    print("image_redsized:\n",image_resized)
    #静态形状修改
    image_resized.set_shape(shape=[200,200,3])
    print("image_redsized:\n",image_resized)
    # 3、批处理
    image_batch = tf.compat.v1.train.batch([image_resized],batch_size=100,num_threads=1,capacity=100)
    print("image_batch",image_batch)

    # 开启会话
    with tf.compat.v1.Session()as sess:
        # 开启线程
        coord = tf.compat.v1.train.Coordinator()
        threads = tf.compat.v1.train.start_queue_runners(sess=sess,coord=coord)
        key_new,value_new,image_new,image_resized_new = sess.run([key,value,image,image_resized])
        print("key_new:\n",key_new)
        print("value_new:\n",value_new)
        print(("image_new:\n",image_new))
        print("image_resized_new:\n",image_resized_new)
        # 回收线程
        coord.request_stop()
        coord.join(threads)

    return None
# 构建路径+文件名列表
filename = os.listdir("./tmp/dog")
# 拼接路径+文件名
file_list = [os.path.join("./tmp/dog",file) for file in filename]
print(file_list)
picture_read(file_list)

结果分析:

构建的文件列表:

原始编码类型:

解码:

统一图片处理,确定通道数:

进行批处理:

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值