yichen-purdue commited on
Commit
2e7060e
1 Parent(s): 62745e5

update the exmaples

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ **/*.*.pyc
2
+ **/*.pyc
app.py CHANGED
@@ -37,6 +37,7 @@ def resize(img, size):
37
  newh = int(h / w * size)
38
 
39
  resized_img = cv2.resize(img, (neww, newh), interpolation=cv2.INTER_AREA)
 
40
  if len(img.shape) != len(resized_img.shape):
41
  resized_img = resized_img[..., none]
42
 
@@ -62,7 +63,7 @@ def padding_mask(rgba_input: np.array):
62
  :returns: H x W x 4 padded RGBAD
63
 
64
  """
65
- padding = 50
66
  padding_size = 256 - padding * 2
67
 
68
  h, w = rgba_input.shape[:2]
@@ -74,6 +75,7 @@ def padding_mask(rgba_input: np.array):
74
  h_min, h_max = hh.min(), hh.max()
75
  w_min, w_max = ww.min(), ww.max()
76
 
 
77
  # if the area already has enough padding
78
  if h_max - h_min < padding_size and w_max - w_min < padding_size:
79
  return rgba_input
@@ -83,8 +85,12 @@ def padding_mask(rgba_input: np.array):
83
 
84
  padded_rgba = resize(rgba_input, padding_size)
85
  new_h, new_w = padded_rgba.shape[:2]
 
 
 
86
 
87
- padding_output[padding:padding+new_h, padding:padding+new_w, :] = padded_rgba
 
88
 
89
  return padding_output
90
 
@@ -108,6 +114,8 @@ def render_btn_fn(mask, ibl):
108
  mask = mask / 255.0
109
  ibl = ibl/ 255.0
110
 
 
 
111
  # smoothing ibl
112
  ibl = cv2.GaussianBlur(ibl, (11, 11), 0)
113
 
@@ -157,13 +165,16 @@ def gamma_change(x):
157
  ret, shadow = shadow_composite(cur_rgba, cur_shadow, cur_intensity, cur_gamma)
158
  return ret, shadow
159
 
 
 
 
160
 
161
  ibl_h = 128
162
  ibl_w = ibl_h * 2
163
 
164
  with gr.Blocks() as demo:
165
  with gr.Row():
166
- mask_input = gr.Image(shape=(256, 256), image_mode="RGBA", label="Mask")
167
  ibl_input = gr.Sketchpad(shape=(ibl_w, ibl_h), image_mode="L", label="IBL", tool='sketch', invert_colors=True)
168
  output = gr.Image(shape=(256, 256), height=256, width=256, image_mode="RGB", label="Output")
169
  shadow_output = gr.Image(shape=(256, 256), height=256, width=256, image_mode="L", label="Shadow Layer")
@@ -173,6 +184,14 @@ with gr.Blocks() as demo:
173
  gamma_slider = gr.Slider(1.0, 4.0, value=DEFAULT_GAMMA, step=0.1, label="Gamma", info="Gamma correction for shadow")
174
  render_btn = gr.Button(label="Render")
175
 
 
 
 
 
 
 
 
 
176
  render_btn.click(render_btn_fn, inputs=[mask_input, ibl_input], outputs=[output, shadow_output])
177
  intensity_slider.release(intensity_change, inputs=[intensity_slider], outputs=[output, shadow_output])
178
  gamma_slider.release(gamma_change, inputs=[gamma_slider], outputs=[output, shadow_output])
 
37
  newh = int(h / w * size)
38
 
39
  resized_img = cv2.resize(img, (neww, newh), interpolation=cv2.INTER_AREA)
40
+
41
  if len(img.shape) != len(resized_img.shape):
42
  resized_img = resized_img[..., none]
43
 
 
63
  :returns: H x W x 4 padded RGBAD
64
 
65
  """
66
+ padding = 40
67
  padding_size = 256 - padding * 2
68
 
69
  h, w = rgba_input.shape[:2]
 
75
  h_min, h_max = hh.min(), hh.max()
76
  w_min, w_max = ww.min(), ww.max()
77
 
78
+
79
  # if the area already has enough padding
80
  if h_max - h_min < padding_size and w_max - w_min < padding_size:
81
  return rgba_input
 
85
 
86
  padded_rgba = resize(rgba_input, padding_size)
87
  new_h, new_w = padded_rgba.shape[:2]
88
+
89
+ padding_h = (256 - new_h) // 2
90
+ padding_w = (256 - new_w) // 2
91
 
92
+ padding_output[padding_h:padding_h+new_h, padding_w:padding_w+new_w, :] = padded_rgba
93
+ padding_output = np.clip(padding_output, 0.0, 1.0)
94
 
95
  return padding_output
96
 
 
114
  mask = mask / 255.0
115
  ibl = ibl/ 255.0
116
 
117
+ mask = np.clip(mask, 0.0, 1.0)
118
+
119
  # smoothing ibl
120
  ibl = cv2.GaussianBlur(ibl, (11, 11), 0)
121
 
 
165
  ret, shadow = shadow_composite(cur_rgba, cur_shadow, cur_intensity, cur_gamma)
166
  return ret, shadow
167
 
168
+ def update_input(mask):
169
+ return mask
170
+
171
 
