-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_from_file.py
More file actions
71 lines (58 loc) · 2.37 KB
/
Copy pathpredict_from_file.py
File metadata and controls
71 lines (58 loc) · 2.37 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
import sys
import os
import numpy as np
import pandas as pd
from utils import save_predictions, save_probabilities
from utils import preprocess_image
from utils import kappa_loss, ordinal_loss, cauchy_loss, correntropy_loss
from keras.models import load_model
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
file_path = 'data'
if len(sys.argv) > 2:
output_csv = sys.argv[2]
else:
output_csv = 'output.csv'
model_path = '.'
model_name = 'DRNet.h5' # Name of the model file to load
model = load_model(os.path.join(model_path, model_name), custom_objects = {'kappa_loss': kappa_loss, 'ordinal_loss': ordinal_loss, 'cauchy_loss': cauchy_loss, 'correntropy_loss': correntropy_loss})
fill_type = 'mix' # Fill type for image proprocessing. 'crop, 'pad', or 'mix'
softmax_activation = False # If True, softmax activation was used for the output layer, otherwise sigmoid was used
im_size = 224 # 299 for inception style networks, 224 for densenet style networks
### Load data
try:
files = pd.read_csv(output_csv).iloc[:,0].values
except:
files = []
for f in sorted(os.listdir(file_path)):
if f.endswith('.tif') or f.endswith('.jpeg') or f.endswith('.jpg'):
files.append(f)
N = len(files)
print("Files:", N)
x_test = np.empty((N, im_size, im_size, 3), dtype = np.uint8)
for i in range(N):
filename = files[i]
filename = os.path.join(file_path, filename)
if not (filename.endswith('.tif') or filename.endswith('.jpeg') or filename.endswith('.jpg') or filename.endswith('.png')):
if os.path.exists(filename + '.tif'):
filename += '.tif'
elif os.path.exists(filename + '.jpeg'):
filename += '.jpeg'
elif os.path.exists(filename + '.jpg'):
filename += '.jpg'
elif os.path.exists(filename + '.png'):
filename += '.png'
else:
print("Check file extension")
x_test[i, :, :, :] = preprocess_image(filename, fill_type = fill_type, desired_size = im_size)
def predict_test(x_test):
if softmax_activation:
y_test_raw = model.predict(x_test, verbose = 1)
y_test = np.argmax(y_test_raw, axis = 1)
else:
y_test_raw = model.predict(x_test, verbose = 1)
y_test = (y_test_raw > 0.5).astype(int).sum(axis=1) - 1
save_predictions(y_test, files, save_name = output_csv)
if __name__ == "__main__":
predict_test(x_test)