前提・実現したいこと
画像処理の学習モデルのh5ファイルをweightsファイルに変換でエラーがでます。
下記ソース以外でh5ファイルをweightsファイルに変換する方法などがあれば
教えて頂けても助かります。
発生している問題・エラーメッセージ
File "converter_h5-2-wts.py", line 119, in <module> keras_loader.close() NameError: name 'keras_loader' is not defined
該当のソースコード
python
1# Script converter_h5-2-wts.py 2# -*-coding: utf-8 -*- 3''' yolov3_keras_to_darknet.py''' 4import argparse 5import numpy 6import numpy as np 7import keras 8from keras.models import load_model 9from keras import backend as K 10def parser(): 11 parser= argparse.ArgumentParser(description="Darknet\'s yolov3.cfg and yolov3.weights \converted into Keras\'s yolov3.h5!") 12 parser.add_argument('-cfg_path', help='yolov3.cfg') 13 parser.add_argument('-h5_path', help='yolov3.h5') 14 parser.add_argument('-output_path', help='yolov3.weights') 15 return parser.parse_args() 16class WeightSaver(object): 17 def __init__(self,h5_path,output_path): 18 self.model= load_model(h5_path) 19 self.layers= {weight.name:weight for weight in self.model.weights} 20 self.sess= K.get_session() 21 self.fhandle= open(output_path,'wb') 22 self._write_head() 23 def _write_head(self): 24 numpy_data= numpy.ndarray(shape=(3,), 25 dtype='int32', 26 buffer=np.array([0,2,0],dtype='int32') ) 27 self.save(numpy_data) 28 numpy_data= numpy.ndarray(shape=(1,), 29 dtype='int64', 30 buffer=np.array([320000],dtype='int64')) 31 self.save(numpy_data) 32 def get_bn_layername(self,num): 33 layer_name= 'batch_normalization_{num}'.format(num=num) 34 bias= self.layers['{0}/beta:0'.format(layer_name)] 35 scale= self.layers['{0}/gamma:0'.format(layer_name)] 36 mean= self.layers['{0}/moving_mean:0'.format(layer_name)] 37 var= self.layers['{0}/moving_variance:0'.format(layer_name)] 38 bias_np= self.get_numpy(bias) 39 scale_np= self.get_numpy(scale) 40 mean_np= self.get_numpy(mean) 41 var_np= self.get_numpy(var) 42 return bias_np,scale_np,mean_np,var_np 43def get_convbias_layername(self,num): 44 layer_name= 'conv2d_{num}'.format(num=num) 45 bias= self.layers['{0}/bias:0'.format(layer_name)] 46 bias_np= self.get_numpy(bias) 47 return bias_np 48def get_conv_layername(self,num): 49 layer_name= 'conv2d_{num}'.format(num=num) 50 conv= self.layers['{0}/kernel:0'.format(layer_name)] 51 conv_np= self.get_numpy(conv) 52 return conv_np 53def get_numpy(self,layer_name): 54 numpy_data= self.sess.run(layer_name) 55 return numpy_data 56def save(self,numpy_data): 57 bytes_data= numpy_data.tobytes() 58 self.fhandle.write(bytes_data) 59 self.fhandle.flush() 60def close(self): 61 self.fhandle.close() 62class KerasParser(object): 63 def __init__(self, cfg_path, h5_path, output_path): 64 self.block_gen= self._get_block(cfg_path) 65 self.weights_saver= WeightSaver(h5_path, output_path) 66 self.count_conv= 0 67 self.count_bn= 0 68def _get_block(self,cfg_path): 69 block= {} 70 with open(cfg_path,'r', encoding='utf-8') as fr: 71 for line in fr: 72 line= line.strip() 73 if '[' in line and ']' in line: 74 if block: 75 yield block 76 block= {} 77 block['type']= line.strip(' []') 78 elif not line or '#' in line: 79 continue 80 else: 81 key,val= line.strip().replace(' ','').split('=') 82 key,val= key.strip(), val.strip() 83 block[key]= val 84 yield block 85def close(self): 86 self.weights_saver.close() 87def conv(self, block): 88 self.count_conv += 1 89 batch_normalize= 'batch_normalize' in block 90 print('handing.. ',self.count_conv) 91 # If bn exists, process bn first, in order of bias, scale, mean, var 92 if batch_normalize: 93 bias,scale,mean,var= self.bn() 94 self.weights_saver.save(bias) 95 scale= scale.reshape(1,-1) 96 mean= mean.reshape(1,-1) 97 var= var.reshape(1,-1) 98 remain= np.concatenate([scale,mean,var],axis=0) 99 self.weights_saver.save(remain) 100 # biase 101 else: 102 conv_bias= self.weights_saver.get_convbias_layername(self.count_conv) 103 self.weights_saver.save(conv_bias) 104 # weights 105 conv_weights= self.weights_saver.get_conv_layername(self.count_conv) 106 # (height, width, in_dim, out_dim) (out_dim, in_dim, height, width) 107 conv_weights= np.transpose(conv_weights,[3,2,0,1]) 108 self.weights_saver.save(conv_weights) 109def bn(self): 110 self.count_bn += 1 111 bias,scale,mean,var= self.weights_saver.get_bn_layername(self.count_bn) 112 return bias,scale,mean,var 113def main(): 114 args= parser() 115 keras_loader= KerasParser(args.cfg_path, args.h5_path, args.output_path) 116 for block in keras_loader.block_gen: 117 if 'convolutional' in block['type']: 118 keras_loader.conv(block) 119keras_loader.close() 120if __name__== "__main__": 121 main() 122
試したこと
補足情報(FW/ツールのバージョンなど)
python 3.6.3
質問に掲載されてるコードは、どこから持ってきたものでしょうか?
もしこれなら、インデントが間違いまくってますよ
https://github.com/OmniXRI/OpenVINO_RealSense_HarvestBot/blob/master/my_yolo3/yolov3_keras_to_darknet.py
あなたの回答
tips
プレビュー