-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdataHandling.py
173 lines (158 loc) · 6.76 KB
/
dataHandling.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import division
import numpy as np
import os
import pandas as pd
import argparse
import sys
import random
import png
from matplotlib.pyplot import imsave, imread
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image
import cv2
matplotlib.use("Agg")
import torchvision.datasets as datasets
from skimage.transform import resize
import ast
import pickle
import csv
import pydicom as dcm
import Augmentor
from tqdm import tqdm
import pathlib
from torch import randint, manual_seed
from copy import copy
from collections import defaultdict
def random_flip(input, axis, with_fa=False):
ran = random.random()
if ran > 0.5:
if with_fa:
axis += 1
return np.flip(input, axis=axis)
else:
return input
def random_crop(input, with_fa=False):
ran = random.random()
if ran > 0.2:
# find a random place to be the left upper corner of the crop
if with_fa:
rx = int(random.random() * input.shape[1] // 10)
ry = int(random.random() * input.shape[2] // 10)
return input[:, rx: rx + int(input.shape[1] * 9 // 10), ry: ry + int(input.shape[2] * 9 // 10)]
else:
rx = int(random.random() * input.shape[0] // 10)
ry = int(random.random() * input.shape[1] // 10)
return input[rx: rx + int(input.shape[0] * 9 // 10), ry: ry + int(input.shape[1] * 9 // 10)]
else:
return input
def random_rotate_90(input, with_fa=False):
ran = random.random()
if ran > 0.5:
if with_fa:
return np.rot90(input, axes=(1,2))
return np.rot90(input)
else:
return input
def random_rotation(x, chance, with_fa=False):
ran = random.random()
if with_fa:
img = Image.fromarray(x[0])
mask = Image.fromarray(x[1])
if ran > 1 - chance:
# create black edges
angle = np.random.randint(0, 90)
img = img.rotate(angle=angle, expand=1)
mask = mask.rotate(angle=angle, expand=1, fillcolor=1)
return np.stack([np.asarray(img), np.asarray(mask)])
else:
return np.stack([np.asarray(img), np.asarray(mask)])
img = Image.fromarray(x)
if ran > 1 - chance:
# create black edges
angle = np.random.randint(0, 90)
img = img.rotate(angle=angle, expand=1)
return np.asarray(img)
else:
return np.asarray(img)
def augment_numpy_images(path, targetNumber, targetDir, skip=None, rot=True, with_fa=False):
classes = os.listdir(path)
if not os.path.exists(targetDir):
os.mkdir(targetDir)
for class_ in classes:
if not os.path.exists(targetDir + class_):
os.makedirs(targetDir + class_)
for class_ in classes:
count, round = 0, 0
while count < targetNumber:
round += 1
for root, dir, files in os.walk(os.path.join(path, class_)):
for file in files:
if skip and skip in file:
continue
filepath = os.path.join(root, file)
arr = np.load(filepath)
print("loaded ", file)
print(arr.shape)
try:
arr = random_crop(arr, with_fa)
print(arr.shape)
if rot:
arr = random_rotation(arr, 0.9, with_fa)
print(arr.shape)
arr = random_flip(arr, 0, with_fa)
arr = random_flip(arr, 1, with_fa)
arr = random_rotate_90(arr, with_fa)
arr = random_rotate_90(arr, with_fa)
arr = random_rotate_90(arr, with_fa)
print(arr.shape)
if with_fa:
whites = arr.shape[2] * arr.shape[1] - np.count_nonzero(np.round(arr[0] - np.amax(arr[0]), 2))
black = arr.shape[2] * arr.shape[1] - np.count_nonzero(np.round(arr[0], 2))
if arr.shape[2] < 10 or arr.shape[1] < 10 or black >= arr.shape[2] * arr.shape[1] * 0.8 or \
whites >= arr.shape[2] * arr.shape[1] * 0.8:
print("illegal content")
continue
else:
whites = arr.shape[0] * arr.shape[1] - np.count_nonzero(np.round(arr - np.amax(arr), 2))
black = arr.shape[0] * arr.shape[1] - np.count_nonzero(np.round(arr, 2))
if arr.shape[0] < 10 or arr.shape[1] < 10 or black >= arr.shape[0] * arr.shape[1] * 0.8 or \
whites >= arr.shape[0] * arr.shape[1] * 0.8:
print("illegal content")
continue
if count % 10 == 0:
if not os.path.exists("./visualizations_of_augmentation/" + class_ + "/"):
os.makedirs("./visualizations_of_augmentation/" + class_ + "/")
if with_fa:
imsave("./visualizations_of_augmentation/" + class_ + "/" + str(count), np.transpose(np.stack([arr[0], arr[0], arr[1]]), (1,2,0)))
else:
imsave("./visualizations_of_augmentation/" + class_ + "/" + str(count), np.transpose(np.stack([arr, arr, arr]), (1,2,0)))
np.save(targetDir + class_ + "/" + file[:-4] + "aug" + str(round), arr)
count += 1
print(count)
except:
print("something is wrong in try, details:", sys.exc_info()[2])
if not os.path.exists("./error_of_augmentation/" + class_ + "/"):
os.makedirs("./error_of_augmentation/" + class_ + "/")
np.save("./error_of_augmentation/" + class_ + "/" + str(count), arr)
if count > targetNumber:
break
print(count)
def window_augmentation(wwidth, wcen):
if wcen == 2047 and wwidth == 4096:
return wwidth, wcen
else:
new_wcen = np.random.randint(-100, 300)
new_wwidth = np.random.randint(-200, 300)
wwidth += new_wwidth
wcen += new_wcen
return wwidth, wcen
if __name__ == "__main__":
print("Data augmentation")
for pos in ["Spiculated","Circumscribed", "Indistinct"]:
augment_numpy_images(
path="/usr/xtmp/mammo/npdata/datasetname_with_fa/train/",
targetNumber=5000,
targetDir="/usr/xtmp/mammo/npdata/datasetname_with_fa/train_augmented_5000/",
rot=True,
with_fa=True)