-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
308 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import tifffile\n", | ||
"import numpy as np\n", | ||
"import torch\n", | ||
"import os\n", | ||
"import random\n", | ||
"import h5py\n", | ||
"import cv2\n", | ||
"import math\n", | ||
"from scipy.ndimage.interpolation import zoom\n", | ||
"from PIL import Image\n", | ||
"\n", | ||
"from tqdm import tqdm\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"from sr_3dunet.utils.data_utils import random_crop_3d, random_crop_2d, augment_3d, augment_2d, preprocess, get_projection, get_rotated_img, crop_block" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ims_path = '/share/home/wangwb/workspace/sr_3dunet/datasets/cellbody/out_h5/output_res0.h5'\n", | ||
"out_ims_path = '/share/home/wangwb/workspace/sr_3dunet/datasets/cellbody/out_h5/output_res2.h5'\n", | ||
"\n", | ||
"h5 = h5py.File(ims_path, 'r')\n", | ||
"img_total = h5['DataSet']['ResolutionLevel 0']['TimePoint 0']['Channel 0']['Data']\n", | ||
"h5_dir = 'DataSet/ResolutionLevel 2/TimePoint 0/Channel 0/Data'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"step_size = 512\n", | ||
"\n", | ||
"downscale_factor = 4\n", | ||
"\n", | ||
"with h5py.File(out_ims_path, 'w') as f:\n", | ||
" f.create_dataset(h5_dir, shape=img_total.shape, chunks=(1, 128, 128), dtype=img_total.dtype)\n", | ||
"\n", | ||
"len1 = math.ceil(img_total.shape[0]/step_size)\n", | ||
"len2 = math.ceil(img_total.shape[1]/step_size)\n", | ||
"len3 = math.ceil(img_total.shape[2]/step_size)\n", | ||
"pbar1 = tqdm(total=len1*len2*len3, unit='h5_img', desc='inference')\n", | ||
"\n", | ||
"for start_h in range(0, img_total.shape[0], step_size):\n", | ||
" end_h = img_total.shape[0] if start_h+step_size>img_total.shape[0] else start_h+step_size\n", | ||
" for start_w in range(0, img_total.shape[1], step_size):\n", | ||
" end_w = img_total.shape[1] if start_w+step_size>img_total.shape[1] else start_w+step_size\n", | ||
" for start_d in range(0, img_total.shape[2], step_size):\n", | ||
" end_d = img_total.shape[2] if start_d+step_size>img_total.shape[2] else start_d+step_size\n", | ||
" \n", | ||
" new_shape = ((end_h-start_h) // downscale_factor,\n", | ||
" (end_w-start_w) // downscale_factor,\n", | ||
" (end_d-start_d) // downscale_factor)\n", | ||
" \n", | ||
" # img_total[start_h:end_h, start_w:end_w, start_d:end_d].resize(new_shape, resample=Image.BICUBIC)\n", | ||
" new_img = zoom(img_total[start_h:end_h, start_w:end_w, start_d:end_d], zoom = 0.5, order=1)\n", | ||
"\n", | ||
" with h5py.File(out_ims_path, 'r+') as f:\n", | ||
" f[h5_dir][start_h//2:end_h//2, start_w//2:end_w//2, start_d//2:end_d//2] = new_img\n", | ||
"\n", | ||
" pbar1.update(1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"original_matrix = np.random.rand(1000, 1000, 1000)\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"new_shape = (original_matrix.shape[0] // downscale_factor,\n", | ||
" original_matrix.shape[1] // downscale_factor,\n", | ||
" original_matrix.shape[2] // downscale_factor)\n", | ||
"\n", | ||
"new_matrix = np.zeros(new_shape)\n", | ||
"for i in range(new_shape[0]):\n", | ||
" for j in range(new_shape[1]):\n", | ||
" for k in range(new_shape[2]):\n", | ||
" new_matrix[i, j, k] = np.mean(original_matrix[i*downscale_factor:(i+1)*downscale_factor,\n", | ||
" j*downscale_factor:(j+1)*downscale_factor,\n", | ||
" k*downscale_factor:(k+1)*downscale_factor])\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "MPCN", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
|
||
#SBATCH --job-name=inference_cellbody | ||
#SBATCH --nodelist=c001 | ||
#SBATCH --gres=gpu:2 | ||
#SBATCH --ntasks-per-node=2 | ||
#SBATCH --ntasks=2 | ||
#SBATCH --cpus-per-task=16 | ||
|
||
source activate MPCN | ||
|
||
# TODO num_io_consumer half | ||
|
||
# i: Path to a single H5 file | ||
# o: Path to the output single H5 file | ||
# h5_dir: Dictionary of images in the specified H5 file | ||
# model_path: | ||
# piece_size: Determining the size of smaller images | ||
# piece_overlap: Overlap between neighboring small images | ||
|
||
CUDA_VISIBLE_DEVICES=0 python scripts/inference_from_h5.py \ | ||
-i /share/data/VISoR_Reconstruction/SIAT_ION/LiuCiRong/20230910_CJ004/CJ4-1um-ROI1/CJ4ROI1.ims \ | ||
-o datasets/cellbody/out_h5/output_res0.h5 \ | ||
--h5_dir "DataSet/ResolutionLevel 0/TimePoint 0/Channel 0/Data" \ | ||
--model_path weights/MPCN_VISoR_oldbaseline_cellbody_correctproj_net_g_80000.pth \ | ||
--piece_size 128 --piece_overlap 16 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters