您不必再次训练模型。只需加载它,然后使用它来进行预测。问题是模型是在 imagenet 数据集上训练的。这个数据集有 1000 个类。您的图像类型可能包含在 1000 个类别中,也可能不包含。要为您的应用程序定制模型,您应该使用您的自定义数据集对其进行训练。例如,如果您有一个包含 250 类鸟类的训练集,每个类都有一个单独的目录,并且您想要对鸟类进行分类,那么您应该使用 Inception 模型,如下所示。我
img_size= (224,224) # set this to the size of images you want to use
img_shape=(224,224,3) # set this to the input image shape
no_of_classes =250 # set this to an integer value for the number of classes
train_dir=r'c:\birds\train' #point this to your training directory'
t_gen=ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input, validation_split=0.3)
train_gen=t_gen.flow_from_directory( directory= train_dir, target_size=img_size,
class_mode="categorical", batch_size=32, shuffle=True, seed=123,
subset='training)
valid_gen=t_gen.flow_from_directory(directory= train_dir, target_size=img_size,
class_mode="categorical", batch_size=32, shuffle=False, seed=123,
subset='validation')
base_model=tf.keras.applications.InceptionV3( include_top=False, weights="imagenet",
input_shape=img_shape, pooling='max')
x=basemodel.output
x= Dense(512, activation-'relu')(x)
x= Dropout(.3)(x)
output=Dense(no_of_classes, activation='softmax')(x)
model=Model(inputs=base_model.input, outputs=output)
model.compile(Adam(lr=.001), loss='categorical_crossentropy', metrics=['accuracy'])
history==model.fit(x=train_gen, epochs=20, verbose=2,
validation_data=valid_gen, shuffle=False, initial_epoch=0)