172
  ibl_h = 128
173
  ibl_w = ibl_h * 2
174
 
175
  with gr.Blocks() as demo:
176
  with gr.Row():
177
+ mask_input = gr.Image(shape=None, width=256, height=256,image_mode="RGBA", label="RGBA")
178
  ibl_input = gr.Sketchpad(shape=(ibl_w, ibl_h), image_mode="L", label="IBL", tool='sketch', invert_colors=True)
179
  output = gr.Image(shape=(256, 256), height=256, width=256, image_mode="RGB", label="Output")
180
  shadow_output = gr.Image(shape=(256, 256), height=256, width=256, image_mode="L", label="Shadow Layer")
 
184
  gamma_slider = gr.Slider(1.0, 4.0, value=DEFAULT_GAMMA, step=0.1, label="Gamma", info="Gamma correction for shadow")
185
  render_btn = gr.Button(label="Render")
186
 
187
+ with gr.Row():
188
+ gr.Examples(
189
+ examples=[['imgs/woman.png'],['imgs/man.png'], ['imgs/plant1.png'], ['imgs/human2.png'], ['imgs/cloud.png']],
190
+ fn=update_input,
191
+ inputs=[mask_input],
192
+ outputs=mask_input
193
+ )
194
+
195
  render_btn.click(render_btn_fn, inputs=[mask_input, ibl_input], outputs=[output, shadow_output])
196
  intensity_slider.release(intensity_change, inputs=[intensity_slider], outputs=[output, shadow_output])
197
  gamma_slider.release(gamma_change, inputs=[gamma_slider], outputs=[output, shadow_output])
imgs/bike.png ADDED
imgs/cloud.png ADDED
imgs/convert_rgb_alpha.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+
4
+ rgb_file = 'fg-1-rgb.png'
5
+ alpha_file = 'fg-1-alpha.png'
6
+ output_file = 'fg-1-rgba.png'
7
+
8
+ rgb = plt.imread(rgb_file)
9
+ alpha = plt.imread(alpha_file)
10
+
11
+ print(rgb.shape, alpha.shape)
12
+
13
+ rgba = np.concatenate([rgb[..., :3], alpha[..., 0:1]], axis=2)
14
+ plt.imsave(output_file, rgba)
imgs/flower_mask.png ADDED
imgs/human2.png ADDED
imgs/man.png ADDED
imgs/plant1.png ADDED
imgs/test.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+
4
+
5
+ rgb = 'woman.png'
6
+ mask = 'woman_mask.png'
7
+ ofile = 'test1.png'
8
+
9
+ rgb = plt.imread(rgb)
10
+ mask = plt.imread(mask)
11
+
12
+ output = np.concatenate([rgb[..., :3], mask[..., :1]], axis=2)
13
+ plt.imsave(ofile, output)
14
+
imgs/woman.png ADDED
models/Loss/Loss.py CHANGED
@@ -10,7 +10,7 @@ import cv2
10
  # from vgg19_loss import VGG19Loss
11
  # import pytorch_ssim
12
 
13
- from .vgg19_loss import VGG19Loss
14
  from . import pytorch_ssim
15
  from abc import ABC, abstractmethod
16
  from collections import OrderedDict
@@ -96,15 +96,15 @@ class hierarchical_ssim_loss(abs_loss):
96
  return total_loss/b
97
 
98
 
99
- class vgg_loss(abs_loss):
100
- def __init__(self):
101
- self.vgg19_ = VGG19Loss()
102
 
103
 
104
- def loss(self, gt_img, pred_img):
105
- b, c, h, w = gt_img.shape
106
- v = self.vgg19_(gt_img, pred_img, pred_img.device)
107
- return v/b
108
 
109
 
110
  class grad_loss(abs_loss):
 
10
  # from vgg19_loss import VGG19Loss
11
  # import pytorch_ssim
12
 
13
+ # from .vgg19_loss import VGG19Loss
14
  from . import pytorch_ssim
15
  from abc import ABC, abstractmethod
16
  from collections import OrderedDict
 
96
  return total_loss/b
97
 
98
 
99
+ # class vgg_loss(abs_loss):
100
+ # def __init__(self):
101
+ # self.vgg19_ = VGG19Loss()
102
 
103
 
104
+ # def loss(self, gt_img, pred_img):
105
+ # b, c, h, w = gt_img.shape
106
+ # v = self.vgg19_(gt_img, pred_img, pred_img.device)
107
+ # return v/b
108
 
109
 
110
  class grad_loss(abs_loss):
models/SSN.py CHANGED
@@ -97,7 +97,8 @@ class SSN(abs_model):
97
  assert len(x[k].shape) == 2, '{} should be 2D tensor'.format(k)
98
 
99
 
100
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
101
 
102
  mask = torch.tensor(x['mask'])[None, None, ...].float().to(device)
103
  ibl = torch.tensor(x['ibl'])[None, None, ...].float().to(device)
 
97
  assert len(x[k].shape) == 2, '{} should be 2D tensor'.format(k)
98
 
99
 
100
+ # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
101
+ device = torch.device('cpu')
102
 
103
  mask = torch.tensor(x['mask'])[None, None, ...].float().to(device)
104
  ibl = torch.tensor(x['ibl'])[None, None, ...].float().to(device)