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

使用TensorFlow Hub预训练鸟类模型时验证精度低及参数显示0的问题

鸟类图像分类任务问题:预训练模型参数显示为0+精度不达标

问题背景

基于Google Colab平台,使用TensorFlow Hub中针对964种鸟类训练的预训练模型aiy/vision/classifier/birds_V1/1执行10种鸟类的图像分类任务,遇到以下问题:

  • 训练后验证精度远低于预期,损失值偏高
  • model.summary()显示KerasLayer的参数数量为0,存在疑问
  • 增加训练轮数时精度反而下降,仅训练1轮仍无法达标

代码重现

import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
dataset_path = '/content/gdrive/MyDrive/images/'

image_size = (224,224)
batch_size = 10

train_datagen = ImageDataGenerator(rescale=1./255, 
                                   rotation_range=15,
                                   zoom_range = (0.95,0.95),
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   validation_split=0.2,
                                   dtype = tf.float32,
                                  )

validation_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2, dtype = tf.float32,)

train_batches = train_datagen.flow_from_directory(
    dataset_path,
    target_size = image_size,
    batch_size = batch_size,
    color_mode='rgb',
    class_mode = 'categorical',
    shuffle = True,
    seed = 123,
    subset = 'training', )

validation_batches = validation_datagen.flow_from_directory(
    dataset_path,
    target_size = image_size,
    batch_size = batch_size,
    color_mode='rgb',
    class_mode = 'categorical',
    shuffle = True,
    seed = 123,
    subset = 'validation', )

test_batches = validation_datagen.flow_from_directory(
    dataset_path,
    target_size = image_size,
    batch_size = batch_size,
    color_mode='rgb',
    class_mode = 'categorical',
    shuffle = True,
    seed = 123,
    subset = 'validation', )

from keras import layers
import tensorflow_hub as hub

url = 'https://tfhub.dev/google/aiy/vision/classifier/birds_V1/1'
base_model = hub.KerasLayer(url, input_shape=(224, 224, 3), trainable=False)

num_of_birds = 10

model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.Dense(num_of_birds, activation='softmax'),
])

model.summary()

相关截图说明

  • model.summary()截图:显示KerasLayer的参数计数为0,后续Dense层参数为9650
  • 训练测试结果截图:训练1轮后,训练精度约0.17,验证精度约0.1,损失值处于较高水平

问题分析与解决建议

1. KerasLayer参数显示为0的原因

你加载的aiy/vision/classifier/birds_V1/1是完整的分类模型(包含964类的输出层),而非仅做特征提取的模型骨架。设置trainable=False后,该层的所有参数被冻结,不会被计入模型的可训练参数列表,因此model.summary()中显示参数为0。而你添加的Dense(10)层参数为964*10+10=9650,与截图中的数值一致,这部分是正常的。

2. 精度不达标的核心原因

(1)数据集划分错误

test_batches和validation_batches使用了同一个validation_split子集,导致测试集与验证集完全重复,无法客观评估模型的泛化能力。

(2)模型结构适配错误

直接在完整分类模型的输出后添加新的Dense层不合理:原模型的输出是针对964类鸟类的softmax结果,并非通用特征向量,在此基础上做二次分类会丢失原模型的特征表达能力,导致模型学习效率极低。

(3)数据预处理与增强问题

  • 原AIY鸟类模型的输入要求可能并非rescale=1./255(部分模型要求输入为[0,255]的原始像素值,或归一化到[-1,1]),错误的预处理会破坏模型的特征提取逻辑。
  • zoom_range=(0.95,0.95)相当于没有缩放增强,无法起到正则化或扩充数据的作用。

(4)训练策略不当

  • trainable=False完全冻结了预训练模型,仅训练最后一层Dense,模型无法适配你的10类任务的特定特征。
  • 仅训练1轮不足以让模型收敛,而精度下降可能是由于模型结构不合理导致的快速过拟合或梯度消失。

3. 具体修改方案

(1)修正数据集划分

如果没有单独的测试集,可删除test_batches,直接使用validation_batches作为验证和测试的基准;若有单独测试集,需指定单独的路径加载:

# 示例:使用单独的测试集路径
test_datagen = ImageDataGenerator(rescale=1./255, dtype=tf.float32)
test_batches = test_datagen.flow_from_directory(
    '/content/gdrive/MyDrive/test_images/',
    target_size=image_size,
    batch_size=batch_size,
    class_mode='categorical'
)

(2)更换为特征提取版模型

使用TensorFlow Hub中该模型的特征提取版本,输出为通用特征向量,再连接分类层:

url = 'https://tfhub.dev/google/aiy/vision/classifier/birds_V1/feature_vector/1'
base_model = hub.KerasLayer(url, input_shape=(224, 224, 3), trainable=False)

model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.Dense(num_of_birds, activation='softmax'),
])

(3)调整数据预处理与增强

  • 查阅模型文档确认输入要求,若模型需要原始像素值,删除rescale=1./255;若需要归一化到[-1,1],改为rescale=1./127.5 - 1。
  • 优化数据增强参数,增加有效扩充:
train_datagen = ImageDataGenerator(
    rescale=1./255,  # 根据模型要求调整
    rotation_range=20,
    zoom_range=(0.8, 1.2),
    width_shift_range=0.15,
    height_shift_range=0.15,
    horizontal_flip=True,  # 增加水平翻转
    validation_split=0.2,
    dtype=tf.float32
)

(4)优化训练策略

  • 先冻结预训练模型,训练顶层Dense层3-5轮,再解冻部分层进行微调:
# 第一步:训练顶层
model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-3),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_batches, validation_data=validation_batches, epochs=5)

# 第二步:解冻部分层微调
base_model.trainable = True
# 仅解冻最后N层,避免破坏预训练特征
for layer in base_model.layers[:-5]:
    layer.trainable = False

model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-5),  # 用更小的学习率
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_batches, validation_data=validation_batches, epochs=20, initial_epoch=5)
  • 添加EarlyStopping防止过拟合:
from keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
model.fit(..., callbacks=[early_stop])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 05:05:31