-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdl_basics_utilities.py
More file actions
138 lines (114 loc) · 5.48 KB
/
Copy pathdl_basics_utilities.py
File metadata and controls
138 lines (114 loc) · 5.48 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'''
Copyright (c) 2017-21 Andrew Glassner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and #associated documentation files (the "Software"), to deal i
n the Software without restriction, including #without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the #following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Deep Learning: A Visual Approach
by Andrew Glassner, https://glassner.com
Order: https://nostarch.com/deep-learning-visual-approach
GitHub: https://github.com/blueberrymusic
'''
#
# -----------------------------------------------------------------------------
#
import os
import matplotlib.pyplot as plt
import numpy as np
import h5py
from keras.models import load_model
class File_Helper:
"""
These routines let us conveniently save and load input data, such
as text and image files, as well as save and load Keras model files
and weight files. When we save a file, the corresponding directory
is created if necessary.
When we make the object, we can optionally set the one argument really_save_files
to True or False, depending on whether or not we want save_xx() calls to really
write files. It's time-saving to set this to False when debugging because writing
files can take a while. The default value is True.
Here's a typical way to import this package from a file in a folder two levels down
(adapted from https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder)
# find the absolute path to the parent folder and add that to Python's search list
import os, sys, inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(0, os.path.dirname(current_dir)) # path to grandparent dir
# Now that we can find the folder, import the package and instantiate a File_Helper object
from Python_Utilities import File_Helper
file_helper = File_Helper(True)
"""
def __init__(self, really_save_files=True):
self.really_save_files = really_save_files
self.saved_output_dir = 'saved_output'
self.input_data_dir = 'input_data'
self.saved_models_dir = 'saved_models'
self.saved_weights_dir = 'saved_weights'
def get_input_file_path(self, filename):
"""Get the local path relative to the calling file's location to the input file"""
return self.input_data_dir + '/' + filename
def check_for_directory(self, directory, create_if_needed=True):
"""See if the directory exists. Optionally, create it."""
path_exists = os.path.exists(directory)
if path_exists:
if not os.path.isdir(directory):
raise Exception('Found '+directory+' but it is a file, not a directory')
return False
return True
if create_if_needed:
os.makedirs(directory)
return path_exists
def save_figure(self, filename):
"""Save the figure. Call this just before plt.show()."""
if self.really_save_files and (filename != None):
self.check_for_directory(self.saved_output_dir)
plt.savefig(self.saved_output_dir+'/'+filename+'.png', dpi=300, bbox_inches='tight')
def load_model_weights(self, model, weights_filename):
"""If the weights file exists, load from it and return True, else return False."""
fullpath = self.saved_weights_dir+'/'+weights_filename+'.h5'
if os.path.exists(fullpath):
if os.path.isfile(fullpath):
model.load_weights(fullpath)
return True
return False
def save_model_weights(self, model, weights_filename):
"""Save the weights file in the saved weights directory."""
if self.really_save_files and (weights_filename != None):
self.check_for_directory(self.saved_weights_dir)
fullpath = self.saved_weights_dir+'/'+weights_filename+'.h5'
model.save_weights(fullpath)
def load_model(self, model_filename):
"""If the model file exists, load from it and return the model, else return None."""
fullpath = self.saved_models_dir+'/'+model_filename+'.h5'
if os.path.exists(fullpath):
if os.path.isfile(fullpath):
model = load_model(fullpath)
return model
return None
def save_model(self, model, model_filename):
"""Save the model file in the saved models directory."""
if self.really_save_files and (model_filename != None):
self.check_for_directory(self.saved_models_dir)
fullpath = self.saved_models_dir+'/'+model_filename+'.h5'
model.save(fullpath)
def get_saved_output_dir(self):
"""Get the name of the directory where we save matplotlib output PNG files."""
return self.saved_output_dir
def get_input_data_dir(self):
"""Get the name of the directory where we look for input files."""
return self.input_data_dir
def get_saved_weights_dir(self):
"""Get the name of the directory where we read and write Keras weight files."""
return self.saved_weights_dir
def get_saved_models_dir(self):
"""Get the name of the directory where we read and write Keras model files."""
return self.saved_models_dir