jakeret / tf_unet

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

Questions about prediction #146

Open Nearby36 opened 6 years ago

Nearby36 commented 6 years ago

tf_unet is easy to be understood and the code you've provided is really helpful for me to do the segmentation. I met some questions when I made my own data as the input.

  1. where is the "model.cpkt" file? I can't find it in the './unet_model' , which is the directory where I save my model.
  2. I put my own image data into an array with shape (1, nx, ny, 1), and then as the input of net.predict(). The prediction result I got was an array with 0 and nan. I am confused and don't know how to do it next. def load_data_unit(path): test_data = np.zeros((1,500,500,1)) img = np.array(Image.open(path), dtype=np.float32) test_data[0,...,0] = img return test_data

data_provider = image_util.ImageDataProvider(search_path=r"png_M_train_resize/*.png", data_suffix = u'.png', mask_suffix='_mask.png')

net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2) trainer = unet.Trainer(net) path = trainer.train(data_provider, './unet_model', training_iters=32, epochs= 100, display_step=10)

test_data = load_data_unit(r"./test/11.png") prediction = net.predict(r"./unet_model/model.cpkt", test_data)

  1. After reading the doc of tf_unet, I found the store_prediction function. Does it mean that I can save the segmentation results by using this function? Any help is appreciated, thank you.
jakeret commented 6 years ago

1) the model state is stored after each epoch. The path returned by the trainer points to the exact location 2) this could indicate that the model struggled to learn any proper segmentation. Maybe you need to experiment with the network architecture. You should also check that the input data is properly normalized 3) this method stores the validation prediction during the training process

Nearby36 commented 6 years ago

Thanks for your timely reply. I realize that the path returned from the trainer contains the file 'model.cpkt', but I can't find a file named exactly 'model.cpkt' in that directory. So, 'model.cpkt.data-0000', 'model.cpkt.index', 'model.cpkt.meta' are related to it, right?

What do you mean by advising me to check whether the input data is properly normalized? from the doc, I see the shape of x_test is [n, nx, ny, channel], I made this by code below: test_data= np.zeros((1,500,500,1)) img = np.array(Image.open(path), dtype=np.float32) test_data[0,...,0]= img path is the image data path. I'm not sure whether it's right. Maybe it's where the issue exists.

jakeret commented 6 years ago

Do you see the same behaviour when you run the toy model?

By normalization I mean that the data has to be in the range of [0,1). In the ImageDataProvider I to it like this: https://github.com/jakeret/tf_unet/blob/master/tf_unet/image_util.py#L70

Nearby36 commented 6 years ago

@jakeret Thanks very much!! After doing the normalization as you said, I got the mask results. How many training samples do you suggest when doing the segmentaion? I am using your model to do the medical image segmentation, like hand x-ray image.

jakeret commented 6 years ago

This is great news! Happy to hear that Generally, in deep learning, more is alway better. Collect as much data as you can and then maybe also look into data augmentation techniques to create even more training data

Nearby36 commented 6 years ago

I've read issue #41 , #91 and other issues related to output shape. As explained by @AlibekJ in #91

You need to find out how output pixels relate to the original ones and just multiply the coordinates accordingly. or

add borders to input images prior to feeding them into tf_unet.

For example, the original input image is (500,500), the output is (460,460). If we want to get the output the same size as the original input, then we should add borders to the input images prior to feeding them into the model. That is, the input image should be (500500/460, 500500/460). As to the opencv method from #41 img = cv2.copyMakeBorder(img, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType=cv2.BORDER_CONSTANT, value=[0, 0, 0]) bordersize=500*500/460-500 I'm not sure if my understanding is correct. Hope to get the reply.