geetu040 commited on
Commit
613748c
·
1 Parent(s): eee4e23

upload app

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
37
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
38
+ *.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .gradio
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from model import predict
4
+
5
+ description = """
6
+ - This work is a part of the [DepthPro: Beyond Depth Estimation](https://github.com/geetu040/depthpro-beyond-depth) repository, which further explores this model's capabilities on:
7
+ - Image Segmentation - Human Segmentation
8
+ - Image Super Resolution - 384px to 1536px (4x Upscaling)
9
+ - Image Super Resolution - 256px to 1024px (4x Upscaling)
10
+ """
11
+ examples_dir = "assets/examples/"
12
+ examples = [[os.path.join(examples_dir, filename)] for filename in os.listdir(examples_dir)]
13
+
14
+ interface = gr.Interface(
15
+ fn=predict,
16
+ inputs=gr.Image(type="pil"),
17
+ outputs=gr.Image(type="pil"),
18
+ title="DepthPro: Super Resolution: 384px to 1536px (4x Upscaling)",
19
+ description=description,
20
+ examples=examples,
21
+ )
22
+
23
+ if __name__ == "__main__":
24
+ interface.launch()
assets/examples/girl_praying.jpeg ADDED

Git LFS Details

  • SHA256: 0b9f8e0faad4036cd670df828fe6230884724f76af036b703327fa088c34d625
  • Pointer size: 130 Bytes
  • Size of remote file: 17.1 kB
assets/examples/man_with_arms_open.jpeg ADDED

Git LFS Details

  • SHA256: e00f87c2ec8a754ccced5f135582dc139cfb46825ef2b55bb0d314ffec8f88fb
  • Pointer size: 130 Bytes
  • Size of remote file: 25.3 kB
assets/examples/man_with_camera_in_hand.jpeg ADDED

Git LFS Details

  • SHA256: 581477594e8f30cab2ec7758414912452c07004c9cf9bfb007c21b197e23bc18
  • Pointer size: 130 Bytes
  • Size of remote file: 14.8 kB
assets/examples/myself.jpeg ADDED

Git LFS Details

  • SHA256: b5a0bac5f023e5ed08ae3d026d91c3970478ff5233788d63d9760fb36bdb7286
  • Pointer size: 130 Bytes
  • Size of remote file: 13.9 kB
model.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import torch
3
+ from huggingface_hub import hf_hub_download
4
+ import matplotlib.pyplot as plt
5
+
6
+ # custom installation from this PR: https://github.com/huggingface/transformers/pull/34583
7
+ # !pip install git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers
8
+ from transformers import DepthProConfig, DepthProImageProcessorFast, DepthProForDepthEstimation
9
+
10
+ # load DepthPro model, used as backbone
11
+ config = DepthProConfig(
12
+ patch_size=192,
13
+ patch_embeddings_size=16,
14
+ num_hidden_layers=12,
15
+ intermediate_hook_ids=[11, 8, 7, 5],
16
+ intermediate_feature_dims=[256, 256, 256, 256],
17
+ scaled_images_ratios=[0.5, 1.0],
18
+ scaled_images_overlap_ratios=[0.5, 0.25],
19
+ scaled_images_feature_dims=[1024, 512],
20
+ use_fov_model=False,
21
+ )
22
+ depthpro_for_depth_estimation = DepthProForDepthEstimation(config)
23
+
24
+ # create DepthPro for super resolution
25
+ class DepthProForSuperResolution(torch.nn.Module):
26
+ def __init__(self, depthpro_for_depth_estimation):
27
+ super().__init__()
28
+
29
+ self.depthpro_for_depth_estimation = depthpro_for_depth_estimation
30
+ hidden_size = self.depthpro_for_depth_estimation.config.fusion_hidden_size
31
+
32
+ self.image_head = torch.nn.Sequential(
33
+ torch.nn.ConvTranspose2d(
34
+ in_channels=config.num_channels,
35
+ out_channels=hidden_size,
36
+ kernel_size=4, stride=2, padding=1
37
+ ),
38
+ torch.nn.ReLU(),
39
+ )
40
+
41
+ self.head = torch.nn.Sequential(
42
+ torch.nn.Conv2d(
43
+ in_channels=hidden_size,
44
+ out_channels=hidden_size,
45
+ kernel_size=3, stride=1, padding=1
46
+ ),
47
+ torch.nn.ReLU(),
48
+ torch.nn.ConvTranspose2d(
49
+ in_channels=hidden_size,
50
+ out_channels=hidden_size,
51
+ kernel_size=4, stride=2, padding=1
52
+ ),
53
+ torch.nn.ReLU(),
54
+ torch.nn.Conv2d(
55
+ in_channels=hidden_size,
56
+ out_channels=self.depthpro_for_depth_estimation.config.num_channels,
57
+ kernel_size=3, stride=1, padding=1
58
+ ),
59
+ )
60
+
61
+ def forward(self, pixel_values):
62
+ # x is the low resolution image
63
+ x = pixel_values
64
+ encoder_features = self.depthpro_for_depth_estimation.depth_pro(x).features
65
+ fused_hidden_state = self.depthpro_for_depth_estimation.fusion_stage(encoder_features)[-1]
66
+ x = self.image_head(x)
67
+ x = torch.nn.functional.interpolate(x, size=fused_hidden_state.shape[2:])
68
+ x = x + fused_hidden_state
69
+ x = self.head(x)
70
+ return x
71
+
72
+ # initialize the model
73
+ model = DepthProForSuperResolution(depthpro_for_depth_estimation)
74
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
75
+ model = model.to(device)
76
+
77
+ # load weights
78
+ weights_path = hf_hub_download(repo_id="geetu040/DepthPro_SR_4x_384p", filename="model_weights.pth")
79
+ model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu')))
80
+
81
+ # load image processor
82
+ image_processor = DepthProImageProcessorFast(
83
+ do_resize=True,
84
+ size={"width": 384, "height": 384},
85
+ do_rescale=True,
86
+ do_normalize=True
87
+ )
88
+
89
+ # define crop function to ensure square image
90
+ def crop_image(image):
91
+ """
92
+ Crops the image from the center to make aspect ratio 1:1.
93
+ """
94
+ width, height = image.size
95
+ min_dim = min(width, height)
96
+ left = (width - min_dim) // 2
97
+ top = (height - min_dim) // 2
98
+ right = left + min_dim
99
+ bottom = top + min_dim
100
+ image = image.crop((left, top, right, bottom))
101
+ return image
102
+
103
+
104
+ def predict(image):
105
+ # inference
106
+
107
+ image = crop_image(image)
108
+ image = image.resize((384, 384), Image.Resampling.BICUBIC)
109
+
110
+ # prepare image for the model
111
+ inputs = image_processor(images=image, return_tensors="pt")
112
+ inputs = {k: v.to(device) for k, v in inputs.items()}
113
+
114
+ with torch.no_grad():
115
+ outputs = model(**inputs)
116
+
117
+ # convert tensors to PIL.Image
118
+ output = outputs[0] # extract the first and only batch
119
+ output = output.cpu() # unload from cuda if used
120
+ output = torch.permute(output, (1, 2, 0)) # (C, H, W) -> (H, W, C)
121
+ output = output * 0.5 + 0.5 # undo normalization
122
+ output = output * 255. # undo scaling
123
+ output = output.clip(0, 255.) # fix out of range
124
+ output = output.numpy() # convert to numpy
125
+ output = output.astype('uint8') # convert to PIL.Image compatible format
126
+ output = Image.fromarray(output) # create PIL.Image object
127
+
128
+ return output
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ pillow
4
+ torch
5
+ torchvision
6
+ git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers