Delete duplicate/old file: \/Generators.py
Browse files- ///Generators.py +0 -145
///Generators.py
DELETED
|
@@ -1,145 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/python
|
| 2 |
-
'''
|
| 3 |
-
Defines the Encoders and Decoder that make up the
|
| 4 |
-
Enc+Dec/Enc+2Dec model defined in DLoc
|
| 5 |
-
Idea and code originally from fstin Johnson's architecture.
|
| 6 |
-
https://github.com/jcjohnson/fast-neural-style/
|
| 7 |
-
Code base expanded from pix2pix Phillip Isola's implementation
|
| 8 |
-
https://github.com/phillipi/pix2pix
|
| 9 |
-
|
| 10 |
-
You can add your costum network building blocks here to test various other architectures.
|
| 11 |
-
'''
|
| 12 |
-
import torch
|
| 13 |
-
import torch.nn as nn
|
| 14 |
-
import functools
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Tha base Encoder function defined for the DLoc's Encoder
|
| 18 |
-
class ResnetEncoder(nn.Module):
|
| 19 |
-
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, padding_type='zero'):
|
| 20 |
-
assert(n_blocks >= 0)
|
| 21 |
-
super(ResnetEncoder, self).__init__()
|
| 22 |
-
self.input_nc = input_nc
|
| 23 |
-
self.output_nc = output_nc
|
| 24 |
-
self.ngf = ngf
|
| 25 |
-
if type(norm_layer) == functools.partial:
|
| 26 |
-
use_bias = norm_layer.func == nn.InstanceNorm2d
|
| 27 |
-
else:
|
| 28 |
-
use_bias = norm_layer == nn.InstanceNorm2d
|
| 29 |
-
|
| 30 |
-
model = [nn.Conv2d(input_nc, input_nc, kernel_size=7, padding=3,
|
| 31 |
-
bias=use_bias),
|
| 32 |
-
norm_layer(input_nc),
|
| 33 |
-
nn.Tanh()]
|
| 34 |
-
|
| 35 |
-
model += [nn.Conv2d(input_nc, ngf, kernel_size=7, padding=3,
|
| 36 |
-
bias=use_bias),
|
| 37 |
-
norm_layer(ngf),
|
| 38 |
-
nn.ReLU(True)]
|
| 39 |
-
|
| 40 |
-
n_downsampling = 2
|
| 41 |
-
for i in range(n_downsampling):
|
| 42 |
-
mult = 2**i
|
| 43 |
-
model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3,
|
| 44 |
-
stride=2, padding=1, bias=use_bias),
|
| 45 |
-
norm_layer(ngf * mult * 2),
|
| 46 |
-
nn.ReLU(True)]
|
| 47 |
-
|
| 48 |
-
mult = 2**n_downsampling
|
| 49 |
-
for i in range(n_blocks):
|
| 50 |
-
model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)]
|
| 51 |
-
|
| 52 |
-
self.model = nn.Sequential(*model)
|
| 53 |
-
|
| 54 |
-
def forward(self, input):
|
| 55 |
-
return self.model(input)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Tha base Decoder function defined for the DLoc's Decoders.
|
| 59 |
-
# Depending upon the ModelADT wraper around the decoder,
|
| 60 |
-
# the decoder would either be a Location Decoder or a Consistency decoder.
|
| 61 |
-
# For more details refer to ModelADT.py wrapper implementation and params.py
|
| 62 |
-
class ResnetDecoder(nn.Module):
|
| 63 |
-
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=9, padding_type='zero', encoder_blocks=6):
|
| 64 |
-
assert(n_blocks >= 0)
|
| 65 |
-
super(ResnetDecoder, self).__init__()
|
| 66 |
-
self.input_nc = input_nc
|
| 67 |
-
self.output_nc = output_nc
|
| 68 |
-
self.ngf = ngf
|
| 69 |
-
if type(norm_layer) == functools.partial:
|
| 70 |
-
use_bias = norm_layer.func == nn.InstanceNorm2d
|
| 71 |
-
else:
|
| 72 |
-
use_bias = norm_layer == nn.InstanceNorm2d
|
| 73 |
-
|
| 74 |
-
model = []
|
| 75 |
-
n_downsampling = 2
|
| 76 |
-
for i in range(n_downsampling):
|
| 77 |
-
mult = 2**i
|
| 78 |
-
|
| 79 |
-
mult = 2**n_downsampling
|
| 80 |
-
for i in range(n_blocks):
|
| 81 |
-
if i <= encoder_blocks:
|
| 82 |
-
continue
|
| 83 |
-
model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)]
|
| 84 |
-
|
| 85 |
-
for i in range(n_downsampling):
|
| 86 |
-
mult = 2**(n_downsampling - i)
|
| 87 |
-
model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
|
| 88 |
-
kernel_size=3, stride=2,
|
| 89 |
-
padding=1, output_padding=0,
|
| 90 |
-
bias=use_bias),
|
| 91 |
-
norm_layer(int(ngf * mult / 2)),
|
| 92 |
-
nn.ReLU(True)]
|
| 93 |
-
# model += [nn.ReflectionPad2d(3)]
|
| 94 |
-
model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=[3,3])]
|
| 95 |
-
model += [nn.Sigmoid()]
|
| 96 |
-
|
| 97 |
-
self.model = nn.Sequential(*model)
|
| 98 |
-
|
| 99 |
-
def forward(self, input):
|
| 100 |
-
#print("decoder.input = ", input.shape)
|
| 101 |
-
return self.model(input)
|
| 102 |
-
# In[3]:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Define a resnet block
|
| 106 |
-
class ResnetBlock(nn.Module):
|
| 107 |
-
def __init__(self, dim, padding_type, norm_layer, use_dropout, use_bias):
|
| 108 |
-
super(ResnetBlock, self).__init__()
|
| 109 |
-
self.conv_block = self.build_conv_block(dim, padding_type, norm_layer, use_dropout, use_bias)
|
| 110 |
-
|
| 111 |
-
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias):
|
| 112 |
-
conv_block = []
|
| 113 |
-
p = 0
|
| 114 |
-
if padding_type == 'reflect':
|
| 115 |
-
conv_block += [nn.ReflectionPad2d(1)]
|
| 116 |
-
elif padding_type == 'replicate':
|
| 117 |
-
conv_block += [nn.ReplicationPad2d(1)]
|
| 118 |
-
elif padding_type == 'zero':
|
| 119 |
-
p = 1
|
| 120 |
-
else:
|
| 121 |
-
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
|
| 122 |
-
|
| 123 |
-
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
|
| 124 |
-
norm_layer(dim),
|
| 125 |
-
nn.ReLU(True)]
|
| 126 |
-
if use_dropout:
|
| 127 |
-
conv_block += [nn.Dropout(0.25)]
|
| 128 |
-
|
| 129 |
-
p = 0
|
| 130 |
-
if padding_type == 'reflect':
|
| 131 |
-
conv_block += [nn.ReflectionPad2d(1)]
|
| 132 |
-
elif padding_type == 'replicate':
|
| 133 |
-
conv_block += [nn.ReplicationPad2d(1)]
|
| 134 |
-
elif padding_type == 'zero':
|
| 135 |
-
p = 1
|
| 136 |
-
else:
|
| 137 |
-
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
|
| 138 |
-
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
|
| 139 |
-
norm_layer(dim)]
|
| 140 |
-
|
| 141 |
-
return nn.Sequential(*conv_block)
|
| 142 |
-
|
| 143 |
-
def forward(self, x):
|
| 144 |
-
out = x + self.conv_block(x)
|
| 145 |
-
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|