|
1 | 1 | # EdgeML FastCells on a sample public dataset
|
2 | 2 |
|
3 |
| -This directory includes example notebook and general execution script of |
4 |
| -FastCells (FastRNN & FastGRNN) developed as part of EdgeML along with modified |
| 3 | +This directory includes example notebooks and scripts of |
| 4 | +FastCells (FastRNN & FastGRNN) along with modified |
5 | 5 | UGRNN, GRU and LSTM to support the LSQ training routine.
|
6 |
| -Also, we include a sample cleanup and use-case on the USPS10 public dataset. |
7 |
| - |
8 |
| -`edgeml_pytorch.graph.rnn` implements the custom RNN cells of **FastRNN** ([`FastRNNCell`](../../pytorch_edgeml/graph/rnn.py#L226)) and **FastGRNN** ([`FastGRNNCell`](../../pytorch_edgeml/graph/rnn.py#L80)) with |
9 |
| -multiple additional features like Low-Rank parameterisation, custom |
10 |
| -non-linearities etc., Similar to Bonsai and ProtoNN, the three-phase training |
11 |
| -routine for FastRNN and FastGRNN is decoupled from the custom cells to |
12 |
| -facilitate a plug and play behaviour of the custom RNN cells in other |
13 |
| -architectures (NMT, Encoder-Decoder etc.,) in place of the inbuilt `RNNCell`, `GRUCell`, `BasicLSTMCell` etc., |
14 |
| -`edgeml_pytorch.graph.rnn` also contains modified RNN cells of **UGRNN** ([`UGRNNLRCell`](../../pytorch_edgeml/graph/rnn.py#L742)), |
15 |
| -**GRU** ([`GRULRCell`](../../edgeml/graph/rnn.py#L565)) and **LSTM** ([`LSTMLRCell`](../../pytorch_edgeml/graph/rnn.py#L369)). These cells also can be substituted for FastCells where ever feasible. |
16 |
| - |
17 |
| -`edgeml_pytorch.graph.rnn` also contains fully wrapped RNNs which are equivalent to `nn.LSTM` and `nn.GRU`. Implemented cells: |
18 |
| -**FastRNN** ([`FastRNN`](../../pytorch_edgeml/graph/rnn.py#L968)), **FastGRNN** ([`FastGRNN`](../../pytorch_edgeml/graph/rnn.py#L993)), **UGRNN** ([`UGRNN`](../../edgeml_pytorch/graph/rnn.py#L945)), **GRU** ([`GRU`](../../edgeml/graph/rnn.py#L922)) and **LSTM** ([`LSTM`](../../pytorch_edgeml/graph/rnn.py#L899)). |
19 |
| - |
20 |
| -Note that all the cells and wrappers (when used independently from `fastcell_example.py` or `edgeml_pytorch.trainer.fastTrainer`) take in data in a batch first format ie., [batchSize, timeSteps, inputDims] by default but it can also support [timeSteps, batchSize, inputDims] format by setting `batch_first` argument to False when used. `fast_example.py` automatically takes care it while assuming the standard format between tf, c++ and pytorch. |
| 6 | +There is also a sample cleanup and train/test script for the USPS10 public dataset. |
| 7 | + |
| 8 | +[`edgeml_pytorch.graph.rnn`](../../../pytorch/pytorch_edgeml/graph/rnn.py) |
| 9 | +provides two RNN cells **FastRNNCell** and **FastGRNNCell** with additional |
| 10 | +features like low-rank parameterisation and custom non-linearities. Akin to |
| 11 | +Bonsai and ProtoNN, the three-phase training routine for FastRNN and FastGRNN |
| 12 | +is decoupled from the custom cells to facilitate a plug and play behaviour of |
| 13 | +the custom RNN cells in other architectures (NMT, Encoder-Decoder etc.). |
| 14 | +Additionally, numerically equivalent CUDA-based implementations FastRNNCuda |
| 15 | +and FastGRNNCuda are provided for faster training. |
| 16 | +`edgeml_pytorch.graph.rnn` also contains modified RNN cells of **UGRNNCell**, |
| 17 | +**GRUCell**, and **LSTMCell**, which can be substituted for Fast(G)RNN, |
| 18 | +as well as untrolled RNNs which are equivalent to `nn.LSTM` and `nn.GRU`. |
| 19 | + |
| 20 | +Note that all the cells and wrappers, when used independently from `fastcell_example.py` |
| 21 | +or `edgeml_pytorch.trainer.fastTrainer`, take in data in a batch first format, i.e., |
| 22 | +[batchSize, timeSteps, inputDims] by default, but can also support [timeSteps, |
| 23 | +batchSize, inputDims] format if `batch_first` argument is set to False. |
| 24 | +`fast_example.py` automatically adjusts to the correct format across tf, c++ and pytorch. |
21 | 25 |
|
22 | 26 | For training FastCells, `edgeml_pytorch.trainer.fastTrainer` implements the three-phase
|
23 |
| -FastCell training routine in PyTorch. A simple example, |
24 |
| -`examples/fastcell_example.py` is provided to illustrate its usage. |
25 |
| - |
26 |
| -Note that `fastcell_example.py` assumes that data is in a specific format. It |
27 |
| -is assumed that train and test data is contained in two files, `train.npy` and |
28 |
| -`test.npy`. Each containing a 2D numpy array of dimension `[numberOfExamples, |
| 27 | +FastCell training routine in PyTorch. A simple example `fastcell_example.py` is provided |
| 28 | +to illustrate its usage. Note that `fastcell_example.py` assumes that data is in a specific format. |
| 29 | +It is assumed that train and test data is contained in two files, `train.npy` and |
| 30 | +`test.npy`, each containing a 2D numpy array of dimension `[numberOfExamples, |
29 | 31 | numberOfFeatures]`. numberOfFeatures is `timesteps x inputDims`, flattened
|
30 |
| -across timestep dimension. So the input of 1st timestep followed by second and |
31 |
| -so on. For an N-Class problem, we assume the labels are integers from 0 |
| 32 | +across timestep dimension with the input of the first time step followed by the second |
| 33 | +and so on. For an N-Class problem, we assume the labels are integers from 0 |
32 | 34 | through N-1. Lastly, the training data, `train.npy`, is assumed to well shuffled
|
33 | 35 | as the training routine doesn't shuffle internally.
|
34 | 36 |
|
35 | 37 | **Tested With:** PyTorch = 1.1 with Python 3.6
|
36 | 38 |
|
37 | 39 | ## Download and clean up sample dataset
|
38 | 40 |
|
39 |
| -We will be testing out the validation of the code by using the USPS dataset. |
40 |
| -The download and cleanup of the dataset to match the above-mentioned format is |
41 |
| -done by the script [fetch_usps.py](fetch_usps.py) and |
| 41 | +To validate the code with USPS dataset, first download and format the dataset to match |
| 42 | +the required format using the script [fetch_usps.py](fetch_usps.py) and |
42 | 43 | [process_usps.py](process_usps.py)
|
43 | 44 |
|
44 | 45 | ```
|
45 | 46 | python fetch_usps.py
|
46 | 47 | python process_usps.py
|
47 | 48 | ```
|
48 | 49 |
|
| 50 | +Note: Even though usps10 is not a time-series dataset, it can be regarding as a time-series |
| 51 | +dataset where time step sees a new row. So the number of timesteps = 16 and inputDims = 16. |
49 | 52 |
|
50 | 53 | ## Sample command for FastCells on USPS10
|
51 |
| -The following sample run on usps10 should validate your library: |
52 |
| - |
53 |
| -Note: Even though usps10 is not a time-series dataset, it can be assumed as, a time-series where each row is coming in at one single time. |
54 |
| -So the number of timesteps = 16 and inputDims = 16 |
| 54 | +The following is a sample run on usps10 : |
55 | 55 |
|
56 | 56 | ```bash
|
57 | 57 | python fastcell_example.py -dir usps10/ -id 16 -hd 32
|
58 | 58 | ```
|
59 |
| -This command should give you a final output screen which reads roughly similar to (might not be exact numbers due to various version mismatches): |
| 59 | +This command should give you a final output that reads roughly similar to |
| 60 | +(might not be exact numbers due to various version mismatches): |
60 | 61 |
|
61 | 62 | ```
|
62 | 63 | Maximum Test accuracy at compressed model size(including early stopping): 0.9407075 at Epoch: 262
|
63 | 64 | Final Test Accuracy: 0.93721974
|
64 | 65 |
|
65 | 66 | Non-Zeros: 1932 Model Size: 7.546875 KB hasSparse: False
|
66 | 67 | ```
|
67 |
| -`usps10/` directory will now have a consolidated results file called `FastRNNResults.txt` or `FastGRNNResults.txt` depending on the choice of the RNN cell. |
68 |
| -A directory `FastRNNResults` or `FastGRNNResults` with the corresponding models with each run of the code on the `usps10` dataset. |
| 68 | +`usps10/` directory will now have a consolidated results file called `FastRNNResults.txt` or |
| 69 | +`FastGRNNResults.txt` depending on the choice of the RNN cell. A directory `FastRNNResults` or |
| 70 | +`FastGRNNResults` with the corresponding models with each run of the code on the `usps10` dataset. |
69 | 71 |
|
70 |
| -Note that the scalars like `alpha`, `beta`, `zeta` and `nu` are all before the application of the sigmoid function over them. |
| 72 | +Note that the scalars like `alpha`, `beta`, `zeta` and `nu` correspond to the values before |
| 73 | +the application of the sigmoid function. |
71 | 74 |
|
72 | 75 | ## Byte Quantization(Q) for model compression
|
73 |
| -If you wish to quantize the generated model to use byte quantized integers use `quantizeFastModels.py`. Usage Instructions: |
| 76 | +If you wish to quantize the generated model, use `quantizeFastModels.py`. Usage Instructions: |
74 | 77 |
|
75 | 78 | ```
|
76 | 79 | python quantizeFastModels.py -h
|
77 | 80 | ```
|
78 | 81 |
|
79 |
| -This will generate quantized models with a suffix of `q` before every param stored in a new directory `QuantizedFastModel` inside the model directory. |
80 |
| -One can use this model further on edge devices. |
| 82 | +This will generate quantized models with a suffix of `q` before every param stored in a |
| 83 | +new directory `QuantizedFastModel` inside the model directory. |
81 | 84 |
|
82 |
| -Note that the scalars like `qalpha`, `qbeta`, `qzeta` and `qnu` are all after the application of the sigmoid function over them and quantization, they can be directly plugged into the inference pipleines. |
| 85 | +Note that the scalars like `qalpha`, `qbeta`, `qzeta` and `qnu` correspond to values |
| 86 | +after the application of the sigmoid function over them post quantization; |
| 87 | +they can be directly plugged into the inference pipleines. |
83 | 88 |
|
84 | 89 | Copyright (c) Microsoft Corporation. All rights reserved.
|
85 |
| - |
86 | 90 | Licensed under the MIT license.
|
0 commit comments