当前位置:   article > 正文

基于卷积神经网络(CNN)模型的垃圾分类设计与实现_基于densenet密集网络的垃圾分类算法的设计与实现

基于densenet密集网络的垃圾分类算法的设计与实现

本篇博客主要内容如下:

目录

项目背景

数据集介绍

模型构建与训练

结果分析

结果对比分析

项目背景

如何通过垃圾分类管理,最大限度地实现垃圾资源利用,减少垃圾处置量,改善生存环境质量,是当前世界各国共同关注的迫切问题之一。根据国家制定的统一标准,现在生活垃圾被广泛分为四类,分别是可回收物、餐厨垃圾、有害垃圾和其他垃圾。可回收物表示适宜回收和资源利用的垃圾,主要包括废纸、塑料、玻璃、金属和布料五大类,用蓝色垃圾容器收集,通过综合处理回收利用。餐厨垃圾包括剩菜剩饭、骨头、菜根菜叶、果皮等食品类废物,用绿色垃圾容器收集等等。但是随着深度学习技术的发展,为了简单高效地对生活垃圾进行识别分类,本篇文章将实现一种基于卷积神经网络的垃圾分类识别方法。该方法只需要对图像进行简单的预处理,CNN模型便能够自动提取图像特征且池化过程能够减少参数数量,降低计算的复杂度,实验结果表明卷积神经网络,能克服传统图像分类算法的诸多缺点,当然更为复杂的模型等待大家去实验研究,讨论研究Q525894654。但是目前认为采用VGG或者global 池化方式可能效果更好一点。

数据集介绍

数据描述:

数据集一共包括四大类垃圾,分别为:其他垃圾,厨余垃圾、可回收垃圾及有害垃圾,并对其四大类进行了细致分类。具体描述如下:

  1. "0": "其他垃圾/一次性快餐盒",
  2. "1": "其他垃圾/污损塑料",
  3. "2": "其他垃圾/烟蒂",
  4. "3": "其他垃圾/牙签",
  5. "4": "其他垃圾/破碎花盆及碟碗",
  6. "5": "其他垃圾/竹筷",
  7. "6": "厨余垃圾/剩饭剩菜",
  8. "7": "厨余垃圾/大骨头",
  9. "8": "厨余垃圾/水果果皮",
  10. "9": "厨余垃圾/水果果肉",
  11. "10": "厨余垃圾/茶叶渣",
  12. "11": "厨余垃圾/菜叶菜根",
  13. "12": "厨余垃圾/蛋壳",
  14. "13": "厨余垃圾/鱼骨",
  15. "14": "可回收物/充电宝",
  16. "15": "可回收物/包",
  17. "16": "可回收物/化妆品瓶",
  18. "17": "可回收物/塑料玩具",
  19. "18": "可回收物/塑料碗盆",
  20. "19": "可回收物/塑料衣架",
  21. "20": "可回收物/快递纸袋",
  22. "21": "可回收物/插头电线",
  23. "22": "可回收物/旧衣服",
  24. "23": "可回收物/易拉罐",
  25. "24": "可回收物/枕头",
  26. "25": "可回收物/毛绒玩具",
  27. "26": "可回收物/洗发水瓶",
  28. "27": "可回收物/玻璃杯",
  29. "28": "可回收物/皮鞋",
  30. "29": "可回收物/砧板",
  31. "30": "可回收物/纸板箱",
  32. "31": "可回收物/调料瓶",
  33. "32": "可回收物/酒瓶",
  34. "33": "可回收物/金属食品罐",
  35. "34": "可回收物/锅",
  36. "35": "可回收物/食用油桶",
  37. "36": "可回收物/饮料瓶",
  38. "37": "有害垃圾/干电池",
  39. "38": "有害垃圾/软膏",
  40. "39": "有害垃圾/过期药物"

数据标签与统计结果:
类别:0    该类别总样本数:469    训练集样本数:375    验证集样本数:94

类别:1    该类别总样本数:471    训练集样本数:376    验证集样本数:95

类别:2    该类别总样本数:440    训练集样本数:352    验证集样本数:88

类别:3    该类别总样本数:150    训练集样本数:120    验证集样本数:30

类别:4    该类别总样本数:458    训练集样本数:366    验证集样本数:92

类别:5    该类别总样本数:413    训练集样本数:330    验证集样本数:83

类别:6    该类别总样本数:463    训练集样本数:370    验证集样本数:93

类别:7    该类别总样本数:422    训练集样本数:337    验证集样本数:85

类别:8    该类别总样本数:455    训练集样本数:364    验证集样本数:91

类别:9    该类别总样本数:482    训练集样本数:385    验证集样本数:97

类别:10    该类别总样本数:474    训练集样本数:379    验证集样本数:95

类别:11    该类别总样本数:806    训练集样本数:644    验证集样本数:162

类别:12    该类别总样本数:450    训练集样本数:360    验证集样本数:90

类别:13    该类别总样本数:466    训练集样本数:372    验证集样本数:94

类别:14    该类别总样本数:448    训练集样本数:358    验证集样本数:90

类别:15    该类别总样本数:514    训练集样本数:411    验证集样本数:103

类别:16    该类别总样本数:459    训练集样本数:367    验证集样本数:92

类别:17    该类别总样本数:740    训练集样本数:592    验证集样本数:148

类别:18    该类别总样本数:462    训练集样本数:369    验证集样本数:93

类别:19    该类别总样本数:491    训练集样本数:392    验证集样本数:99

类别:20    该类别总样本数:284    训练集样本数:227    验证集样本数:57

类别:21    该类别总样本数:825    训练集样本数:660    验证集样本数:165

类别:22    该类别总样本数:452    训练集样本数:361    验证集样本数:91

类别:23    该类别总样本数:415    训练集样本数:332    验证集样本数:83

类别:24    该类别总样本数:424    训练集样本数:339    验证集样本数:85

类别:25    该类别总样本数:781    训练集样本数:624    验证集样本数:157

类别:26    该类别总样本数:464    训练集样本数:371    验证集样本数:93

类别:27    该类别总样本数:623    训练集样本数:498    验证集样本数:125

类别:28    该类别总样本数:485    训练集样本数:388    验证集样本数:97

类别:29    该类别总样本数:479    训练集样本数:383    验证集样本数:96

类别:30    该类别总样本数:388    训练集样本数:310    验证集样本数:78

类别:31    该类别总样本数:496    训练集样本数:396    验证集样本数:100

类别:32    该类别总样本数:376    训练集样本数:300    验证集样本数:76

类别:33    该类别总样本数:373    训练集样本数:298    验证集样本数:75

类别:34    该类别总样本数:517    训练集样本数:413    验证集样本数:104

类别:35    该类别总样本数:443    训练集样本数:354    验证集样本数:89

类别:36    该类别总样本数:297    训练集样本数:237    验证集样本数:60

类别:37    该类别总样本数:380    训练集样本数:304    验证集样本数:76

类别:38    该类别总样本数:445    训练集样本数:356    验证集样本数:89

类别:39    该类别总样本数:487    训练集样本数:389    验证集样本数:98

总类别数:40    总样本数:18967    训练集总样本数:15159    验证集总样本数:3808

模型构建与训练

模型构建代码:model.py

  1. from keras.models import Sequential
  2. from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, Dropout, Activation, BatchNormalization
  3. from keras import backend as K
  4. from keras import optimizers, regularizers, Model
  5. from keras.applications import vgg19, densenet
  6. def generate_trashnet_model(input_shape, num_classes):
  7. # create model
  8. model = Sequential()
  9. # add model layers
  10. model.add(Conv2D(96, kernel_size=11, strides=4, activation='relu', input_shape=input_shape))
  11. model.add(MaxPooling2D(pool_size=3, strides=2))
  12. model.add(Conv2D(256, kernel_size=5, strides=1, activation='relu'))
  13. model.add(MaxPooling2D(pool_size=3, strides=2))
  14. model.add(Conv2D(384, kernel_size=3, strides=1, activation='relu'))
  15. model.add(Conv2D(384, kernel_size=3, strides=1, activation='relu'))
  16. model.add(Conv2D(256, kernel_size=3, strides=1, activation='relu'))
  17. model.add(MaxPooling2D(pool_size=3, strides=2))
  18. model.add(Flatten())
  19. model.add(Dropout(0.5))
  20. model.add(Dense(4096))
  21. model.add(Activation(lambda x: K.relu(x, alpha=1e-3)))
  22. model.add(Dropout(0.5))
  23. model.add(Dense(4096))
  24. model.add(Activation(lambda x: K.relu(x, alpha=1e-3)))
  25. model.add(Dense(num_classes, activation="softmax"))
  26. # compile model using accuracy to measure model performance
  27. model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  28. return model
  29. # Generate model using a pretrained architecture substituting the fully connected layer
  30. def generate_transfer_model(input_shape, num_classes):
  31. # imports the pretrained model and discards the fc layer
  32. base_model = densenet.DenseNet121(
  33. include_top=False,
  34. weights='imagenet',
  35. input_tensor=None,
  36. input_shape=input_shape,
  37. pooling='max') #using max global pooling, no flatten required
  38. x = base_model.output
  39. #x = Dense(256, activation="relu")(x)
  40. x = Dense(256, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x)
  41. x = Dropout(0.6)(x)
  42. x = BatchNormalization()(x)
  43. predictions = Dense(num_classes, activation="softmax")(x)
  44. # this is the model we will train
  45. model = Model(inputs=base_model.input, outputs=predictions)
  46. # compile model using accuracy to measure model performance and adam optimizer
  47. optimizer = optimizers.Adam(lr=0.001)
  48. #optimizer = optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
  49. model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
  50. return model

Total params: 7,302,470
Trainable params: 7,218,310
Non-trainable params: 84,160

