Advertisement

毕设-基于人工智能的图像分类算法研究与实现

阅读量:

毕设分享 基于人工智能的图像分类算法研究与实现

文章目录

0 简介

🔥计算机视觉、图像处理、毕业辅导、作业帮助、代码获取,远程协助,代码定制,私聊会回复!
✍🏻作者简介:机器学习,深度学习,卷积神经网络处理,图像处理
🚀B站项目实战:https://space.bilibili.com/364224477
😄 如果文章对你有帮助的话, 欢迎评论 💬点赞👍🏻 收藏 📂加关注+
🤵‍♂代做需求:@个人主页
🧿 选题指导, 项目分享:见文末

1 常用的分类网络介绍

1.1 CNN

传统CNN包含卷积层、全连接层等组件,并采用softmax多类别分类器和多类交叉熵损失函数。如下图:

在CNN的训练过程总,由于每一层的参数都是不断更新的,会导致下一次输入分布发生变化,这样就需要在训练过程中花费时间去设计参数。在后续提出的BN算法中,由于每一层都做了归一化处理,使得每一层的分布相对稳定,而且实验证明该算法加速了模型的收敛过程,所以被广泛应用到较深的模型中。

1.2 VGG

VGG 模型是由牛津大学提出的(19层网络),该模型的特点是加宽加深了网络结构,核心是五组卷积操作,每两组之间做Max-Pooling空间降维。同一组内采用多次连续的3X3卷积,卷积核的数目由较浅组的64增多到最深组的512,同一组内的卷积核数目是一样的。卷积之后接两层全连接层,之后是分类层。该模型由于每组内卷积层的不同主要分为 11、13、16、19 这几种模型

增加网络深度和宽度,也就意味着巨量的参数,而巨量参数容易产生过拟合,也会大大增加计算量。

1.3 GoogleNet

GoogleNet模型由多组Inception模块组成,模型设计借鉴了NIN的一些思想.

NIN模型特点:

Inception 结构的主要思路是怎样用密集成分来近似最优的局部稀疏结构。

2 图像分类部分代码实现

2.1 环境依赖

复制代码
    python 3.7
    jupyter-notebook : 6.0.3
    cudatoolkit 10.0.130
    cudnn 7.6.5
    tensorflow-gpu 2.0.0
    scikit-learn 0.22.1
    numpy
    cv2
    matplotlib
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/eGmpi13gYUC4LaW7DSyhltOxXVEr.png)

2.2 需要导入的包

复制代码
      import os
      import cv2
      import numpy as np
      import pandas as pd
      import tensorflow as tf
      from tensorflow import keras
      from tensorflow.keras import layers,models
      from tensorflow.keras.models import Sequential
      from tensorflow.keras.optimizers import Adam
      from tensorflow.keras.callbacks import Callback
      from tensorflow.keras.utils import to_categorical
      from tensorflow.keras.applications import VGG19
      from tensorflow.keras.models import load_model
      import matplotlib.pyplot as plt
      from sklearn.preprocessing import label_binarize
      tf.compat.v1.disable_eager_execution()
      os.environ['CUDA_VISIBLE_DEVICES'] = '0' #使用GPU
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/tOpnEGqrFsPZIWy0JhdNLY9xvQlH.png)

2.3 参数设置(路径,图像尺寸,数据集分割比例)

复制代码
     preprocessedFolder = '.\ ClassificationData\ ' #预处理文件夹
     outModelFileName=".\ outModelFileName\ " 
     ImageWidth = 512
     ImageHeight = 320
     ImageNumChannels = 3
     TrainingPercent = 70  #训练集比例
     ValidationPercent = 15 #验证集比例

2.4 从preprocessedFolder读取图片并返回numpy格式(便于在神经网络中训练)

复制代码
    def read_dl_classifier_data_set(preprocessedFolder):
      num = 0  # 图片的总数量
      cnt_class = 0  #图片所属的类别
      label_list = []  # 存放每个图像的label,图像的类别
      img_list = []   #存放图片数据
      for directory in os.listdir(preprocessedFolder):
      tmp_dir = preprocessedFolder + directory
      cnt_class += 1
      for image in os.listdir(tmp_dir):
          num += 1
          tmp_img_filepath = tmp_dir + '\ ' + image
          im = cv2.imread(tmp_img_filepath)  # numpy.ndarray
          im = cv2.resize(im, (ImageWidth, ImageHeight))  # 重新设置图片的大小
          img_list.append(im)
          label_list.append(cnt_class)  # 在标签中添加类别
          print("Picture " + str(num) + "Load "+tmp_img_filepath+"successfully")
    print("共有" + str(num) + "张图片")
    print("all"+str(num)+"picturs belong to "+str(cnt_class)+"classes")
    return np.array(img_list),np.array(label_list)
    
    all_data,all_label=read_dl_classifier_data_set(preprocessedFolder)
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/gRWaSfqh1oNe37UZTmpXwFQBzAPI.png)

2.5 数据预处理

图像数据压缩, 标签数据进行独立热编码one-hot

复制代码
    def preprocess_dl_Image(all_data,all_label):
      all_data = all_data.astype("float32")/255  #把图像灰度值压缩到0--1.0便于神经网络训练
      all_label = to_categorical(all_label)  #对标签数据进行独立热编码
      return all_data,all_label
    
    all_data,all_label = preprocess_dl_Image(all_data,all_label) #处理后的数据

对数据及进行划分(训练集:验证集:测试集 = 0.7:0.15:0.15)

复制代码
    def split_dl_classifier_data_set(all_data,all_label,TrainingPercent,ValidationPercent):
      s = np.arange(all_data.shape[0])
      np.random.shuffle(s)  #随机打乱顺序
      all_data = all_data[s] #打乱后的图像数据
      all_label = all_label[s] #打乱后的标签数据
      all_len = all_data.shape[0]
      train_len = int(all_len*TrainingPercent/100)  #训练集长度
      valadation_len = int(all_len*ValidationPercent/100)#验证集长度
      temp_len=train_len+valadation_len
      train_data,train_label = all_data[0:train_len,:,:,:],all_label[0:train_len,:] #训练集
      valadation_data,valadation_label = all_data[train_len:temp_len, : , : , : ],all_label[train_len:temp_len, : ] #验证集
      test_data,test_label = all_data[temp_len:, : , : , : ],all_label[temp_len:, : ] #测试集
      return train_data,train_label,valadation_data,valadation_label,test_data,test_label
    
    train_data,train_label,valadation_data,valadation_label,test_data,test_label=split_dl_classifier_data_set(all_data,all_label,TrainingPercent,ValidationPercent)
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/iM4D8W6ZSLdzb2UeswVQRhfpBCnt.png)

2.6 训练分类模型

复制代码
    def train_classifier(train_data,train_label,valadation_data,valadation_label,lr=1e-4):
      conv_base = VGG19(weights='imagenet',
              include_top=False,
              input_shape=(ImageHeight, ImageWidth, 3) )  
      model = models.Sequential()
      model.add(conv_base)
      model.add(layers.Flatten())
      model.add(layers.Dense(30, activation='relu')) 
      model.add(layers.Dense(6, activation='softmax')) #Dense: 全连接层。activation: 激励函数,‘linear’一般用在回归任务的输出层,而‘softmax’一般用在分类任务的输出层
      conv_base.trainable=False
      model.compile(
      loss='categorical_crossentropy',#loss: 拟合损失方法,这里用到了多分类损失函数交叉熵  
      optimizer=Adam(lr=lr),#optimizer: 优化器,梯度下降的优化方法 #rmsprop
      metrics=['accuracy'])
      model.summary() #每个层中的输出形状和参数。
      early_stoping =tf.keras.callbacks.EarlyStopping(monitor="val_loss",min_delta=0,patience=5,verbose=0,baseline=None,restore_best_weights=True)
      history = model.fit(
      train_data, train_label,
      batch_size=16, #更新梯度的批数据的大小 iteration = epochs / batch_size,
      epochs=30,  # 迭代次数
      validation_data=(valadation_data, valadation_label),  # 验证集
      callbacks=[early_stoping])
      return model,history
    model,history = train_classifier(train_data,train_label,valadation_data,valadation_label,)
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/CvVo1wh2TWbm8DS0txABp3JgPuOj.png)

2.7 模型训练效果

复制代码
    def plot_history(history):
      history_df = pd.DataFrame(history.history)
      history_df[['loss', 'val_loss']].plot()
      plt.title('Train and valadation loss')
      history_df = pd.DataFrame(history.history)
      history_df[['accuracy', 'val_accuracy']].plot()
      plt.title('Train and valadation accuracy')
    
    plot_history(history)
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/bdjMqzYCw0B8QkieJZoVNlg7GpUs.png)

2.8 模型性能评估

复制代码
    from sklearn.metrics import classification_report
    from sklearn.metrics import confusion_matrix
    from sklearn.metrics import accuracy_score
    import seaborn as sns
    Y_pred_tta=model.predict_classes(test_data) #模型对测试集数据进行预测
    Y_test = [np.argmax(one_hot)for one_hot in test_label]# 由one-hot转换为普通np数组
    Y_pred_tta=model.predict_classes(test_data) #模型对测试集进行预测
    Y_test = [np.argmax(one_hot)for one_hot in test_label]# 由one-hot转换为普通np数组
    print('验证集分类报告:\n',classification_report(Y_test,Y_pred_tta))
    confusion_mc = confusion_matrix(Y_test,Y_pred_tta)#混淆矩阵
    df_cm = pd.DataFrame(confusion_mc)
    plt.figure(figsize = (10,7))
    sns.heatmap(df_cm, annot=True, cmap="BuPu",linewidths=1.0,fmt="d")
    plt.title('PipeLine accuracy:{0:.3f}'.format(accuracy_score(Y_test,Y_pred_tta)),fontsize=20)
    plt.ylabel('True label',fontsize=20)
    plt.xlabel('Predicted label',fontsize=20)
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/wdaDf1uUq5riEBmGSN8vC2toLTX3.png)
复制代码
    from sklearn.metrics import precision_recall_curve
    from sklearn.metrics import average_precision_score
    from sklearn.metrics import roc_curve
    from sklearn import metrics
    import matplotlib as mpl
    
    # 计算属于各个类别的概率,返回值的shape = [n_samples, n_classes]
    y_score = model.predict_proba(test_data)
    # 1、调用函数计算验证集的AUC 
    print ('调用函数auc:', metrics.roc_auc_score(test_label, y_score, average='micro'))
    # 2、手动计算验证集的AUC
    #首先将矩阵test_label和y_score展开,然后计算假正例率FPR和真正例率TPR
    fpr, tpr, thresholds = metrics.roc_curve(test_label.ravel(),y_score.ravel())
    auc = metrics.auc(fpr, tpr)
    print('手动计算auc:', auc)
    mpl.rcParams['font.sans-serif'] = u'SimHei'
    mpl.rcParams['axes.unicode_minus'] = False
    #FPR就是横坐标,TPR就是纵坐标
    plt.figure(figsize = (10,7))
    plt.plot(fpr, tpr, c = 'r', lw = 2, alpha = 0.7, label = u'AUC=%.3f' % auc)
    plt.plot((0, 1), (0, 1), c = '#808080', lw = 1, ls = '--', alpha = 0.7)
    plt.xlim((-0.01, 1.02))
    plt.ylim((-0.01, 1.02))
    plt.xticks(np.arange(0, 1.1, 0.1))
    plt.yticks(np.arange(0, 1.1, 0.1))
    plt.xlabel('False Positive Rate', fontsize=16)
    plt.ylabel('True Positive Rate', fontsize=16)
    plt.grid(b=True, ls=':')
    plt.legend(loc='lower right', fancybox=True, framealpha=0.8, fontsize=12)
    plt.title('37个验证集分类后的ROC和AUC', fontsize=18)
    plt.show()
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/iTZe8FV4z9Nb1L0HrynlDSRUsqEQ.png)

3 1000种图像分类

这是学长训练的能识别1000种类目标的图像分类模型,演示效果如下

4 最后

🔥计算机视觉、图像处理、毕业辅导、作业帮助、代码获取,远程协助,代码定制,私聊会回复!
✍🏻作者简介:机器学习,深度学习,卷积神经网络处理,图像处理
🚀B站项目实战:https://space.bilibili.com/364224477
😄 如果文章对你有帮助的话, 欢迎评论 💬点赞👍🏻 收藏 📂加关注+
🤵‍♂代做需求:@个人主页

全部评论 (0)

还没有任何评论哟~