-
Notifications
You must be signed in to change notification settings - Fork 39
dev yolov3 (train and validate) #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mls1999725
wants to merge
10
commits into
main
Choose a base branch
from
yolov3_train_val
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
62c3d41
validate
mls1999725 25d22a4
train
mls1999725 cfa642c
evaluate
mls1999725 85b18a9
update loss
mls1999725 5e9f307
early stopping
mls1999725 3ca1ecd
update logger
mls1999725 cccc10f
fix bugs
mls1999725 8ccfb3d
update metrics
mls1999725 dccf4e7
requirements
mls1999725 8e0f97d
fix requirements
mls1999725 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import ctypes | ||
import argparse | ||
import time | ||
from pathlib import Path | ||
|
||
import cv2 | ||
import numpy as np | ||
import oneflow as flow | ||
|
||
from models.experimental import attempt_load | ||
from utils.datasets import LoadImages, CLASS_NAMES | ||
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \ | ||
scale_coords, scale_coords_np, xyxy2xywh, strip_optimizer, set_logging, increment_path, save_one_box | ||
from utils.plots import colors, plot_one_box | ||
from utils.flow_utils import select_device, load_classifier, time_synchronized | ||
from ops import lib_path | ||
|
||
|
||
def detect(opt): | ||
# p = ctypes.CDLL(lib_path()) | ||
source, weights, view_img, save_txt, imgsz = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size | ||
save_img = not opt.nosave and not source.endswith('.txt') # save inference images | ||
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith( | ||
('rtsp://', 'rtmp://', 'http://', 'https://')) | ||
|
||
# Directories | ||
save_dir = increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok) # increment run | ||
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir | ||
|
||
# Initialize | ||
set_logging() | ||
device = select_device(opt.device) | ||
#half = device.type != 'cpu' # half precision only supported on CUDA | ||
|
||
# Load model | ||
model = attempt_load(weights, opt.cfg, device) # load FP32 model | ||
stride = int(model.stride.max().numpy()) # model stride | ||
imgsz = check_img_size(imgsz, s=stride) # check img_size | ||
names = CLASS_NAMES | ||
|
||
# Second-stage classifier | ||
classify = False | ||
if classify: | ||
modelc = load_classifier(name='resnet101', n=2) # initialize | ||
modelc.load_state_dict(flow.load('weights/resnet101_ckpt')) | ||
|
||
# Set Dataloader | ||
vid_path, vid_writer = None, None | ||
if webcam: | ||
view_img = check_imshow() | ||
dataset = LoadStreams(source, img_size=imgsz, stride=stride) | ||
else: | ||
dataset = LoadImages(source, img_size=imgsz, stride=stride) | ||
|
||
# Run inference | ||
if device.type != 'cpu': | ||
with flow.no_grad(): | ||
model(flow.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once | ||
t0 = time.time() | ||
with flow.no_grad(): | ||
for path, img, im0s, vid_cap in dataset: | ||
img = flow.tensor(img).to(device) | ||
img = img.to(dtype=flow.float) | ||
img /= 255.0 # 0 - 255 to 0.0 - 1.0 | ||
if img.ndimension() == 3: | ||
img = img.unsqueeze(0) | ||
|
||
# Inference | ||
t1 = time_synchronized() | ||
pred = model(img, opt.augment)[0] | ||
np.save("output_oneflow.npy", pred.cpu().numpy()) | ||
|
||
# Apply NMS | ||
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, opt.classes, opt.agnostic_nms, | ||
max_det=opt.max_det) | ||
t2 = time_synchronized() | ||
|
||
# Apply Classifier | ||
if classify: | ||
pred = apply_classifier(pred, modelc, img, im0s) | ||
|
||
# Process detections | ||
for i, det in enumerate(pred): # detections per image | ||
if webcam: # batch_size >= 1 | ||
p, s, im0, frame = path[i], f'{i}: ', im0s[i].copy(), dataset.count | ||
else: | ||
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0) | ||
|
||
p = Path(p) # to Path | ||
save_path = str(save_dir / p.name) # img.jpg | ||
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt | ||
s += '%gx%g ' % img.numpy().shape[2:] # print string | ||
gn = flow.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh | ||
imc = im0.copy() if opt.save_crop else im0 # for opt.save_crop | ||
if len(det): | ||
# Rescale boxes from img_size to im0 size | ||
det[:, :4] = scale_coords_np(img.shape[2:], det[:, :4], im0.shape).round() | ||
|
||
# Print results | ||
for c in np.unique(det[:, -1]): | ||
n = (det[:, -1] == c).sum() # detections per class | ||
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string | ||
|
||
# Write results | ||
for *xyxy, conf, cls in reversed(det): | ||
if save_txt: # Write to file | ||
xywh = (xyxy2xywh(flow.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh | ||
line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) # label format | ||
with open(txt_path + '.txt', 'a') as f: | ||
f.write(('%g ' * len(line)).rstrip() % line + '\n') | ||
|
||
if save_img or opt.save_crop or view_img: # Add bbox to image | ||
c = int(cls) # integer class | ||
label = None if opt.hide_labels else (names[c] if opt.hide_conf else f'{names[c]} {conf:.2f}') | ||
plot_one_box(xyxy, im0, label=label, color=colors(c, True), line_thickness=opt.line_thickness) | ||
if opt.save_crop: | ||
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True) | ||
|
||
# Print time (inference + NMS) | ||
print(f'{s}Done. ({t2 - t1:.3f})') | ||
|
||
# Stream results | ||
if view_img: | ||
cv2.imshow(str(p), im0) | ||
cv2.waitKey(1) # 1 millisecond | ||
|
||
# Save results (image with detections) | ||
if save_img: | ||
if dataset.mode == 'image': | ||
cv2.imwrite(save_path, im0) | ||
else: # 'video' or 'stream' | ||
if vid_path != save_path: # new video | ||
vid_path = save_path | ||
if isinstance(vid_writer, cv2.VideoWriter): | ||
vid_writer.release() # release previous video writer | ||
if vid_cap: # video | ||
fps = vid_cap.get(cv2.CAP_PROP_FPS) | ||
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | ||
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | ||
else: # stream | ||
fps, w, h = 30, im0.shape[1], im0.shape[0] | ||
save_path += '.mp4' | ||
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) | ||
vid_writer.write(im0) | ||
|
||
if save_txt or save_img: | ||
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else '' | ||
print(f"Results saved to {save_dir}{s}") | ||
|
||
print(f'Done. ({time.time() - t0:.3f}s)') | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--weights', nargs='+', type=str, default='yolov3_ckpt', help='model path(s)') | ||
parser.add_argument('--cfg', type=str, default='models/yolov3.yaml', help='model.yaml path') | ||
parser.add_argument('--source', type=str, default='dataset/images', help='source') # file/folder, 0 for webcam | ||
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') | ||
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold') | ||
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS') | ||
parser.add_argument('--max-det', type=int, default=1000, help='maximum number of detections per image') | ||
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') | ||
parser.add_argument('--view-img', action='store_true', help='display results') | ||
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') | ||
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels') | ||
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes') | ||
parser.add_argument('--nosave', action='store_true', help='do not save images/videos') | ||
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3') | ||
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') | ||
parser.add_argument('--augment', action='store_true', help='augmented inference') | ||
parser.add_argument('--update', action='store_true', help='update all models') | ||
parser.add_argument('--project', default='runs/detect', help='save results to project/name') | ||
parser.add_argument('--name', default='exp', help='save results to project/name') | ||
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment') | ||
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)') | ||
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels') | ||
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences') | ||
opt = parser.parse_args() | ||
print(opt) | ||
check_requirements(exclude=('pycocotools',)) | ||
|
||
if opt.update: # update all models (to fix SourceChangeWarning) | ||
for opt.weights in ['yolov3_ckpt', 'yolov3-spp_ckpt', 'yolov3-tiny_ckpt']: | ||
detect(opt=opt) | ||
strip_optimizer(opt.weights) | ||
else: | ||
detect(opt=opt) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# YOLOv3 common modules | ||
|
||
import math | ||
from copy import copy | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import requests | ||
import oneflow as flow | ||
import oneflow.nn as nn | ||
|
||
from utils.general import make_divisible | ||
|
||
|
||
def autopad(k, p=None): # kernel, padding | ||
# Pad to 'same' | ||
if p is None: | ||
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad | ||
return p | ||
|
||
|
||
def DWConv(c1, c2, k=1, s=1, act=True): | ||
# Depthwise convolution | ||
return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) | ||
|
||
|
||
class Conv(nn.Module): | ||
# Standard convolution | ||
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups | ||
super(Conv, self).__init__() | ||
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) | ||
self.bn = nn.BatchNorm2d(c2) | ||
self.act = nn.LeakyReLU(0.1, inplace=False) if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) | ||
|
||
def forward(self, x): | ||
return self.act(self.bn(self.conv(x))) | ||
|
||
def fuseforward(self, x): | ||
return self.act(self.conv(x)) | ||
|
||
|
||
class Bottleneck(nn.Module): | ||
# Standard bottleneck | ||
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion | ||
super(Bottleneck, self).__init__() | ||
c_ = int(c2 * e) # hidden channels | ||
self.cv1 = Conv(c1, c_, 1, 1) | ||
self.cv2 = Conv(c_, c2, 3, 1, g=g) | ||
self.add = shortcut and c1 == c2 | ||
|
||
def forward(self, x): | ||
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) | ||
|
||
|
||
class BottleneckCSP(nn.Module): | ||
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks | ||
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion | ||
super(BottleneckCSP, self).__init__() | ||
c_ = int(c2 * e) | ||
self.cv1 = Conv(c1, c_, 1, 1) | ||
self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) | ||
self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) | ||
self.cv4 = Conv(2 * c_, c2, 1, 1) | ||
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) | ||
self.act = nn.LeakyReLU(0.1, inplace=False) | ||
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) | ||
|
||
def forward(self, x): | ||
y1 = self.cv3(self.m(self.cv1(x))) | ||
y2 = self.cv2(x) | ||
return self.cv4(self.act(self.bn(flow.cat((y1, y2), dim=1)))) | ||
|
||
|
||
class C3(nn.Module): | ||
# CSP bottleneck with 3 convolutions | ||
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion | ||
super(C3, self).__init__() | ||
c_ = int(c2 * e) # hidden channels | ||
self.cv1 = Conv(c1, c_, 1, 1) | ||
self.cv2 = Conv(c1, c_, 1, 1) | ||
self.cv3 = Conv(2 * c_, c2, 1) # act=FReLU(c2) | ||
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) | ||
|
||
def forward(self, x): | ||
return self.cv3(flow.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1)) | ||
|
||
|
||
class SPP(nn.Module): | ||
# Spatial pyramid pooling layer used in YOLOv3-SPP | ||
def __init__(self, c1, c2, k=(5, 9, 13)): | ||
super(SPP, self).__init__() | ||
c_ = c1 // 2 # hidden channels | ||
self.cv1 = Conv(c1, c_, 1, 1) | ||
self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) | ||
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) | ||
|
||
def forward(self, x): | ||
x = self.cv1(x) | ||
return self.cv2(flow.cat([x] + [m(x) for m in self.m], 1)) | ||
|
||
|
||
class Contract(nn.Module): | ||
# Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40) | ||
def __init__(self, gain=2): | ||
super().__init__() | ||
self.gain = gain | ||
|
||
def forward(self, x): | ||
N, C, H, W = x.size() # assert (H / s == 0) and (W / s == 0), 'Indivisible gain' | ||
s = self.gain | ||
x = x.view(N, C, H // s, s, W // s, s) # x(1,64,40,2,40,2) | ||
x = x.permute(0, 3, 5, 1, 2, 4) # x(1,2,2,64,40,40) | ||
return x.view(N, C * s * s, H // s, W // s) # x(1,256,40,40) | ||
|
||
|
||
class Focus(nn.Module): | ||
# Focus wh information into c-space | ||
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups | ||
super(Focus, self).__init__() | ||
self.conv = Conv(c1 * 4, c2, k, s, p, g, act) | ||
# self.contract = Contract(gain=2) | ||
|
||
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) | ||
return self.conv(flow.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) | ||
# return self.conv(self.contract(x)) | ||
|
||
|
||
class Expand(nn.Module): | ||
# Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160) | ||
def __init__(self, gain=2): | ||
super().__init__() | ||
self.gain = gain | ||
|
||
def forward(self, x): | ||
N, C, H, W = x.size() # assert C / s ** 2 == 0, 'Indivisible gain' | ||
s = self.gain | ||
x = x.view(N, s, s, C // s ** 2, H, W) # x(1,2,2,16,80,80) | ||
x = x.permute(0, 3, 4, 1, 5, 2) # x(1,16,80,2,80,2) | ||
return x.view(N, C // s ** 2, H * s, W * s) # x(1,16,160,160) | ||
|
||
|
||
class Concat(nn.Module): | ||
# Concatenate a list of tensors along dimension | ||
def __init__(self, dimension=1): | ||
super(Concat, self).__init__() | ||
self.d = dimension | ||
|
||
def forward(self, x): | ||
return flow.cat(x, self.d) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把没有使用的code都删了吧