-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
125 lines (102 loc) · 3.14 KB
/
evaluate.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
import glob
import os
import dlci
import json
import sys
import getopt
import numpy as np
from PIL import Image
from dlci import deeptrack as dt
_PATH_TO_DATASET = "data"
_PATH_TO_MODELS = "/virtual staining/models"
# Grab passed arguments
opts, args = getopt.getopt(sys.argv[1:], "i:t:s:")
# Defaults
args = {
"index": None,
"set": "1",
"save": True,
}
for opt, arg in opts:
if opt == "-i":
args["index"] = [i for i in arg.split("-")]
elif opt == "-t":
args["set"] = arg
elif opt == "-s":
args["save"] = arg == "True"
print("Loading models...")
calcein = dlci.load_model(
glob.glob(os.path.join(_PATH_TO_MODELS, "model_calcein", "*"))[0]
)
calcein.compile(loss="mae")
caspase = dlci.load_model(
glob.glob(os.path.join(_PATH_TO_MODELS, "model_caspase", "*"))[0]
)
caspase.compile(loss="mae")
print("")
print("=" * 50, "START", "=" * 50)
network = (
dt.Lambda(
lambda: lambda image: [
calcein.predict(np.expand_dims(image, axis=0)),
caspase.predict(np.expand_dims(image, axis=0)),
]
)
+ dt.Multiply(0.5)
+ dt.Add(0.5)
)
filenames = glob.glob(
os.path.join(_PATH_TO_DATASET, "set " + args["set"], "*ch00*.tif")
)
SITES = list(set([file[-19:-17] for file in filenames]))
if args["index"]:
SITES = list(filter(lambda fn: fn in args["index"], SITES))
print("Analyzing {} samples...".format(len(SITES)))
for site in SITES:
_filenames = list(filter(lambda fn: fn[-19:-17] == site, filenames))
for _type in ("calcein", "caspase"):
folder_path = os.path.join(
_PATH_TO_DATASET, "set " + args["set"], "results", site, _type
)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
print("Analyzing sample {}... saving to {}".format(site, folder_path))
for idx, file in enumerate(_filenames):
print(file)
print("----frame: " + str(idx))
loader = (
dt.LoadImage(path=file)
+ dt.PadToMultiplesOf(multiple=(32, 32, None))
+ dt.NormalizeMinMax(min=-1, max=1)
)
staining = loader + network
image_calcein, image_caspase = staining.update().resolve()
image_calcein = np.array(image_calcein)
image_caspase = np.array(image_caspase)
if args["save"]:
im = Image.fromarray(
(255 * image_calcein[0, ..., 0]).astype(np.uint8)
)
im.save(
os.path.join(
_PATH_TO_DATASET,
"set " + args["set"],
"results",
site,
"calcein",
file.split("\\")[2].replace("ch00", "ch01"),
)
)
im = Image.fromarray(
(255 * image_caspase[0, ..., 0]).astype(np.uint8)
)
im.save(
os.path.join(
_PATH_TO_DATASET,
"set " + args["set"],
"results",
site,
"caspase",
file.split("\\")[2].replace("ch00", "ch01"),
)
)