def test_cppn_xor(network, conv_layer, channel): optvis = OptVis(network, xor_objective) for _ in range(3): # 3 attempts cppn = CPPNParam(64) optvis.vis(cppn, thresh=(50, ), lr=0.01, transform=False, verbose=False) if check_channel(torch.sigmoid(cppn())): return raise RuntimeError('CPPN failed xor after 3 tries.')
def test_neuron_fail(network, linear_layer, channel, n_classes): with pytest.raises(AssertionError): optvis = OptVis.from_layer(network, linear_layer, channel=channel, neuron=n_classes - 1) optvis.vis(verbose=False)
def test_cppn_loss(network, conv_layer, channel): cppn = CPPNParam(64) optvis = OptVis.from_layer(network, conv_layer, channel=channel) assert_loss_decreases(optvis, img_param=cppn, thresh=(50, ), lr=0.01, transform=False)
def test_shortcut(network, imsize, conv_layer, channel, n_steps): def was_called(h, m, i, o): raise Exception with Hook(network['layer4/0/conv1'], was_called): optvis = OptVis.from_layer(network, conv_layer, channel=channel, neuron=6, shortcut=True) assert_loss_decreases(optvis, thresh=n_steps)
def test_image_params(network, imsize, conv_layer, channel, n_steps): optvis = OptVis.from_layer(network, conv_layer, channel=channel) # with fft and decorrelate img_param = ImageParam(imsize, fft=True, decorrelate=True) assert_loss_decreases(optvis, img_param=img_param, thresh=n_steps) # with fft and without decorrelate img_param = ImageParam(imsize, fft=True, decorrelate=True) assert_loss_decreases(optvis, img_param=img_param, thresh=n_steps) # without fft and with decorrelate img_param = ImageParam(imsize, fft=True, decorrelate=True) assert_loss_decreases(optvis, img_param=img_param, thresh=n_steps) # without fft and decorrelate img_param = ImageParam(imsize, fft=False, decorrelate=False) assert_loss_decreases(optvis, img_param=img_param, thresh=n_steps)
def test_neuron(network, imsize, linear_layer, neuron, n_steps): optvis = OptVis.from_layer(network, linear_layer, neuron=neuron) assert_loss_decreases(optvis, thresh=n_steps)
def test_channel(network, imsize, conv_layer, channel, n_steps): optvis = OptVis.from_layer(network, conv_layer, channel=channel, neuron=6) assert_loss_decreases(optvis, thresh=n_steps)
def test_objective_operators(network, conv_layer, channels, n_steps): objective1 = LayerObjective(network, conv_layer, channel=channels[0]) objective2 = LayerObjective(network, conv_layer, channel=channels[1]) # Sum optvis = OptVis(network, objective1 + objective2) assert_loss_decreases(optvis, thresh=n_steps) # Sub optvis = OptVis(network, objective1 - 1) assert_loss_decreases(optvis, thresh=n_steps) optvis = OptVis(network, 1 - objective1) assert_loss_decreases(optvis, thresh=n_steps) # Neg optvis = OptVis(network, -objective1) assert_loss_decreases(optvis, thresh=n_steps) # Product with pytest.raises(TypeError): optvis = OptVis(network, objective1 * objective2) optvis.vis(thresh=(1, ), verbose=False) optvis = OptVis(network, 2 * objective1) assert_loss_decreases(optvis, thresh=n_steps) optvis = OptVis(network, objective1 * 2) assert_loss_decreases(optvis, thresh=n_steps)
def test_dream(network, imsize, conv_layer, n_steps): optvis = OptVis.from_dream(network, conv_layer) img_param = ImageFile("test/fixtures/dog.jpg", imsize) optvis.vis(img_param, thresh=n_steps, transform=False, verbose=False) assert_loss_decreases(optvis, train=False)