-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
370 lines (285 loc) · 10.1 KB
/
train.py
File metadata and controls
370 lines (285 loc) · 10.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# 导入依赖包
print("导包...")
import os
import json
import torch
import random
import argparse
import numpy as np
from tqdm import tqdm
import torch.nn as nn
import torch.optim as optim
from resnet_50 import ResNet50
from resnet_18 import ResNet18
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
from torchvision import transforms
# 定义训练过程梯度信息列表
shallow_grad_info_list = []
deep_grad_info_list = []
# 钩子函数
def hook_fn_shallow(module,grad_input,grad_output):
global shallow_grad_info_list
shallow_grad_info_list.append(
torch.mean(
torch.abs(
grad_output[0]
)
).item()
)
def hook_fn_deep(module,grad_input,grad_output):
global deep_grad_info_list
deep_grad_info_list.append(
torch.mean(
torch.abs(
grad_output[0]
)
).item()
)
# 程序入口
if __name__ == "__main__":
print("处理参数...")
# 定义参数解析器
parser = argparse.ArgumentParser(description='Train a ResNet on CIFAR')
# 增加命令行参数:残差连接开关
parser.add_argument('-r', '--ResConnection',type = int, default = 1, help='是否启动残差连接')
# 增加命令行参数:BatchNorm开关
parser.add_argument('-b', '--BatchNorm',type = int, default = 1, help='是否启动BatchNorm')
# 增加命令行参数:加载已存在的模型开关
parser.add_argument('-l', '--LoadModel',type = int, default = 0, help='是否加载已存在的模型')
# 增加命令行参数:模型名称
parser.add_argument('-m', '--ModelName',type = str,default = None,help='模型名称')
# 增加命令行参数:训练轮数
parser.add_argument('-n', '--NEpoch',type = int, default = 20, help='训练轮数')
# 增加命令行参数:batch size
parser.add_argument('-s', '--BatchSize',type = int, default = 128, help='batch size')
# 增加命令行参数:模型规模
parser.add_argument('-d', '--Depth',type = int, default = 50, help='模型规模')
# 增加命令行参数:设备
parser.add_argument('-D', '--Device',type = str, default = 'cuda', choices = ['cuda', 'cpu'], help='训练设备')
# 增加命令行参数:随机种子
parser.add_argument('-S', '--Seed',type = int, default = 42, help='随机种子')
# 解析命令行参数
args = parser.parse_args()
# 确定是否使用残差连接
RESCONNECTION = bool(args.ResConnection)
# 确定 BatchNorm 是否启动
BATCHNORM = bool(args.BatchNorm)
# 确定是否加载已存在的模型
LOADMODEL = bool(args.LoadModel)
# 确定模型名称
MODELNAME = args.ModelName
# 确定训练轮数
N_EPOCH = int(args.NEpoch)
# 确定batch size
BATCHSIZE = int(args.BatchSize)
# 确定模型规模
DEPTH = int(args.Depth)
# 确定训练过程中随机行为种子
RANDOM_SEED = int(args.Seed)
print("随机种子",RANDOM_SEED)
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)
torch.cuda.manual_seed(RANDOM_SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# 选择设备
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
print("设备:", device)
# 设置学习率
LR = 0.1
# 确定文件路径
ROOT = os.path.abspath(__file__)
ROOT = os.path.dirname(ROOT)
print("构造模型...")
# 构造模型
if not LOADMODEL:
if DEPTH == 50:
model = ResNet50(
num_classes = 10,
res_connection = RESCONNECTION,
bn = BATCHNORM
).to(device)
else:
model = ResNet18(
num_classes = 10,
res_connection = RESCONNECTION,
bn = BATCHNORM
).to(device)
else:
if DEPTH == 50:
model = ResNet50()
model.load_state_dict(
torch.load(ROOT + "/models_params/" + f"{MODELNAME}.pth")
)
else:
model = ResNet18()
model.load_state_dict(
torch.load(ROOT + "/models_params/" + f"{MODELNAME}.pth")
)
# 设置数据集转换方法
transform = transforms.Compose([
# 调整图像尺寸为224x224
transforms.Resize((224,224)),
# 将图像转换为张量
transforms.ToTensor(),
# 对图像进行归一化
transforms.Normalize(
(0.5, 0.5, 0.5),
(0.5, 0.5, 0.5)
)
])
print("加载数据...")
# 加载数据集
trainset = CIFAR10(
root = f'{ROOT}/data',
train = True,
transform = transform,
download = True
)
testset = CIFAR10(
root = f'{ROOT}/data',
train = False,
transform = transform,
download = True
)
# 更改数据标签为独热编码
trainset.targets = torch.LongTensor(trainset.targets)
testset.targets = torch.LongTensor(testset.targets)
trainset.targets = F.one_hot(trainset.targets,10).float()
testset.targets = F.one_hot(testset.targets,10).float()
# 构造数据加载器
trainloader = DataLoader(
trainset,
batch_size = BATCHSIZE,
shuffle = True,
)
testloader = DataLoader(
testset,
batch_size = BATCHSIZE,
shuffle = False,
)
print("准备训练...")
# 定义损失函数
criterion = nn.CrossEntropyLoss()
# 定义优化器
optimizer = optim.SGD(
model.parameters(),
lr = LR,
)
# 定义训练集准确率列表
train_acc_list = []
# 定义测试集准确率列表
test_acc_list = []
# 注册钩子函数,在训练过程中记录参数的梯度
model.stage0.register_full_backward_hook(hook_fn_shallow)
model.stage4.register_full_backward_hook(hook_fn_deep)
# 训练模型
print('训练开始...')
for epoch in range(N_EPOCH):
# 设置模型为训练模式
model.train()
# 定义训练正确率
train_correct = 0
# 从训练数据中随机抽取一个批次
for i, (inputs, labels) in enumerate(
tqdm(
iterable = trainloader,
desc = f'Epoch {epoch + 1}/{N_EPOCH}'
)
):
# 将输入数据和标签转移到GPU上
inputs = inputs.to(device)
labels = labels.to(device)
# 清空梯度
optimizer.zero_grad()
# 前向传播
outputs = model(inputs)
# 计算损失
loss = criterion(outputs, labels)
# 反向传播
loss.backward()
# 更新参数
optimizer.step()
# 编码预测结果
predicted_indices = torch.argmax(outputs, dim = 1)
predicted_indices = F.one_hot(
predicted_indices,
num_classes = 10
).float()
# 统计训练准确率
train_correct += torch.all(
labels == predicted_indices,
dim = 1
).sum().item()
# 计算训练集准确率
train_accuracy = train_correct / len(trainloader.dataset)
train_accuracy = round(train_accuracy, 4)
# 打印训练结果信息
print(
f'损失[{loss.item()}] 训练集准确率[{train_accuracy}]'
)
# 每1轮训练,保存一次训练集的准确率
if (epoch + 1) % 1 == 0:
train_acc_list.append(train_accuracy)
# 每1轮训练,进行一次测试
if (epoch + 1) % 1 == 0:
# 设置模型为评估模式
model.eval()
# 关闭梯度
with torch.no_grad():
# 定义测试正确率
test_correct = 0
# 从测试数据中随机抽取一个批次
print()
for i, (inputs, labels) in enumerate(
tqdm(
iterable = testloader,
desc = 'Testing ',
)
):
# 将输入数据和标签转移到GPU上
inputs = inputs.to(device)
labels = labels.to(device)
# 前向传播
outputs = model(inputs)
# 编码预测结果
predicted_indices = torch.argmax(outputs, dim = 1)
predicted_indices = F.one_hot(
predicted_indices,
num_classes = 10
).float()
# 统计测试准确率
test_correct += torch.all(
labels == predicted_indices,
dim = 1
).sum().item()
# 计算、保存并打印测试准确率
test_accuracy = test_correct / len(testloader.dataset)
test_accuracy = round(test_accuracy, 4)
test_acc_list.append(test_accuracy)
print("测试准确率" + str(test_accuracy * 100) + "%\n" )
# 保存模型参数
print('\n保存模型参数...')
if MODELNAME != None:
torch.save(model.state_dict(), f'{ROOT}/models_params/' + MODELNAME + '.pth')
else:
torch.save(model.state_dict(), f"{ROOT}/models_params/ResNet{DEPTH}.pth")
# 保存测试参数和结果信息
data_dict = {
"random_seed": RANDOM_SEED,
"n_epochs": N_EPOCH,
"batch_size": BATCHSIZE,
"learning_rate": LR,
"train_acc_list": train_acc_list,
"test_acc_list": test_acc_list,
"shallow_grad_info_list": shallow_grad_info_list,
"deep_grad_info_list": deep_grad_info_list
}
with open(f"{ROOT}/model_data/{MODELNAME}_acc.json", "w") as f:
json.dump(data_dict, f)
print('保存完成!')