如何从预训练的ResNet50V2 Keras模型中删除多个图层

时间:2020-08-20 17:03:00

标签: python tensorflow keras resnet transfer-learning

我正在尝试从预先训练的Keras模型(ResNet50V2)中删除多个图层,但是无论我做什么,都行不通。在过去的一个月中,我已经阅读了无数其他与该主题相关的questions on stack overflowgithub issues和论坛帖子,但是我仍然无法使其正常工作,所以我直接问。我可能做错了什么?

from ray.rllib.models.tf.tf_modelv2 import TFModelV2
from ray.rllib.utils.framework import try_import_tf
from ray.rllib.models import ModelCatalog
tf = try_import_tf()

def resnet_core(x):
    x = tf.keras.applications.resnet_v2.preprocess_input(x)
    resnet = tf.keras.applications.ResNet50V2(
        include_top=False,
        weights="imagenet",
    )
    remove_n = 130
    for i in range(remove_n):
        resnet._layers.pop()
        print(len(resnet._layers))
    s = tf.keras.models.Model(resnet.input, resnet._layers[-1].output, name='resnet-core')
    for layer in s.layers:
        print('adding layer',layer.name)
    for layer in s.layers[:]:
        layer.trainable = False
    s.build(None)

    return s(x)

class ImpalaCNN(TFModelV2):

    def __init__(self, obs_space, action_space, num_outputs, model_config, name):
        super().__init__(obs_space, action_space, num_outputs, model_config, name)

        inputs = tf.keras.layers.Input(shape=obs_space.shape, name="observations")
        x = inputs
        x = resnet_core(x)
        x = tf.keras.layers.Flatten()(x)
        x = tf.keras.layers.ReLU()(x)
        x = tf.keras.layers.Dense(units=256, activation="relu", name="hidden")(x)

        logits = tf.keras.layers.Dense(units=num_outputs, name="pi")(x)
        value = tf.keras.layers.Dense(units=1, name="vf")(x)

        self.base_model = tf.keras.Model(inputs, [logits, value])
        self.register_variables(self.base_model.variables)

    def forward(self, input_dict, state, seq_lens):
        obs = tf.cast(input_dict["obs"], tf.float32)
        logits, self._value = self.base_model(obs)
        return logits, state

    def value_function(self):
        return tf.reshape(self._value, [-1])


# Register model in ModelCatalog
ModelCatalog.register_custom_model("impala_cnn_tf", ImpalaCNN)

我得到的错误是:

  ...
  File "/Users/manu/anaconda3/envs/procgen/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 376, in __init__
    self._build_policy_map(policy_dict, policy_config)
  File "/Users/manu/anaconda3/envs/procgen/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 859, in _build_policy_map
    policy_map[name] = cls(obs_space, act_space, merged_conf)
  File "/Users/manu/anaconda3/envs/procgen/lib/python3.7/site-packages/ray/rllib/policy/tf_policy_template.py", line 143, in __init__
    obs_include_prev_action_reward=obs_include_prev_action_reward)
  File "/Users/manu/anaconda3/envs/procgen/lib/python3.7/site-packages/ray/rllib/policy/dynamic_tf_policy.py", line 163, in __init__
    framework="tf")
  File "/Users/manu/anaconda3/envs/procgen/lib/python3.7/site-packages/ray/rllib/models/catalog.py", line 317, in get_model_v2
    registered))
ValueError: It looks like variables {<tf.Variable 'default_policy/
conv4_block4_1_conv/kernel:0' ... } 
were created as part of <impala_cnn_tf.ImpalaCNN object at 
0x19a8ccc90> but does not appear in model.variables() 
({<tf.Variable 'default_policy/pi/
kernel:0' shape=(256, 15) dtype=float32> ...}). Did you forget to call
 model.register_variables() on the variables in question?

该错误似乎表明我尝试跳过的层中的某些变量未注册,但这是因为我不想使用它们!有什么想法吗?

提供更多上下文信息,以帮助解决问题:

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试从最后一层访问第130层,而不是弹出层。然后,您可以使用原始模型的输入和该层的输出来构建新模型。

model = tf.keras.models.Model(resnet.input, resnet.layers[-130].output)

这基本上与您尝试的操作相同,但是它更加容易和安全,因为您没有访问模型本身的任何私有属性。