orobix / retina-unet

Retina blood vessel segmentation with a convolutional neural network

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prediction for a single image

threefivehill opened this issue · comments

Hi,

I used your best weights to predict for a single test image. I revised the code (see attachments) and it generates very noisy image. In the background there are so many false-positives. I applied the same pre-processing steps as you did (by looking at the documentation). I use training values for the standart deviation and mean. The result and the code are at the attachment. Am I doing wrong in somewhere?
I changed the priority for the channel order as "first channel" since I use Keras. Could you please help me figure out this issue?
predicted

from keras.models import model_from_json
import cv2
import copy
from matplotlib import pyplot as plt
import numpy as np


def prepocess_data(the_img):
    #normalize
    #std:55.851990770984536, img_mean:83.96562255652191
    img_std = 55.851990770984536 #np.std(the_img)
    img_mean = 83.96562255652191#np.mean(the_img)
    img_normalized = (the_img - img_mean)/img_std
    img_normalized = ((img_normalized-np.min(img_normalized)) / (np.max(img_normalized)-np.min(img_normalized)))*255

    #clahe preporcess
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    clahe_processed = clahe.apply(np.array(img_normalized, dtype=np.uint8))

    #gama correction
    gamma = 1.2
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
    gama_img =  cv2.LUT(np.array(clahe_processed, dtype = np.uint8), table)

    #finally return res
    return gama_img

def predictImage(inImg, theModel):
    stepSize = 24
    windowSize = 48
    prob_threshold = 0.99

    #create zero img
    outImg = np.zeros((inImg.shape[0], inImg.shape[1]), dtype=inImg.dtype)#outImg with zeros
    print inImg.shape
    for x in range(48, inImg.shape[0]-windowSize, stepSize):
        for y in range(48, inImg.shape[1]-windowSize, stepSize):
            #sub image rectangle
            subImg =copy.copy(inImg[x-windowSize//2:x+windowSize//2, y-windowSize//2:y+windowSize//2])
            subImg = prepocess_data(subImg)
            cnnShape = np.reshape(subImg,(1,1,48,48))
            predictions = theModel.predict(cnnShape, batch_size=32, verbose=2) #(1,2304,2)
            #reshape out to img
            positives = (predictions[0,:,1] > prob_threshold).astype(inImg.dtype)
            positives = np.reshape(positives,(48,48))
            outImg[x-windowSize//2:x+windowSize//2, y-windowSize//2:y+windowSize//2] = positives
    return outImg


baseFolder = './test/'
#load model
jsonFile= open(baseFolder+'test_architecture.json','r')
jsonString = jsonFile.read()
jsonFile.close()
cnnModel = model_from_json(jsonString)
#load models
cnnModel.load_weights(baseFolder+'test_best_weights.h5')
#cnnModel.summary()

#read img
img_path = 'DRIVE/images/01_test.tif' #DRIVE data, test image 1
img = cv2.imread(img_path)
b,g,r = cv2.split(img) # also tried your gray scale version, the results even worse. I use direclty g channel as gray scale

#cv2.imshow('OriginalG',g);
#preprocess
g = prepocess_data(g)

#cv2.waitKey(-1)

print('Please wait, image is predicting...')
outImg = predictImage(g, cnnModel)

cv2.imwrite('predicted.jpg',outImg[:,:]*255 )
plt.subplot(121),plt.imshow(g,'gray'), plt.title('ORIGINAL')
plt.subplot(122),plt.imshow(outImg[:,:],'gray'), plt.title('PREDICTED1')
plt.show()

del cnnModel