import os
os.chdir('../..')
from torchvision.datasets import MNIST
import torch
import torch.nn as nn
import torchvision.transforms as TF
from eXNN.InnerNeuralViz import api
train_ds = MNIST(root='./.cache', train=True, download=True, transform=TF.ToTensor())
test_ds = MNIST(root='./.cache', train=False, download=False, transform=TF.ToTensor())
train_dl = torch.utils.data.DataLoader(train_ds, batch_size=36, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_ds, batch_size=36, shuffle=False)
num_classes = 10
model = nn.Sequential(nn.Linear(28*28, 256),
nn.ReLU(), nn.Linear(256, 64),
nn.ReLU(), nn.Linear(64, num_classes))
layers = ['1', '3']
mode = 'pca'
data, labels = next(iter(train_dl))
data = torch.flatten(data, start_dim=1)
visualizations = api.VisualizeNetSpace(model, mode, data, layers, labels=labels)
visualizations['input']
visualizations['1']
visualizations['3']
data = api.get_random_input([36, 784])
visualizations = api.VisualizeNetSpace(model, mode, data, layers)
visualizations['input']
visualizations['1']
visualizations['3']
model = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.AdaptiveAvgPool2d(output_size=1),
nn.Flatten(),
nn.Linear(64, num_classes))
mode = 'umap'
data, labels = next(iter(train_dl))
visualizations = api.VisualizeNetSpace(model, mode, data, labels=labels)
visualizations['input']
visualizations['6']
data = api.get_random_input([36, 1, 28, 28])
visualizations = api.VisualizeNetSpace(model, mode, data)
visualizations['input']
visualizations['6']