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爬取网站数据保存使用的方法
Nov 20 Python
Python中用memcached来减少数据库查询次数的教程
Apr 07 Python
用Python计算三角函数之atan()方法的使用
May 15 Python
python中使用iterrows()对dataframe进行遍历的实例
Jun 09 Python
Pycharm以root权限运行脚本的方法
Jan 19 Python
Python面向对象编程基础实例分析
Jan 17 Python
详解字符串在Python内部是如何省内存的
Feb 03 Python
pycharm设置python文件模板信息过程图解
Mar 10 Python
Selenium及python实现滚动操作多种方法
Jul 21 Python
如何更换python默认编辑器的背景色
Aug 10 Python
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
Apr 06 Python
一篇文章带你搞懂Python类的相关知识
May 20 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
用mysql_fetch_array()获取当前行数据的方法详解
2013/06/05 PHP
php字符串过滤与替换小结
2015/01/26 PHP
PHP基于timestamp和nonce实现的防止重放攻击方案分析
2019/07/26 PHP
Laravel模糊查询区分大小写的实例
2019/09/29 PHP
浅谈PHP array_search 和 in_array 函数效率问题
2019/10/15 PHP
Js的MessageBox
2006/12/03 Javascript
js jquery分别实现动态的文件上传操作按钮的添加和删除
2014/01/13 Javascript
javascript实现跨域的方法汇总
2015/06/25 Javascript
jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法
2015/12/12 Javascript
JS点击某个图标或按钮弹出文件选择框的实现代码
2016/09/27 Javascript
Android中Okhttp3实现上传多张图片同时传递参数
2017/02/18 Javascript
vue.js指令v-model使用方法
2017/03/20 Javascript
详解vue-cli脚手架中webpack配置方法
2018/08/22 Javascript
vue.js中ref及$refs的使用方法解析
2019/10/08 Javascript
JavaScript实现原型封装轮播图
2020/12/27 Javascript
[41:17]完美世界DOTA2联赛PWL S3 access vs CPG 第二场 12.13
2020/12/17 DOTA
Python中的zip函数使用示例
2015/01/29 Python
python操作小程序云数据库实现简单的增删改查功能
2019/06/06 Python
pyinstaller打包单个exe后无法执行错误的解决方法
2019/06/21 Python
Python使用sklearn库实现的各种分类算法简单应用小结
2019/07/04 Python
python 实现在无序数组中找到中位数方法
2020/03/03 Python
Tensorflow tensor 数学运算和逻辑运算方式
2020/06/30 Python
HTML5中视频音频的使用详解
2017/07/07 HTML / CSS
迪拜航空官方网站:flydubai
2017/04/20 全球购物
英国领先的在线旅游和休闲零售商:lastminute.com
2019/01/23 全球购物
查找廉价航班和发现新目的地:Kiwi.com
2019/02/25 全球购物
什么是smarty? Smarty的优点是什么?
2013/08/11 面试题
介绍一下gcc特性
2015/10/31 面试题
质检部部长职责
2013/12/16 职场文书
销售部主管岗位职责
2013/12/18 职场文书
团队经理竞聘书
2014/03/31 职场文书
党政领导班子民主生活会整改措施
2014/09/18 职场文书
小学五年级语文上册教学计划
2015/01/22 职场文书
2019销售早会主持词
2019/06/27 职场文书
MySQL 分组查询的优化方法
2021/05/12 MySQL
一些让Python代码简洁的实用技巧总结
2021/08/23 Python