jakeret / tf_unet

Generic U-Net Tensorflow implementation for image segmentation
GNU General Public License v3.0
1.9k stars 748 forks source link

Always having a blank white image as prediction #224

Closed abderhasan closed 5 years ago

abderhasan commented 5 years ago

After training the model and trying to predict the segmentation, I always get a blank white image. In some other question, it seems this can be solved by changing the clipping maybe?

Where can I make such change in the code?

Thanks.

jakeret commented 5 years ago

The relevant code for loading an preprocessing the data is here: https://github.com/jakeret/tf_unet/blob/master/tf_unet/image_util.py

abderhasan commented 5 years ago

Thanks for your kind reply. I have the trained model saved, and when viewing the epochs it shows good predictions. I'm having the blank result when trying to use the saved model for prediction.

This is the test code I'm using which is returning a blank image (based on #173)

from tf_unet import unet
import cv2
import numpy as np

test_images = []

net = unet.Unet(channels=3, n_class=2, layers=3, features_root=64)

filelist = ['ISIC_0000000.jpg']

test_images = np.array([cv2.imread(fname, 1) for fname in filelist])

prediction = net.predict("/home/me/Desktop/tf_unet/results/model.ckpt", test_images)

output = prediction[0, :, :, 1] * 1000
# Times 1000 because it is hardly visible otherwise

cv2.imwrite("prediction.jpg", output)

Would the changes be made here or in image_util.py?

Thanks.

jakeret commented 5 years ago

The tf_unet implementation performs normalization of your input data. By loading the validation data by hand the value ranges are very different. Hence the bad results. You should rather use a new instance of ImageDataProvider to load the validation set

abderhasan commented 5 years ago

Can you kindly provide a sample example just to make the idea more clear?

Thanks a lot.

abderhasan commented 5 years ago

I also noticed that the predicted image is of dimension 132x132, while the image I'm trying to predict is of size 512x512. Maybe this is why the predicted result I get is black?

Any edits I have to make to the saved model?

Thanks.

jakeret commented 5 years ago

You have to create an instance of the ImageDataProvider like you did for the training data.

The output of the network is always smaller. This is expected behaviour due to the network architecture. There is a good explaination on this in the original paper an plenty of discussions in Github issues. One of the reasons for "black" output is that the gradients get very large (explode) - there are various for that. You have to explore different common preprocessing techniques to see if there is a way to get around this. There is no absolute solution as with many ML problems

abderhasan commented 5 years ago

Thanks for your reply. For instance, I did the following:

net = unet.Unet(channels=3, n_class=2, layers=6, features_root=100)
filelist = ['abc.jpg']
test_images = np.array([np.asarray(Image.open(fname)) for fname in filelist])
prediction = net.predict('/project/6012565/abder/tf_unet/results/model.ckpt', test_images)
output = prediction[0, :, :, 1]

How can I create an instance of the ImageDataProvider in the above code snippet? I know how to create an instance, what I meant is how it would look like and the parameters I should pass?

An example on that (i.e. line of code) would be very appreciated.

jakeret commented 5 years ago

Something along the line

data_provider = image_util.ImageDataProvider("test/*.tif")
test_images = data_provider(test_batch_size)
prediction = net.predict('/project/6012565/abder/tf_unet/results/model.ckpt', test_images)
abderhasan commented 5 years ago

Thanks so much. I did the following:

from tf_unet import unet, util, image_util

test_batch_size = 1

data_provider = image_util.ImageDataProvider('test-images/*.jpg')
test_images = data_provider(test_batch_size)
prediction = net.predict('/home/abder/Desktop/tf_unet/results/model.ckpt', test_images)

output = prediction[0, :, :, 1] * 1000

And, got the following:

IOError: [Errno 2] No such file or directory: u'test-images/image001_mask.jpg

Why did _mask show up here. Images with suffix _mask where the ground truth images used while training.

Thanks.

Billsbob commented 5 years ago

^abderhasan were you able to resolve your error? IOError: [Errno 2] No such file or directory: u'test-images/image001_mask.jpg I'm stuck at the same place with a trained model and trying to input new data to get a prediction.