手把手帶你實現基於深度學習的垃圾分類器

2023-08-09 00:42:12 字數 7910 閱讀 6023

隨著paddlepaddle2.0的更新,paddleclas影象分類套件也更新到了2.0-rc1版本。新版本的paddleclas套件已經預設使用動態圖來進行模型訓練。現在我們使用paddleclas套件從零開始實現乙個簡單的垃圾分類器。來體驗一下新版本的paddleclas的的方便快捷,即使初學者也能快速的訓練出高精度的模型。本篇文章分為上下兩部分,上部講解如何從零開始訓練,下部講解部分核心**以及深度學習訓練過程中使用到的技術。

資料集位址評論回覆獲取。

mkdir datasetcd datasetunzip garbage_classify.zip
資料集中共包含43個分類,例如:9代表"廚餘垃圾/水果果肉代表"可**物/舊衣服、39代表有害垃圾/過期藥物"。具體類別可以檢視garbage_classify中的garbage_classify_rule.json檔案。

有了資料集之後,需要對資料集進行劃分。在dataset目錄下建立process_dataset.py檔案,使用下列**將資料集劃分為訓練集、驗證集和測試集,劃分比例為8:1:1。

import os

import glob

import numpy as np

file_list = glob.glob('.garbage_classify/train_data/*.txt')

np.random.shuffle(file_list)

train_len = len(file_list) /10 * 8

val_len = len(file_list) /10

train_list =

for txt_file in file_list[:train_len]:

with open(txt_file, 'r') as f:

line = f.readlines()[0]

line = line.strip()

