# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="iJ7hbp9w32Uy" outputId="69e84138-f086-402d-bf58-1d2c1962d9bd"
# !nvidia-smi

# + colab={"base_uri": "https://localhost:8080/"} id="AHctWu2vPf0q" outputId="ce2dd527-42cc-451a-e707-538e84ee3794"
# 確認NVIDIA是否安裝及驅動版本
# !/usr/local/cuda/bin/nvcc --version

# + colab={"base_uri": "https://localhost:8080/"} id="bUx8RLwuDRxD" outputId="2aa8f152-2842-402e-9f39-5ef08dfa372a"
# !git clone https://github.com/alicebook12220/YOLOX.git

# + id="xfwA7nTuh_xB"
# #!pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

# + colab={"base_uri": "https://localhost:8080/"} id="JHmzlOov4Hnm" outputId="11b98ae7-7f0a-4b71-e5fe-71bab0b4577e"
#建置YOLOX環境
# %cd YOLOX
# !pip install -U pip && pip install -r requirements.txt
# !python setup.py develop

# + colab={"base_uri": "https://localhost:8080/"} id="BM_6naDn9kxC" outputId="f4768d81-0cda-487e-bf3a-f4bc1c471518"
#檢查是否有使用GPU，0是沒有，1是有
import torch
print(torch.cuda.device_count())

# + colab={"base_uri": "https://localhost:8080/"} id="TLRrWjVe4RBE" outputId="99141399-f9f2-4598-e50c-736c086b31b3"
#安裝pycocotools，讀取COCO格式資料用
# !pip install cython
# !pip install pycocotools

# + id="9pUrUO-D5loW" colab={"base_uri": "https://localhost:8080/"} outputId="ae8e72dd-513a-4cdb-f3ce-286df1bf5377"
#下載yolox_nano COCO權重
# %cd /content/YOLOX/
# !wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.pth

# + colab={"base_uri": "https://localhost:8080/"} id="vs7NaAI15lq4" outputId="c375affc-063a-4242-99ad-45488cfb0093"
# %cd /content/YOLOX/
#開始訓練
#-d為GPU數量 -b為batch_size --fp16為混合精度訓練 -o為先使用GPU記憶體 -c為訓練權重路徑
# !python tools/train.py -f yolox_voc_nano.py -d 1 -b 8 --fp16 -o -c yolox_nano.pth

# + colab={"base_uri": "https://localhost:8080/"} id="I-A0Pa62nql8" outputId="1de412a0-45b9-4181-c44e-bc94ee47512e"
#demo預測影像
TEST_IMAGE_PATH = "datasets/VOCdevkit/VOC2007/JPEGImages/002387.jpg"
# !python tools/demo.py image -f yolox_voc_nano.py -c YOLOX_outputs/yolox_voc_nano/best_ckpt.pth --path {TEST_IMAGE_PATH} --conf 0.25 --nms 0.45 --tsize 416 --save_result --device gpu

# + [markdown] id="p5lyDtOMw4YS"
# # 拆解demo.py

# + id="Mmvze6aqr6ru"
import argparse
import os
import time
from loguru import logger

import cv2
import torch
import torchvision

from yolox.data.data_augment import ValTransform
from yolox.exp import get_exp
from yolox.utils import fuse_model, get_model_info, postprocess, vis

# + colab={"base_uri": "https://localhost:8080/"} id="JP6bb4Lir8jW" outputId="23ca2e58-d8ac-4329-e86d-f9e792a278bb"
#讀yolox-nano模型
exp = get_exp("yolox_voc_nano.py", "yolox-nano")
model = exp.get_model()
logger.info("Model Summary: {}".format(get_model_info(model, exp.test_size)))
model.cuda()
model.eval()

ckpt = torch.load("YOLOX_outputs/yolox_voc_nano/best_ckpt.pth", map_location=lambda storage, loc: storage.cuda(0))
model.load_state_dict(ckpt["model"])
logger.info("loaded checkpoint done.")

# + colab={"base_uri": "https://localhost:8080/"} id="-7ELfOnxw0FO" outputId="15979564-129a-411a-ad6f-286ef5936ebc"
#預測影像
t0 = time.time()
img = cv2.imread("datasets/VOCdevkit/VOC2007/JPEGImages/002387.jpg")
preproc = ValTransform()
test_size = exp.test_size #(416, 416)
img, _ = preproc(img, None, test_size)
img = torch.from_numpy(img).unsqueeze(0)
img = img.cuda()

prediction = model(img)
print("Inference time:",time.time() - t0)
print(prediction)

# + colab={"base_uri": "https://localhost:8080/"} id="L86d32OTyO6w" outputId="8a2e1cbe-b1ba-43bb-9a1b-80b3f04e851f"
#prediction解碼
conf_thre = 0.5 #信心度閥值
nms_thre = 0.4 #重疊率閥值
num_classes = 1 #類別數量

