Exemplo n.º 1
0
 def unet_predict(self):
     d = divide_image(test_image=self.test_image)
     data = separate_channels(d)
     preds = self.model.predict(data,verbose=1)
     data = combine_channels(data)
     predicted_image = preds.reshape((-1,data[0].shape[0],data[0].shape[1]))
     predicted_image = stitch(predicted_image,1024,4)[0]
     print "heatmap of unet",predicted_image.shape
     return predicted_image
Exemplo n.º 2
0
    def preprocess(self,test_image):
        if isinstance(test_image,str):
            test_image = plt.imread(test_image)

        test_image = imresize(test_image,(1024,1024))
        d = divide_image(test_image=test_image)
        print "d.shape is ",np.shape(d)
        data = separate_channels(d)
        self.orig_shape = (-1,d[0].shape[0],d[0].shape[1])
        data = combine_channels(data)
        return data
Exemplo n.º 3
0
 def _save_debug_img(self, x, y, idx):
     # x: (w, h, d, 1)
     # y: (w, h, d, c)
     if self.categorical:
         y = combine_channels(y)
     x = nib.Nifti1Image(x, np.eye(4))
     y = nib.Nifti1Image(y, np.eye(4))
     debug_dir = "debug"
     if not os.path.exists(debug_dir):
         os.mkdir(debug_dir)
     nib.save(x, os.path.join(debug_dir, "%d.nii" % idx))
     nib.save(y, os.path.join(debug_dir, "%d_seg.nii" % idx))
Exemplo n.º 4
0
    def take_out(self, layer_name, image, average=True):
        if isinstance(image, str):
            image = plt.imread(image)

        images = self.preprocess(image)
        if not self.func:
            self.func = self.make_func(layer_name)
        feature_maps = self.func([images])[0]
        if average:
            feature_maps = np.mean(feature_maps, axis=1)
        else:
            feature_maps = combine_channels(feature_maps)
        feature_map = self.custom_stitch(feature_maps)

        if not average:
            feature_map = separate_channels(np.array([feature_map]))[0]
        return feature_map
Exemplo n.º 5
0
#     batches = 0
#     for X_batch, Y_batch in izip(image_datagen.flow(data,shuffle=False,batch_size=1,seed=seed),mask_datagen.flow(labels,shuffle=False,batch_size=1,seed=seed)):
#         print X_batch.shape, Y_batch.shape
#         x = combine_channels([X_batch[0]])[0]
#         y = Y_batch[0].reshape((img_rows,img_cols))
#         print x.shape, y.shape
#         # show_images([x,y])
#         #
#         # raise
#         loss = model.train(X_batch, Y_batch)
#         batches += 1
#         if batches >= len(X_train) / 32:
#             # we need to break the loop by hand because
#             # the generator loops indefinitely
#             break




save_model(model,"aug_dunet")

from sklearn.utils import shuffle
inds = shuffle(range(len(data)))[:5]
preds = model.predict(data[inds],verbose=1)
print "preds orig:\n",preds
print "orig shape",preds.shape
data_dash = combine_channels(data[inds])
preds = preds.reshape((-1,img_rows,img_cols))
show_images(data_dash,preds/255)

Exemplo n.º 6
0
    data, labels = load_bce_data()

    shape = data[0].shape
    print data.shape, labels.shape
    data = separate_channels(
        data)  # make data compatible with theano dimension ordering
    print data.shape, labels.shape
    labels = labels.reshape(
        (-1, img_rows * img_cols)
    )  # Flatten the ground truths from 2D to 1D for evaluation of binary cross entropy at the other end of model
    data_train, data_test, labels_train, labels_test = train_test_split(
        data, labels, test_size=0.3)  # split the data into 70-30 train-test
    model.fit(data_train, labels_train, batch_size=32,
              nb_epoch=3000)  # Train the model on the train-data portion
    save_model(model, "dunet7")  # Save the model

    from sklearn.utils import shuffle

    inds = shuffle(range(len(data_test)))[:5]
    preds = model.predict(
        data_test[inds], verbose=1
    )  # Test the model on randomly picked 5 images from the test-portion
    print "preds orig:\n", preds
    print "orig shape", preds.shape
    data_dash = combine_channels(
        data_test[inds])  # Make data compatible for visualizing
    preds = preds.reshape(
        (-1, img_rows,
         img_cols))  # Reshape back the predictions from flattened array to 2D
    show_images(data_dash, preds / 255)
Exemplo n.º 7
0
to_continue = mkdir_p(result_dir)
if not to_continue:
    print("Overwrite declined, program interrupted!")
    sys.exit(0)

print "loading data.."
data, labels  = load_bce_data()
print "done"
# rand_inds = shuffle(range(len(data)))
# data = data[rand_inds]
# labels = labels[rand_inds]
data = separate_channels(data)
print "predicting.."
preds = model.predict(data,batch_size=1,verbose=1)
print "done"
data = combine_channels(data)
predicted_images = preds.reshape((-1,data[0].shape[0],data[0].shape[1]))

#data = stitch(data,1024,4)
#predicted_images = stitch(predicted_images,1024,4)
#labels = stitch(labels,1024,4)


print "saving results to "+result_dir
count = 0
results = {}
for orig_image, label_image, heat_map in tqdm(zip(data,labels,predicted_images)):
    count = count +1
    result = prepare_result(orig_image,label_image,heat_map,heat_map_metrics=[Result.BRIER_SCORE],mask_metrics=[Result.CONFUSION_MATRIX,Result.F1_SCORE,Result.ACCURACY, Result.PRECISION_SCORE, Result.RECALL_SCORE])
    result.show()
    result.save(result_dir+"/tr_"+model_name+"_"+str(count)+".jpg")