import tensorflow as tffrom tensorflow.keras.applications.inception_v3 import InceptionV3train_data_dir=".../food_data/images"data=tf.keras.preprocessing.image.ImageDataGenerator( featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=45, width_shift_range=0.2, height_shift_range=0.2, brightness_range=None, shear_range=0.2, zoom_range=0.2, channel_shift_range=0.0, fill_mode="nearest", cval=0.0, horizontal_flip=True, vertical_flip=True, rescale=1./255,)datagen=data.flow_from_directory( train_data_dir, target_size=(360, 360), batch_size=10, class_mode='categorical')base_model = InceptionV3(weights='imagenet',input_shape=(360,360,3), include_top=False)for layer in base_model.layers: layer.trainable = Falsex = base_model.outputx = tf.keras.layers.GlobalAveragePooling2D()(x)x = tf.keras.layers.Dropout(0.3)(x)x = tf.keras.layers.Dense(1024, activation='relu')(x)x = tf.keras.layers.BatchNormalization()(x)x = tf.keras.layers.Dense(512, activation='relu')(x)x = tf.keras.layers.Dense(256, activation='relu')(x)predictions = tf.keras.layers.Dense(101, activation='softmax')(x)model = tf.keras.models.Model(inputs=base_model.input, outputs=predictions)adam=tf.keras.optimizers.Adam( learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, name="Adam",)model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])model.fit_generator(datagen,steps_per_epoch=100,epochs=50)model.save('trained_food_new.h5')推荐答案几乎没有什么可以提高分类准确性的:There are few that may improve the classification accuracy: 使用带有nosy_student权重的EfficientNet.需要训练的参数数量较少.由于它具有可伸缩的体系结构,因此具有更高的准确性.Use EfficientNet with noisy_student weights. There are less number of parameters to train. It gives better accuracy due to the scalable architecture it has.您可以使用测试时间增加.在测试数据生成器中,执行简单的水平翻转,垂直翻转(如果数据看起来逼真)和仿射变换.它将生成数据的多个视图,并帮助模型对更可能的类进行平均.You can use test time augmentation. In your test data generator, do a simple horizontal flip, vertical flip (if data looks realistic) and affine transformations. It will generate multiple views of the data and helps the model to average out more probable class.签出图像库(压纹,锐化,添加噪点等).另外,还提供了random_eraser,剪切和混合策略,这些方法已被证明是有用的.Checkout imgaug library (embossing, sharpening, noise addition, etc.). Plus, there are random_eraser, cut out and mix up strategies that have been proved to be useful.尝试平滑标签.它还可以帮助您的分类器为正确的分类提供更多的可能性.Try label smoothing. It can also help your classifier to give more probability to the correct class.尝试学习率预热.像这样的东西:Try learning rate warmup. Something like this:LR_START = 0.0001LR_MAX = 0.00005LR_MIN = 0.0001LR_RAMPUP_EPOCHS = 4LR_SUSTAIN_EPOCHS = 6LR_EXP_DECAY = .8def lrfn(epoch): if epoch < LR_RAMPUP_EPOCHS: lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS: lr = LR_MAX else: lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN return lr您还可以提取特征并应用整体特征分类(XGBoost,Adaboost,BaggingClassifier)或三元组损失. 这篇关于使用InceptionV3,VGG16进行多类分类,具有101个分类的准确性非常低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 23:05