-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict2.py
142 lines (120 loc) · 4.72 KB
/
predict2.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
import cv2
import numpy as np
import os
import dicom
import data
import copy
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import SGD
from keras.utils import np_utils
RECEP_HEI = 56
RECEP_WEI = 28
DATA_PATH = os.getcwd()
MODE_TEST_PATH = '/data/metROI/test_txt'
nb_classes = 2
def getmodel():
nb_classes = 2
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same',
input_shape=(1, RECEP_HEI, RECEP_WEI)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.load_weights('weights.hdf5')
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
# model.fit(X_train, Y_train, batch_size=32, nb_epoch=1,
# verbose=1, shuffle = True ,validation_split=0.25)
return model
def get_input_and_output(source):
file = open(source, 'r')
list = file.readlines()
test_array = np.empty((0, len(list)), int)
for line in list:
# print line
line = line.split(',')
line[len(line) - 1] = line[len(line) - 1].replace('\n', '')
line = map(int, line)
test_array = np.append(test_array, np.array([line]), axis=0)
dir_array = source.split('/')
dir_name = dir_array[len(dir_array) - 1].split('_')
dir_image_file = DATA_PATH + '/data/Sagittal-segmentation/' + \
dir_name[0] + '/' + dir_name[1] + '/' + \
str(dir_name[2]).replace('txt', 'dcm')
medical_img = dicom.read_file(dir_image_file)
return medical_img, test_array
def predict():
count_img = 0
count = 0
index = 0
count_pos = 0
class_lable = 2
x_start = RECEP_WEI
y_start = RECEP_HEI
TRAIN_DATA_TXT_PATH = DATA_PATH + MODE_TEST_PATH
IMG_LIST = os.listdir(TRAIN_DATA_TXT_PATH)
TRAIN_NUM = len(IMG_LIST)
TRAIN_OUT = []
TRAIN_IN = []
Resolution = []
for name in IMG_LIST:
med_img, test_array = get_input_and_output(TRAIN_DATA_TXT_PATH + '/' + name)
med_img_pix = cv2.resize(med_img.pixel_array, (320, 320), interpolation=cv2.INTER_AREA)
test_array = test_array + 0.001
test_array = cv2.resize(test_array, (320, 320), interpolation=cv2.INTER_AREA)
cropImg1 = med_img_pix[0:320, 320/ 4: 3 * 320 / 4]
cropImg2 = test_array[0:320, 320 / 4: 3 * 320 / 4]
Resolution.append( med_img.PixelSpacing[0]* med_img.Rows / np.float(320) )
y_max = 320
x_max = 320 / 2
for i in range(y_start, y_max):
for m in range(x_start, x_max):
region = cropImg1[i - RECEP_HEI: i, m - RECEP_WEI: m]
class_lable = cropImg2[i - RECEP_HEI / 2][m - RECEP_WEI / 2]
if(class_lable != 1.001 ):
TRAIN_IN.append(region.reshape(1, RECEP_HEI, RECEP_WEI))
TRAIN_OUT.append(np_utils.to_categorical([0], nb_classes)[0])
index += 1
# print(index)
elif (class_lable == 1.001):
TRAIN_IN.append(region.reshape(1, RECEP_HEI, RECEP_WEI))
TRAIN_OUT.append(np_utils.to_categorical([1], nb_classes)[0])
count_pos += 1
index += 1
# print(index)
count += 1
if(index % 1000 == 0):
print('Processed training set:' + str(index))
count_img += 1
print('Processed images:' + str(count_img) + '.Positive num:' +
str(count_pos) + '.Total size:' + str(index))
TRAIN_IN = np.asarray(TRAIN_IN, dtype=np.float)
TRAIN_OUT = np.asarray(TRAIN_OUT, dtype=np.float)
Resolution = np.asarray(Resolution)
print('Normalizing training data...')
for i in range(TRAIN_IN.shape[0]):
for m in range(TRAIN_IN.shape[1]):
mean = np.mean(TRAIN_IN[i][m]) # mean for data centering
std = np.std(TRAIN_IN[i][m]) # std for data normalization
TRAIN_IN[i][m] = (TRAIN_IN[i][m] - mean) / std
model = getmodel()
print('Predicting...')
predict_result = model.predict_proba(TRAIN_IN)
np.save('answer_image'+'.npy',TRAIN_OUT)
np.save('predicted_image'+'.npy', predict_result)
np.save('resolution'+'.npy', Resolution)
if __name__ == '__main__':
predict()