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)