-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
38 lines (28 loc) · 1006 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import torch
import numpy as np
def makedirs(path):
if not os.path.exists(path):
os.makedirs(path)
def load_model(model, file_name):
model.load_state_dict(
torch.load(file_name, map_location=lambda storage, loc: storage))
def save_model(model, file_name):
# 如果文件路径不存在则创建
# if not os.path.exists(file_name):
# os.makedirs(file_name)
torch.save(model.state_dict(), file_name)
def save_array_dict(data, file_name):
# 如果文件路径不存在则创建
# if not os.path.exists(file_name):
# os.makedirs(file_name)
np.save("./Checkpoint/%s.npy" % str(file_name), data)
def load_array_dict(file_name, Type='Array'):
# 如果文件路径不存在则创建
if not os.path.exists(file_name):
os.makedirs(file_name)
if Type == 'Array':
k = np.load("./Checkpoint/%s.npy" % str(file_name))
else:
k = np.load('./Checkpoint/%s.npy' % str(file_name)).item()
return k