Skip to content

Commit 6d77193

Browse files
committed
pass in model name and task id to run auto annotation script
1 parent 01dd071 commit 6d77193

File tree

2 files changed

+130
-45
lines changed

2 files changed

+130
-45
lines changed

utils/auto_annotation/README.md

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,65 @@ A small command line program to test and run AutoAnnotation Scripts.
44

55
## Instructions
66

7-
Change in to the root of the project directory and run
7+
There are two modes to run this script in. If you already have a model uploaded into the server, and you're having
8+
issues with running it in production, you can pass in the model name and a task id that you want to test against.
89

910
```shell
10-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
11-
--xml /path/to/xml/file.xml \
12-
--bin /path/to/bin/file.bin \
13-
--json /path/to/json/mapping/mapping.json
11+
# Note that this module can be found in cvat/utils/auto_annotation/run_model.py
12+
$ python /path/to/run_model.py --model-name mymodel --task-id 4
13+
```
14+
15+
If you're running in docker, this can be useful way to debug your model.
16+
17+
``` shell
18+
$ docker exec -it cvat bash -ic 'python3 ~/cvat/apps/auto_annotation/run_model.py --model-name my-model --task-id 4
19+
```
20+
21+
If you are developing an auto annotation model or you can't get something uploaded into the server,
22+
then you'll need to specify the individual inputs.
23+
24+
```shell
25+
# Note that this module can be found in cvat/utils/auto_annotation/run_model.py
26+
$ python path/to/run_model.py --py /path/to/python/interp.py \
27+
--xml /path/to/xml/file.xml \
28+
--bin /path/to/bin/file.bin \
29+
--json /path/to/json/mapping/mapping.json
1430
```
1531
1632
Some programs need to run unrestricted or as an administer. Use the `--unrestriced` flag to simulate.
1733
1834
You can pass image files in to fully simulate your findings. Images are passed in as a list
1935
2036
```shell
21-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
22-
--xml /path/to/xml/file.xml \
23-
--bin /path/to/bin/file.bin \
24-
--json /path/to/json/mapping/mapping.json \
25-
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg
37+
$ python /path/to/run_model.py --py /path/to/python/interp.py \
38+
--xml /path/to/xml/file.xml \
39+
--bin /path/to/bin/file.bin \
40+
--json /path/to/json/mapping/mapping.json \
41+
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg
2642
```
2743
2844
Additionally, it's sometimes useful to visualize your images.
2945
Use the `--show-images` flag to have each image with the annotations pop up.
3046

3147
```shell
32-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
33-
--xml /path/to/xml/file.xml \
34-
--bin /path/to/bin/file.bin \
35-
--json /path/to/json/mapping/mapping.json \
36-
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
37-
--show-images
48+
$ python /path/to/run_model.py --py /path/to/python/interp.py \
49+
--xml /path/to/xml/file.xml \
50+
--bin /path/to/bin/file.bin \
51+
--json /path/to/json/mapping/mapping.json \
52+
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
53+
--show-images
3854
```
3955

