0

在尝试使用在 ImageNet 上预训练的 XCeption 架构中的卷积基础时,我正在做一些非常明显的事情吗?这是我在问题末尾产生错误的代码:

require(keras)

conv_base1 <- application_xception(
weights = "imagenet",
include_top = FALSE,
pooling=FALSE,
input_shape = c(300, 300, 3)
)

model51 <- keras_model_sequential() %>%
conv_base1 %>%
layer_flatten() %>%
layer_dense(units = 256, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")

相比之下,下面使用 application_vgg16 的几乎相同的代码工作得很好:

require(keras)

conv_base2 <- application_vgg16(
weights = "imagenet",
include_top = FALSE,
pooling=FALSE,
input_shape = c(300, 300, 3)
)

model52 <- keras_model_sequential() %>%
conv_base2 %>%
layer_flatten() %>%
layer_dense(units = 2048, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")

我收到以下错误(使用 keras_2.1.5 R 包在 Windows 10 x86_64-w64-mingw32/x64(64 位)上使用 R 版本 3.4.0 (2017-04-21)):

py_call_impl(callable, dots$args, dots$keywords) 中的错误:ValueError: 变量 block1_conv1_bn_1/moving_mean/biased 已经存在,不允许。您的意思是在 VarScope 中设置 reuse=True 吗?最初定义于:

文件“D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py”,第 1269 行,在 init self._traceback = _extract_stack() 文件“D:\Anaconda3\lib\site-packages\tensorflow \python\framework\ops.py",第 2506 行,在 create_op original_op=self._default_original_op, op_def=op_def) 文件“D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py”,行767, 在 apply_op op_def=op_def)

详细回溯:文件“D:\Anaconda3\lib\site-packages\keras\models.py”,第 467 行,添加层(x)文件“D:\Anaconda3\lib\site-packages\keras\engine\topology .py”,第 617 行,调用 output = self.call(inputs, **kwargs) 文件“D:\Anaconda3\lib\site-packages\keras\engine\topology.py”,第 2081 行,调用 output_tensors, _, _ = self.run_internal_graph(inputs, mask) 文件“D:\Anaconda3\l

进一步的背景,以防万一:我在 Chollet 和 Allaire 最优秀的书的第 5.3.1 节的“使用数据增强的特征提取”小节中尝试用 XCeption 替换 VGG16 时遇到了这个问题(“无数据增强的快速特征提取”中的所有内容) " 适用于 VGG16 和 XCeption)。

4

1 回答 1

1

我不明白这个错误的来源,但我怀疑它与在另一个模型中使用模型有关(将基本模型添加到顺序模型中)。

我建议尝试使用功能 API 模型。但不幸的是,我不擅长用 R 来理解它的符号。

这个想法是(从这里复制,我希望语法没问题。任何对 R 有更好理解的人都可以修复此代码)

首先正常定义xception模型:

conv_base1 <- application_xception(
weights = "imagenet",
include_top = FALSE,
pooling=FALSE,
input_shape = c(300, 300, 3)
)

尝试1:

现在让我们获取该模型的输出张量并将其传递给其他层

#the inputs of the following layers is the output of the exception model     
    #this is where I can't handle with R, these two lines may be wrong
base_inputs <- conv_base1$input
base_outputs <- conv_base1$output

#the base_outputs are the input tensor to further layers
#predictions is the output tensor from those layers
predictions <- base_outputs %>%
    layer_flatten() %>% 
    layer_dense(units = 256, activation = "relu") %>% 
    layer_dense(units = 1, activation = 'sigmoid') 

# create and compile model - model starts at base_inputs and ends at predcitions
model <- keras_model(inputs = base_inputs, outputs = predictions)

尝试2:

或者,如果定义base_inputs并且base_outputs不可能按照其他代码中的方式定义:

inputs <- layer_input(shape = c(300,300,3))

# outputs compose input + layers, where conv_base1 should behave like a layer
predictions <- inputs %>%
    conv_base1 %>%
    layer_flatten() %>% 
    layer_dense(units = 256, activation = "relu") %>% 
    layer_dense(units = 1, activation = 'sigmoid') 

# create and compile model
model <- keras_model(inputs = inputs, outputs = predictions)
于 2018-04-24T13:33:10.440 回答