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 相关文章推荐
在Django框架中运行Python应用全攻略
Jul 17 Python
简单谈谈python中的多进程
Nov 06 Python
Python之日期与时间处理模块(date和datetime)
Feb 16 Python
Django权限机制实现代码详解
Feb 05 Python
对python多线程与global变量详解
Nov 09 Python
python sklearn库实现简单逻辑回归的实例代码
Jul 01 Python
python Django中models进行模糊查询的示例
Jul 18 Python
python模拟鼠标点击和键盘输入的操作
Aug 04 Python
Python datetime包函数简单介绍
Aug 28 Python
Django admin禁用编辑链接和添加删除操作详解
Nov 15 Python
tensorflow安装成功import tensorflow 出现问题
Apr 16 Python
10个python爬虫入门基础代码实例 + 1个简单的python爬虫完整实例
Dec 16 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
CI框架验证码CAPTCHA辅助函数用法实例
2014/11/05 PHP
PHP计算指定日期所在周的开始和结束日期的方法
2015/03/24 PHP
jValidate 基于jQuery的表单验证插件
2009/12/12 Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
2011/03/07 Javascript
js写一个弹出层并锁屏效果实现代码
2012/12/07 Javascript
js库Modernizr的介绍和使用
2015/05/07 Javascript
jQuery实现鼠标滑向当前图片高亮显示并且其它图片变灰的方法
2015/07/27 Javascript
jQuery实现带幻灯的tab滑动切换风格菜单代码
2015/08/27 Javascript
JavaScript过滤字符串中的中文与空格方法汇总
2016/03/07 Javascript
ES6教程之for循环和Map,Set用法分析
2017/04/10 Javascript
jQuery简单实现对数组去重及排序操作实例
2017/10/31 jQuery
vue页面跳转后返回原页面初始位置方法
2018/02/11 Javascript
layui的table中显示图片方法
2018/08/17 Javascript
Vue绑定内联样式问题
2018/10/17 Javascript
从0到1学习JavaScript编写贪吃蛇游戏
2020/07/28 Javascript
Vue项目中使用mock.js的完整步骤
2021/01/12 Vue.js
[02:12]打造更好的电竞完美世界:完美盛典回顾篇
2018/12/19 DOTA
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
Python将DataFrame的某一列作为index的方法
2018/04/08 Python
Pycharm导入Python包,模块的图文教程
2018/06/13 Python
Flask框架信号用法实例分析
2018/07/24 Python
12个Python程序员面试必备问题与答案(小结)
2019/06/24 Python
Pytorch实现基于CharRNN的文本分类与生成示例
2020/01/08 Python
Python爬虫中Selenium实现文件上传
2020/12/04 Python
Pytorch1.5.1版本安装的方法步骤
2020/12/31 Python
浅谈pc和移动端的响应式的使用
2019/01/03 HTML / CSS
加拿大领先的冒险和户外零售商:Atmosphere
2017/12/19 全球购物
澳大利亚当地社区首选的光学商店:1001 Optical
2019/08/24 全球购物
艺术爱好者的自我评价分享
2013/10/08 职场文书
益达广告词
2014/03/14 职场文书
职工代表大会主持词
2014/04/01 职场文书
婚庆开业庆典主持词
2015/06/30 职场文书
2016读书月活动心得体会
2016/01/14 职场文书
Python离线安装openpyxl模块的步骤
2021/03/30 Python
利用html+css实现菜单栏缓慢下拉效果的示例代码
2021/03/30 HTML / CSS
为什么在foreach循环中JAVA集合不能添加或删除元素
2021/06/11 Java/Android