Tensorflow 张量板图孤立层
同时构建包括迁移学习的模型(来自VGG-16)。 我遇到这种奇怪的行为。Tensorboard图显示的层不是新模型的一部分,而是旧模型的一部分,在分离点上方,它们只是悬挂在那里Tensorflow 张量板图孤立层,tensorflow,tensorboard,Tensorflow,Tensorboard,同时构建包括迁移学习的模型(来自VGG-16)。 我遇到这种奇怪的行为。Tensorboard图显示的层不是新模型的一部分,而是旧模型的一部分,在分离点上方,它们只是悬挂在那里 进一步调查时,model.summary()没有显示这些层,model.get\u层(“block4\u conv1”)也找不到它们,而kerastf.keras.utils.plot\u模型也没有显示它们。但如果它们不是图表的一部分,tensorboard怎么知道它们呢 为了构建新模型,我使用了推荐的方法 模型第一
进一步调查时,
model.summary()
没有显示这些层,model.get\u层(“block4\u conv1”)
也找不到它们,而kerastf.keras.utils.plot\u模型也没有显示它们。但如果它们不是图表的一部分,tensorboard怎么知道它们呢
为了构建新模型,我使用了推荐的方法
模型第一阶段:
vgg_input_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_tensor=x)
final_vgg_kayer = vgg_input_model.get_layer("block3_pool")
input_model = tf.keras.Model(inputs=vgg_input_model.inputs, outputs=final_vgg_kayer.output)
input_model.trainable = True
x = tf.keras.layers.Conv2D(512, 1, padding="same", activation='relu', name="stage0_final_conv1")(input_model.output)
x = tf.keras.layers.Conv2D(512, 1, padding="same", activation='relu', name="stage0_final_conv2")(x)
x = tf.keras.layers.Conv2D(256, 1, padding="same", activation='relu', name="stage0_final_conv3")(x)
x = tf.keras.layers.Conv2D(128, 1, padding="same", activation='relu', name="stage0_final_conv4")(x)
在尝试了多种方法后,我得出结论,推荐的方法是错误的。执行model\u b=tf.keras.model(输入=model\u a.inputs,输出=model\u a.get\u layet(“某些层”).output)
将导致从model\u a
悬挂层。
在这两者之间使用tf.keras.backend.clear_session()
可能会清除keras图形,但tensorboard的图形则保留为空
我找到的最佳解决方案是按层配置所需模型的副本。
并在新模型中重建连接。这样,两个模型之间的Keras图中就没有任何关系了。
(这对于像VGG这样的顺序模型很简单,但对于像ResNet这样的模型可能更难)
示例代码:
tf.keras.backend.clear_session()
input_shape = (368, 368, 3) #only the input shape is shared between the models
#transfer learning model definition
input_layer_vgg = tf.keras.layers.Input(shape=input_shape)
vgg_input_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_layer_vgg)
name_last_layer = "block3_pool" #the last layer to copy
tf.keras.backend.clear_session() #clean the graph from the transfer learning model
input_layer = tf.keras.layers.Input(shape=input_shape) #define the input layer for the first model
x=input_layer
for layer in vgg_input_model.layers[1:]: #copy over layers, without the other input layer
config=layer.get_config() #get config
weights=layer.get_weights() #get weights
#print(config)
copy_layer=type(layer).from_config(config) #create the new layer from config
x=copy_layer(x) #connect to previous layers,
#required for the proper sizing of the layer,
#set_weights will not work without it
copy_layer.set_weights(weights)
if layer.name == name_last_layer:
break
del vgg_input_model
input_model=tf.keras.Model(inputs=input_layer,outputs=x) #create the new model,
#if needed x can be used further doen the line
在尝试了多种方法后,我得出结论,推荐的方法是错误的。执行model\u b=tf.keras.model(输入=model\u a.inputs,输出=model\u a.get\u layet(“某些层”).output)
将导致从model\u a
悬挂层。
在这两者之间使用tf.keras.backend.clear_session()
可能会清除keras图形,但tensorboard的图形则保留为空
我找到的最佳解决方案是按层配置所需模型的副本。
并在新模型中重建连接。这样,两个模型之间的Keras图中就没有任何关系了。
(这对于像VGG这样的顺序模型很简单,但对于像ResNet这样的模型可能更难)
示例代码:
tf.keras.backend.clear_session()
input_shape = (368, 368, 3) #only the input shape is shared between the models
#transfer learning model definition
input_layer_vgg = tf.keras.layers.Input(shape=input_shape)
vgg_input_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_layer_vgg)
name_last_layer = "block3_pool" #the last layer to copy
tf.keras.backend.clear_session() #clean the graph from the transfer learning model
input_layer = tf.keras.layers.Input(shape=input_shape) #define the input layer for the first model
x=input_layer
for layer in vgg_input_model.layers[1:]: #copy over layers, without the other input layer
config=layer.get_config() #get config
weights=layer.get_weights() #get weights
#print(config)
copy_layer=type(layer).from_config(config) #create the new layer from config
x=copy_layer(x) #connect to previous layers,
#required for the proper sizing of the layer,
#set_weights will not work without it
copy_layer.set_weights(weights)
if layer.name == name_last_layer:
break
del vgg_input_model
input_model=tf.keras.Model(inputs=input_layer,outputs=x) #create the new model,
#if needed x can be used further doen the line