Skip to content

Commit ff43689

Browse files
author
Robert Muchsel
authored
README: Clarify models and dataset structures, model instantiation and parameters (#149)
1 parent 370f5e4 commit ff43689

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

README.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MAX78000 Model Training and Synthesis
22

3-
_July 14, 2021_
3+
_July 16, 2021_
44

55
The Maxim Integrated AI project is comprised of five repositories:
66

@@ -1231,7 +1231,17 @@ For both approaches, the `quantize.py` software quantizes an existing PyTorch ch
12311231
12321232
#### Quantization-Aware Training (QAT)
12331233
1234-
Quantization-aware training is the better performing approach. It is enabled by default. QAT learns additional parameters during training that help with quantization (see [Weights: Quantization-Aware Training (QAT)](#Weights: Quantization-Aware Training (QAT)). No additional arguments are needed for `quantize.py`.
1234+
Quantization-aware training is the better performing approach. It is enabled by default. QAT learns additional parameters during training that help with quantization (see [Weights: Quantization-Aware Training (QAT)](#Weights: Quantization-Aware Training (QAT)). No additional arguments (other than input, output, and device) are needed for `quantize.py`.
1235+
1236+
The input checkpoint to `quantize.py` is either `qat_best.pth.tar`, the best QAT epoch’s checkpoint, or `qat_checkpoint.pth.tar`, the final QAT epoch’s checkpoint.
1237+
1238+
Example:
1239+
1240+
```shell
1241+
(ai8x-synthesis) $ ./quantize.py proj/qat_best.pth.tar proj/proj_q8.pth.tar --device MAX78000
1242+
```
1243+
1244+
12351245
12361246
#### Post-Training Quantization
12371247
@@ -1241,6 +1251,17 @@ While several approaches for clipping are implemented in `quantize.py`, clipping
12411251
12421252
Note that the “optimum” scale factor for simple clipping is highly dependent on the model and weight data. For the MNIST example, a `--scale 0.85` works well. For the CIFAR-100 example on the other hand, Top-1 performance is 30 points better with `--scale 1.0`.
12431253
1254+
The input checkpoint to `quantize.py` for post-training quantization is typically `best.pth.tar`, the best epoch’s checkpoint, or `checkpoint.pth.tar`, the final epoch’s checkpoint.
1255+
1256+
Example:
1257+
1258+
```shell
1259+
(ai8x-synthesis) $ ./quantize.py proj2/best.pth.tar proj2/proj2_q8.pth.tar \
1260+
--device MAX78000 --scale 0.85 --clip-method SCALE
1261+
```
1262+
1263+
1264+
12441265
#### Command Line Arguments
12451266
12461267
The `quantize.py` software has the following important command line arguments:
@@ -1259,7 +1280,7 @@ The `quantize.py` software has the following important command line arguments:
12591280
12601281
*Note: The syntax for the optional YAML file is described below. The same file can be used for both `quantize.py` and `ai8xize.py`.*
12611282
1262-
`quantize.py` does not need access to the dataset.
1283+
*Note:* `quantize.py` does <u>not</u> need access to the dataset.
12631284
12641285
#### Example and Evaluation
12651286
@@ -1295,18 +1316,47 @@ In all cases, ensure that the quantizer writes out a checkpoint file that the Ne
12951316
12961317
The following step is needed to add new network models:
12971318
1298-
Implement a new network model based on the constraints described earlier, see [Custom nn.Modules](#custom-nnmodules) (and `models/ai85net.py` for an example).
1319+
Implement a new network model based on the constraints described earlier, see [Custom nn.Modules](#custom-nnmodules) (and `models/ai85net.py` for an example).
1320+
1321+
***Note:*** *When re-using existing models, please note that some of the example models are designed to be used with [Neural Architecture Search (NAS)](#Neural Architecture Search (NAS)). These models will typically not perform well, or not fit into hardware without the NAS steps. These models have “nas” as part of their name.*
1322+
1323+
##### Model Instantiation and Initialization
1324+
1325+
To support *evaluation* of the quantized model using PyTorch, the model must be instantiated and initialized using all parameters supplied by `train.py`, and the parameters must be passed to the individual *nn.Modules*.
1326+
1327+
Example:
1328+
1329+
```python
1330+
class NewModel(nn.Module):
1331+
def __init__(self, num_classes=10, num_channels=3, dimensions=(64, 64), bias=False, **kwargs):
1332+
super().__init__()
1333+
self.conv1 = ai8x.FusedConv2dReLU(..., bias=bias, **kwargs)
1334+
...
1335+
1336+
def forward(self, x):
1337+
...
1338+
1339+
def newmodel(pretrained=False, **kwargs):
1340+
...
1341+
return NewModel(**kwargs)
1342+
```
1343+
1344+
Note the `__init(...)__` function signature, the extra arguments to `ai8x.FusedConv2dReLU(...)` and the `NewModel(**kwargs)` instantiation.
12991345
13001346
##### `models` Data Structure
13011347
13021348
The file must include the `models` data structure that describes the model. `models` can list multiple models in the same file.
13031349
13041350
For each model, three fields are required in the data structure:
13051351
1306-
* The `name` field assigns a name to the model for discovery by `train.py`, for example “`resnet5`”.
1352+
* The `name` field assigns a name to the model for discovery by `train.py`, for example “`resnet5`”. *Note: The `name` must be unique.*
13071353
* The `min_input` field describes the minimum width for 2D models, it is typically `1` *(when the input `W` dimension is smaller than `min_input`, it is padded to `min_input`)*.
13081354
* The `dim` field is either `1` (the model handles 1D inputs) or `2` (the model handles 2D inputs).
13091355
1356+
##### Model File Location
1357+
1358+
Place the new model file (with its unique model name as specified by `name` in the data structure described above) into the `models` folder. `train.py` will now be able to discover and use the new model by specifying `--model modelname`.
1359+
13101360
#### Data Loader
13111361
13121362
The following steps are needed for new data formats and datasets:
@@ -1333,10 +1383,9 @@ On the other hand, a different sensor may produce unsigned data values in the fu
13331383
13341384
Add the new data loader to a new file in the `datasets` directory (for example `datasets/mnist.py`). The file must include the `datasets` data structure that describes the dataset and points to the new loader. `datasets` can list multiple datasets in the same file.
13351385
1336-
* The `input` field describes the dimensionality of the data, and the first dimension is passed as `num_channels` to the model, whereas the remaining dimensions are passed as `dimension`. For example, `'input': (1, 28, 28)` will be passed to the model as `num_channels=1` and `dimensions=(28,28)`.
1337-
1386+
* The `name` field assigns a name to the dataset for discovery by `train.py`, for example “`MNIST`”. *Note: The `name` must be unique.*
1387+
* The `input` field describes the dimensionality of the data, and the first dimension is passed as `num_channels` to the model, whereas the remaining dimensions are passed as `dimension`. For example, `'input': (1, 28, 28)` will be passed to the model as `num_channels=1` and `dimensions=(28, 28)`. One-dimensional input uses a single “dimension”, for example `'input': (2, 512)` will be passed to the model as `num_channels=2` and `dimensions=(512, )`.
13381388
* The optional `regression` field in the structure can be set to `True` to automatically select the `--regression` command line argument. `regression` defaults to `False`.
1339-
13401389
* The optional `visualize` field can point to a custom visualization function used when creating `--embedding`. The input to the function (format NCHW for 2D data, or NCL for 1D data) is a batch of data (with N ≤ 100). The default handles square RGB or monochrome images. For any other data, a custom function must be supplied.
13411390
13421391
#### Training and Verification Data

README.pdf

30.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)