Tensorflow 2.3 model.evaluate报错InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]

Tensorflow 2.3使用model.evaluate进行模型评估时报错tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]

⚡插播一条老家自产的糖心苹果,多个品种,欢迎选购!有问题随时私信我⚡:🍎🍎来自雪域高原的馈赠——海拔2000米的大凉山高原生态糖心苹果,欢迎选购!!🍎🍎
在这里插入图片描述

大凉山高原生态糖心苹果

1.软件环境⚙️

Windows10 教育版64位
Python 3.6.3
Tensorflow-GPU 2.3.0
CUDA 10.1

2.问题描述🔍

我们在模型训练完时,都需要对模型的性能进行评估。而在Tensorflow.Keras中,往往通过.flow_from_directory函数读入本地的图片,然后使用model.evaluate对模型进行精度评估:

test_datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)

test_generator = test_datagen.flow_from_directory(test_dir,
                                                  shuffle=False,
                                                  target_size=(299,299),
                                                  batch_size=32)

print("================开始模型评估======================")
model_evaluation = model.evaluate(test_generator, verbose=1)

比如我们这边有一个评估数据集val-fewer-sample,该数据集中包含dogcat两类:
在这里插入图片描述
这些样本已经被打上了正确的标签(即文件夹名),我们训练出来的分类器对这些样本进行预测,如果标签对得上,那么该图片预测正确。
如果你训练的时候使用的是softmax那么不会有问题,但二分类问题我更喜欢用sigmoid,这个时候,如果是Tensorflow 2.3,就会出现报错:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]

即:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/Code/Python/classification model evaluation/model_evaluation_sigmoid.py", line 71, in <module>
    model_evaluation = model.evaluate(test_generator, verbose=1, workers=4, return_dict=True)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1379, in evaluate
    tmp_logs = test_function(iterator)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\def_function.py", line 846, in _call
    return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds)  # pylint: disable=protected-access
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\function.py", line 1848, in _filtered_call
    cancellation_manager=cancellation_manager)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\function.py", line 1924, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\function.py", line 550, in call
    ctx=ctx)
  File "C:\Users\Jayce\Anaconda3\envs\tf2.3\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [1,64] vs. [1,128]
	 [[node LogicalAnd (defined at E:/Code/Python/classification model evaluation/model_evaluation_sigmoid.py:71) ]] [Op:__inference_test_function_7318]
Function call stack:
test_function

可以看到,2个版本的Tensorflow 的报错原因都是tensorshape不一致,这就很尴尬了,为什么softmax训练出来的模型可以正常评估,但是sigmoid训练出来的模型就报错呢?
在这里插入图片描述

3.解决方法🐡

经过查询,发现是因为.flow_from_directory中的class_mode参数的默认值是categorical,而我们使用的是sigmoid进行训练:

  def flow_from_directory(self,
                          directory,
                          target_size=(256, 256),
                          color_mode='rgb',
                          classes=None,
                          class_mode='categorical',
                          batch_size=32,
                          shuffle=True,
                          seed=None,
                          save_to_dir=None,
                          save_prefix='',
                          save_format='png',
                          follow_links=False,
                          subset=None,
                          interpolation='nearest'):
    """Takes the path to a directory & generates batches of augmented data."""

因此需要将class_mode修改为binary才能用sigmoid适配,即:

test_generator = test_datagen.flow_from_directory(test_dir,
                                                  shuffle=False,
                                                  target_size=(299,299),
                                                  batch_size=32)
# 修改为:
test_generator = test_datagen.flow_from_directory(test_dir,
                                                  shuffle=False,
                                                  target_size=(299,299),
                                                  class_mode='binary',
                                                  batch_size=32)

4.结果预览🤔

修改完class_mode之后,发现模型评估可以正常运行了:

Found 12226 images belonging to 2 classes.
================开始模型评估======================
2022-09-01 13:56:51.913965: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2022-09-01 13:56:53.702954: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
C:\Users\Anaconda3\envs\tf2.3\lib\site-packages\PIL\Image.py:974: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
  "Palette images with Transparency expressed in bytes should be "
2022-09-01 13:56:55.118644: W tensorflow/stream_executor/gpu/redzone_allocator.cc:314] Internal: Invoking GPU asm compilation is supported on Cuda non-Windows platforms only
Relying on driver to perform ptx compilation. 
Modify $PATH to customize ptxas location.
This message will be only logged once.
 53/192 [=======>......................] - ETA: 28s - loss: 0.2310 - accuracy: 0.9593 - precision: 0.9521 - recall: 0.9668


渣男!都看到这里了,还不赶紧点赞评论收藏走一波?

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任博啥时候能毕业?

有用的话,请博主喝杯咖啡吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值