def predict_image(self, filepath_image, show=False):
'''
predicts classes of input image
INPUT (1) str 'filepath_image': filepath to image to predict on
(2) bool 'show': True to show the results of prediction, False to return prediction
OUTPUT (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels
(2) if show == True: displays segmentation results
'''
print 'Starting prediction...'
if self.cascade_model:
images = io.imread(filepath_image).astype('float').reshape(5, 216, 160)
p33list = []
p65list = []
# create patches from an entire slice
for image in images[:-1]:
if np.max(image) != 0:
image /= np.max(image)
patch65 = extract_patches_2d(image, (65, 65))
p65list.append(patch65)
p33list.append(self.center_n(33, patch65))
print str(len(p33list))
patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3]))
patches65 = np.array(zip(p65list[0], p65list[1], p65list[2], p65list[3]))
# predict classes of each pixel based on model
prediction = self.model.predict([patches65, patches33])
print 'Predicted'
prediction = prediction.reshape(208, 208)
if show:
io.imshow(prediction)
plt.show
else:
return prediction
else:
images = io.imread(filepath_image).astype('float').reshape(5, 216, 160)
p33list = []
# create patches from an entire slice
for image in images[:-1]:
if np.max(image) != 0:
image /= np.max(image)
patch33 = extract_patches_2d(image, (33, 33))
p33list.append(patch33)
patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3]))
# predict classes of each pixel based on model
prediction = self.cnn1.predict(patches33)
print 'Predicted'
prediction = prediction.reshape(5, 184, 128)
predicted_classes = np.argmax(prediction, axis=0)
if show:
print 'Let s show'
for i in range(5):
io.imshow(prediction[i])
plt.show
print 'Showed'
return prediction
else:
return predicted_classes
评论列表
文章目录