【发布时间】:2020-09-30 02:50:18
【问题描述】:
我正在尝试建立一个包含 101 个类别的食品分类模型。数据集每个类别有 1000 张图像。我训练的模型的准确率低于 6%。我尝试使用 imagenet 权重实现 NASNet 和 VGG16,但准确度没有提高。我尝试过使用带或不带 amsgrad 的 Adam 优化器。我还尝试将学习率更改为 0.01 和 0.0001,但仍然保持在个位数。请建议将准确率提高到至少 60% 的方法。由于硬件限制(Macbook air 2017),我无法训练非常深的模型。
数据集:https://www.kaggle.com/kmader/food41
import tensorflow as tf
from tensorflow.keras.applications.inception_v3 import InceptionV3
train_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 = False
x = base_model.output
x = 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')
【问题讨论】:
-
如果可能,尝试增加批量大小。如您所见,当有 101 个类时,批量大小为 10 可能会出现问题。
-
我已经尝试将批量大小增加到 50。我应该将它增加到 100 以上吗?
-
每批 50 张图像至少应该让您的准确率达到 6%。您还应该查看您正在执行的增强,以确保图像不会因添加噪声而失真到模型无法训练的位置。
-
感谢您的回复。我会尽量减少噪音。将批次增加到 50 以上不会显着提高准确性。我的目标是达到 60% 左右。我认为要达到 60% 的准确度,需要更改架构或更改模型。该模型在二元分类和类别少于 10 的情况下表现得非常好。但是随着类别的增加,这会产生一个问题
标签: python tensorflow machine-learning keras deep-learning