File size: 2,769 Bytes
4ad6955
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23d3539
4ad6955
 
 
 
 
 
23d3539
4ad6955
 
 
 
 
23d3539
4ad6955
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## Objective

Inspired by Vishnu's excellent [Marvel Character classifier](https://notebookse.jarvislabs.ai/jY5fsv-S9jKoQQrgd1dsoJuCDt6pTg6ZjBpNK9afxLIGInQv4OlHVuTMHqOPh2LU/), this model is designed to thwart adversarial attacks by DC fans who can only dream of their characters being brought into the superior Marvel universe.

## Dataset

The dataset is composed of roughly 200 Marvel and 200 DC character images fetched from https://duckduckgo.com/ using the code in 
Jeremy Howard's ['Is it a bird? Creating a model from your own data'](https://www.kaggle.com/code/jhoward/is-it-a-bird-creating-a-model-from-your-own-data) Kaggle notebook.

## Training
With minimal modifications to our `DataBlock` and parameters passed to fastai's `vision_learner`, this model demonstrates how we can turn the multi-classification example
Jeremy presented in session 1 of the 2022 fastai course into a regression task.  These changes include:

1. Creating a labeling function that returns a float, 0.0 if it is a DC character and 1.0 if it is a Marvel character

```python
def is_marvel(img):
    return 1. if img.parent.name.lower().startswith("marvel") else 0.
```

2. Updating our `DataBlock`to use a `RegressionBlock` for our targets, and then assigning our labeling function above to the `get_y` argument.

```python
blocks=(ImageBlock, RegressionBlock)
```

3. Updating our call to `vision_learner` to use a regression friendly metric like RMSE, as well as specifying a `y_range` to constrain our predictions to the expected range of between 0 and 1.

```python
learn = vision_learner(dls, resnet18, metrics=rmse, y_range=(0, 1))
```

We'll start with a pre-trained `ResNet18` model which we'll train by calling `Learner.fine_tune()` and a learning rate of `1e-3`.  This will train the classification head of the model (e.g., it will update the completely randomized weights dedicated to predicting a value) for 1 epoch, and then train all the model weights for 3 epochs.  The final results of this process are included below.


Training the classification head only:
| epoch	| train_loss	| valid_loss	| rmse	| time  |
|-------|---------------|---------------|---------------|-------|
| 0	| 0.344505	| 0.324276	| 0.569452	| 00:02 | 


Training the entire model:
| epoch	| train_loss	| valid_loss	| rmse	| time  |
|-------|---------------|---------------|---------------|-------|
| 0	| 0.303813	| 0.292256	| 0.540607	| 00:02 | 
| 1	| 0.250147	| 0.272003	| 0.521539	| 00:01 | 
| 2	| 0.223758	| 0.270610	| 0.520202	| 00:01 | 

## Examples

Example Marvel and DC character images from the dataset above are provided as examples for this demo. Feel free to upload your own Marvel, DC, and/or whatever else images to see whether you got a hero worth rooting for (or one to avoid).