Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,871 Bytes
476e0f0 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
class UpsampleBlock(nn.Module):
def __init__(self, num_input_channels, num_output_channels):
super(UpsampleBlock, self).__init__()
self.pixel_shuffle = nn.PixelShuffle(2)
self.conv = nn.Conv2d(num_input_channels // (2 * 2), num_output_channels, 3, stride=1, padding=1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.pixel_shuffle(x)
x = self.conv(x)
x = self.relu(x)
return x
class AppearanceNetwork(nn.Module):
def __init__(self, num_input_channels, num_output_channels):
super(AppearanceNetwork, self).__init__()
self.conv1 = nn.Conv2d(num_input_channels, 256, 3, stride=1, padding=1)
self.up1 = UpsampleBlock(256, 128)
self.up2 = UpsampleBlock(128, 64)
self.up3 = UpsampleBlock(64, 32)
self.up4 = UpsampleBlock(32, 16)
self.conv2 = nn.Conv2d(16, 16, 3, stride=1, padding=1)
self.conv3 = nn.Conv2d(16, num_output_channels, 3, stride=1, padding=1)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.up1(x)
x = self.up2(x)
x = self.up3(x)
x = self.up4(x)
# bilinear interpolation
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
x = self.conv2(x)
x = self.relu(x)
x = self.conv3(x)
x = self.sigmoid(x)
return x
if __name__ == "__main__":
H, W = 1200//32, 1600//32
input_channels = 3 + 64
output_channels = 3
input = torch.randn(1, input_channels, H, W).cuda()
model = AppearanceNetwork(input_channels, output_channels).cuda()
output = model(input)
print(output.shape) |