File size: 6,909 Bytes
9b7fcdb |
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"#XFeat minimal inference example"
],
"metadata": {
"id": "2tDj94al5GAJ"
}
},
{
"cell_type": "markdown",
"source": [
"## Clone repository"
],
"metadata": {
"id": "X8MPXBro5IFv"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tVkH1ChzNcLW",
"outputId": "da9a9474-76bd-4b66-8ecd-8ba0022f030e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cloning into 'accelerated_features'...\n",
"remote: Enumerating objects: 27, done.\u001b[K\n",
"remote: Counting objects: 100% (11/11), done.\u001b[K\n",
"remote: Compressing objects: 100% (10/10), done.\u001b[K\n",
"remote: Total 27 (delta 0), reused 5 (delta 0), pack-reused 16\u001b[K\n",
"Receiving objects: 100% (27/27), 13.29 MiB | 23.03 MiB/s, done.\n",
"Resolving deltas: 100% (1/1), done.\n",
"/content/accelerated_features\n"
]
}
],
"source": [
"!cd /content && git clone 'https://github.com/verlab/accelerated_features.git'\n",
"%cd /content/accelerated_features"
]
},
{
"cell_type": "markdown",
"source": [
"## Test on simple input (sparse setting)"
],
"metadata": {
"id": "32T-WzfU5NRH"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import os\n",
"import torch\n",
"import tqdm\n",
"\n",
"from modules.xfeat import XFeat\n",
"\n",
"xfeat = XFeat()\n",
"\n",
"#Random input\n",
"x = torch.randn(1,3,480,640)\n",
"\n",
"#Simple inference with batch = 1\n",
"output = xfeat.detectAndCompute(x, top_k = 4096)[0]\n",
"print(\"----------------\")\n",
"print(\"keypoints: \", output['keypoints'].shape)\n",
"print(\"descriptors: \", output['descriptors'].shape)\n",
"print(\"scores: \", output['scores'].shape)\n",
"print(\"----------------\\n\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "o1TMnCEfNfvD",
"outputId": "f59757f5-477a-4642-e955-7a5abefe3c21"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"loading weights from: /content/accelerated_features/modules/../weights/xfeat.pt\n",
"----------------\n",
"keypoints: torch.Size([4096, 2])\n",
"descriptors: torch.Size([4096, 64])\n",
"scores: torch.Size([4096])\n",
"----------------\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Stress test to check FPS on VGA (sparse setting)"
],
"metadata": {
"id": "8b9C09ya5UwL"
}
},
{
"cell_type": "code",
"source": [
"x = torch.randn(1,3,480,640)\n",
"# Stress test\n",
"for i in tqdm.tqdm(range(100), desc=\"Stress test on VGA resolution\"):\n",
"\toutput = xfeat.detectAndCompute(x, top_k = 4096)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Zsjz-QT95ZrM",
"outputId": "2df6f545-419f-4cc3-ad8b-bf5e12741dba"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Stress test on VGA resolution: 100%|ββββββββββ| 100/100 [00:14<00:00, 6.74it/s]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Test with batched mode (sparse)"
],
"metadata": {
"id": "1jAl-ejS5du_"
}
},
{
"cell_type": "code",
"source": [
"# Batched mode\n",
"x = torch.randn(4,3,480,640)\n",
"outputs = xfeat.detectAndCompute(x, top_k = 4096)\n",
"print(\"# detected features on each batch item:\", [len(o['keypoints']) for o in outputs])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lAarS8UH5gyg",
"outputId": "883f13f8-3fac-48f2-c0a3-656a81b57f2c"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"# detected features on each batch item: [4096, 4096, 4096, 4096]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Matches two images with built-in MNN matcher (sparse mode)"
],
"metadata": {
"id": "H60iMAlh5nqP"
}
},
{
"cell_type": "code",
"source": [
"# Match two images with sparse features\n",
"x1 = torch.randn(1,3,480,640)\n",
"x2 = torch.randn(1,3,480,640)\n",
"mkpts_0, mkpts_1 = xfeat.match_xfeat(x1, x2)"
],
"metadata": {
"id": "6N-ZqoMZ5syf"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Matches two images with semi-dense matching, and batched mode (batch size = 4) for demonstration purpose"
],
"metadata": {
"id": "MOV4vZDp5v9_"
}
},
{
"cell_type": "code",
"source": [
"# Create 4 image pairs\n",
"x1 = torch.randn(4,3,480,640)\n",
"x2 = torch.randn(4,3,480,640)\n",
"\n",
"#Obtain matches for each batch item\n",
"matches_list = xfeat.match_xfeat_star(x1, x2, top_k = 5000)\n",
"print('number of img pairs', len(matches_list))\n",
"print(matches_list[0].shape) # -> output is (x1,y1,x2,y2)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Axe0o6U85zGV",
"outputId": "e1257959-24fc-4194-b2f1-ee06cf450b24"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"number of img pairs 4\n",
"torch.Size([182, 4])\n"
]
}
]
}
]
} |