我正在尝试从预测图像中提取特征。
它基于JJ的article。 Allaire。

基本上,它的工作是使用经过训练的模型并选择
前K层,并确定在其中激活的图像区域
每层。

可以下载here(134Mb)我的模型,并且可以下载here测试猫的图像。

该模型如下所示:

> summary(model)
___________________________________________________________________________________________________________________________________________________________________________________________________
Layer (type)                                                                           Output Shape                                                                  Param #
===================================================================================================================================================================================================
vgg16 (Model)                                                                          (None, 4, 4, 512)                                                             14714688
___________________________________________________________________________________________________________________________________________________________________________________________________
flatten_1 (Flatten)                                                                    (None, 8192)                                                                  0
___________________________________________________________________________________________________________________________________________________________________________________________________
dense_1 (Dense)                                                                        (None, 256)                                                                   2097408
___________________________________________________________________________________________________________________________________________________________________________________________________
dense_2 (Dense)                                                                        (None, 1)                                                                     257
===================================================================================================================================================================================================
Total params: 16,812,353
Trainable params: 16,552,193
Non-trainable params: 260,160
___________________________________________________________________________________________________________________________________________________________________________________________________


这是我完整的代码:

library(keras)
model_file <- "data/kaggle_cats_dogs_small/model//model.hdf5"
model <- load_model_hdf5(model_file)
summary(model)

img_path <- "data/kaggle_cats_dogs_small/test_generic/cat.5009.jpg"
# We preprocess the image into a 4D tensor
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))
# Remember that the model was trained on inputs
# that were preprocessed in the following way:
img_tensor <- img_tensor / 255
dim(img_tensor)


# Display picture ---------------------------------------------------------

plot(as.raster(img_tensor[1,,,]))

# Extracting layers and activation ----------------------------------------
# Extracts the outputs of the top 8 layers:
layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)
# Creates a model that will return these outputs, given the model input:
activation_model <- keras_model(inputs = model$input, outputs = layer_outputs)


它在最后两行代码处中断:

> layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)
 Show Traceback

 Rerun with Debug
 Error in py_get_attr_impl(x, name, silent) :
  AttributeError: Layer vgg16 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead. > # Creates a model that will return these outputs, given the model input:
> activation_model <- keras_model(inputs = model$input, outputs = layer_outputs)
 Show Traceback

 Rerun with Debug
 Error in py_call_impl(callable, dots$args, dots$keywords) :
  RuntimeError: Graph disconnected: cannot obtain value for tensor Tensor("input_1_4:0", shape=(?, 150, 150, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []


什么是正确的方法?

最佳答案

当出现此消息(多个入站节点)时,表示该模型与原始输入以外的其他输入一起使用。 (因此,尽管您仅使用一种可能的路径,但是该模型具有许多输入。即,它具有VGG原始输入以及用于创建堆叠模型的另一输入)。

要执行您想做的事情,必须在创建堆叠模型之前执行此操作。

一些伪代码(很抱歉,不熟悉R表示法):

VGGModel <- functionToCreateVGG
layer_outputs <- lapply(VGGModel$layers[1:8], function(layer) layer$output)
activation_model <- keras_model(inputs = VGGModel$input, outputs = layer_outputs)


在添加顶层之前执行此操作时,VGG模型还没有多个入站节点。

现在,您像以前一样将顶层堆叠到VGG模型上。

另一个选择是简单地为activation_model创建另一个VGG模型(以防您未训练VGG模型)。

关于python - 如何在Keras R中的VGG16中提取top-K层,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48634151/

10-12 21:12