1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "private_outputs" : true ,
7+ "provenance" : [],
8+ "gpuType" : " T4"
9+ },
10+ "kernelspec" : {
11+ "name" : " python3" ,
12+ "display_name" : " Python 3"
13+ },
14+ "language_info" : {
15+ "name" : " python"
16+ },
17+ "accelerator" : " GPU"
18+ },
19+ "cells" : [
20+ {
21+ "cell_type" : " markdown" ,
22+ "source" : [
23+ " # **MitoEM Benchmark**\n " ,
24+ " \n " ,
25+ " This notebook aims to produce a reproducable benchmark for the [Connectomics MitoEM tutorial](https://connectomics.readthedocs.io/en/latest/tutorials/mito.html). Both evaluation data and a pre-trained benchmark are provided for the user. In this notebook, due to resource limitations, we perform inference on ten slices of data."
26+ ],
27+ "metadata" : {
28+ "id" : " cEtYbrL25jrh"
29+ }
30+ },
31+ {
32+ "cell_type" : " markdown" ,
33+ "source" : [
34+ " ## (1) Install dependencies and fetch prepared data"
35+ ],
36+ "metadata" : {
37+ "id" : " HXmj6mY8Iv6Y"
38+ }
39+ },
40+ {
41+ "cell_type" : " code" ,
42+ "execution_count" : null ,
43+ "metadata" : {
44+ "id" : " -qQ1kkxYSaso"
45+ },
46+ "outputs" : [],
47+ "source" : [
48+ " # install Connectomics and dependencies\n " ,
49+ " %%capture\n " ,
50+ " ! pip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu113\n " ,
51+ " ! git clone https://github.com/zudi-lin/pytorch_connectomics.git\n " ,
52+ " %cd pytorch_connectomics/\n " ,
53+ " ! pip install --editable .\n " ,
54+ " ! pip install numpy\" <1.24\" "
55+ ]
56+ },
57+ {
58+ "cell_type" : " code" ,
59+ "source" : [
60+ " # fetch training data from Hugging Face along with a small amount of cleanup\n " ,
61+ " ! mkdir -p datasets/MitoEM\n " ,
62+ " ! wget -q -O datasets/MitoEM/EM30-R-im.zip --show-progress https://huggingface.co/datasets/pytc/EM30/resolve/main/EM30-R-im.zip?download=true\n " ,
63+ " ! unzip -q datasets/MitoEM/EM30-R-im.zip -d datasets/MitoEM/EM30-R-im\n " ,
64+ " ! rm -r datasets/MitoEM/EM30-R-im/__MACOSX\n " ,
65+ " ! rm datasets/MitoEM/EM30-R-im.zip\n " ,
66+ " ! wget -q -O datasets/MitoEM/mito_val.zip --show-progress https://huggingface.co/datasets/pytc/MitoEM/resolve/main/EM30-R-mito-train-val-v2.zip?download=true\n " ,
67+ " ! unzip -q datasets/MitoEM/mito_val.zip -d datasets/MitoEM/EM30-R-val\n " ,
68+ " ! rm datasets/MitoEM/mito_val.zip\n " ,
69+ " \n " ,
70+ " # fetch pre-trained model weights from Hugging Face\n " ,
71+ " ! mkdir -p outputs/MitoEM\n " ,
72+ " ! wget -q -O outputs/MitoEM/mito_u3d-bc_mitoem_300k.pth.tar --show-progress https://huggingface.co/pytc/mito/resolve/main/mito_u3d-bc_mitoem_300k.pth.tar?download=true"
73+ ],
74+ "metadata" : {
75+ "id" : " AkoIDRs6gpdf"
76+ },
77+ "execution_count" : null ,
78+ "outputs" : []
79+ },
80+ {
81+ "cell_type" : " markdown" ,
82+ "source" : [
83+ " ## (2) Specify model inference parameters\n " ,
84+ " The provided model configuration that comes in the [Connectomics Github repo](https://github.com/zudi-lin/pytorch_connectomics.git) must be modified to accomodate the resource limitations of Colab. Important configurations for inference include:\n " ,
85+ " \n " ,
86+ " * SYSTEM.NUM_GPUS: the number of GPUs that are available for inference\n " ,
87+ " * DATASET.INPUT_PATH: the location that images are stored\n " ,
88+ " * INFERENCE.IMAGE_NAME: the images that one is running inference on\n " ,
89+ " * INFERNCE.OUTPUT_PATH: the location that the results will be located in"
90+ ],
91+ "metadata" : {
92+ "id" : " piJNEA6yJAAK"
93+ }
94+ },
95+ {
96+ "cell_type" : " code" ,
97+ "source" : [
98+ " base_yaml = \"\"\" SYSTEM:\n " ,
99+ " NUM_CPUS: 1\n " ,
100+ " NUM_GPUS: 1\n " ,
101+ " MODEL:\n " ,
102+ " ARCHITECTURE: unet_plus_3d\n " ,
103+ " BLOCK_TYPE: residual_se\n " ,
104+ " INPUT_SIZE: [17, 225, 225]\n " ,
105+ " OUTPUT_SIZE: [17, 225, 225]\n " ,
106+ " IN_PLANES: 1\n " ,
107+ " NORM_MODE: sync_bn\n " ,
108+ " FILTERS: [32, 64, 96, 128, 160]\n " ,
109+ " DATASET:\n " ,
110+ " IMAGE_NAME: [\" im_train.json\" ]\n " ,
111+ " LABEL_NAME: [\" mito_train.json\" ]\n " ,
112+ " INPUT_PATH: datasets/MitoEM/EM30-R-im/im\n " ,
113+ " OUTPUT_PATH: outputs/MitoEM-R/\n " ,
114+ " PAD_SIZE: [4, 64, 64]\n " ,
115+ " SOLVER:\n " ,
116+ " LR_SCHEDULER_NAME: WarmupCosineLR\n " ,
117+ " BASE_LR: 0.04\n " ,
118+ " ITERATION_STEP: 1\n " ,
119+ " ITERATION_SAVE: 5000\n " ,
120+ " ITERATION_TOTAL: 150000\n " ,
121+ " SAMPLES_PER_BATCH: 2\n " ,
122+ " INFERENCE:\n " ,
123+ " INPUT_SIZE: [10, 1024, 1024]\n " ,
124+ " OUTPUT_SIZE: [10, 1024, 1024]\n " ,
125+ " IMAGE_NAME: imstack_400_410.tif\n " ,
126+ " OUTPUT_PATH: outputs/MitoEM/EM30-R-im/results/\n " ,
127+ " OUTPUT_NAME: result # will automatically save to HDF5\n " ,
128+ " PAD_SIZE: [0, 0, 0]\n " ,
129+ " AUG_MODE: mean\n " ,
130+ " AUG_NUM: None\n " ,
131+ " STRIDE: [1, 513, 513]\n " ,
132+ " SAMPLES_PER_BATCH: 1\"\"\"\n " ,
133+ " \n " ,
134+ " bc_yaml = \"\"\" MODEL:\n " ,
135+ " OUT_PLANES: 2\n " ,
136+ " TARGET_OPT: [\" 0\" , \" 4-1-1\" ]\n " ,
137+ " LOSS_OPTION:\n " ,
138+ " - - WeightedBCEWithLogitsLoss\n " ,
139+ " - DiceLoss\n " ,
140+ " - - WeightedBCEWithLogitsLoss\n " ,
141+ " - DiceLoss\n " ,
142+ " LOSS_WEIGHT: [[1.0, 0.5], [1.0, 0.5]]\n " ,
143+ " WEIGHT_OPT: [[\" 1\" , \" 0\" ], [\" 1\" , \" 0\" ]]\n " ,
144+ " OUTPUT_ACT: [[\" none\" , \" sigmoid\" ], [\" none\" , \" sigmoid\" ]]\n " ,
145+ " INFERENCE:\n " ,
146+ " OUTPUT_ACT: [\" sigmoid\" , \" sigmoid\" ]\"\"\"\n " ,
147+ " \n " ,
148+ " with open('configs/MitoEM/MitoEM-R-Base.yaml', 'w') as fp:\n " ,
149+ " fp.write(base_yaml)\n " ,
150+ " \n " ,
151+ " with open('configs/MitoEM/MitoEM-R-BC.yaml', 'w') as fp:\n " ,
152+ " fp.write(bc_yaml)\n "
153+ ],
154+ "metadata" : {
155+ "id" : " s4nO2B1etbH7"
156+ },
157+ "execution_count" : null ,
158+ "outputs" : []
159+ },
160+ {
161+ "cell_type" : " code" ,
162+ "source" : [
163+ " # move data around such that the configuration above points to the right data\n " ,
164+ " from PIL import Image\n " ,
165+ " import tifffile\n " ,
166+ " import numpy as np\n " ,
167+ " \n " ,
168+ " imstack = np.zeros([10, 4096, 4096])\n " ,
169+ " for idx in range(400, 410):\n " ,
170+ " imstack[idx-400] = np.array(Image.open(f'datasets/MitoEM/EM30-R-im/im/im{idx:04}.png'))\n " ,
171+ " tifffile.imwrite('datasets/MitoEM/EM30-R-im/im/imstack_400_410.tif', imstack)"
172+ ],
173+ "metadata" : {
174+ "id" : " E0Tk6gdP0U78"
175+ },
176+ "execution_count" : null ,
177+ "outputs" : []
178+ },
179+ {
180+ "cell_type" : " markdown" ,
181+ "source" : [
182+ " ## (3) Model infence"
183+ ],
184+ "metadata" : {
185+ "id" : " DRzRysVYK1_m"
186+ }
187+ },
188+ {
189+ "cell_type" : " code" ,
190+ "source" : [
191+ " ! time python -u scripts/main.py --config-base configs/MitoEM/MitoEM-R-Base.yaml --config-file configs/MitoEM/MitoEM-R-BC.yaml --inference --checkpoint outputs/MitoEM/mito_u3d-bc_mitoem_300k.pth.tar"
192+ ],
193+ "metadata" : {
194+ "id" : " WJTmH2LVdaBs" ,
195+ "collapsed" : true
196+ },
197+ "execution_count" : null ,
198+ "outputs" : []
199+ },
200+ {
201+ "cell_type" : " markdown" ,
202+ "source" : [
203+ " ## (4) Evaluation\n " ,
204+ " Evaluation is done using code from the [mAP_3Dvolume Github repo master branch](https://github.com/ygCoconut/mAP_3Dvolume/tree/master), which is where the MitoEM Grand Challenge also performs evaluation."
205+ ],
206+ "metadata" : {
207+ "id" : " kGNKOeO-K_E1"
208+ }
209+ },
210+ {
211+ "cell_type" : " code" ,
212+ "source" : [
213+ " # import dependencies\n " ,
214+ " import glob\n " ,
215+ " import h5py\n " ,
216+ " import itertools\n " ,
217+ " import numpy as np\n " ,
218+ " from scipy import ndimage\n " ,
219+ " from connectomics.data.utils import readvol, writeh5\n " ,
220+ " from connectomics.utils.process import bc_watershed"
221+ ],
222+ "metadata" : {
223+ "id" : " 5NDORNjo7H6I"
224+ },
225+ "execution_count" : null ,
226+ "outputs" : []
227+ },
228+ {
229+ "cell_type" : " code" ,
230+ "source" : [
231+ " # perform watershed processing on the data (currently represented as semantic/countour segmentations) to retrieve mitochondria instance segmentation\n " ,
232+ " data = np.array(h5py.File(\" outputs/MitoEM/EM30-R-im/results/result.h5\" )['vol0'])\n " ,
233+ " connected = bc_watershed(data, thres1=0.85, thres2=0.6, thres3=0.8, thres_small=512)\n " ,
234+ " with h5py.File(\" outputs/MitoEM/EM30-R-im/results/watershed.h5\" , \" w\" ) as fp:\n " ,
235+ " fp.create_dataset('main', data=connected.astype(np.uint16))"
236+ ],
237+ "metadata" : {
238+ "id" : " 6uSZ0THHI2_z"
239+ },
240+ "execution_count" : null ,
241+ "outputs" : []
242+ },
243+ {
244+ "cell_type" : " code" ,
245+ "source" : [
246+ " # prepare validation data for evaluation\n " ,
247+ " files = [f\" datasets/MitoEM/EM30-R-val/mito-val-v2/seg{idx:04}.tif\" for idx in range(400, 410)]\n " ,
248+ " data = []\n " ,
249+ " for file in files:\n " ,
250+ " data.append(tifffile.imread(file))\n " ,
251+ " data = np.array(data)\n " ,
252+ " writeh5(\" outputs/MitoEM/EM30-R-im/results/validation_gt.h5\" , data)"
253+ ],
254+ "metadata" : {
255+ "id" : " FfMDYhcpJCJo"
256+ },
257+ "execution_count" : null ,
258+ "outputs" : []
259+ },
260+ {
261+ "cell_type" : " code" ,
262+ "source" : [
263+ " # fetch and execute validation scripts\n " ,
264+ " ! wget -q --show-progress https://raw.githubusercontent.com/ygCoconut/mAP_3Dvolume/master/demo.py\n " ,
265+ " ! wget -q --show-progress https://raw.githubusercontent.com/ygCoconut/mAP_3Dvolume/master/vol3d_eval.py\n " ,
266+ " ! wget -q --show-progress https://raw.githubusercontent.com/ygCoconut/mAP_3Dvolume/master/vol3d_util.py\n " ,
267+ " ! python demo.py -gt outputs/MitoEM/EM30-R-im/results/validation_gt.h5 -p outputs/MitoEM/EM30-R-im/results/watershed.h5"
268+ ],
269+ "metadata" : {
270+ "id" : " ePJhmUZI3xee"
271+ },
272+ "execution_count" : null ,
273+ "outputs" : []
274+ },
275+ {
276+ "cell_type" : " code" ,
277+ "source" : [
278+ " # show performance stats\n " ,
279+ " # more data is available at pytorch_connectomics/map_output_match_fn.txt and pytorch_connectomics/map_output_match_p.txt\n " ,
280+ " ! cat map_output_map.txt"
281+ ],
282+ "metadata" : {
283+ "id" : " ZbkzUEhoSjYs"
284+ },
285+ "execution_count" : null ,
286+ "outputs" : []
287+ },
288+ {
289+ "cell_type" : " code" ,
290+ "source" : [],
291+ "metadata" : {
292+ "id" : " wycBG-owqTmD"
293+ },
294+ "execution_count" : null ,
295+ "outputs" : []
296+ }
297+ ]
298+ }
0 commit comments