步遥情感网
您的当前位置:首页对python制作自己的数据集实例讲解

对python制作自己的数据集实例讲解

来源:步遥情感网
对python制作⾃⼰的数据集实例讲解

⼀、数据集介绍

17_Category_Flower 是⼀个不同种类鲜花的图像数据,包含 17 不同种类的鲜花,每类 80 张该类鲜花的图⽚,鲜花种类是英国地区常见鲜花。下载数据后解压⽂件,然后将不同的花剪切到对应的⽂件夹,如下图所⽰:

每个⽂件夹下⾯有80个图⽚⽂件。⼆、使⽤的⼯具

⾸先是在tensorflow框架下,然后介绍⼀下⽤到的两个库,⼀个是os,⼀个是PIL。PIL(Python Imaging Library)是 Python 中最常⽤的图像处理库,⽽Image类⼜是 PIL库中⼀个⾮常重要的类,通过这个类来创建实例可以有直接载⼊图像⽂件,读取处理过的图像和通过抓取的⽅法得到的图像这三种⽅法。三、代码实现

我们是通过TFRecords来创建数据集的,TFRecords其实是⼀种⼆进制⽂件,虽然它不如其他格式好理解,但是它能更好的利⽤内存,更⽅便复制和移动,并且不需要单独的标签⽂件(label)。1、制作TFRecords⽂件

import os

import tensorflow as tf

from PIL import Image # 注意Image,后⾯会⽤到import matplotlib.pyplot as pltimport numpy as np

cwd = 'D:\\PyCharm Community Edition 2017.2.3\\Work\\google_net\\jpg\\\\'

classes = {'daffodil', 'snowdrop', 'lilyvalley', 'bluebell', 'crocus', 'iris', 'tigerlily', 'tulip', 'fritiuary',

'sunflower', 'daisy', 'coltsfoot', 'dandelion', 'cowslip', 'buttercup', 'windflower', 'pansy'} # 花为 设定 17 类writer = tf.python_io.TFRecordWriter(\"flower_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((224, 224))

img_raw = img.tobytes() # 将图⽚转化为⼆进制格式

example = tf.train.Example(features=tf.train.Features(feature={ \"label\": tf.train.Feature(int_list=tf.train.IntList(value=[index])),

'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) })) # example对象对label和image数据进⾏封装

writer.write(example.SerializeToString()) # 序列化为字符串writer.close()

⾸先将⽂件移动到对应的路径:

D:\\PyCharm Community Edition 2017.2.3\\Work\\google_net\\jpg

然后对每个⽂件下的图⽚进⾏读写和相应的⼤⼩惊醒改变,具体过程是使⽤tf.train.Example来定义我们要填⼊的数据格式,其中label即为标签,也就是最外层的⽂件夹名字,img_raw为易经理⼆进制化的图⽚。然后使⽤tf.python_io.TFRecordWriter来写⼊。基本的,⼀个Example中包含Features,Features⾥包含Feature(这⾥没s)的字典。最后,Feature⾥包含有⼀个FloatList, 或者ByteList,或者IntList。就这样,我们把相关的信息都存到了⼀个⽂件中,所以前⾯才说不⽤单独的label⽂件。⽽且读取也很⽅便。

执⾏完以上代码就会出现如下图所⽰的TF⽂件

2、读取TFRECORD⽂件

制作完⽂件后,将该⽂件读⼊到数据流中,具体代码如下:

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.int),

'img_raw': tf.FixedLenFeature([], tf.string), }) # 将image数据和label取出来

img = tf.decode_raw(features['img_raw'], tf.uint8)

img = tf.reshape(img, [224, 224, 3]) # reshape为128*128的3通道图⽚ img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 # 在流中抛出img张量 label = tf.cast(features['label'], tf.int32) # 在流中抛出label张量 return img, label

注意,feature的属性“label”和“img_raw”名称要和制作时统⼀ ,返回的img数据和label数据⼀⼀对应。3、显⽰tfrecord格式的图⽚

