使用kerassurgeon对Xception剪枝触发TypeError的调试求助
kerassurgeon通道剪枝Xception模型运行TypeError报错记录
任务基础信息
- 使用者为神经网络领域入门新手
- 核心任务目标:对预训练Xception模型开展通道剪枝工作,选定kerassurgeon为剪枝实现工具
- 代码运行环境:Google Colab,全流程报错可稳定复现,寻求相关调试帮助,如需补充其他实验信息可随时沟通
相关代码片段
模块导入代码
from kerassurgeon import identify from kerassurgeon.operations import delete_channels, delete_layer
核心实现代码
#load pre trained Xception model model=tf.keras.applications.xception.Xception(weights='imagenet',include_top=True) weight = model.layers[1].get_weights()[0] weight_dict = {} num_filters = len(weight[0,0,0,:]) for j in range(num_filters) : w_s = np.sum(abs(weight[:,:,:,j])) filt = 'filt_{}'.format(j) weight_dict[filt]=w_s weights_dict_sort = sorted(weight_dict.items(), key = lambda kv:kv[1]) print('L1 norm conv layer {}\n'.format(1), weights_dict_sort) weights_value = [] for elem in weights_dict_sort : weights_value.append(elem[1]) xc = range(num_filters) layer_0 = model.layers[1] model_new = delete_channels(model=model, layer=layer_0, channels=[1,7,2,8,16]) model_new.compile(loss = 'binary_crossentropy', optimizer = 'rmsprop', metrics = ['accuracy'])
运行触发的TypeError类报错栈
TypeError Traceback (most recent call last) <ipython-input-42-8ec328142934> in <module>() 23 24 layer_0 = model.layers[1] ---> 25 model_new = delete_channels(model=model, layer=layer_0, channels=[1,7,2,8,16]) 26 27 model_new.compile(loss = 'binary_crossentropy', optimizer = 'rmsprop', metrics = ['accuracy']) 4 frames /usr/local/lib/python3.7/dist-packages/kerassurgeon/operations.py in delete_channels(model, layer, channels, node_indices, copy) 103 surgeon = Surgeon(model, copy) 104 surgeon.add_job('delete_channels', layer, node_indices=node_indices, channels=channels) --> 105 return surgeon.operate() /usr/local/lib/python3.7/dist-packages/kerassurgeon/surgeon.py in operate(self) 157 # Perform surgery at this node 158 kwargs = self._kwargs_map[node] --> 159 self._mod_func_map[node](node, outputs, output_masks, **kwargs) 160 161 # Finish rebuilding model /usr/local/lib/python3.7/dist-packages/kerassurgeon/surgeon.py in _delete_channels(self, node, inputs, input_masks, channels, layer_name) 317 """ 318 old_layer = node.outbound_layer --> 319 old_layer_output = utils.single_element(node.output_tensors) 320 # Create a mask to propagate the deleted channels to downstream layers 321 new_delete_mask = self._make_delete_mask(old_layer, channels) /usr/local/lib/python3.7/dist-packages/kerassurgeon/utils.py in single_element(x) 141 return x 142 --> 143 if len(x) == 1: 144 x = x[0] 145 return x /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/keras_tensor.py in __len__(self) 238 239 def __len__(self): --> 240 raise TypeError('Keras symbolic inputs/outputs do not ' 241 'implement `__len__`. You may be ' 242 'trying to pass Keras symbolic inputs/outputs ' TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.
内容的提问来源于stack exchange,提问作者Nitin Sharma
相关产品推荐
相关产品推荐

