Mercurial > repos > bgruening > bioimage_inference
annotate main.py @ 1:b0f2d3b4a278 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 3564cc70b5eb512031ac122534b5c2ff2e558274
author | bgruening |
---|---|
date | Tue, 03 Sep 2024 09:07:44 +0000 |
parents | caea9ee1ffac |
children |
rev | line source |
---|---|
0
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
1 """ |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
2 Predict images using AI models from BioImage.IO |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
3 """ |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
4 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
5 import argparse |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
6 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
7 import imageio |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
8 import numpy as np |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
9 import torch |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
10 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
11 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
12 def find_dim_order(user_in_shape, input_image): |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
13 """ |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
14 Find the correct order of input image's |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
15 shape. For a few models, the order of input size |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
16 mentioned in the RDF.yaml file is reversed compared |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
17 to the input image's original size. If it is reversed, |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
18 transpose the image to find correct order of image's |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
19 dimensions. |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
20 """ |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
21 image_shape = list(input_image.shape) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
22 # reverse the input shape provided from RDF.yaml file |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
23 correct_order = user_in_shape.split(",")[::-1] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
24 # remove 1s from the original dimensions |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
25 correct_order = [int(i) for i in correct_order if i != "1"] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
26 if (correct_order[0] == image_shape[-1]) and (correct_order != image_shape): |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
27 input_image = torch.tensor(input_image.transpose()) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
28 return input_image, correct_order |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
29 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
30 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
31 if __name__ == "__main__": |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
32 arg_parser = argparse.ArgumentParser() |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
33 arg_parser.add_argument("-im", "--imaging_model", required=True, help="Input BioImage model") |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
34 arg_parser.add_argument("-ii", "--image_file", required=True, help="Input image file") |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
35 arg_parser.add_argument("-is", "--image_size", required=True, help="Input image file's size") |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
36 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
37 # get argument values |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
38 args = vars(arg_parser.parse_args()) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
39 model_path = args["imaging_model"] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
40 input_image_path = args["image_file"] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
41 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
42 # load all embedded images in TIF file |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
43 test_data = imageio.v3.imread(input_image_path, index="...") |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
44 test_data = np.squeeze(test_data) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
45 test_data = test_data.astype(np.float32) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
46 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
47 # assess the correct dimensions of TIF input image |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
48 input_image_shape = args["image_size"] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
49 im_test_data, shape_vals = find_dim_order(input_image_shape, test_data) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
50 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
51 # load model |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
52 model = torch.load(model_path) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
53 model.eval() |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
54 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
55 # find the number of dimensions required by the model |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
56 target_dimension = 0 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
57 for param in model.named_parameters(): |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
58 target_dimension = len(param[1].shape) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
59 break |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
60 current_dimension = len(list(im_test_data.shape)) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
61 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
62 # update the dimensions of input image if the required image by |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
63 # the model is smaller |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
64 slices = tuple(slice(0, s_val) for s_val in shape_vals) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
65 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
66 # apply the slices to the reshaped_input |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
67 im_test_data = im_test_data[slices] |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
68 exp_test_data = torch.tensor(im_test_data) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
69 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
70 # expand input image's dimensions |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
71 for i in range(target_dimension - current_dimension): |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
72 exp_test_data = torch.unsqueeze(exp_test_data, i) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
73 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
74 # make prediction |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
75 pred_data = model(exp_test_data) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
76 pred_data_output = pred_data.detach().numpy() |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
77 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
78 # save original image matrix |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
79 np.save("output_predicted_image_matrix.npy", pred_data_output) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
80 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
81 # post process predicted file to correctly save as TIF file |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
82 pred_data = torch.squeeze(pred_data) |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
83 pred_numpy = pred_data.detach().numpy() |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
84 |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
85 # write predicted TIF image to file |
caea9ee1ffac
planemo upload for repository https://github.com/bgruening/galaxytools/tree/recommendation_training/tools/bioimaging commit 57f46739f4365f59cd52c515bdd3fae2e01b734e
bgruening
parents:
diff
changeset
|
86 imageio.v3.imwrite("output_predicted_image.tif", pred_numpy, extension=".tif") |