def guide_channel(bimg, device='cpu', threshold=1): ''' Given a binary image (Mask), generating guiding channels using receptive fields ''' if isinstance(device, str): device = torch.device(device) vgg = Vgg16(requires_grad=False).to('cpu') rf = receptive_field(vgg, input_size=(3, bimg.shape[0], bimg.shape[1]), device='cpu') f_size = bimg.shape[0] ft_layer = ['4', '9', '16', '23'] gch = [] for layer in ft_layer: M = torch.zeros(f_size, f_size) for i in range(f_size): for j in range(f_size): ind = receptive_field_for_unit(rf, layer, (i, j)) A = bimg[int(ind[0][0]):int(ind[0][1]), int(ind[1][0]):int(ind[1][1])] #print(A.mean()) #if A.min() > 0: if A.mean() >= threshold: M[i, j] = 1 if M.min() > 0: M = M / (M**2).sum() gch.append(M.to(device)) ### f_size = int(f_size / 2) return gch
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1) def forward(self, x): y = self.conv(x) y = self.bn(y) y = self.relu(y) y = self.maxpool(y) y = self.avgpool(y) return y device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0 model = Net().to(device) receptive_field_dict = receptive_field(model, (3, 256, 256)) receptive_field_for_unit(receptive_field_dict, "2", (1, 1))
import torch import torch.nn as nn from torch_receptive_field import receptive_field, receptive_field_for_unit class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv = nn.Sequential( nn.Conv1d(3, 6, 3, dilation=1), nn.ReLU(), nn.Conv1d(6, 6, 3, dilation=2), nn.ReLU(), nn.Conv1d(6, 6, 3, dilation=3), ) self.relu = nn.ReLU(inplace=True) def forward(self, x): y = self.conv(x) y = self.relu(y) return y device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0 model = Net().to(device) receptive_field_dict = receptive_field(model, (3, 100)) # receptive_field_for_unit(receptive_field_dict, "2", (1, 1)) print(receptive_field_dict["1"]["r"])
import torch import torch.nn as nn import torch.nn.functional as F from torch_receptive_field import receptive_field from model import GGGAN, GGGGAN, SNDIS, ResDense if __name__ == '__main__': device = torch.device('cuda') model = GGGAN.Discriminator().to(device) if True: receptive_field(model, (2, 640, 480)) else: receptive_field_dict = receptive_field(model, (2, 640, 480)) receptive_field_for_unit(receptive_field_dict, "2", (2, 2))
import torch.nn.functional as F from torch_receptive_field import receptive_field class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) def forward(self, x): y = self.conv(x) y = self.bn(y) y = self.relu(y) y = self.maxpool(y) return y device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0 model = Net().to(device) receptive_field(model, (3, 256, 256))
def get_receptive_field_dict(net, input_shape=(1, 128, 128)): receptive_field_dict = None receptive_field_dict = receptive_field(net, input_shape) return receptive_field_dict