使用CNN进行多类图像分类

我想对包含五个类别的图像进行分类。我想使用CNN。但是当我尝试使用几个模型时,训练准确率不会超过20%。请有人帮我解决这个问题。大多数模型会在3个周期内完成训练,当周期增加时,准确率没有提高。谁能建议我一个解决方案或模型,或者能指出可能的问题是什么?

以下是我使用的一个模型

#defining training and test setsx_train,x_val,y_train,y_val=train_test_split(x,y,test_size=0.2, random_state=42)print('Training data and target sizes: \n{}, {}'.format(x_train.shape,y_train.shape))print('Test data and target sizes: \n{}, {}'.format(x_val.shape,y_val.shape))

训练数据和目标大小:(2398, 224, 224, 3), (2398,)测试数据和目标大小:(600, 224, 224, 3), (600,)

img_rows, img_cols, img_channel = 224, 224, 3base_model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet',pooling='avg', input_shape=(img_rows, img_cols, img_channel))print(base_model.summary())#Adding custom Layersadd_model = Sequential()add_model.add(Dense(1024, activation='relu',input_shape=base_model.output_shape[1:]))add_model.add(Dropout(0.60))add_model.add(Dense(1, activation='sigmoid'))print(add_model.summary())# creating the final modelmodel = Model(inputs=base_model.input, outputs=add_model(base_model.output))# compile the modelopt = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)reduce_lr = ReduceLROnPlateau(monitor='val_acc',                              patience=5,                              verbose=1,                              factor=0.1,                              cooldown=10,                              min_lr=0.00001)   model.compile(              loss='categorical_crossentropy',               metrics=['acc'],              optimizer='adam'             )print(model.summary())n_fold = 5kf = model_selection.KFold(n_splits = n_fold, shuffle = True)eval_fun = metrics.roc_auc_score model.fit(x_train,y_train,epochs=50,batch_size=50,validation_data=(x_val,y_val))

回答:

可以吗,你能分享一下你拟合模型的代码部分吗?帖子中没有提供这些信息。

由于缺乏数据,输出无法重现,我建议你查看这个链接 https://www.kaggle.com/kenconstable/alzheimer-s-multi-class-classification

这个链接解释得很好,并且提供了基于迁移学习以及从头开始的多类分类的最佳实践。如果你觉得这个链接没有帮助,分享包括模型拟合(model.fit())代码的训练脚本会很有帮助。

好的,这就是问题所在,在你的代码中,你可能创建了一个基于Inception V3的基础模型,但是你并没有真正将这个基础模型添加到你的 add_model 变量中。

你的 add_model 变量本质上是一个密集网络,而不是CNN。另外,虽然这不是什么大事,你创建了自己的优化器 opt,但在 model.compile 中并没有使用它。

你能试试这个代码吗,然后告诉我它是否有效:

# function to build the modeldef build_transfer_model(conv_base,dropout,dense_node,learn_rate,metric):    """    Build and compile a transfer learning model    Input: a base model, dropout rate, the number of filters in the dense node,            the learning rate and performance metrics    Output: A compiled CNN model    """        # clear previous run    backend.clear_session()        # build the model    model = Sequential()    model.add(conv_base)    model.add(Dropout(dropout))    model.add(BatchNormalization())    model.add(Flatten())    model.add(Dense(dense_node,activation='relu'))    model.add(Dense(1,activation='sigmoid'))    # complile the model    model.compile(        optimizer = tensorflow.keras.optimizers.Adam(lr=learn_rate),        loss      = 'categorical_crossentropy',         metrics   = metric )        model.summary()    return modelimg_rows, img_cols, img_channel = 224, 224, 3base_model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet',pooling='avg', input_shape=(img_rows, img_cols, img_channel))model = build_transfer_model(conv_base=base_model,dropout=0.6,dense_node =1024,learn_rate=0.001,metric=['acc'])print(model.summary())model.fit(x_train,y_train,epochs=50,batch_size=50,validation_data=(x_val,y_val))

如果你注意这个函数,我们添加到 Sequential() 实例的第一件事是基础层(在你的案例中是InceptionV3)。但你直接添加了一个密集层。虽然它可能会从基础Inception V3的输出层获取权重,但它将是一个密集网络,而不是CNN。所以请检查一下这一点。

我可能更改了一些变量名,尽管我尽量不这样做。请根据你的需求更改 build_transfer_model 函数中层的顺序。

如果它不起作用,请告诉我。谢谢。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注