-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneratePrepData.py
169 lines (151 loc) · 4.57 KB
/
generatePrepData.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
from scipy.misc import imread, imsave, imresize
import pickle
import numpy as np
import os
import cv2, csv
import sys, random, time
from sklearn.externals import joblib
# os.environ['CUDA_VISIBLE_DEVICES'] = '1'
from keras.utils import to_categorical
CLASSES = 8
LABELS = {
'No Finding': -1,
'Atelectasis': 1,
'Cardiomegaly': 2,
'Consolidation': -1,
'Edema': -1,
'Effusion': 3,
'Emphysema': -1,
'Fibrosis': -1,
'Hernia': -1,
'Infiltration': 4,
'Infiltrate': -1,
'Mass': 5,
'Nodule': 6,
'Pleural_Thickening': -1,
'Pneumonia': 7,
'Pneumothorax': 8
}
def EqualizeHistogram(img, in_path=None):
if in_path != None:
img = cv2.imread(in_path,0)
equ = np.array(cv2.equalizeHist(img))
# print(equ.shape)
return equ
def CLAHE(img, in_path = None, tileGridsize=(8,8)):
if in_path != None:
img = cv2.imread(in_path,0)
clahe1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=tileGridsize)
cl1 = clahe1.apply(img)
return cl1
# binary data
def bin_preprocess():
trainset = set()
with open('data/train.txt') as f:
re = csv.reader(f)
for r in re:
trainset.add(r[0])
isInfiltration = []
notInfiltration = []
with open('data/Data_Entry_2017_v2.csv') as f:
re = csv.reader(f)
next(re)
for r in re:
id = r[0]
flag = False
for observe in r[1].split('|'):
ob = LABELS[observe]
if observe == 'Infiltration':
isInfiltration.append(id)
flag = False
break
if ob != -1:
flag = True
if (flag):
notInfiltration.append(id)
print(len(isInfiltration), len(notInfiltration))
X = []
y = []
for idx in range(1,10001):
img1 = imread('data/images/' + isInfiltration[idx], mode ='RGB')
img2 = imread('data/images/' + notInfiltration[idx], mode ='RGB')
X.append(imresize(img1 ,size=(224,224)))
y.append([1])
X.append(imresize(img2 ,size=(224,224)))
y.append([0])
# if(idx % 10 == 0):
# print(idx)
if(idx % 2000 == 0):
print(idx)
X = np.array(X)
y = np.array(y)
with open('data/bin/X_' + str(idx // 2000) +'.npy', 'wb') as f:
joblib.dump(X, f)
with open('data/bin/y_' + str(idx // 2000) +'.npy', 'wb') as f:
joblib.dump(y, f)
X = []
y = []
if not (os.path.exists("data/bin/")):
os.makedirs("data/bin/")
if not (os.path.exists("data/npy")):
os.makedirs("data/npy")
bin_preprocess()
# valid
with open('data/pickles/labels_valid.pkl', 'rb') as f:
validdata = pickle.load(f)
X, y = [], []
for k, v in validdata.items():
img = imread('data/images/' + k, mode ='RGB')
# img = cv2.imread('data/images/' + k,0)
# resized_image = cv2.resize(img, (224, 224))
# img = EqualizeHistogram(resized_image)
# img = CLAHE(img)
# img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
X.append(imresize(img ,size=(224,224)))
# X.append(img)
y.append(v)
y = to_categorical(y, num_classes=CLASSES)
X = np.array(X)
print(X.shape, y.shape)
with open('data/npy/X_valid.npy', 'wb') as f:
joblib.dump(X, f)
with open('data/npy/y_valid.npy', 'wb') as f:
joblib.dump(y, f)
# train
with open('data/pickles/labels_train.pkl', 'rb') as f:
traindata = pickle.load(f)
X, y = [], []
count = 0
item_count = 0
for k, v in traindata.items():
# if(item_count % 10 == 0):
# print(item_count)
# img = cv2.imread('data/images/' + k,0)
# resized_image = cv2.resize(img, (224, 224))
# img = EqualizeHistogram(resized_image)
# img = CLAHE(img)
# img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
img = imread('data/images/' + k, mode ='RGB')
X.append(imresize(img ,size=(224,224)))
y.append(v)
if(len(X) == 1600):
print(count)
y = to_categorical(y, num_classes=CLASSES)
X = np.array(X)
with open('data/npy/X_' + str(count) +'.npy', 'wb') as f:
joblib.dump(X, f)
with open('data/npy/y_' + str(count) +'.npy', 'wb') as f:
joblib.dump(y, f)
X, y = [], []
count += 1
item_count += 1
if(count >= 20):
break
count += 1
print(len(X))
X = np.array(X)
y = to_categorical(y, num_classes=CLASSES)
with open('data/npy/X_' + str(count) +'.npy', 'wb') as f:
joblib.dump(X, f)
with open('data/npy/y_' + str(count) +'.npy', 'wb') as f:
joblib.dump(y, f)