dgarrett-synaptics commited on
Commit
689ac29
·
1 Parent(s): 8491e38

Adding the README info

Browse files
Files changed (1) hide show
  1. README.md +395 -0
README.md CHANGED
@@ -11,3 +11,398 @@ short_description: Helps train SR Models
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+
16
+ * Introduction
17
+
18
+ The SyNet repository is a library for developing networks for
19
+ Synaptics vision chips. It consists of PyTorch model components
20
+ which can be exported to TensorFlow and tflite without an
21
+ intermediate export backend like ONNX. The resulting tflite files
22
+ are clean, and respect chip memory constraints. The aim of SyNet is
23
+ to streamline the process of generating trained models to deploy on
24
+ Synaptics chips, for internal and external use.
25
+
26
+ In addition to model definitions, analysis, data manipulation, and
27
+ data analysis tools, SyNet also aims to support several "backends".
28
+ For now, the only backend supported in Ultralytics. Ultralytics
29
+ provides a suite of tools for training vision models and visualizing
30
+ those models. Using Ultralytics as a backend, you can generate
31
+ trained vision models optimized for our chips.
32
+
33
+ This code is GPL, and the Ultralytics backend is AGPL, but the
34
+ output of this code are tflite files. These output tflite files are
35
+ data that is not covered by the copyright on the code of either code
36
+ base. Consequently, the respective licenses of the code do not
37
+ apply to the output tflite files.
38
+
39
+ * Performance
40
+
41
+ The following table summarizes model performance on the person class
42
+ of the COCO dataset for four major computer vision tasks. All of
43
+ these models run on VGA (640x480) resolution at about 10fps on the
44
+ Sabre A0 chip.
45
+
46
+ | Task | Score | Metric | Data |
47
+ |-----------------------+-------+---------------+------------------------------------------------------|
48
+ | Classification | 0.945 | Top1 accuracy | Person Visual Wake Words with standard minival split |
49
+ | Object Detection | 0.730 | Box AP50 | COCO detection subset used by Ultralytics |
50
+ | Pose Estimation | 0.729 | Pose AP50 | COCO keypoint subset used by Ultralytics |
51
+ | Instance Segmentation | 0.631 | Mask AP50 | COCO segmentation subset used by Ultralytics |
52
+
53
+ * Roadmap
54
+
55
+ ** Current Features
56
+
57
+ - Models optimized for Sabre
58
+ - Memory and compute efficient model components
59
+ - Usable with PyTorch and TensorFlow training libraries
60
+ - Usable from the command line or python environment
61
+ - In-model demosaicing export option
62
+ - faster than demosaic hardware block
63
+ - increases available weight memory by >2x
64
+ - Includes slim tflite runtime utilities (for demos)
65
+ - Has the ability to support training backends
66
+ - Main backend is Ultralytics
67
+ - Currently supports all core visions tasks from Ultralytics
68
+ - Object Detection
69
+ - Pose Estimation
70
+ - Instance Segmintation
71
+ - Classification
72
+ - Supports evaluating tflites through Ultralytics
73
+ - Allows for easy setup of laptop demos
74
+ - quickly run and view model running on webcam
75
+ - Includes more advanced custom tflite evaluation (not through
76
+ Ultralytics).
77
+ - Computes combined and per-dataset statistics of model
78
+ performance.
79
+
80
+ ** Planned for later releases
81
+
82
+ - Models optimized for VS680
83
+ - Automatic model selection from zoo
84
+ - Select training resolution, inference resolution, and heads.
85
+ - Dataset manipulation tools
86
+ - subsample and combine classes for embedded application
87
+ - camera augmentations
88
+ - Enable arbitrary addition of box attributes to regress
89
+ - age, orientation (pitch, yaw, roll)
90
+ - Complete miscelaneous tasks (see corresponding Gitlab milestone)
91
+ - Mixed precision support
92
+
93
+ ** Future Research
94
+
95
+ - Hugging Face backend integration
96
+
97
+ * Installation
98
+
99
+ In more complex setups, you should create a virtual environment:
100
+ https://docs.python.org/3/library/venv.html. In the following
101
+ examples, we include the Ultralytics backend by adding '[ultra]'.
102
+ To install via pip:
103
+
104
+ #+begin_src shell
105
+ pip install "synet[ultra] @ git+ssh://git@gitlab.synaptics.com/wssd-ai-algorithms/synet-fork.git"
106
+ #+end_src
107
+
108
+ or if you have cloned to a local copy of the repository:
109
+
110
+ #+begin_src shell
111
+ pip install [-e] "/PATH/TO/LOCAL/SYNET[ultra]"
112
+ #+end_src
113
+
114
+ where '-e' will allow you to make edits to your local clone after
115
+ install. If the install fails in any way, please report this to us.
116
+ In this case, you can use the exact library versions used to produce
117
+ the results in the Performance section above using the following
118
+ (requires CUDA 11.8)
119
+
120
+ #+begin_src shell
121
+ pip install -r /PATH/TO/LOCAL/SYNET/requirements-11.8.txt "/PATH/TO/LOCAL/SYNET[ultra]"
122
+ #+end_src
123
+
124
+ * Quickstart
125
+
126
+ In the following, I give a simple example of how one can train a model on COCO, quantize to tflite, and benchmark that tflite on a custom dataset specified by a user's CUSTOM_DATA.yaml. First, train the model:
127
+ #+begin_src shell
128
+ synet ultralytics train model=sabre-detect-vga.yaml data=coco.yaml
129
+ #+end_src
130
+ Quantize the trained model.
131
+ #+begin_src shell
132
+ synet quantize --backend ultralytics --tflite runs/train/detect/weights/best.pt --data /path/to/coco.yaml
133
+ #+end_src
134
+ Evaluate that trained and quantized model.
135
+ #+begin_src shell
136
+ synet ultralytics val model=runs/train/detect/weights/best.tflite task=detect data=coco.yaml
137
+ #+end_src
138
+ If you have a custom evaluation dataset, you can evaluate on that (e.g. test split) as well
139
+ #+begin_src shell
140
+ synet ultralytics val model=runs/train/detect/weights/best.tflite split=test task=detect save_txt=True save_conf=True data=CUSTOM_DATA.yaml
141
+ #+end_src
142
+ And finally generate metrics for the model performance, especially at the .95 precision operating point.
143
+ #+begin_src shell
144
+ synet metrics CUSTOM_DATA.YAML --out-dirs runs/detect/val --project runs/detect/val --precision .95
145
+ #+end_src
146
+
147
+ * Core Shell API
148
+
149
+ The basic syntax for running SyNet from a shell is:
150
+
151
+ #+begin_src shell
152
+ synet [entrypoint] [entrypoint specific args]
153
+ #+end_src
154
+
155
+ Where entrypoint can be a native SyNet module, or a backend like
156
+ ultralytics. For instance:
157
+
158
+ #+begin_src shell
159
+ synet ultralytics train ...
160
+ synet quantize --backend ultralytics ...
161
+ #+end_src
162
+
163
+ Notice that while some backends are callable this way, the backend
164
+ may also need to be specified for other modules. For instance,
165
+ synet.quantize needs to know with which backend to load the model.
166
+
167
+ For information on training/visualizing models, see the section on
168
+ backends below.
169
+
170
+ ** Quantize
171
+
172
+ The SyNet repository includes the ability to quantize models
173
+
174
+ #+begin_src shell
175
+ synet quantize --backend BACKEND --weights MODEL_PT_SAVE --data REP_DATA
176
+ #+end_src
177
+
178
+ For instance, running:
179
+
180
+ #+begin_src shell
181
+ synet quantize --backend ultralytics --weights ./exp/weights/best.pt --data /PATH/TO/CUSTOM_DATASET.YAML --image-shape 480 640
182
+ #+end_src
183
+
184
+ will create a tflite at ./exp/weights/best.tflite with input shape
185
+ [480, 640]. The image shape will default to whatever the model is
186
+ designed to take, but can be overrided in this way. You may also
187
+ specify a model yaml like so:
188
+
189
+ #+begin_src shell
190
+ synet quantize --backend ultralytics --cfg sabre-detect-qvga.yaml
191
+ #+end_src
192
+
193
+ This will place a quantized model at ./sabre-detect-qvga.tflite.
194
+ This will let you inspect the architecture, though it will not be a
195
+ trained model, so the model output will be useless. For more
196
+ information see:
197
+
198
+ #+begin_src shell
199
+ synet quantize --help
200
+ #+end_src
201
+
202
+ ** Metrics
203
+
204
+ SyNet's metrics code is an advanced model benchmarking tool which
205
+ allows the user to simultaneously score object detection on
206
+ multiple datasets. The benefit of doing multiple datasets is that
207
+ it can find a confidence threshold by applying a precision
208
+ threshold to the combined data. This global operating point is
209
+ then applied to each dataset individually. Plots are generated
210
+ showing the mAP curves for each class, each dataset, the combined
211
+ dataset, and combined classes. Additionally, on each curve, the
212
+ global precision point, the dataset precision point, and the .5
213
+ confidence point are plotted. The exact coordinates and
214
+ confidences of each point are printed. The basic usage is:
215
+
216
+ #+begin_src shell
217
+ synet metrics DATA1.YAML DATA2.YAML... --out-dirs OUT_AIR1 OUT_DIR2... --project PLOT_DIR --precisions PRECISION...
218
+ #+end_src
219
+
220
+ There must be one data yaml for each dataset, and they are expected
221
+ to be in Ultralytics format:
222
+ https://docs.ultralytics.com/datasets/?h=data#steps-to-contribute-a-new-dataset
223
+
224
+ If present, the 'test' data split is used. Otherwise, the 'val'
225
+ split is used for each dataset. The metrics code does not actually
226
+ run the model, but instead uses the output from running the model
227
+ via a different code, hence the "OUT_DIR" is the output directory
228
+ of that other code. This may be changed in the future, but
229
+ currently you should populate the out dir with the only supported
230
+ backend:
231
+
232
+ #+begin_src shell
233
+ synet ultralytics val model=/PATH/TO/BEST.TFLITE split=test imgsz=HEIGHT,WIDTH data=DATA1.YAML task=detect save_txt=True save_conf=True
234
+ #+end_src
235
+
236
+ See notes on validation in the ultralytics backend section below.
237
+ For more information on the metrics code see:
238
+
239
+ #+begin_src shell
240
+ synet metrics --help
241
+ #+end_src
242
+
243
+ * Core Python API
244
+
245
+ ** Base Layers
246
+
247
+ *** Converting to Keras/TensorFlow
248
+
249
+ SyNet exists to be the glue between State of the Art training, and
250
+ our chips. Each model component knows how to "export itself" to a
251
+ Keras/TensorFlow model. This done approximately like so:
252
+
253
+ #+begin_src python
254
+ from keras import Input, Model
255
+ from synet.base import askeras
256
+ model = ...
257
+ inp = Input(...)
258
+ with askeras:
259
+ kmodel = Model(inp, model(inp))
260
+ #+end_src
261
+
262
+ This method works so long as only SyNet blocks operate directly on
263
+ the input. For a more complex example, see quantize.py.
264
+
265
+ * Backends
266
+
267
+ For now, the only backend supported is Ultralytics.
268
+
269
+ ** Ultralytics
270
+
271
+ Any Ultralytics function (train, predict, val, etc.) will run
272
+ through SyNet with SyNet modules. The basic shell syntax is:
273
+
274
+ #+begin_src shell
275
+ synet ultralytics [ultralytics ARGS]...
276
+ #+end_src
277
+
278
+ This performs 3 SyNet-specific operations, then passes off
279
+ execution to the normal Ultralytics code entrypoint:
280
+ - Copy the model config from the synet zoo (synet/zoo/ultralytics) if necessary.
281
+ - Set the imgsz (image size) ultralytics parameter according to the
282
+ model specification.
283
+ - Apply patches to the Ultralytics modules where necessary to
284
+ enable proper SyNet model loading within Ultralytics.
285
+ If you need to use this backend through python (instead of a
286
+ shell), then the only necessary step is to apply the patches as in
287
+ the following snippet:
288
+
289
+ #+begin_src python
290
+ from synet.backends import get_backend
291
+ get_backend('ultralytics').patch()
292
+ #+end_src
293
+
294
+ After this point, you are free to use SyNet models and tflites
295
+ using the normal Ultralytics API, but do not try to use
296
+ Ultralytics' "export" functionality to deploy to Sabre. Use
297
+ SyNet's quantize instead. The resulting models will not be
298
+ properly optimized and are not expected to run on our chips.
299
+
300
+ We give some examples/explanations for basic Ultralytics usage
301
+ here, but for any further questions about Ultralytics, you should
302
+ consult the Ultralytics github page and documentation:
303
+ - [[https://github.com/ultralytics/ultralytics]]
304
+ - https://docs.ultralytics.com/
305
+
306
+ *** Train
307
+
308
+ The SyNet repository provides a thin wrapper around Ultralytics
309
+ training for simple training situations. The basic usage is
310
+
311
+ #+begin_src shell
312
+ synet ultralytics [OTHER ULTRALYTICS ARGS]
313
+ #+end_src
314
+
315
+ For instance, if you want to train a person detect model, you
316
+ can train a VGA (640x480) model for the sabre chip with.
317
+
318
+ #+begin_src shell
319
+ synet ultralytics train model=sabre-detect-vga.yaml data=coco.yaml
320
+ #+end_src
321
+
322
+ This will put all output at ./runs/train/exp. See "name",
323
+ "project" and "exists-ok" in the Ultralytics docs for changing
324
+ this. The above command also tries to download the coco dataset
325
+ to ../datasets.
326
+
327
+ For any further information, see the ultralytics documentation for
328
+ training: https://docs.ultralytics.com/modes/train
329
+
330
+ *** Validation
331
+
332
+ Validation will be performed during training, but only on the
333
+ validation set, and only with the floating point (non-quantized)
334
+ model. In order to use ultralytics to run validation on your
335
+ quantized (.tflite) model, you will need to specify the model, the
336
+ task, the dataset split, and the canvas size. Additionally, if
337
+ you want to use SyNet's advanced metrics tools, you should be sure
338
+ to cache the results of model evaluation by passing 'save_txt' and
339
+ 'save_conf' like so:
340
+
341
+ #+begin_src shell
342
+ synet ultralytics val model=runs/train/detect/weights/best.tflite split=val task=detect save_txt=True save_conf=True imgsz=640,480 data=coco.yaml
343
+ #+end_src
344
+
345
+ This should place the results of model evaluation in
346
+ runs/val/detect, which you can point to when calling "synet
347
+ metrcis" (see above). For more information, see the ultralytics
348
+ documentation for validation:
349
+ https://docs.ultralytics.com/modes/val
350
+
351
+ *** Predict (for demos)
352
+
353
+ You can use Ultralytics' Predict to infer the model on an input
354
+ and optionally generate visualizations. For example, you can see
355
+ the results of the model on your webcam stream with:
356
+
357
+ #+begin_src shell
358
+ synet ultralytics predict model=vga/detect/finetuned.tflite source=0 imgsz='[480,640]' show=True iou=.3 conf=.5
359
+ #+end_src
360
+
361
+ Breaking this apart: You are calling SyNet with the ultralytics
362
+ backend in predict mode. You are passing predict the path to your
363
+ model (tflite in this case), telling it to run from a webcam
364
+ (undocumented in Ultralytics, but this is source=0), setting the
365
+ image shape (ultralytics cannot infer image shape from tflite),
366
+ telling it to generate a graphical display, and specifying iou and
367
+ confidence thresholds. For more information, see the ultralytics
368
+ documentation: https://docs.ultralytics.com/modes/predict
369
+
370
+ * Contributing
371
+
372
+ ** Test Suite
373
+
374
+ Please run the test suite before pushing ANY changes upstream. To
375
+ do so, ensure that you have the development dependencies by
376
+ installing synet with the [dev] set of optional dependencies.
377
+
378
+ #+begin_src shell
379
+ pip install -e ...synet[dev]
380
+ #+end_src
381
+
382
+ Then run the following in the synet root folder (the directory
383
+ containing the "synet" folder):
384
+
385
+ #+begin_src shell
386
+ pytest -v
387
+ #+end_src
388
+
389
+ If you notice that a bug is present despite the tests passing,
390
+ please consider adding an appropriate test case in the 'tests'
391
+ folder: https://docs.pytest.org/en/latest/getting-started.html
392
+
393
+ ** Docstring Style
394
+
395
+ Docstrings conform to numpy, scipy, and scikits docstring
396
+ conventions: https://numpydoc.readthedocs.io/en/latest/format.html
397
+
398
+ ** Imports
399
+
400
+ Only quantize.py and tflite_utils.py should import TensorFlow at
401
+ the top of the file. Otherwise, TensorFlow modules should be
402
+ imported at the beginning of functions where they are used. This
403
+ ensures TensorFlow is only loaded when strictly necessary.
404
+
405
+ Only backends/ultralytics.py should directly import anything from
406
+ ultralytics, and backends.ultralytics should only be accessed by
407
+ obtaining the ultralytics backend from backends.get_backend().:w
408
+