python生成tensorflow输入输出的图像格式的方法
作者:何雷 发布时间:2021-03-20 05:21:08
标签:tensorflow,输入,输出
TensorFLow能够识别的图像文件,可以通过numpy,使用tf.Variable或者tf.placeholder加载进tensorflow;也可以通过自带函数(tf.read)读取,当图像文件过多时,一般使用pipeline通过队列的方法进行读取。下面我们介绍两种生成tensorflow的图像格式的方法,供给tensorflow的graph的输入与输出。
import cv2
import numpy as np
import h5py
height = 460
width = 345
with h5py.File('make3d_dataset_f460.mat','r') as f:
images = f['images'][:]
image_num = len(images)
data = np.zeros((image_num, height, width, 3), np.uint8)
data = images.transpose((0,3,2,1))
先生成图像文件的路径:ls *.jpg> list.txt
import cv2
import numpy as np
image_path = './'
list_file = 'list.txt'
height = 48
width = 48
image_name_list = [] # read image
with open(image_path + list_file) as fid:
image_name_list = [x.strip() for x in fid.readlines()]
image_num = len(image_name_list)
data = np.zeros((image_num, height, width, 3), np.uint8)
for idx in range(image_num):
img = cv2.imread(image_name_list[idx])
img = cv2.resize(img, (height, width))
data[idx, :, :, :] = img
2 Tensorflow自带函数读取
def get_image(image_path):
"""Reads the jpg image from image_path.
Returns the image as a tf.float32 tensor
Args:
image_path: tf.string tensor
Reuturn:
the decoded jpeg image casted to float32
"""
return tf.image.convert_image_dtype(
tf.image.decode_jpeg(
tf.read_file(image_path), channels=3),
dtype=tf.uint8)
pipeline读取方法
# Example on how to use the tensorflow input pipelines. The explanation can be found here ischlag.github.io.
import tensorflow as tf
import random
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
dataset_path = "/path/to/your/dataset/mnist/"
test_labels_file = "test-labels.csv"
train_labels_file = "train-labels.csv"
test_set_size = 5
IMAGE_HEIGHT = 28
IMAGE_WIDTH = 28
NUM_CHANNELS = 3
BATCH_SIZE = 5
def encode_label(label):
return int(label)
def read_label_file(file):
f = open(file, "r")
filepaths = []
labels = []
for line in f:
filepath, label = line.split(",")
filepaths.append(filepath)
labels.append(encode_label(label))
return filepaths, labels
# reading labels and file path
train_filepaths, train_labels = read_label_file(dataset_path + train_labels_file)
test_filepaths, test_labels = read_label_file(dataset_path + test_labels_file)
# transform relative path into full path
train_filepaths = [ dataset_path + fp for fp in train_filepaths]
test_filepaths = [ dataset_path + fp for fp in test_filepaths]
# for this example we will create or own test partition
all_filepaths = train_filepaths + test_filepaths
all_labels = train_labels + test_labels
all_filepaths = all_filepaths[:20]
all_labels = all_labels[:20]
# convert string into tensors
all_images = ops.convert_to_tensor(all_filepaths, dtype=dtypes.string)
all_labels = ops.convert_to_tensor(all_labels, dtype=dtypes.int32)
# create a partition vector
partitions = [0] * len(all_filepaths)
partitions[:test_set_size] = [1] * test_set_size
random.shuffle(partitions)
# partition our data into a test and train set according to our partition vector
train_images, test_images = tf.dynamic_partition(all_images, partitions, 2)
train_labels, test_labels = tf.dynamic_partition(all_labels, partitions, 2)
# create input queues
train_input_queue = tf.train.slice_input_producer(
[train_images, train_labels],
shuffle=False)
test_input_queue = tf.train.slice_input_producer(
[test_images, test_labels],
shuffle=False)
# process path and string tensor into an image and a label
file_content = tf.read_file(train_input_queue[0])
train_image = tf.image.decode_jpeg(file_content, channels=NUM_CHANNELS)
train_label = train_input_queue[1]
file_content = tf.read_file(test_input_queue[0])
test_image = tf.image.decode_jpeg(file_content, channels=NUM_CHANNELS)
test_label = test_input_queue[1]
# define tensor shape
train_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS])
test_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS])
# collect batches of images before processing
train_image_batch, train_label_batch = tf.train.batch(
[train_image, train_label],
batch_size=BATCH_SIZE
#,num_threads=1
)
test_image_batch, test_label_batch = tf.train.batch(
[test_image, test_label],
batch_size=BATCH_SIZE
#,num_threads=1
)
print "input pipeline ready"
with tf.Session() as sess:
# initialize the variables
sess.run(tf.initialize_all_variables())
# initialize the queue threads to start to shovel data
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
print "from the train set:"
for i in range(20):
print sess.run(train_label_batch)
print "from the test set:"
for i in range(10):
print sess.run(test_label_batch)
# stop our queue threads and properly close the session
coord.request_stop()
coord.join(threads)
sess.close()
来源:http://blog.csdn.net/helei001/article/details/51354404
0
投稿
猜你喜欢
- 一、介绍我想做的是基于人脸识别的表情(情绪)分析。看到网上也是有很多的开源库提供使用,为开发提供了很大的方便。我选择目前用的比较多的dlib
- 如下所示:import numpy as npimport codecs, json a = np.arange(10).reshape(2
- PHP PDO 预处理语句与存储过程很多更成熟的数据库都支持预处理语句的概念。什么是预处理语句?可以把它看作是想要运行的 SQL 的一种编译
- 起序:本文是在 python 自己的虚拟环境下做的,不是在 Anaconda 下做的。一、安装想要更改 jupyterlab 的默认启动位置
- I. 前言在上一篇文章深入理解PyTorch中LSTM的输入和输出(从input输入到Linear输出)中,我详细地解释了如何利用PyTor
- 在一个大型的项目中,不可避免会出现操作时间的业务,比如时间的格式化,比如时间的加减,我们一般会直接使用moment.js库来做,毕竟稳定可靠
- This is a {t}. {name}是一个很强大的字符串模板解析方法。它接受三个参数,分别是{args.text},{args.obj
- 问题,用python生成如下所示的1000个txt文件?解答:import osfor i in range(0,1001): os.mkn
- 原理就是先声明常量,包括列数,行数,各列的属性,然后在程序的其它过程用这些常量来控制Cells。非常方便,便于修改和移植! 以下为窗体整体代
- python matplotlib画图使用colorbar工具自定义颜色 colorbar(draw colorbar without an
- 一、multiprocess模块multiprocess不是一个模块而是python中一个操作、管理进程的包。子模块分为四个部分:创建进程部
- 最近基于selenium写了一个python小工具,记录下学习记录,自己运行的环境是Ubuntu 14.04.4, Python 2.7,C
- 前言使用pandas进行数据分析的时候,我们经常需要对DataFrame的行或者列进行索引。使用pandas进行索引的方法主要有三种:直接使
- (1)在校大学生。最好是数学或计算机相关专业,编程能力还可以的话,稍微看一下爬虫知识,主要涉及一门语言的爬虫库、html解析、内容存储等,复
- 前言引用一张比较清晰易懂的图php伪协议是php自己支持的一种协议与封装协议,简单来说就是php定义的一种特殊访问资源的方法。常见的php伪
- 其实r 是只读,只能读不能写,这是很明确的,但是r+是可读写,变成r+后还没太明白到底加了什么,还是照样写不了,有没有这样的体验呢,如下代码
- window.onload 是页面全部加载完成,包括图片、CSS、JavaScript等。但加载JavaScript的方法有很多种
- 在标志设计中文字与图形元素总是紧密配合,只有恰当的字体与图形搭配时才能发挥最大的视觉效果。这也意味着你不能抛开图案,只是一厢情愿地选择你自己
- 一、引入方式from itertools import cycle二、使用方法我们先来看看它的源码cycle它接收一个可迭代对象,可以将一个
- 学习目的: 掌握文本框的用法 初次接触try…catch…语法 今天内容很轻松,用一个例子,输入年月日,判断输入是否正确 图片如下: 用个