4056
If you'd like to see the labels printed on the image, use the `--show-labels` flag
4157
4258
```shell
43-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
44-
--xml /path/to/xml/file.xml \
45-
--bin /path/to/bin/file.bin \
46-
--json /path/to/json/mapping/mapping.json \
47-
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
48-
--show-images \
49-
--show-labels
59+
$ python /path/to/run_model.py --py /path/to/python/interp.py \
60+
--xml /path/to/xml/file.xml \
61+
--bin /path/to/bin/file.bin \
62+
--json /path/to/json/mapping/mapping.json \
63+
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
64+
--show-images \
65+
--show-labels
5066
```
5167
5268
There's a command that let's you scan quickly by setting the length of time (in milliseconds) to display each image.
@@ -55,13 +71,13 @@ In this example, 2000 milliseconds is 2 seconds for each image.
5571
5672
```shell
5773
# Display each image in a window for 2 seconds
58-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
59-
--xml /path/to/xml/file.xml \
60-
--bin /path/to/bin/file.bin \
61-
--json /path/to/json/mapping/mapping.json \
62-
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
63-
--show-images \
64-
--show-image-delay 2000
74+
$ python /path/to/run_model.py --py /path/to/python/interp.py \
75+
--xml /path/to/xml/file.xml \
76+
--bin /path/to/bin/file.bin \
77+
--json /path/to/json/mapping/mapping.json \
78+
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
79+
--show-images \
80+
--show-image-delay 2000
6581
```
6682
6783
Visualization isn't always enough.
@@ -70,10 +86,10 @@ You must install the necessary packages installed, but then you can add the `--s
7086
results will serialize correctly.
7187

7288
```shell
73-
$ python cvat/utils/auto_annotation/run_model.py --py /path/to/python/interp.py \
74-
--xml /path/to/xml/file.xml \
75-
--bin /path/to/bin/file.bin \
76-
--json /path/to/json/mapping/mapping.json \
77-
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
78-
--serialize
89+
$ python /path/to/run_model.py --py /path/to/python/interp.py \
90+
--xml /path/to/xml/file.xml \
91+
--bin /path/to/bin/file.bin \
92+
--json /path/to/json/mapping/mapping.json \
93+
--image-files /path/to/img.jpg /path2/to/img2.png /path/to/img3.jpg \
94+
--serialize
7995
```

utils/auto_annotation/run_model.py

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import argparse
55
import random
66
import logging
7+
import fnmatch
8+
from operator import xor
79

810
import numpy as np
911
import cv2
@@ -18,10 +20,14 @@
1820

1921
def _get_kwargs():
2022
parser = argparse.ArgumentParser()
21-
parser.add_argument('--py', required=True, help='Path to the python interpt file')
22-
parser.add_argument('--xml', required=True, help='Path to the xml file')
23-
parser.add_argument('--bin', required=True, help='Path to the bin file')
24-
parser.add_argument('--json', required=True, help='Path to the JSON mapping file')
23+
parser.add_argument('--py', help='Path to the python interpt file')
24+
parser.add_argument('--xml', help='Path to the xml file')
25+
parser.add_argument('--bin', help='Path to the bin file')
26+
parser.add_argument('--json', help='Path to the JSON mapping file')
27+
28+
parser.add_argument('--model-name', help='Name of the model in the Model Manager')
29+
parser.add_argument('--task-id', type=int, help='ID task used to test the model')
30+
2531
parser.add_argument('--restricted', dest='restricted', action='store_true')
2632
parser.add_argument('--unrestricted', dest='restricted', action='store_false')
2733
parser.add_argument('--image-files', nargs='*', help='Paths to image files you want to test')
@@ -56,14 +62,75 @@ def find_min_y(array):
5662

5763
return array[index]
5864

65+
def _get_docker_files(model_name: str, task_id: int):
66+
os.environ['DJANGO_SETTINGS_MODULE'] = 'cvat.settings.development'
67+
68+
import django
69+
django.setup()
70+
71+
from cvat.apps.auto_annotation.models import AnnotationModel
72+
from cvat.apps.engine.models import Task as TaskModel
73+
74+
task = TaskModel(pk=task_id)
75+
model = AnnotationModel.objects.get(name=model_name)
76+
77+
images_dir = task.get_data_dirname()
78+
79+
py_file = model.interpretation_file.name
80+
mapping_file = model.labelmap_file.name
81+
xml_file = model.model_file.name
82+
bin_file = model.weights_file.name
83+
84+
image_files = []
85+
for root, _, filenames in os.walk(images_dir):
86+
for filename in fnmatch.filter(filenames, '*.jpg'):
87+
image_files.append(os.path.join(root, filename))
88+
89+
return py_file, mapping_file, bin_file, xml_file, image_files
90+
5991

6092
def main():
6193
kwargs = _get_kwargs()
6294

63-
py_file = kwargs['py']
64-
bin_file = kwargs['bin']
65-
mapping_file = kwargs['json']
66-
xml_file = kwargs['xml']
95+
py_file = kwargs.get('py')
96+
bin_file = kwargs.get('bin')
97+
mapping_file = kwargs.get('json')
98+
xml_file = kwargs.get('xml')
99+
100+
model_name = kwargs.get('model_name')
101+
task_id = kwargs.get('task_id')
102+
103+
is_docker = model_name and task_id
104+
105+
# xor is `exclusive or`. English is: if one or the other but not both
106+
if xor(bool(model_name), bool(task_id)):
107+
logging.critical('Must provide both `--model-name` and `--task-id` together!')
108+
return
109+
110+
if is_docker:
111+
files = _get_docker_files(model_name, task_id)
112+
py_file = files[0]
113+
mapping_file = files[1]
114+
bin_file = files[2]
115+
xml_file = files[3]
116+
image_files = files[4]
117+
else:
118+
return_ = False
119+
if not py_file:
120+
logging.critical('Must provide --py file!')
121+
return_ = True
122+
if not bin_file:
123+
logging.critical('Must provide --bin file!')
124+
return_ = True
125+
if not xml_file:
126+
logging.critical('Must provide --xml file!')
127+
return_ = True
128+
if not mapping_file:
129+
logging.critical('Must provide --json file!')
130+
return_ = True
131+
132+
if return_:
133+
return
67134

68135
if not os.path.isfile(py_file):
69136
logging.critical('Py file not found! Check the path')
@@ -98,7 +165,9 @@ def main():
98165
mapping = {int(k): v for k, v in mapping.items()}
99166

100167
restricted = kwargs['restricted']
101-
image_files = kwargs.get('image_files')
168+
169+
if not is_docker:
170+
image_files = kwargs.get('image_files')
102171

103172
if image_files:
104173
image_data = [cv2.imread(f) for f in image_files]

0 commit comments

Comments
 (0)