如何解决如何获得卷积神经网络的 2D 输出
我正在为康奈尔抓取数据集开发卷积神经网络。
我正在尝试使用自定义损失函数,根据输入图像将形状的预测抓取参数 (n,1,5) 与形状的真实抓取参数 (n,40,5) 进行比较,其中n 是样本数。
在自定义损失函数中,重复/复制预测的抓取参数以匹配真实抓取参数的形状,以便正确进行比较。
但是,当我尝试训练模型时,出现以下错误:
ValueError: Dimensions must be equal,but is 40 and 50 for '{{node Equal}} = Equal[T=DT_INT64,incompatible_shape_error=true](ArgMax,ArgMax_1)' 输入形状:[50,40], [50]。
我认为这与真实抓取参数的维度有关,因为当我只使用真实抓取参数 (1,5) 中的一个列表而不是列表列表时,代码运行良好。
这是模型的架构(它是一个顺序模型):
Layer (type) Output Shape Param #
=================================================================
conv2d_30 (Conv2D) (None,111,64) 1792
_________________________________________________________________
max_pooling2d_29 (MaxPooling (None,55,64) 0
_________________________________________________________________
conv2d_31 (Conv2D) (None,27,128) 73856
_________________________________________________________________
max_pooling2d_30 (MaxPooling (None,13,128) 0
_________________________________________________________________
conv2d_32 (Conv2D) (None,11,128) 147584
_________________________________________________________________
max_pooling2d_31 (MaxPooling (None,5,128) 0
_________________________________________________________________
flatten_5 (Flatten) (None,3200) 0
_________________________________________________________________
dense_50 (Dense) (None,512) 1638912
_________________________________________________________________
dense_51 (Dense) (None,256) 131328
_________________________________________________________________
dense_52 (Dense) (None,128) 32896
_________________________________________________________________
dense_53 (Dense) (None,64) 8256
_________________________________________________________________
dense_54 (Dense) (None,5) 325
=================================================================
Total params: 2,034,949
Trainable params: 2,949
Non-trainable params: 0
以及创建模型的代码:
model = tf.keras.Sequential([
Conv2D(64,kernel_size=3,strides=(2,2),activation='relu',input_shape=(224,224,3)),MaxPooling2D(pool_size=(2,2)),Conv2D(128,activation='relu'),Flatten(),Dense(512,Dense(256,Dense(128,Dense(64,Dense(5) #output layer
])
这也是自定义损失函数的代码(基于mean_squared-error):
import tensorflow.keras.backend as K
def min_mse(y_true,y_pred):
y_pred_temp = K.repeat(y_pred,K.shape(y_true)[1])
min_mse = [] # creating an empty list to store the min MSE
# we set this to 50 thus batch size for learning must always be 50
for i in range(50):
min_mse.append(K.mean(K.square(y_pred_temp[i][:] - y_true[i][:]))) # computing the MSE
return tf.convert_to_tensor(min_mse) #converting the MSE List to a Tensor
有什么办法可以让神经网络输出一个二维数组而不是单个值?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。