Wraps the Adobe Research PIH model (CVPR 2023) as ComfyUI nodes. Includes auto-download of the pretrained checkpoint on first use. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
# Copyright 2023 Adobe. All rights reserved.
|
|
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License. You may obtain a copy
|
|
# of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# Unless required by applicable law or agreed to in writing, software distributed under
|
|
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
# OF ANY KIND, either express or implied. See the License for the specific language
|
|
# governing permissions and limitations under the License.
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
|
|
class IBN(nn.Module):
|
|
r"""Instance-Batch Normalization layer from
|
|
`"Two at Once: Enhancing Learning and Generalization Capacities via IBN-Net"
|
|
<https://arxiv.org/pdf/1807.09441.pdf>`
|
|
Args:
|
|
planes (int): Number of channels for the input tensor
|
|
ratio (float): Ratio of instance normalization in the IBN layer
|
|
"""
|
|
|
|
def __init__(self, planes, ratio=0.5):
|
|
super(IBN, self).__init__()
|
|
self.half = int(planes * ratio)
|
|
self.IN = nn.InstanceNorm2d(self.half, affine=True)
|
|
self.BN = nn.BatchNorm2d(planes - self.half)
|
|
|
|
def forward(self, x):
|
|
split = torch.split(x, self.half, 1)
|
|
out1 = self.IN(split[0].contiguous())
|
|
out2 = self.BN(split[1].contiguous())
|
|
out = torch.cat((out1, out2), 1)
|
|
return out
|
|
|
|
|
|
class SELayer(nn.Module):
|
|
def __init__(self, channel, reduction=16):
|
|
super(SELayer, self).__init__()
|
|
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
|
self.fc = nn.Sequential(
|
|
nn.Linear(channel, int(channel / reduction), bias=False),
|
|
nn.ReLU(inplace=True),
|
|
nn.Linear(int(channel / reduction), channel, bias=False),
|
|
nn.Sigmoid(),
|
|
)
|
|
|
|
def forward(self, x):
|
|
b, c, _, _ = x.size()
|
|
y = self.avg_pool(x).view(b, c)
|
|
y = self.fc(y).view(b, c, 1, 1)
|
|
return x * y.expand_as(x)
|