added README.md
Browse files
README.md
CHANGED
@@ -2,8 +2,84 @@
|
|
2 |
tags:
|
3 |
- model_hub_mixin
|
4 |
- pytorch_model_hub_mixin
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
---
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
tags:
|
3 |
- model_hub_mixin
|
4 |
- pytorch_model_hub_mixin
|
5 |
+
datasets:
|
6 |
+
- zalando-datasets/fashion_mnist
|
7 |
+
metrics:
|
8 |
+
- accuracy
|
9 |
+
library_name: pytorch
|
10 |
+
pipeline_tag: image-classification
|
11 |
---
|
12 |
|
13 |
+
# mlp-fashion-mnist
|
14 |
+
|
15 |
+
A multi-layer perceptron (MLP) trained on the Fashion-MNIST dataset.
|
16 |
+
|
17 |
+
It is a PyTorch adaptation of the TensorFlow model in Chapter 10 of Aurelien Geron's book 'Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow'.
|
18 |
+
|
19 |
+
Code: https://github.com/sambitmukherjee/handson-ml3-pytorch/blob/main/chapter10/mlp_fashion_mnist.ipynb
|
20 |
+
|
21 |
+
Experiment tracking: https://wandb.ai/sadhaklal/mlp-fashion-mnist
|
22 |
+
|
23 |
+
## Usage
|
24 |
+
|
25 |
+
```
|
26 |
+
!pip install -q datasets
|
27 |
+
|
28 |
+
from datasets import load_dataset
|
29 |
+
|
30 |
+
fashion_mnist = load_dataset("zalando-datasets/fashion_mnist")
|
31 |
+
|
32 |
+
features = fashion_mnist['train'].features
|
33 |
+
id2label = {id: label for id, label in enumerate(features['label'].names)}
|
34 |
+
|
35 |
+
import torch
|
36 |
+
import torchvision.transforms.v2 as v2
|
37 |
+
|
38 |
+
tfms = v2.Compose([
|
39 |
+
v2.ToImage(),
|
40 |
+
v2.ToDtype(torch.float32, scale=True)
|
41 |
+
])
|
42 |
+
|
43 |
+
import torch.nn as nn
|
44 |
+
from huggingface_hub import PyTorchModelHubMixin
|
45 |
+
|
46 |
+
device = torch.device("cpu")
|
47 |
+
|
48 |
+
class MLP(nn.Module, PyTorchModelHubMixin):
|
49 |
+
def __init__(self):
|
50 |
+
super().__init__()
|
51 |
+
self.fc1 = nn.Linear(28 * 28, 300)
|
52 |
+
self.fc2 = nn.Linear(300, 100)
|
53 |
+
self.fc3 = nn.Linear(100, 10)
|
54 |
+
|
55 |
+
def forward(self, x):
|
56 |
+
x = x.view(-1, 28 * 28)
|
57 |
+
act = torch.relu(self.fc1(x))
|
58 |
+
act = torch.relu(self.fc2(act))
|
59 |
+
return self.fc3(act)
|
60 |
+
|
61 |
+
model = MLP.from_pretrained("sadhaklal/mlp-fashion-mnist")
|
62 |
+
model.to(device)
|
63 |
+
|
64 |
+
example = fashion_mnist['test'][0]
|
65 |
+
img = tfms(example['image'])
|
66 |
+
x_batch = img.unsqueeze(0)
|
67 |
+
|
68 |
+
model.eval()
|
69 |
+
x_batch = x_batch.to(device)
|
70 |
+
with torch.no_grad():
|
71 |
+
logits = model(x_batch)
|
72 |
+
proba = torch.softmax(logits, dim=-1)
|
73 |
+
|
74 |
+
confidence, pred = proba.max(dim=-1)
|
75 |
+
print(f"Predicted class: {pred[0].item()}")
|
76 |
+
print(f"Predicted confidence: {round(confidence[0].item(), 4)}")
|
77 |
+
```
|
78 |
+
|
79 |
+
## Metric
|
80 |
+
|
81 |
+
Accuracy on the test set: 0.8829
|
82 |
+
|
83 |
+
---
|
84 |
+
|
85 |
+
This model has been pushed to the Hub using the [PyTorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration.
|