Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] initial pass at convolutional layer visualization #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gallettilance
Copy link
Owner

@gallettilance gallettilance commented May 25, 2021

    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
    X_train = X_train.astype('float32')
    X_train /= 255

    number_of_classes = 10
    Y_train = utils.to_categorical(y_train, number_of_classes)

    ACTIVATION = "relu"
    model = keras.models.Sequential()
    model.add(layers.Conv2D(32, 5, input_shape=(28, 28, 1), activation=ACTIVATION))
    model.add(layers.MaxPooling2D())
    model.add(layers.Conv2D(64, 5, activation=ACTIVATION))
    model.add(layers.MaxPooling2D())
    model.add(layers.Flatten())
    model.add(layers.Dense(100, activation=ACTIVATION))
    model.add(layers.Dense(10, activation="softmax"))
    model.compile(loss="categorical_crossentropy", metrics=['accuracy'])

    model.fit(X_train, Y_train, batch_size=100, epochs=5)

    dg = ConvGraph(model)
    X = [np.expand_dims(X_train[np.where(y_train == 0)[0][0]], axis=0)]
    dg.render(X, filename='test_input_mnist')

    X = []
    # get all the digit 0 examples
    for i in range(len(np.where(y_train == 0)[0])):
        X.append(np.expand_dims(X_train[np.where(y_train == 0)[0][i]], axis=0))
    dg.animate(X, filename='test_animate_mnist') 

Render of First Convolutional Layer for digit 0
test_animate_mnist_conv2d_2

Render of Second Convolutional Layer for digit 0
test_animate_mnist_conv2d_3

Animate of First Convolutional Layer of 50 digit 0 examples
test_animate_mnist_conv2d_2

Animate of Second Convolutional Layer of 50 digit 0 examples
test_animate_mnist_conv2d_3

Heatmap of First Convolutional Layer of all digit 0 examples
test_heatmap_mnist_0_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 1 examples
test_heatmap_mnist_1_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 2 examples
test_heatmap_mnist_2_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 3 examples
test_heatmap_mnist_3_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 4 examples
test_heatmap_mnist_4_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 5 examples
test_heatmap_mnist_5_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 6 examples
test_heatmap_mnist_6_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 7 examples
test_heatmap_mnist_7_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 8 examples
test_heatmap_mnist_8_conv2d_heatmap

Heatmap of First Convolutional Layer of all digit 9 examples
test_heatmap_mnist_9_conv2d_heatmap

@JamesKunstle
Copy link
Collaborator

Illustrations of layers are very interesting.
Consider changing output label to:
"Heatmap of First Convolutional Layer: all instances of '9' input"
for the sake of specificity. When I originally read it I misinterpreted it to mean "all" 9 classes, as opposed to a previous run with only 8 classes.

Also, in architecture for CNN you use 5x5 kernels. 3x3 kernels seems to be a more common choice but that seems up to interpretation.

For the sake of reproducibility on the part of the eventual end-user, I would consider passing input parameters via their local name: e.g.
layers.Conv2D( filters=32, kernel_size=5, strides=(1, 1), padding="same", ... )
This is easier to follow for new users if they're not familiar with the library API.

@JamesKunstle JamesKunstle self-requested a review June 14, 2021 22:52
Copy link
Collaborator

@JamesKunstle JamesKunstle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rendering process is difficult to follow so I would suggest adding compartmentalized comments about what is happening at each stage. Additionally, I would write a README of the process of why the activations are rendering the interesting gifs that they are (as I currently understand the gif) and how the gif rendering process works. It would be really useful as a teaching tool to see a scope of the "why" you're rendering each of these gifs and then show the "how."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants