python生成tensorflow输入输出的图像格式的方法


Posted in Python onFebruary 12, 2018

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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现把xml或xsl转换为html格式
Apr 08 Python
非递归的输出1-N的全排列实例(推荐)
Apr 11 Python
python 显示数组全部元素的方法
Apr 19 Python
Python基于百度AI的文字识别的示例
Apr 21 Python
NumPy 数组使用大全
Apr 25 Python
Django处理多用户类型的方法介绍
May 18 Python
对Django中内置的User模型实例详解
Aug 16 Python
python破解bilibili滑动验证码登录功能
Sep 11 Python
python实现将字符串中的数字提取出来然后求和
Apr 02 Python
python实现图书馆抢座(自动预约)功能的示例代码
Sep 29 Python
Keras保存模型并载入模型继续训练的实现
Feb 20 Python
Python实战之用tkinter库做一个鼠标模拟点击器
Apr 27 Python
Flask解决跨域的问题示例代码
Feb 12 #Python
tensorflow实现对图片的读取的示例代码
Feb 12 #Python
python中数据爬虫requests库使用方法详解
Feb 11 #Python
python 接口测试response返回数据对比的方法
Feb 11 #Python
使用Python读取大文件的方法
Feb 11 #Python
python脚本作为Windows服务启动代码详解
Feb 11 #Python
分析Python读取文件时的路径问题
Feb 11 #Python
You might like
php 破解防盗链图片函数
2008/12/09 PHP
php实现的中文分词类完整实例
2017/02/06 PHP
PHP面向对象五大原则之单一职责原则(SRP)详解
2018/04/04 PHP
jQuery asp.net 用json格式返回自定义对象
2010/04/07 Javascript
js 点击页面其他地方关闭弹出层(示例代码)
2013/12/24 Javascript
Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
2015/01/01 NodeJs
JS模拟并美化的表单控件完整实例
2015/08/19 Javascript
跟我学习javascript的this关键字
2020/05/28 Javascript
JS取模、取商及取整运算方法示例
2016/10/13 Javascript
Jquery Easyui分割按钮组件SplitButton使用详解(17)
2016/12/18 Javascript
BootStrap实现轮播图效果(收藏)
2016/12/30 Javascript
BackBone及其实例探究_动力节点Java学院整理
2017/07/14 Javascript
Django模板继承 extend标签实例代码详解
2019/05/16 Javascript
Vue.js实现大屏数字滚动翻转效果
2019/11/29 Javascript
js实现时分秒倒计时
2019/12/03 Javascript
javascript设计模式 ? 迭代器模式原理与用法实例分析
2020/04/17 Javascript
JavaScript中常用的3种弹出提示框(alert、confirm、prompt)
2020/11/10 Javascript
[01:25:09]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS DT第二场
2014/05/24 DOTA
python使用xauth方式登录饭否网然后发消息
2014/04/11 Python
python中正则的使用指南
2016/12/04 Python
Python使用Flask-SQLAlchemy连接数据库操作示例
2018/08/31 Python
解决python os.mkdir创建目录失败的问题
2018/10/16 Python
python使用zip将list转为json的方法
2018/12/31 Python
简单了解Python生成器是什么
2019/07/02 Python
python实现静态web服务器
2019/09/03 Python
Python pandas.DataFrame 找出有空值的行
2019/09/09 Python
Python同时处理多个异常的方法
2020/07/28 Python
Python3如何使用tabulate打印数据
2020/09/25 Python
编码实现字符串转整型的函数
2012/06/02 面试题
个人纪律作风整改措施思想汇报
2014/10/12 职场文书
单位作风建设自查报告
2014/10/23 职场文书
2014小学一年级班主任工作总结
2014/12/05 职场文书
2015年机关后勤工作总结
2015/05/26 职场文书
公司员工宿舍管理制度
2015/08/07 职场文书
音乐课《小猫钓鱼》教学反思
2016/02/18 职场文书
Python 类,对象,数据分类,函数参数传递详解
2021/09/25 Python