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 paramiko实现ssh远程访问的方法
Dec 03 Python
Python深入学习之特殊方法与多范式
Aug 31 Python
Django中几种重定向方法
Apr 28 Python
python解决汉字编码问题:Unicode Decode Error
Jan 19 Python
使用Python进行AES加密和解密的示例代码
Feb 02 Python
pandas进行数据的交集与并集方式的数据合并方法
Jun 27 Python
python 处理telnet返回的More,以及get想要的那个参数方法
Feb 14 Python
python获取磁盘号下盘符步骤详解
Jun 19 Python
python3.7简单的爬虫实例详解
Jul 08 Python
python 实现按对象传值
Dec 26 Python
利用Pycharm + Django搭建一个简单Python Web项目的步骤
Oct 22 Python
Python使用UDP实现720p视频传输的操作
Apr 24 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
Gregarius中文日期格式问题解决办法
2008/04/22 PHP
php中smarty实现多模版网站的方法
2015/06/11 PHP
Yii视图操作之自定义分页实现方法
2016/07/14 PHP
phpinfo的知识点总结
2019/10/10 PHP
php封装的page分页类完整实例代码
2020/02/01 PHP
编写兼容IE和FireFox的脚本
2009/05/18 Javascript
jquery ui dialog里调用datepicker的问题
2009/08/06 Javascript
javascript学习笔记整理(概述、变量、数据类型简介)
2015/10/25 Javascript
基于nodejs+express(4.x+)实现文件上传功能
2015/11/23 NodeJs
微信小程序 利用css实现遮罩效果实例详解
2017/01/21 Javascript
Javascript刷新页面的实例
2017/09/23 Javascript
js 毫秒转天时分秒的实例
2017/11/17 Javascript
vue-cli系列之vue-cli-service整体架构浅析
2019/01/14 Javascript
JS实现深度优先搜索求解两点间最短路径
2019/01/17 Javascript
基于Fixed定位的框选功能的实现代码
2019/05/13 Javascript
layui动态绑定事件的方法
2019/09/20 Javascript
[04:37]DOTA2英雄梦之声Vol20发条
2014/06/20 DOTA
Python3.5编程实现修改IIS WEB.CONFIG的方法示例
2017/08/18 Python
Python+matplotlib+numpy绘制精美的条形统计图
2018/01/02 Python
Python实现计算文件MD5和SHA1的方法示例
2019/06/11 Python
python中pytest收集用例规则与运行指定用例详解
2019/06/27 Python
如何在windows下安装配置python工具Ulipad
2020/10/27 Python
简单掌握CSS3中resize属性的用法
2016/04/01 HTML / CSS
HTML5移动开发图片压缩上传功能
2016/11/09 HTML / CSS
加拿大最大的钻石商店:Peoples Jewellers
2018/01/01 全球购物
京东奢侈品:全球奢侈品牌
2018/03/17 全球购物
英语专业大学生求职简历的自我评价
2013/10/18 职场文书
《猴子种树》教学反思
2014/02/14 职场文书
cf收人广告词大全
2014/03/14 职场文书
活动总结格式范文
2014/04/26 职场文书
2015年普法依法治理工作总结
2015/05/26 职场文书
小学生红领巾广播稿
2015/08/19 职场文书
Python爬虫:从m3u8文件里提取小视频的正确操作
2021/05/14 Python
OpenCV全景图像拼接的实现示例
2021/06/05 Python
dubbo服务整合zipkin详解
2021/07/26 Java/Android
Vue实现跑马灯样式文字横向滚动
2021/11/23 Vue.js