File size: 3,005 Bytes
71cf8b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
The following is a simple example algorithm.

It is meant to run within a container.

To run it locally, you can call the following bash script:

  ./test_run.sh

This will start the inference and reads from ./test/input and outputs to ./test/output

To save the container and prep it for upload to Grand-Challenge.org you can call:

  ./save.sh

Any container that shows the same behavior will do, this is purely an example of how one COULD do it.

Happy programming!
"""
from pathlib import Path

from glob import glob
from two_stage_inference import inference_one_image
import SimpleITK
import numpy

INPUT_PATH = Path("/input")
OUTPUT_PATH = Path("/output")
RESOURCE_PATH = Path("resources")


def run():
    _show_torch_cuda_info()
    output_path = OUTPUT_PATH / "images/pelvic-fracture-ct-segmentation"
    inference_one_image(input_dir=load_image_dir(location=INPUT_PATH / "images/pelvic-fracture-ct"),
                        output_dir=output_path)

    # # Read the input
    # pelvic_facture_ct = load_image_file_as_array(
    #     location=INPUT_PATH / "images/pelvic-fracture-ct",
    # )
    #
    #
    # # Process the inputs: any way you'd like

    #
    # with open(RESOURCE_PATH / "some_resource.txt", "r") as f:
    #     print(f.read())
    #
    # # For now, let us set make bogus predictions
    # pelvic_fracture_segmentation = numpy.eye(4, 2)
    #
    # # Save your output
    # write_array_as_image_file(
    #     location=OUTPUT_PATH / "images/pelvic-fracture-ct-segmentation",
    #     array=pelvic_fracture_segmentation,
    # )

    return 0


# def load_image_file_as_array(*, location):
#     # Use SimpleITK to read a file
#     input_files = glob(str(location / "*.mha"))
#     result = SimpleITK.ReadImage(input_files[0])
#
#     # Convert it to a Numpy array
#     return SimpleITK.GetArrayFromImage(result)


def load_image_dir(*, location):
    # Use SimpleITK to read a file
    input_files = glob(str(location / "*.mha"))
    result = input_files[0]

    # Convert it to a Numpy array
    return result



def write_array_as_image_file(*, location, array):
    location.mkdir(parents=True, exist_ok=True)

    # You may need to change the suffix to .tiff to match the expected output
    suffix = ".mha"

    image = SimpleITK.GetImageFromArray(array)
    SimpleITK.WriteImage(
        image,
        location / f"output{suffix}",
        useCompression=True,
    )


def _show_torch_cuda_info():
    import torch

    print("=+=" * 10)
    print("Collecting Torch CUDA information")
    print(f"Torch CUDA is available: {(available := torch.cuda.is_available())}")
    if available:
        print(f"\tcuda version: {torch.version.cuda}")
        print(f"\tnumber of devices: {torch.cuda.device_count()}")
        print(f"\tcurrent device: { (current_device := torch.cuda.current_device())}")
        print(f"\tproperties: {torch.cuda.get_device_properties(current_device)}")

    print("=+=" * 10)


if __name__ == "__main__":
    raise SystemExit(run())