统一设置ggplot2的绘图风格

library(ggplot2)
theme_set(theme_bw(base_family = "STKaiti"))

12.3 应用VGG16模型进行分类

library(keras)

## 使用VGG16识别图像
model <- application_vgg16(weights = "imagenet", include_top = TRUE)
## 读取数据并将图像处理位224*224大小进行预处理
img <- image_load("data/chap12/tiger.jpg", target_size = c(224,224))
imgx <- image_to_array(img)
imgx <- array_reshape(imgx, c(1, dim(imgx)))
imgx <- imagenet_preprocess_input(imgx)
## 使用VGG16预测类别
vgg16cla <- model %>% predict(imgx)
imagenet_decode_predictions(vgg16cla, top = 3)[[1]]
##   class_name class_description        score
## 1  n02129604             tiger 0.8723027110
## 2  n02123159         tiger_cat 0.1276341677
## 3  n02128925            jaguar 0.0000537035

12.3 从VGG16模型中获取特征

## 导入模型
model <- application_vgg16(weights = "imagenet", include_top = FALSE)
img <- image_load("data/chap12/tiger.jpg", target_size = c(224,224))
x <- image_to_array(img)
x <- array_reshape(x, c(1,dim(x)))
x <- imagenet_preprocess_input(x)
features <- model %>% predict(x)
dim(features)
## [1]   1   7   7 512
par(mfrow = c(20,20),mai=c(0.005,0.005,0.005,0.005))
## 可视化前400个特征映射
for(ii in 1:400){
  image(features[1,,,ii],xaxt= "n", yaxt= "n",main = "VGG16 features")
}