image_file,label = line.split(',image_file = image_file.strip()

label = label.strip()

image_path = os.path.join('.garbage_classify/train_data/',image_file)

train_list.append(image_path + label + n')

with open('train_list.txt', w') as f:

f.writelines(train_list)

val_list =

for txt_file in file_list[train_len:train_len + val_len]:

with open(txt_file, 'r') as f:

line = f.readlines()[0]

line = line.strip()

image_file,label = line.split(',image_file = image_file.strip()

label = label.strip()

image_path os.path.join('.garbage_classify/train_data/',image_file)

val_list.append(image_path + label + n')

with open('val_list.txt', w') as f:

f.writelines(val_list)

test_list =

for txt_file in file_list[train_len + val_len:]:

with open(txt_file, 'r') as f:

line = f.readlines()[0]

line = line.strip()

image_file,label = line.split(',image_file = image_file.strip()

label = label.strip()

image_path = os.path.join('.garbage_classify/train_data/',image_file)

test_list.append(image_path + label + n')

with open('test_list.txt', w') as f:

f.writelines(test_list)

以上**執行結束後,目錄結構如下:

├──garbage_classify├──process_dataset.py├──test_list.txt├──train_list.txt└──val_list.txt
**paddleclas源**,並切換到2.0-rc1版本。

git clone fetchgit branch release/2.0-rc1 origin/release/2.0-rc1
paddleclas套件中包含了多種神經網路模型,也包含了模型對應的訓練引數,配置引數儲存在configs路徑下。本次的垃圾分類器我選擇乙個工業界常用的resnet50網路作為分類器。首先通過拷貝的方式新建乙個垃圾分類器的配置檔案。

cd paddleclas/configs/resnet/cp resnet50_vd.yaml garbage_resnet50_vd.yaml
然後修改garbage_resnet50_vd.yaml內容如下:

mode: 'train'

architecture:

name: 'resnet50_vd'

pretrained_model: "

model_s**e_dir: "output/"

classes_num: 43

total_images: 1281167

s**e_interval: 1

validate: true

valid_interval: 1

epochs: 200

topk: 5

image_shape: [3, 224, 224]

use_mix: false

ls_epsilon: 0.1

learning_rate:

function: 'cosine'

params:

lr: 0.001

optimizer:

function: 'momentum'

params:

momentum: 0.9

regularizer:

function: 'l2'

factor: 0.000070

train:

batch_size: 256

num_workers: 0

#這裡改成dataset的真實路徑,推薦使用絕對路徑。

file_list: "dataset/train_list.txt"

data_dir: "dataset/"

shuffle_seed: 0

transforms:

decodeimage:

to_rgb: true

to_np: false

channel_first: false

randcropimage:

size: 224

randflipimage:

flip_code: 1

normalizeimage:

scale: 1./255.

mean: [0.485, 0.456, 0.406]

std: [0.229, 0.224, 0.225]

order: '

tochwimage:

mix: -mixupoperator:

alpha: 0.2

valid:

batch_size: 64

num_workers: 0

#這裡改成dataset的真實路徑,推薦使用絕對路徑。

file_list: "dataset/val_list.txt"

data_dir: "dataset/aistudio/"

shuffle_seed: 0

transforms:

decodeimage:

to_rgb: true

to_np: false

channel_first: false

resizeimage:

resize_short: 256

cropimage:

size: 224

normalizeimage:

scale: 1.0/255.0

mean: [0.485, 0.456, 0.406]

std: [0.229, 0.224, 0.225]

order: '

tochwimage:

為了加快模型的收斂,同時提公升模型的精度,這裡我選擇先載入預訓練模型,然後對模型進行微調。首先需要**預訓練權重。

wget
然後開始訓練模型:

python tools/train.py \

c configs/resnet/garbage_resnet50_vd.yaml \

o pretrained_model="resnet50_vd_pretrained" \

o use_gpu=true

訓練過程中輸入日誌如下:

w1214 20:29:28.872682 1473 device_context.cc:338] please note: device: 0, cuda capability: 70, driver api version: 10.1, runtime api version: 10.1w1214 20:29:28.877846 1473 device_context.cc:346] device: 0, cudnn version: 7.6./opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py:1175: userwarning: skip loading for out.weight. out.weight receives a shape [2048, 1000], but the expected shape is [2048, 43]. warnings.warn(("skip loading for {}format(key) +str(err)))opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py:1175: userwarning: skip loading for out.bias. out.bias receives a shape [1000], but the expected shape is [43]. warnings.warn(("skip loading for {}format(key) +str(err)))2020-12-14 20:29:33 info: finish initing model from resnet50_vd_pretrained2020-12-14 20:29:36 info: epoch:0 , train step:0 , loss: 3.78009, top1: 0.00781, top5: 0.11328, lr: 0.001000, batch_cost: 2.94361 s, reader_cost: 2.13878 s, ips: 86.96806 images/sec.2020-12-14 20:30:01 info: epoch:0 , train step:10 , loss: 3.70998, top1: 0.06641, top5: 0.26953, lr: 0.001000, batch_cost: 2.42268 s, reader_cost: 1.62624 s, ips: 105.66822 images/sec.2020-12-14 20:30:25 info: epoch:0 , train step:20 , loss: 3.62013, top1: 0.10938, top5: 0.35938, lr: 0.001000, batch_cost: 2.43433 s, reader_cost: 1.63609 s, ips: 105.16244 images/sec.2020-12-14 20:30:50 info: epoch:0 , train step:30 , loss: 3.53434, top1: 0.21484, top5: 0.41406, lr: 0.001000, batch_cost: 2.46094 s, reader_cost: 1.66256 s, ips: 104.02520 images/sec.
為了可以快速的看到效果,訓練100個epoch之後,可以先停止訓練。當前最優模型在驗證集上的精度為top1: 0.90589, top5: 0.98966。

然後我們在測試集上評估一下最優模型的精度。

將paddleclas/configs/resnet/garbage_resnet50_vd.yaml檔案中驗證集的路徑改為測試集。

valid:

batch_size: 64

num_workers: 0

file_list: "home/aistudio/test_list.txt"

data_dir: "home/aistudio/"

開始評估模型,
python tools/eval.py -c \.configs/resnet/garbage_resnet50_vd.yaml -o \pretrained_model=".output/resnet50_vd/best_model/ppcls"
執行結果如下:

2020-12-15 09:08:25 info: epoch:0 , valid step:0 , loss: 1.05716, top1: 0.89062, top5: 1.00000, lr: 0.000000, batch_cost: 0.75766 s, reader_cost: 0.68446 s, ips: 84.47009 images/sec.2020-12-15 09:08:31 info: epoch:0 , valid step:10 , loss: 0.89015, top1: 0.92188, top5: 1.00000, lr: 0.000000, batch_cost: 0.58153 s, reader_cost: 0.51459 s, ips: 110.05544 images/sec.2020-12-15 09:08:36 info: epoch:0 , valid step:20 , loss: 0.91526, top1: 0.90625, top5: 1.00000, lr: 0.000000, batch_cost: 0.58075 s, reader_cost: 0.51361 s, ips: 110.20320 images/sec.2020-12-15 09:08:42 info: epoch:0 , valid step:30 , loss: 0.83382, top1: 0.92857, top5: 1.00000, lr: 0.000000, batch_cost: 0.55392 s, reader_cost: 0.48895 s, ips: 25.27445 images/sec.2020-12-15 09:08:42 info: end epoch:0 valid loss: 0.96556, top1: 0.90331, top5: 0.99018, batch_cost: 0.55392 s, reader_cost: 0.48895 s, batch_cost_sum: 11.63230 s, ips: 25.27445 images/sec.
可以看出當前的最優模型在測試集上的精度為top1: 0.90331, top5: 0.99018。準確率可以達到90%,當然這個精度還是可以繼續提公升的。可以通過調參、更換模型和資料增強進一步提公升模型精度。

下一篇會解析一下paddleclas套件中的核心**,以及一些調優的策略。

遭遇“0x00000019”藍屏?手把手帶你一步步解決!

近來,許多使用者反映自己在使用電腦時出現了 0x00000019 藍屏錯誤。這種情況不僅中斷了使用者的工作或娛樂,還可能導致資料丟失。但別擔心,這篇文章將詳細為你解讀該錯誤,並提供逐步解決方法。0x00000019 藍屏錯誤,通常被稱為badpoolheader,是windows作業系統中的一種常見...

《我的錯誤》在哪裡看?手把手教你

對於想要 我的錯誤 的讀者來說,這篇文章將提供一些有用的資訊。首先,我們需要明確一點,我的錯誤 可能是一部電影 一部電視劇集 一本書,甚至是一首歌曲。不論它是哪乙個,你通常可以在各大 平台找到。例如,如果它是一部電影或電視劇集,你可能會在netflix amazon prime video hulu...

手把手教你註冊谷歌郵箱 Gmail 賬號的方法

一 註冊前需要準備的資料 1.加速器 國內需要使用加速器才能開啟gmail 2.郵箱位址 乙個常用的郵箱位址,用來接收谷歌郵箱的確認郵件。3.手機號 用於接收谷歌郵箱賬號註冊時的簡訊驗證,且之前沒有註冊過gmail。二 正式註冊 1 開啟加速器,在瀏覽器輸入谷歌郵箱 位址 mail google c...