pytorch中.pth文件转成.bin的二进制文件

                model_dict = torch.load(save_path)
                fp = open('model_parameter.bin', 'wb')
                weight_count = 0
                num=1
                for k, v in model_dict.items():
                    print(k,num)
                    num=num+1
                    if 'num_batches_tracked' in k:
                        continue
                    v = v.cpu().numpy().flatten()
                    for d in v:
                        fp.write(d)
                        weight_count+=1
                print('model_weight has Convert Completely!',weight_count)

 

posted @ 2020-09-25 15:13  皮卡皮卡妞  阅读(5184)  评论(0编辑  收藏  举报