程式碼實踐——Faster R-CNN

專注的阿熊發表於2021-06-16

"""

Faster rcnn 實現目標檢測

"""

import os

import time

import torch

import torchvision.transforms as transforms

import torchvision

from PIL import Image

from matplotlib import pyplot as plt

# 獲取當前路徑

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# classes_coco 類別資訊

COCO_INSTANCE_CATEGORY_NAMES = [

     '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',

     'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',

     'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',

     'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',

     'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',

     'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',

     'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',

     'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',

     'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',

     'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',

     'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',

     'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'

]

if __name__ == "__main__":

     # 檢測圖片路徑

     path_img = os.path.join(BASE_DIR, "moto.jpg")

     # 預處理

     preprocess = transforms.Compose([

         transforms.ToTensor(),

     ])

     input_image = Image.open(path_img).convert("RGB")

     img_chw = preprocess(input_image)

     # 載入預訓練模型

     model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)

     model.eval()

     if torch.cuda.is_available():

         img_chw = img_chw.to('cuda')

         model.to('cuda')

     # 前向傳播

     input_list = [img_chw]

     with torch.no_grad():

         tic = time.time()

         print("input img tensor shape:{}".format(input_list[0].shape))

         output_list = 外匯跟單gendan5.commodel(input_list)

         output_dict = output_list[0]

         print("pass: {:.3f}s".format(time.time() - tic))

         # 列印輸出資訊

         for k, v in output_dict.items():

             print("key:{}, value:{}".format(k, v))

     # 取得相應結果

     out_boxes = output_dict["boxes"].cpu()

     out_scores = output_dict["scores"].cpu()

     out_labels = output_dict["labels"].cpu()

     # 視覺化

     fig, ax = plt.subplots(figsize=(12, 12))

     ax.imshow(input_image, aspect='equal')

     num_boxes = out_boxes.shape[0]

     max_vis = 400

     thres = 0.6

     # 迴圈描框

     for idx in range(0, min(num_boxes, max_vis)):

         score = out_scores[idx].numpy()

         bbox = out_boxes[idx].numpy()

         class_name = COCO_INSTANCE_CATEGORY_NAMES[out_labels[idx]]

         if score < thres:

             continue

         ax.add_patch(plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False,

                                    edgecolor='red', linewidth=3.5))

         ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(class_name, score), bbox=dict(facecolor='blue', alpha=0.5),

                 fontsize=14, color='white')

         ax.set_title("just a simple try about Faster Rcnn", fontsize=28, color='blue')

     plt.show()

     plt.close()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2776991/,如需轉載,請註明出處,否則將追究法律責任。

相關文章