|
| 1 | +import json |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +from cellpose import core, denoise, io, models |
| 6 | +from pathlib import Path |
| 7 | +from tqdm import trange |
| 8 | +from natsort import natsorted |
| 9 | + |
| 10 | +io.logger_setup() # run this to get printing of progress |
| 11 | + |
| 12 | +# Check if colab notebook instance has GPU access |
| 13 | +if core.use_gpu() is False: |
| 14 | + raise ImportError("No GPU access, change your runtime") |
| 15 | + |
| 16 | +model = models.CellposeModel(gpu=True) |
| 17 | + |
| 18 | +# *** change to your google drive folder path *** |
| 19 | +cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet" |
| 20 | +input_dir = os.path.join(cochlea_dir, "AnnotatedImageCrops/F1ValidationSGNs/for_consensus_annotation") |
| 21 | +out_dir = os.path.join(cochlea_dir, "predictions/val_sgn/cellpose3") |
| 22 | + |
| 23 | +input_dir = Path(input_dir) |
| 24 | +if not input_dir.exists(): |
| 25 | + raise FileNotFoundError("directory does not exist") |
| 26 | + |
| 27 | +# *** change to your image extension *** |
| 28 | +image_ext = ".tif" |
| 29 | + |
| 30 | +# list all files |
| 31 | +files = natsorted([f for f in input_dir.glob("*"+image_ext) if "_masks" not in f.name and "_flows" not in f.name]) |
| 32 | + |
| 33 | +if len(files) == 0: |
| 34 | + raise FileNotFoundError("no image files found, did you specify the correct folder and extension?") |
| 35 | +else: |
| 36 | + print(f"{len(files)} images in folder:") |
| 37 | + |
| 38 | +for f in files: |
| 39 | + print(f.name) |
| 40 | + |
| 41 | +flow_threshold = 0.4 |
| 42 | +cellprob_threshold = 0.0 |
| 43 | +tile_norm_blocksize = 0 |
| 44 | + |
| 45 | +masks_ext = ".png" if image_ext == ".png" else ".tif" |
| 46 | +for i in trange(len(files)): |
| 47 | + f = files[i] |
| 48 | + start = time.perf_counter() |
| 49 | + |
| 50 | + img = io.imread(f) |
| 51 | + |
| 52 | + basename = "".join(f.name.split(".")[:-1]) |
| 53 | + out_path = os.path.join(out_dir, f"{basename}_seg.tif") |
| 54 | + timer_output = os.path.join(out_dir, f"{basename}_timer.json") |
| 55 | + |
| 56 | + io.logger_setup() # run this to get printing of progress |
| 57 | + |
| 58 | + # DEFINE CELLPOSE MODEL |
| 59 | + # model_type="cyto3" or "nuclei", or other model |
| 60 | + # restore_type: "denoise_cyto3", "deblur_cyto3", "upsample_cyto3", "denoise_nuclei", "deblur_nuclei" |
| 61 | + model = denoise.CellposeDenoiseModel(gpu=True, model_type="cyto3", restore_type="denoise_cyto3") |
| 62 | + |
| 63 | + diameter = 20 |
| 64 | + |
| 65 | + masks, flows, styles, imgs_dn = model.eval(img, diameter=diameter, channels=[0, 0]) |
| 66 | + |
| 67 | + # masks, flows, styles = model.eval(img, batch_size=32, flow_threshold=flow_threshold, |
| 68 | + # cellprob_threshold=cellprob_threshold, |
| 69 | + # normalize={"tile_norm_blocksize": tile_norm_blocksize}) |
| 70 | + |
| 71 | + io.imsave(out_path, masks) |
| 72 | + |
| 73 | + duration = time.perf_counter() - start |
| 74 | + time_dict = {"total_duration[s]": duration} |
| 75 | + with open(timer_output, "w") as f: |
| 76 | + json.dump(time_dict, f, indent='\t', separators=(',', ': ')) |
0 commit comments