Spaces:
Runtime error
Runtime error
File size: 2,621 Bytes
60f558e 9324d64 198a05b 60f558e 9324d64 60f558e b57be36 198a05b 60f558e 1553854 60f558e 1553854 60f558e 1553854 60f558e 1553854 60f558e |
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 |
import os
os.system('pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html')
os.system('pip install torch==1.9.0 torchvision==0.10.0')
import gradio as gr
# check pytorch installation:
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
assert torch.__version__.startswith("1.9") # please manually install torch 1.9 if Colab changes its default version
import detectron2
from detectron2.utils.logger import setup_logger
import numpy as np
import os, json, random
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from PIL import Image
from pathlib import Path
from matplotlib import pyplot as plt
cfg = get_cfg()
cfg.MODEL.DEVICE='cpu'
cfg.INPUT.MASK_FORMAT='bitmask'
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 3
cfg.TEST.DETECTIONS_PER_IMAGE = 1000
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
cfg.MODEL.WEIGHTS = "model_final.pth"
predictor = DefaultPredictor(cfg)
def inference(img):
class_names = ['0','1','2'] #['astro', 'cort', 'sh-sy5y']
im = np.asarray(Image.open(img).convert('RGB'))
outputs = predictor(im)
pred_classes = outputs['instances'].pred_classes.cpu().numpy().tolist()
take = outputs['instances'].scores >= 0.5 #Threshold
pred_masks = outputs['instances'].pred_masks[take].cpu().numpy()
pred_class = max(set(pred_classes), key=pred_classes.count)
mask = np.stack(pred_masks)
mask = np.any(mask == 1, axis=0)
p = plt.imshow(im,cmap='gray')
p = plt.imshow(mask, alpha=0.4)
p = plt.xticks(fontsize=8)
p = plt.yticks(fontsize=8)
p = plt.title("cell type: " + class_names[pred_class])
return plt
title = "Sartorius Cell Instance Segmentation"
description = "Sartorius Cell Instance Segmentation Demo: Current Kaggle competition - kaggle.com/c/sartorius-cell-instance-segmentation"
article = "<p style='text-align: center'><a href='https://ai.facebook.com/blog/-detectron2-a-pytorch-based-modular-object-detection-library-/' target='_blank'>Detectron2: A PyTorch-based modular object detection library</a> | <a href='https://github.com/facebookresearch/detectron2' target='_blank'>Github Repo</a></p>"
examples = [['0030fd0e6378.png']]
gr.Interface(inference, inputs=gr.inputs.Image(type="filepath"), outputs=gr.outputs.Image('plot') ,enable_queue=True, title=title,
description=description,
article=article,
examples=examples).launch(debug=False)
|