首页 文章

在预先训练的XCeption-architecture中使用keras在Windows下指定卷积基数时出错

提问于
浏览
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行,在call output = self.call(inputs,** kwargs)文件”D:\ Anaconda3 \ lib \ site-packages \ keras \ engine \ topology.py“,第2081行,调用output_tensors, = self.run_internal_graph(输入,掩码)文件”D:\ Anaconda3 \ l

Further background in case it matters: 我试图用Chollet的第5.3.1节的"Freature extraction with data augementation"小节和Allaire最优秀的书中的XCeption替换VGG16("Fast feature extraction without data augmentation"中的所有内容都适用于VGG16和XCeption) .

1 回答

  • 1

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

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

    这个想法是(copied from here,我希望语法没问题 . 任何具有更好R理解的人都可以修复此代码)

    首先通常定义xception模型:

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

    Try 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)
    

    Try 2:

    或者,如果无法按照其他代码中的方式定义 base_inputsbase_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)
    

相关问题