-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataAugmentation.py
131 lines (124 loc) · 4.33 KB
/
DataAugmentation.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import numpy as np
import skimage.filters as filters
from skimage.transform import rotate
from skimage.transform import resize
import matplotlib.pyplot as plt
import sklearn.preprocessing as pp
import random
import imageio
import os
'''
Random Rotation
'''
def rot(img, ang):
#angle = random.randrange(0, 180)
return(rotate(img, ang))
'''
Flip Horizontal
'''
def hflip(img):
return np.fliplr(img)
'''
Flip Vertical
'''
def vflip(img):
return np.flipud(img)
'''
Flip Diagonal
'''
def dflip(img):
return vflip(hflip(img))
'''
Gaussian Blur
'''
def gauss(img, sig):
return filters.gaussian(img, sigma = sig)
'''
Resize Images
'''
def rsz(img, x, y):
return np.resize(img, (x,y,3))
'''
Crop based on ROI in CSV
def csv_crop(img, row):
xmin = row["xmin"]
xmax = row["xmax"]
ymin = row["ymin"]
ymax = row["ymax"]
img = img[ymin:ymax, xmin:xmax]
return (img, row["name"])
'''
plaque_images = "./Data/Patches/Plaque/"
patch_images = "./Data/Patches/NoPlaque/"
csv_images = "./Data/CSVs/"
aug_images ="./Data/Patches/Plaque/Aug/"
folder = (os.path.join(plaque_images, file_path) for file_path in os.listdir(plaque_images))
for counter, file_path in enumerate(folder):
if('.png' in file_path):
img = np.array(imageio.imread(file_path))
path = os.path.join(csv_images, (file_path.split("/")[-1][9:-4]+'.csv'))
train = pd.read_csv(path)
ct = 0
for _,row in train.iterrows():
if(row['name'] == 'Calcific plaque'):
classify = 'cal/'
elif(row['name'] == 'Fibrous plaque'):
classify = 'fibrous/'
else:
classify = ''
img_name = (aug_images + classify + file_path.split("/")[-1][:-4]+"_"+row['name']+str(ct))
img = rsz(img, 64,64)
np.save(img_name+"_rsz.npy", img)
img_rot90 = rot(img, 90)
np.save(img_name+"_rot90.npy", img_rot90)
img_rot270 = rot(img, 270)
np.save(img_name+"_rot270.npy", img_rot270)
img_hflip = hflip(img)
ct += 1
np.save(img_name+"_hflip.npy", img_hflip)
img_vflip = vflip(img)
np.save(img_name+"_vflip.npy", img_vflip)
img_dflip = dflip(img)
np.save(img_name+"_dflip.npy", img_dflip)
img_gauss = gauss(img, 10)
np.save(img_name+"_gauss.npy", img_gauss)
'''
print(file_path)
path = os.path.join('./CSVs/',(file_path.split("/")[-1][:-4]+'.csv'))
print(path)
train = pd.read_csv(path)
ct = 0
for _,row in train.iterrows():
print(row)
[img,cls] = csv_crop(img, row)
ct = str(ct)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+".npy", img)
print(img.shape)
img_rot90 = rot(img, 90)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+"_rot90.npy", img_rot90)
img_rot270 = rot(img, 270)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+"_rot270.npy", img_rot270)
img_hflip = hflip(img)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+"_hflip.npy", img_hflip)
img_vflip = vflip(img)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+"_vflip.npy", img_vflip)
img_dflip = dflip(img)
np.save(aug_images[aug_num-1] + file_path.split("/")[-1][:-4]+"_"+cls+ct+"_dflip.npy", img_dflip)
img = rsz(img, 64,64)
np.save(img_name, img)
img_rot90 = rot(img, 90)
np.save(img_name, img_rot90)
img_rot270 = rot(img, 270)
np.save(img_name, img_rot270)
img_hflip = hflip(img)
np.save(img_name, img_hflip)
img_vflip = vflip(img)
np.save(img_name, img_vflip)
img_dflip = dflip(img)
np.save(img_name, img_dflip)
img_gauss = gauss(img, 10)
np.save(img_name, img_gauss)
#img_gauss = gauss(img_, 20)
#np.save(aug_images + file_path.split("/")[-1][:-4]+"_gauss20.npy", img_gauss)
'''