Keras中ConvNeXt系列模型是否被移除?调用ConvNeXtTiny报错求助
解决Keras中
ConvNeXtTiny的AttributeError问题 问题背景
运行代码时触发错误:AttributeError: module 'keras.applications' has no attribute 'ConvNeXtTiny',相关代码、导入语句及环境版本如下:
报错代码
filename = "ConvNextTiny_firstpass_model" # layer construction base_model = applications.ConvNeXtTiny( #preproccing included input_shape=(targetWidth, targetHeight, 3), include_top=False, ) base_model.trainable = False flatten_layer = layers.Flatten() fc_layer = layers.Dense(1024, activation='relu') dropout_layer = layers.Dropout(0.3) #layer connecting x = flip_layer(input_layer) x = base_model(x, training=False) x = flatten_layer(x) x = fc_layer(x) x = dropout_layer(x) predictions = output_layer(x) model = keras.Model(input_layer, predictions)
导入语句
import tensorflow as tf import keras from keras import layers from keras import optimizers from keras import applications from keras import losses from keras import callbacks import pandas as pd import numpy as np from matplotlib import pyplot as plt import cv2 as cv import csv from sklearn.utils import shuffle
环境版本
ipython==8.5.0 tensorflow==2.10.0 keras==2.10.0 Keras-Preprocessing==1.1.2 pandas==1.4.4 numpy==1.23.3 matplotlib==3.6.0 opencv-python==4.6.0.66 sklearn==0.0
用户疑问:ConvNeXt系列模型是否已从Keras中移除?
问题原因与解决方法
ConvNeXt并未从Keras移除,版本不兼容是核心问题
Keras 2.10.0(对应TensorFlow 2.10.0)的keras.applications模块尚未集成ConvNeXt系列模型。该系列模型是在Keras 2.12.0及更高版本中才正式加入到官方应用模块的。三种可行解决方案
- 升级TensorFlow与Keras:直接升级到TensorFlow 2.12.0及以上版本(TensorFlow集成的Keras版本会同步更新),执行命令:
升级后可直接沿用原有代码调用pip install --upgrade tensorflowapplications.ConvNeXtTiny()。 - 通过TensorFlow Hub加载模型:若不想升级环境,可使用TensorFlow Hub加载预训练的ConvNeXt Tiny模型,示例代码:
import tensorflow_hub as hub base_model = hub.KerasLayer( "https://tfhub.dev/google/imagenet/convnext_tiny_1k_224/feature_vector/2", input_shape=(targetWidth, targetHeight, 3), trainable=False ) - 使用Keras CV扩展库:安装
keras-cv库(需对应较新的TensorFlow版本),通过该库加载ConvNeXt模型,示例代码:from keras_cv.models import ConvNeXtTiny base_model = ConvNeXtTiny( input_shape=(targetWidth, targetHeight, 3), include_top=False )
- 升级TensorFlow与Keras:直接升级到TensorFlow 2.12.0及以上版本(TensorFlow集成的Keras版本会同步更新),执行命令:
注意事项
无论采用哪种方式,需确保输入图像的尺寸与模型要求匹配(ConvNeXt Tiny默认输入尺寸为224×224)。
内容的提问来源于stack exchange,提问作者seth
相关产品推荐
相关产品推荐

