1919
Browse files
app.py
CHANGED
|
@@ -8,48 +8,45 @@ from models.arch.RDnet_ import FullNet_NLP
|
|
| 8 |
from models.arch.classifier import PretrainedConvNext
|
| 9 |
import torchvision.transforms.functional as TF
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
net_i
|
| 20 |
-
|
| 21 |
-
net_c = PretrainedConvNext("convnext_small_in22k")
|
| 22 |
-
net_c.load_state_dict(torch.load('./classifier_32.pt')['icnn'])
|
| 23 |
-
net_c=net_c.to('cpu')
|
| 24 |
-
#net_c=net_c.to('cuda')
|
| 25 |
-
net_i.eval().to('cuda')
|
| 26 |
-
net_c.eval().to('cuda')
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
@spaces.GPU(duration=120)
|
| 35 |
def predict(img):
|
| 36 |
-
|
| 37 |
-
image_tensor = torch.from_numpy(img).permute(2, 0, 1).float().unsqueeze(0)
|
| 38 |
-
h, w = image_tensor.shape[-2], image_tensor.shape[-1]
|
| 39 |
-
h, w = h // 32 * 32, w // 32 * 32
|
| 40 |
-
image_tensor = torch.nn.functional.interpolate(image_tensor, size=(h, w), mode='bilinear')
|
| 41 |
-
ipt=net_c(image_tensor)
|
| 42 |
-
image_tensor = image_tensor.half()
|
| 43 |
-
ipt = ipt.half()
|
| 44 |
-
output_i, output_j=net_i(image_tensor,ipt,prompt=True)
|
| 45 |
-
output_j_out=[]
|
| 46 |
-
for i in range(4):
|
| 47 |
-
out_reflection, out_clean = output_j[i][:, :3, ...], output_j[i][:, 3:, ...]
|
| 48 |
-
output_j_out.append(out_clean)
|
| 49 |
-
output_j_out.append(out_reflection)
|
| 50 |
-
clean = output_j_out[6]
|
| 51 |
-
clean=torch.clamp(clean, 0, 1)
|
| 52 |
-
return clean
|
| 53 |
|
| 54 |
demo=gr.Interface(predict, gr.Image(), "image")
|
| 55 |
|
|
|
|
| 8 |
from models.arch.classifier import PretrainedConvNext
|
| 9 |
import torchvision.transforms.functional as TF
|
| 10 |
|
| 11 |
+
class Pipe:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
channels = [64, 128, 256, 512]
|
| 14 |
+
layers = [2, 2, 4, 2]
|
| 15 |
+
num_subnet = 4
|
| 16 |
+
self.net_i = FullNet_NLP(channels, layers, num_subnet, 4,num_classes=1000, drop_path=0,save_memory=True, inter_supv=True, head_init_scale=None,kernel_size=3)
|
| 17 |
+
for param in self.net_i.parameters():
|
| 18 |
+
param.data = param.data.to(torch.float16)
|
| 19 |
+
self.net_i.load_state_dict(torch.load('./fp16_check.pt')['icnn'])
|
| 20 |
+
self.net_i = self.net_i.to('cpu')
|
| 21 |
+
self.net_c = PretrainedConvNext("convnext_small_in22k")
|
| 22 |
+
self.net_c.load_state_dict(torch.load('./classifier_32.pt')['icnn'])
|
| 23 |
+
self.net_c=self.net_c.to('cpu')
|
| 24 |
+
#net_c=net_c.to('cuda')
|
| 25 |
+
self.net_i.eval().to('cuda')
|
| 26 |
+
self.net_c.eval().to('cuda')
|
| 27 |
+
self.output = None
|
| 28 |
+
|
| 29 |
+
def __call__(self, img):
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
image_tensor = torch.from_numpy(img).permute(2, 0, 1).float().unsqueeze(0)
|
| 32 |
+
h, w = image_tensor.shape[-2], image_tensor.shape[-1]
|
| 33 |
+
h, w = h // 32 * 32, w // 32 * 32
|
| 34 |
+
image_tensor = torch.nn.functional.interpolate(image_tensor, size=(h, w), mode='bilinear')
|
| 35 |
+
ipt=net_c(image_tensor)
|
| 36 |
+
image_tensor = image_tensor.half()
|
| 37 |
+
ipt = ipt.half()
|
| 38 |
+
output_i, output_j=net_i(image_tensor,ipt,prompt=True)
|
| 39 |
+
output_j_out=[]
|
| 40 |
+
clean = output[-1][:, 3:, ...]
|
| 41 |
+
clean=torch.clamp(clean, 0, 1)
|
| 42 |
+
self.output = clean
|
| 43 |
+
|
| 44 |
+
pipe = Pipe()
|
| 45 |
|
| 46 |
|
| 47 |
@spaces.GPU(duration=120)
|
| 48 |
def predict(img):
|
| 49 |
+
return pipe(img).output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
demo=gr.Interface(predict, gr.Image(), "image")
|
| 52 |
|