Caffe 1.0.274でkeras2caffeを使った際、KeyErrorが発生します。
これは、hasattrがgetattrで実装されていますが、caffeがKeyErrorをAttributeErrorに置き換えていないために発生します。
How to make a class which has __getattr__ properly pickable?
そこで、NetSpecでKeyErrorをAttributeErrorに変換します。
これで正常にcaffemodelに変換することができます。
Traceback (most recent call last): File "convert_to_caffemodel.py", line 76, inkeras2caffe.convert(keras_model, PROTOTXT, WEIGHTS) File "keras2caffe\convert.py", line 76, in convert if layer_type=='InputLayer' or not hasattr(caffe_net, 'data'): File "Anaconda3\envs\py35\lib\site-packages\caffe\net_spec.py", line 180, in __getattr__ return self.tops[name] KeyError: 'data'
これは、hasattrがgetattrで実装されていますが、caffeがKeyErrorをAttributeErrorに置き換えていないために発生します。
How to make a class which has __getattr__ properly pickable?
class NetSpec(object): """A NetSpec contains a set of Tops (assigned directly as attributes). Calling NetSpec.to_proto generates a NetParameter containing all of the layers needed to produce all of the assigned Tops, using the assigned names.""" def __init__(self): super(NetSpec, self).__setattr__('tops', OrderedDict()) def __setattr__(self, name, value): self.tops[name] = value def __getattr__(self, name): return self.tops[name]
そこで、NetSpecでKeyErrorをAttributeErrorに変換します。
class NetSpec(object): """A NetSpec contains a set of Tops (assigned directly as attributes). Calling NetSpec.to_proto generates a NetParameter containing all of the layers needed to produce all of the assigned Tops, using the assigned names.""" def __init__(self): super(NetSpec, self).__setattr__('tops', OrderedDict()) def __setattr__(self, name, value): self.tops[name] = value def __getattr__(self, name): try: return self.tops[name] except KeyError: raise AttributeError(name)
これで正常にcaffemodelに変換することができます。
コメント