Skip to content
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

WIP: yolov5 analyser #189

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions examples/yolov5-test.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
folder: media/yolov5
select:
name: Local
config:
source: data/images
aggregate: true
# select:
# name: Youtube
# config:
# in_parallel: false
# search_term: Tear gas
# uploaded_before: "2015-10-02T00:00:00Z"
# uploaded_after: "2015-10-01T00:00:00Z"
elements_in:
- Youtube
analyse:
name: TorchHub
config:
dev: true
repo: ultralytics/yolov5
args:
- yolov5s
kwargs:
pretrained: true
- name: Frames
- name: Yolov5
config:
in_parallel: false
51 changes: 51 additions & 0 deletions src/lib/analysers/Yolov5/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
from lib.common.exceptions import InvalidAnalyserConfigError
from lib.common.analyser import Analyser
from lib.common.etypes import Etype, Union, Array
from lib.util.cvjson import generate_meta
from lib.etypes.cvjson import CvJson

import torch

class Yolov5(Analyser):
in_etype = Union(Array(Etype.Image), Etype.Json)
out_etype = CvJson
""" Override to always run serially. Otherwise it hangs, presumably due to
the parallelisation that tensorflow does under the hood. """

@property
def in_parallel(self):
return False

def pre_analyse(self, config):
# TODO: work out how to load in custom weights, rather than using this default.
self.model = torch.hub.load("ultralytics/yolov5", "yolov5n")

# This function assumes that `element.paths` represents an array of images
# to be interpreted. The `get_preds` function operates on a single image,
# accepting one argument that is a path to an image. It returns a list of
# tuples `('classname', 0.8)`, where `'classname'` is a string
# representing the class predicted, and `0.8` is the normalized prediction
# probability between 0 and 1. See KerasPretrained/core.py in analysers
# for an example. """
def get_preds(img_path: Path):
results = self.model(img_path)
# TODO: work out how to return not the `results` object from yolov5,
# but a list of tuples as specified above
return []

self.get_preds = get_preds


def analyse_element(self, element, _):
self.logger(f"Running inference on frames in {element.id}...")
val = Etype.CvJson.from_preds(element, self.get_preds)
self.logger("Tried to show results.")
self.disk.delete_local_on_write = True
return val

def post_analyse(self, elements) -> Etype.Json.as_array():
return generate_meta(elements, logger=self.logger)


module = Yolov5
6 changes: 6 additions & 0 deletions src/lib/analysers/Yolov5/info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
desc: Classify objects in images using YoloV5 - https://github.com/ultralytics/yolov5. This analyser allows you to run inference using one of the pretrained checkpoints, or to provide custom weights.
args: []
# - name: checkpoint
# desc: The checkpoint you want to use for weights. This can be a local path to custom weights, or one of the available pretrained checkpoints (see https://github.com/ultralytics/yolov5#pretrained-checkpoints).
# required: true
# input: string
2 changes: 2 additions & 0 deletions src/lib/analysers/Yolov5/partial.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RUN git clone https://github.com/ultralytics/yolov5
RUN cd yolov5 && pip install -r requirements.txt
3 changes: 3 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Youtube
Frames
Yolov5