本文试图解决一个问题,即我们自定义的数据如何训练模型?初识深度学习,我们接触手写数字识别模型,但是批次数据是mnist已经定义好的,我们现在有自己的图片如何做成批次进行训练模型。

  现在我们将准备好的原始数据放在flower_photos下的文件夹下面,里面又分类包含五种花,文件夹的名字即为类名,每种文件夹下面又有若干图片,如下图所示:

例如,daisy文件下面图片,图片的尺寸大小不一致,这在后期要处理。

数据增强

  数据增强通过对图片进行一定的处理,使得训练数据量增强,防止过拟合等,这块后期完善。。

  由于模型需要既定尺寸大小的图片,我们先对图片进行resize,以下代码将实现对图片调整尺寸后保存在新的地址中。本文只是解决了在一个文件下

def resize_image(base_dir, new_dir, wight, height):
if not os.path.exists(new_dir):
os.mkdir(new_dir)
foldername = os.listdir(base_dir)
for folder in foldername:
if not os.path.exists(new_dir+'\\'+folder):
os.mkdir(new_dir+'\\'+folder)
imagename = os.listdir(base_dir+'\\'+folder)
for img in imagename:
image = Image.open(base_dir +"\\"+ folder+'\\'+img)
image_sized = image.resize((wight, height),Image.ANTIALIAS)
image_sized.save(new_dir +"\\"+ folder+'\\'+img)
print('--------------Data_resized DONE-------------------')

write_tfrecord

  这里需要一个将类标对应成0,1,2。。的数字,建立一个label.txt,文件夹为类名,对应相应的数字。

  

  本节理论后期完善,下面代码将会生成xx.tfrecord

def convert2example(img_dir, label):
image=Image.open(img_dir)
width, height = image.size
if image.mode != 'RGB':
image=image.convert('RGB')
image_data=image.tobytes()
#img_name1 = bytes(img_name, 'utf-8')
example = tf.train.Example(features=tf.train.Features(feature={
'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_data])),
'image/height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])),
'image/width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])),
'image/label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
}))
return example def writer_tfrecord(data_dir, label_txt_dir, output_dir, dataset):
if not tf.gfile.Exists(output_dir):
tf.gfile.MakeDirs(output_dir)
out_file=os.path.join(output_dir, dataset+'.tfrecord')
num_samples=0
with tf.python_io.TFRecordWriter(out_file) as tfWriter:
label_dict = {}
with open(label_txt_dir,'r') as f:
for line in f.readlines():
folder = line.strip().split(':')[0]
label = line.strip().split(':')[1]
label_dict[folder] = label
image_list, label_list = [], []
for folder in os.listdir(data_dir):
dir_path = os.path.join(data_dir,folder,'*.jpg')
for image in glob.glob(dir_path):
image_list.append(image)
label_list.append(int(label_dict[folder]))
label = int(label_dict[folder])
example=convert2example(image, label)
tfWriter.write(example.SerializeToString())
num_samples+=1
print("Number of samples: {}".format(num_samples))

Get_batch

  首先read_tfrecord读取上文生成的tfrecord文件,之后shuffle生成batch,test文件是测试的,

def read_tfrecord(filename_queue):
feature = {'image/encoded': tf.FixedLenFeature([], tf.string),
'image/height': tf.FixedLenFeature([], tf.int64),
'image/width': tf.FixedLenFeature([], tf.int64),
'image/label': tf.FixedLenFeature([], tf.int64)} reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features=feature) image = tf.decode_raw(features['image/encoded'], tf.uint8)
image = tf.cast(image, tf.float32)
height = tf.cast(features['image/height'],tf.int32)
width = tf.cast(features['image/width'], tf.int32)
label = tf.cast(features['image/label'], tf.int32)
img = tf.reshape(image, [height, width, 3]) # preprocess
# subtract mean valu
rgb_mean=np.array([123.68, 116.779, 103.939])
img = tf.subtract(img, rgb_mean)
# red, green, blue = tf.split(3, 3, img)
# img = tf.concat(3, [
# tf.subtract(red , bgr_mean[2]),
# tf.subtract(green , bgr_mean[1]),
# tf.subtract(blue , bgr_mean[0]),
# ])
# center_crop
img = tf.image.resize_images(img, [256, 256])
j = int(round((256 - 224) / 2.))
i = int(round((256 - 224) / 2.))
img = img[j:j+224, i:i+224, :] # scale to 1
img = tf.cast(img, tf.float32) * 0.017 return img, label def get_batch(infile, batch_size, num_threads=4, shuffle=False, min_after_dequeue=None):
# 使用batch,img的shape必须是静态常量
image, label = read_tfrecord(infile) if min_after_dequeue is None:
min_after_dequeue = batch_size * 10
capacity = min_after_dequeue + 3 * batch_size if shuffle:
img_batch, label_batch = tf.train.shuffle_batch([image, label], batch_size=batch_size,
capacity=capacity,num_threads=num_threads,
min_after_dequeue=min_after_dequeue)
else:
img_batch, label_batch = tf.train.batch([image, label], batch_size,
capacity=capacity, num_threads=num_threads,
allow_smaller_final_batch=True) return img_batch, label_batch
def test_tfrecord(dataset_dir,batch_size):
glob_pattern = os.path.join(dataset_dir, '*.tfrecord')
tfrecords_list = glob.glob(glob_pattern)
filename_queue = tf.train.string_input_producer(tfrecords_list, num_epochs=None)
img_batch, label_batch = get_batch(filename_queue, batch_size)
print('-----img_batch:------',type(img_batch))
print('-----label_batch:------',type(label_batch))

main 测试

  通过下面的代码我们测试下数据。

  生成的batch为tensor,这为后期placeholder占位符送入的数据非tensor,需要转换成列表或者其他都可以,有两种方式:

 with tf.Session() as sess:
