-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·211 lines (188 loc) · 5.88 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import argparse
import logging
import sys
from hpa_densenet import constants, dimred, prediction, preprocess, umapNd
def _build_preprocessing_subcommand(preprocessing: argparse.ArgumentParser) -> None:
preprocessing.set_defaults(command="preprocess")
preprocessing.add_argument(
"-s",
"--src-dir",
type=str,
default=None,
help="source directory",
required=True,
)
preprocessing.add_argument(
"-d",
"--dst-dir",
type=str,
default=None,
help="destination directory",
required=True,
)
preprocessing.add_argument("--size", type=int, default=1536, help="image size")
preprocessing.add_argument(
"-w",
"--num-workers",
type=int,
default=10,
help="The number of multiprocessing workers to perform the resizing",
)
preprocessing.add_argument(
"--continue",
dest="cont",
action="store_true",
help="Continue from a previously aborted run.",
)
def _build_prediction_subcommand(prediction: argparse.ArgumentParser) -> None:
prediction.set_defaults(command="predict")
prediction.add_argument(
"-s",
"--src-dir",
type=str,
default=None,
help="src image directory (preprocessed)",
required=True,
)
prediction.add_argument(
"-d",
"--dst-dir",
type=str,
default=None,
help="output directory",
required=True,
)
prediction.add_argument("--size", type=int, default=1536, help="image size")
prediction.add_argument(
"--gpu",
type=str,
default=None,
help=(
"Which gpus to use for prediction. Any string valid for `CUDA_VISIBLE_DEVICES`"
"is valid for this. If cpu calculations ONLY is desired, a value of 'cpu' is "
"also allowed."
),
)
def _build_dimred_subcommand(dimred: argparse.ArgumentParser) -> None:
dimred.set_defaults(command="dimred")
dimred.add_argument(
"-s",
"--src",
type=str,
default=None,
help="Source feature file to reduce.",
required=True,
)
dimred.add_argument(
"-d",
"--dst",
type=str,
default=None,
help=(
"File to store predictions in. "
"The prediction will be stored in the compressed numpy format '.npz'."
),
required=True,
)
dimred.add_argument(
"-n",
"--num-dim",
type=int,
default=2,
help="Number of dimensions to reduce to. Defaults to 2.",
)
def _build_umapNd_subcommand(umapNd: argparse.ArgumentParser) -> None:
umapNd.set_defaults(command="umapNd")
umapNd.add_argument(
"-sred",
"--sred",
type=str,
default=None,
help="Source of the reduction file.",
required=True,
)
umapNd.add_argument(
"-smeta",
"--smeta",
type=str,
default=None,
help="Source of the meta-information file.",
required=True,
)
umapNd.add_argument(
"-sprob",
"--sprob",
type=str,
default=None,
help="Source of the probabilities file.",
required=True,
)
umapNd.add_argument(
"-d",
"--dst",
type=str,
default=None,
help="output directory",
required=True,
)
umapNd.add_argument(
"-n",
"--num-dim",
type=int,
default=None,
help="Number of present reduced dimensions to add to the CSV.",
)
def _build_argparser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="PyTorch Protein Classification")
parser.add_argument("-v", "--verbose", action="store_true")
subparsers = parser.add_subparsers(title="Commands", help="Valid subcommands")
preprocessing = subparsers.add_parser(
"preprocess", help="Preprocess images for Densenet use"
)
_build_preprocessing_subcommand(preprocessing)
prediction = subparsers.add_parser("predict", help="Run Densenet for prediction")
_build_prediction_subcommand(prediction)
dimred = subparsers.add_parser(
"dimred", help="Perform dimensionality reduction on Densenet features"
)
_build_dimred_subcommand(dimred)
umapNd = subparsers.add_parser(
"umapNd", help="Genearates data to plot Nd UMAP"
)
_build_umapNd_subcommand(umapNd)
return parser
def main():
parser = _build_argparser()
args = parser.parse_args()
logger = logging.getLogger(name=constants.LOGGER_NAME)
logger.addHandler(logging.StreamHandler(sys.stdout))
if args.verbose:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.ERROR)
match args.command:
case "preprocess":
logger.info(f"Preprocessing images from {args.src_dir} to {args.dst_dir}")
preprocess.resize_images(
args.src_dir, args.dst_dir, size=args.size, cont=args.cont, num_workers=args.num_workers
)
case "predict":
logger.info(
f"Running prediction for images from {args.src_dir} to {args.dst_dir}"
)
prediction.d121_predict(
args.src_dir, args.dst_dir, args.size, gpus=args.gpu
)
case "dimred":
logger.info(
f"Running dimensionality reduction on {args.src} to be stored in {args.dst}"
)
reduced = dimred.dimred(args.src, args.num_dim)
dimred.store_dimred(reduced, filename=args.dst)
case "umapNd":
logger.info(
f"Running {args.num_dim}d UMAP data generation on {args.sred}/{args.smeta}/{args.sprob} to be stored in {args.dst}"
)
umapNd.generateCSV(args.sred, args.smeta, args.sprob, args.dst, args.num_dim)
if __name__ == "__main__":
main()