GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — deepfeatinterp/torch_models.py at master · paulu/deepfeatinterp Expand file tree/ Copy pathtorch_models.pyMore file actions 222 lines (185 loc) · 7.96 KB / Copy pathtorch_models.pyFile metadata and controls222 lines (185 loc) · 7.96 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 import torch import torchvision as tv import torch.nn as nn import torch.nn.functional as F from torch.autograd import Variable from collections import OrderedDict model_urls = { 'vgg19g': 'https://www.dropbox.com/s/cecy6wtjy97wt3d/vgg19g-4aff041b.pth?dl=1', } class Vgg19g(nn.Module): def __init__(self, pretrained=True): super(Vgg19g, self).__init__() self.features_1 = nn.Sequential(OrderedDict([ ('conv1_1', nn.Conv2d(3, 64, kernel_size = 3, padding = 1)), ('relu1_1', nn.ReLU(inplace = True)), ('conv1_2', nn.Conv2d(64, 64, kernel_size = 3, padding = 1)), ('relu1_2', nn.ReLU(inplace = True)), ('pool1', nn.MaxPool2d(2, 2)), ('conv2_1', nn.Conv2d(64, 128, kernel_size = 3, padding = 1)), ('relu2_1', nn.ReLU(inplace = True)), ('conv2_2', nn.Conv2d(128, 128, kernel_size = 3, padding = 1)), ('relu2_2', nn.ReLU(inplace = True)), ('pool2', nn.MaxPool2d(2, 2)), ('conv3_1', nn.Conv2d(128, 256, kernel_size = 3, padding = 1)), ('relu3_1', nn.ReLU(inplace = True)), ])) self.features_2 = nn.Sequential(OrderedDict([ ('conv3_2', nn.Conv2d(256, 256, kernel_size = 3, padding = 1)), ('relu3_2', nn.ReLU(inplace = True)), ('conv3_3', nn.Conv2d(256, 256, kernel_size = 3, padding = 1)), ('relu3_3', nn.ReLU(inplace = True)), ('conv3_4', nn.Conv2d(256, 256, kernel_size = 3, padding = 1)), ('relu3_4', nn.ReLU(inplace = True)), ('pool3', nn.MaxPool2d(2, 2)), ('conv4_1', nn.Conv2d(256, 512, kernel_size = 3, padding = 1)), ('relu4_1', nn.ReLU(inplace = True)), ])) self.features_3 = nn.Sequential(OrderedDict([ ('conv4_2', nn.Conv2d(512, 512, kernel_size = 3, padding = 1)), ('relu4_2', nn.ReLU(inplace = True)), ('conv4_3', nn.Conv2d(512, 512, kernel_size = 3, padding = 1)), ('relu4_3', nn.ReLU(inplace = True)), ('conv4_4', nn.Conv2d(512, 512, kernel_size = 3, padding = 1)), ('relu4_4', nn.ReLU(inplace = True)), ('pool4', nn.MaxPool2d(2, 2)), ('conv5_1', nn.Conv2d(512, 512, kernel_size = 3, padding = 1)), ('relu5_1', nn.ReLU(inplace = True)), ])) if pretrained: state_dict = torch.utils.model_zoo.load_url(model_urls['vgg19g']) self.load_state_dict(state_dict) def forward(self, x): features_1 = self.features_1(x) features_2 = self.features_2(features_1) features_3 = self.features_3(features_2) return features_1, features_2, features_3 class _PoolingBlock(nn.Sequential): def __init__(self, n_convs, n_input_filters, n_output_filters, drop_rate): super(_PoolingBlock, self).__init__() for i in range(n_convs): self.add_module('conv.%d' % (i+1), nn.Conv2d(n_input_filters if i == 0 else n_output_filters, n_output_filters, kernel_size=3, padding=1)) self.add_module('norm.%d' % (i+1), nn.BatchNorm2d(n_output_filters)) self.add_module('relu.%d' % (i+1), nn.ReLU(inplace=True)) if drop_rate > 0: self.add_module('drop.%d' % (i+1), nn.Dropout(p=drop_rate)) class _TransitionUp(nn.Sequential): def __init__(self, n_input_filters, n_output_filters): super(_TransitionUp, self).__init__() self.add_module('unpool.conv', nn.ConvTranspose2d(n_input_filters, n_output_filters, kernel_size=1, stride=2, output_padding=1)) self.add_module('unpool.norm', nn.BatchNorm2d(n_output_filters)) class PatchDiscModel(nn.Sequential): def __init__(self, drop_rate = 0): super(PatchDiscModel, self).__init__() self.add_module('patch_conv1', nn.Conv2d(3, 64, kernel_size=3, padding=1)) self.add_module('patch_norm1', nn.BatchNorm2d(64)) self.add_module('patch_relu1', nn.ReLU(inplace=True)) if drop_rate > 0: self.add_module('patch_drop1', nn.Dropout(p=drop_rate)) self.add_module('patch_conv2', nn.Conv2d(64, 64, kernel_size=3, padding=1)) self.add_module('patch_norm2', nn.BatchNorm2d(64)) self.add_module('patch_relu2', nn.ReLU(inplace=True)) if drop_rate > 0: self.add_module('patch_drop2', nn.Dropout(p=drop_rate)) self.add_module('patch_pool', nn.MaxPool2d(2, 2)) self.add_module('patch_conv3', nn.Conv2d(64, 64, kernel_size=3, padding=1)) self.add_module('patch_norm3', nn.BatchNorm2d(64)) self.add_module('patch_relu3', nn.ReLU(inplace=True)) if drop_rate > 0: self.add_module('patch_drop3', nn.Dropout(p=drop_rate)) self.add_module('patch_conv4', nn.Conv2d(64, 1, kernel_size=3, padding=1)) def forward(self, x): out = super(PatchDiscModel, self).forward(x) return F.sigmoid(out) class ReconModel(nn.Module): def __init__(self, drop_rate=0): super(ReconModel, self).__init__() self.recon5 = _PoolingBlock(3, 512, 512, drop_rate = drop_rate) self.upool4 = _TransitionUp(512, 512) self.recon4 = _PoolingBlock(3, 1024, 512, drop_rate = drop_rate) self.upool3 = _TransitionUp(512, 256) self.recon3 = _PoolingBlock(3, 512, 256, drop_rate = drop_rate) self.upool2 = _TransitionUp(256, 128) self.recon2 = _PoolingBlock(2, 128, 128, drop_rate = drop_rate) self.upool1 = _TransitionUp(128, 64) self.recon1 = _PoolingBlock(1, 64, 64, drop_rate = drop_rate) self.recon0 = nn.Conv2d(64, 3, kernel_size=3, padding=1) def forward(self, x, mc_samples=1): # Non MC inference if mc_samples == 1 or not any([isinstance(module, nn.Dropout) for module in self.modules()]): res = self._forward(x) return res, res*0 # MC inference for module in self.modules(): if isinstance(module, nn.Dropout): module.train() means = None covars = None size = None for i in range(mc_samples): output_var = self._forward(x) output = output_var.data if size is None: size = output.size() output = output.permute(0, 2, 3, 1).contiguous().view(-1, 1, 3) if means is None: means = output.clone() else: means.add_(output) if covars is None: covars = torch.bmm(output.permute(0, 2, 1), output) else: covars.baddbmm_(output.permute(0, 2, 1), output) means.div_(mc_samples) covars.div_(mc_samples).sub_(torch.bmm(means.permute(0, 2, 1), means)) # Set stdv to be frobenius norm stdvs = covars.view(-1, 9).norm(p=2, dim=1) stdvs.sqrt_() stdvs.clamp_(0, 1) # Reshape means = means.view(-1, size[2], size[3], 3).permute(0, 3, 1, 2) stdvs = stdvs.view(-1, 1, size[2], size[3]) means_var = Variable(means, volatile=True) stdvs_var = Variable(stdvs.repeat(1, 3, 1, 1), volatile=True) return means_var, stdvs_var def _forward(self, x): features_1, features_2, features_3 = x recon5 = self.recon5(features_3) upool4 = self.upool4(recon5) recon4 = self.recon4(torch.cat([upool4, features_2], 1)) upool3 = self.upool3(recon4) recon3 = self.recon3(torch.cat([upool3, features_1], 1)) upool2 = self.upool2(recon3) recon2 = self.recon2(upool2) upool1 = self.upool1(recon2) recon1 = self.recon1(upool1) recon0 = self.recon0(recon1) return recon0 class PatchDiscLoss(nn.Module): def __init__(self): super(PatchDiscLoss, self).__init__() self.bce_loss = nn.BCELoss() def loss_names(self): return ('d-disc',) def forward(self, input_var, target_val): input_var = input_var.view(-1) target = torch.Tensor(input_var.data.size()).type_as(input_var.data) target.fill_(target_val) target_var = Variable(target) target_var.data.fill_(target_val) return self.bce_loss(input_var, target_var) class ReconLoss(nn.Module): def __init__(self, lamda): super(ReconLoss, self).__init__() self.lamda = lamda self.disc_loss = PatchDiscLoss() def loss_names(self): return ('c3diff', 'c4diff', 'c5diff', 'g-disc') def forward(self, x, y=None): recon_var, conv3diff, conv4diff, conv5diff, disc_output_var = x return torch.cat([ torch.norm(conv3diff)**2 / conv3diff.nelement(), torch.norm(conv4diff)**2 / conv4diff.nelement(), torch.norm(conv5diff)**2 / conv5diff.nelement(), self.lamda * self.disc_loss(disc_output_var, 1) ]) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |