tsilva commited on
Commit
e73e9bc
·
verified ·
1 Parent(s): 3a8a5c9

Use ONNX-first model card structure

Browse files
Files changed (1) hide show
  1. README.md +115 -40
README.md CHANGED
@@ -1,77 +1,152 @@
1
  ---
2
- library_name: pytorch
 
 
3
  tags:
4
- - image-classification
5
- - mnist
6
- - pytorch
7
- - onnx
8
- - multilayer-perceptron
 
 
9
  datasets:
10
- - mnist
11
  metrics:
12
- - accuracy
13
- pipeline_tag: image-classification
14
  ---
15
 
16
  # MNIST MLP Classifier
17
 
18
- This is a fully connected MNIST digit classifier trained as part of the
19
- [`dlab`](https://github.com/tsilva/dlab) deep-learning experimentation roadmap.
 
20
 
21
- The model is intentionally an MLP rather than a CNN. It is useful as a strong
22
- baseline for studying optimization, regularization, augmentation, seed variance,
23
- and the ceiling of non-convolutional models on MNIST.
24
 
25
  ## Results
26
 
27
  10-seed confirmation sweep:
28
 
29
- | metric | result |
30
- | --- | ---: |
31
  | validation accuracy | 99.3600% ± 0.0817 pp |
32
  | validation loss | 0.15172 ± 0.00235 |
33
  | test accuracy | 99.4470% ± 0.0195 pp |
34
  | test loss | 0.14746 ± 0.00034 |
35
  | test errors | 55.3 ± 1.95 / 10000 |
36
 
37
- ## Architecture
 
 
38
 
39
  - Dataset: MNIST
40
- - Model: MLP
41
- - Hidden width: 1024
42
- - Hidden layers: 3
43
  - Activation: ReLU
44
  - Batch normalization: enabled
45
- - Dropout: 0.2
46
  - Optimizer: Adam
47
- - Learning rate: 0.001
 
48
  - Scheduler: OneCycleLR
49
- - Weight decay: 0.0001
50
- - Label smoothing: 0.02
51
  - Weight averaging: EMA
52
- - Train augmentation: random affine, 10 degrees, translate 0.05, scale 0.9-1.1
 
 
 
53
 
54
- ## Files
55
 
56
- - `model.ckpt`: PyTorch Lightning checkpoint from the best run.
57
- - `model.onnx`: ONNX export of the EMA/current model state used for evaluation.
58
- - `config.yaml`: resolved Hydra training config.
59
- - `metrics.csv`: training metrics from the run.
60
- - `metadata.json`: compact metadata for inference and provenance.
 
 
 
 
61
 
62
- ## Preprocessing
 
 
 
 
63
 
64
- Inputs should be MNIST grayscale images converted to tensors and normalized with:
 
 
 
 
 
 
 
 
65
 
66
  ```python
67
- mean = [0.1307]
68
- std = [0.3081]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  ```
70
 
71
- The ONNX model expects float tensors shaped `[batch, 1, 28, 28]` under the input
72
- name `images`, and returns class logits under the output name `logits`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- ## Provenance
75
 
76
- - W&B run: https://wandb.ai/tsilva/dlab/runs/gsuy1ifx
77
- - W&B sweep: https://wandb.ai/tsilva/dlab/sweeps/xa56lubb
 
1
  ---
2
+ license: mit
3
+ library_name: onnx
4
+ pipeline_tag: image-classification
5
  tags:
6
+ - image-classification
7
+ - mnist
8
+ - multilayer-perceptron
9
+ - onnx
10
+ - onnxruntime
11
+ - pytorch
12
+ - dlab
13
  datasets:
14
+ - mnist
15
  metrics:
16
+ - accuracy
 
17
  ---
18
 
19
  # MNIST MLP Classifier
20
 
21
+ This repository contains a validation-selected MNIST MLP digit classifier trained with [dlab](https://github.com/tsilva/dlab).
22
+
23
+ ## Architecture
24
 
25
+ ![MNIST MLP architecture](assets/architecture.png)
 
 
26
 
27
  ## Results
28
 
29
  10-seed confirmation sweep:
30
 
31
+ | metric | value |
32
+ |---|---:|
33
  | validation accuracy | 99.3600% ± 0.0817 pp |
34
  | validation loss | 0.15172 ± 0.00235 |
35
  | test accuracy | 99.4470% ± 0.0195 pp |
36
  | test loss | 0.14746 ± 0.00034 |
37
  | test errors | 55.3 ± 1.95 / 10000 |
38
 
39
+ The ONNX model was exported from the best run checkpoint. Test metrics were produced after the recipe was selected and were logged in W&B sweep [`xa56lubb`](https://wandb.ai/tsilva/dlab/sweeps/xa56lubb).
40
+
41
+ ## Model Details
42
 
43
  - Dataset: MNIST
44
+ - Architecture: MLP
45
+ - Hidden width: `1024`
46
+ - Hidden layers: `3`
47
  - Activation: ReLU
48
  - Batch normalization: enabled
49
+ - Dropout: `0.2`
50
  - Optimizer: Adam
51
+ - Learning rate: `0.001`
52
+ - Weight decay: `0.0001`
53
  - Scheduler: OneCycleLR
54
+ - Label smoothing: `0.02`
 
55
  - Weight averaging: EMA
56
+ - Batch size: `512`
57
+ - Training augmentation: random affine rotation/translation/scale
58
+ - Source W&B run: [`gsuy1ifx`](https://wandb.ai/tsilva/dlab/runs/gsuy1ifx)
59
+ - Source W&B sweep: [`xa56lubb`](https://wandb.ai/tsilva/dlab/sweeps/xa56lubb)
60
 
61
+ ## Input / Output
62
 
63
+ Use `model.onnx` for code-independent inference.
64
+
65
+ - Input name: `images`
66
+ - Input shape: `[batch, 1, 28, 28]`
67
+ - Input dtype: `float32`
68
+ - Output name: `logits`
69
+ - Output shape: `[batch, 10]`
70
+
71
+ Preprocessing:
72
 
73
+ - Convert image to grayscale.
74
+ - Resize to `28 x 28`.
75
+ - Scale pixel values to `[0, 1]`.
76
+ - Normalize with mean `0.1307` and standard deviation `0.3081`.
77
+ - Arrange the tensor as channels-first `[batch, 1, 28, 28]`.
78
 
79
+ ## Usage
80
+
81
+ Install the runtime dependencies:
82
+
83
+ ```bash
84
+ pip install huggingface_hub onnxruntime pillow numpy
85
+ ```
86
+
87
+ Run inference with the ONNX model:
88
 
89
  ```python
90
+ import numpy as np
91
+ import onnxruntime as ort
92
+ from huggingface_hub import hf_hub_download
93
+ from PIL import Image
94
+
95
+ LABELS = {
96
+ 0: "0",
97
+ 1: "1",
98
+ 2: "2",
99
+ 3: "3",
100
+ 4: "4",
101
+ 5: "5",
102
+ 6: "6",
103
+ 7: "7",
104
+ 8: "8",
105
+ 9: "9",
106
+ }
107
+
108
+ model_path = hf_hub_download(
109
+ repo_id="tsilva/mnist-classifier-mlp",
110
+ filename="model.onnx",
111
+ )
112
+
113
+ image = Image.open("example.png").convert("L").resize((28, 28))
114
+ x = np.asarray(image, dtype=np.float32) / 255.0
115
+ x = (x - 0.1307) / 0.3081
116
+ x = x[None, None, :, :].astype(np.float32)
117
+
118
+ session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
119
+ logits = session.run(["logits"], {"images": x})[0]
120
+ prediction = int(logits.argmax(axis=1)[0])
121
+
122
+ print(prediction, LABELS[prediction])
123
  ```
124
 
125
+ ## Labels
126
+
127
+ MNIST labels:
128
+
129
+ | id | label |
130
+ |---:|---|
131
+ | 0 | 0 |
132
+ | 1 | 1 |
133
+ | 2 | 2 |
134
+ | 3 | 3 |
135
+ | 4 | 4 |
136
+ | 5 | 5 |
137
+ | 6 | 6 |
138
+ | 7 | 7 |
139
+ | 8 | 8 |
140
+ | 9 | 9 |
141
+
142
+ ## Files
143
+
144
+ - `model.onnx`: ONNX export of the validation-selected checkpoint. Prefer this file for portable inference.
145
+ - `model.ckpt`: PyTorch Lightning checkpoint for the same model. This is code-dependent and mainly useful for PyTorch-based inspection or continued experimentation.
146
+ - `config.yaml`: resolved Hydra training config.
147
+ - `metrics.csv`: training metrics from the uploaded checkpoint run.
148
+ - `metadata.json`: compact metadata for inference and provenance.
149
 
150
+ ## Limitations
151
 
152
+ This MLP does not use convolutional inductive bias. It performs strongly on MNIST, but remaining errors are mostly concentrated in ambiguous or unusually written digits.