-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResNet.py
More file actions
388 lines (320 loc) · 14.4 KB
/
ResNet.py
File metadata and controls
388 lines (320 loc) · 14.4 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os, sys
import time
import gc
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# 导入DeepFlows框架组件
from DeepFlows import tensor
from DeepFlows.tensor import Tensor
from DeepFlows.autograd import no_grad
from DeepFlows import nn
from DeepFlows.optim.sgd import SGD
from DeepFlows.optim.scheduler import StepLR
from DeepFlows.utils import data_loader
from DeepFlows import backend_api
# ResNet基本组件:残差块
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None, device="cuda"):
super().__init__()
# 第一个卷积层
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False, device=device)
self.bn1 = nn.BatchNorm2d(out_channels, device=device)
# 第二个卷积层
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, device=device)
self.bn2 = nn.BatchNorm2d(out_channels, device=device)
# 下采样模块(用于残差连接时的维度匹配)
self.downsample = downsample
self.stride = stride
self.device = device
self.relu = nn.ReLU()
def forward(self, x):
identity = x
# 第一个卷积块
out = self.conv1(x)
out = self.bn1(out)
# 简化实现,暂时跳过激活函数以测试基本功能
# 第二个卷积块
out = self.conv2(out)
out = self.bn2(out)
# 如果需要下采样,对输入进行处理以匹配输出维度
if self.downsample is not None:
# 手动执行下采样步骤
for layer in self.downsample:
identity = layer(identity)
# 残差连接 - 直接使用加法运算符,确保结果是tensor对象
# 确保out和identity都是tensor类型
out = out + identity
# 简化实现,暂时跳过最终激活函数
out = self.relu(out)
return out
# ResNet主模型
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=10, img_size=(32, 32), device="cuda"):
super().__init__()
self.device = device
self.in_channels = 64
# 初始卷积层
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False, device=device)
self.bn1 = nn.BatchNorm2d(64, device=device)
self.relu = nn.ReLU()
# 构建残差层列表
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
# 全连接层:输入为全局平均池化后的通道数(512)
self.fc = nn.Linear(512, num_classes, device=device)
def _make_layer(self, block, out_channels, blocks, stride=1):
# 创建下采样模块(如果需要)
downsample = None
if stride != 1 or self.in_channels != out_channels:
# 手动创建下采样模块
conv = nn.Conv2d(self.in_channels, out_channels, kernel_size=1, stride=stride, bias=False, device=self.device)
bn = nn.BatchNorm2d(out_channels, device=self.device)
downsample = [conv, bn] # 使用列表存储下采样组件
layers = []
# 添加第一个残差块(可能包含下采样)
layers.append(block(self.in_channels, out_channels, stride, downsample, device=self.device))
self.in_channels = out_channels
# 添加剩余的残差块
for _ in range(1, blocks):
layers.append(block(self.in_channels, out_channels, device=self.device))
# 返回层列表,后续在forward中手动调用
return layers
def forward(self, x):
# 确保输入是Tensor对象
from DeepFlows.tensor import Tensor
if not isinstance(x, Tensor):
x = Tensor(x)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
# 手动依次调用每个残差块
for block in self.layer1:
x = block(x)
# 确保每个残差块后的输出都是Tensor对象
if not isinstance(x, Tensor):
x = Tensor(x)
for block in self.layer2:
x = block(x)
if not isinstance(x, Tensor):
x = Tensor(x)
for block in self.layer3:
x = block(x)
if not isinstance(x, Tensor):
x = Tensor(x)
for block in self.layer4:
x = block(x)
if not isinstance(x, Tensor):
x = Tensor(x)
# 简化全局平均池化:由于backend只支持对单个轴进行归约,我们需要分别对每个轴求平均
# 先获取输入形状
batch_size, channels, height, width = x.shape[0], x.shape[1], x.shape[2], x.shape[3]
# 使用更安全的方式计算全局平均池化,对每个轴分别求平均
from DeepFlows import tensor
# 先对height轴(轴2)求平均
x = tensor.mean(x, axis=2)
# 再对width轴(现在变为轴2)求平均
x = tensor.mean(x, axis=2)
# 现在x的形状是[batch, 512],所以我们需要修改全连接层的输入维度
# 全连接层分类
x = self.fc(x)
return x
# 创建ResNet-18模型
def ResNet18(num_classes=10, img_size=(32, 32), device="cuda"):
return ResNet(ResidualBlock, [2, 2, 2, 2], num_classes, img_size, device)
# 数据加载和预处理(与CNN_Animal10_cuda.py相同)
def load_animal_data(root_dir, img_size=(32, 32)):
exts = {'.jpg', '.jpeg', '.png', '.bmp'}
class_names = [d for d in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, d))]
class_names = sorted(class_names)
class_to_idx = {name: i for i, name in enumerate(class_names)}
X, Y = [], []
for cname in class_names:
cdir = os.path.join(root_dir, cname)
for fname in os.listdir(cdir):
ext = os.path.splitext(fname)[1].lower()
if ext in exts:
path = os.path.join(cdir, fname)
img = Image.open(path).convert('RGB').resize(img_size, Image.BILINEAR)
arr = np.asarray(img, dtype=np.uint8)
arr = arr.transpose(2, 0, 1) # 转换为(C, H, W)
X.append(arr)
Y.append(class_to_idx[cname])
x = np.stack(X).astype(np.float32) / 255.0 # 归一化到[0, 1]
y = np.asarray(Y, dtype=np.int32)
# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=1/7, random_state=42, stratify=y
)
# 计算均值和标准差用于标准化
mean_c = x_train.mean(axis=(0, 2, 3), keepdims=True)
std_c = x_train.std(axis=(0, 2, 3), keepdims=True) + 1e-6 # 防止除零
# 标准化数据
x_train = (x_train - mean_c) / std_c
x_test = (x_test - mean_c) / std_c
# 确保数据是连续的,提高性能
x_train = np.ascontiguousarray(x_train)
x_test = np.ascontiguousarray(x_test)
return x_train, y_train, x_test, y_test, class_names, mean_c.squeeze(), std_c.squeeze()
# 数据增强函数
def augment_batch(inputs):
bs = inputs.shape[0]
out = inputs.copy()
# 随机水平翻转
flip_mask = np.random.rand(bs) < 0.5
out[flip_mask] = out[flip_mask][:, :, :, ::-1]
return out
# 主训练函数
def train_resnet():
# 数据路径和参数设置
root_dir = r"./data/Animal"
img_size = (32, 32)
batch_size = 16
num_epochs = 15
learning_rate = 0.01
target_acc = 95.0
# 加载数据
print("正在加载数据...")
x_train, y_train, x_test, y_test, class_names, mean_c, std_c = load_animal_data(root_dir, img_size=img_size)
num_classes = len(class_names)
print(f"数据集加载完成,类别数: {num_classes}")
print(f"训练集大小: {x_train.shape[0]}, 测试集大小: {x_test.shape[0]}")
# 创建OneHotEncoder
encoder = OneHotEncoder(sparse_output=False)
all_classes = np.arange(num_classes).reshape(-1, 1)
encoder.fit(all_classes)
# 创建数据加载器
loader = data_loader(x_train, y_train, batch_size, shuffle=True, prefetch_size=0, as_contiguous=True)
test_loader = data_loader(x_test, y_test, batch_size, shuffle=False, prefetch_size=0, as_contiguous=True)
# 创建模型
print("正在创建ResNet-18模型...")
model = ResNet18(num_classes, img_size=img_size, device='cuda')
# 损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = SGD(model.parameters(), lr=learning_rate, momentum=0.9, weight_decay=0.0005)
scheduler = StepLR(optimizer, step_size=5, gamma=0.5)
# 训练记录
train_losses = []
test_accuracies = []
train_batch_losses = []
test_batch_accuracies = []
total_start_time = time.time()
# 训练循环
for epoch in range(num_epochs):
epoch_start = time.time()
model.train()
running_loss = 0.0
print(f"\nEpoch [{epoch+1}/{num_epochs}] 开始训练...")
for batch_idx, (inputs, labels) in enumerate(loader):
# 数据增强
inputs = augment_batch(inputs)
# Label smoothing
eps = 0.1
labels_onehot = encoder.transform(labels.reshape(-1, 1)).astype(np.float32)
labels_onehot = labels_onehot * (1 - eps) + eps / num_classes
# 转换为Tensor并移至GPU
inputs, labels_onehot = Tensor(inputs, device=backend_api.Device('cuda')), \
Tensor(labels_onehot, device=backend_api.Device('cuda'))
# 前向传播
outputs = model(inputs)
loss = criterion(outputs, labels_onehot)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 记录损失
loss_value = loss.data.numpy().item()
running_loss += loss_value
train_batch_losses.append(loss_value)
# 打印进度
if batch_idx % 2 == 0 or batch_idx + 1 == len(loader.batch_sampler):
print(f"Epoch [{epoch+1}/{num_epochs}] 训练批次 [{batch_idx+1}/{len(loader.batch_sampler)}] 当前Loss: {loss_value:.4f}")
# 释放内存
outputs.dispose()
loss.dispose()
inputs.dispose()
labels_onehot.dispose()
del inputs, labels_onehot, outputs, loss
# 定期回收垃圾
if batch_idx % 50 == 0:
gc.collect()
# 计算并记录平均损失
train_loss = running_loss / len(loader.batch_sampler)
train_losses.append(train_loss)
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {train_loss:.4f} | Time: {time.time()-epoch_start:.2f}s")
# 测试模型
model.eval()
correct = 0
total = 0
print(f"\nEpoch [{epoch+1}/{num_epochs}] 开始测试...")
with no_grad():
for batch_idx, (inputs, labels) in enumerate(test_loader):
# 转换标签为one-hot编码
labels_onehot = encoder.transform(labels.reshape(-1, 1)).astype(np.float32)
# 转换为Tensor并移至GPU
inputs, labels_onehot = Tensor(inputs, device=backend_api.Device('cuda')), \
Tensor(labels_onehot, device=backend_api.Device('cuda'))
# 前向传播
outputs = model(inputs)
# 计算准确率
total += labels_onehot.shape[0]
_pred = np.argmax(outputs.data.numpy(), 1).reshape(-1, 1)
_true = np.argmax(labels_onehot.data.numpy(), 1).reshape(-1, 1)
correct += np.sum(_pred == _true)
# 打印测试进度
if batch_idx % 1 == 0:
current_acc = 100 * correct / total
test_batch_accuracies.append(current_acc)
print(f"Epoch [{epoch+1}/{num_epochs}] 测试批次 [{batch_idx+1}/{len(test_loader.batch_sampler)}] 当前准确率: {current_acc:.2f}%")
# 释放内存
outputs.dispose()
inputs.dispose()
labels_onehot.dispose()
del inputs, labels_onehot, outputs
# 定期回收垃圾
if batch_idx % 20 == 0:
gc.collect()
# 计算并记录测试准确率
accuracy = 100 * correct / total
test_accuracies.append(accuracy)
print(f"Test Accuracy after epoch {epoch+1}: {accuracy:.2f}% | Time: {time.time()-epoch_start:.2f}s")
# 学习率调度
scheduler.step()
# 提前停止条件
if accuracy >= target_acc:
print(f"达到目标准确率 {target_acc:.2f}% ,提前停止训练")
break
# 垃圾回收
gc.collect()
# 最终垃圾回收
gc.collect()
# 打印总训练时间
total_time = time.time() - total_start_time
print(f"Total Training Time: {total_time:.2f}s")
# 可视化训练过程
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(range(1, len(train_batch_losses) + 1), train_batch_losses)
plt.title('训练损失')
plt.xlabel('批次')
plt.ylabel('损失')
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(range(1, len(test_batch_accuracies) + 1), test_batch_accuracies, color='orange')
plt.title('测试准确率')
plt.xlabel('批次')
plt.ylabel('准确率 (%)')
plt.grid(True)
plt.tight_layout()
plt.savefig('resnet_animal_training.png')
print("训练完成!图表已保存为 'resnet_animal_training.png'")
print(f"最终测试准确率: {accuracy:.2f}%")
# 如果作为主程序运行,则执行训练
if __name__ == "__main__":
train_resnet()