【问题标题】:Logits 和标签必须是可广播的:logits_size=[400,3] labels_size=[16,3]
【发布时间】:2022-01-23 15:45:27
【问题描述】:

我正在尝试建立一个预测面部表情的模型。我使用的模型:link.

我调整了数据,使其包含三个文件夹:训练、测试、验证。每个文件夹包含三个子文件夹,名为:失望、感兴趣、中立。

这就是我运行代码的方式。

image_gen=ImageDataGenerator(rotation_range=30,
                        width_shift_range=0.1,
                        height_shift_range=0.1,
                        rescale=1/255,
                        shear_range=0.2,
                        zoom_range=0.2,
                        horizontal_flip=True,
                        fill_mode='nearest')

train_image_gen=image_gen.flow_from_directory(train_dir,
                                          batch_size=batch_size,
                                          class_mode='categorical') #implemented the same code for test and validation dirs.

这是模型本身:

model.add(Conv2D(32, kernel_size=(3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 
model.add(Conv2D(64, kernel_size = (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 
model.add(Conv2D(64, kernel_size = (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 
model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(3, activation="softmax"))

model.compile(loss='categorical_crossentropy',
             optimizer=keras.optimizers.Adam(lr=0.001),
             metrics=['accuracy'])

model.fit(train_image_gen,epochs=1,steps_per_epoch= nb_train_samples/16,
                          validation_data=valid_image_gen,validation_steps=nb_valid_samples//16)

当我运行 model.fit 时,它给了我以下错误

InvalidArgumentError:  logits and labels must be broadcastable: logits_size=[400,3] labels_size=[16,3]
     [[node categorical_crossentropy/softmax_cross_entropy_with_logits
 (defined at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/keras/backend.py:5009)
]] [Op:__inference_train_function_7946]

Errors may have originated from an input operation.
Input Source operations connected to node categorical_crossentropy/softmax_cross_entropy_with_logits:
In[0] categorical_crossentropy/softmax_cross_entropy_with_logits/Reshape:   
In[1] categorical_crossentropy/softmax_cross_entropy_with_logits/Reshape_1:

Operation defined at: (most recent call last)
>>>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 193, in _run_module_as_main
>>>     return _run_code(code, main_globals, None,
>>> 
>>>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_code
>>>     exec(code, run_globals)
>>> 
>>>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ipykernel_launcher.py", line 16, in <module>
>>>     app.launch_new_instance()
>>> 
>>>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/traitlets/config/application.py", line 846, in launch_instance
>>>     app.start()
>>> 
>>>   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ipykernel/kernelapp.py", line 677, in start
>>>     self.io_loop.start()

我尝试了很多不同的方法和代码,但我总是遇到同样的错误。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    要获取有关错误的更多信息,您可以在 eager 模式下运行:

    model.compile(loss='categorical_crossentropy',
                 optimizer=keras.optimizers.Adam(lr=0.001),
                 metrics=['accuracy'], run_eagerly=True)
    

    其实输入的shape有错误,其实应该是256, 256, 3

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2021-12-12
      相关资源
      最近更新 更多