keisen / tf-keras-vis

Neural network visualization toolkit for tf.keras
https://keisen.github.io/tf-keras-vis-docs/
MIT License
313 stars 45 forks source link

re-executions of activation_maximization generate different results for same graph/weights #33

Open omerbrandis opened 3 years ago

omerbrandis commented 3 years ago

hello,

I've made the following minor change to the visualize dense layer example:

for CurrentAttempt in range(0,4):
        print('attempt number {}'.format(CurrentAttempt))
        activation = activation_maximization(loss,callbacks=[Print(interval=50)])
        image = activation[0].astype(np.uint8)

        subplot_args = { 'nrows': 1, 'ncols': 1, 'figsize': (3, 3),
                         'subplot_kw': {'xticks': [], 'yticks': []} }
        f, ax = plt.subplots(**subplot_args)
        ax.imshow(image)
        ax.set_title('Ouzel', fontsize=14)
        plt.tight_layout()
        plt.show()

the generated images for ouzel class are different for each execution. ( i can upload the images if that will help ).

I'm not sure if this is an error of some kind or expected behavior . can someone please shed some light on this ? Omer.

keisen commented 3 years ago

Hi, @omerbrandis .

the generated images for ouzel class are different for each execution.

It's exactly expected behavior. There are two reasons for that. First, in default, the initial values of the image is randomly generated. (If you don't want that, you can gave arbitrary values as initial values to tf-keras-vis using seed_input option.)

https://github.com/keisen/tf-keras-vis/blob/b420516ac29ddd9f3f5b964d53a6f3177241d014/tf_keras_vis/activation_maximization.py#L15

Second, in default, the image is randomly modified each step using InputModifiers.

https://github.com/keisen/tf-keras-vis/blob/b420516ac29ddd9f3f5b964d53a6f3177241d014/tf_keras_vis/activation_maximization.py#L17

For that reason, the image during activation maximization is changing as follow.