pytorch 從頭開始YOLOV3(五) 檢測

2021-09-11 22:59:25 字數 3604 閱讀 7416

**完,只要讀取模型,然後進行**就ok了.

1.讀模型

# set up model

model = darknet(opt.model_config_path)

model.load_weights(opt.weight_path)

model.cuda()

model.eval() #

2.讀資料

dataset = datasets(opt.valid)

dataloader = torch.utils.data.dataloader(

dataset, batch_size=opt.batch_size, shuffle=false)

3.**並繪圖

for batch_i, (img_paths, input_imgs, targets) in enumerate(dataloader):

# configure input

input_imgs = input_imgs.type(tensor)

# get detections

with torch.no_grad():

detections = model(input_imgs)

detections = non_max_suppression(

detections, 80, 0.8, 0.4)

# log progress

# 繪製**和真值影象

draw_predict(input_imgs, detections, img_paths)

draw_predict(input_imgs, targets, img_paths, 'gt')

current_time = time.time()

inference_time = datetime.timedelta(seconds=current_time - prev_time)

prev_time = current_time

print ('\t+ batch %d, inference time: %s' % (batch_i, inference_time))

4.繪圖(本文儲存使用有填充的,如果需要沒有填充的,**中也有,具體請看github原始碼)

讀影象

for detection in detections:

if detection is none:

continue

img_path = img_paths[ii]

# 由於是torch.cuda()格式,需要轉化為陣列

image = imgs[ii].cpu().numpy() * 255

# opencv為gbr,所以要轉化通道 ,這裡有點bug,所以先存在讀取.

w, h, _ = image.shape

讀bounding box座標,由於真值和**的資料格式不同,所以要分別處理.

if types == 'pred':

# yolo**的為(x1,y1,x2,y2,c,score,label)

bboxs = detection[:, 0:4]

name = detection[:, -1].cpu().numpy()

score = detection[:, 5]

# print detection.shape

x1 = detection[:, 0]

y1 = detection[:, 1]

x2 = detection[:, 2]

y2 = detection[:, 3]

else:

# yolo的真值標籤為(label,x,y,w,h) 其中,x,y是bounding box矩陣中心

# 這裡求出x1,y1,x2,y2

target = np.zeros((50, 5))

target[:, 0] = detection[:, 0]

target[:, 1] = (detection[:, 1] - detection[:, 3] / 2.0) * w

target[:, 2] = (detection[:, 2] - detection[:, 4] / 2.0) * h

target[:, 3] = (detection[:, 1] + detection[:, 3] / 2.0) * w

target[:, 4] = (detection[:, 2] + detection[:, 4] / 2.0) * h

# 由於真值包含零填充,所以去除掉

detection = target[np.sum(target, axis=1) > 0]

if detection is none:

continue

bboxs = detection[:, 1:5]

name = detection[:, 0]

# score = 1.0

# print detection.shape

x1 = detection[:, 1]

y1 = detection[:, 2]

x2 = detection[:, 3]

y2 = detection[:, 4]

繪圖和儲存

for i in range(len(name)):

xmin = int(round(float(x1[i])))

ymin = int(round(float(y1[i])))

xmax = int(round(float(x2[i])))

ymax = int(round(float(y2[i])))

# if score[i] <= 0.9:

# continue

if xmax < xmin or ymax < ymin:

continue

# 繪製bounding box

cv2.rectangle(image, (xmin, ymin),

(xmax, ymax), (0, 0, 255), 1)

# 繪製label

cv2.puttext(image, classname[int(name[i])], (xmin, ymin - 10),

cv2.font_hershey_******x, 1e-3 * image.shape[0], (0, 0, 255), 1)

# 繪製標籤

Deep Learning 從頭開始

deep learning已經火了好久,有些人已經在這裡面耕耘了好多年,而有些人才剛剛開始,比如本人。如何才能快速地進入這個領域在較短的時間內掌握deep learning最新的技術是值得思考的問題。就目前的情況看,通過網路上的課程及各種tutorials以及各種 來研究這個領域是最佳的途徑。經過一...

git從頭開始

當你本地修改了乙個檔案,而且該檔案被另乙個人修改,並push了,那麼 users terry workspace git練習 git practise git master git pull updating 67e4e18.cdbf666 error your local changes to t...

English 從頭開始

我們有好多事情都不能重新開始,比如我們的人生你沒有辦法把自己在塞回媽媽的肚子裡吧?比如我們的時間在此時此刻只有這乙個時間,全世界不會再有第二個。比如我們後悔的事情.雖然有很多我們無法改變的事情存在,但也有許多我們可以改變的事情。雖然我們沒有辦法從一歲開始重新開始,但我們可以掌握自己的人生,做自己想做...