在 Windows 下的 R 中使用 keras 从预训练的 XCeption 架构指定卷积基时出错
Error specifying convolutional base from pre-trained XCeption-architecture with keras in R under Windows
当我尝试使用在 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")
我收到以下错误(在 Windows 10 x86_64-w64-mingw32/x64(64 位)上使用 R 版本 3.4.0 (2017-04-21) 使用 keras_2.1.5 R 包):
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Variable block1_conv1_bn_1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in init
self._traceback = _extract_stack()
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
Detailed traceback:
File "D:\Anaconda3\lib\site-packages\keras\models.py", line 467, in add
layer(x)
File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 617, in call
output = self.call(inputs, **kwargs)
File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2081, in call
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "D:\Anaconda3\l
更多背景信息以防万一: 我在第 5.3.1 节的 "Freature extraction with data augementation" 小节中尝试用 XCeption 替换 VGG16 时 运行 Chollet 和 Allaire 最优秀的书("Fast feature extraction without data augmentation" 中的所有内容都适用于 VGG16 和 XCeption)。
我不明白这个错误的来源,但我怀疑它与在另一个模型中使用一个模型有关(将基本模型添加到顺序模型中)。
我建议尝试使用功能性 API 模型。但不幸的是,我不擅长 R 来理解它的符号。
思路是(copied from here,希望语法没问题,对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)
当我尝试使用在 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")
我收到以下错误(在 Windows 10 x86_64-w64-mingw32/x64(64 位)上使用 R 版本 3.4.0 (2017-04-21) 使用 keras_2.1.5 R 包):
Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Variable block1_conv1_bn_1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in init self._traceback = _extract_stack() File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op original_op=self._default_original_op, op_def=op_def) File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op op_def=op_def)
Detailed traceback: File "D:\Anaconda3\lib\site-packages\keras\models.py", line 467, in add layer(x) File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 617, in call output = self.call(inputs, **kwargs) File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2081, in call output_tensors, _, _ = self.run_internal_graph(inputs, masks) File "D:\Anaconda3\l
更多背景信息以防万一: 我在第 5.3.1 节的 "Freature extraction with data augementation" 小节中尝试用 XCeption 替换 VGG16 时 运行 Chollet 和 Allaire 最优秀的书("Fast feature extraction without data augmentation" 中的所有内容都适用于 VGG16 和 XCeption)。
我不明白这个错误的来源,但我怀疑它与在另一个模型中使用一个模型有关(将基本模型添加到顺序模型中)。
我建议尝试使用功能性 API 模型。但不幸的是,我不擅长 R 来理解它的符号。
思路是(copied from here,希望语法没问题,对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)