Python 如何从预训练的ResNet50V2 Keras模型中删除多个层
我试图从预先训练过的Keras模型中删除多个层(Python 如何从预训练的ResNet50V2 Keras模型中删除多个层,python,tensorflow,keras,resnet,transfer-learning,Python,Tensorflow,Keras,Resnet,Transfer Learning,我试图从预先训练过的Keras模型中删除多个层(ResNet50V2),但无论我做什么,它都不起作用。在过去的一个月里,我已经阅读了无数与这个话题相关的其他帖子和论坛帖子,但我仍然无法让它发挥作用。。。所以我会直接问。我可能做错了什么 from ray.rllib.models.tf.tf_modelv2 import TFModelV2 from ray.rllib.utils.framework import try_import_tf from ray.rllib.models impor
ResNet50V2
),但无论我做什么,它都不起作用。在过去的一个月里,我已经阅读了无数与这个话题相关的其他帖子和论坛帖子,但我仍然无法让它发挥作用。。。所以我会直接问。我可能做错了什么
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?
。。。
文件“/Users/manu/anaconda3/envs/procgen/lib/python3.7/site packages/ray/rllib/evaluation/rollout_worker.py”,第376行,在初始化中__
self.\u build\u policy\u map(policy\u dict,policy\u config)
文件“/Users/manu/anaconda3/envs/procgen/lib/python3.7/site packages/ray/rllib/evaluation/rollout\u worker.py”,第859行,在构建策略图中
策略映射[名称]=cls(obs\U空间、act\U空间、合并\U配置)
文件“/Users/manu/anaconda3/envs/procgen/lib/python3.7/site packages/ray/rllib/policy/tf_policy_template.py”,第143行,在u init中__
obs_包括_上一步行动_奖励=obs_包括_上一步行动_奖励)
文件“/Users/manu/anaconda3/envs/procgen/lib/python3.7/site packages/ray/rllib/policy/dynamic_tf_policy.py”,第163行,在__
framework=“tf”)
文件“/Users/manu/anaconda3/envs/procgen/lib/python3.7/site packages/ray/rllib/models/catalog.py”,第317行,在get_model_v2中
(已登记)
ValueError:看起来像是变量{而不是弹出层,您可以尝试从最后一层访问第130层。然后,您可以使用原始模型的输入和该层的输出构建新模型
model = tf.keras.models.Model(resnet.input, resnet.layers[-130].output)
这与您尝试的方法基本相同,但由于您不访问模型本身的任何私有属性,因此更容易、更安全。嗨,Richard!感谢您的回复。我尝试了您的方法,但仍然收到有关未注册的一些变量的ValueError
。我真的不想要这些变量s注册,因为我想放弃它们!有什么想法吗?