duan8/unet/gen_wts.py
Wang Xinyu cb9efbdf21 unet: fix bug and coding style (#1171)
* unet: fix bug

* update readme

* remove useless code
2022-12-13 22:37:41 +08:00

25 lines
538 B
Python

import torch
import sys
import struct
def main():
device = torch.device('cpu')
state_dict = torch.load(sys.argv[1], map_location=device)
f = open("unet.wts", 'w')
f.write("{}\n".format(len(state_dict.keys())))
for k, v in state_dict.items():
print('key: ', k)
print('value: ', v.shape)
vr = v.reshape(-1).cpu().numpy()
f.write("{} {}".format(k, len(vr)))
for vv in vr:
f.write(" ")
f.write(struct.pack(">f", float(vv)).hex())
f.write("\n")
f.close()
if __name__ == '__main__':
main()