Conv2D CNN edge detection script returns blank images

早过忘川 提交于 2019-12-11 16:45:03

问题


I want to colour the oil, water and plastic in the images shown.

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.

Please advise as to:

  1. -whether my network is appropriate,
  2. -what parameters I should be using
  3. -what training images I should be using.

#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()

回答1:


I accidentally normalised (divided by 255) my data twice. This lead to the network training against blank images and therefore producing blank images.



来源:https://stackoverflow.com/questions/56217777/conv2d-cnn-edge-detection-script-returns-blank-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!