-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_io_functions.py
61 lines (50 loc) · 1.75 KB
/
model_io_functions.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import pickle
from keras.models import model_from_json
# Validate the presence of the audio file
def checkForFile(filepath):
if os.path.exists(filepath):
return True
else:
return False
# Save the model and weights
def saveModel(modelName, model):
save_dir = os.path.join(os.getcwd(), 'my_models')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, modelName+".h5")
model.save(model_path)
print('Saved trained model at %s ' % model_path)
model_json = model.to_json()
with open(save_dir + "/" + modelName+".json", "w") as json_file:
json_file.write(model_json)
# Save the model and weights
def savePickleModel(modelName, model):
save_dir = os.path.join(os.getcwd(), 'my_models')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, modelName+".sav")
pickle.dump(model, open(model_path, 'wb'))
print('Saved trained model at %s ' % model_path)
# Load the models from pickled files
def loadPickledModel(filepath):
try:
loaded_model = pickle.load(open(filepath, 'rb'))
return loaded_model
except FileNotFoundError:
print("ERROR - The imported model files were not found")
exit(1)
# Load model and weights from Json
def loadJsonModel(weightFile, modelFile):
try:
jsonFile = open(modelFile, 'r')
loadedModel = jsonFile.read()
jsonFile.close()
loadedModel = model_from_json(loadedModel)
# load weights
loadedModel.load_weights(weightFile)
print("Loaded Model From Disk")
return loadedModel
except FileNotFoundError:
print("ERROR - The imported model files were not found")
exit(1)