-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder_decoder_disc_VQ.py
175 lines (145 loc) · 7.05 KB
/
Encoder_decoder_disc_VQ.py
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
import torch
import numpy as np
from torchvision import models
from torchsummary import summary
import matplotlib.pyplot as plt # plotting library
import numpy as np # this module is useful to work with numerical arrays
#import pandas as pd
import torch.utils.data as data_loader
from torchvision import transforms
import torch.nn.functional as F
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv4 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.bn4 = nn.BatchNorm2d(64)
self.conv5 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.bn5 = nn.BatchNorm2d(64)
self.conv6 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.bn6 = nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv7 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.bn7 = nn.BatchNorm2d(128)
self.conv8 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1)
self.bn8 = nn.BatchNorm2d(128)
self.conv9 = nn.Conv2d(128, 32, kernel_size=3, stride=1, padding=1)
self.bn9 = nn.BatchNorm2d(32)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.bn10 = nn.BatchNorm2d(32)
def forward(self, x):
x = nn.functional.relu(self.bn1(self.conv1(x)))
x = nn.functional.relu(self.bn2(self.conv2(x)))
x = nn.functional.relu(self.bn3(self.conv3(x)))
x = self.pool1(x)
x = nn.functional.relu(self.bn4(self.conv4(x)))
x = nn.functional.relu(self.bn5(self.conv5(x)))
x = nn.functional.relu(self.bn6(self.conv6(x)))
x = self.pool2(x)
x = nn.functional.relu(self.bn7(self.conv7(x)))
x = nn.functional.relu(self.bn8(self.conv8(x)))
x = nn.functional.relu(self.bn9(self.conv9(x)))
x = self.pool3(x)
x = self.bn10(x)
return x
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.conv1 = nn.ConvTranspose2d(32, 64, kernel_size=3, stride=2, padding=1, output_padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.ConvTranspose2d(64, 64, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2, padding=1, output_padding=1)
self.bn3 = nn.BatchNorm2d(64)
self.conv4 = nn.ConvTranspose2d(64, 64, kernel_size=3, stride=1, padding=1)
self.bn4 = nn.BatchNorm2d(64)
self.conv5 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1, output_padding=1)
self.bn5 = nn.BatchNorm2d(32)
self.conv6 = nn.ConvTranspose2d(32, 32, kernel_size=3, stride=1, padding=1)
self.bn6 = nn.BatchNorm2d(32)
self.conv7 = nn.ConvTranspose2d(32, 3, kernel_size=3, stride=1, padding=1)
def forward(self, x):
x = nn.functional.relu(self.bn1(self.conv1(x)))
x = nn.functional.relu(self.bn2(self.conv2(x)))
x = nn.functional.relu(self.bn3(self.conv3(x)))
x = nn.functional.relu(self.bn4(self.conv4(x)))
x = nn.functional.relu(self.bn5(self.conv5(x)))
x = nn.functional.relu(self.bn6(self.conv6(x)))
x = nn.functional.sigmoid(self.conv7(x))
return x
class VQ (nn.Module ):
def __init__(self) :
super().__init__()
self.word_embedding_dim = 32 #if we make it 32 then change above in encoder and decoder
self._num_embeddings = 64
self._embedding = nn.Embedding(self._num_embeddings, self.word_embedding_dim)
self._embedding.weight.data.uniform_(-1/self._num_embeddings, 1/self._num_embeddings)
self._commitment_cost = 0.25
def forward(self,x):
inputs=x.permute(0, 2, 3, 1).contiguous()
input_shape = inputs.shape
x=inputs.view(-1,self.word_embedding_dim)
distances = (torch.sum(x**2, dim=1, keepdim=True)
+ torch.sum(self._embedding.weight**2, dim=1)
- 2 * torch.matmul(x, self._embedding.weight.t()))
encoding_indices=torch.argmin(distances, dim=1).unsqueeze(1)
encodings = torch.zeros(encoding_indices.shape[0], self._num_embeddings, device=inputs.device)
encodings.scatter_(1, encoding_indices, 1)
#encoding_indices=torch.nn.functional.one_hot(encoding_indices,self._num_embeddings)
quantized = torch.matmul(encodings, self._embedding.weight).view(input_shape)
e_latent_loss = F.mse_loss(quantized.detach(), inputs)
q_latent_loss = F.mse_loss(quantized, inputs.detach())
loss = q_latent_loss + self._commitment_cost * e_latent_loss
quantized = inputs + (quantized - inputs).detach()
avg_probs = torch.mean(encodings, dim=0)
#perplexity = torch.exp(-torch.sum(avg_probs * torch.log(avg_probs + 1e-10)))
perplexity=1
# convert quantized from BHWC -> BCHW
return loss, quantized.permute(0, 3, 1, 2).contiguous(), perplexity, self._embedding
class Disc_net1(nn.Module):
def __init__(self):
super(Disc_net1, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(num_features=16)
self.relu1 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(num_features=32)
self.relu2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(num_features=64)
self.relu3 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=64*16*16, out_features=256)
self.relu4 = nn.ReLU(inplace=True)
self.dropout = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(in_features=256, out_features=1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.pool3(x)
x = x.view(-1, 64*16*16)
x = self.fc1(x)
x = self.relu4(x)
x = self.dropout(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x