# Model analysis

## Model analysis

ML Model Analysis

### Model Investigation <a href="#model-investigation" id="model-investigation"></a>

#### Using Keras <a href="#using-keras" id="using-keras"></a>

```shellscript
from tensorflow import keras
from keras.models import load_model

model = load_model("example.h5")

# Summarization
print(model.summary())

# Configuration
print(model.get_config())

# List inputs
print(model.inputs)
# List outputs
print(model.outputs)
```

#### Using PyTorch <a href="#using-pytorch" id="using-pytorch"></a>

If we don’t have **`torchinfo`**, we need to install it at first.

```shellscript
pip install torchinfo
```

Here is the code for investigation.

```shellscript
import torch
from torchinfo import summary

model = torch.load("example.pt")
model.eval() # it's not required for investigation only but required when inferening

batch_size = 16
print(summary(model=model, input_size=(batch_size, 3, 16, 16)))

# Also simply show model's state dict
print(model.state_dict)
```

### Scan Model <a href="#scan-model" id="scan-model"></a>

#### ModelScan <a href="#modelscan" id="modelscan"></a>

[ModelScan](https://github.com/protectai/modelscan/tree/main) is a machine learning model scanner to protect againt Model Serialization Attacks.

```shellscript
# -p: Path to the file
modelscan -p example.h5
modelscan -p example.pt

# Scan all models in Hugging Face Repository
modelscan -hf owner/model-repository-name
```

### Model Fingerprinting <a href="#model-fingerprinting" id="model-fingerprinting"></a>

Reference: [Crucible](https://crucible.dreadnode.io/challenges/bear3)

When we don't know which model is being used, we can use various approaches to identify it.

#### For Image Recognition Models <a href="#for-image-recognition-models" id="for-image-recognition-models"></a>

* Input an image that is rotated.
* Input an image that is changed its color.
* Input random images.
* Replicate a model from ImageNet because many image recognition models are based on ImageNet.

### References <a href="#references" id="references"></a>

* [PyTorch](https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-entire-model)
