forked from scott-yjyang/DiffMIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
131 lines (111 loc) · 4.03 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models.resnet import resnet18, resnet50
from torchvision.models.densenet import densenet121
from timm.models import create_model
import numpy as np
class ConditionalLinear(nn.Module):
def __init__(self, num_in, num_out, n_steps):
super(ConditionalLinear, self).__init__()
self.num_out = num_out
self.lin = nn.Linear(num_in, num_out)
self.embed = nn.Embedding(n_steps, num_out)
self.embed.weight.data.uniform_()
def forward(self, x, t):
out = self.lin(x)
gamma = self.embed(t)
out = gamma.view(-1, self.num_out) * out
return out
class ConditionalModel(nn.Module):
def __init__(self, config, guidance=False):
super(ConditionalModel, self).__init__()
n_steps = config.diffusion.timesteps + 1
data_dim = config.model.data_dim
y_dim = config.data.num_classes
arch = config.model.arch
feature_dim = config.model.feature_dim
hidden_dim = config.model.hidden_dim
self.guidance = guidance
# encoder for x
self.encoder_x = ResNetEncoder(arch=arch, feature_dim=feature_dim)
# batch norm layer
self.norm = nn.BatchNorm1d(feature_dim)
# Unet
if self.guidance:
self.lin1 = ConditionalLinear(y_dim * 2, feature_dim, n_steps)
else:
self.lin1 = ConditionalLinear(y_dim, feature_dim, n_steps)
self.unetnorm1 = nn.BatchNorm1d(feature_dim)
self.lin2 = ConditionalLinear(feature_dim, feature_dim, n_steps)
self.unetnorm2 = nn.BatchNorm1d(feature_dim)
self.lin3 = ConditionalLinear(feature_dim, feature_dim, n_steps)
self.unetnorm3 = nn.BatchNorm1d(feature_dim)
self.lin4 = nn.Linear(feature_dim, y_dim)
def forward(self, x, y, t, yhat=None):
features = []
x = self.encoder_x(x)
x = self.norm(x)
if self.guidance:
#for yh in yhat:
y = torch.cat([y, yhat], dim=-1)
y = self.lin1(y, t)
features.append(y)
y = self.unetnorm1(y)
y = F.softplus(y)
y = x * y
y = self.lin2(y, t)
features.append(y)
y = self.unetnorm2(y)
y = F.softplus(y)
y = self.lin3(y, t)
features.append(y)
y = self.unetnorm3(y)
y = F.softplus(y)
y = self.lin4(y)
features.append(y)
return y, features
# ResNet 18 or 50 as image encoder
class ResNetEncoder(nn.Module):
def __init__(self, arch='resnet18', feature_dim=128):
super(ResNetEncoder, self).__init__()
self.f = []
#print(arch)
if arch == 'resnet50':
backbone = resnet50()
self.featdim = backbone.fc.weight.shape[1]
elif arch == 'resnet18':
backbone = resnet18()
self.featdim = backbone.fc.weight.shape[1]
elif arch == 'densenet121':
backbone = densenet121(pretrained=True)
self.featdim = backbone.classifier.weight.shape[1]
elif arch == 'vit':
backbone = create_model('pvt_v2_b2',
pretrained=True,
num_classes=4,
drop_rate=0,
drop_path_rate=0.1,
drop_block_rate=None,
)
backbone.head = nn.Sequential()
self.featdim = 512
for name, module in backbone.named_children():
#if not isinstance(module, nn.Linear):
# self.f.append(module)
if name != 'fc':
self.f.append(module)
# encoder
self.f = nn.Sequential(*self.f)
#print(self.featdim)
self.g = nn.Linear(self.featdim, feature_dim)
#self.z = nn.Linear(feature_dim, 4)
def forward_feature(self, x):
feature = self.f(x)
#x = x.mean(dim=1)
feature = torch.flatten(feature, start_dim=1)
feature = self.g(feature)
return feature
def forward(self, x):
feature = self.forward_feature(x)
return feature