You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用VGG16做特征提取器时遇ValueError优化器识别错误

图像分类任务特征提取报错排查

我正在用XGBoost结合ImageNet预训练的VGG16做图像分类,环境是TensorFlow 2.11.0,数据集用的是Kaggle的CK+48。运行到提取卷积网络特征的代码feature_extractor=VGG_model.predict(x_train)时,抛出错误:ValueError: Could not interpret optimizer identifier: [],试了多种方法没解决,求帮助。


原代码
import numpy as np
import matplotlib.pyplot as plt
import glob
import cv2
import keras
from tensorflow.keras import Model
#from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activatation
from keras.models import Model, Sequential
from keras.models import load_model
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
import os
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, BatchNormalization
from keras.callbacks import TensorBoard
from keras.callbacks import ModelCheckpoint
#from keras.optimizers import adam
import seaborn as sns
from keras.applications.vgg16 import VGG16

# Read input images and assign labels based on folder names
print(os.listdir("C:/Users/Tanzeel ur Rehman/Desktop/CK+48"))

SIZE = 256  #Resize 

#Capture training data and labels into respective lists
train_images = []
train_labels = [] 


for directory_path in glob.glob("C:/Users/Tanzeel ur Rehman/Desktop/CK+48/train"):
    label = directory_path.split("\\")[-1]
    print(label)
    for img_path in glob.glob(os.path.join(directory_path, "*.jpg")):
        print(img_path)
        img = cv2.imread(img_path, cv2.IMREAD_COLOR)       
        img = cv2.resize(img, (SIZE, SIZE))
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        train_images.append(img)
        train_labels.append(label)

#Convert lists to arrays        
train_images = np.array(train_images)
train_labels = np.array(train_labels)



# Capture test/validation data and labels into respective lists

test_images = []
test_labels = [] 
for directory_path in glob.glob("C:/Users/Tanzeel ur Rehman/Desktop/CK+48/test"):
    fruit_label = directory_path.split("\\")[-1]
    for img_path in glob.glob(os.path.join(directory_path, "*.jpg")):
        img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        img = cv2.resize(img, (SIZE, SIZE))
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        test_images.append(img)
        test_labels.append(fruit_label)


#Convert lists to arrays                
test_images = np.array(test_images)
test_labels = np.array(test_labels)

#Encode labels from text to integers.
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(test_labels)
test_labels_encoded = le.transform(test_labels)
le.fit(train_labels)
train_labels_encoded = le.transform(train_labels)



#Split data into test and train datasets (already split but assigning to meaningful convention)
x_train, y_train, x_test, y_test = train_images, train_labels_encoded, test_images, test_labels_encoded


###################################################################
# Normalize pixel values to between 0 and 1
x_train, x_test = x_train / 255.0, x_test / 255.0

#One hot encode y values for neural network. 
#from keras.utils import to_categorical
#y_train_one_hot = to_categorical(y_train)
#y_test_one_hot = to_categorical(y_test)

#############################
#Load model wothout classifier/fully connected layers
VGG_model = VGG16(weights='imagenet', include_top=False, input_shape=(SIZE, SIZE, 3))

#Make loaded layers as non-trainable. This is important as we want to work with pre-trained weights
for layer in VGG_model.layers:
    layer.trainable = True
    
VGG_model.summary()  #Trainable parameters will be 0


#Now, let us use features from convolutional network for RF
feature_extractor=VGG_model.predict(x_train)

报错原因与解决方法

这个错误的核心原因是混用了原生keras和tensorflow.keras的API。TensorFlow 2.x版本已将Keras整合到内部,两个库的模块不兼容,会导致模型初始化时出现优化器识别异常。

解决步骤:

  1. 统一API导入:清理所有原生keras的导入,全部替换为tensorflow.keras下的对应模块
  2. 清理冗余代码:移除重复的导入语句(比如多次导入Sequential)
  3. 简化模型配置:仅做特征提取时,将VGG16的层设为不可训练(trainable=False),无需编译模型即可直接调用predict

修改后的关键代码片段

import numpy as np
import matplotlib.pyplot as plt
import glob
import cv2
import os
import seaborn as sns
from sklearn import preprocessing

# 统一使用tensorflow.keras的API,避免版本冲突
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, LSTM
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from tensorflow.keras.applications.vgg16 import VGG16

# 数据读取、预处理部分代码不变...

# 加载不带分类头的VGG16模型
VGG_model = VGG16(weights='imagenet', include_top=False, input_shape=(SIZE, SIZE, 3))

# 特征提取无需微调,设置所有层不可训练
for layer in VGG_model.layers:
    layer.trainable = False
    
VGG_model.summary()

# 直接提取特征
feature_extractor=VGG_model.predict(x_train)

内容的提问来源于stack exchange,提问作者Technlogics

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 15:10:41