box_corner = prediction.new(prediction.shape)
box_corner[:, :, 0] = prediction[:, :, 0] - prediction[:, :, 2] / 2
box_corner[:, :, 1] = prediction[:, :, 1] - prediction[:, :, 3] / 2
box_corner[:, :, 2] = prediction[:, :, 0] + prediction[:, :, 2] / 2
box_corner[:, :, 3] = prediction[:, :, 1] + prediction[:, :, 3] / 2
prediction[:, :, :4] = box_corner[:, :, :4]

output = [None for _ in range(len(prediction))]
for i, image_pred in enumerate(prediction):
  # If none are remaining => process next image
  if not image_pred.size(0):
    continue
  # Get score and class with highest confidence
  class_conf, class_pred = torch.max(image_pred[:, 5: 5 + num_classes], 1, keepdim=True)

  conf_mask = (image_pred[:, 4] * class_conf.squeeze() >= conf_thre).squeeze()
  # Detections ordered as (x1, y1, x2, y2, obj_conf, class_conf, class_pred)
  detections = torch.cat((image_pred[:, :5], class_conf, class_pred.float()), 1)
  detections = detections[conf_mask]
  if not detections.size(0):
    continue

  nms_out_index = torchvision.ops.nms(
      detections[:, :4],
      detections[:, 4] * detections[:, 5],
      nms_thre,
  )

  detections = detections[nms_out_index]
  if output[i] is None:
    output[i] = detections
  else:
    output[i] = torch.cat((output[i], detections))

print(output) #(x1, y1, x2, y2, obj_conf, class_conf, class_pred)

# + colab={"base_uri": "https://localhost:8080/"} id="7TKGkyUX1j3U" outputId="1fcc464c-6a3e-4725-f96c-22f2a04cef01"
#取得bboxes、class_id, 信心度
cls_names = ["car"]
img = cv2.imread("datasets/VOCdevkit/VOC2007/JPEGImages/002387.jpg")
ratio = min(test_size[0] / img.shape[0], test_size[1] / img.shape[1])
output_vis = output[0]
if output_vis is not None:
  output_vis = output_vis.cpu()

  bboxes = output_vis[:, 0:4]

  # preprocessing: resize
  bboxes /= ratio
  print("bboxes:", bboxes)
  cls = output_vis[:, 6]
  print("cls:",cls)
  scores = output_vis[:, 4] * output_vis[:, 5]
  print("scores", scores)

  #vis_res = vis(img, bboxes, scores, cls, conf_thre, cls_names)

# + id="th4PeKT96kms"
import numpy as np
_COLORS = np.array(
    [
        0.000, 0.447, 0.741,
        0.850, 0.325, 0.098,
        0.929, 0.694, 0.125,
        0.494, 0.184, 0.556,
        0.466, 0.674, 0.188,
        0.301, 0.745, 0.933,
        0.635, 0.078, 0.184,
        0.300, 0.300, 0.300,
    ]
).astype(np.float32).reshape(-1, 3)

# + colab={"base_uri": "https://localhost:8080/"} id="MTFITc-A5Uz7" outputId="fbb0c4cd-1ed6-4f54-8f89-63659d81680d"
#視覺化
boxes = bboxes
cls_ids = cls
conf = conf_thre
img = cv2.imread("datasets/VOCdevkit/VOC2007/JPEGImages/002387.jpg")
img_copy = img.copy()
for i in range(len(boxes)):
  box = boxes[i]
  cls_id = int(cls_ids[i])
  score = scores[i]
  if score < conf:
    continue
  #box左上角和右下角座標
  x0 = int(box[0])
  y0 = int(box[1])
  x1 = int(box[2])
  y1 = int(box[3])
  print("x0, y0:", x0, y0, " x1, y1:", x1, y1)
  color = (_COLORS[cls_id] * 255).astype(np.uint8).tolist()
  text = '{}:{:.1f}%'.format(cls_names[cls_id], score * 100)
  txt_color = (0, 0, 0) if np.mean(_COLORS[cls_id]) > 0.5 else (255, 255, 255)
  font = cv2.FONT_HERSHEY_SIMPLEX

  txt_size = cv2.getTextSize(text, font, 0.4, 1)[0]
  #畫框
  cv2.rectangle(img_copy, (x0, y0), (x1, y1), color, 2)

  txt_bk_color = (_COLORS[cls_id] * 255 * 0.7).astype(np.uint8).tolist()
  
  cv2.rectangle(
      img_copy,
      (x0, y0 + 1),
      (x0 + txt_size[0] + 1, y0 + int(1.5*txt_size[1])),
      txt_bk_color,
      -1
  )
  
  #寫字
  cv2.putText(img_copy, text, (x0, y0 + txt_size[1]), font, 0.4, txt_color, thickness=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 503} id="z2EA5PUy6tFY" outputId="d6a50012-0ab9-4cf2-fb49-f543f0331d98"
from matplotlib import pyplot as plt
plt.figure(figsize=(12, 8))
plt.imshow(cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB))

StarcoderdataJupyterScriptsDedupFiltered