如何解决如何从预训练的ResNet50V2 Keras模型中删除多个图层
我正在尝试从预先训练的Keras模型(ResNet50V2
)中删除多个图层,但是无论我做什么,都行不通。在过去的一个月中,我已经阅读了无数其他与该主题相关的questions on stack overflow,github 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,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?
该错误似乎表明我尝试跳过的层中的某些变量未注册,但这是因为我不想使用它们!有什么想法吗?
提供更多上下文信息,以帮助解决问题:
- 如果我设置了
remove_n = 0
,我看不到错误(但是整个ResNet50V2
都在使用中) - 我是Keras和ML的新手。这可能是一个非常愚蠢的问题。
- 我要删除很多层而不仅仅是最后一层的原因是我希望模型能够安装在小型GPU上。
- 我正在尝试使用rllib为the aicrowd procgen competition训练模型(通过比赛学习Keras / RL)
- 此处的完整代码:https://github.com/maraoz/neurips2020-procgen-starter-kit/blob/master/models/impala_cnn_tf.py#L55
- 此处有完整的错误日志:https://pastebin.com/X0Dk7wdd
谢谢!
解决方法
您可以尝试从最后一层访问第130层,而不是弹出层。然后,您可以使用原始模型的输入和该层的输出来构建新模型。
model = tf.keras.models.Model(resnet.input,resnet.layers[-130].output)
这基本上与您尝试的操作相同,但是它更加容易和安全,因为您没有访问模型本身的任何私有属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。