linux tenserflow add traning picture dataset to TFRecord
来源:互联网 发布:centos eclipse太卡 编辑:程序博客网 时间:2024/06/05 08:30
import os
import tensorflow as tf
from PIL import Image #注意Image,后面会用到
import matplotlib.pyplot as plt
import numpy as np
import cv2
classes={'husky','chihuahua'} #人为 设定 2 类
##cv2.imshow('image',img)
##cv2.waitKey(0)
##cv2.destroyAllWindows()
##cwd='D:\Python\data\dog\\' windows dir
writer= tf.python_io.TFRecordWriter("dog_train.tfrecords") #要生成的文件
for index,name in enumerate(classes):
class_path=cwd+name+'/'
for img_name in os.listdir(class_path):
img_path=class_path+img_name #每一个图片的地址
img=Image.open(img_path)
img= img.resize((128,128))
## print(img_path)
## imgcv = cv2.imread(img_path,0)
## cv2.imshow(img_path,imgcv)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
img_raw=img.tobytes()#将图片转化为二进制格式
print(index)
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
})) #example对象对label和image数据进行封装
writer.write(example.SerializeToString()) #序列化为字符串
writer.close()
print('-------1211----------')
def read_and_decode(filename): # 读入dog_train.tfrecords
filename_queue = tf.train.string_input_producer([filename])#生成一个queue队列
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)#返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})#将image数据和label取出来
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [128, 128, 3]) #reshape为128*128的3通道图片
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中抛出img张量
print('img',img)
label = tf.cast(features['label'], tf.int32) #在流中抛出label张量
print('label',label)
return img, label
read_and_decode('fildog_train.tfrecordsename') # 读入dog_train.tfrecords
filename_queue = tf.train.string_input_producer(["dog_train.tfrecords"]) #读入流中
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
}) #取出包含image和label的feature对象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [128, 128, 3])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess: #开始一个会话
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
for i in range(10):
example, l = sess.run([image,label])#在会话中取出image和label
img=Image.fromarray(example, 'RGB')#这里Image是之前提到的
img.save(cwd+str(i)+'_''Label_'+str(l)+'.jpg')#存下图
img.save('/home/duan/'+str(i)+'_''Label_'+str(l)+'.jpg')#存下图片
print(example, l)
coord.request_stop()
coord.join(threads)
import tensorflow as tf
from PIL import Image #注意Image,后面会用到
import matplotlib.pyplot as plt
import numpy as np
import cv2
#download pictiures and save picture as
#/tmp/dog/husky/5.jpeg
#/tmp/dog/husky/2.jpeg
#/tmp/dog/husky/1.jpeg
#/tmp/dog/husky/4.jpeg
#/tmp/dog/husky/3.jpeg
#/tmp/dog/chihuahua/5.jpeg
#/tmp/dog/chihuahua/2.jpeg
#/tmp/dog/chihuahua/1.jpeg
#/tmp/dog/chihuahua/4.jpeg
#/tmp/dog/chihuahua/3.jpeg
classes={'husky','chihuahua'} #人为 设定 2 类
##opencv check file name
##cv2.imshow('image',img)
##cv2.waitKey(0)
##cv2.destroyAllWindows()
##cwd='D:\Python\data\dog\\' windows dir
writer= tf.python_io.TFRecordWriter("dog_train.tfrecords") #要生成的文件
for index,name in enumerate(classes):
class_path=cwd+name+'/'
for img_name in os.listdir(class_path):
img_path=class_path+img_name #每一个图片的地址
img=Image.open(img_path)
img= img.resize((128,128))
## print(img_path)
## imgcv = cv2.imread(img_path,0)
## cv2.imshow(img_path,imgcv)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
img_raw=img.tobytes()#将图片转化为二进制格式
print(index)
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
})) #example对象对label和image数据进行封装
writer.write(example.SerializeToString()) #序列化为字符串
writer.close()
print('-------1211----------')
def read_and_decode(filename): # 读入dog_train.tfrecords
filename_queue = tf.train.string_input_producer([filename])#生成一个queue队列
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)#返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})#将image数据和label取出来
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [128, 128, 3]) #reshape为128*128的3通道图片
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中抛出img张量
print('img',img)
label = tf.cast(features['label'], tf.int32) #在流中抛出label张量
print('label',label)
return img, label
read_and_decode('fildog_train.tfrecordsename') # 读入dog_train.tfrecords
filename_queue = tf.train.string_input_producer(["dog_train.tfrecords"]) #读入流中
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
}) #取出包含image和label的feature对象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [128, 128, 3])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess: #开始一个会话
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
for i in range(10):
example, l = sess.run([image,label])#在会话中取出image和label
img=Image.fromarray(example, 'RGB')#这里Image是之前提到的
img.save(cwd+str(i)+'_''Label_'+str(l)+'.jpg')#存下图
img.save('/home/duan/'+str(i)+'_''Label_'+str(l)+'.jpg')#存下图片
print(example, l)
coord.request_stop()
coord.join(threads)
阅读全文
1 0
- linux tenserflow add traning picture dataset to TFRecord
- Tenserflow
- add a dynamic picture to a paramter in rdlc
- Linux Add User To Group
- How can I add or import a picture to a QWidget?
- Saves picture to database
- how to print picture
- Howto: Linux Add User To Group (tips)
- Add a system call to linux source
- Howto: Linux Add User To Group
- How to add swap space in Linux
- How to add a Linux service
- How to add route in Linux
- Howto: Linux Add User To Group
- [Linux]--add user to sudo group
- linux : add new log type to plog
- Translate Picture to Char-Set Picture
- How To Add Breakpad To Your Linux Application
- Linux下firefox无法添加书签
- 简单的Servlet实现
- Exception in thread "main" java.lang.UnsupportedClassVersionError
- swing & awt
- MySql学习记录2-视图
- linux tenserflow add traning picture dataset to TFRecord
- 服务器Svn 冲突解决
- 树状数组
- HDU 1878 欧拉回路 (使用并查集~)
- LeetCode#22. Generate Parentheses
- 【OpenCV学习笔记】2:开启摄像头和对捕获到的图像的处理
- Linux五种I/O模型
- 欢迎使用CSDN-markdown编辑器
- 代码之外_教你做坏人