import spaces import gradio as gr from util import imread, imsave, copy_skimage_data import torch from PIL import Image, ImageDraw import numpy as np from os.path import join def torch_compile(*args, **kwargs): def decorator(func): return func return decorator torch.compile = torch_compile # temporary workaround default_model = 'ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c' default_score_thresh = .9 default_nms_thresh = np.round(np.pi / 10, 4) default_samples = 128 default_order = 5 examples_dir = 'examples' copy_skimage_data(examples_dir) examples = [ [join(examples_dir, 'bbbc039_test_00014.png'), 'ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c', False, default_score_thresh, False, default_nms_thresh, True, 64, True], [join(examples_dir, 'coins.png'), 'ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c', False, default_score_thresh, False, default_nms_thresh, True, 64, True], [join(examples_dir, 'cell.png'), 'ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c', False, default_score_thresh, False, default_nms_thresh, True, 64, True], ] @spaces.GPU def predict( filename, model=None, enable_score_threshold=False, score_threshold=.9, enable_nms_threshold=False, nms_threshold=0.3141592653589793, enable_samples=False, samples=128, use_label_channels=False, enable_order=False, order=5, device=None, ): from cpn import CpnInterface from prep import multi_norm from celldetection import label_cmap, to_h5, data, __version__ global default_model assert isinstance(filename, str) if device is None: if torch.cuda.device_count(): device = 'cuda' else: device = 'cpu' meta = dict( cd_version=__version__, filename=str(filename), model=model, device=device, use_label_channels=use_label_channels, enable_score_threshold=enable_score_threshold, score_threshold=float(score_threshold), enable_order=enable_order, order=order, enable_nms_threshold=enable_nms_threshold, nms_threshold=float(nms_threshold), ) print(meta, flush=True) raw = img = imread(filename) print('Image:', img.dtype, img.shape, (img.min(), img.max()), flush=True) if model is None or len(str(model)) <= 0: model = default_model img = multi_norm(img, 'cstm-mix') # TODO kw = {} if enable_score_threshold: kw['score_thresh'] = score_threshold if enable_nms_threshold: kw['nms_thresh'] = nms_threshold if enable_order: kw['order'] = order if enable_samples: kw['samples'] = samples m = CpnInterface(model.strip(), device=device, **kw) y = m(img, reduce_labels=not use_label_channels) dst_h5 = '.'.join(filename.split('.')[:-1]) + '.h5' to_h5( dst_h5, inputs=img, **y, attributes=dict(inputs=meta) ) labels = y['labels'] vis_labels = label_cmap(labels) dst_csv = '.'.join(filename.split('.')[:-1]) + '.csv' data.labels2property_table( labels, "label", "area", "feret_diameter_max", "bbox", "centroid", "convex_area", "eccentricity", "equivalent_diameter", "extent", "filled_area", "major_axis_length", "minor_axis_length", "orientation", "perimeter", "solidity", "mean_intensity", "max_intensity", "min_intensity", intensity_image=raw ).to_csv(dst_csv) return vis_labels, img, dst_h5, dst_csv with gr.Blocks(title='Cell Segmentation with Contour Proposal Networks') as app: with gr.Row(): gr.Markdown("