Conv2D CNN边缘检测脚本返回空白图像 [英] Conv2D CNN edge detection script returns blank images
本文介绍了Conv2D CNN边缘检测脚本返回空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想给所示图像中的油,水和塑料上色.
I want to colour the oil, water and plastic in the images shown.
当前,我分割训练图像(仅使用颜色正确的部分).然后,我训练一个conv2D网络并绘制预测.
Currently I split up the training image (only using the parts which are coloured properly). I then train a conv2D network and plot the predictions.
运行它时,我得到空白的蓝色或黑色图像作为回报.
When I run it, I get blank blue or black images in return.
请告知:
- -我的网络是否合适
- -我应该使用什么参数
- -我应该使用什么训练图像.
#IMPORT AND SPLIT
from cam_img_split import cam_img_split
import cv2
import numpy as np
img_tr_in=cv2.imread('frame 1.png')
img_tr_out=cv2.imread('Red edge.png')
seg_shape=[32,32]
tr_in=cam_img_split(img_tr_in,seg_shape)
tr_out=cam_img_split(img_tr_out,seg_shape)
pl=[4,20]
##################### NEURAL NETWORK
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv2D, MaxPooling2D
from keras.optimizers import adam
import matplotlib.pyplot as plt
pad=3
input_shape=(seg_shape[0]+2*pad,seg_shape[1]+2*pad,3)
model = Sequential()
model.add(Conv2D(32, (3, 3),input_shape=input_shape, activation='relu'))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(Conv2D(3, (3, 3),input_shape=input_shape, activation='relu'))
model.compile(optimizer=adam(lr=0.001), loss='mean_squared_error', metrics=['accuracy'])
tr_in_sel=tr_in[0:pl[0],0:pl[1],:,:,:]
tr_out_sel=tr_out[0:pl[0],0:pl[1],:,:,:]
tr_in_sel_flat=tr_in_sel.reshape([pl[0]*pl[1],seg_shape[0],seg_shape[1],3])
tr_out_sel_flat=tr_out_sel.reshape([pl[0]*pl[1],seg_shape[0],seg_shape[1],3])
tr_in_sel_flat_norm=tr_in_sel_flat/255
tr_out_sel_flat_norm=tr_out_sel_flat/255
from cam_pad import cam_pad
tr_in_sel_flat_norm_pad=np.zeros(tr_in_sel_flat.shape+np.array([0,2*pad,2*pad,0]))
for n3 in range(0,tr_in_sel_flat.shape[0]):
for n4 in range(0,tr_in_sel_flat.shape[3]):
tr_in_sel_flat_norm_pad[n3,:,:,n4]=cam_pad(tr_in_sel_flat_norm[n3,:,:,n4], pad)
model.fit(tr_in_sel_flat_norm_pad, tr_out_sel_flat_norm, epochs=10, batch_size=int(pl[0]/2),shuffle=True)
n_ch=10
img_check=np.zeros([n_ch,seg_shape[0]+2*pad,seg_shape[1]+2*pad,3])
for n8 in range(0,n_ch):
for n5 in range(0,3):
img_check[n8,:,:,n5]=cam_pad(tr_in_sel_flat_norm[n8,:,:,n5],pad)
pred = model.predict(img_check/255)
pred_img=(pred.reshape([n_ch,seg_shape[0],seg_shape[1],3]))
for n9 in range(1,n_ch):
plt.subplot(n_ch,1,n9)
plt.imshow(pred_img[n9-1,:,:,:])
plt.show()
推荐答案
我不小心将数据标准化了两次(除以255).这样就可以针对空白图像进行网络训练,从而生成空白图像.
I accidentally normalised (divided by 255) my data twice. This lead to the network training against blank images and therefore producing blank images.
这篇关于Conv2D CNN边缘检测脚本返回空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文