-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·73 lines (60 loc) · 2.48 KB
/
model.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import nn
from hr_net import config
from hr_net.hrnet import get_seg_model
class LayerNorm(nn.Module):
def __init__(self, dim, eps=1e-5, affine=True):
super().__init__()
self.eps = eps
self.affine = affine
if self.affine:
self.g = nn.Parameter(torch.ones(1, dim, 1, 1))
self.b = nn.Parameter(torch.zeros(1, dim, 1, 1))
else:
self.g = 1.0
self.b = 0.0
def forward(self, x):
std = torch.var(x, dim=1, unbiased=False, keepdim=True).sqrt()
mean = torch.mean(x, dim=1, keepdim=True)
return (x - mean) / (std + self.eps) * self.g + self.b
class MOVES_Model(nn.Module):
def __init__(self, args=None):
super().__init__()
embed_dim = args.embed_size[0]
self.args = args
config.defrost()
config.merge_from_file('./hr_net/seg_hrnet_w48_train_512x1024_sgd_lr1e-2_wd5e-4_bs_12_epoch484.yaml')
config.merge_from_list(['DATASET.NUM_CLASSES', embed_dim, 'TRAIN.IMAGE_SIZE', args.img_size, 'TRAIN.BASE_SIZE', args.img_size[0]])
config.freeze()
backbone = get_seg_model(config)
self.backbone = backbone
if 'dd' in args.target:
# build a 3-layer MLP for comparing embeddings
self.compare_nn = nn.Sequential(
nn.Linear(embed_dim * 2, embed_dim * 2, bias=False),
nn.BatchNorm1d(embed_dim * 2),
nn.ReLU(inplace=True), # first layer
nn.Linear(embed_dim * 2, embed_dim, bias=False),
nn.BatchNorm1d(embed_dim),
nn.ReLU(inplace=True), # first layer
nn.Linear(embed_dim, 2, bias=False))
if 'aa' in args.target:
# build a 3-layer MLP for comparing embeddings
self.compare_assoc = nn.Sequential(
nn.Linear(embed_dim * 2, embed_dim * 2, bias=False),
nn.BatchNorm1d(embed_dim * 2),
nn.ReLU(inplace=True), # first layer
nn.Linear(embed_dim * 2, embed_dim, bias=False),
nn.BatchNorm1d(embed_dim),
nn.ReLU(inplace=True), # first layer
nn.Linear(embed_dim, 2, bias=False))
def forward(self, x):
z = self.backbone(x)
z = F.interpolate(z, size=self.args.embed_size, mode='bilinear')
return {'e': z}
if __name__ == "__main__":
dummy = MOVES_Model()