coderdee commited on
Commit
7407eb1
·
1 Parent(s): 4da34c8
Files changed (1) hide show
  1. app.py +35 -113
app.py CHANGED
@@ -1,115 +1,37 @@
 
 
 
 
1
  import numpy as np
2
- import torch
3
- import sys
4
- import os
5
- from fastai.vision.all import *
6
  import gradio as gr
7
-
8
- ############### HF ###########################
9
-
10
- HF_TOKEN = os.getenv('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')
11
-
12
- hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "savtadepth-flags")
13
-
14
- ############## DVC ################################
15
-
16
- PROD_MODEL_PATH = "src/models"
17
- TRAIN_PATH = "src/data/processed/train/bathroom"
18
- TEST_PATH = "src/data/processed/test/bathroom"
19
-
20
- if os.path.isdir(".dvc"):
21
- print("Running DVC")
22
- os.system("dvc config cache.type copy")
23
- os.system("dvc config core.no_scm true")
24
- if os.system(f"dvc pull {PROD_MODEL_PATH} {TRAIN_PATH } {TEST_PATH }") != 0:
25
- exit("dvc pull failed")
26
- os.system("rm -r .dvc")
27
- # .apt/usr/lib/dvc
28
-
29
- ############## Inference ##############################
30
-
31
-
32
- class ImageImageDataLoaders(DataLoaders):
33
- """Basic wrapper around several `DataLoader`s with factory methods for Image to Image problems"""
34
- @classmethod
35
- @delegates(DataLoaders.from_dblock)
36
- def from_label_func(cls, path, filenames, label_func, valid_pct=0.2, seed=None, item_transforms=None,
37
- batch_transforms=None, **kwargs):
38
- """Create from list of `fnames` in `path`s with `label_func`."""
39
- datablock = DataBlock(blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
40
- get_y=label_func,
41
- splitter=RandomSplitter(valid_pct, seed=seed),
42
- item_tfms=item_transforms,
43
- batch_tfms=batch_transforms)
44
- res = cls.from_dblock(datablock, filenames, path=path, **kwargs)
45
- return res
46
-
47
-
48
- def get_y_fn(x):
49
- y = str(x.absolute()).replace('.jpg', '_depth.png')
50
- y = Path(y)
51
-
52
- return y
53
-
54
-
55
- def create_data(data_path):
56
- fnames = get_files(data_path/'train', extensions='.jpg')
57
- data = ImageImageDataLoaders.from_label_func(
58
- data_path/'train', seed=42, bs=4, num_workers=0, filenames=fnames, label_func=get_y_fn)
59
- return data
60
-
61
-
62
- data = create_data(Path('src/data/processed'))
63
- learner = unet_learner(data, resnet34, metrics=rmse,
64
- wd=1e-2, n_out=3, loss_func=MSELossFlat(), path='src/')
65
- learner.load('model')
66
-
67
-
68
- def gen(input_img):
69
- return PILImageBW.create((learner.predict(input_img))[0]).convert('L')
70
-
71
- ################### Gradio Web APP ################################
72
-
73
-
74
- title = "SavtaDepth WebApp"
75
-
76
- description = """
77
- <p>
78
- <center>
79
- Savta Depth is a collaborative Open Source Data Science project for monocular depth estimation - Turn 2d photos into 3d photos. To test the model and code please check out the link bellow.
80
- <img src="https://huggingface.co/spaces/kingabzpro/savtadepth/resolve/main/examples/cover.png" alt="logo" width="250"/>
81
- </center>
82
- </p>
83
- """
84
- article = "<p style='text-align: center'><a href='https://dagshub.com/OperationSavta/SavtaDepth' target='_blank'>SavtaDepth Project from OperationSavta</a></p><p style='text-align: center'><a href='https://colab.research.google.com/drive/1XU4DgQ217_hUMU1dllppeQNw3pTRlHy1?usp=sharing' target='_blank'>Google Colab Demo</a></p></center></p>"
85
-
86
- examples = [
87
- ["examples/00008.jpg"],
88
- ["examples/00045.jpg"],
89
- ]
90
- favicon = "examples/favicon.ico"
91
- thumbnail = "examples/SavtaDepth.png"
92
-
93
-
94
- def main():
95
- iface = gr.Interface(
96
- gen,
97
- gr.inputs.Image(shape=(640, 480), type='numpy'),
98
- "image",
99
- title=title,
100
- flagging_options=["incorrect", "worst", "ambiguous"],
101
- allow_flagging="manual",
102
- flagging_callback=hf_writer,
103
- description=description,
104
- article=article,
105
- examples=examples,
106
- theme="peach",
107
- allow_screenshot=True
108
- )
109
-
110
- iface.launch(enable_queue=True)
111
- # enable_queue=True,auth=("admin", "pass1234")
112
-
113
-
114
- if __name__ == '__main__':
115
- main()
 
1
+ from layers import BilinearUpSampling2D
2
+ from tensorflow.keras.models import load_model
3
+ from utils import load_images, predict
4
+ import matplotlib.pyplot as plt
5
  import numpy as np
 
 
 
 
6
  import gradio as gr
7
+ from huggingface_hub import from_pretrained_keras
8
+
9
+ custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D,
10
+ 'depth_loss_function': None}
11
+ print('Loading model...')
12
+ model = from_pretrained_keras(
13
+ "keras-io/monocular-depth-estimation", custom_objects=custom_objects, compile=False)
14
+ print('Successfully loaded model...')
15
+ examples = ['examples/00015_colors.png',
16
+ 'examples/00084_colors.png', 'examples/00033_colors.png']
17
+
18
+
19
+ def infer(image):
20
+ inputs = load_images([image])
21
+ outputs = predict(model, inputs)
22
+ plasma = plt.get_cmap('plasma')
23
+ rescaled = outputs[0][:, :, 0]
24
+ rescaled = rescaled - np.min(rescaled)
25
+ rescaled = rescaled / np.max(rescaled)
26
+ image_out = plasma(rescaled)[:, :, :3]
27
+ return image_out
28
+
29
+
30
+ iface = gr.Interface(
31
+ fn=infer,
32
+ title="Monocular Depth Estimation",
33
+ description="Keras Implementation of Unet architecture with Densenet201 backbone for estimating the depth of image 📏",
34
+ inputs=[gr.inputs.Image(label="image", type="numpy", shape=(640, 480))],
35
+ outputs="image",
36
+ article="Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. The ideal based on the keras example from <a href=\"https://keras.io/examples/vision/depth_estimation/\">Victor Basu</a>",
37
+ examples=examples, cache_examples=True).launch(debug=True)