模型训练代码:train_test.py

  1. from keras.preprocessing.image import ImageDataGenerator
  2. from keras import backend as K
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from sklearn.metrics import classification_report, confusion_matrix
  6. from model import *
  7. #from google.colab import drive
  8. import tensorflow as tf
  9. import seaborn as sn
  10. import pandas as pd
  11. # parameters
  12. img_width, img_height = 224, 224 # dimensions to which the images will be resized
  13. n_epochs = 10
  14. batch_size = 32
  15. num_classes = 40 # categories of trash
  16. #project_dir = '/cnn/data/'
  17. project_dir = ''
  18. trainset_dir = project_dir + 'dataset-splitted/training-set'
  19. testset_dir = project_dir + 'dataset-splitted/test-set'
  20. load_weights_file = project_dir + 'weights_save_densenet121_val_acc_86.0.h5'
  21. save_weights_file = project_dir + 'weights_save_4.h5'
  22. # this is the augmentation configuration we will use for training
  23. train_datagen = ImageDataGenerator(
  24. rescale=1. / 255,
  25. shear_range=0.2,
  26. zoom_range=0.2,
  27. horizontal_flip=True,
  28. rotation_range=40,
  29. width_shift_range=0.2,
  30. height_shift_range=0.2)
  31. test_datagen = ImageDataGenerator(rescale=1. / 255)
  32. train_generator = train_datagen.flow_from_directory(
  33. trainset_dir,
  34. target_size=(img_width, img_height),
  35. batch_size=batch_size)
  36. test_generator = test_datagen.flow_from_directory(
  37. testset_dir,
  38. target_size=(img_width, img_height),
  39. batch_size=batch_size,
  40. shuffle=False)
  41. if K.image_data_format() == 'channels_first':
  42. input_shape = (3, img_width, img_height)
  43. else:
  44. input_shape = (img_width, img_height, 3)
  45. model = generate_transfer_model(input_shape, num_classes)
  46. def load_weights():
  47. model.load_weights(load_weights_file)
  48. print("Weights loaded")
  49. def fit(n_epochs):
  50. history = model.fit_generator(
  51. train_generator,
  52. steps_per_epoch=len(train_generator),
  53. epochs=n_epochs,
  54. validation_data=test_generator,
  55. validation_steps=len(test_generator))
  56. # list all data in history
  57. print(history.history.keys())
  58. # summarize history for accuracy
  59. plt.plot(history.history['acc'])
  60. plt.plot(history.history['val_acc'])
  61. plt.title('model accuracy')
  62. plt.ylabel('accuracy')
  63. plt.xlabel('epoch')
  64. plt.legend(['train', 'test'], loc='upper left')
  65. plt.show()
  66. # summarize history for loss
  67. plt.plot(history.history['loss'])
  68. plt.plot(history.history['val_loss'])
  69. plt.title('model loss')
  70. plt.ylabel('loss')
  71. plt.xlabel('epoch')
  72. plt.legend(['train', 'test'], loc='upper left')
  73. plt.show()
  74. model.save_weights(save_weights_file)
  75. def print_layers():
  76. for layer in model.layers:
  77. print(layer.name)
  78. print("trainable: " + str(layer.trainable))
  79. print("input_shape: " + str(layer.input_shape))
  80. print("output_shape: " + str(layer.output_shape))
  81. print("_____________")
  82. def print_classification_report():
  83. # Confution Matrix and Classification Report
  84. Y_pred = model.predict_generator(test_generator, len(test_generator))
  85. y_pred = np.argmax(Y_pred, axis=1)
  86. print('Classification Report')
  87. target_names = list(test_generator.class_indices.keys())
  88. print(classification_report(test_generator.classes, y_pred, target_names=target_names))
  89. print('Confusion Matrix')
  90. conf_mat = confusion_matrix(test_generator.classes, y_pred)
  91. df_cm = pd.DataFrame(conf_mat, index=target_names, columns=target_names)
  92. plt.figure(figsize=(10, 7))
  93. sn.heatmap(df_cm, annot=True)
  94. #save keras model and convert it into tflite model
  95. def save_model():
  96. # Save tf.keras model in HDF5 format.
  97. keras_file = "keras_model.h5"
  98. model.save(keras_file)
  99. # Convert to TensorFlow Lite model.
  100. converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
  101. tflite_model = converter.convert()
  102. open("converted_model.tflite", "wb").write(tflite_model)
  103. print("saved")
  104. #print_layers()
  105. load_weights()
  106. #fit(n_epochs)
  107. print_classification_report()
  108. #save_model()

结果分析

首先构建或者下载好数据集,直接运行train_test.py即可,训练十次左右的准确率与损失函数图像如下:

最终训练次数达到60次左右趋于稳定,准确率可达75%左右。该模型可根据需求更改为四分类问题,只需要修改numclass参数即可。

结果对比分析

该方法与传统的机器学习方法SVM相比,训练较慢,但是准确率较高,该数据集上高于6%-9%;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/374085
推荐阅读
相关标签
  

闽ICP备14008679号