Skip to content

Commit

Permalink
Add pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
greentfrapp committed May 14, 2020
1 parent 521ffd0 commit 4073241
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: python
python:
- "3.6"
script:
- pytest
46 changes: 46 additions & 0 deletions tests/optvis/test_objectives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import absolute_import, division, print_function

import pytest

import torch
import numpy as np
from lucent.optvis import objectives, param, render, transform
from lucent.modelzoo import InceptionV1


NUM_STEPS = 3


@pytest.fixture
def inceptionv1():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = InceptionV1().to(device)
model.eval()
return model


def assert_gradient_descent(objective, model):
params, image = param.image(224)
optimizer = torch.optim.Adam(params, lr=0.1)
T = render.hook_model(model, image)
objective_f = objectives.as_objective(objective)
model(image())
start_value = objective_f(T)
for _ in range(NUM_STEPS):
optimizer.zero_grad()
model(image())
loss = objective_f(T)
loss.backward()
optimizer.step()
end_value = objective_f(T)
assert start_value > end_value


def test_neuron(inceptionv1):
objective = objectives.neuron("mixed3a_1x1_pre_relu_conv", 0)
assert_gradient_descent(objective, inceptionv1)


def test_channel(inceptionv1):
objective = objectives.channel("mixed3a_1x1_pre_relu_conv", 0)
assert_gradient_descent(objective, inceptionv1)

0 comments on commit 4073241

Please sign in to comment.