unet icon indicating copy to clipboard operation
unet copied to clipboard

用自己的数据训练后,输出的预测结果全黑

Open sanersbug opened this issue 5 years ago • 23 comments

不知道咋搞的,预测结果输出来是全黑的,我乘255以后就是灰色的了,看了下值,都是在127左右,怎么也想不通究竟哪里有问题,大家有遇到这个问题的吗?能给个方向吗?我感觉是数据有问题,但不知道怎么入手

sanersbug avatar Aug 17 '18 01:08 sanersbug

输出的是 sigmoid 结果,值域范围[0,1],做一下处理就好

y_pred = model.predict(image)
y_pred = (y_pred > 0.5).astype(np.uint8).reshape(IMAGE_H, IMAGE_W)
plt.imshow(np.squeeze(y_pred), plt.cm.gray)

请参考我的代码 TFLearn U-Net Starter https://github.com/SixQuant/U-Net/blob/master/kernel.ipynb

SixQuant avatar Aug 30 '18 11:08 SixQuant

@SixQuant 好的,谢谢了,我试一下

sanersbug avatar Aug 31 '18 00:08 sanersbug

我跟你遇到了一样的问题,怎么解决的?

xhsoldier avatar Sep 03 '18 09:09 xhsoldier

不知道咋搞的,预测结果输出来是全黑的,我乘255以后就是灰色的了,看了下值,都是在127左右,怎么也想不通究竟哪里有问题,大家有遇到这个问题的吗?能给个方向吗?我感觉是数据有问题,但不知道怎么入手

问一下,自己的数据需要怎样的处理。我用自己数据跑会出现维度问题ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (1, 256, 256, 3, 1)

zhaojingji avatar Sep 14 '18 03:09 zhaojingji

不知道咋搞的,预测结果输出来是全黑的,我乘255以后就是灰色的了,看了下值,都是在127左右,怎么也想不通究竟哪里有问题,大家有遇到这个问题的吗?能给个方向吗?我感觉是数据有问题,但不知道怎么入手

问一下,自己的数据需要怎样的处理。我用自己数据跑会出现维度问题ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (1, 256, 256, 3, 1)

你多个了一个维度,读出来的图片应该是(256, 256, 3)的,不知道你咋就搞成(1, 256, 256, 3, 1)了。不过没关系,reshape 一下 reshape(1, 256, 256, 3)

SixQuant avatar Sep 14 '18 09:09 SixQuant

不知道咋搞的,预测结果输出来是全黑的,我乘255以后就是灰色的了,看了下值,都是在127左右,怎么也想不通究竟哪里有问题,大家有遇到这个问题的吗?能给个方向吗?我感觉是数据有问题,但不知道怎么入手

问一下,自己的数据需要怎样的处理。我用自己数据跑会出现维度问题ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (1, 256, 256, 3, 1)

你多个了一个维度,读出来的图片应该是(256, 256, 3)的,不知道你咋就搞成(1, 256, 256, 3, 1)了。不过没关系,reshape 一下 reshape(1, 256, 256, 3)

是啊,我用这个代码跑rgb图的,多了后面那个维度,然后改了一下代码,定义geneTrainNpy函数时少加一维就可以跑了

zhaojingji avatar Sep 14 '18 09:09 zhaojingji

楼主, 可以尝试调整一下 Adam 的参数--初始化学习率, 比如 将 lr=1e-4 改为 3e-4. 希望楼主有用.

shiyanrubing avatar Oct 16 '18 14:10 shiyanrubing

请问楼主解决了吗,怎么解决啊

pTOvOTq avatar Nov 06 '18 01:11 pTOvOTq

个人建议:(1)考虑设置不同的随机初始化种子,比如seed=0或者1;(2)考虑在卷积操作之后,增加batch normalization 或者 dropout.

shiyanrubing avatar Nov 06 '18 01:11 shiyanrubing

你说这些,不起作用啊

