Abid commited on
Commit
f43d01b
1 Parent(s): e706d2b

DVC added and working webapp with inference

Browse files
Files changed (4) hide show
  1. Aptfile +1 -0
  2. DVC-heroku-deployment.md +21 -0
  3. app/app_savta.py +57 -11
  4. requirements.txt +3 -3
Aptfile ADDED
@@ -0,0 +1 @@
 
 
1
+ https://github.com/iterative/dvc/releases/download/2.8.3/dvc_2.8.3_amd64.deb
DVC-heroku-deployment.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ We need to give Heroku the ability to pull in data from DVC upon app start up. We will install a [buildpack](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-apt) that allows the installation of apt-files and then define the Aptfile that contains a path to DVC. I.e., in the CLI run:
2
+
3
+ ```
4
+ heroku buildpacks:add --index 1 heroku-community/apt
5
+ ```
6
+
7
+ Then in your root project folder create a file called `Aptfile` that specifies the release of DVC you want installed, https://github.com/iterative/dvc/releases/download/2.8.3/dvc_2.8.3_amd64.deb
8
+
9
+ Add the following code block to your **streamlit_app.py**:
10
+
11
+ ```python
12
+ import os
13
+
14
+ if "DYNO" in os.environ and os.path.isdir(".dvc"):
15
+ os.system("dvc config core.no_scm true")
16
+ if os.system(f"dvc pull {model} {image}") != 0:
17
+ exit("dvc pull failed")
18
+ os.system("rm -r .dvc .apt/usr/lib/dvc")
19
+ ```
20
+
21
+ Reference: [Heroku ML](https://github.com/GuilhermeBrejeiro/deploy_ML_model_Heroku_FastAPI)
app/app_savta.py CHANGED
@@ -1,9 +1,61 @@
1
  import numpy as np
2
  import torch
3
  import sys
 
4
  from fastai.vision.all import *
5
  import gradio as gr
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  title = "SavtaDepth WebApp"
8
  description = "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."
9
  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>"
@@ -14,22 +66,16 @@ examples = [
14
  ]
15
  favicon = "examples/favicon.ico"
16
  thumbnail = "examples/SavtaDepth.png"
17
- learner = unet_learner(resnet34, path='src/')
18
- learner.load('model')
19
- def sepia(input_img):
20
- sepia_filter = np.array(
21
- [[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]
22
- )
23
- sepia_img = input_img.dot(sepia_filter.T)
24
- sepia_img /= sepia_img.max()
25
- return sepia_img
26
 
 
 
 
27
 
28
 
29
  def main():
30
- iface = gr.Interface(sepia, gr.inputs.Image(shape=(640,480)), "image", title = title, description = description, article = article, examples = examples,theme ="peach",thumbnail=thumbnail)
31
 
32
- iface.launch(favicon_path=favicon,auth=("admin", "dagshubsota123"),server_name="0.0.0.0")
33
  # enable_queue=True,auth=("admin", "pass1234")
34
 
35
  if __name__ == '__main__':
 
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
+ ############## DVC ################################
9
+
10
+ PROD_MODEL_PATH = "src/models"
11
+ TRAIN_PATH = "src/data/processed/train/bathroom"
12
+ TEST_PATH = ""src/data/processed/test/bathroom""
13
+
14
+ if "DYNO" in os.environ and os.path.isdir(".dvc"):
15
+ print("Running DVC")
16
+ os.system("dvc config cache.type copy")
17
+ os.system("dvc config core.no_scm true")
18
+ if os.system(f"dvc pull {PROD_MODEL_PATH} {TRAIN_PATH } {TEST_PATH }") != 0:
19
+ exit("dvc pull failed")
20
+ os.system("rm -r .dvc .apt/usr/lib/dvc")
21
+
22
+
23
+ ############## Inference ##############################
24
+
25
+ class ImageImageDataLoaders(DataLoaders):
26
+ """Basic wrapper around several `DataLoader`s with factory methods for Image to Image problems"""
27
+ @classmethod
28
+ @delegates(DataLoaders.from_dblock)
29
+ def from_label_func(cls, path, filenames, label_func, valid_pct=0.2, seed=None, item_transforms=None,
30
+ batch_transforms=None, **kwargs):
31
+ """Create from list of `fnames` in `path`s with `label_func`."""
32
+ datablock = DataBlock(blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
33
+ get_y=label_func,
34
+ splitter=RandomSplitter(valid_pct, seed=seed),
35
+ item_tfms=item_transforms,
36
+ batch_tfms=batch_transforms)
37
+ res = cls.from_dblock(datablock, filenames, path=path, **kwargs)
38
+ return res
39
+
40
+
41
+ def get_y_fn(x):
42
+ y = str(x.absolute()).replace('.jpg', '_depth.png')
43
+ y = Path(y)
44
+
45
+ return y
46
+
47
+
48
+ def create_data(data_path):
49
+ fnames = get_files(data_path/'train', extensions='.jpg')
50
+ data = ImageImageDataLoaders.from_label_func(data_path/'train', seed=42, bs=4, num_workers=0, filenames=fnames, label_func=get_y_fn)
51
+ return data
52
+
53
+ data = create_data(Path('src/data/processed'))
54
+ learner = unet_learner(data,resnet34, metrics=rmse, wd=1e-2, n_out=3, loss_func=MSELossFlat(), path='src/')
55
+ learner.load('model')
56
+
57
+ ################### Gradio Web APP ################################
58
+
59
  title = "SavtaDepth WebApp"
60
  description = "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."
61
  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>"
 
66
  ]
67
  favicon = "examples/favicon.ico"
68
  thumbnail = "examples/SavtaDepth.png"
 
 
 
 
 
 
 
 
 
69
 
70
+ def sepia(input_img):
71
+
72
+ return PILImageBW.create((learner.predict(input_img))[0]).convert('L')
73
 
74
 
75
  def main():
76
+ iface = gr.Interface(sepia, gr.inputs.Image(shape=(640,480),type='numpy'), "image", title = title, description = description, article = article, examples = examples,theme ="peach",thumbnail=thumbnail)
77
 
78
+ iface.launch(favicon_path=favicon,server_name="0.0.0.0",server_port=8080)
79
  # enable_queue=True,auth=("admin", "pass1234")
80
 
81
  if __name__ == '__main__':
requirements.txt CHANGED
@@ -1,11 +1,11 @@
1
  -f https://download.pytorch.org/whl/cpu/torch_stable.html
2
  #dvc
3
- #fastai
4
  torch==1.10.2+cpu
5
- h5py
6
  opencv-python-headless
7
  tqdm
8
- numpy
9
  #scikit-learn
10
  #dagshub
11
  #tables
 
1
  -f https://download.pytorch.org/whl/cpu/torch_stable.html
2
  #dvc
3
+ fastai
4
  torch==1.10.2+cpu
5
+ #h5py
6
  opencv-python-headless
7
  tqdm
8
+ #numpy
9
  #scikit-learn
10
  #dagshub
11
  #tables