我在CNN模型中有很高的准确度,但是在火车上的准确度较低

2024-04-20 12:43:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有CNN的图像分类模型。我用MobileNet做过迁移学习。在移动网络的末尾,我添加了4层来学习图像的权重(而不是更新MobileNet的权重)。移动网的重量不变。结果,我用这个模型得到了91%的准确率,并用相同的训练集(train\u generator)进行了评估。但我现在的准确率只有41%。为什么会出现不同的结果?我用过同样的训练设备。。。你和我有什么不同吗型号.fit\u发电机的准确性和发电机型号?或者数据有问题?请帮忙。。。如何提高准确性??下面是我的全部代码。你知道吗

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
base_model = MobileNet(weights='imagenet', include_top=False)

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024, activation='relu')(x)
x=Dense(1024, activation='relu')(x)
x=Dense(512, activation='relu')(x)
preds=Dense(7, activation='softmax')(x)

model=Model(inputs=base_model.input, outputs=preds)

for layer in model.layers[:-4]:
    layer.trainable=False
for layer in model.layers[-4:]:
    layer.trainable=True

train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator = train_datagen.flow_from_directory('/Users/LG/Desktop/finger',
                                                   target_size=(224, 224),
                                                   color_mode='rgb',
                                                   batch_size=32,
                                                   class_mode='categorical',
                                                   shuffle=True)

model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])

step_size_train=train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
                   steps_per_epoch=step_size_train,
                   epochs=10)

纪元1/10 17/17[===============================]-53s 3s/步-损耗:1.9354-acc:0.3026

纪元2/10 17/17[============================]-52s 3s/步-损耗:1.1933-acc:0.5276

纪元3/10 17/17[===============================]-52s 3s/步-损耗:0.8936-加速度:0.6787

纪元4/10 17/17[===============================]-54s 3s/步-损耗:0.6040-加速度:0.7843

纪元5/10 17/17[===============================]-53s 3s/步-损耗:0.5367-加速度:0.8080

纪元6/10 17/17[===============================]-55s 3s/步-损耗:0.2676-加速度:0.9099

纪元7/10 17/17[===============================]-52s 3s/步-损耗:0.4531-加速度:0.8387

纪元8/10 17/17[============================]-53s 3s/步-损耗:0.3580-acc:0.8747

纪元9/10 17/17[===============================]-55s 3s/步-损耗:0.1963-acc:0.9301

10/10纪元 17/17[===============================]-53s 3s/步-损耗:0.2237-加速度:0.9133

model.evaluate_generator(train_generator, steps=5)

[2.169835996627808,0.41875]


Tags: fromimportlayersizemodeltensorflowtraingenerator