pTOvOTq avatar Nov 06 '18 12:11 pTOvOTq

@sanersbug @xhsoldier 请问你们解决了吗

pTOvOTq avatar Nov 06 '18 12:11 pTOvOTq

You can try more epochs to improve the acc . Another place to check is the function of adjustData. def adjustData(img,mask,flag_multi_class,num_class): #here# elif(np.max(img) > 1): img = img / 255 mask = mask /255
mask[mask > 0.5] = 1 mask[mask <= 0.5] = 0 return (img,mask) This code works for py3, if you use py2, modify '255' to '255.'; Last, you need to check the threshold '0.5' in order to make sure the returned mask is a binary(0/1) mask.

JianyingLi avatar Dec 03 '18 02:12 JianyingLi

楼上的各位大佬,有个问题想咨询一下。 我在博主的代码里看到好多DROPOUT层,但是从网络的结构图上没看到,请问这些是自己加得吗? 还有个问题,从网络结构图上看最后一个箭头代表1*1的卷积核,但是为什么博主的代码里是 conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) 而不是 conv9 = Conv2D(2, 1, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) ,十分感谢!

louxy126 avatar Dec 04 '18 11:12 louxy126

请问楼主你的问题解决了吗 ?@sanersbug

wuyang0329 avatar Dec 25 '18 13:12 wuyang0329

楼上的各位大佬,有个问题想咨询一下。 我在博主的代码里看到好多DROPOUT层,但是从网络的结构图上没看到,请问这些是自己加得吗? 还有个问题,从网络结构图上看最后一个箭头代表1*1的卷积核,但是为什么博主的代码里是 conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) 而不是 conv9 = Conv2D(2, 1, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) ,十分感谢!

dropout层是为了防止过拟合才加上去的,如果你训练出来的模型没有过拟合的情况,可以不用加上这些层

wuyang0329 avatar Dec 26 '18 01:12 wuyang0329

不知道咋搞的,预测结果输出来是全黑的,我乘255以后就是灰色的了,看了下值,都是在127左右,怎么也想不通究竟哪里有问题,大家有遇到这个问题的吗?能给个方向吗?我感觉是数据有问题,但不知道怎么入手

我也遇到了相同的问题,确实是数据的问题,首先训练的数据必须是tif格式图片,不然出来的结果肯定是全黑的,还有图片的大小也有影响。 我自己改过之后的代码你可以参考一下:https://github.com/wuyang0329/unet

wuyang0329 avatar Dec 28 '18 10:12 wuyang0329

hi @sanersbug ,

Do you resolve this problem now? if you are done, please share some details. Thanks.

frankdeepl avatar Jan 11 '19 06:01 frankdeepl

hi @sanersbug ,

Do you resolve this problem now? if you are done, please share some details. Thanks.

I have resloved this problem ,you can see this:https://github.com/wuyang0329/unet

wuyang0329 avatar Jan 11 '19 06:01 wuyang0329

hi @wuyang0329 , When I tried to run your code and original data, I faced below problem...do you know how to fix it?

Traceback (most recent call last): File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1664, in main() File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/dongs/Documents/Project/unet-road/main.py", line 24, in results = model.predict_generator(test_data,steps=20,verbose=1) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1522, in predict_generator verbose=verbose) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training_generator.py", line 435, in predict_generator generator_output = next(output_generator) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 709, in get six.reraise(*sys.exc_info()) File "/home/dongs/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 685, in get inputs = self.queue.get(block=True).get() File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 670, in get raise self._value File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 626, in next_sample return six.next(_SHARED_SEQUENCES[uid]) File "/home/dongs/Documents/Project/unet-road/data.py", line 110, in testGenerator img = io.imread(os.path.join(self.test_path,filename),as_gray=True) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_io.py", line 61, in imread img = call_plugin('imread', fname, plugin=plugin, **plugin_args) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/manage_plugins.py", line 211, in call_plugin return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_plugins/tifffile_plugin.py", line 31, in imread return tif.asarray(**kwargs) TypeError: asarray() got an unexpected keyword argument 'as_gray'

