Spaces:
Runtime error
Runtime error
File size: 2,112 Bytes
9b19640 9a50b3c 54e731d 9b19640 9a50b3c 9b19640 54e731d 9b19640 54e731d 9b19640 ac676e1 f3ad556 ac676e1 92cd034 ac676e1 54e731d ac676e1 fb8e149 203877e ac676e1 203877e 9b19640 06ed781 9b19640 6eab7f6 acf9b65 9b19640 db81908 203877e acf9b65 c8d2409 9b19640 54e731d 9b19640 | 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 | from icevision.all import *
import PIL
import torch
from torchvision import transforms
import gradio as gr
# Load model
class_map = ClassMap(['selected_variant'])
backbone = faster_rcnn.backbones.resnet_fpn.resnet18(pretrained=True)
model = faster_rcnn.model(backbone=backbone, num_classes=len(class_map))
model.load_state_dict(torch.load('object_localization_full-ancestry.model.pth', map_location=torch.device('cpu')))
#model_type = models.torchvision.faster_rcnn
def predict(model, image, detection_threshold: float = 0.5):
# Whenever you have images in memory (numpy arrays) you can use `Dataset.from_images`
infer_ds = Dataset.from_images([image])
batch, samples = faster_rcnn.build_infer_batch(infer_ds)
preds = faster_rcnn.predict(
model=model,
batch=batch,
detection_threshold=detection_threshold
)
return samples[0]["img"], preds[0]
def show_preds(input_image, detection_threshold=0.5):
img, pred = predict(model=model, image=input_image, detection_threshold=detection_threshold)
# print(pred)
img = draw_pred(img=img, pred=pred, class_map=class_map, display_label=False, display_bbox=True)
img = PIL.Image.fromarray(img)
pred_bbox = pred['bboxes']
pred_score = pred['scores']
# print("Output Image: ", img.size, type(img))
return img, pred_bbox, pred_score
# Populate examples in Gradio interface
examples = [
['1.png'],
['2.png'],
['3.png']
]
description = "An object detection framework to localize regions under post-admixture selection from images of ancestry-painted chromosomes!"
gr_interface = gr.Interface(
fn=show_preds,
inputs=[gr.Image(label="Upload 200x200 B&W image of ancestry-painted chromosomes:"), gr.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")],
outputs=[gr.Image(type="pil"), gr.Textbox(label="Predicted BBox:"), gr.Textbox(label="Predicted BBox Score:")],
title='Detect adaptive variants in admixed populations',
description=description,
examples=examples)
gr_interface.launch(inline=False, share=False, debug=True) |