Skip to main content
 首页 » 编程设计

python之Keras 值错误 : Dimensions must be equal

2024年10月01日6zfyouxi

该模型的目标是根据与视频输入相关联的单词对视频输入进行分类。每个输入的维度为 45 帧、1 个灰色 channel 、100 像素行和 150 像素列(45、1、100、150),而每个对应的输出是 3 个可能单词之一的单热编码表示(例如"is"=> [0, 0, 1]).

模型编译过程中,出现如下错误:

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with  
input shapes: [?,100,150,1], [3,3,3,32]. 

这是用于训练模型的脚本:

video = Input(shape=(self.frames_per_sequence, 
                     1, 
                     self.rows, 
                     self.columns)) 
cnn = InceptionV3(weights="imagenet", 
                  include_top=False) 
cnn.trainable = False 
encoded_frames = TimeDistributed(cnn)(video) 
encoded_vid = LSTM(256)(encoded_frames) 
hidden_layer = Dense(output_dim=1024, activation="relu")(encoded_vid) 
outputs = Dense(output_dim=class_count, activation="softmax")(hidden_layer) 
osr = Model([video], outputs) 
optimizer = Nadam(lr=0.002, 
                  beta_1=0.9, 
                  beta_2=0.999, 
                  epsilon=1e-08, 
                  schedule_decay=0.004) 
osr.compile(loss="categorical_crossentropy", 
            optimizer=optimizer, 
            metrics=["categorical_accuracy"])  

请您参考如下方法:

根据 Convolution2D在 Keras 中,下面应该是输入和过滤器的形状。

shape of input = [batch, in_height, in_width, in_channels] 
shape of filter = [filter_height, filter_width, in_channels, out_channels] 

所以,你得到的错误的含义 -

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with  
input shapes: [?,100,150,1], [3,3,3,32]. 

[?,100,150,1] 表示 in_channels 值为 1 而 [3,3,3,32] 表示 in_channels 值为 3。这就是您收到错误的原因 - Dimensions must be equal, but are 1 and 3。

因此您可以将过滤器的形状更改为[3, 3, 1, 32]。