SimJeg / FC-DenseNet

Fully Convolutional DenseNets for semantic segmentation.
487 stars 143 forks source link

Pass individual images from a city scape #27

Closed rnunziata closed 7 years ago

rnunziata commented 7 years ago

Trying to on my own images by passing them in in a loop. I did notice that if you try print the return value of the theano.function the system aborts. It looks like the return value is not correct when calling only for predictions.

    net = cf.net
    weight_path = 'weights/FC-DenseNet103_weights.npz'
    net.restore(weight_path)
    # Compile test functions
    prediction = get_output(net.output_layer, deterministic=True, batch_norm_use_averages=False)
    g = theano.function([net.input_var], prediction)   
    while(True):
       ret_val, img = cam.read()
       h = np.shape(img)[1]/4
       w = np.shape(img)[0]/4  
       img = cv2.resize(img, (h, w))               
       X = np.transpose(img, (2, 0, 1))                       
       X = X[np.newaxis,:] #(1, 3, 180, 320))        
       g_X = np.argmax(g(X), axis = 1)
       print(g_X)   #  **************   prints all zeros....[0,0,0....0,0,0]        
       image = np.reshape(g_X, (h, w))
       cv2.imshow('image',img)              
       cv2.imshow('proc',image)  # ******** displays all white
       cv2.waitKey(1)
borismagocsi commented 7 years ago

You are supposed to be normalising the image data to be between 0 and 1, I believe. And then you would have to undo that at the other end...

See also the closed issue https://github.com/SimJeg/FC-DenseNet/issues/3

rnunziata commented 7 years ago

Not sure what is happening here....this should work as suggested

       img = cv2.resize(img, (w,h)).astype(np.float32)
       norm_image = img.copy()
       cv2.normalize(img, norm_image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)      
       X = np.transpose(norm_image, (2, 0, 1)) #shape (1, 3, n_rows, n_cols)
       X = X[np.newaxis,:] 

Normalized image

('norm_image', array([[[ 0.74509805,  0.57647061,  0.43529415],
        [ 0.73725492,  0.56862748,  0.42745101],
        [ 0.74509805,  0.57647061,  0.43529415],
        ..., 
        [ 0.65882355,  0.4784314 ,  0.3137255 ],
        [ 0.66666669,  0.48627454,  0.32156864],
        [ 0.66274512,  0.48235297,  0.31764707]],

       [[ 0.74509805,  0.57647061,  0.43529415],
        [ 0.74901962,  0.58039218,  0.43921572],
        [ 0.74901962,  0.58039218,  0.43921572],
        ..., 
        [ 0.65882355,  0.4784314 ,  0.3137255 ],
        [ 0.66274512,  0.48235297,  0.31764707],
        [ 0.66274512,  0.48235297,  0.31764707]],

       [[ 0.73725492,  0.58039218,  0.43137258],
        [ 0.74117649,  0.58431375,  0.43529415],
        [ 0.73725492,  0.58039218,  0.43137258],
        ..., 
        [ 0.65882355,  0.4784314 ,  0.3137255 ],
        [ 0.66274512,  0.48235297,  0.31764707],
        [ 0.66274512,  0.48235297,  0.31764707]],

       ..., 
       [[ 0.29019609,  0.3137255 ,  0.40000004],
        [ 0.29019609,  0.3137255 ,  0.40000004],
        [ 0.29803923,  0.32156864,  0.40784317],
        ..., 
        [ 0.33725491,  0.34117648,  0.43137258],
        [ 0.33725491,  0.34117648,  0.43137258],
        [ 0.33725491,  0.34117648,  0.43137258]],

       [[ 0.28627452,  0.30980393,  0.39607847],
        [ 0.29019609,  0.3137255 ,  0.40000004],
        [ 0.29803923,  0.32156864,  0.40784317],
        ..., 
        [ 0.25882354,  0.26274511,  0.35294119],
        [ 0.23137257,  0.23529413,  0.32549021],
        [ 0.29803923,  0.3019608 ,  0.3921569 ]],

       [[ 0.28627452,  0.30980393,  0.39607847],
        [ 0.29411766,  0.31764707,  0.4039216 ],
        [ 0.29803923,  0.32156864,  0.40784317],
        ..., 
        [ 0.2392157 ,  0.24313727,  0.33333334],
        [ 0.21176472,  0.21568629,  0.30588236],
        [ 0.2392157 ,  0.24313727,  0.33333334]]], dtype=float32))

Return of g(X)

('g(X):', array([[  5.16892001e-02,   5.37014604e-01,   3.35374102e-02, ...,
          2.50593498e-02,   1.63066313e-02,   6.18744316e-03],
       [  3.82298827e-02,   6.96301639e-01,   1.91512723e-02, ...,
          7.21380720e-03,   5.82522387e-03,   1.35315082e-03],
       [  4.52625677e-02,   7.75088787e-01,   3.00194304e-02, ...,
          2.99504772e-03,   5.46914386e-03,   9.83099919e-04],
       ..., 
       [  2.79952183e-05,   1.88658465e-04,   1.46922466e-04, ...,
          7.99288973e-05,   1.00793601e-04,   9.67805900e-05],
       [  6.12435470e-05,   5.19294816e-04,   3.65711894e-04, ...,
          2.10508355e-04,   2.46379728e-04,   2.68867356e-04],
       [  1.00591476e-03,   4.57753614e-03,   3.59754357e-03, ...,
          3.20910173e-03,   2.54967832e-03,   3.21199372e-03]], dtype=float32)

from argmax:

('g_X:', array([1, 1, 1, ..., 3, 3, 3]))

this does not look right.

print of image after imwrite and imread.

[[[0 0 0] [0 0 0] [0 0 0] ..., [1 1 1] [1 1 1] [1 1 1]]

[[0 0 0] [0 0 0] [0 0 0] ..., [1 1 1] [1 1 1] [1 1 1]]

[[0 0 0] [0 0 0] [0 0 0] ..., [1 1 1] [1 1 1] [1 1 1]]

..., [[3 3 3] [3 3 3] [3 3 3] ..., [3 3 3] [3 3 3] [3 3 3]]

[[3 3 3] [3 3 3] [3 3 3] ..., [3 3 3] [3 3 3] [3 3 3]]

[[3 3 3] [3 3 3] [3 3 3] ..., [3 3 3] [3 3 3] [3 3 3]]]

borismagocsi commented 7 years ago

Here is what I am doing, which is working. I am using my own weights, not the weights from this GitHub repo. Maybe replace the cv2 normalisation with the more straightforward normalisation / 255?

   X = io.imread( imagepath ).astype("float32") / 255 
    X = transform.resize(X, (224,224) )
    # Correct where the color info is 
    X = np.transpose( X, (2, 0, 1) )
    Y = []
    Y.append(X)
    X = np.array(Y, dtype=np.float32)

    result = np.reshape(np.argmax(f(X), axis=1), (224,224))

    result = result * 255
rnunziata commented 7 years ago

thanks....but must be the published weights then....its too slow in either case for my use case.