概述
Tensorflow 2.3使用model.evaluate进行模型评估时报错tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]
- 1.软件环境⚙️
- 2.问题描述????
- 3.解决方法????
- 4.结果预览????
⚡插播一条老家自产的糖心苹果,多个品种,欢迎选购!有问题随时私信我⚡:????????来自雪域高原的馈赠——海拔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
,该数据集中包含dog
、cat
两类:
这些样本已经被打上了正确的标签(即文件夹名),我们训练出来的分类器对这些样本进行预测,如果标签对得上,那么该图片预测正确。
如果你训练的时候使用的是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 FilesJetBrainsPyCharm 2020.1pluginspythonhelperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:Program FilesJetBrainsPyCharm 2020.1pluginspythonhelperspydev_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:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythonkerasenginetraining.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythonkerasenginetraining.py", line 1379, in evaluate
tmp_logs = test_function(iterator)
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerdef_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerdef_function.py", line 846, in _call
return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds) # pylint: disable=protected-access
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerfunction.py", line 1848, in _filtered_call
cancellation_manager=cancellation_manager)
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerfunction.py", line 1924, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerfunction.py", line 550, in call
ctx=ctx)
File "C:UsersJayceAnaconda3envstf2.3libsite-packagestensorflowpythoneagerexecute.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 的报错原因都是tensor
的shape
不一致,这就很尴尬了,为什么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:UsersAnaconda3envstf2.3libsite-packagesPILImage.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
渣男!都看到这里了,还不赶紧点赞
,评论
,收藏
走一波?
最后
以上就是大力蜗牛为你收集整理的Tensorflow 2.3 model.evaluate报错InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]1.软件环境⚙️2.问题描述????3.解决方法????4.结果预览????的全部内容,希望文章能够帮你解决Tensorflow 2.3 model.evaluate报错InvalidArgumentError: Incompatible shapes: [1,64] vs. [1,128]1.软件环境⚙️2.问题描述????3.解决方法????4.结果预览????所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复