Data Augment ------TensorFlow 训练图片处理

Deep Learning 是基于一个数据的分析模型或是数据的分析方法。数据在足够大时,其分析或事更佳,数据更佳能分析出结果。如果收集的数据过于小或是数据收集小,这样训练出来的结果会让其出乎意料。我在此是针对于CNN模型,其实该模型不是说一定需要大量数据才可以得到结果,其涉及到您训练的模型-fine-tune,采集的数据是否够优质或是数据之间的区分度等。方法1:其实我们如果涉及到一个大类中的数据进行在此的分类时,我们可以进行拆解不一样区域而进行多模型的输出,最后进行一个综合判定,该方法自己在阅读RCNN、Faster-RCNN等论文的后感,具体自己会在MIT的bird数据集上进行测试和验证,具体后面来续写。方法2:还有一种方法是迁移学习模型,主要迁移imageNet数据集上的训练。该方法目前表现不错,自己也用于测试过在Category101上,基于ResNet50,基本在15epoch上其表现已经很好了。方法3:细小颗粒区分模型训练,其实也是正对其少量数据训练非常不错的,关注过近年来的论文,在细小颗粒上区分模型也是可以进行区分的,该方法目前还是需要完善。方法4: 适用Data Augment方法进行,图片的翻转等,实际我们都知道,图片实际是一个矩阵,我们实际就是将矩阵里数据部分变换了(所谓的翻转等)。自己在学习过程中需要扩充数据写了一个数据扩充的类:

def floatrange(start,stop,steps):
    return [start+float(i)*(stop-start)/(float(steps)-1) for i in range(steps)]

class DataAugment:
    def __init__(self,path,shape,count=0,num1=150,num2=100,num3=100,num4=150,num5=100,num6=50,num7=200,num8=150):
        get_tensor=tf.gfile.FastGFile(path,'rb').read()
        self.image_data=tf.image.decode_jpeg(get_tensor)
        self.shape=shape
        self.count=count
        self.num1 = num1
        self.num2 = num2
        self.num3 = num3
        self.num4 = num4
        self.num5 = num5
        self.num6 = num6
        self.num7 = num7
        self.num8 = num8
    def Save_image(self,name,x):
        x=tf.image.encode_jpeg(x)
        with tf.Session() as sess :
            sess.run(tf.global_variables_initializer())
            with tf.gfile.FastGFile(name,'wb') as save:
                save.write(x.eval())
#150 1
    def get_image_central(self):
        for i in floatrange(0.4,1.0,self.num1):
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.central_crop(self.image_data,i)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print ('1')
#100 2
    def get_image_random_tranpose(self):
        for i in range(self.num2):
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.transpose_image(self.image_data)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print ('2')
#100 3
    def get_image_up_down(self):
        for i in range(self.num3):
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.random_flip_up_down(self.image_data)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print('3')
#150 4
    def get_image_left_right(self):
        for i in range(self.num4):
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.random_flip_left_right(self.image_data)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print('4')
#100 5
    def get_image_bright(self):
        for i in floatrange(-0.6,0.6,self.num5):
           self.count=self.count+1
           t=self.count
           name=dir+str(t)+'.jpg'
           contest=tf.image.adjust_brightness(self.image_data,i)
           contest.set_shape(self.shape)
           with tf.Session() as sess:
               sess.run(tf.global_variables_initializer())
               self.Save_image(name,contest)
        #print('5')
#050 6
    def get_image_contrast(self):
        for i in range(self.num6):
            index=np.random.randint(-4,4)
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.adjust_contrast(self.image_data,index)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print ('6')

#200 7
    def get_image_hue(self):
        for i in floatrange(0.0,0.5,self.num7):
           self.count=self.count+1
           t=self.count
           name=dir+str(t)+'.jpg'
           contest=tf.image.random_hue(self.image_data,i)
           contest.set_shape(self.shape)
           with tf.Session() as sess:
               sess.run(tf.global_variables_initializer())
               self.Save_image(name,contest)
        #print ('7')
#150 8
    def get_image_sation(self):
        for i in range(self.num8):
            index=np.random.randint(-6,10)
            self.count=self.count+1
            t=self.count
            name=dir+str(t)+'.jpg'
            contest=tf.image.adjust_saturation(self.image_data,index)
            contest.set_shape(self.shape)
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                self.Save_image(name,contest)
        #print ('8')
#'1'
    def get_count(self):
        return self.count
    def run(self):
        self.get_image_random_tranpose()
        self.get_image_left_right()
        self.get_image_central()
        self.get_image_bright()
        self.get_image_hue()
        self.get_image_contrast()
        self.get_image_sation()
        self.get_image_up_down()
    def run_1(self):
        #self.get_image_bright()
        self.get_image_random_tranpose()
        self.get_image_left_right()
        #self.get_image_contrast()
        #self.get_image_up_down()
        #self.get_image_central()
        #self.get_image_hue()
        self.get_image_hue()
        self.get_image_sation()
        #self.get_image_sation()
    def show(self):
        x=tf.image.encode_jpeg(self.image_data)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            plt.imread(x.eval())
            plt.show()

def get_image_path(path):
    list_name = []
    if os.path.isdir(path):
        for file in os.listdir(path):
            if file.endswith('.jpg') and not file.startswith('.'):
                filename = path+'/'+file
                list_name.append(filename)
    return list_name
def get_dir(dir):
    dirs = []
    if os.path.isdir(dir):
        for path in os.listdir(dir):
            paths = dir +'/'+path
            if os.path.isdir(paths):
                dirs.append(paths)
    return dirs
def make_dirs(path):
    name = 'file'
    for i in range(250):
        names = path +name +str(i)
        if not os._exists(names):
            os.makedirs(names)

调用用例为:


if __name__=='__main__':

    test=DataAugment('/Users/josen/Desktop/001_0003.jpg',[64,64,3],0,2,2,2,2,2,2,2,2)
    test.run_1()
    t=test.get_count()
    print ('the final data is ',str(t))

    cout = 0
    path = '/Users/josen/Desktop/256_ObjectCategories/'+input('dir:')
    dir = input('file-path:')
    list_name = get_image_path(path)
    for i in range(len(list_name)):
        test = DataAugment(list_name[i],[64,64,3],cout,2,2,2,2,2,2,2,2)
        test.run_1()
        cout = test.get_count()
    print(cout)

    root = '/Users/josen/Desktop/Object256/'+input('file:--')
    #list_dir = get_dir(root)
    #print(len(list_dir))
    cout = 0
    #for i in range(len(list_dir)):
    list_name = get_image_path(root)
    #dir = list_dir[i] +'/'
    #nfor = list_name.split('/')[4].split('.')[0]
    for j in range(len(list_name)):
            test = DataAugment(list_name[j],[64,64,3],cout,2,2,2,2,2,2,2,2)
            test.run_1()
            cout = test.get_count()
            if cout == 650:
                break
    print(cout)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值