jakeret / tf_unet

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

can not predict images by already trained model #273

Open kxhaaa opened 5 years ago

kxhaaa commented 5 years ago

i have already trained a model and get a good prediction image.

but when i try to load the model and predict other images, i find the results of prediction images are all black.

if i run this code, the result is good :

data, y_test = data_provider(3)

prediction = net.predict(trainpath, data)

print("Testing error rate: {:.2f}%".format(unet.error_rate(prediction,
                                                               util.crop_to_shape(y_test, prediction.shape))))

unet.error_rate(prediction, util.crop_to_shape(y_test, prediction.shape))
img = util.combine_img_prediction(data, y_test, prediction)
util.save_image(img, "prediction.jpg")

and if i use the code, the result is almost black (cell_1.tif is one of the training images):

from tf_unet import unet, util
import numpy as np

import cv2

model_path = 'd:/NET/predict/model.ckpt'

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

image_path=("d:/NET/data/cell_1.tif")
data = np.reshape(a=cv2.imread(image_path, flags=0), newshape=(1, 400,400, 1))
prediction = net.predict(model_path, data)
prediction=util.to_rgb(prediction[..., 1].reshape(-1, prediction.shape[2], 1))
util.save_image(prediction, "d:/NET/predict/cell_1.tif")
jakeret commented 5 years ago

it's probably a bit late, but the reason for this behaviour might be because the images are not preprocessed as the ones used for training (e.g. scaling to [0,1), removing outliers, etc)

kxhaaa commented 5 years ago

it's probably a bit late, but the reason for this behaviour might be because the images are not preprocessed as the ones used for training (e.g. scaling to [0,1), removing outliers, etc)

thanks for your reply. and in fact, currently i just use image_util.ImageDataProvider with a '0' label to load the image, though it looks like a little 'lazy',o( ̄▽ ̄)ブ.