Example #1
0
def runDiversitywithTransforms(layerName,
                               layerNeuron,
                               transforms=None,
                               imageSize=256,
                               batch=4,
                               weight=1e2):
    '''
    Function to run Lucent neuron diversity optimisation for a given Layer and Neuron (Channel) in a PyTorch CNN.
    This function uses image augmentation transforms to help improve the clarity and resolution of the produced neuron maximisations.

    '''
    if transforms == None:
        transforms = [
            transform.pad(16),
            transform.jitter(8),
            transform.random_scale([n / 100. for n in range(80, 120)]),
            transform.random_rotate(
                list(range(-10, 10)) + list(range(-5, 5)) +
                10 * list(range(-2, 2))),
            transform.jitter(2),
        ]
    batch_param_f = lambda: param.image(imageSize, batch=batch)
    obj = objectives.channel(
        layerName, layerNeuron) - weight * objectives.diversity(layerName)
    _ = render.render_vis(model_,
                          obj,
                          batch_param_f,
                          transforms=transforms,
                          show_inline=True)
Example #2
0
def runDiversity(layerName, layerNeuron, imageSize=256, batch=4, weight=1e2):
    '''
    Function to run Lucent neuron diversity optimisation for a given Layer and Neuron (Channel) in a PyTorch CNN.

    '''
    batch_param_f = lambda: param.image(imageSize, batch=batch)
    obj = objectives.channel(
        layerName, layerNeuron) - weight * objectives.diversity(layerName)
    _ = render.render_vis(model_, obj, batch_param_f, show_inline=True)
Example #3
0
def visualize_diversity_fc(model, layer, filter, path, batch_size, device):

    model.to(device).eval()

    if not os.path.exists(f'{path}/'):
        os.makedirs(f'{path}/')

    batch_param_f = lambda: param.image(128, batch=batch_size)
    layer_name = f'fc_{layer}:{filter}'

    obj = objectives.channel(
        f"fc_{layer}", filter) - 1e2 * objectives.diversity(f"fc_{layer}")

    image_name = f"{path}/{layer_name}_diversity.jpg"

    _ = render.render_vis(model,
                          obj,
                          batch_param_f,
                          save_image=True,
                          image_name=image_name.replace(':', '_'),
                          show_image=False)
Example #4
0
def test_diversity(inceptionv1_model):
    objective = objectives.channel("mixed4a", 0) - 100 * objectives.diversity("mixed4a")
    assert_gradient_descent(objective, inceptionv1_model)