深度学习和目标检测系列教程 11-300:小麦数据集训练Faster-RCNN模型
生活随笔
收集整理的這篇文章主要介紹了
深度学习和目标检测系列教程 11-300:小麦数据集训练Faster-RCNN模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@Author:Runsen
上次訓練的Faster-RCNN的數據格式是xml和jpg圖片提供,在很多Object-Detection中,數據有的是csv格式,
- 數據集來源:https://www.kaggle.com/c/global-wheat-detection
width和heigth是圖片的長和寬,bbox是框的位置。
我們需要在小麥植物的室外圖像中檢測小麥頭,分類的類別只有一個。
我們來看一個牛逼人的做法:https://www.kaggle.com/pestipeti/pytorch-starter-fasterrcnn-train
這次使用torch訓練Faster-RCNN和之前的沒有什么不一樣。
import pandas as pd import numpy as np import cv2 import os import refrom PIL import Imageimport albumentations as A from albumentations.pytorch.transforms import ToTensorV2import torch import torchvisionfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor from torchvision.models.detection import FasterRCNN from torchvision.models.detection.rpn import AnchorGeneratorfrom torch.utils.data import DataLoader, Dataset from torch.utils.data.sampler import SequentialSamplerfrom matplotlib import pyplot as pltDIR_INPUT = '/kaggle/input/global-wheat-detection' DIR_TRAIN = f'{DIR_INPUT}/train' DIR_TEST = f'{DIR_INPUT}/test'train_df = pd.read_csv(f'{DIR_INPUT}/train.csv')train_df['x'] = -1 train_df['y'] = -1 train_df['w'] = -1 train_df['h'] = -1def expand_bbox(x):r = np.array(re.findall("([0-9]+[.]?[0-9]*)", x))if len(r) == 0:r = [-1, -1, -1, -1]return r # 讀取'x', 'y', 'w', 'h' train_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x))) train_df.drop(columns=['bbox'], inplace=True) train_df['x'] = train_df['x'].astype(np.float) train_df['y'] = train_df['y'].astype(np.float) train_df['w'] = train_df['w'].astype(np.float) train_df['h'] = train_df['h'].astype(np.float)image_ids = train_df['image_id'].unique() valid_ids = image_ids[-665:] train_ids = image_ids[:-665]# Albumentations def get_train_transform():return A.Compose([A.Flip(0.5),ToTensorV2(p=1.0)], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})def get_valid_transform():return A.Compose([ToTensorV2(p=1.0)], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})# load a model; pre-trained on COCO model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)num_classes = 2 # 1 class (wheat) + background# get number of input features for the classifier in_features = model.roi_heads.box_predictor.cls_score.in_features# replace the pre-trained head with a new one model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)不同的是定義了Averager類,這一個類來保存對應的loss。
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') images, targets, image_ids = next(iter(train_data_loader)) images = list(image.to(device) for image in images) targets = [{k: v.to(device) for k, v in t.items()} for t in targets] boxes = targets[2]['boxes'].cpu().numpy().astype(np.int32) sample = images[2].permute(1,2,0).cpu().numpy()fig, ax = plt.subplots(1, 1, figsize=(16, 8))for box in boxes:cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(220, 0, 0), 3)ax.set_axis_off() ax.imshow(sample) model.to(device) params = [p for p in model.parameters() if p.requires_grad] optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005) # lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1) lr_scheduler = Nonenum_epochs = 2loss_hist = Averager() itr = 1for epoch in range(num_epochs):loss_hist.reset()for images, targets, image_ids in train_data_loader:images = list(image.to(device) for image in images)targets = [{k: v.to(device) for k, v in t.items()} for t in targets]loss_dict = model(images, targets)losses = sum(loss for loss in loss_dict.values())loss_value = losses.item()loss_hist.send(loss_value)optimizer.zero_grad()losses.backward()optimizer.step()if itr % 50 == 0:print(f"Iteration #{itr} loss: {loss_value}")itr += 1# update the learning rateif lr_scheduler is not None:lr_scheduler.step()print(f"Epoch #{epoch} loss: {loss_hist.value}") images, targets, image_ids = next(iter(valid_data_loader)) images = list(img.to(device) for img in images) targets = [{k: v.to(device) for k, v in t.items()} for t in targets] boxes = targets[1]['boxes'].cpu().numpy().astype(np.int32) sample = images[1].permute(1,2,0).cpu().numpy() model.eval() cpu_device = torch.device("cpu")outputs = model(images) outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs] fig, ax = plt.subplots(1, 1, figsize=(16, 8))for box in boxes:cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(220, 0, 0), 3)ax.set_axis_off() ax.imshow(sample) torch.save(model.state_dict(), 'fasterrcnn_resnet50_fpn.pth')這個代碼真的值得學習和模仿:
https://www.kaggle.com/pestipeti/pytorch-starter-fasterrcnn-train
總結
以上是生活随笔為你收集整理的深度学习和目标检测系列教程 11-300:小麦数据集训练Faster-RCNN模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 斯柯达明锐方向机几个螺丝
- 下一篇: 深度学习和目标检测系列教程 12-300