Skip to content

Commit

Permalink
v9.1 release (ultralytics#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored Jan 13, 2021
1 parent 0bc1db5 commit 166a4d5
Show file tree
Hide file tree
Showing 17 changed files with 519 additions and 249 deletions.
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ WORKDIR /usr/src/app
# Copy contents
COPY . /usr/src/app

# Copy weights
#RUN python3 -c "from models import *; \
#attempt_download('weights/yolov3.pt'); \
#attempt_download('weights/yolov3-spp.pt'); \
#attempt_download('weights/yolov3-tiny.pt')"


# --------------------------------------------------- Extras Below ---------------------------------------------------

Expand All @@ -31,7 +25,7 @@ COPY . /usr/src/app
# for v in {300..303}; do t=ultralytics/coco:v$v && sudo docker build -t $t . && sudo docker push $t; done

# Pull and Run
# t=ultralytics/yolov3:latest && sudo docker pull $t && sudo docker run -it --ipc=host $t
# t=ultralytics/yolov3:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all $t

# Pull and Run with local directory access
# t=ultralytics/yolov3:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all -v "$(pwd)"/coco:/usr/src/coco $t
Expand Down
28 changes: 14 additions & 14 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, \
strip_optimizer, set_logging, increment_path
from utils.general import check_img_size, check_requirements, non_max_suppression, apply_classifier, scale_coords, \
xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.plots import plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized

Expand Down Expand Up @@ -81,12 +81,13 @@ def detect(save_img=False):
# Process detections
for i, det in enumerate(pred): # detections per image
if webcam: # batch_size >= 1
p, s, im0 = Path(path[i]), '%g: ' % i, im0s[i].copy()
p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
else:
p, s, im0 = Path(path), '', im0s
p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0)

save_path = str(save_dir / p.name)
txt_path = str(save_dir / 'labels' / p.stem) + ('_%g' % dataset.frame if dataset.mode == 'video' else '')
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.shape[2:] # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
if len(det):
Expand All @@ -96,7 +97,7 @@ def detect(save_img=False):
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += '%g %ss, ' % (n, names[int(c)]) # add to string
s += f'{n} {names[int(c)]}s, ' # add to string

# Write results
for *xyxy, conf, cls in reversed(det):
Expand All @@ -107,23 +108,21 @@ def detect(save_img=False):
f.write(('%g ' * len(line)).rstrip() % line + '\n')

if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

# Print time (inference + NMS)
print('%sDone. (%.3fs)' % (s, t2 - t1))
print(f'{s}Done. ({t2 - t1:.3f}s)')

# Stream results
if view_img:
cv2.imshow(str(p), im0)
if cv2.waitKey(1) == ord('q'): # q to quit
raise StopIteration

# Save results (image with detections)
if save_img:
if dataset.mode == 'images':
if dataset.mode == 'image':
cv2.imwrite(save_path, im0)
else:
else: # 'video'
if vid_path != save_path: # new video
vid_path = save_path
if isinstance(vid_writer, cv2.VideoWriter):
Expand All @@ -140,7 +139,7 @@ def detect(save_img=False):
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('Done. (%.3fs)' % (time.time() - t0))
print(f'Done. ({time.time() - t0:.3f}s)')


if __name__ == '__main__':
Expand All @@ -163,6 +162,7 @@ def detect(save_img=False):
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
opt = parser.parse_args()
print(opt)
check_requirements()

with torch.no_grad():
if opt.update: # update all models (to fix SourceChangeWarning)
Expand Down
27 changes: 14 additions & 13 deletions hubconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
set_logging()


def create(name, pretrained, channels, classes):
def create(name, pretrained, channels, classes, autoshape):
"""Creates a specified YOLOv3 model
Arguments:
Expand All @@ -41,7 +41,8 @@ def create(name, pretrained, channels, classes):
model.load_state_dict(state_dict, strict=False) # load
if len(ckpt['model'].names) == classes:
model.names = ckpt['model'].names # set class names attribute
# model = model.autoshape() # for PIL/cv2/np inputs and NMS
if autoshape:
model = model.autoshape() # for file/URI/PIL/cv2/np inputs and NMS
return model

except Exception as e:
Expand All @@ -50,7 +51,7 @@ def create(name, pretrained, channels, classes):
raise Exception(s) from e


def yolov3(pretrained=False, channels=3, classes=80):
def yolov3(pretrained=False, channels=3, classes=80, autoshape=True):
"""YOLOv3 model from https://github.com/ultralytics/yolov3
Arguments:
Expand All @@ -61,10 +62,10 @@ def yolov3(pretrained=False, channels=3, classes=80):
Returns:
pytorch model
"""
return create('yolov3', pretrained, channels, classes)
return create('yolov3', pretrained, channels, classes, autoshape)


def yolov3_spp(pretrained=False, channels=3, classes=80):
def yolov3_spp(pretrained=False, channels=3, classes=80, autoshape=True):
"""YOLOv3-SPP model from https://github.com/ultralytics/yolov3
Arguments:
Expand All @@ -75,10 +76,10 @@ def yolov3_spp(pretrained=False, channels=3, classes=80):
Returns:
pytorch model
"""
return create('yolov3-spp', pretrained, channels, classes)
return create('yolov3-spp', pretrained, channels, classes, autoshape)


def yolov3_tiny(pretrained=False, channels=3, classes=80):
def yolov3_tiny(pretrained=False, channels=3, classes=80, autoshape=True):
"""YOLOv3-tiny model from https://github.com/ultralytics/yolov3
Arguments:
Expand All @@ -89,16 +90,17 @@ def yolov3_tiny(pretrained=False, channels=3, classes=80):
Returns:
pytorch model
"""
return create('yolov3-tiny', pretrained, channels, classes)
return create('yolov3-tiny', pretrained, channels, classes, autoshape)


def custom(path_or_model='path/to/model.pt'):
def custom(path_or_model='path/to/model.pt', autoshape=True):
"""YOLOv3-custom model from https://github.com/ultralytics/yolov3
Arguments (3 options):
path_or_model (str): 'path/to/model.pt'
path_or_model (dict): torch.load('path/to/model.pt')
path_or_model (nn.Module): torch.load('path/to/model.pt')['model']
Returns:
pytorch model
"""
Expand All @@ -109,13 +111,12 @@ def custom(path_or_model='path/to/model.pt'):
hub_model = Model(model.yaml).to(next(model.parameters()).device) # create
hub_model.load_state_dict(model.float().state_dict()) # load state_dict
hub_model.names = model.names # class names
return hub_model
return hub_model.autoshape() if autoshape else hub_model


if __name__ == '__main__':
model = create(name='yolov3', pretrained=True, channels=3, classes=80) # pretrained example
model = create(name='yolov3', pretrained=True, channels=3, classes=80, autoshape=True) # pretrained example
# model = custom(path_or_model='path/to/model.pt') # custom example
model = model.autoshape() # for PIL/cv2/np inputs and NMS

# Verify inference
from PIL import Image
Expand Down
Loading

0 comments on commit 166a4d5

Please sign in to comment.