Delve lets you monitor PyTorch model layer saturation during training

# Delve: Deep Live Visualization and Evaluation

Delve is a Python package for analyzing the inference dynamics of your model.

Use Delve if you need a lightweight PyTorch extension that:

• Gives you insight into the inference dynamics of your architecture
• Allows you to optimize and adjust neural networks models to your dataset without much trial and error
• Allows you to analyze the eigenspaces your data at different stages of inference
• Provides you basic tooling for experiment logging

## Motivation

Designing a deep neural network is a trial and error heavy process that mostly revolves around comparing performance metrics of different runs. One of the key issues with this development process is that the results of metrics not really propagate back easily to concrete design improvements. Delve provides you with spectral analysis tools that allow you to investigate the inference dynamic evolving in the model while training. This allows you to spot underutilized and unused layers. Mismatches between object size and neural architecture among other inefficiencies. These observations can be propagated back directly to design changes in the architecture even before the model has fully converged, allowing for a quicker and more guided design process.

## Installation

pip install delve


### Using Layer Saturation to improve model performance

The saturation metric is the core feature of delve. By default saturation is a value between 0 and 1.0 computed for any convolutional, lstm or dense layer in the network. The saturation describes the percentage of eigendirections required for explaining 99% of the variance. Simply speaking, it tells you how much your data is "filling up" the individual layers inside your model.

In the image below you can see how saturation portraits inefficiencies in your neural network. The depicted model is ResNet18 trained on 32 pixel images, which is way to small for a model with a receptive field exceeding 400 pixels in the final layers.

To visualize what this poorly chosen input resolution does to the inference, we trained logistic regressions on the output of every layer to solve the same task as the model. You can clearly see that only the first half of the model (at best) is improving the intermedia solutions of our logistic regression "probes". The layers following this are contributing nothing to the quality of the prediction! You also see that saturation is extremly low for this layers!

We call this a tail and it can be removed by either increasing the input resolution or (which is more economical) reducing the receptive field size to match the object size of your dataset.

We can do this by removing the first two downsampling layers, which quarters the growth of the receptive field of your network, which reduced not only the number of parameters but also makes more use of the available parameters, by making more layers contribute effectivly!

For more details check our publication on this topics

## Demo

import torch
from delve import SaturationTracker
from torch.cuda import is_available
from torch.nn import CrossEntropyLoss
from torchvision.datasets import CIFAR10
from torchvision.transforms import ToTensor, Compose
from torchvision.models.vgg import vgg16

# setup compute device
from tqdm import tqdm

if __name__ == "__main__":

device = "cuda:0" if is_available() else "cpu"

# Get some data
train_data = CIFAR10(root="./tmp", train=True,

shuffle=True, num_workers=6,
pin_memory=True)
shuffle=False, num_workers=6,
pin_memory=True)

# instantiate model
model = vgg16(num_classes=10).to(device)

# instantiate optimizer and loss
criterion = CrossEntropyLoss().to(device)

# initialize delve
tracker = SaturationTracker("my_experiment", save_to="plotcsv", modules=model, device=device)

# begin training
for epoch in range(10):
model.train()
images, labels = images.to(device), labels.to(device)
prediction = model(images)
with torch.cuda.amp.autocast():
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)

loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

total = 0
test_loss = 0
correct = 0
model.eval()
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)

total += labels.size(0)
correct += torch.sum((predicted == labels)).item()
test_loss += loss.item()

# add saturation to the mix

# close the tracker to finish training
tracker.close()


### Why this name, Delve?

delve (verb):

• reach inside a receptacle and search for something
• to carry on intensive and thorough research for data, information, or the like

## Project details

Uploaded source
Uploaded py2 py3