当前位置:   article > 正文

Fruits 360 基于CNN实现果蔬识别系统_fruits-360是用什么模型训练的

fruits-360是用什么模型训练的

先看最终效果:

项目主要分为三部分:

  1. 1、数据集解析处理
  2. 2、模型训练
  3. 3、推理计算与GUI可视化

本文使用的数据集是开源的果蔬数据集Fruits 360,下载后截图如下所示:

其中:

Test 表示测试数据集目录

Training 表示训练数据集目录

接下来各自看下对应的数据集情况。

Training 目录截图如下所示:

Test目录截图如下所示:

       可以看到:训练集和测试集都是提前划分好的,每个目录下面都有131个子目录,也就是该数据集中一共是131个类别,接下来我们详细统计下数据详情,代码如下:

  1. def lookDataset():
  2. '''
  3. 查看数据集情况
  4. '''
  5. dataDir = "data/train/"
  6. train_dict = {}
  7. train_num = 0
  8. for one_label in os.listdir(dataDir):
  9. oneDir = dataDir + one_label + "/"
  10. one_num = len(os.listdir(oneDir))
  11. train_dict[one_label] = one_num
  12. train_num += one_num
  13. print("Total Train Number: ", train_num)
  14. with open("train_num.json", "w") as f:
  15. f.write(json.dumps(train_dict))
  16. dataDir = "data/test/"
  17. test_dict = {}
  18. test_num = 0
  19. for one_label in os.listdir(dataDir):
  20. oneDir = dataDir + one_label + "/"
  21. one_num = len(os.listdir(oneDir))
  22. test_dict[one_label] = one_num
  23. test_num += one_num
  24. print("Total Test Number: ", test_num)
  25. with open("test_num.json", "w") as f:
  26. f.write(json.dumps(test_dict))

        结果输出如下:

        训练集-测试集数据量如下:

  1. Total Train Number: 67692
  2. Total Test Number: 22688

      其中,我对其各个类别数据量也进行了统计,测试集详情如下所示:

  1. {
  2. "Apple Braeburn": 164,
  3. "Apple Crimson Snow": 148,
  4. "Apple Golden 1": 160,
  5. "Apple Golden 2": 164,
  6. "Apple Golden 3": 161,
  7. "Apple Granny Smith": 164,
  8. "Apple Pink Lady": 152,
  9. "Apple Red 1": 164,
  10. "Apple Red 2": 164,
  11. "Apple Red 3": 144,
  12. "Apple Red Delicious": 166,
  13. "Apple Red Yellow 1": 164,
  14. "Apple Red Yellow 2": 219,
  15. "Apricot": 164,
  16. "Avocado": 143,
  17. "Avocado ripe": 166,
  18. "Banana": 166,
  19. "Banana Lady Finger": 152,
  20. "Banana Red": 166,
  21. "Beetroot": 150,
  22. "Blueberry": 154,
  23. "Cactus fruit": 166,
  24. "Cantaloupe 1": 164,
  25. "Cantaloupe 2": 164,
  26. "Carambula": 166,
  27. "Cauliflower": 234,
  28. "Cherry 1": 164,
  29. "Cherry 2": 246,
  30. "Cherry Rainier": 246,
  31. "Cherry Wax Black": 164,
  32. "Cherry Wax Red": 164,
  33. "Cherry Wax Yellow": 164,
  34. "Chestnut": 153,
  35. "Clementine": 166,
  36. "Cocos": 166,
  37. "Corn": 150,
  38. "Corn Husk": 154,
  39. "Cucumber Ripe": 130,
  40. "Cucumber Ripe 2": 156,
  41. "Dates": 166,
  42. "Eggplant": 156,
  43. "Fig": 234,
  44. "Ginger Root": 99,
  45. "Granadilla": 166,
  46. "Grape Blue": 328,
  47. "Grape Pink": 164,
  48. "Grape White": 166,
  49. "Grape White 2": 166,
  50. "Grape White 3": 164,
  51. "Grape White 4": 158,
  52. "Grapefruit Pink": 166,
  53. "Grapefruit White": 164,
  54. "Guava": 166,
  55. "Hazelnut": 157,
  56. "Huckleberry": 166,
  57. "Kaki": 166,
  58. "Kiwi": 156,
  59. "Kohlrabi": 157,
  60. "Kumquats": 166,
  61. "Lemon": 164,
  62. "Lemon Meyer": 166,
  63. "Limes": 166,
  64. "Lychee": 166,
  65. "Mandarine": 166,
  66. "Mango": 166,
  67. "Mango Red": 142,
  68. "Mangostan": 102,
  69. "Maracuja": 166,
  70. "Melon Piel de Sapo": 246,
  71. "Mulberry": 164,
  72. "Nectarine": 164,
  73. "Nectarine Flat": 160,
  74. "Nut Forest": 218,
  75. "Nut Pecan": 178,
  76. "Onion Red": 150,
  77. "Onion Red Peeled": 155,
  78. "Onion White": 146,
  79. "Orange": 160,
  80. "Papaya": 164,
  81. "Passion Fruit": 166,
  82. "Peach": 164,
  83. "Peach 2": 246,
  84. "Peach Flat": 164,
  85. "Pear": 164,
  86. "Pear 2": 232,
  87. "Pear Abate": 166,
  88. "Pear Forelle": 234,
  89. "Pear Kaiser": 102,
  90. "Pear Monster": 166,
  91. "Pear Red": 222,
  92. "Pear Stone": 237,
  93. "Pear Williams": 166,
  94. "Pepino": 166,
  95. "Pepper Green": 148,
  96. "Pepper Orange": 234,
  97. "Pepper Red": 222,
  98. "Pepper Yellow": 222,
  99. "Physalis": 164,
  100. "Physalis with Husk": 164,
  101. "Pineapple": 166,
  102. "Pineapple Mini": 163,
  103. "Pitahaya Red": 166,
  104. "Plum": 151,
  105. "Plum 2": 142,
  106. "Plum 3": 304,
  107. "Pomegranate": 164,
  108. "Pomelo Sweetie": 153,
  109. "Potato Red": 150,
  110. "Potato Red Washed": 151,
  111. "Potato Sweet": 150,
  112. "Potato White": 150,
  113. "Quince": 166,
  114. "Rambutan": 164,
  115. "Raspberry": 166,
  116. "Redcurrant": 164,
  117. "Salak": 162,
  118. "Strawberry": 164,
  119. "Strawberry Wedge": 246,
  120. "Tamarillo": 166,
  121. "Tangelo": 166,
  122. "Tomato 1": 246,
  123. "Tomato 2": 225,
  124. "Tomato 3": 246,
  125. "Tomato 4": 160,
  126. "Tomato Cherry Red": 164,
  127. "Tomato Heart": 228,
  128. "Tomato Maroon": 127,
  129. "Tomato not Ripened": 158,
  130. "Tomato Yellow": 153,
  131. "Walnut": 249,
  132. "Watermelon": 157
  133. }

        训练集详情如下所示:

  1. {
  2. "Apple Braeburn": 492,
  3. "Apple Crimson Snow": 444,
  4. "Apple Golden 1": 480,
  5. "Apple Golden 2": 492,
  6. "Apple Golden 3": 481,
  7. "Apple Granny Smith": 492,
  8. "Apple Pink Lady": 456,
  9. "Apple Red 1": 492,
  10. "Apple Red 2": 492,
  11. "Apple Red 3": 429,
  12. "Apple Red Delicious": 490,
  13. "Apple Red Yellow 1": 492,
  14. "Apple Red Yellow 2": 672,
  15. "Apricot": 492,
  16. "Avocado": 427,
  17. "Avocado ripe": 491,
  18. "Banana": 490,
  19. "Banana Lady Finger": 450,
  20. "Banana Red": 490,
  21. "Beetroot": 450,
  22. "Blueberry": 462,
  23. "Cactus fruit": 490,
  24. "Cantaloupe 1": 492,
  25. "Cantaloupe 2": 492,
  26. "Carambula": 490,
  27. "Cauliflower": 702,
  28. "Cherry 1": 492,
  29. "Cherry 2": 738,
  30. "Cherry Rainier": 738,
  31. "Cherry Wax Black": 492,
  32. "Cherry Wax Red": 492,
  33. "Cherry Wax Yellow": 492,
  34. "Chestnut": 450,
  35. "Clementine": 490,
  36. "Cocos": 490,
  37. "Corn": 450,
  38. "Corn Husk": 462,
  39. "Cucumber Ripe": 392,
  40. "Cucumber Ripe 2": 468,
  41. "Dates": 490,
  42. "Eggplant": 468,
  43. "Fig": 702,
  44. "Ginger Root": 297,
  45. "Granadilla": 490,
  46. "Grape Blue": 984,
  47. "Grape Pink": 492,
  48. "Grape White": 490,
  49. "Grape White 2": 490,
  50. "Grape White 3": 492,
  51. "Grape White 4": 471,
  52. "Grapefruit Pink": 490,
  53. "Grapefruit White": 492,
  54. "Guava": 490,
  55. "Hazelnut": 464,
  56. "Huckleberry": 490,
  57. "Kaki": 490,
  58. "Kiwi": 466,
  59. "Kohlrabi": 471,
  60. "Kumquats": 490,
  61. "Lemon": 492,
  62. "Lemon Meyer": 490,
  63. "Limes": 490,
  64. "Lychee": 490,
  65. "Mandarine": 490,
  66. "Mango": 490,
  67. "Mango Red": 426,
  68. "Mangostan": 300,
  69. "Maracuja": 490,
  70. "Melon Piel de Sapo": 738,
  71. "Mulberry": 492,
  72. "Nectarine": 492,
  73. "Nectarine Flat": 480,
  74. "Nut Forest": 654,
  75. "Nut Pecan": 534,
  76. "Onion Red": 450,
  77. "Onion Red Peeled": 445,
  78. "Onion White": 438,
  79. "Orange": 479,
  80. "Papaya": 492,
  81. "Passion Fruit": 490,
  82. "Peach": 492,
  83. "Peach 2": 738,
  84. "Peach Flat": 492,
  85. "Pear": 492,
  86. "Pear 2": 696,
  87. "Pear Abate": 490,
  88. "Pear Forelle": 702,
  89. "Pear Kaiser": 300,
  90. "Pear Monster": 490,
  91. "Pear Red": 666,
  92. "Pear Stone": 711,
  93. "Pear Williams": 490,
  94. "Pepino": 490,
  95. "Pepper Green": 444,
  96. "Pepper Orange": 702,
  97. "Pepper Red": 666,
  98. "Pepper Yellow": 666,
  99. "Physalis": 492,
  100. "Physalis with Husk": 492,
  101. "Pineapple": 490,
  102. "Pineapple Mini": 493,
  103. "Pitahaya Red": 490,
  104. "Plum": 447,
  105. "Plum 2": 420,
  106. "Plum 3": 900,
  107. "Pomegranate": 492,
  108. "Pomelo Sweetie": 450,
  109. "Potato Red": 450,
  110. "Potato Red Washed": 453,
  111. "Potato Sweet": 450,
  112. "Potato White": 450,
  113. "Quince": 490,
  114. "Rambutan": 492,
  115. "Raspberry": 490,
  116. "Redcurrant": 492,
  117. "Salak": 490,
  118. "Strawberry": 492,
  119. "Strawberry Wedge": 738,
  120. "Tamarillo": 490,
  121. "Tangelo": 490,
  122. "Tomato 1": 738,
  123. "Tomato 2": 672,
  124. "Tomato 3": 738,
  125. "Tomato 4": 479,
  126. "Tomato Cherry Red": 492,
  127. "Tomato Heart": 684,
  128. "Tomato Maroon": 367,
  129. "Tomato not Ripened": 474,
  130. "Tomato Yellow": 459,
  131. "Walnut": 735,
  132. "Watermelon": 475
  133. }

       整体来看,划分得还是比较均衡的,基本维持在3:1的状况。

      当然了,如果想要自己对数据集划分,也是可以的,这里我也同样实现了数据集随机比例划分功能,如下所示:

  1. def random2Dataset(dataDir='data/original/',ratio=0.3):
  2. '''
  3. 对原始数据集进行划分,得到:训练集和测试集
  4. '''
  5. label_list=os.listdir(dataDir)
  6. for one_label in label_list:
  7. oneDir=dataDir+one_label+'/'
  8. pic_list=os.listdir(oneDir)
  9. testNum=int(len(pic_list)*ratio)
  10. oneTrainDir='data/train/'+one_label+'/'
  11. oneTestDir='data/test/'+one_label+'/'
  12. if not os.path.exists(oneTrainDir):
  13. os.makedirs(oneTrainDir)
  14. if not os.path.exists(oneTestDir):
  15. os.makedirs(oneTestDir)
  16. #创建测试集
  17. for i in range(testNum):
  18. one_path=oneDir+random.choice(os.listdir(oneDir))
  19. name=str(len(os.listdir(oneTestDir))+1)
  20. new_path=oneTestDir+one_label+'_'+name+'.jpg'
  21. shutil.move(one_path,new_path)
  22. #创建训练集
  23. for one_pic in os.listdir(oneDir):
  24. one_path=oneDir+one_pic
  25. name=str(len(os.listdir(oneTrainDir))+1)
  26. new_path=oneTrainDir+one_label+'_'+name+'.jpg'
  27. shutil.move(one_path,new_path)

       可以根据自己的实际情况进行选择。

       模型层面我基于VGG主干网络进行改造,设计新的网络模型,如下所示:

        可以看到:参数量缩减了很多。

       我默认设置了1000次的迭代,实际观察发现:不到10次就足够了,我们可以看下训练可视化的曲线:

      准确度曲线:

       损失曲线:

      我在模型训练结束的时候在测试集总数据集上面进行了测试,准确率达到了96.55%,如下所示:

 

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

闽ICP备14008679号