-
Notifications
You must be signed in to change notification settings - Fork 16
/
sedinet_predictfolder.py
197 lines (172 loc) · 7.79 KB
/
sedinet_predictfolder.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
## Written by Daniel Buscombe,
## MARDA Science
##> Release v1.3 (July 2020)
###===================================================
# import libraries
import sys, getopt, json, os
from numpy import any as npany
USE_GPU = True
if USE_GPU == True:
##use the first available GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '0' #'1'
else:
## to use the CPU (not recommended):
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from sedinet_eval import *
#==============================================================
if __name__ == '__main__':
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv,"h:c:i:w:1:2:3:4:")
except getopt.GetoptError:
print('python sedinet_predictfolder.py -c configfile.json -i path/to/image_folder {-w weightsfile.hdf5} OR {-1 weightsfile_batch1.hdf5 -2 weightsfile_batch2.hdf5 -3 weightsfile_batch3.hdf5 -4 weightsfile_batch4.hdf5}')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Example usage (single batch / weights file): python sedinet_predict1image.py \
-c config/config_mattole.json \
-i images/mattole_images/all/DSCN3521c.JPG \
-w mattole/res/mattole_simo_batch7_im512_512_2vars_pinball_aug.hdf5')
print('Example usage (multiple batches / weights files): python sedinet_predict1image.py -c config/config_9percentiles.json \
-1 grain_size_global/res/global_9prcs_simo_batch7_im768_9vars_pinball_noaug.hdf5 \
-2 grain_size_global/res/global_9prcs_simo_batch12_im768_9vars_pinball_noaug.hdf5 \
-3 grain_size_global/res/global_9prcs_simo_batch14_im768_9vars_pinball_noaug.hdf5')
sys.exit()
elif opt in ("-c"):
configfile = arg
elif opt in ("-w"):
weights_path = arg
elif opt in ("-i"):
folder = arg
elif opt in ("-1"):
weights_path1 = arg
elif opt in ("-2"):
weights_path2 = arg
elif opt in ("-3"):
weights_path3 = arg
elif opt in ("-4"):
weights_path4 = arg
if 'folder' not in locals():
if not os.path.isfile(image):
print("image folder path does not exist ... exiting")
sys.exit()
if 'weights_path1' not in locals():
if not os.path.isfile(os.getcwd()+os.sep+weights_path):
if not os.path.isfile(weights_path):
print("Weights path does not exist ... exiting")
sys.exit()
else:
weights_path = []
if not os.path.isfile(os.getcwd()+os.sep+weights_path1):
if not os.path.isfile(weights_path1):
print("Weights path 1 does not exist ... exiting")
sys.exit()
else:
weights_path.append(weights_path1)
else:
weights_path.append(weights_path1)
if 'weights_path2' in locals():
if not os.path.isfile(os.getcwd()+os.sep+weights_path2):
if not os.path.isfile(weights_path2):
print("Weights path 2 does not exist ... exiting")
sys.exit()
else:
weights_path.append(weights_path2)
else:
weights_path.append(weights_path2)
if 'weights_path3' in locals():
if not os.path.isfile(os.getcwd()+os.sep+weights_path3):
if not os.path.isfile(weights_path3):
print("Weights path 3 does not exist ... exiting")
sys.exit()
else:
weights_path.append(weights_path3)
else:
weights_path.append(weights_path3)
if 'weights_path4' in locals():
if not os.path.isfile(os.getcwd()+os.sep+weights_path4):
if not os.path.isfile(weights_path4):
print("Weights path 4 does not exist ... exiting")
sys.exit()
else:
weights_path.append(weights_path4)
else:
weights_path.append(weights_path4)
try:
# load the user configs
with open(os.getcwd()+os.sep+configfile) as f:
config = json.load(f)
except:
# load the user configs
with open(configfile) as f:
config = json.load(f)
###===================================================
#csvfile containing image names and class values
csvfile = config["csvfile"]
#csvfile containing image names and class values
res_folder = config["res_folder"]
#folder containing csv file and that will contain model outputs
name = config["name"]
#name prefix for output files
#convert imagery to greyscale or not
dropout = config["dropout"]
#dropout factor
scale = config["scale"] #do scaling on variable
greyscale = config['greyscale']
try:
numclass = config['numclass']
except:
numclass = 0
try:
greyscale = config['greyscale']
except:
greyscale = 'true'
#output variables
vars = [k for k in config.keys() if not npany([k.startswith('base'), k.startswith('MAX_LR'),
k.startswith('MIN_LR'), k.startswith('DO_AUG'), k.startswith('SHALLOW'),
k.startswith('res_folder'), k.startswith('train_csvfile'), k.startswith('csvfile'),
k.startswith('test_csvfile'), k.startswith('name'),
k.startswith('greyscale'), k.startswith('aux_in'),
k.startswith('dropout'), k.startswith('N'),
k.startswith('scale'), k.startswith('numclass')])]
vars = sorted(vars)
#this relates to 'mimo' and 'miso' modes that are planned for the future but not currently implemented
auxin = [k for k in config.keys() if k.startswith('aux_in')]
if len(auxin) > 0:
auxin = config[auxin[0]] ##at least for now, just one 'auxilliary' (numerical/categorical) input in addition to imagery
if len(vars) ==1:
mode = 'miso'
elif len(vars) >1:
mode = 'mimo'
else:
if len(vars) ==1:
mode = 'siso'
elif len(vars) >1:
mode = 'simo'
print("Mode: %s" % (mode))
###==================================================
if type(BATCH_SIZE) is list and type(weights_path) is not list:
print("Please specify one weights file per batch size in the list ... exiting")
sys.exit()
if (mode=='siso' or mode=='simo'):
if numclass>0:
files = glob(folder+os.sep+'*.jpg')+glob(folder+os.sep+'*.tif')+glob(folder+os.sep+'*.png')
result = []
for image in files:
result.append(estimate_categorical_1image(vars, image, res_folder, dropout,
numclass, greyscale, name, mode, weights_path))
else:
files = glob(folder+os.sep+'*.jpg')+glob(folder+os.sep+'*.tif')+glob(folder+os.sep+'*.png')
result = []
for image in files:
result.append(estimate_siso_simo_1image(vars, image, greyscale,
dropout, numclass, scale, name, mode,
res_folder, BATCH_SIZE, weights_path))
d = {}
counter = 0
for var in vars:
d[var] = [r[counter] for r in result]
counter +=1
pd.DataFrame.from_dict(d).to_csv('data_out.csv')
print("Results written to data_out.csv")