-
Notifications
You must be signed in to change notification settings - Fork 0
/
RN_training.py
177 lines (137 loc) · 5.04 KB
/
RN_training.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 copy
from pathlib import Path
import random
from statistics import mean
import numpy as np
import torch
from torch import nn
from tqdm import tqdm
from easyfsl.modules.resnet import MyResNet
random_seed = 0
np.random.seed(random_seed)
torch.manual_seed(random_seed)
random.seed(random_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
n_way = 3
n_shot = 1
n_query = 5
DEVICE = "cuda"
n_workers = 0
from easyfsl.datasets import CUB
from easyfsl.samplers import TaskSampler
from torch.utils.data import DataLoader
# 打印当前工
n_tasks_per_epoch = 500
n_validation_tasks = 100
# Instantiate the datasets
train_set = CUB(split="train", training=True, image_size=224)
val_set = CUB(split="val", training=False, image_size=224)
# Those are special batch samplers that sample few-shot classification tasks with a pre-defined shape
train_sampler = TaskSampler(
train_set, n_way=n_way, n_shot=n_shot, n_query=n_query, n_tasks=n_tasks_per_epoch
)
val_sampler = TaskSampler(
val_set, n_way=n_way, n_shot=n_shot, n_query=n_query, n_tasks=n_validation_tasks
)
# Finally, the DataLoader. We customize the collate_fn so that batches are delivered
# in the shape: (support_images, support_labels, query_images, query_labels, class_ids)
train_loader = DataLoader(
train_set,
batch_sampler=train_sampler,
num_workers=n_workers,
pin_memory=True,
collate_fn=train_sampler.episodic_collate_fn,
)
val_loader = DataLoader(
val_set,
batch_sampler=val_sampler,
num_workers=n_workers,
pin_memory=True,
collate_fn=val_sampler.episodic_collate_fn,
)
from easyfsl.methods import RelationNetworks, FewShotClassifier
from easyfsl.modules import resnet18
# 实例化模型
convolutional_network = MyResNet(original_resnet = resnet18())
# 编码维度和关系提取模块维度一致
few_shot_classifier = RelationNetworks(feature_dimension=512,backbone=convolutional_network).to(DEVICE)
from torch.optim import SGD, Optimizer, Adam
from torch.optim.lr_scheduler import MultiStepLR
from torch.utils.tensorboard import SummaryWriter
LOSS_FUNCTION = nn.CrossEntropyLoss()
n_epochs = 50
scheduler_milestones = [1, 5, 10]
scheduler_gamma = 0.1
learning_rate = 1e-5
tb_logs_dir = Path("./logs")
train_optimizer = Adam(
few_shot_classifier.parameters(), lr=learning_rate, betas=(0.9, 0.999), eps=1e-8, weight_decay=5e-4
)
train_scheduler = MultiStepLR(
train_optimizer,
milestones=scheduler_milestones,
gamma=scheduler_gamma,
)
tb_writer = SummaryWriter(log_dir=str(tb_logs_dir))
def training_epoch( # 训练一轮
model: FewShotClassifier, data_loader: DataLoader, optimizer: Optimizer
):
all_loss = []
model.train()
with tqdm(
enumerate(data_loader), total=len(data_loader), desc="Training"
) as tqdm_train:
for episode_index, (
support_images,
support_labels,
query_images,
query_labels,
_,
) in tqdm_train:
optimizer.zero_grad()
model.process_support_set( # 先处理支持集,得到支持集的原型表示
support_images.to(DEVICE), support_labels.to(DEVICE)
)
classification_scores = model(query_images.to(DEVICE)) # 将查询集喂入模型
loss = LOSS_FUNCTION(classification_scores, query_labels.to(DEVICE))
loss.backward()
optimizer.step()
all_loss.append(loss.item())
tqdm_train.set_postfix(loss=mean(all_loss))
return mean(all_loss)
from easyfsl.utils import evaluate
best_state = few_shot_classifier.state_dict()
best_validation_accuracy = 0.0
for epoch in range(n_epochs):
print(f"Epoch {epoch}")
average_loss = training_epoch(few_shot_classifier, train_loader, train_optimizer)
validation_accuracy = evaluate(
few_shot_classifier, val_loader, device=DEVICE, tqdm_prefix="Validation"
)
if validation_accuracy > best_validation_accuracy:
best_validation_accuracy = validation_accuracy
best_state = copy.deepcopy(few_shot_classifier.state_dict())
# state_dict() returns a reference to the still evolving model's state so we deepcopy
# https://pytorch.org/tutorials/beginner/saving_loading_models
print("Ding ding ding! We found a new best model!")
tb_writer.add_scalar("Train/loss", average_loss, epoch)
tb_writer.add_scalar("Val/acc", validation_accuracy, epoch)
# Warn the scheduler that we did an epoch
# so it knows when to decrease the learning rate
train_scheduler.step()
few_shot_classifier.load_state_dict(best_state)
n_test_tasks = 1000
test_set = CUB(split="test", training=False, image_size=224)
test_sampler = TaskSampler(
test_set, n_way=n_way, n_shot=n_shot, n_query=n_query, n_tasks=n_test_tasks
)
test_loader = DataLoader(
test_set,
batch_sampler=test_sampler,
num_workers=n_workers,
pin_memory=True,
collate_fn=test_sampler.episodic_collate_fn,
)
accuracy = evaluate(few_shot_classifier, test_loader, device=DEVICE)
print(f"Average accuracy : {(100 * accuracy):.2f} %")