-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
55 lines (43 loc) · 1.46 KB
/
models.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
import torch.nn as nn
import torch
class Decoder(nn.Module):
"""MLP decoder for segmentation task."""
def __init__(self, latent_dim: int, hidden_dim: int, output_dim: int):
"""
Initialize the class.
Args:
latent_dim (int): embedding vector dimension in ViT.
hidden_dim (int): hidden non-linear dimension.
output_dim (int): task output dimension.
"""
super().__init__()
self.decoder = nn.Sequential(
nn.Linear(latent_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim)
)
def forward(self, x: torch.Tensor):
return self.decoder(x)
class AutoEncoder(nn.Module):
"""Autoencoder for segmentation task."""
def __init__(self, encoder: nn.Module, decoder: nn.Module):
"""
Initialize the class.
Args:
encoder (nn.Moudule): embedding vector encoder.
decoder (nn.Moudule): task output decoder.
"""
super().__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, x: torch.Tensor, mask: torch.Tensor):
"""
Forward process.
Args:
x (Tensor): the inputs.
mask (Tensor): the random mask applied to inputs.
"""
x_masked = mask * x
encode_patch = self.encoder(x_masked)
reconstruct = self.decoder(encode_patch)
return reconstruct