-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.py
42 lines (34 loc) · 1.17 KB
/
example.py
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
"""
Run the HMAX model on the example images.
Authors: Marijn van Vliet <[email protected]>
"""
import torch
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import pickle
import hmax
# Initialize the model with the universal patch set
print('Constructing model')
model = hmax.HMAX('./universal_patch_set.mat')
# A folder with example images
example_images = datasets.ImageFolder(
'./example_images/',
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Lambda(lambda x: x * 255),
])
)
# A dataloader that will run through all example images in one batch
dataloader = DataLoader(example_images, batch_size=10)
# Determine whether there is a compatible GPU available
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Run the model on the example images
print('Running model on', device)
model = model.to(device)
for X, y in dataloader:
s1, c1, s2, c2 = model.get_all_layers(X.to(device))
print('Saving output of all layers to: output.pkl')
with open('output.pkl', 'wb') as f:
pickle.dump(dict(s1=s1, c1=c1, s2=s2, c2=c2), f)
print('[done]')