我使用u-net模型训练一些图片,其中有3个缺陷,损失是“ categorical_crossentropy”,矩阵是预测和真实之间的IOU(联合之上的交集)。当我完成训练后,结果是好的,IOU达到了87%,损耗低于7.4443e-04,但是经过预测,即使测试样本仍然没有任何结果。下面是比较真实标签和预测标签的测试代码。
a = 0
for i in training.label_name_val:
if '\\' in i and a<100:
a+=1
raw_img = cv2.imread(i.replace('.png','.bmp'),0)
image=np.expand_dims(raw_img,axis=2)
img = np.zeros((1,608,528,1),dtype=np.float32)
img[0,:,:,:] = np.array(image/255,dtype='float32')
mask = cv2.imread(i)
infer_img = training.model.predict(img,batch_size=16)
infer_img = np.argmax(infer_img,axis=-1)
plt.figure(figsize=(20,20))
plt.subplot(1,3,1)
plt.imshow(mask)
plt.subplot(1,3,2)
plt.title(np.unique(infer_img))
plt.imshow(infer_img[0,:,:],cmap='jet')
结果:左为真标签,右为预测标签。 enter image description here
在这里我添加了结果数组的一部分,看起来好的预测(第一个)接近1,缺陷预测(后3个数字)接近0。这与训练结果的逻辑大不相同。
[[[[1.00000000e+00 1.53722974e-21 4.75415969e-17 5.70772332e-32]
[1.00000000e+00 1.90659765e-31 5.15209822e-23 0.00000000e+00]
[1.00000000e+00 1.34475142e-34 2.92512058e-26 0.00000000e+00]
...
[1.00000000e+00 1.35033362e-27 9.07716726e-20 0.00000000e+00]
[1.00000000e+00 4.04645854e-23 1.60991434e-15 0.00000000e+00]
[1.00000000e+00 3.18021596e-13 1.09597352e-10 4.99951517e-22]]
[[1.00000000e+00 6.41531835e-37 7.19892247e-28 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 1.90926201e-38 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
...
[1.00000000e+00 0.00000000e+00 1.11940014e-30 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 2.47139768e-25 0.00000000e+00]
[1.00000000e+00 5.32593445e-23 8.56831213e-16 1.92205218e-36]]
[[1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
...
[1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[1.00000000e+00 0.00000000e+00 1.01963658e-32 0.00000000e+00]
[1.00000000e+00 4.66948815e-31 3.11143341e-20 0.00000000e+00]]
...