Spaces:
Sleeping
Sleeping
Update app/app_savta.py
Browse files- app/app_savta.py +126 -80
app/app_savta.py
CHANGED
|
@@ -1,106 +1,152 @@
|
|
| 1 |
-
import torch
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastai.vision.all import *
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
| 10 |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "savtadepth-flags-V2")
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
PROD_MODEL_PATH = "src/models"
|
| 15 |
TRAIN_PATH = "src/data/processed/train/bathroom"
|
| 16 |
TEST_PATH = "src/data/processed/test/bathroom"
|
| 17 |
|
| 18 |
-
if
|
| 19 |
print("Running DVC")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
os.system("rm -r .dvc")
|
| 25 |
-
# .apt/usr/lib/dvc
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
class ImageImageDataLoaders(DataLoaders):
|
| 30 |
-
"""
|
|
|
|
| 31 |
@classmethod
|
| 32 |
@delegates(DataLoaders.from_dblock)
|
| 33 |
-
def from_label_func(
|
| 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 |
title = "SavtaDepth WebApp"
|
| 67 |
|
| 68 |
-
description =
|
| 69 |
-
|
| 70 |
-
<center>
|
| 71 |
-
Savta Depth is a collaborative Open
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
</p>
|
| 75 |
-
"""
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
examples = [
|
| 79 |
["examples/00008.jpg"],
|
| 80 |
["examples/00045.jpg"],
|
| 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 |
-
if __name__ == '__main__':
|
| 106 |
-
main()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
from fastai.vision.all import *
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
######################
|
| 9 |
+
# Hugging Face flags #
|
| 10 |
+
######################
|
| 11 |
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
+
# `HuggingFaceDatasetSaver` is still available in Gradio ≥ 5
|
| 14 |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "savtadepth-flags-V2")
|
| 15 |
|
| 16 |
+
############
|
| 17 |
+
# DVC #
|
| 18 |
+
############
|
| 19 |
|
| 20 |
PROD_MODEL_PATH = "src/models"
|
| 21 |
TRAIN_PATH = "src/data/processed/train/bathroom"
|
| 22 |
TEST_PATH = "src/data/processed/test/bathroom"
|
| 23 |
|
| 24 |
+
if Path(".dvc").is_dir():
|
| 25 |
print("Running DVC")
|
| 26 |
+
if os.system(f"dvc pull {PROD_MODEL_PATH} {TRAIN_PATH} {TEST_PATH}") != 0:
|
| 27 |
+
raise SystemExit("dvc pull failed")
|
| 28 |
+
# remove DVC metadata to avoid accidental reuse in the Space
|
| 29 |
+
os.system("rm -rf .dvc")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
#######################
|
| 32 |
+
# Data & Learner #
|
| 33 |
+
#######################
|
| 34 |
|
| 35 |
class ImageImageDataLoaders(DataLoaders):
|
| 36 |
+
"""Wrapper to create DataLoaders for image→image tasks."""
|
| 37 |
+
|
| 38 |
@classmethod
|
| 39 |
@delegates(DataLoaders.from_dblock)
|
| 40 |
+
def from_label_func(
|
| 41 |
+
cls,
|
| 42 |
+
path: Path,
|
| 43 |
+
filenames,
|
| 44 |
+
label_func,
|
| 45 |
+
valid_pct: float = 0.2,
|
| 46 |
+
seed: int | None = None,
|
| 47 |
+
item_transforms=None,
|
| 48 |
+
batch_transforms=None,
|
| 49 |
+
**kwargs,
|
| 50 |
+
):
|
| 51 |
+
dblock = DataBlock(
|
| 52 |
+
blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
|
| 53 |
+
get_y=label_func,
|
| 54 |
+
splitter=RandomSplitter(valid_pct, seed=seed),
|
| 55 |
+
item_tfms=item_transforms,
|
| 56 |
+
batch_tfms=batch_transforms,
|
| 57 |
+
)
|
| 58 |
+
return cls.from_dblock(dblock, filenames, path=path, **kwargs)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_y_fn(x: Path) -> Path:
|
| 62 |
+
"""Map an RGB image path to its depth‑map counterpart."""
|
| 63 |
+
return Path(str(x).replace(".jpg", "_depth.png"))
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def create_data(data_path: Path):
|
| 67 |
+
fnames = get_files(data_path / "train", extensions=".jpg")
|
| 68 |
+
return ImageImageDataLoaders.from_label_func(
|
| 69 |
+
data_path / "train",
|
| 70 |
+
seed=42,
|
| 71 |
+
bs=4,
|
| 72 |
+
num_workers=0,
|
| 73 |
+
filenames=fnames,
|
| 74 |
+
label_func=get_y_fn,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
data = create_data(Path("src/data/processed"))
|
| 79 |
+
learner = unet_learner(
|
| 80 |
+
data,
|
| 81 |
+
resnet34,
|
| 82 |
+
metrics=rmse,
|
| 83 |
+
wd=1e-2,
|
| 84 |
+
n_out=3,
|
| 85 |
+
loss_func=MSELossFlat(),
|
| 86 |
+
path="src/",
|
| 87 |
+
)
|
| 88 |
+
learner.load("model")
|
| 89 |
+
|
| 90 |
+
#####################
|
| 91 |
+
# Inference Logic #
|
| 92 |
+
#####################
|
| 93 |
+
|
| 94 |
+
def predict_depth(input_img: PILImage) -> PILImageBW:
|
| 95 |
+
"""Generate a single‑channel depth prediction from an RGB image."""
|
| 96 |
+
depth, *_ = learner.predict(input_img)
|
| 97 |
+
return PILImageBW.create(depth).convert("L")
|
| 98 |
+
|
| 99 |
+
#####################
|
| 100 |
+
# Gradio UI #
|
| 101 |
+
#####################
|
| 102 |
|
| 103 |
title = "SavtaDepth WebApp"
|
| 104 |
|
| 105 |
+
description = (
|
| 106 |
+
"""
|
| 107 |
+
<p style="text-align:center;">
|
| 108 |
+
Savta Depth is a collaborative Open‑Source project for monocular depth estimation – turn 2‑D photos into 3‑D. 🏞️<br>
|
| 109 |
+
Try the model below or explore the resources.
|
| 110 |
+
<br><img src="https://huggingface.co/spaces/kingabzpro/savtadepth/resolve/main/examples/cover.png" width="250"/>
|
| 111 |
+
</p>
|
| 112 |
+
"""
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
article = (
|
| 116 |
+
"""
|
| 117 |
+
<p style='text-align:center'>
|
| 118 |
+
<a href='https://dagshub.com/OperationSavta/SavtaDepth' target='_blank'>Project on DAGsHub</a> •
|
| 119 |
+
<a href='https://colab.research.google.com/drive/1XU4DgQ217_hUMU1dllppeQNw3pTRlHy1?usp=sharing' target='_blank'>Google Colab Demo</a>
|
| 120 |
+
<br/>
|
| 121 |
+
<img src='https://visitor-badge.glitch.me/badge?page_id=kingabzpro/savtadepth' alt='visitor badge'/>
|
| 122 |
+
</p>
|
| 123 |
+
"""
|
| 124 |
+
)
|
| 125 |
|
| 126 |
examples = [
|
| 127 |
["examples/00008.jpg"],
|
| 128 |
["examples/00045.jpg"],
|
| 129 |
]
|
| 130 |
+
|
| 131 |
+
# Modern Gradio components (v5+)
|
| 132 |
+
input_component = gr.Image(shape=(640, 480), image_mode="RGB", label="Input RGB")
|
| 133 |
+
output_component = gr.Image(image_mode="L", label="Predicted Depth")
|
| 134 |
+
|
| 135 |
+
with gr.Blocks(title=title) as demo:
|
| 136 |
+
gr.Markdown(description)
|
| 137 |
+
gr.Markdown(article)
|
| 138 |
+
|
| 139 |
+
gr.Interface(
|
| 140 |
+
fn=predict_depth,
|
| 141 |
+
inputs=input_component,
|
| 142 |
+
outputs=output_component,
|
| 143 |
+
allow_flagging="manual",
|
| 144 |
+
flagging_options=["incorrect", "worst", "ambiguous"],
|
| 145 |
+
flagging_callback=hf_writer,
|
| 146 |
+
examples=examples,
|
| 147 |
+
cache_examples=False, # use live inference for examples
|
| 148 |
+
theme=gr.themes.Soft(),
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.queue(concurrency_count=3).launch()
|
|
|
|
|
|