frankdeepl avatar Jan 11 '19 07:01 frankdeepl

hi @wuyang0329 , When I tried to run your code and original data, I faced below problem...do you know how to fix it?

Traceback (most recent call last): File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1664, in main() File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/dongs/Documents/Project/unet-road/main.py", line 24, in results = model.predict_generator(test_data,steps=20,verbose=1) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1522, in predict_generator verbose=verbose) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training_generator.py", line 435, in predict_generator generator_output = next(output_generator) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 709, in get six.reraise(*sys.exc_info()) File "/home/dongs/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 685, in get inputs = self.queue.get(block=True).get() File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 670, in get raise self._value File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 626, in next_sample return six.next(_SHARED_SEQUENCES[uid]) File "/home/dongs/Documents/Project/unet-road/data.py", line 110, in testGenerator img = io.imread(os.path.join(self.test_path,filename),as_gray=True) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_io.py", line 61, in imread img = call_plugin('imread', fname, plugin=plugin, **plugin_args) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/manage_plugins.py", line 211, in call_plugin return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_plugins/tifffile_plugin.py", line 31, in imread return tif.asarray(**kwargs) TypeError: asarray() got an unexpected keyword argument 'as_gray'

maybe you should check your skicit-image’s version. Mine is 0.14.1

wuyang0329 avatar Jan 11 '19 07:01 wuyang0329

hi @wuyang0329 , When I tried to run your code and original data, I faced below problem...do you know how to fix it?

Traceback (most recent call last): File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1664, in main() File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/home/dongs/Documents/Software/pycharm-community-2018.2.4/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/dongs/Documents/Project/unet-road/main.py", line 24, in results = model.predict_generator(test_data,steps=20,verbose=1) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1522, in predict_generator verbose=verbose) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/engine/training_generator.py", line 435, in predict_generator generator_output = next(output_generator) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 709, in get six.reraise(*sys.exc_info()) File "/home/dongs/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 685, in get inputs = self.queue.get(block=True).get() File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 670, in get raise self._value File "/home/dongs/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/home/dongs/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py", line 626, in next_sample return six.next(_SHARED_SEQUENCES[uid]) File "/home/dongs/Documents/Project/unet-road/data.py", line 110, in testGenerator img = io.imread(os.path.join(self.test_path,filename),as_gray=True) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_io.py", line 61, in imread img = call_plugin('imread', fname, plugin=plugin, **plugin_args) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/manage_plugins.py", line 211, in call_plugin return func(*args, **kwargs) File "/home/dongs/anaconda3/lib/python3.6/site-packages/skimage/io/_plugins/tifffile_plugin.py", line 31, in imread return tif.asarray(**kwargs) TypeError: asarray() got an unexpected keyword argument 'as_gray'

Maybe you are using skimage version 0.13, you can see help(skimage.io.imread) and find the argument is actually as_grey, hope this might help

Krisloveless avatar Mar 11 '19 16:03 Krisloveless

我自己的训练数据是rgb的,这个项目默认的数据是灰度的,所以有些地方要改,参照这个问答就可以了,https://github.com/zhixuhao/unet/issues/59

sanersbug avatar Jun 04 '19 03:06 sanersbug

我也遇到类似的情况,我把网络改成1维的了,训练集每一个样本里大概2000个0,10个1,我训练出来结果大概都是0.47,我考虑可能是正负样本分布不均造成的,我给交叉熵加入了权重,然后出现了过拟合现象,在测试集上都是错的,后来我有把代码该成每一个epoch洗牌一次,可是这样真的就学习不到特征了,很苦恼,我也不知道怎么解决。

HaiNanJim avatar Mar 05 '20 08:03 HaiNanJim