Oliver Hahn
commited on
Commit
·
1dc26c7
1
Parent(s):
1091308
add demo
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- app.py +76 -0
- modules/.DS_Store +0 -0
- modules/__pycache__/clustering.cpython-310.pyc +0 -0
- modules/__pycache__/clustering.cpython-311.pyc +0 -0
- modules/__pycache__/clustering.cpython-36.pyc +0 -0
- modules/__pycache__/clustering.cpython-38.pyc +0 -0
- modules/__pycache__/clustering.cpython-39.pyc +0 -0
- modules/__pycache__/crf.cpython-310.pyc +0 -0
- modules/__pycache__/crf.cpython-311.pyc +0 -0
- modules/__pycache__/crf.cpython-36.pyc +0 -0
- modules/__pycache__/crf.cpython-37.pyc +0 -0
- modules/__pycache__/crf.cpython-38.pyc +0 -0
- modules/__pycache__/crf.cpython-39.pyc +0 -0
- modules/__pycache__/ema.cpython-311.pyc +0 -0
- modules/__pycache__/ema.cpython-36.pyc +0 -0
- modules/__pycache__/ema.cpython-38.pyc +0 -0
- modules/__pycache__/ema.cpython-39.pyc +0 -0
- modules/__pycache__/gansbeke_batched_crf.cpython-36.pyc +0 -0
- modules/__pycache__/maskprop.cpython-310.pyc +0 -0
- modules/__pycache__/maskprop.cpython-36.pyc +0 -0
- modules/__pycache__/maskprop.cpython-38.pyc +0 -0
- modules/__pycache__/maskprop.cpython-39.pyc +0 -0
- modules/__pycache__/median_pool.cpython-310.pyc +0 -0
- modules/__pycache__/median_pool.cpython-311.pyc +0 -0
- modules/__pycache__/median_pool.cpython-36.pyc +0 -0
- modules/__pycache__/median_pool.cpython-37.pyc +0 -0
- modules/__pycache__/median_pool.cpython-39.pyc +0 -0
- modules/__pycache__/metrics.cpython-311.pyc +0 -0
- modules/__pycache__/metrics.cpython-36.pyc +0 -0
- modules/__pycache__/pamr.cpython-36.pyc +0 -0
- modules/__pycache__/parser.cpython-311.pyc +0 -0
- modules/__pycache__/parser.cpython-36.pyc +0 -0
- modules/__pycache__/primaps.cpython-310.pyc +0 -0
- modules/__pycache__/primaps.cpython-311.pyc +0 -0
- modules/__pycache__/primaps.cpython-36.pyc +0 -0
- modules/__pycache__/primaps.cpython-37.pyc +0 -0
- modules/__pycache__/transforms.cpython-310.pyc +0 -0
- modules/__pycache__/transforms.cpython-311.pyc +0 -0
- modules/__pycache__/transforms.cpython-36.pyc +0 -0
- modules/__pycache__/transforms.cpython-37.pyc +0 -0
- modules/__pycache__/visualization.cpython-310.pyc +0 -0
- modules/__pycache__/visualization.cpython-311.pyc +0 -0
- modules/__pycache__/visualization.cpython-36.pyc +0 -0
- modules/__pycache__/visualization.cpython-37.pyc +0 -0
- modules/backbone/__pycache__/__init__.cpython-36.pyc +0 -0
- modules/backbone/__pycache__/dinovit.cpython-36.pyc +0 -0
- modules/backbone/__pycache__/resnet.cpython-36.pyc +0 -0
- modules/backbone/dino/__pycache__/dinovit.cpython-310.pyc +0 -0
- modules/backbone/dino/__pycache__/dinovit.cpython-311.pyc +0 -0
- modules/backbone/dino/__pycache__/dinovit.cpython-36.pyc +0 -0
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from argparse import ArgumentParser
|
| 2 |
+
from typing import Dict
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import modules.transforms as transforms
|
| 6 |
+
from modules.primaps import PriMaPs
|
| 7 |
+
from modules.backbone.dino.dinovit import DinoFeaturizerv2
|
| 8 |
+
from modules.visualization import visualize_demo
|
| 9 |
+
import gradio as gr
|
| 10 |
+
# set seeds
|
| 11 |
+
torch.manual_seed(0)
|
| 12 |
+
torch.cuda.manual_seed(0)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def gradio_primaps(image_path, threshold, architecture):
|
| 17 |
+
'''
|
| 18 |
+
Gradio demo to visualize PriMaPs for a single image.
|
| 19 |
+
'''
|
| 20 |
+
|
| 21 |
+
device='cuda:0'
|
| 22 |
+
resize_to = 320 if 'v2' not in architecture else 322
|
| 23 |
+
patch_size = 8 if 'v2' not in architecture else 14
|
| 24 |
+
|
| 25 |
+
# get SLL image encoder and primaps module
|
| 26 |
+
net = DinoFeaturizerv2(architecture, patch_size)
|
| 27 |
+
net.to(device)
|
| 28 |
+
primaps_module = PriMaPs(threshold=threshold,
|
| 29 |
+
ignore_id=255)
|
| 30 |
+
|
| 31 |
+
# get transforms
|
| 32 |
+
demo_transforms = transforms.Compose([transforms.ToTensor(),
|
| 33 |
+
transforms.Resize(resize_to),
|
| 34 |
+
transforms.CenterCrop([resize_to, resize_to]),
|
| 35 |
+
transforms.Normalize()])
|
| 36 |
+
|
| 37 |
+
# load image and apply transforms
|
| 38 |
+
image = Image.open(image_path)
|
| 39 |
+
image, _ = demo_transforms(image, torch.zeros(image.size))
|
| 40 |
+
image.to(device)
|
| 41 |
+
# get SSL features
|
| 42 |
+
feats = net(image.unsqueeze(0).to(device), n=1).squeeze()
|
| 43 |
+
# get primaps pseudo labels
|
| 44 |
+
primaps = primaps_module._get_pseudo(image, feats, torch.zeros(image.shape[1:]))
|
| 45 |
+
# visualize overlay
|
| 46 |
+
return visualize_demo(image, primaps)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
# Example image paths
|
| 51 |
+
example_images = [
|
| 52 |
+
"assets/demo_examples/cityscapes_example.png",
|
| 53 |
+
"assets/demo_examples/coco_example.jpg",
|
| 54 |
+
"assets/demo_examples/potsdam_example.png"
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
# Gradio interface
|
| 58 |
+
interface = gr.Interface(
|
| 59 |
+
fn=gradio_primaps,
|
| 60 |
+
inputs=[
|
| 61 |
+
gr.Image(type="filepath", label="Image"),
|
| 62 |
+
gr.Slider(0.0, 1.0, step=0.05, value=0.35, label="Threshold"),
|
| 63 |
+
gr.Dropdown(choices=['dino_vits', 'dino_vitb', 'dinov2_vits', 'dinov2_vitb'], value='dino_vitb', label="SSL Features"),
|
| 64 |
+
],
|
| 65 |
+
outputs=gr.Image(label="PriMaPs"),
|
| 66 |
+
title="PriMaPs Demo",
|
| 67 |
+
description="Upload an image and adjust the threshold to visualize PriMaPs.",
|
| 68 |
+
examples=[
|
| 69 |
+
[example_images[0], 0.35, 'dino_vitb'],
|
| 70 |
+
[example_images[1], 0.35, 'dino_vitb'],
|
| 71 |
+
[example_images[2], 0.35, 'dino_vitb']
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Launch the app
|
| 76 |
+
interface.launch(debug=True)
|
modules/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
modules/__pycache__/clustering.cpython-310.pyc
ADDED
|
Binary file (2.16 kB). View file
|
|
|
modules/__pycache__/clustering.cpython-311.pyc
ADDED
|
Binary file (3.09 kB). View file
|
|
|
modules/__pycache__/clustering.cpython-36.pyc
ADDED
|
Binary file (1.7 kB). View file
|
|
|
modules/__pycache__/clustering.cpython-38.pyc
ADDED
|
Binary file (2.14 kB). View file
|
|
|
modules/__pycache__/clustering.cpython-39.pyc
ADDED
|
Binary file (2.9 kB). View file
|
|
|
modules/__pycache__/crf.cpython-310.pyc
ADDED
|
Binary file (2.17 kB). View file
|
|
|
modules/__pycache__/crf.cpython-311.pyc
ADDED
|
Binary file (4.18 kB). View file
|
|
|
modules/__pycache__/crf.cpython-36.pyc
ADDED
|
Binary file (2.12 kB). View file
|
|
|
modules/__pycache__/crf.cpython-37.pyc
ADDED
|
Binary file (2.14 kB). View file
|
|
|
modules/__pycache__/crf.cpython-38.pyc
ADDED
|
Binary file (1.46 kB). View file
|
|
|
modules/__pycache__/crf.cpython-39.pyc
ADDED
|
Binary file (2.16 kB). View file
|
|
|
modules/__pycache__/ema.cpython-311.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
modules/__pycache__/ema.cpython-36.pyc
ADDED
|
Binary file (5.82 kB). View file
|
|
|
modules/__pycache__/ema.cpython-38.pyc
ADDED
|
Binary file (5.85 kB). View file
|
|
|
modules/__pycache__/ema.cpython-39.pyc
ADDED
|
Binary file (5.82 kB). View file
|
|
|
modules/__pycache__/gansbeke_batched_crf.cpython-36.pyc
ADDED
|
Binary file (2.14 kB). View file
|
|
|
modules/__pycache__/maskprop.cpython-310.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
modules/__pycache__/maskprop.cpython-36.pyc
ADDED
|
Binary file (2.85 kB). View file
|
|
|
modules/__pycache__/maskprop.cpython-38.pyc
ADDED
|
Binary file (13.5 kB). View file
|
|
|
modules/__pycache__/maskprop.cpython-39.pyc
ADDED
|
Binary file (2.86 kB). View file
|
|
|
modules/__pycache__/median_pool.cpython-310.pyc
ADDED
|
Binary file (2.04 kB). View file
|
|
|
modules/__pycache__/median_pool.cpython-311.pyc
ADDED
|
Binary file (3.64 kB). View file
|
|
|
modules/__pycache__/median_pool.cpython-36.pyc
ADDED
|
Binary file (2.01 kB). View file
|
|
|
modules/__pycache__/median_pool.cpython-37.pyc
ADDED
|
Binary file (2.01 kB). View file
|
|
|
modules/__pycache__/median_pool.cpython-39.pyc
ADDED
|
Binary file (2.03 kB). View file
|
|
|
modules/__pycache__/metrics.cpython-311.pyc
ADDED
|
Binary file (6.92 kB). View file
|
|
|
modules/__pycache__/metrics.cpython-36.pyc
ADDED
|
Binary file (3.02 kB). View file
|
|
|
modules/__pycache__/pamr.cpython-36.pyc
ADDED
|
Binary file (4.45 kB). View file
|
|
|
modules/__pycache__/parser.cpython-311.pyc
ADDED
|
Binary file (8.34 kB). View file
|
|
|
modules/__pycache__/parser.cpython-36.pyc
ADDED
|
Binary file (3.76 kB). View file
|
|
|
modules/__pycache__/primaps.cpython-310.pyc
ADDED
|
Binary file (2.82 kB). View file
|
|
|
modules/__pycache__/primaps.cpython-311.pyc
ADDED
|
Binary file (6.67 kB). View file
|
|
|
modules/__pycache__/primaps.cpython-36.pyc
ADDED
|
Binary file (2.77 kB). View file
|
|
|
modules/__pycache__/primaps.cpython-37.pyc
ADDED
|
Binary file (2.77 kB). View file
|
|
|
modules/__pycache__/transforms.cpython-310.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
modules/__pycache__/transforms.cpython-311.pyc
ADDED
|
Binary file (19 kB). View file
|
|
|
modules/__pycache__/transforms.cpython-36.pyc
ADDED
|
Binary file (10.6 kB). View file
|
|
|
modules/__pycache__/transforms.cpython-37.pyc
ADDED
|
Binary file (10.6 kB). View file
|
|
|
modules/__pycache__/visualization.cpython-310.pyc
ADDED
|
Binary file (11.8 kB). View file
|
|
|
modules/__pycache__/visualization.cpython-311.pyc
ADDED
|
Binary file (31.1 kB). View file
|
|
|
modules/__pycache__/visualization.cpython-36.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
modules/__pycache__/visualization.cpython-37.pyc
ADDED
|
Binary file (11.6 kB). View file
|
|
|
modules/backbone/__pycache__/__init__.cpython-36.pyc
ADDED
|
Binary file (234 Bytes). View file
|
|
|
modules/backbone/__pycache__/dinovit.cpython-36.pyc
ADDED
|
Binary file (2.4 kB). View file
|
|
|
modules/backbone/__pycache__/resnet.cpython-36.pyc
ADDED
|
Binary file (7.85 kB). View file
|
|
|
modules/backbone/dino/__pycache__/dinovit.cpython-310.pyc
ADDED
|
Binary file (1.7 kB). View file
|
|
|
modules/backbone/dino/__pycache__/dinovit.cpython-311.pyc
ADDED
|
Binary file (3.47 kB). View file
|
|
|
modules/backbone/dino/__pycache__/dinovit.cpython-36.pyc
ADDED
|
Binary file (1.63 kB). View file
|
|
|