File size: 1,805 Bytes
6137d3a
0e6708a
 
9177536
760fd59
9177536
 
 
 
 
 
0e6708a
 
 
 
6137d3a
0e6708a
760fd59
 
 
 
0e6708a
 
fb0a5c2
 
 
 
 
 
 
0e6708a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import spaces
import gradio as gr
from util import imread, imsave, get_examples
import torch

def torch_compile(*args, **kwargs):
    def decorator(func):
        return func
    return decorator

torch.compile = torch_compile  # temporary workaround

default_model = 'ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c'


@spaces.GPU
def predict(filename, model=None, device=None, reduce_labels=True):
    from cpn import CpnInterface
    from prep import multi_norm
    from celldetection import label_cmap
    
    global default_model
    assert isinstance(filename, str)

    if device is None:
        if torch.cuda.device_count():
            device = 'cuda'
        else:
            device = 'cpu'
    
    print(dict(
        filename=filename,
        model=model,
        device=device,
        reduce_labels=reduce_labels
    ), flush=True)

    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

    m = CpnInterface(model.strip(), device=device)
    y = m(img, reduce_labels=reduce_labels)

    labels = y['labels']

    vis_labels = label_cmap(labels)
    dst = '.'.join(filename.split('.')[:-1]) + '_labels.tiff'
    imsave(dst, labels)

    return img, vis_labels, dst


gr.Interface(
    predict,
    inputs=[gr.components.Image(label="Upload Input Image", type="filepath"),
            gr.components.Textbox(label='Model Name', value=default_model, max_lines=1)],
    outputs=[gr.Image(label="Processed Image"),
             gr.Image(label="Label Image"),
             gr.File(label="Download Label Image")],
    title="Cell Detection with Contour Proposal Networks",
    examples=get_examples(default_model)
).launch()