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 相关文章推荐
Python3实现连接SQLite数据库的方法
Aug 23 Python
Python写的英文字符大小写转换代码示例
Mar 06 Python
Django Admin实现上传图片校验功能
Mar 06 Python
python 找出list中最大或者最小几个数的索引方法
Oct 30 Python
Python实现的各种常见分布算法示例
Dec 13 Python
Mac 使用python3的matplot画图不显示的解决
Nov 23 Python
pycharm 更改创建文件默认路径的操作
Feb 15 Python
python用WxPython库实现无边框窗体和透明窗体实现方法详解
Feb 21 Python
Windows下Anaconda安装、换源与更新的方法
Apr 17 Python
python基于爬虫+django,打造个性化API接口
Jan 21 Python
Python requests库参数提交的注意事项总结
Mar 29 Python
Pytest实现setup和teardown的详细使用详解
Apr 17 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
改变Apache端口等配置修改方法
2008/06/05 PHP
php.ini修改php上传文件大小限制的方法详解
2013/06/17 PHP
php获取、检查类名、函数名、方法名的函数方法
2015/06/25 PHP
浅谈PHP表单提交(POST&GET&URL编/解码)
2017/04/03 PHP
Laravel 中创建 Zip 压缩文件并提供下载的实现方法
2019/04/02 PHP
Prototype使用指南之ajax
2007/01/10 Javascript
让 JavaScript 轻松支持函数重载 (Part 2 - 实现)
2009/08/04 Javascript
javascript 面向对象编程基础 多态
2009/08/21 Javascript
jquery滚动组件(vticker.js)实现页面动态数据的滚动效果
2013/07/03 Javascript
js实现网页多级级联菜单代码
2015/08/20 Javascript
jQuery自动完成插件completer附源码下载
2016/01/04 Javascript
vue.js 中使用(...)运算符报错的解决方法
2018/08/09 Javascript
详解Angular模板引用变量及其作用域
2018/11/23 Javascript
微信小程序使用websocket通讯的demo,含前后端代码,亲测可用
2019/05/22 Javascript
vue项目中将element-ui table表格写成组件的实现代码
2019/06/12 Javascript
axios异步提交表单数据的几种方法
2019/08/11 Javascript
如何优雅地在Node应用中进行错误异常处理
2019/11/25 Javascript
JavaScript 替换所有匹配内容及正则替换方法
2020/02/12 Javascript
JS数组的高级使用方法示例小结
2020/03/14 Javascript
简单了解前端渐进式框架VUE
2020/07/20 Javascript
Python urlopen()函数 示例分享
2014/06/12 Python
Python实现的远程登录windows系统功能示例
2018/06/21 Python
python环形单链表的约瑟夫问题详解
2018/09/27 Python
如何在pycharm中安装第三方包
2020/10/27 Python
Data URI scheme详解和使用实例及图片base64编码实现方法
2014/05/08 HTML / CSS
加拿大约会网站:EliteSingles.ca
2018/01/12 全球购物
Nordgreen英国官网:斯堪的纳维亚设计师手表
2018/10/24 全球购物
年度考核自我评价
2014/01/25 职场文书
职业生涯规划书范文
2014/03/10 职场文书
大学生实习证明范文(5篇)
2014/09/18 职场文书
圣诞晚会主持词
2015/07/01 职场文书
开学季:喜迎新生,迎新标语少不了
2019/11/07 职场文书
导游词之天下银坑景区
2019/11/21 职场文书
导游词之青岛崂山
2019/12/27 职场文书
详解MySQL连接挂死的原因
2021/05/18 MySQL
一篇文章带你深入了解Mysql触发器
2021/08/02 MySQL