学习TensorFlow,生成tensorflow输入输出的图像格式
来源:互联网 发布:php popen 异步 编辑:程序博客网 时间:2024/06/16 12:58
TensorFLow能够识别的图像文件,可以通过numpy,使用tf.Variable或者tf.placeholder加载进tensorflow;也可以通过自带函数(tf.read)读取,当图像文件过多时,一般使用pipeline通过队列的方法进行读取。下面我们介绍两种生成tensorflow的图像格式的方法,供给tensorflow的graph的输入与输出。
1
import cv2import numpy as npimport h5pyheight = 460width = 345with 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 cv2import numpy as npimage_path = './'list_file = 'list.txt'height = 48width = 48image_name_list = [] # read imagewith 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 tfimport randomfrom tensorflow.python.framework import opsfrom tensorflow.python.framework import dtypesdataset_path = "/path/to/your/dataset/mnist/"test_labels_file = "test-labels.csv"train_labels_file = "train-labels.csv"test_set_size = 5IMAGE_HEIGHT = 28IMAGE_WIDTH = 28NUM_CHANNELS = 3BATCH_SIZE = 5def 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 pathtrain_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 pathtrain_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 partitionall_filepaths = train_filepaths + test_filepathsall_labels = train_labels + test_labelsall_filepaths = all_filepaths[:20]all_labels = all_labels[:20]# convert string into tensorsall_images = ops.convert_to_tensor(all_filepaths, dtype=dtypes.string)all_labels = ops.convert_to_tensor(all_labels, dtype=dtypes.int32)# create a partition vectorpartitions = [0] * len(all_filepaths)partitions[:test_set_size] = [1] * test_set_sizerandom.shuffle(partitions)# partition our data into a test and train set according to our partition vectortrain_images, test_images = tf.dynamic_partition(all_images, partitions, 2)train_labels, test_labels = tf.dynamic_partition(all_labels, partitions, 2)# create input queuestrain_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 labelfile_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 shapetrain_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS])test_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS])# collect batches of images before processingtrain_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()
参考资料
[1] http://ischlag.github.io/2016/06/19/tensorflow-input-pipeline-example/
[2] https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/
1 0
- 学习TensorFlow,生成tensorflow输入输出的图像格式
- 生成tensorflow输入输出的图像格式——使用pipeline通过队列的方法
- [TensorFlow] TensorFlow学习的方向
- TensorFlow学习---tf生成数据的方法
- Tensorflow实战学习(十五)【加载图像、图像格式、图像操作、颜色】
- Tensorflow随机数的生成
- Tensorflow 的初步学习
- Tensorflow的常用矩阵生成
- TensorFlow学习笔记(5)----TF生成数据的方法
- TensorFlow学习笔记(5)----TF生成数据的方法
- TensorFlow学习笔记----TF生成数据的方法
- tensorflow学习——GAN手写体生成
- tensorflow学习——DCGAN手写体生成
- TensorFlow 学习
- Tensorflow学习
- 学习tensorflow
- Tensorflow学习
- TensorFlow 学习
- git patch 使用
- QT中常见的问题总结系列
- PHP设计模式
- 密码学扫盲:加密、认证、公钥、私钥
- FFMPEG视音频编解码零基础学习方法
- 学习TensorFlow,生成tensorflow输入输出的图像格式
- java反射的例子
- python网页自动摘要和关键词提取
- 187. Repeated DNA Sequences
- googles示例RecyclerView
- Java Singletom 单例模式 常用实现方式
- inteliij 14 新建maven项目 停在 [INFO] Generating project in Batch mode 的解决办法
- 网络编程知识(2)--Socket理解
- java基本数据类型、对应的封装类以及字符串之间的相互转换