-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune.py
177 lines (145 loc) · 6.8 KB
/
finetune.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
import os
import dgl
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
from typing import Optional, List
from .model import Generator, SVDDEncoder
from .model import SSIMLoss, SVDDLoss, SCELoss
from .utils import seed_everything
from thop import profile
class SNNet:
def __init__(self, epochs: List[int] = [10, 5], batch_size: int = 128,
learning_rate: float = 1e-4, GPU: Optional[str] = "cuda:0",
random_state: Optional[int] = None, Mobile: bool = False):
if GPU is not None:
if torch.cuda.is_available():
self.device = torch.device(GPU)
else:
print("GPU isn't available, and use CPU to train STAND.")
self.device = torch.device("cpu")
else:
self.device = torch.device("cpu")
self.epochs = epochs
self.batch_size = batch_size
self.lr = learning_rate
self.Mobile = Mobile
self.rate = 0.01
if random_state is not None:
seed_everything(random_state)
def fit(self, ref_g: dgl.DGLGraph, weight_dir: Optional[str] = None, **kwargs):
'''Training on reference graph'''
tqdm.write('Begin to train the model on normal spots...')
# dataset provides subgraph for training
self.sampler = dgl.dataloading.MultiLayerFullNeighborSampler(3)
self.dataloader = dgl.dataloading.DataLoader(
ref_g, ref_g.nodes(), self.sampler,
batch_size=self.batch_size, shuffle=True,
drop_last=False, num_workers=4, device=self.device)
self.in_dim = ref_g.ndata['gene'].shape[1]
self.patch_size = ref_g.ndata['patch'].shape[2]
self.G = Generator(self.patch_size, self.in_dim, Mobile=self.Mobile, **kwargs).to(self.device)
self.E = SVDDEncoder(self.patch_size, self.in_dim, **kwargs).to(self.device)
# Load pretrained weights
self.prepare(weight_dir)
self.opt_G = optim.Adam(self.G.parameters(), lr=self.lr, betas=(0.5, 0.999))
self.sch_G = optim.lr_scheduler.CosineAnnealingLR(optimizer=self.opt_G, T_max=self.epochs[0])
self.opt_E = optim.Adam(self.E.parameters(), lr=self.rate*self.lr, betas=(0.5, 0.999))
self.sch_E = optim.lr_scheduler.CosineAnnealingLR(optimizer=self.opt_E, T_max=self.epochs[1])
self.l1_loss = nn.L1Loss().to(self.device)
self.ssim_loss = SSIMLoss().to(self.device)
self.svdd_loss = SVDDLoss().to(self.device)
self.sce_loss = SCELoss().to(self.device)
self.G.train()
self.E.train()
for e in range(self.epochs[0]):
loop = tqdm(self.dataloader, total=len(self.dataloader))
for _, _, blocks in loop:
# blocks = [b.to(self.device) for b in blocks]
self.UpdateG(blocks)
loop.set_description(f'Stage II Epochs [{e}/{self.epochs[0]}]')
loop.set_postfix(Loss = self.G_loss.item())
# Update learning rate for G
self.sch_G.step()
for e in range(self.epochs[1]):
loop = tqdm(self.dataloader, total=len(self.dataloader))
for _, _, blocks in loop:
blocks = [b.to(self.device) for b in blocks]
self.UpdateE(blocks)
loop.set_description(f'Stage III Epochs [{e}/{self.epochs[1]}]')
loop.set_postfix(Loss = self.E_loss.item())
# Update learning rate for G and D
self.sch_E.step()
tqdm.write('Training has been finished.')
@torch.no_grad()
def predict(self, tgt_g: dgl.DGLGraph):
'''Detect anomalous spots on target graph'''
if (self.G is None):
raise RuntimeError('Please fine-tune the model first.')
dataloader = dgl.dataloading.DataLoader(
tgt_g, tgt_g.nodes(), self.sampler,
batch_size=self.batch_size, shuffle=False,
drop_last=False, num_workers=4, device=self.device)
self.G.eval()
self.E.eval()
tqdm.write('Detect anomalous spots on test dataset...')
# calucate anomaly score
dis = []
for _, _, blocks in dataloader:
# blocks = [b.to(self.device) for b in blocks]
input_g, input_p = self.read_input(blocks)
real_g, real_p, fake_g, fake_p = self.G(blocks, input_g, input_p)
real_fused = self.E(real_g.detach(), real_p.detach())
fake_fused = self.E(fake_g.detach(), fake_p.detach())
d = self.svdd_loss(real_fused, fake_fused)
dis.append(d.cpu().detach())
# Normalize anomaly scores
dis = torch.mean(torch.cat(dis, dim=0), dim=1).numpy()
score = (dis.max() - dis)/(dis.max() - dis.min())
tqdm.write('Anomalous spots have been detected.\n')
return list(score)
@torch.no_grad()
def prepare(self, weight_dir: Optional[str]):
'''Prepare stage for pretrained weights'''
weight_name = '/MobileUNet.pth' if self.Mobile else '/UNet.pth'
if weight_dir:
pre_weights = torch.load(weight_dir)
else:
pre_weights = torch.load(os.path.dirname(__file__) + weight_name)
# Load the pre-trained weights for Encoder and Decoder
model_dict = self.G.UNet.state_dict()
pretrained_dict = {k: v for k, v in pre_weights.items()}
model_dict.update(pretrained_dict)
self.G.UNet.load_state_dict(model_dict)
for param in self.G.UNet.encoder.parameters():
param.requires_grad = False
def read_input(self, blocks):
input_g = blocks[0].srcdata['gene']
input_p = blocks[0].srcdata['patch']
return input_g, input_p
def UpdateG(self, blocks):
'''Updating generator'''
input_g, input_p = self.read_input(blocks)
real_g, real_p, fake_g, fake_p = self.G(blocks, input_g, input_p)
# flops, params = profile(self.G, inputs=(blocks, input_g, input_p, ))
# print(params)
dg = self.in_dim
dp = 3 * self.patch_size**2
alpha = dp / (dg + dp)
loss_g = self.sce_loss(real_g, fake_g)
loss_p = self.l1_loss(real_p, fake_p) + self.ssim_loss(real_p, fake_p)
self.G_loss = (1-alpha)*loss_g + alpha*loss_p
self.opt_G.zero_grad()
self.G_loss.backward()
self.opt_G.step()
def UpdateE(self, blocks):
'''Updating SVDD Encoder'''
input_g, input_p = self.read_input(blocks)
real_g, real_p, fake_g, fake_p = self.G(blocks, input_g, input_p)
real_fused = self.E(real_g.detach(), real_p.detach())
fake_fused = self.E(fake_g.detach(), fake_p.detach())
self.E_loss = self.svdd_loss(real_fused, fake_fused).mean()
self.opt_E.zero_grad()
self.E_loss.backward()
self.opt_E.step()