File size: 2,160 Bytes
df87f01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c1cae
 
9adc936
d6c1cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9adc936
d6c1cae
df87f01
 
d6c1cae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
datasets:
- cifar10
metrics:
- accuracy
library_name: pytorch
pipeline_tag: image-classification
---

# mlp-cifar2-v2

Multi-layer perceptron (MLP) trained on CIFAR-2 (a subset of CIFAR-10 for classifying 'airplane' vs. 'bird').

`nn.BCEWithLogitsLoss` was used to train the model.

This model pertains to Exercise 2 of Chapter 7 of the book "Deep Learning with PyTorch" by Eli Stevens, Luca Antiga, and Thomas Viehmann.

Code: https://github.com/sambitmukherjee/dlwpt-exercises/blob/main/chapter_7/exercise_2.ipynb

Experiment tracking: https://wandb.ai/sadhaklal/mlp-cifar2-v2

## Usage

```
!pip install -q datasets

from datasets import load_dataset

cifar10 = load_dataset("cifar10")
label_map = {0: 0.0, 2: 1.0}
class_names = ['airplane', 'bird']
cifar2_train = [(example['img'], label_map[example['label']]) for example in cifar10['train'] if example['label'] in [0, 2]]
cifar2_val = [(example['img'], label_map[example['label']]) for example in cifar10['test'] if example['label'] in [0, 2]]

example = cifar2_val[0]
img, label = example

import torch
from torchvision.transforms import v2

val_tfms = v2.Compose([
    v2.ToImage(),
    v2.ToDtype(torch.float32, scale=True),
    v2.Normalize(mean=[0.4915, 0.4823, 0.4468], std=[0.2470, 0.2435, 0.2616])
])
img = val_tfms(img)
batch = img.reshape(-1).unsqueeze(0) # Flatten.

import torch.nn as nn
from huggingface_hub import PyTorchModelHubMixin

class MLPForCIFAR2(nn.Module, PyTorchModelHubMixin):
    def __init__(self):
        super().__init__()
        self.mlp = nn.Sequential(
            nn.Linear(3072, 64), # Hidden layer.
            nn.Tanh(),
            nn.Linear(64, 1) # Output layer.
        )

    def forward(self, x):
        return self.mlp(x)

model = MLPForCIFAR2.from_pretrained("sadhaklal/mlp-cifar2-v2")
model.eval()

import torch.nn.functional as F

with torch.no_grad():
    logits = model(batch)
    proba = F.sigmoid(logits.squeeze())
    pred = int(proba.item() > 0.5)

print(f"Predicted class: {class_names[pred]}")
print(f"Predicted class probabilities ('airplane' vs. 'bird'): {[proba.item(), 1 - proba.item()]}")
```

## Metric

Accuracy on `cifar2_val`: 0.829