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的几条建议分享
Feb 10 Python
Python中转换角度为弧度的radians()方法
May 18 Python
Python中对元组和列表按条件进行排序的方法示例
Nov 10 Python
详解Python nose单元测试框架的安装与使用
Dec 20 Python
对python添加模块路径的三种方法总结
Oct 16 Python
连接pandas以及数组转pandas的方法
Jun 28 Python
深入了解Python枚举类型的相关知识
Jul 09 Python
pandas DataFrame 警告(SettingWithCopyWarning)的解决
Jul 23 Python
pytorch 图像预处理之减去均值,除以方差的实例
Jan 02 Python
用Python绘制漫步图实例讲解
Feb 26 Python
Python 中由 yield 实现异步操作
May 04 Python
基于python实现判断字符串是否数字算法
Jul 10 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邮件发送,php发送邮件的类
2011/03/24 PHP
PHP处理会话函数大总结
2015/08/05 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
Laravel模糊查询区分大小写的实例
2019/09/29 PHP
ext 列表页面关于多行查询的办法
2010/03/25 Javascript
基于javascript 闭包基础分享
2013/07/10 Javascript
js操作输入框提示信息且响应鼠标事件
2014/03/25 Javascript
js图片预加载示例
2014/04/30 Javascript
JavaScript AOP编程实例
2015/06/16 Javascript
图解js图片轮播效果
2015/12/20 Javascript
JS动态加载脚本并执行回调操作
2016/08/24 Javascript
JavaScript 身份证号有效验证详解及实例代码
2016/10/20 Javascript
Vue.js计算属性computed与watch(5)
2016/12/09 Javascript
JS如何生成一个不重复的ID的函数
2016/12/25 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
原生JS+Canvas实现五子棋游戏
2020/05/28 Javascript
微信小程序实现点击文字页面跳转功能【附源码下载】
2017/12/12 Javascript
vue 2.x 中axios 封装的get 和post方法
2018/02/28 Javascript
基于element-ui的rules中正则表达式
2018/09/04 Javascript
基于vue-draggable 实现三级拖动排序效果
2020/01/10 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
2020/04/30 jQuery
python解析xml文件实例分析
2015/05/27 Python
Python RabbitMQ消息队列实现rpc
2018/05/30 Python
使用pip发布Python程序的方法步骤
2018/10/11 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
2019/11/15 Python
Django 多对多字段的更新和插入数据实例
2020/03/31 Python
pandas 强制类型转换 df.astype实例
2020/04/09 Python
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
2020/04/17 Python
有趣的流行文化T恤、马克杯、手机壳和更多:Look Human
2019/01/07 全球购物
Lowe’s加拿大:家居装修、翻新和五金店
2019/12/06 全球购物
Myprotein荷兰官网:欧洲第一运动营养品牌
2020/07/11 全球购物
大学本科毕业生的自我鉴定范文
2013/11/19 职场文书
腾讯广告词
2014/03/19 职场文书
竞选班长演讲稿400字
2014/08/22 职场文书
庆国庆活动总结
2014/08/28 职场文书
2016高考寄语或鼓励的话语
2015/12/04 职场文书