-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar10Preprocessing
83 lines (66 loc) · 2.27 KB
/
cifar10Preprocessing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 20 20:49:55 2016
@author: XFZ
"""
#cifar10 data reprocessing
import numpy as np
import cv2
from random import shuffle
from multiprocessing import Pool
import cPickle
dataPrifix = 'data/batch_class_'
def unpickle(file):
import cPickle
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
def transform_image(img,ang_range,shear_range,trans_range):
'''
This function transforms images to generate new images.
The function takes in following arguments,
1- Image
2- ang_range: Range of angles for rotation
3- shear_range: Range of values to apply affine transform to
4- trans_range: Range of values to apply translations over.
A Random uniform distribution is used to generate different parameters for transformation
'''
# Rotation
ang_rot = np.random.uniform(ang_range)-ang_range/2
rows,cols,ch = img.shape
Rot_M = cv2.getRotationMatrix2D((cols/2,rows/2),ang_rot,1)
# Translation
tr_x = trans_range*np.random.uniform()-trans_range/2
tr_y = trans_range*np.random.uniform()-trans_range/2
Trans_M = np.float32([[1,0,tr_x],[0,1,tr_y]])
# Shear
pts1 = np.float32([[5,5],[20,5],[5,20]])
pt1 = 5+shear_range*np.random.uniform()-shear_range/2
pt2 = 20+shear_range*np.random.uniform()-shear_range/2
pts2 = np.float32([[pt1,5],[pt2,pt1],[5,pt2]])
shear_M = cv2.getAffineTransform(pts1,pts2)
#img = cv2.warpAffine(img,Rot_M,(cols,rows))
#img = cv2.warpAffine(img,Trans_M,(cols,rows))
#img = cv2.warpAffine(img,shear_M,(cols,rows))
img = cv2.resize(img,(224,224))
return img
def dataAug(img,n=16):
img = img.reshape(32,32,3,order='F')
imgs =[]
for i in range(1):
imgs.append(transform_image(img,20,10,5).reshape(224*224*3,order="F"))
return imgs
def dataProcess(nclass):
datas = cPickle.load(open(dataPrifix+str(nclass),'rb'))
aug = []
for data in datas:
imgs = dataAug(data)
aug.extend(imgs)
shuffle(aug)
cPickle.dump(aug,open('data/aug_data_class_'+str(nclass),'wb'))
if __name__ == '__main__':
from contextlib import closing
with closing(Pool(processes=1)) as p:
p.map(dataProcess,range(1))