制作類似pascal voc格式的目標檢測數據集:https://www.cnblogs.com/xiximayou/p/12546061.html
訓練自己創建的數據集:https://www.cnblogs.com/xiximayou/p/12546556.html
驗證自己創建的數據集:https://www.cnblogs.com/xiximayou/p/12550471.html
測試自己創建的數據集:https://www.cnblogs.com/xiximayou/p/12550566.html
還是以在谷歌colab上為例:
cd /content/drive/My Drive/pytorch_ssd
導入相應的包:
import os import sys module_path = os.path.abspath(os.path.join('..')) if module_path not in sys.path: sys.path.append(module_path) import torch import torch.nn as nn import torch.backends.cudnn as cudnn from torch.autograd import Variable import numpy as np import cv2 if torch.cuda.is_available(): torch.set_default_tensor_type('torch.cuda.FloatTensor') from ssd import build_ssd
加載谷歌網盤:
from google.colab import drive drive.mount('/content/drive')
加載模型:
net = build_ssd('test', 300, 3) # initialize SSD net.load_weights('weights/ssd300_MASK_5000.pth')
可視化要檢測的圖像:
# image = cv2.imread('./data/example.jpg', cv2.IMREAD_COLOR) # uncomment if dataset not downloaded %matplotlib inline from matplotlib import pyplot as plt from data import MASKDetection, MASK_ROOT, MASKAnnotationTransform # here we specify year (07 or 12) and dataset ('test', 'val', 'train') mask_root="/content/drive/My Drive/pytorch_ssd" testset = MASKDetection(mask_root, "val", None, MASKAnnotationTransform()) img_id = 2 image = testset.pull_image(img_id) rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # View the sampled input image before transform plt.figure(figsize=(10,10)) plt.imshow(rgb_image) plt.show()

調整圖片的格式:
x = cv2.resize(image, (300, 300)).astype(np.float32) x -= (104.0, 117.0, 123.0) x = x.astype(np.float32) x = x[:, :, ::-1].copy() plt.imshow(x) x = torch.from_numpy(x).permute(2, 0, 1)

使用模型進行預測:
xx = Variable(x.unsqueeze(0)) # wrap tensor in Variable if torch.cuda.is_available(): xx = xx.cuda() y = net(xx)
輸出結果:
from data import MASK_CLASSES as labels top_k=3 plt.figure(figsize=(10,10)) colors = plt.cm.hsv(np.linspace(0, 1, 3)).tolist() plt.imshow(rgb_image) # plot the image for matplotlib currentAxis = plt.gca() detections = y.data # scale each detection back up to the image scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2) for i in range(detections.size(1)): j = 0 while detections[0,i,j,0] >= 0.6: score = detections[0,i,j,0] label_name = labels[i-1] display_txt = '%s: %.2f'%(label_name, score) pt = (detections[0,i,j,1:]*scale).cpu().numpy() coords = (pt[0], pt[1]), pt[2]-pt[0]+1, pt[3]-pt[1]+1 color = colors[i] currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2)) currentAxis.text(pt[0], pt[1], display_txt, bbox={'facecolor':color, 'alpha':0.5}) j+=1

由於我的數據集中很少沒有戴口罩的樣本,因此沒有戴口罩的AP較低。
至此,使用pytorch-ssd訓練測試自己數據集就全部完成啦。
