Spaces:
Runtime error
Runtime error
File size: 4,572 Bytes
8a3583d |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import cv2\n",
"import codecs\n",
"import torch\n",
"import torchvision.transforms as transforms\n",
"import gradio as gr\n",
"\n",
"from PIL import Image\n",
"\n",
"from unetplusplus import NestedUNet\n",
"\n",
"torch.manual_seed(0)\n",
"\n",
"if torch.cuda.is_available():\n",
" torch.backends.cudnn.deterministic = True\n",
"\n",
"# Device\n",
"DEVICE = \"cpu\"\n",
"print(DEVICE)\n",
"\n",
"# Load color map\n",
"cmap = np.load('cmap.npy')\n",
"\n",
"# Make directories\n",
"os.system(\"mkdir ./models\")\n",
"\n",
"# Get model weights\n",
"if not os.path.exists(\"./models/masksupnyu39.31d.pth\"):\n",
" os.system(\"wget -O ./models/masksupnyu39.31d.pth https://github.com/hasibzunair/masksup-segmentation/releases/download/v0.1/masksupnyu39.31iou.pth\")\n",
"\n",
"# Load model\n",
"model = NestedUNet(num_classes=40)\n",
"checkpoint = torch.load(\"./models/masksupnyu39.31d.pth\")\n",
"model.load_state_dict(checkpoint)\n",
"model = model.to(DEVICE)\n",
"model.eval()\n",
"\n",
"\n",
"# Main inference function\n",
"def inference(img_path):\n",
" image = Image.open(img_path).convert(\"RGB\")\n",
" transforms_image = transforms.Compose(\n",
" [\n",
" transforms.Resize((224, 224)),\n",
" transforms.CenterCrop((224, 224)),\n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n",
" ]\n",
" )\n",
"\n",
" image = transforms_image(image)\n",
" image = image[None, :]\n",
" # Predict\n",
" with torch.no_grad():\n",
" output = torch.sigmoid(model(image.to(DEVICE).float()))\n",
" output = torch.softmax(output, dim=1).argmax(dim=1)[0].float().cpu().numpy().astype(np.uint8)\n",
" pred = cmap[output]\n",
" return pred\n",
"\n",
"# App\n",
"title = \"Masked Supervised Learning for Semantic Segmentation\"\n",
"description = codecs.open(\"description.html\", \"r\", \"utf-8\").read()\n",
"article = \"<p style='text-align: center'><a href='https://arxiv.org/abs/2210.00923' target='_blank'>Masked Supervised Learning for Semantic Segmentation</a> | <a href='https://github.com/hasibzunair/masksup-segmentation' target='_blank'>Github</a></p>\"\n",
"\n",
"gr.Interface(\n",
" inference,\n",
" gr.inputs.Image(type='file', label=\"Input Image\"),\n",
" gr.outputs.Image(type=\"file\", label=\"Predicted Output\"),\n",
" examples=[\"./sample_images/a.png\", \"./sample_images/b.png\", \n",
" \"./sample_images/c.png\", \"./sample_images/d.png\"],\n",
" title=title,\n",
" description=description,\n",
" article=article,\n",
" allow_flagging=False,\n",
" analytics_enabled=False,\n",
" ).launch(debug=True, enable_queue=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.12 ('fifa')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "5a4cff4f724f20f3784f32e905011239b516be3fadafd59414871df18d0dad63"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|