为了知道TF ⽂件的具体内容,或者是怕图⽚对应的label出错,可以将数据流以图⽚的形式读出来并保存以便查看,具体的代码如下:

filename_queue = tf.train.string_input_producer([\"flower_train.tfrecords\"]) # 读⼊流中reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue) # 返回⽂件名和⽂件features = tf.parse_single_example(serialized_example, features={

'label': tf.FixedLenFeature([], tf.int), 'img_raw': tf.FixedLenFeature([], tf.string), }) # 取出包含image和label的feature对象

image = tf.decode_raw(features['img_raw'], tf.uint8)image = tf.reshape(image, [224, 224, 3])label = tf.cast(features['label'], tf.int32)label = tf.one_hot(label, 17, 1, 0)

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(100):

example, l = sess.run([image, label]) # 在会话中取出image和label img = Image.fromarray(example, 'RGB') # 这⾥Image是之前提到的 img.save(cwd + str(i) + '_''Label_' + str(l) + '.jpg') # 存下图⽚ print(example, l)

coord.request_stop() coord.join(threads)

执⾏以上代码后,当前项⽬对应的⽂件夹下会⽣成100张图⽚,还有对应的label,如下图所⽰:

在这⾥我们可以看到,前80个图⽚⽂件的label是1,后20个图⽚的label是2。 由此可见,我们⼀开始制作tfrecord⽂件时,图⽚分类正确。完整代码如下:

import os

import tensorflow as tf

from PIL import Image # 注意Image,后⾯会⽤到import matplotlib.pyplot as pltimport numpy as np

cwd = 'D:\\PyCharm Community Edition 2017.2.3\\Work\\google_net\\jpg\\\\'

classes = {'daffodil', 'snowdrop', 'lilyvalley', 'bluebell', 'crocus', 'iris', 'tigerlily', 'tulip', 'fritiuary',

'sunflower', 'daisy', 'coltsfoot', 'dandelion', 'cowslip', 'buttercup', 'windflower', 'pansy'} # 花为 设定 17 类writer = tf.python_io.TFRecordWriter(\"flower_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((224, 224))

img_raw = img.tobytes() # 将图⽚转化为⼆进制格式

example = tf.train.Example(features=tf.train.Features(feature={ \"label\": tf.train.Feature(int_list=tf.train.IntList(value=[index])),

'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) })) # example对象对label和image数据进⾏封装

writer.write(example.SerializeToString()) # 序列化为字符串writer.close()

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.int), 'img_raw': tf.FixedLenFeature([], tf.string), }) # 将image数据和label取出来

img = tf.decode_raw(features['img_raw'], tf.uint8)

img = tf.reshape(img, [224, 224, 3]) # reshape为128*128的3通道图⽚ img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 # 在流中抛出img张量 label = tf.cast(features['label'], tf.int32) # 在流中抛出label张量 return img, label

filename_queue = tf.train.string_input_producer([\"flower_train.tfrecords\"]) # 读⼊流中reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue) # 返回⽂件名和⽂件features = tf.parse_single_example(serialized_example, features={

'label': tf.FixedLenFeature([], tf.int), 'img_raw': tf.FixedLenFeature([], tf.string), }) # 取出包含image和label的feature对象

image = tf.decode_raw(features['img_raw'], tf.uint8)image = tf.reshape(image, [224, 224, 3])label = tf.cast(features['label'], tf.int32)label = tf.one_hot(label, 17, 1, 0)

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(100):

example, l = sess.run([image, label]) # 在会话中取出image和label img = Image.fromarray(example, 'RGB') # 这⾥Image是之前提到的 img.save(cwd + str(i) + '_''Label_' + str(l) + '.jpg') # 存下图⽚ print(example, l)

coord.request_stop() coord.join(threads)

本⼈也是刚刚学习深度学习,能⼒有限,不⾜之处请见谅,欢迎⼤⽜⼀起讨论,共同进步!

以上这篇对python制作⾃⼰的数据集实例讲解就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容