Last active
September 6, 2018 09:36
-
-
Save siero5335/6889f224015550960ca6a5cf278ea188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tensorflow backend | |
# original: https://github.com/jjallaire/deep-learning-with-r-notebooks/blob/master/notebooks/5.4-visualizing-what-convnets-learn.Rmd | |
library(keras) | |
model <- application_vgg16( | |
weights = "imagenet", | |
include_top = FALSE | |
) | |
layer_name <- "block3_conv1" | |
filter_index <- 1 | |
summary(model) | |
layer_output <- get_layer(model, layer_name)$output | |
loss <- k_mean(layer_output[,,,filter_index]) | |
grads <- k_gradients(loss, model$input)[[1]] | |
grads <- grads / (k_sqrt(k_mean(k_square(grads))) + 1e-5) | |
# PlaidML-Keras ver. | |
library(keras) | |
use_backend(backend = "plaidml") | |
model <- application_vgg16( | |
weights = "imagenet", | |
include_top = FALSE | |
) | |
layer_name <- "block3_conv1" | |
filter_index <- 1 | |
summary(model) | |
layer_output <- get_layer(model, layer_name)$output | |
loss <- k_mean(layer_output) | |
grads <- k_l2_normalize(k_gradients(loss, model$input)[[1]]) | |
# grads <- grads / (k_sqrt(k_mean(k_square(grads))) + 1e-5) cannot work | |
# generate_pattern() function is also changed as below | |
generate_pattern <- function(layer_name, filter_index, size = 150) { | |
# Build a loss function that maximizes the activation | |
# of the nth filter of the layer considered. | |
layer_output <- model$get_layer(layer_name)$output | |
loss <- k_mean(layer_output) | |
# Compute the gradient of the input picture wrt this loss | |
grads <- k_l2_normalize(k_gradients(loss, model$input)[[1]]) | |
# This function returns the loss and grads given the input picture | |
iterate <- k_function(list(model$input), list(loss, grads)) | |
# We start from a gray image with some noise | |
input_img_data <- | |
array(runif(size * size * 3), dim = c(1, size, size, 3)) * 20 + 128 | |
# Run gradient ascent for 40 steps | |
step <- 1 | |
for (i in 1:40) { | |
c(loss_value, grads_value) %<-% iterate(list(input_img_data)) | |
input_img_data <- input_img_data + (grads_value * step) | |
} | |
img <- input_img_data[1,,,] | |
deprocess_image(img) | |
} |
k_l2_normalize()でnormalizeとかどうだろうか。結果は変わらなかったが…。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.pngは出力されたがオリジナルと違って全部均一なノイズだからうまく言ってるのかは分からない。とりあえず走った。