keras2.2.4使用resnet101网络和预训练模型

2022-09-05 13:49:10 浏览数 (1)

keras中直接可供使用的网络和预训练权重如下:

代码语言:javascript复制
from .vgg16 import VGG16
from .vgg19 import VGG19
from .resnet50 import ResNet50
from .inception_v3 import InceptionV3
from .inception_resnet_v2 import InceptionResNetV2
from .xception import Xception
from .mobilenet import MobileNet
from .mobilenet_v2 import MobileNetV2
from .densenet import DenseNet121, DenseNet169, DenseNet201
from .nasnet import NASNetMobile, NASNetLarge

但是,后来当我想用resnet101或者152等网络时,常规的操作是不行的。以下代码会报错:

代码语言:javascript复制
from keras.applications.resnet101 import ResNet101

经过查看keras源代码,我发现resnet101网络的定义并不在keras.applications模块中,而是在keras_applications.resnet_common模块中,于是我使用以下代码导入resnet101:

代码语言:javascript复制
from keras_applications.resnet_common import ResNet101

但是结果仍然报错,详细报错信息如下:

Traceback (most recent call last): File “/home/harley/Program/Kaggle_Competiton/histopathologic_cancer_detection/main_all.py”, line 295, in model = get_model_classif_ResNet101() File “/home/harley/Program/Kaggle_Competiton/histopathologic_cancer_detection/main_all.py”, line 247, in get_model_classif_ResNet101 weights=’imagenet’, File “/home/harley/.local/lib/python3.6/site-packages/keras_applications/resnet_common.py”, line 455, in ResNet101 **kwargs) File “/home/harley/.local/lib/python3.6/site-packages/keras_applications/resnet_common.py”, line 348, in ResNet data_format=backend.image_data_format(), AttributeError: ‘NoneType’ object has no attribute ‘image_data_format’

后来经过google查找资料,在这里发现了解决方案,原因是因为没有指定所用的keras后端,所以数据格式有问题,于是有了以下代码:

代码语言:javascript复制
from keras_applications.resnet_common import ResNet101
import keras
inputs = Input((96, 96, 3))
base_model = ResNet101(include_top=False,
                       input_tensor=inputs,
                       weights='imagenet',  
                       backend=keras.backend,
                       layers=keras.layers,
                       models=keras.models,
                       utils=keras.utils)

参考资料

Using ResNeXt in Keras 2.2.4

0 人点赞