print('-----img_batch:------',type(img_batch))
print('-----label_batch:------',type(label_batch))
     #第一种 tensor在run后变成array
#img_batch, label_batch = sess.run([img_batch, label_batch])
     # 第二种,eval()方法可以将tensor变为array
     # 同里,将array变为tensor可以用convert_to_tensor()
img_batch = img_batch.eval()
label_batch = label_batch.eval()
print('-----img_batch:------',type(img_batch))
print('-----label_batch:------',type(label_batch))
def main():

    label_txt_dir = 'label.txt'
data_dir = './resized_flower_photos'
base_dir = './flower_photos'
new_dir = './resized_flower_photos'
resize_wight = 224
resize_height = 224 output_dir = './tfrecord'
dataset = 'mobilenetv2_data' #resize_image(base_dir,new_dir,resize_wight,resize_height)
#image_list, label_list = read_file(label_txt_dir,data_dir)
#writer_tfrecord(data_dir, label_txt_dir, output_dir, dataset)
img_batch, label_batch = test_tfrecord(output_dir,64)
with tf.Session() as sess:
print('-----img_batch:------',type(img_batch))
print('-----label_batch:------',type(label_batch))
img_batch, label_batch = sess.run([img_batch, label_batch])
print('-----img_batch:------',type(img_batch))
print('-----label_batch:------',type(label_batch))
if __name__ == '__main__':
main()

创建txt文件

  我们需要将图片和类标对应起来作为一个txt文件存在,下面的代码实现了将文件中的内容做成txt文件,这个可以在caffe中的imagedata中使用,需要的数据label_txt_dir如下图:

import tensorflow as tf
import os
import glob def creat_txt(label_txt_dir, data_dir,txt_name):
#将label_txt中的内容放在字典中
label_dict = {}
with open(label_txt_dir,'r') as f:
for line in f.readlines():
folder = line.strip().split(':')[0]
label = line.strip().split(':')[1]
label_dict[folder] = label
#将文件中的数据放在列表中
image_list, label_list = [], []
for folder in os.listdir(data_dir):
dir_path = os.path.join(data_dir,folder,'*.jpg')
for image in glob.glob(dir_path):
image_list.append(image)
label_list.append(label_dict[folder])
#将列表中内容写在文件中
with open(txt_name,'w') as file:
for i in range(len(image_list)):
file.write(image_list[i])
file.write(' ')
file.write(label_list[i])
file.write('\n')
file.close()
print("There are %d data" % (len(image_list)))
print('-------creat_%s Done--------'% txt_name) #------------执行main函数-------------------#
def main():
print('----------main-------------')
data_dir = './resized_image'
label_txt_dir = 'label.txt'
txt_name = 'train.txt'
creat_txt(label_txt_dir, data_dir,txt_name) if __name__ == '__main__':
main()

生成的train.txt文件如下图所示:

Data Preprocess的更多相关文章

  1. 文本分类实战(十)—— BERT 预训练模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  2. 文本分类实战(九)—— ELMO 预训练模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  3. 文本分类实战(八)—— Transformer模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  4. 文本分类实战(七)—— Adversarial LSTM模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  5. 文本分类实战(六)—— RCNN模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  6. 文本分类实战(五)—— Bi-LSTM + Attention模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  7. 文本分类实战(四)—— Bi-LSTM模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  8. 文本分类实战(三)—— charCNN模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  9. 文本分类实战(二)—— textCNN 模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

随机推荐

  1. CSS3里的瀑布流效果

    页面伸缩之后,内部的布局会自动缩放,高度不同的内容也会自动以垂直瀑布的效果平铺. 下面就是一部分代码: CSS: /*大层*/ .container{width:%;margin: auto;} /* ...

  2. Python 爬虫七 Scrapy

    Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...

  3. MyBatis中---数据库配置的属性名冲突问题

    一.db.properties 属性文件中 最好加特殊的标志前缀  jdbc.username ,如果单纯的username有可能影响到 mapper.xml中的 ${username}; 举例   ...

  4. 新语法11. – LINQ

    LINQ分组: IEnumerable<IGrouping< group dog by dog.Age; 遍历分组: foreach (IGrouping<int, Dog> ...

  5. Redis 集群环境的搭建

    下载与解压 [root@localhost ~]# cd /usr/temp/ [root@localhost temp]# wget http://download.redis.io/release ...

  6. mybatis-plus调用自身的 selectById 方法报错:org.apache.ibatis.binding.BindingException:

    mybatis-plus的版本号是 2.0.1,在调用自身的insert(T)的时候没有报错,但是执行update报错,调用selectById.deleteById的时候也报错.也就是涉及到需要主键 ...

  7. Linux三剑客之awk命令

    awk简介 awk其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母.实际上 AWK 的确拥有自己的语言: AWK 程 ...

  8. 虚拟机CentOS7下NAT模式的网络配置

    NAT模式 就是让Guest OS借助NAT(网络地址交换)功能,通过Host OS所在的网络来访问公网.也就是说,使用NAT模式可以实现Guest OS轻松访问互联网,可以访问宿主计算机所在网络的其 ...

  9. Java的static类

    首先Java的static类只能是静态内部类.如果在外部类声明为static,程序会编译通不过. 其次,主要了解下static内部类与普通内部类的区别是什么,以及static内部类的作用是什么,详见下 ...

  10. MySQL数据库的锁详解【转】

    当然在我们的数据库中也有锁用来控制资源的并发访问,这也是数据库和文件系统的区别之一. 为什么要懂数据库锁? 通常来说对于一般的开发人员,在使用数据库的时候一般懂点 DQL(select),DML(in ...