-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_PACS_DINL.py
158 lines (121 loc) · 4.95 KB
/
train_PACS_DINL.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
import torch
import numpy as np
from resnet import wide_resnet50_2
from de_resnet import de_wide_resnet50_2
from dataset import PACSDataset, AugMixDatasetPACS
from torch.nn import functional as F
import torchvision.transforms as transforms
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def loss_fucntion(a, b):
cos_loss = torch.nn.CosineSimilarity()
loss = 0
for item in range(len(a)):
loss += torch.mean(1 - cos_loss(a[item].view(a[item].shape[0], -1),
b[item].view(b[item].shape[0], -1)))
return loss
def loss_fucntion_last(a, b):
mse_loss = torch.nn.MSELoss()
cos_loss = torch.nn.CosineSimilarity()
loss = 0
item = 0
loss += torch.mean(1 - cos_loss(a[item].view(a[item].shape[0], -1),
b[item].view(b[item].shape[0], -1)))
return loss
def loss_fucntion_bn(a, b):
cos_loss = torch.nn.CosineSimilarity()
loss = 0
for item in range(len(a)):
a[item] = torch.amax(a[item], dim=(2, 3))
b[item] = torch.amax(b[item], dim=(2, 3))
loss += torch.mean(1 - cos_loss(a[item].view(a[item].shape[0], -1),
b[item].view(b[item].shape[0], -1)))
return loss
def loss_concat(a, b):
mse_loss = torch.nn.MSELoss()
cos_loss = torch.nn.CosineSimilarity()
loss = 0
a_map = []
b_map = []
size = a[0].shape[-1]
for item in range(len(a)):
a_map.append(F.interpolate(a[item], size=size, mode='bilinear', align_corners=True))
b_map.append(F.interpolate(b[item], size=size, mode='bilinear', align_corners=True))
a_map = torch.cat(a_map, 1)
b_map = torch.cat(b_map, 1)
loss += torch.mean(1 - cos_loss(a_map, b_map))
return loss
def train(_class_):
print(_class_)
epochs = 20
learning_rate = 0.005
batch_size = 16
image_size = 256
labels_dict = {
'dog': 0,
'elephant': 1,
'giraffe': 2,
'guitar': 3,
'horse': 4,
'house': 5,
'person': 6
}
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)
resize_transform = transforms.Compose([
transforms.Resize((image_size, image_size)),
])
mean_train = [0.485, 0.456, 0.406]
std_train = [0.229, 0.224, 0.225]
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean_train,
std=std_train),
])
train_path = './PACS/train/photo/' +_class_
train_data = PACSDataset(root=train_path, transform=resize_transform)
train_data = AugMixDatasetPACS(train_data, preprocess)
train_dataloader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True)
encoder, bn = wide_resnet50_2(pretrained=True)
encoder = encoder.to(device)
bn = bn.to(device)
encoder.eval()
decoder = de_wide_resnet50_2(pretrained=False)
decoder = decoder.to(device)
optimizer = torch.optim.Adam(list(decoder.parameters()) + list(bn.parameters()), lr=learning_rate,
betas=(0.5, 0.999))
for epoch in range(epochs):
bn.train()
decoder.train()
loss_list = []
for normal, augmix_img, gray_img in train_dataloader:
normal = normal.to(device)
inputs_normal = encoder(normal)
bn_normal = bn(inputs_normal)
outputs_normal = decoder(bn_normal)
augmix_img = augmix_img.to(device)
inputs_augmix = encoder(augmix_img)
bn_augmix = bn(inputs_augmix)
outputs_augmix = decoder(bn_augmix)
gray_img = gray_img.to(device)
inputs_gray = encoder(gray_img)
bn_gray = bn(inputs_gray)
outputs_gray = decoder(bn_gray)
loss_bn = loss_fucntion([bn_normal], [bn_augmix]) + loss_fucntion([bn_normal], [bn_gray])
loss_last = loss_fucntion_last(outputs_normal, outputs_augmix) + loss_fucntion_last(outputs_normal, outputs_gray)
loss_normal = loss_fucntion(inputs_normal, outputs_normal)
loss = loss_normal*0.9 + loss_bn*0.05 + loss_last*0.05
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_list.append(loss.item())
print('epoch [{}/{}], loss:{:.4f}'.format(epoch + 1, epochs, np.mean(loss_list)))
if (epoch + 1) % 20 == 0 :
ckp_path = './checkpoints/' + 'PACS_DINL_' + str(_class_) + '_' + str(epoch) + '.pth'
torch.save({'bn': bn.state_dict(),
'decoder': decoder.state_dict()}, ckp_path)
return
if __name__ == '__main__':
item_list = ["dog", "elephant", "giraffe", "guitar", "horse", "house", "person"]
for i in item_list:
train(i)