Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,807 Bytes
5f093a6 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "9bca0f41",
"metadata": {},
"source": [
"# Simple inference example with CroCo-Stereo or CroCo-Flow"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80653ef7",
"metadata": {},
"outputs": [],
"source": [
"# Copyright (C) 2022-present Naver Corporation. All rights reserved.\n",
"# Licensed under CC BY-NC-SA 4.0 (non-commercial use only)."
]
},
{
"cell_type": "markdown",
"id": "4f033862",
"metadata": {},
"source": [
"First download the model(s) of your choice by running\n",
"```\n",
"bash stereoflow/download_model.sh crocostereo.pth\n",
"bash stereoflow/download_model.sh crocoflow.pth\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fb2e392",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"use_gpu = torch.cuda.is_available() and torch.cuda.device_count()>0\n",
"device = torch.device('cuda:0' if use_gpu else 'cpu')\n",
"import matplotlib.pylab as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0e25d77",
"metadata": {},
"outputs": [],
"source": [
"from stereoflow.test import _load_model_and_criterion\n",
"from stereoflow.engine import tiled_pred\n",
"from stereoflow.datasets_stereo import img_to_tensor, vis_disparity\n",
"from stereoflow.datasets_flow import flowToColor\n",
"tile_overlap=0.7 # recommended value, higher value can be slightly better but slower"
]
},
{
"cell_type": "markdown",
"id": "86a921f5",
"metadata": {},
"source": [
"### CroCo-Stereo example"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64e483cb",
"metadata": {},
"outputs": [],
"source": [
"image1 = np.asarray(Image.open('<path_to_left_image>'))\n",
"image2 = np.asarray(Image.open('<path_to_right_image>'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0d04303",
"metadata": {},
"outputs": [],
"source": [
"model, _, cropsize, with_conf, task, tile_conf_mode = _load_model_and_criterion('stereoflow_models/crocostereo.pth', None, device)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47dc14b5",
"metadata": {},
"outputs": [],
"source": [
"im1 = img_to_tensor(image1).to(device).unsqueeze(0)\n",
"im2 = img_to_tensor(image2).to(device).unsqueeze(0)\n",
"with torch.inference_mode():\n",
" pred, _, _ = tiled_pred(model, None, im1, im2, None, conf_mode=tile_conf_mode, overlap=tile_overlap, crop=cropsize, with_conf=with_conf, return_time=False)\n",
"pred = pred.squeeze(0).squeeze(0).cpu().numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "583b9f16",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(vis_disparity(pred))\n",
"plt.axis('off')"
]
},
{
"cell_type": "markdown",
"id": "d2df5d70",
"metadata": {},
"source": [
"### CroCo-Flow example"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ee257a7",
"metadata": {},
"outputs": [],
"source": [
"image1 = np.asarray(Image.open('<path_to_first_image>'))\n",
"image2 = np.asarray(Image.open('<path_to_second_image>'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5edccf0",
"metadata": {},
"outputs": [],
"source": [
"model, _, cropsize, with_conf, task, tile_conf_mode = _load_model_and_criterion('stereoflow_models/crocoflow.pth', None, device)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b19692c3",
"metadata": {},
"outputs": [],
"source": [
"im1 = img_to_tensor(image1).to(device).unsqueeze(0)\n",
"im2 = img_to_tensor(image2).to(device).unsqueeze(0)\n",
"with torch.inference_mode():\n",
" pred, _, _ = tiled_pred(model, None, im1, im2, None, conf_mode=tile_conf_mode, overlap=tile_overlap, crop=cropsize, with_conf=with_conf, return_time=False)\n",
"pred = pred.squeeze(0).permute(1,2,0).cpu().numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26f79db3",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(flowToColor(pred))\n",
"plt.axis('off')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|