-
Notifications
You must be signed in to change notification settings - Fork 5
/
models_t1000.py
executable file
·179 lines (152 loc) · 5.22 KB
/
models_t1000.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
175
176
177
178
179
"""
This file contains the pytorch model definitions for the dataset using
the top 1000 select tags.
"""
import torch
from torch import nn
from torch.nn import Sequential, Linear, Dropout, ReLU, Sigmoid, Conv2d, ConvTranspose2d, BatchNorm1d, BatchNorm2d, LeakyReLU
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class UnFlatten(nn.Module):
def forward(self, input, size=128):
return input.view(input.size(0), size, 3, 3)
class AudioEncoder(nn.Module):
def __init__(self):
super(AudioEncoder, self).__init__()
self.audio_encoder = Sequential(
Conv2d(1, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x48x48
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x24x24
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x12x12
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x6x6
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x3x3
Dropout(.25),
Flatten(),
)
self.fc_audio = Sequential(
Linear(1152, 1152, bias=False),
Dropout(0.25),
)
def forward(self, x):
z = self.audio_encoder(x)
z_d = self.fc_audio(z)
return z, z_d
class AudioDecoder(nn.Module):
def __init__(self):
super(AudioDecoder, self).__init__()
self.audio_decoder = Sequential(
UnFlatten(),
Dropout(.25),
ConvTranspose2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(),
Dropout(.25),
ConvTranspose2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(),
Dropout(.25),
ConvTranspose2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(),
Dropout(.25),
ConvTranspose2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(),
ConvTranspose2d(128, 1, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(1),
Sigmoid(),
)
def forward(self, z):
return self.audio_decoder(z)
class TagEncoder(nn.Module):
def __init__(self):
super(TagEncoder, self).__init__()
self.tag_encoder = Sequential(
Linear(1000, 512),
BatchNorm1d(512),
ReLU(),
Dropout(.25),
Linear(512, 512),
BatchNorm1d(512),
ReLU(),
Dropout(.25),
Linear(512, 1152),
BatchNorm1d(1152),
ReLU(),
Dropout(.25),
)
self.fc_tag = Sequential(
Linear(1152, 1152, bias=False),
Dropout(.25),
)
def forward(self, tags):
z = self.tag_encoder(tags)
z_d = self.fc_tag(z)
return z, z_d
class TagDecoder(nn.Module):
def __init__(self):
super(TagDecoder, self).__init__()
self.tag_decoder = Sequential(
Linear(1152, 512),
BatchNorm1d(512),
ReLU(),
Dropout(.25),
Linear(512, 512),
BatchNorm1d(512),
ReLU(),
Linear(512, 1000),
BatchNorm1d(1000),
Sigmoid(),
)
def forward(self, z):
return self.tag_decoder(z)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.encoder = Sequential(
Conv2d(1, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x48x48
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x24x24
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x12x12
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x6x6
Dropout(.25),
Conv2d(128, 128, kernel_size=4, stride=2, padding=1, padding_mode='zeros'),
BatchNorm2d(128),
ReLU(), # 128x3x3
Dropout(.25),
Flatten(),
)
self.fc = Sequential(
Linear(1152, 1152),
ReLU(),
Linear(1152, 1000),
Sigmoid(),
)
def forward(self, x):
z = self.encoder(x)
y = self.fc(z)
return z, y