【发布时间】:2019-10-05 17:18:23
【问题描述】:
我有一个关于水滴的视频。我已经拍摄了第一帧并手动标记了边缘。我将图像分割成更小的图像。然后,我尝试针对小标记图像训练未标记小图像的 keras 模型。
我尝试过使用“密集”层。模型训练,但损失没有改善。当我尝试使用该模型时,它只会给我一个黑色图像输出。
#################### IMPORT AND SPLIT
from cam_img_split import cam_img_split
import cv2
img_tr_in=cv2.imread('frame 1.png')
img_tr_out=cv2.imread('frame 1 so far.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] #images selected for training
##################### NEURAL NETWORK
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import adam
b_sha=np.prod(tr_in.shape[2:5]) #batch shape
model = Sequential()
model.add(Dense(b_sha, activation='relu'))
model.add(Dense(3072, activation='softmax'))
model.add(Dense(3072, activation='softmax'))
model.add(Dense(3072, activation='softmax'))
model.add(Dense(np.prod(tr_out.shape[2:5]), activation='softmax'))
model.compile(optimizer=adam(lr=0.1), 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([np.prod(pl),b_sha]) #Flattening
tr_out_sel_flat=tr_in_sel.reshape([np.prod(pl),b_sha])
tr_in_sel_flat_norm=tr_in_sel_flat/255
tr_out_sel_flat_norm=tr_out_sel_flat/255
model.fit(tr_in_sel_flat_norm, tr_out_sel_flat_norm, epochs=10, batch_size=pl[0])
我希望输出匹配带有标记边缘的图像。相反,我得到了黑色图像输出。
【问题讨论】:
-
你不应该使用 softmax 作为隐藏层的激活。
-
谢谢,我应该使用哪个激活函数?
-
通常坚持使用 ReLU 激活效果很好,除非您有一些特定的转换。另外,既然您基本上是在寻找边缘,那么卷积似乎不是比密集层更理想吗?
-
你也可以使用 sigmoid,你可以 print(model.summary()) 吗?
-
谢谢!我在顶部的链接中添加了模型摘要的图像。
标签: python image-processing machine-learning keras edge-detection