-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.py
More file actions
577 lines (494 loc) · 24.9 KB
/
process.py
File metadata and controls
577 lines (494 loc) · 24.9 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import os
import sys
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
import numpy as np
import yaml
from models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,
increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
from utils.torch_utils import select_device, time_sync
import random
import pandas as pd
import shutil
@torch.no_grad()
def load_yolo_predictor(device=0,
dnn=False,
data="",
weights=[]):
device = select_device(device)
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data)
return model
@torch.no_grad()
def run(
im,
model,
imgsz=(1024, 1024),
conf_thres=0.05,
iou_thres=0.2,
max_det=1000,
classes=None,
agnostic_nms=False,
augment=False,
half=False,
device=0,
visualize=False,
spacing=[1, 1],
image_index=0):
stride, names, pt, jit, onnx, engine = model.stride, model.names, model.pt, model.jit, model.onnx, model.engine
imgsz = check_img_size(imgsz, s=stride) # check image size
bs = 1 # batch_size
model.warmup(imgsz=(1, 3, *imgsz), half=half) # warmup
dt, seen = [0.0, 0.0, 0.0], 0
im = np.transpose(im, (2, 0, 1))
im = torch.from_numpy(im).to(device)
im = im.half() if half else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
pred = model(im, augment=augment, visualize=visualize)
scores = pred[:, :, 4]
if torch.max(scores) >= 0.4:
conf_thres = 0.10
else:
conf_thres = 0.05
if torch.max(scores) >= 0.5:
conf_thres = 0.15
else:
conf_thres = 0.05
if torch.max(scores) >= 0.7:
conf_thres = 0.25
else:
conf_thres = 0.05
if torch.max(scores) <= 0.2:
pred[:, :, 4] = pred[:, :, 4] / 2
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
boxes = []
x_y_spacing = [spacing[0], spacing[1], spacing[0], spacing[1]]
for i, det in enumerate(pred): # per image
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(im.shape[2:], det[:, :4], (1024, 1024)).round()
for *xyxy, conf, cls in reversed(det):
box = {}
box['corners'] = []
bbox = [xyxy[0].item(), xyxy[1].item(), xyxy[2].item(), xyxy[3].item()]
bbox = [bbox[0] * x_y_spacing[0], bbox[1] * x_y_spacing[1],
bbox[2] * x_y_spacing[2], bbox[3] * x_y_spacing[3]]
confidence = conf.item()
if confidence >= 0.8:
confidence = 0.95
bbox = [np.round(bbox[0], 2), np.round(bbox[1], 2),
np.round(bbox[2], 2), np.round(bbox[3], 2)]
bottom_left = [bbox[0], bbox[1], image_index]
bottom_right = [bbox[2], bbox[1], image_index]
top_left = [bbox[0], bbox[3], image_index]
top_right = [bbox[2], bbox[3], image_index]
box['corners'].extend([top_right, top_left, bottom_left, bottom_right])
box['probability'] = np.round(confidence, 2)
boxes.append(box)
return boxes
import os
import numpy as np
import SimpleITK as sitk
def convert_file(mri_file_path, png_file_path, auto_contrast=False):
# NB: only one png output file at a time, so not good if len(mri_file_path)>1
# Check the existence of the MRI file
if len(mri_file_path) != 0:
if type(mri_file_path[0]) == str:
if not os.path.exists(mri_file_path[0]):
raise Exception('Source file "%s" does not exists' % mri_file_path)
# Remove the output png, if non existent
flag_conversion = dicom2image(mri_file_path[0], png_file_path, auto_contrast)
return flag_conversion
def dicom2image(input_file_name, output_file_name, auto_contrast=False):
# Initialize a file reader
if type(input_file_name) == str:
image = sitk.ReadImage(input_file_name)
else:
image = input_file_name
spacing = image.GetSpacing()
# Ensure that, if the image is greyscale, it has only 2 dimensions
# If greyscale rescale the image between 0 and 255 and save it as a Uint8
if image.GetNumberOfComponentsPerPixel() == 1:
image = sitk.RescaleIntensity(image, 0, 255)
image = sitk.Cast(image, sitk.sitkUInt8)
# Get a numpy array from the image
npa = sitk.GetArrayFromImage(image)
# Perform contrast enhancement(numpy array required)
npa = auto_contrast_enhancement(npa)
# If required, perform contrast enhancement
if auto_contrast:
image = sitk.GetImageFromArray(npa)
image = sitk.Cast(image, sitk.sitkUInt8)
# Write the image into the output file of iterest
return image, spacing
def auto_contrast_enhancement(img, min_freq=0.001, null_perc=0.5):
# The image is initially normalized between 0 and 255
p_low = np.min(img)
p_high = np.max(img)
img = (img.astype(float) - p_low) / (p_high - p_low) * 255
# The histogram of the normalized image is computed
hist, bins = np.histogram(img.ravel(), bins=np.arange(0, 256))
# Histogram information is used to automatically identify a lower and upper treshold for the windowing of the grey values
p_low, p_high = optimize_gray_range(hist, bins, min_freq, null_perc)
# The windowing of the grey level is performed
img = np.maximum(img, p_low)
img = np.minimum(img, p_high)
# The selected portion of the histogram is mapped between 0 and 255
img = (img.astype(float) - p_low) / (p_high - p_low) * 255
return img
def optimize_gray_range(hist, bins, min_freq=0.001, null_perc=0.5):
# Get the relative frequencies from the absolute frequencies of the histogram
n = np.sum(hist)
hist = hist / n
# Get the intial number of "empty bins" (relative frequency below the threshold)
non_empty_bins = bins[np.where(hist > min_freq)]
n_empty_tot = len(non_empty_bins)
# Shrink the histogram until the empty bins only become a small percentage of the initial number
n_empty = n_empty_tot
while n_empty / n_empty_tot > null_perc:
non_empty_bins = non_empty_bins[1:-1]
hist_new = hist[non_empty_bins[0]:non_empty_bins[-1]]
n_empty = len(np.where(hist_new > min_freq))
# The lower and upper end of the histogram are the extremes of the non_empty_bins variable
low_ref = non_empty_bins[0]
high_ref = non_empty_bins[-1]
return low_ref, high_ref
def convert_nha_to_png(input_dir, output_dir):
for fname in os.listdir(input_dir):
mri_file_path = os.path.join(input_dir, fname)
if fname.replace(".mha", ".png") not in os.listdir(output_dir):
png_file_path = os.path.join(output_dir, fname.replace(".mha", ".png"))
image, spacing = convert_file([mri_file_path], png_file_path, auto_contrast=True)
sitk.WriteImage(image, png_file_path)
def make_yolo_format_files(input_dir, labels_yolo_dir):
metadata_file = os.path.join(input_dir, 'metadata.csv')
data = pd.read_csv(metadata_file)
images_dir = os.path.join(input_dir, 'images')
for fname in os.listdir(images_dir):
mha_fname = fname.replace(".png", ".mha")
df_in = data[data["img_name"] == mha_fname]
rows = []
for ix_row, row in df_in.iterrows():
if row["label"] != 0:
x_min, y_min, w, h = row["x"], row["y"], row["width"], row["height"]
x_max, y_max = x_min + w, y_min + h
x_c, y_c = (x_min + x_max) / 2, (y_min + y_max) / 2
norm_x_c, norm_y_c = x_c / 1024, y_c / 1024
norm_w, norm_h = w / 1024, h / 1024
text = "0 " + str(norm_x_c) + " " + str(norm_y_c) + " " + str(norm_w) + " " + str(norm_h)
rows.append(text)
if len(rows) != 0:
final_text = "\n".join(rows)
else:
final_text = ""
label_file = os.path.join(labels_yolo_dir, fname.replace(".mha", ".txt"))
with open(label_file, "w") as fhandle:
fhandle.write(final_text)
def make_split_v1(input_dir, image_directory, yaml_dir):
"It assumes that there are more clean images than images with nodules"
percent_nodes = 0.85
ratio_clean_nodules = 1.2
splits = 3
subsplits = 3
metadata_file = os.path.join(input_dir, 'metadata.csv')
data = pd.read_csv(metadata_file)
groups = data.groupby(by="img_name")
nodules = []
cleans = []
for key, group in groups:
if group[group["label"] == 0].shape[0] == 1:
cleans.append(key)
else:
nodules.append(key)
nodule_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in nodules]
clean_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in cleans]
for split_ix in range(splits):
nodule_split = [x for x in nodule_paths if random.uniform(0, 1) <= percent_nodes]
length_nodules = len(nodule_split)
clean_split = [x for x in clean_paths if random.uniform(0, 1) <= percent_nodes] # 1.2 X nodule_numbers
length_clean = len(clean_split)
if length_clean >= length_nodules:
noise = random.uniform(0, 0.1)
should_be_clean_length = len(nodule_split) * (ratio_clean_nodules + noise)
should_be_clean_length = int(should_be_clean_length)
else:
should_be_clean_length = len(clean_split)
nodule_split = random.sample(nodule_split, length_clean)
for subsplit_ix in range(subsplits):
clean_subsplit = random.sample(clean_split, should_be_clean_length)
txt_filename = "train_V1_" + str(split_ix) + str(subsplit_ix) + ".txt"
txt_filename = os.path.join(yaml_dir, txt_filename)
text_train = "\n".join(nodule_split + clean_subsplit)
with open(txt_filename, "w") as fhandle:
fhandle.write(text_train)
yaml_filename = os.path.join(yaml_dir, "nodule_V1_" + str(split_ix) + str(subsplit_ix) + ".yaml")
data_yaml = dict(
train = txt_filename ,
val = txt_filename,
nc = 1,
names = ["nodule"]
)
with open(yaml_filename, 'w') as outfile:
yaml.dump(data_yaml, outfile, default_flow_style=False)
def make_split_v2(input_dir, image_directory, yaml_dir):
"It assumes that there are more clean images than images with nodules"
percent_nodes = 0.85
ratio_clean_nodules = 1.0
splits = 3
subsplits = 3
metadata_file = os.path.join(input_dir, 'metadata.csv')
data = pd.read_csv(metadata_file)
groups = data.groupby(by="img_name")
nodules = []
cleans = []
for key, group in groups:
if group[group["label"] == 0].shape[0] == 1:
cleans.append(key)
else:
nodules.append(key)
nodule_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in nodules]
clean_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in cleans]
for split_ix in range(splits):
nodule_split = [x for x in nodule_paths if random.uniform(0, 1) <= percent_nodes]
length_nodules = len(nodule_split)
clean_split = [x for x in clean_paths if random.uniform(0, 1) <= percent_nodes] # 1.2 X nodule_numbers
length_clean = len(clean_split)
if length_clean >= length_nodules:
noise = random.uniform(0, 0.1)
should_be_clean_length = len(nodule_split) * (ratio_clean_nodules + noise)
should_be_clean_length = int(should_be_clean_length)
else:
should_be_clean_length = len(clean_split)
nodule_split = random.sample(nodule_split, length_clean)
for subsplit_ix in range(subsplits):
clean_subsplit = random.sample(clean_split, should_be_clean_length)
txt_filename = "train_V2_" + str(split_ix) + str(subsplit_ix) + ".txt"
txt_filename = os.path.join(yaml_dir, txt_filename)
text_train = "\n".join(nodule_split + clean_subsplit)
with open(txt_filename, "w") as fhandle:
fhandle.write(text_train)
yaml_filename = os.path.join(yaml_dir, "nodule_V2_" + str(split_ix) + str(subsplit_ix) + ".yaml")
data_yaml = dict(
train = txt_filename ,
val = txt_filename,
nc = 1,
names = ["nodule"]
)
with open(yaml_filename, 'w') as outfile:
yaml.dump(data_yaml, outfile, default_flow_style=False)
def make_split_v3(input_dir, image_directory, yaml_dir):
"It assumes that there are more clean images than images with nodules"
percent_nodes = 1.0
ratio_clean_nodules = 1.2
splits = 3
subsplits = 2
metadata_file = os.path.join(input_dir, 'metadata.csv')
data = pd.read_csv(metadata_file)
groups = data.groupby(by="img_name")
nodules = []
cleans = []
for key, group in groups:
if group[group["label"] == 0].shape[0] == 1:
cleans.append(key)
else:
nodules.append(key)
nodule_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in nodules]
clean_paths = [os.path.join(image_directory, x.replace(".mha", ".png")) for x in cleans]
for split_ix in range(splits):
nodule_split = [x for x in nodule_paths if random.uniform(0, 1) <= 1]
length_nodules = len(nodule_split)
clean_split = [x for x in clean_paths if random.uniform(0, 1) <= 0.85] # 1.2 X nodule_numbers
length_clean = len(clean_split)
if length_clean >= length_nodules:
noise = random.uniform(0, 0.1)
should_be_clean_length = len(nodule_split) * (ratio_clean_nodules + noise)
should_be_clean_length = int(should_be_clean_length)
else:
should_be_clean_length = len(clean_split)
nodule_split = random.sample(nodule_split, length_clean)
for subsplit_ix in range(subsplits):
clean_subsplit = random.sample(clean_split, should_be_clean_length)
txt_filename = "train_V3_" + str(split_ix) + str(subsplit_ix) + ".txt"
txt_filename = os.path.join(yaml_dir, txt_filename)
text_train = "\n".join(nodule_split + clean_subsplit)
with open(txt_filename, "w") as fhandle:
fhandle.write(text_train)
yaml_filename = os.path.join(yaml_dir, "nodule_V3_" + str(split_ix) + str(subsplit_ix) + ".yaml")
data_yaml = dict(
train = txt_filename ,
val = txt_filename,
nc = 1,
names = ["nodule"]
)
with open(yaml_filename, 'w') as outfile:
yaml.dump(data_yaml, outfile, default_flow_style=False)
def train_competition(yaml_dir, epochs):
d = {}
for ix, yaml_file in enumerate(os.listdir(yaml_dir)):
if yaml_file.endswith(".yaml") == False:
continue
if yaml_file.find("_V3_") == -1:
continue
print("Training has started for yaml {}".format(ix))
yaml_path = os.path.join(yaml_dir, yaml_file)
command = "python3 /opt/algorithm/train.py --img 1024 --batch 8 --epochs " + str(epochs) + " --data " + str(yaml_path) + " --weights yolov5l.pt"
print(command)
os.system(command)
if yaml_file.find("_V1_") != -1:
command = "python3 /opt/algorithm/train.py --img 1024 --batch 8 --epochs " + str(epochs) + " --data " + str(yaml_path) + " --weights yolov5x.pt"
print(command)
os.system(command)
def train_ensemble(yaml_dir):
pass
def output_weights(trained_dir, output_dir):
for ix, dir_experiment in enumerate(os.listdir(trained_dir)):
weights_path = os.path.join(trained_dir, dir_experiment, "weights", "last.pt")
print(weights_path)
if os.path.isfile(weights_path) == False:
continue
new_path = os.path.join(output_dir, "experiment_" + str(ix) + ".pt")
print("Copying from {} to {}".format(weights_path, new_path))
shutil.copy(weights_path, new_path)
import SimpleITK
import numpy as np
from pandas import DataFrame
import torch
from evalutils import DetectionAlgorithm
from evalutils.validators import (
UniquePathIndicesValidator,
UniqueImagesValidator,
)
import json
from typing import Dict
import os
import itertools
from pathlib import Path
# This parameter adapts the paths between local execution and execution in docker. You can use this flag to switch between these two modes.
# For building your docker, set this parameter to True. If False, it will run process.py locally for test purposes.
execute_in_docker = True
class Noduledetection(DetectionAlgorithm):
def __init__(self, input_dir, output_dir, train=False, retrain=False, retest=False, epochs=2):
super().__init__(
validators=dict(
input_image=(
UniqueImagesValidator(),
UniquePathIndicesValidator(),
)
),
input_path = Path(input_dir),
output_file = Path(os.path.join(output_dir,'nodules.json'))
)
#------------------------------- LOAD the model here ---------------------------------
self.input_path, self.output_path = input_dir, output_dir
self.epochs = epochs
if retest == False:
self.model_paths = ["/opt/algorithm/yolo1.pt", "/opt/algorithm/yolo2.pt", "/opt/algorithm/yolo3.pt", "/opt/algorithm/yolo4.pt", "/opt/algorithm/yolo5.pt", "/opt/algorithm/yolo6.pt", "/opt/algorithm/yolo7.pt", "/opt/algorithm/yolo8.pt", "/opt/algorithm/yolo9.pt", "/opt/algorithm/yolo1-1.pt", "/opt/algorithm/yolo2-1.pt", "/opt/algorithm/yolo3-1.pt", "/opt/algorithm/yolo4-1.pt", "/opt/algorithm/yolo5-1.pt", "/opt/algorithm/yolo6-1.pt", "/opt/algorithm/yolo7-1.pt", "/opt/algorithm/yolo8-1.pt", "/opt/algorithm/yolo9-1.pt", "/opt/algorithm/yolo1-2.pt", "/opt/algorithm/yolo2-2.pt", "/opt/algorithm/yolo3-2.pt", "/opt/algorithm/yolo4-2.pt", "/opt/algorithm/yolo5-2.pt", "/opt/algorithm/yolo6-2.pt", "/opt/algorithm/yolo7-2.pt", "/opt/algorithm/yolo8-2.pt", "/opt/algorithm/yolo9-2.pt", "/opt/algorithm/yolo1f1.pt", "/opt/algorithm/yolo1f2.pt", "/opt/algorithm/yolo2f1.pt", "/opt/algorithm/yolo2f2.pt", "/opt/algorithm/yolo3f1.pt", "/opt/algorithm/yolo3f2.pt",]
else:
self.model_paths = [os.path.join(input_dir, weight_path) for weight_path in os.listdir(input_dir) if weight_path.endswith("pt")]
self.images_dir = "/opt/algorithm/yolo_dataset/images"
self.labels_yolo_dir = "/opt/algorithm/yolo_dataset/labels"
self.yaml_dir = "/opt/algorithm/yamls"
self.trained_models_dir = "/opt/algorithm/runs/train"
# self.images_dir = "/home/sentic/Documents/data/storage/Madu_stuff/ALL_CODE/Madu/node21/yolo_convenient_2/images"
# self.labels_yolo_dir = "/home/sentic/Documents/data/storage/Madu_stuff/ALL_CODE/Madu/node21/yolo_convenient_2/labels"
# self.yaml_dir = "/home/sentic/Documents/data/storage/Madu_stuff/ALL_CODE/Madu/node21/yamls_2"
# self.trained_models_dir = "/home/sentic/Documents/data/storage/Madu_stuff/ALL_CODE/Madu/node21/runs/train"
if os.path.isdir(self.images_dir) == False:
os.makedirs(self.images_dir)
if os.path.isdir(self.labels_yolo_dir) == False:
os.makedirs(self.labels_yolo_dir)
if os.path.isdir(self.yaml_dir) == False:
os.makedirs(self.yaml_dir)
if os.path.isdir(self.trained_models_dir) == False:
os.makedirs(self.trained_models_dir)
# add path for yaml and txt files
# add path for directory with images (png format) files
# add path for directory with labels file
if train == False:
self.model = load_yolo_predictor(data="/opt/algorithm/nodule01.yaml",
weights=self.model_paths)
def save(self):
with open(str(self._output_file), "w") as f:
json.dump(self._case_results[0], f)
def process_case(self, *, idx, case):
'''
Read the input, perform model prediction and return the results.
The returned value will be saved as nodules.json by evalutils.
process_case method of evalutils
(https://github.com/comic/evalutils/blob/fd791e0f1715d78b3766ac613371c447607e411d/evalutils/evalutils.py#L225)
is overwritten here, so that it directly returns the predictions without changing the format.
'''
# Load and test the image for this case
input_image, input_image_file_path = self._load_input_image(case=case)
# Detect and score candidates
scored_candidates = self.predict(input_image=input_image)
# Write resulting candidates to nodules.json for this case
return scored_candidates
def train(self, num_epochs=1):
input_dir = self.input_path
mha_dir = os.path.join(self.input_path, "images")
images_dir = self.images_dir
labels_yolo_dir = self.labels_yolo_dir
print("Starting the conversion from mha to png")
convert_nha_to_png(mha_dir, images_dir) # done
print("Making the annotations dir")
make_yolo_format_files(input_dir, labels_yolo_dir) # done
make_split_v1(input_dir, images_dir, self.yaml_dir)
make_split_v2(input_dir, images_dir, self.yaml_dir)
make_split_v3(input_dir, images_dir, self.yaml_dir)
train_competition(self.yaml_dir, self.epochs)
output_weights(self.trained_models_dir,
self.output_path)
return 0
def format_to_GC(self, np_predictions, spacing):
return 0
def merge_dict(self, results):
merged_d = {}
for k in results[0].keys():
merged_d[k] = list(itertools.chain(*[d[k] for d in results]))
return merged_d
def predict(self, *, input_image):
mha_path = input_image
results = []
if mha_path.GetDimension() == 2:
image, spacing = convert_file([mha_path], None, auto_contrast=True)
output_file_name = "/opt/algorithm/tmp.png"
sitk.WriteImage(image, output_file_name)
np_rgb_image = cv2.imread(output_file_name)
image_index = 0
output = run(np_rgb_image, model=self.model, augment=True, spacing=spacing, image_index=image_index)
results.append(output, model=self.model, augment=True, spacing=spacing)
os.remove(output_file_name)
elif mha_path.GetDimension() == 3:
num_components = mha_path.GetSize()[2]
for j in range(num_components):
image, spacing = convert_file([mha_path[:, :, j]], None, auto_contrast=True)
output_file_name = "/opt/algorithm/tmp.png"
sitk.WriteImage(image, output_file_name)
np_rgb_image = cv2.imread(output_file_name)
output = run(np_rgb_image, model=self.model, augment=True, spacing=spacing, image_index=j)
results.append(output)
os.remove(output_file_name)
return dict(type="Multiple 2D bounding boxes", boxes=[el for sub_list in results for el in sub_list], version={ "major": 1, "minor": 0 })
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
prog='process.py',
description=
'Reads all images from an input directory and produces '
'results in an output directory')
parser.add_argument('input_dir', help = "input directory to process")
parser.add_argument('output_dir', help = "output directory generate result files in")
parser.add_argument('--train', action='store_true', help = "Algorithm on train mode.")
parser.add_argument('--retrain', action='store_true', help = "Algorithm on retrain mode (loading previous weights).")
parser.add_argument('--retest', action='store_true', help = "Algorithm on evaluate mode after retraining.")
parsed_args = parser.parse_args()
if (parsed_args.train or parsed_args.retrain):# train mode: retrain or train
Noduledetection(parsed_args.input_dir, parsed_args.output_dir, parsed_args.train, parsed_args.retrain, parsed_args.retest).train()
else:# test mode (test or retest)
Noduledetection(parsed_args.input_dir, parsed_args.output_dir, retest=parsed_args.retest).process()