-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apr19_21_homework.Rmd
303 lines (247 loc) · 8.91 KB
/
Apr19_21_homework.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
---
title: "041921-homework"
author: "Julin Maloof"
date: "4/17/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse) # metapackage of all tidyverse packages
library(keras)
use_condaenv("r-reticulate")
```
## 1) Repeat the activation visualization (5.4.1) but for a dog image. Compare to the cat activations. Any interesting differences, especially in the last layer?
Load our model
```{r}
model <- load_model_hdf5("cats_and_dogs_small_2.h5")
summary(model)
```
Get input image:
```{r}
img_path <- "cats_and_dogs_small/test/dogs/dog.1516.jpg"
img <- image_load(img_path, target_size = c(150, 150))
img_tensor <- image_to_array(img)
img_tensor <- array_reshape(img_tensor, c(1, 150, 150, 3))
img_tensor <- img_tensor / 255
dim(img_tensor)
```
take a look at the image
```{r}
plot(as.raster(img_tensor[1,,,]))
```
now create the model. Using `keras_model` instead of `keras_sequential_model` allows us to access multiple output layers
```{r}
layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)
activation_model <- keras_model(inputs = model$input, outputs = layer_outputs)
```
```{r}
activations <- activation_model %>% predict(img_tensor)
```
define plotting function
```{r}
plot_channel <- function(channel) {
rotate <- function(x) t(apply(x, 2, rev))
image(rotate(channel), axes = FALSE, asp = 1,
col = topo.colors(12))
}
```
```{r}
first_layer_activation <- activations[[1]]
dim(first_layer_activation)
plot_channel(first_layer_activation[1,,,2])
plot_channel(first_layer_activation[1,,,7])
```
plot them all
```{r}
image_size <- 58
images_per_row <- 16
for (i in 1:8) {
layer_activation <- activations[[i]]
layer_name <- model$layers[[i]]$name
n_features <- dim(layer_activation)[[4]]
n_cols <- n_features %/% images_per_row
#png(paste0("cat_activations_", i, "_", layer_name, ".png"),
# width = image_size * images_per_row,
# height = image_size * n_cols)
op <- par(mfrow = c(n_cols, images_per_row), mai = rep_len(0.02, 4))
for (col in 0:(n_cols-1)) {
for (row in 0:(images_per_row-1)) {
channel_image <- layer_activation[1,,,(col*images_per_row) + row + 1]
plot_channel(channel_image)
}
}
par(op)
#dev.off()
}
```
surprisingly, spot glance seems that many of the same final filters are activated by both.
## 2) Visualize the filters (using the methods from 5.4.2) for the cat/dog model
```{r}
model <- load_model_hdf5("cats_and_dogs_small_2.h5" )
model
```
```{r}
for(i in 1:4) pop_layer(model)
model
```
visualizing the filters
set up the loss function
```{r}
library(tensorflow)
tf$compat$v1$disable_eager_execution()
layer_name <- "conv2d_10"
filter_index <- 1
layer_output <- get_layer(model, layer_name)$output %>%
layer_reshape(input_shape = list(150,150,3), target_shape = list(148, 148, 32))
loss <- k_mean(layer_output[,,,filter_index]) # average output as a tensor
```
get the gradient associated with the above loss and normalize (RMS)
```{r}
grads <- k_gradients(loss, model$input)[[1]]
grads <- grads / (k_sqrt(k_mean(k_square(grads))) + 1e-5) # as a tensor
```
now we need to be able to calculate loss and gradient for a given input. We use iterate for this:
```{r}
iterate <- k_function(list(model$input), list(loss, grads))
c(loss_value, grads_value) %<-%
iterate(list(array(0, dim = c(1, 150, 150, 3))))
```
put it together into a loop
```{r}
input_img_data <-
array(runif(150 * 150 * 3), dim = c(1, 150, 150, 3)) * 20 + 128 # input random image, near grey
step <- 1
for (i in 1:40) {
c(loss_value, grads_value) %<-% iterate(list(input_img_data)) # calculate gradient and loss
cat("loss: ", loss_value, "\n")
cat("grads_value: ", grads_value[1,1:5,1,1], "\n")
input_img_data <- input_img_data + (grads_value * step) # update image
}
```
gradient ascent because we are increasing the loss?
post process the tensor so that we can dispaly it as an image:
```{r}
deprocess_image <- function(x) {
dms <- dim(x)
x <- x - mean(x)
x <- x / (sd(x) + 1e-5)
x <- x * 0.1
x <- x + 0.5
x <- pmax(0, pmin(x, 1))
array(x, dim = dms)
}
```
put it all together in a function
```{r}
generate_pattern <- function(layer_name, filter_index, size = 150) {
print(layer_name)
layer_output <- model$get_layer(layer_name)$output %>%
layer_reshape(input_shape=list(150,150,3),
target_shape = list(148,148,32))
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)
iterate <- k_function(list(model$input), list(loss, grads))
input_img_data <-
array(runif(size * size * 3), dim = c(1, size, size, 3)) * 20 + 128
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)
}
```
```{r}
library(grid)
grid.raster(generate_pattern("conv2d_10", 1))
```
```{r, eval=FALSE}
library(grid)
library(gridExtra)
dir.create("catdog_filters")
for (layer_name in c("conv2d_10", "conv2d_9",
"conv2d_8", "conv2d_7")) {
size <- 150
png(paste0("catdog_filters/", layer_name, ".png"),
width = 5 * size, height = 5 * size)
grobs <- list()
for (i in 0:4) {
for (j in 0:4) {
print(i+j*5+1)
pattern <- generate_pattern(layer_name, i + (j*5) + 1, size = size)
grob <- rasterGrob(pattern,
width = unit(0.9, "npc"),
height = unit(0.9, "npc"))
grobs[[length(grobs)+1]] <- grob
}
}
grid.arrange(grobs = grobs, ncol = 8)
dev.off()
}
```
## 3) Run the attached image through the cat/dog categorization. Does it categorize correctly? Make a heatmap showing which areas of the image contributed (5.4.3)
```{r}
model <- load_model_hdf5("cats_and_dogs_small_2.h5")
model
```
```{r}
img_path <- "_MG_2350.jpg"
img <- image_load(img_path, target_size = c(150, 150)) %>%
image_to_array() %>%
array_reshape(dim = c(1, 150, 150, 3)) %>%
imagenet_preprocess_input()
```
```{r}
preds <- model %>% predict(img)
preds
```
```{r}
summary(model)
```
```{r}
dog_output <- model$output
last_conv_layer <- model %>% get_layer("conv2d_7")
grads <- k_gradients(dog_output, last_conv_layer$output)[[1]]
pooled_grads <- k_mean(grads, axis = c(1, 2, 3))
iterate <- k_function(list(model$input),
list(pooled_grads, last_conv_layer$output[1,,,]))
c(pooled_grads_value, conv_layer_output_value) %<-% iterate(list(img))
for (i in 1:128) { # 128 channels
conv_layer_output_value[,,i] <-
conv_layer_output_value[,,i] * pooled_grads_value[[i]]
}
heatmap <- apply(conv_layer_output_value, c(1,2), mean)
```
```{r}
heatmap <- pmax(heatmap, 0)
heatmap <- heatmap / max(heatmap)
write_heatmap <- function(heatmap, filename, width = 150, height = 150,
bg = "white", col = terrain.colors(12)) {
png(filename, width = width, height = height, bg = bg)
op = par(mar = c(0,0,0,0))
on.exit({par(op); dev.off()}, add = TRUE)
rotate <- function(x) t(apply(x, 2, rev))
image(rotate(heatmap), axes = FALSE, asp = 1, col = col)
}
write_heatmap(heatmap, "dog_heatmap.png")
```
```{r}
library(magick)
library(viridis)
image <- image_read(img_path)
info <- image_info(image)
geometry <- sprintf("%dx%d!", info$width, info$height)
pal <- col2rgb(viridis(20), alpha = TRUE)
alpha <- floor(seq(0, 255, length = ncol(pal)))
pal_col <- rgb(t(pal), alpha = alpha, maxColorValue = 255)
write_heatmap(heatmap, "elephant_overlay.png",
width = 14, height = 14, bg = NA, col = pal_col)
image_read("elephant_overlay.png") %>%
image_resize(geometry, filter = "quadratic") %>%
image_composite(image, operator = "blend", compose_args = "20") %>%
plot()
```