Compare commits
No commits in common. "78ca80b1f324841ed3093942c1bbb719bb3e577e" and "d76db395deb8769846c1ad45fbf0a5d8b637aaa9" have entirely different histories.
78ca80b1f3
...
d76db395de
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
raw/
|
dataset/
|
||||||
|
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
@ -1740,19 +1740,6 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"在用时上,由于模型较小,数据集也较小,用时基本没有差别。"
|
"在用时上,由于模型较小,数据集也较小,用时基本没有差别。"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"# 心得体会\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"在本次实验中,我手动实现了基础的神经网络模块,包括全连接层、激活函数、损失函数等。这加深了我对神经网络内部工作原理的理解。通过手动实现的过程,让我更清楚神经网络的各个组成部分是如何协同工作的。\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"我比较不同激活函数和网络结构对模型性能的影响。通过实验发现`sigmoid`函数容易导致梯度消失,而`ReLU`和`Leaky ReLU`等激活函数表现较好。隐藏层的层数和神经元个数也会影响最终的准确率。这让我明白了选择合适的激活函数和网络结构对获得良好性能至关重要。\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"通过系统地调参、训练不同的模型,记录并对比它们的损失曲线和准确率曲线,我也更深入地理解了不同模型设置的优劣。当然本次实验使用的模型还是非常基础的模型,我非常期待在后面的实验中使用复杂的模型,提升我对深度学习的认识。"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
import torch
|
|
||||||
from torch import nn
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
class My_Dropout(nn.Module):
|
|
||||||
def __init__(self, p, **kwargs):
|
|
||||||
super().__init__()
|
|
||||||
self.p = p
|
|
||||||
self.mask = None
|
|
||||||
|
|
||||||
def forward(self, x: torch.Tensor):
|
|
||||||
if self.training:
|
|
||||||
self.mask = (torch.rand(x.shape) > self.p).to(dtype=torch.float32, device=x.device)
|
|
||||||
return x * self.mask / (1 - self.p)
|
|
||||||
else:
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
my_dropout = My_Dropout(p=0.5)
|
|
||||||
nn_dropout = nn.Dropout(p=0.5)
|
|
||||||
x = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0],
|
|
||||||
[6.0, 7.0, 8.0, 9.0, 10.0]])
|
|
||||||
print(f"输入:\n{x}")
|
|
||||||
output_my_dropout = my_dropout(x)
|
|
||||||
output_nn_dropout = nn_dropout(x)
|
|
||||||
print(f"My_Dropout输出:\n{output_my_dropout}")
|
|
||||||
print(f"nn.Dropout输出:\n{output_nn_dropout}")
|
|
@ -1,14 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
learning_rate = 8e-2
|
|
||||||
num_epochs = 101
|
|
||||||
for i in np.arange(3):
|
|
||||||
dropout_rate = 0.1 + 0.4 * i
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=dropout_rate)
|
|
||||||
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
|
|
||||||
print(f"dropout_rate={dropout_rate}")
|
|
||||||
train_loss, test_acc = train_MNIST_CLS(model, optimizer, num_epochs=num_epochs)
|
|
@ -1,49 +0,0 @@
|
|||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
class My_SGD:
|
|
||||||
def __init__(self, params: list[torch.Tensor], lr: float, weight_decay=0.0):
|
|
||||||
self.params = params
|
|
||||||
self.lr = lr
|
|
||||||
self.weight_decay = weight_decay
|
|
||||||
|
|
||||||
def step(self):
|
|
||||||
with torch.no_grad():
|
|
||||||
for param in self.params:
|
|
||||||
if param.grad is not None:
|
|
||||||
if len(param.data.shape) > 1:
|
|
||||||
param.data = param.data - self.lr * (param.grad + self.weight_decay * param.data)
|
|
||||||
else:
|
|
||||||
param.data = param.data - self.lr * param.grad
|
|
||||||
|
|
||||||
def zero_grad(self):
|
|
||||||
for param in self.params:
|
|
||||||
if param.grad is not None:
|
|
||||||
param.grad.data = torch.zeros_like(param.grad.data)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
params1 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
params2 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
|
|
||||||
my_sgd = My_SGD(params=[params1], lr=0.5, weight_decay=0.1)
|
|
||||||
optim_sgd = torch.optim.SGD(params=[params2], lr=0.5, weight_decay=0.1)
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
|
|
||||||
loss1 = 2 * params1.sum()
|
|
||||||
loss2 = 2 * params2.sum()
|
|
||||||
# 偏导为2
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
print("params1的梯度为:\n", params1.grad.data)
|
|
||||||
print("params2的梯度为:\n", params2.grad.data)
|
|
||||||
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
# 结果为:w - lr * grad - lr * weight_decay_rate * w
|
|
||||||
# w[0] = 1 - 0.5 * 2 - 0.5 * 0.1 * 1 = -0.0500
|
|
||||||
# w[1] = 2 - 0.5 * 2 - 0.5 * 0.1 * 2 = 0.9000
|
|
||||||
print("经过L_2正则化后的My_SGD反向传播结果:\n", params1.data)
|
|
||||||
print("经过L_2正则化后的torch.optim.SGD反向传播结果:\n", params2.data)
|
|
@ -1,15 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
learning_rate = 8e-2
|
|
||||||
num_epochs = 101
|
|
||||||
color = ["blue", "green", "orange", "purple"]
|
|
||||||
for i in np.arange(4):
|
|
||||||
weight_decay_rate = i / 4 * 0.01
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=0)
|
|
||||||
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay_rate)
|
|
||||||
print(f"weight_decay_rate={weight_decay_rate}")
|
|
||||||
train_loss, test_acc = train_MNIST_CLS(model, optimizer, num_epochs=num_epochs)
|
|
@ -1,64 +0,0 @@
|
|||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
# 手动实现torch.optim.SGD
|
|
||||||
class My_SGD:
|
|
||||||
def __init__(self, params: list[torch.Tensor], lr: float, weight_decay=0.0, momentum=0.0):
|
|
||||||
self.params = params
|
|
||||||
self.lr = lr
|
|
||||||
self.weight_decay = weight_decay
|
|
||||||
self.momentum = momentum
|
|
||||||
self.velocities = [torch.zeros_like(param.data) for param in params]
|
|
||||||
|
|
||||||
def step(self):
|
|
||||||
with torch.no_grad():
|
|
||||||
for index, param in enumerate(self.params):
|
|
||||||
if param.grad is not None:
|
|
||||||
if self.weight_decay > 0:
|
|
||||||
if len(param.data.shape) > 1:
|
|
||||||
param.grad.data = (param.grad.data + self.weight_decay * param.data)
|
|
||||||
self.velocities[index] = (self.momentum * self.velocities[index] - self.lr * param.grad)
|
|
||||||
param.data = param.data + self.velocities[index]
|
|
||||||
|
|
||||||
def zero_grad(self):
|
|
||||||
for param in self.params:
|
|
||||||
if param.grad is not None:
|
|
||||||
param.grad.data = torch.zeros_like(param.grad.data)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
params1 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
params2 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
|
|
||||||
my_sgd = My_SGD(params=[params1], lr=0.5, momentum=1)
|
|
||||||
optim_sgd = torch.optim.SGD(params=[params2], lr=0.5, momentum=1)
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
|
|
||||||
loss1 = 2 * params1.sum()
|
|
||||||
loss2 = 2 * params2.sum()
|
|
||||||
# 偏导为2
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
# 结果为:w - lr * grad + momentum * velocity
|
|
||||||
# w[0] = 1 - 0.5 * 2 + 1 * 0 = 0
|
|
||||||
# w[1] = 2 - 0.5 * 2 + 1 * 0 = 1
|
|
||||||
print("My_SGD第1次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.SGD第1次反向传播结果:\n", params2.data)
|
|
||||||
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
loss1 = -3 * params1.sum()
|
|
||||||
loss2 = -3 * params2.sum()
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
# 结果为:w - lr * grad + momentum * velocity
|
|
||||||
# w[0] = 0 - 0.5 * -3 + 1 * (-0.5 * 2) = 0.5
|
|
||||||
# w[1] = 1 - 0.5 * -3 + 1 * (-0.5 * 2) = 1.5
|
|
||||||
print("My_SGD第2次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.SGD第2次反向传播结果:\n", params2.data)
|
|
@ -1,63 +0,0 @@
|
|||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
class My_RMSprop:
|
|
||||||
def __init__(self, params: list[torch.Tensor], lr=1e-2, alpha=0.99, eps=1e-8):
|
|
||||||
self.params = params
|
|
||||||
self.lr = lr
|
|
||||||
self.alpha = alpha
|
|
||||||
self.eps = eps
|
|
||||||
self.square_avg = [torch.zeros_like(param.data) for param in params]
|
|
||||||
|
|
||||||
|
|
||||||
def step(self):
|
|
||||||
with torch.no_grad():
|
|
||||||
for index, param in enumerate(self.params):
|
|
||||||
if param.grad is not None:
|
|
||||||
self.square_avg[index] = self.alpha * self.square_avg[index] + (1 - self.alpha) * param.grad ** 2
|
|
||||||
param.data = param.data - self.lr * param.grad / torch.sqrt(self.square_avg[index] + self.eps)
|
|
||||||
|
|
||||||
def zero_grad(self):
|
|
||||||
for param in self.params:
|
|
||||||
if param.grad is not None:
|
|
||||||
param.grad.data = torch.zeros_like(param.grad.data)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
params1 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
params2 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
|
|
||||||
my_sgd = My_RMSprop(params=[params1], lr=1, alpha=0.5, eps=1e-8)
|
|
||||||
optim_sgd = torch.optim.RMSprop(params=[params2], lr=1, alpha=0.5, eps=1e-8)
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
|
|
||||||
loss1 = 2 * params1.sum()
|
|
||||||
loss2 = 2 * params2.sum()
|
|
||||||
# 偏导为2
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
# s = alpha * s + (1-alpha) * grad^2 = 0.5 * 0 + (1-0.5) * 2^2 = 2
|
|
||||||
# w = w - lr * grad * (s + eps)^0.5
|
|
||||||
# w[0] = 1 - 1 * 2 / (2 + 1e-8)^0.5 ~= -0.41
|
|
||||||
# w[1] = 2 - 1 * 2 / (2 + 1e-8)^0.5 ~= -0.59
|
|
||||||
print("My_RMSprop第1次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.RMSprop第1次反向传播结果:\n", params2.data)
|
|
||||||
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
loss1 = -3 * params1.sum()
|
|
||||||
loss2 = -3 * params2.sum()
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
# s = alpha * s + (1-alpha) * grad^2 = 0.5 * 2 + (1-0.5) * (-3)^2 = 5.5
|
|
||||||
# w - lr * grad * (s + eps)^0.5
|
|
||||||
# w[0] = -0.41 - 1 * -3 / (5.5 + 1e-8)^0.5 ~= 0.87
|
|
||||||
# w[1] = 0.59 - 1 * -3 / (5.5 + 1e-8)^0.5 ~= 1.86
|
|
||||||
print("My_RMSprop第2次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.RMSprop第2次反向传播结果:\n", params2.data)
|
|
@ -1,64 +0,0 @@
|
|||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
|
|
||||||
class My_Adam:
|
|
||||||
def __init__(self, params: list[torch.Tensor], lr=1e-3, betas=(0.9, 0.999), eps=1e-8):
|
|
||||||
self.params = params
|
|
||||||
self.lr = lr
|
|
||||||
self.beta1 = betas[0]
|
|
||||||
self.beta2 = betas[1]
|
|
||||||
self.eps = eps
|
|
||||||
self.t = 0
|
|
||||||
self.momentums = [torch.zeros_like(param.data) for param in params]
|
|
||||||
self.velocities = [torch.zeros_like(param.data) for param in params]
|
|
||||||
|
|
||||||
def step(self):
|
|
||||||
self.t += 1
|
|
||||||
with torch.no_grad():
|
|
||||||
for index, param in enumerate(self.params):
|
|
||||||
if param.grad is not None:
|
|
||||||
self.momentums[index] = (self.beta1 * self.momentums[index] + (1 - self.beta1) * param.grad)
|
|
||||||
self.velocities[index] = (self.beta2 * self.velocities[index] + (1 - self.beta2) * param.grad ** 2)
|
|
||||||
|
|
||||||
momentums_hat = self.momentums[index] / (1 - self.beta1 ** self.t)
|
|
||||||
velocities_hat = self.velocities[index] / (1 - self.beta2 ** self.t)
|
|
||||||
|
|
||||||
param.data = param.data - self.lr * momentums_hat / (torch.sqrt(velocities_hat) + self.eps)
|
|
||||||
|
|
||||||
def zero_grad(self):
|
|
||||||
for param in self.params:
|
|
||||||
if param.grad is not None:
|
|
||||||
param.grad.data = torch.zeros_like(param.grad.data)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
params1 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
params2 = torch.tensor([[1.0, 2.0]], requires_grad=True)
|
|
||||||
|
|
||||||
my_sgd = My_Adam(params=[params1], lr=1, betas=(0.5, 0.5), eps=1e-8)
|
|
||||||
optim_sgd = torch.optim.Adam(params=[params2], lr=1, betas=(0.5, 0.5), eps=1e-8)
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
|
|
||||||
loss1 = 2 * params1.sum()
|
|
||||||
loss2 = 2 * params2.sum()
|
|
||||||
# 偏导为2
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
print("My_Adam第1次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.Adam第1次反向传播结果:\n", params2.data)
|
|
||||||
|
|
||||||
my_sgd.zero_grad()
|
|
||||||
optim_sgd.zero_grad()
|
|
||||||
loss1 = -3 * params1.sum()
|
|
||||||
loss2 = -3 * params2.sum()
|
|
||||||
# 偏导为-3
|
|
||||||
loss1.backward()
|
|
||||||
loss2.backward()
|
|
||||||
my_sgd.step()
|
|
||||||
optim_sgd.step()
|
|
||||||
print("My_Adam第2次反向传播结果:\n", params1.data)
|
|
||||||
print("torch.optim.Adam第2次反向传播结果:\n", params2.data)
|
|
@ -1,23 +0,0 @@
|
|||||||
import torch
|
|
||||||
from utils import *
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
learning_rate = 5e-2
|
|
||||||
num_epochs = 161
|
|
||||||
color = ["blue", "green", "orange"]
|
|
||||||
optim_names = ["SGD", "RMSprop", "Adam"]
|
|
||||||
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=0)
|
|
||||||
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)
|
|
||||||
print(f"optimizer: SGD")
|
|
||||||
train_loss, test_acc = train_MNIST_CLS(model, optimizer, num_epochs=num_epochs)
|
|
||||||
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=0)
|
|
||||||
optimizer = torch.optim.RMSprop(model.parameters(), lr=learning_rate, alpha=0.99, eps=1e-8)
|
|
||||||
print(f"optimizer: RMSprop")
|
|
||||||
train_loss, test_acc = train_MNIST_CLS(model, optimizer, num_epochs=num_epochs)
|
|
||||||
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=0)
|
|
||||||
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, betas=(0.9, 0.999), eps=1e-8)
|
|
||||||
print(f"optimizer: Adam")
|
|
||||||
train_loss, test_acc = train_MNIST_CLS(model, optimizer, num_epochs=num_epochs)
|
|
101
Lab3/code/4.py
101
Lab3/code/4.py
@ -1,101 +0,0 @@
|
|||||||
import torch
|
|
||||||
from torch import nn
|
|
||||||
from utils import *
|
|
||||||
from torch.utils.data import random_split
|
|
||||||
|
|
||||||
|
|
||||||
learning_rate = 1e-3
|
|
||||||
num_epochs = 161
|
|
||||||
batch_size = 8192
|
|
||||||
num_classes = 10
|
|
||||||
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
transform = transforms.Compose(
|
|
||||||
[
|
|
||||||
transforms.ToTensor(),
|
|
||||||
transforms.Normalize((0.5,), (0.5,)),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
train_mnist_dataset = datasets.MNIST(root="../dataset", train=True, transform=transform, download=True)
|
|
||||||
test_mnist_dataset = datasets.MNIST(root="../dataset", train=False, transform=transform, download=True)
|
|
||||||
|
|
||||||
train_dataset_length = int(0.8 * len(train_mnist_dataset))
|
|
||||||
val_dataset_length = len(train_mnist_dataset) - train_dataset_length
|
|
||||||
train_mnist_dataset, val_mnist_dataset = random_split(
|
|
||||||
train_mnist_dataset,
|
|
||||||
[train_dataset_length, val_dataset_length],
|
|
||||||
generator=torch.Generator().manual_seed(42),
|
|
||||||
)
|
|
||||||
|
|
||||||
train_loader = DataLoader(dataset=train_mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=14, pin_memory=True)
|
|
||||||
val_loader = DataLoader(dataset=val_mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=14, pin_memory=True)
|
|
||||||
test_loader = DataLoader(dataset=test_mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=14, pin_memory=True)
|
|
||||||
|
|
||||||
model = MNIST_CLS_Model(num_classes=10, dropout_rate=0.2).to(device)
|
|
||||||
criterion = nn.CrossEntropyLoss()
|
|
||||||
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, betas=(0.9, 0.999), eps=1e-8, weight_decay=0)
|
|
||||||
|
|
||||||
early_stopping_patience = 5
|
|
||||||
best_val_loss = float("inf")
|
|
||||||
current_patience = 0
|
|
||||||
|
|
||||||
train_loss = list()
|
|
||||||
test_acc = list()
|
|
||||||
val_loss = list()
|
|
||||||
for epoch in range(num_epochs):
|
|
||||||
model.train()
|
|
||||||
total_epoch_loss = 0
|
|
||||||
for index, (images, targets) in tqdm(enumerate(train_loader), total=len(train_loader)):
|
|
||||||
optimizer.zero_grad()
|
|
||||||
|
|
||||||
images = images.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
one_hot_targets = one_hot(targets, num_classes=num_classes).to(dtype=torch.float)
|
|
||||||
|
|
||||||
outputs = model(images)
|
|
||||||
loss = criterion(outputs, one_hot_targets)
|
|
||||||
total_epoch_loss += loss.item()
|
|
||||||
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
|
|
||||||
model.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
total_epoch_acc = 0
|
|
||||||
for index, (image, targets) in tqdm(enumerate(test_loader), total=len(test_loader)):
|
|
||||||
image = image.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
|
|
||||||
outputs = model(image)
|
|
||||||
pred = softmax(outputs, dim=1)
|
|
||||||
total_epoch_acc += (pred.argmax(1) == targets).sum().item()
|
|
||||||
avg_epoch_acc = total_epoch_acc / len(test_mnist_dataset)
|
|
||||||
|
|
||||||
val_total_epoch_loss = 0
|
|
||||||
for index, (image, targets) in tqdm(enumerate(val_loader), total=len(test_loader)):
|
|
||||||
image = image.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
one_hot_targets = one_hot(targets, num_classes=num_classes).to(dtype=torch.float)
|
|
||||||
|
|
||||||
outputs = model(image)
|
|
||||||
loss = criterion(outputs, one_hot_targets)
|
|
||||||
val_total_epoch_loss += loss.item()
|
|
||||||
|
|
||||||
print(
|
|
||||||
f"Epoch [{epoch + 1}/{num_epochs}],",
|
|
||||||
f"Train Loss: {total_epoch_loss:.10f},",
|
|
||||||
f"Test Acc: {avg_epoch_acc * 100:.3f}%,",
|
|
||||||
f"Val Loss: {val_total_epoch_loss:.10f}",
|
|
||||||
)
|
|
||||||
train_loss.append(total_epoch_loss)
|
|
||||||
test_acc.append(avg_epoch_acc * 100)
|
|
||||||
val_loss.append(val_total_epoch_loss)
|
|
||||||
|
|
||||||
if val_total_epoch_loss < best_val_loss:
|
|
||||||
best_val_loss = val_total_epoch_loss
|
|
||||||
current_patience = 0
|
|
||||||
else:
|
|
||||||
current_patience += 1
|
|
||||||
if current_patience >= early_stopping_patience:
|
|
||||||
print(f"Early stopping after {epoch + 1} epochs.")
|
|
||||||
break
|
|
@ -1,106 +0,0 @@
|
|||||||
import torch
|
|
||||||
from torch.nn.functional import *
|
|
||||||
from torch.utils.data import DataLoader
|
|
||||||
from torch import nn
|
|
||||||
from torchvision import datasets, transforms
|
|
||||||
from tqdm import tqdm
|
|
||||||
|
|
||||||
import ipdb
|
|
||||||
|
|
||||||
|
|
||||||
class MNIST_CLS_Model(nn.Module):
|
|
||||||
def __init__(self, num_classes, dropout_rate=0.5):
|
|
||||||
super().__init__()
|
|
||||||
self.flatten = nn.Flatten()
|
|
||||||
self.fc1 = nn.Linear(in_features=28 * 28, out_features=1024)
|
|
||||||
self.fc2 = nn.Linear(in_features=1024, out_features=num_classes)
|
|
||||||
self.dropout = nn.Dropout(p=dropout_rate)
|
|
||||||
|
|
||||||
def forward(self, x: torch.Tensor):
|
|
||||||
x = self.flatten(x)
|
|
||||||
x = torch.relu(self.fc1(x))
|
|
||||||
x = self.dropout(x)
|
|
||||||
x = self.fc2(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
def train_MNIST_CLS(model, optimizer, num_epochs):
|
|
||||||
batch_size = 8192
|
|
||||||
num_classes = 10
|
|
||||||
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
transform = transforms.Compose(
|
|
||||||
[
|
|
||||||
transforms.ToTensor(),
|
|
||||||
transforms.Normalize((0.5,), (0.5,)),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
train_mnist_dataset = datasets.MNIST(
|
|
||||||
root="../dataset", train=True, transform=transform, download=True
|
|
||||||
)
|
|
||||||
test_mnist_dataset = datasets.MNIST(
|
|
||||||
root="../dataset", train=False, transform=transform, download=True
|
|
||||||
)
|
|
||||||
train_loader = DataLoader(
|
|
||||||
dataset=train_mnist_dataset,
|
|
||||||
batch_size=batch_size,
|
|
||||||
shuffle=True,
|
|
||||||
num_workers=14,
|
|
||||||
pin_memory=True,
|
|
||||||
)
|
|
||||||
test_loader = DataLoader(
|
|
||||||
dataset=test_mnist_dataset,
|
|
||||||
batch_size=batch_size,
|
|
||||||
shuffle=True,
|
|
||||||
num_workers=14,
|
|
||||||
pin_memory=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
model = model.to(device)
|
|
||||||
criterion = nn.CrossEntropyLoss()
|
|
||||||
|
|
||||||
train_loss = list()
|
|
||||||
test_acc = list()
|
|
||||||
for epoch in range(num_epochs):
|
|
||||||
model.train()
|
|
||||||
total_epoch_loss = 0
|
|
||||||
for index, (images, targets) in tqdm(
|
|
||||||
enumerate(train_loader), total=len(train_loader)
|
|
||||||
):
|
|
||||||
optimizer.zero_grad()
|
|
||||||
|
|
||||||
images = images.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
one_hot_targets = one_hot(targets, num_classes=num_classes).to(
|
|
||||||
dtype=torch.float
|
|
||||||
)
|
|
||||||
|
|
||||||
outputs = model(images)
|
|
||||||
loss = criterion(outputs, one_hot_targets)
|
|
||||||
total_epoch_loss += loss.item()
|
|
||||||
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
|
|
||||||
model.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
total_epoch_acc = 0
|
|
||||||
for index, (image, targets) in tqdm(
|
|
||||||
enumerate(test_loader), total=len(test_loader)
|
|
||||||
):
|
|
||||||
image = image.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
|
|
||||||
outputs = model(image)
|
|
||||||
pred = softmax(outputs, dim=1)
|
|
||||||
total_epoch_acc += (pred.argmax(1) == targets).sum().item()
|
|
||||||
|
|
||||||
avg_epoch_acc = total_epoch_acc / len(test_mnist_dataset)
|
|
||||||
print(
|
|
||||||
f"Epoch [{epoch + 1}/{num_epochs}],",
|
|
||||||
f"Train Loss: {total_epoch_loss:.10f},",
|
|
||||||
f"Test Acc: {avg_epoch_acc * 100:.3f}%,",
|
|
||||||
)
|
|
||||||
train_loss.append(total_epoch_loss)
|
|
||||||
test_acc.append(avg_epoch_acc * 100)
|
|
||||||
return train_loss, test_acc
|
|
1143
Lab3/网络优化实验.ipynb
1143
Lab3/网络优化实验.ipynb
File diff suppressed because one or more lines are too long
@ -1,73 +0,0 @@
|
|||||||
from utils import *
|
|
||||||
import ipdb
|
|
||||||
|
|
||||||
|
|
||||||
class My_Conv2d(nn.Module):
|
|
||||||
def __init__(self, in_channels:int, out_channels:int, kernel_size:int, padding:int=0, bias=True):
|
|
||||||
super(My_Conv2d, self).__init__()
|
|
||||||
self.has_bias = bias
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.out_channels = out_channels
|
|
||||||
self.kernel_size = kernel_size
|
|
||||||
self.padding = padding
|
|
||||||
self.weight = nn.Parameter(torch.Tensor(out_channels, in_channels, kernel_size, kernel_size))
|
|
||||||
nn.init.xavier_uniform_(self.weight)
|
|
||||||
if self.has_bias:
|
|
||||||
self.bias = nn.Parameter(torch.zeros(out_channels, requires_grad=True, dtype=torch.float32))
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
batch_size, _, input_height, input_width = x.shape
|
|
||||||
if self.padding > 0:
|
|
||||||
x = F.pad(x, (self.padding, self.padding, self.padding, self.padding))
|
|
||||||
x = F.unfold(x, kernel_size=self.kernel_size)
|
|
||||||
x = x.permute(0, 2, 1).contiguous()
|
|
||||||
weight_unfold = self.weight.view(self.out_channels, -1).t()
|
|
||||||
x = torch.matmul(x, weight_unfold)
|
|
||||||
if self.has_bias:
|
|
||||||
x += self.bias
|
|
||||||
output_height = input_height + 2 * self.padding - self.kernel_size + 1
|
|
||||||
output_width = input_width + 2 * self.padding - self.kernel_size + 1
|
|
||||||
x = x.view(batch_size, output_height, output_width, self.out_channels).permute(0, 3, 1, 2).contiguous()
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_1(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_1, self).__init__()
|
|
||||||
self.conv1 = My_Conv2d(in_channels=3, out_channels=128, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn1 = nn.BatchNorm2d(128)
|
|
||||||
self.conv2 = My_Conv2d(in_channels=128, out_channels=512, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn2 = nn.BatchNorm2d(512)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.bn1(self.conv1(x)))
|
|
||||||
x = F.relu(self.bn2(self.conv2(x)))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_1(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_1, self).__init__()
|
|
||||||
self.conv1 = My_Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn1 = nn.BatchNorm2d(16)
|
|
||||||
self.conv2 = My_Conv2d(in_channels=16, out_channels=48, kernel_size=5, padding=2, bias=False)
|
|
||||||
self.bn2 = nn.BatchNorm2d(48)
|
|
||||||
self.conv3 = My_Conv2d(in_channels=48, out_channels=3, kernel_size=3, padding=1)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.bn1(self.conv1(x)))
|
|
||||||
x = F.relu(self.bn2(self.conv2(x)))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
model = Model_Vehicle_CLS_1_1()
|
|
||||||
train_Vehicle_CLS(model=model, learning_rate=4e-4, batch_size=256)
|
|
||||||
|
|
||||||
model = Model_Haze_Removal_1_1()
|
|
||||||
train_Haze_Removal(model=model, learning_rate=5e-3, batch_size=16)
|
|
@ -1,43 +0,0 @@
|
|||||||
from utils import *
|
|
||||||
import ipdb
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_2(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_2, self).__init__()
|
|
||||||
self.conv1 = nn.Conv2d(in_channels=3, out_channels=128, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn1 = nn.BatchNorm2d(128)
|
|
||||||
self.conv2 = nn.Conv2d(in_channels=128, out_channels=512, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn2 = nn.BatchNorm2d(512)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.bn1(self.conv1(x)))
|
|
||||||
x = F.relu(self.bn2(self.conv2(x)))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_2(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_2, self).__init__()
|
|
||||||
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, bias=False)
|
|
||||||
self.bn1 = nn.BatchNorm2d(16)
|
|
||||||
self.conv2 = nn.Conv2d(in_channels=16, out_channels=48, kernel_size=5, padding=2, bias=False)
|
|
||||||
self.bn2 = nn.BatchNorm2d(48)
|
|
||||||
self.conv3 = nn.Conv2d(in_channels=48, out_channels=3, kernel_size=3, padding=1)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.bn1(self.conv1(x)))
|
|
||||||
x = F.relu(self.bn2(self.conv2(x)))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
model = Model_Vehicle_CLS_1_2()
|
|
||||||
train_Vehicle_CLS(model=model, learning_rate=4e-4, batch_size=64)
|
|
||||||
|
|
||||||
model = Model_Haze_Removal_1_2()
|
|
||||||
train_Haze_Removal(model=model, learning_rate=5e-3, batch_size=16)
|
|
215
Lab4/code/1.3.py
215
Lab4/code/1.3.py
@ -1,215 +0,0 @@
|
|||||||
from utils import *
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_3_1(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_3_1, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=512, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_3_2(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_3_2, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=128, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=512, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes, bias=False)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_3_3(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_3_3, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = F.relu(self.conv3(x))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_1_3_4(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_1_3_4, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
)
|
|
||||||
self.conv4 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = F.relu(self.conv3(x))
|
|
||||||
x = F.relu(self.conv4(x))
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_3_1(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_3_1, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=48, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(48),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Conv2d(in_channels=48, out_channels=3, kernel_size=3, padding=1)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_3_2(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_3_2, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5, padding=2, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=48, kernel_size=5, padding=2, bias=False),
|
|
||||||
nn.BatchNorm2d(48),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Conv2d(in_channels=48, out_channels=3, kernel_size=5, padding=2)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_3_3(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_3_3, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=7, padding=3, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=48, kernel_size=7, padding=3, bias=False),
|
|
||||||
nn.BatchNorm2d(48),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Conv2d(in_channels=48, out_channels=3, kernel_size=7, padding=3)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Haze_Removal_1_3_4(nn.Module):
|
|
||||||
def __init__(self):
|
|
||||||
super(Model_Haze_Removal_1_3_4, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=9, padding=4, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=48, kernel_size=9, padding=4, bias=False),
|
|
||||||
nn.BatchNorm2d(48),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv3 = nn.Conv2d(in_channels=48, out_channels=3, kernel_size=9, padding=4)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = F.relu(self.conv2(x))
|
|
||||||
x = self.conv3(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
num_epochs = 61
|
|
||||||
learning_rate = 2e-4
|
|
||||||
batch_size = 256
|
|
||||||
models = [
|
|
||||||
Model_Vehicle_CLS_1_3_1,
|
|
||||||
Model_Vehicle_CLS_1_3_2,
|
|
||||||
Model_Vehicle_CLS_1_3_3,
|
|
||||||
Model_Vehicle_CLS_1_3_4,
|
|
||||||
]
|
|
||||||
for i in range(4):
|
|
||||||
model = models[i]()
|
|
||||||
print(f"卷积层层数={i + 1}")
|
|
||||||
train_loss, test_acc = train_Vehicle_CLS(model=model, learning_rate=learning_rate,
|
|
||||||
batch_size=batch_size, num_epochs=num_epochs)
|
|
||||||
print()
|
|
||||||
|
|
||||||
num_epochs = 61
|
|
||||||
learning_rate = 8e-3
|
|
||||||
batch_size = 64
|
|
||||||
models = [
|
|
||||||
Model_Haze_Removal_1_3_1,
|
|
||||||
Model_Haze_Removal_1_3_2,
|
|
||||||
Model_Haze_Removal_1_3_3,
|
|
||||||
Model_Haze_Removal_1_3_4,
|
|
||||||
]
|
|
||||||
for i in range(4):
|
|
||||||
model = models[i]()
|
|
||||||
print(f"卷积核大小={3 + 2 * i}")
|
|
||||||
train_loss, test_loss = train_Haze_Removal(model=model, learning_rate=learning_rate, batch_size=batch_size, num_epochs=num_epochs)
|
|
||||||
print()
|
|
168
Lab4/code/2.py
168
Lab4/code/2.py
@ -1,168 +0,0 @@
|
|||||||
from utils import *
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_2_1(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_2_1, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(32),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.conv1(x)
|
|
||||||
x = self.conv2(x)
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_2_2(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_2_2, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=2, dilation=2, bias=False),
|
|
||||||
nn.BatchNorm2d(32),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=5, dilation=5, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=2, dilation=2, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=5, dilation=5, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.conv1(x)
|
|
||||||
x = self.conv2(x)
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_2_3(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_2_3, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=3, dilation=3, bias=False),
|
|
||||||
nn.BatchNorm2d(32),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=5, dilation=5, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=3, dilation=3, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=5, dilation=5, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.conv1(x)
|
|
||||||
x = self.conv2(x)
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_2_4(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_2_4, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(16),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=3, dilation=3, bias=False),
|
|
||||||
nn.BatchNorm2d(32),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=7, dilation=7, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.conv2 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, dilation=1, bias=False),
|
|
||||||
nn.BatchNorm2d(128),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=3, dilation=3, bias=False),
|
|
||||||
nn.BatchNorm2d(256),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=7, dilation=7, bias=False),
|
|
||||||
nn.BatchNorm2d(512),
|
|
||||||
nn.ReLU(inplace=True),
|
|
||||||
)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.conv1(x)
|
|
||||||
x = self.conv2(x)
|
|
||||||
x = F.avg_pool2d(x, 32)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
num_epochs = 61
|
|
||||||
learning_rate = 1e-4
|
|
||||||
batch_size = 256
|
|
||||||
dilations = ["[[1, 1, 1], [1, 1, 1]]",
|
|
||||||
"[[1, 2, 5], [1, 2, 5]]",
|
|
||||||
"[[1, 3, 5], [1, 3, 5]]",
|
|
||||||
"[[1, 3, 7], [1, 3, 7]]"]
|
|
||||||
models = [
|
|
||||||
Model_Vehicle_CLS_2_1,
|
|
||||||
Model_Vehicle_CLS_2_2,
|
|
||||||
Model_Vehicle_CLS_2_3,
|
|
||||||
Model_Vehicle_CLS_2_4,
|
|
||||||
]
|
|
||||||
for i in range(4):
|
|
||||||
model = models[i]()
|
|
||||||
print("Dilation: " + dilations[i])
|
|
||||||
train_loss, test_acc = train_Vehicle_CLS(model=model, learning_rate=learning_rate,
|
|
||||||
batch_size=batch_size, num_epochs=num_epochs)
|
|
||||||
print()
|
|
||||||
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
|||||||
from utils import *
|
|
||||||
|
|
||||||
class BasicResidualBlock(nn.Module):
|
|
||||||
def __init__(self, in_channels, out_channels, stride=1):
|
|
||||||
super(BasicResidualBlock, self).__init__()
|
|
||||||
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
|
|
||||||
self.bn1 = nn.BatchNorm2d(out_channels)
|
|
||||||
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
|
|
||||||
self.bn2 = nn.BatchNorm2d(out_channels)
|
|
||||||
self.shortcut = nn.Sequential()
|
|
||||||
if stride != 1 or in_channels != out_channels:
|
|
||||||
self.shortcut = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
|
|
||||||
nn.BatchNorm2d(out_channels)
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
out = F.relu(self.bn1(self.conv1(x)))
|
|
||||||
out = self.bn2(self.conv2(out))
|
|
||||||
out += self.shortcut(x)
|
|
||||||
out = F.relu(out)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class Model_Vehicle_CLS_3(nn.Module):
|
|
||||||
def __init__(self, num_classes=3):
|
|
||||||
super(Model_Vehicle_CLS_3, self).__init__()
|
|
||||||
self.conv1 = nn.Sequential(
|
|
||||||
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(64),
|
|
||||||
)
|
|
||||||
self.conv2 = BasicResidualBlock(in_channels=64, out_channels=64)
|
|
||||||
self.conv3 = BasicResidualBlock(in_channels=64, out_channels=64)
|
|
||||||
self.conv4 = BasicResidualBlock(in_channels=64, out_channels=128, stride=2)
|
|
||||||
self.conv5 = BasicResidualBlock(in_channels=128, out_channels=128)
|
|
||||||
self.conv6 = BasicResidualBlock(in_channels=128, out_channels=256, stride=2)
|
|
||||||
self.conv7 = BasicResidualBlock(in_channels=256, out_channels=256)
|
|
||||||
self.conv8 = BasicResidualBlock(in_channels=256, out_channels=512, stride=2)
|
|
||||||
self.conv9 = BasicResidualBlock(in_channels=512, out_channels=512)
|
|
||||||
self.fc = nn.Linear(in_features=512, out_features=num_classes)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.relu(self.conv1(x))
|
|
||||||
x = self.conv2(x)
|
|
||||||
x = self.conv3(x)
|
|
||||||
x = self.conv4(x)
|
|
||||||
x = self.conv5(x)
|
|
||||||
x = self.conv6(x)
|
|
||||||
x = self.conv7(x)
|
|
||||||
x = self.conv8(x)
|
|
||||||
x = self.conv9(x)
|
|
||||||
x = F.avg_pool2d(x, 4)
|
|
||||||
x = x.view(x.size(0), -1)
|
|
||||||
x = self.fc(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
num_epochs = 61
|
|
||||||
learning_rate = 15e-5
|
|
||||||
batch_size = 512
|
|
||||||
model = Model_Vehicle_CLS_3(num_classes=3)
|
|
||||||
train_loss, test_acc = train_Vehicle_CLS(model=model, learning_rate=learning_rate, batch_size=batch_size, num_epochs=num_epochs)
|
|
@ -1,197 +0,0 @@
|
|||||||
import torch
|
|
||||||
import torch.nn.functional as F
|
|
||||||
from torch.utils.data import DataLoader, Dataset
|
|
||||||
from torch import nn
|
|
||||||
from torchvision import transforms
|
|
||||||
from tqdm import tqdm
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
from PIL import Image
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
class Vehicle(Dataset):
|
|
||||||
def __init__(self, root:str="../dataset", train:bool=True, transform=None):
|
|
||||||
root = os.path.join(root, "Vehicles")
|
|
||||||
csv_file = os.path.join(root, "train.csv" if train else "test.csv")
|
|
||||||
self.data = pd.read_csv(csv_file).to_numpy().tolist()
|
|
||||||
self.root = root
|
|
||||||
self.transform = transform
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.data)
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
img_name, label = self.data[index]
|
|
||||||
img_path = os.path.join(self.root, img_name)
|
|
||||||
image = Image.open(img_path)
|
|
||||||
label = int(label)
|
|
||||||
if self.transform:
|
|
||||||
image = self.transform(image)
|
|
||||||
return image, label
|
|
||||||
|
|
||||||
|
|
||||||
class Haze(Dataset):
|
|
||||||
def __init__(self, root:str="../dataset", train:bool=True, transform=None):
|
|
||||||
root = os.path.join(root, "Haze")
|
|
||||||
split_file = pd.read_csv(os.path.join(root, "split.csv")).to_numpy().tolist()
|
|
||||||
self.data = list()
|
|
||||||
for img, is_train in split_file:
|
|
||||||
if train and int(is_train) == 1:
|
|
||||||
self.data.append(img)
|
|
||||||
elif not train and int(is_train) == 0:
|
|
||||||
self.data.append(img)
|
|
||||||
self.root = root
|
|
||||||
self.transform = transform
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.data)
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
img_name = self.data[index]
|
|
||||||
img_path = os.path.join(self.root, "raw/haze", img_name)
|
|
||||||
ground_truth_path = os.path.join(self.root, "raw/no_haze", img_name)
|
|
||||||
image = Image.open(img_path)
|
|
||||||
ground_truth = Image.open(ground_truth_path)
|
|
||||||
if self.transform:
|
|
||||||
image = self.transform(image)
|
|
||||||
ground_truth = self.transform(ground_truth)
|
|
||||||
return image, ground_truth
|
|
||||||
|
|
||||||
|
|
||||||
def train_Vehicle_CLS(model:nn.Module, learning_rate=1e-3, batch_size=64, num_epochs=51):
|
|
||||||
num_classes = 3
|
|
||||||
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
transform = transforms.Compose(
|
|
||||||
[
|
|
||||||
transforms.ToTensor(),
|
|
||||||
transforms.Resize((32, 32), antialias=True),
|
|
||||||
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
train_dataset = Vehicle(root="../dataset", train=True, transform=transform)
|
|
||||||
test_dataset = Vehicle(root="../dataset", train=False, transform=transform)
|
|
||||||
|
|
||||||
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=14, pin_memory=True)
|
|
||||||
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, num_workers=14, pin_memory=True)
|
|
||||||
|
|
||||||
model = model.to(device)
|
|
||||||
criterion = nn.CrossEntropyLoss()
|
|
||||||
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
|
|
||||||
|
|
||||||
train_loss = list()
|
|
||||||
test_acc = list()
|
|
||||||
for epoch in range(num_epochs):
|
|
||||||
model.train()
|
|
||||||
total_epoch_loss = 0
|
|
||||||
train_tik = time.time()
|
|
||||||
for index, (images, targets) in tqdm(enumerate(train_loader), total=len(train_loader)):
|
|
||||||
optimizer.zero_grad()
|
|
||||||
|
|
||||||
images = images.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
one_hot_targets = F.one_hot(targets, num_classes=num_classes).to(dtype=torch.float)
|
|
||||||
|
|
||||||
outputs = model(images)
|
|
||||||
loss = criterion(outputs, one_hot_targets)
|
|
||||||
total_epoch_loss += loss.item()
|
|
||||||
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
train_tok = time.time()
|
|
||||||
|
|
||||||
model.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
total_epoch_acc = 0
|
|
||||||
test_tik = time.time()
|
|
||||||
for index, (image, targets) in tqdm(enumerate(test_loader), total=len(test_loader)):
|
|
||||||
image = image.to(device)
|
|
||||||
targets = targets.to(device)
|
|
||||||
|
|
||||||
outputs = model(image)
|
|
||||||
pred = F.softmax(outputs, dim=1)
|
|
||||||
total_epoch_acc += (pred.argmax(1) == targets).sum().item()
|
|
||||||
test_tok = time.time()
|
|
||||||
|
|
||||||
avg_epoch_acc = total_epoch_acc / len(test_dataset)
|
|
||||||
print(
|
|
||||||
f"Epoch [{epoch + 1}/{num_epochs}],",
|
|
||||||
f"Train Loss: {total_epoch_loss:.10f},",
|
|
||||||
f"Train Time: {1000 * (train_tok - train_tik):.2f}ms,",
|
|
||||||
f"Test Acc: {avg_epoch_acc * 100:.3f}%,",
|
|
||||||
f"Test Time: {1000 * (test_tok - test_tik):.2f}ms"
|
|
||||||
)
|
|
||||||
train_loss.append(total_epoch_loss)
|
|
||||||
test_acc.append(avg_epoch_acc)
|
|
||||||
print(f"最大显存使用量: {torch.cuda.max_memory_allocated() / (1024 * 1024):.2f}MiB")
|
|
||||||
torch.cuda.reset_peak_memory_stats()
|
|
||||||
return train_loss, test_acc
|
|
||||||
|
|
||||||
|
|
||||||
def train_Haze_Removal(model:nn.Module, learning_rate=1e-3, batch_size=64, num_epochs=51):
|
|
||||||
num_epochs = 50
|
|
||||||
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
||||||
|
|
||||||
transform = transforms.Compose(
|
|
||||||
[
|
|
||||||
transforms.ToTensor(),
|
|
||||||
transforms.Resize((224, 224), antialias=True),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
train_dataset = Haze(root="../dataset", train=True, transform=transform)
|
|
||||||
test_dataset = Haze(root="../dataset", train=False, transform=transform)
|
|
||||||
|
|
||||||
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=14, pin_memory=True)
|
|
||||||
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, num_workers=14, pin_memory=True)
|
|
||||||
|
|
||||||
model = model.to(device)
|
|
||||||
criterion = nn.MSELoss()
|
|
||||||
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
|
|
||||||
|
|
||||||
train_loss = list()
|
|
||||||
test_loss = list()
|
|
||||||
for epoch in range(num_epochs):
|
|
||||||
model.train()
|
|
||||||
total_epoch_train_loss = 0
|
|
||||||
train_tik = time.time()
|
|
||||||
for index, (images, ground_truth) in tqdm(enumerate(train_loader), total=len(train_loader)):
|
|
||||||
optimizer.zero_grad()
|
|
||||||
|
|
||||||
images = images.to(device)
|
|
||||||
ground_truth = ground_truth.to(device)
|
|
||||||
|
|
||||||
outputs = model(images)
|
|
||||||
loss = criterion(outputs, ground_truth)
|
|
||||||
total_epoch_train_loss += loss.item()
|
|
||||||
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
train_tok = time.time()
|
|
||||||
|
|
||||||
model.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
total_epoch_test_loss = 0
|
|
||||||
test_tik = time.time()
|
|
||||||
for index, (image, ground_truth) in tqdm(enumerate(test_loader), total=len(test_loader)):
|
|
||||||
image = image.to(device)
|
|
||||||
ground_truth = ground_truth.to(device)
|
|
||||||
|
|
||||||
outputs = model(image)
|
|
||||||
loss = criterion(outputs, ground_truth)
|
|
||||||
total_epoch_test_loss += loss.item()
|
|
||||||
test_tok = time.time()
|
|
||||||
|
|
||||||
print(
|
|
||||||
f"Epoch [{epoch + 1}/{num_epochs}],",
|
|
||||||
f"Train Loss: {total_epoch_train_loss:.10f},",
|
|
||||||
f"Train Time: {1000 * (train_tok - train_tik):.2f}ms,",
|
|
||||||
f"Test Loss: {total_epoch_test_loss:.10f},",
|
|
||||||
f"Test Time: {1000 * (test_tok - test_tik):.2f}ms"
|
|
||||||
)
|
|
||||||
train_loss.append(total_epoch_train_loss)
|
|
||||||
test_loss.append(total_epoch_test_loss)
|
|
||||||
print(f"最大显存使用量: {torch.cuda.max_memory_allocated() / (1024 * 1024):.2f}MiB")
|
|
||||||
torch.cuda.reset_peak_memory_stats()
|
|
||||||
return train_loss, test_loss
|
|
||||||
|
|
||||||
|
|
@ -1,521 +0,0 @@
|
|||||||
Image,Train
|
|
||||||
001.jpg,1
|
|
||||||
002.jpg,1
|
|
||||||
003.jpg,1
|
|
||||||
004.jpg,0
|
|
||||||
005.jpg,0
|
|
||||||
006.jpg,1
|
|
||||||
007.jpg,1
|
|
||||||
008.jpg,1
|
|
||||||
009.jpg,1
|
|
||||||
010.jpg,1
|
|
||||||
011.jpg,0
|
|
||||||
012.jpg,1
|
|
||||||
013.jpg,0
|
|
||||||
014.jpg,1
|
|
||||||
015.jpg,1
|
|
||||||
016.jpg,1
|
|
||||||
017.jpg,1
|
|
||||||
018.jpg,1
|
|
||||||
019.jpg,1
|
|
||||||
020.jpg,1
|
|
||||||
021.jpg,1
|
|
||||||
022.jpg,1
|
|
||||||
023.jpg,1
|
|
||||||
024.jpg,1
|
|
||||||
025.jpg,1
|
|
||||||
026.jpg,1
|
|
||||||
027.jpg,0
|
|
||||||
028.jpg,1
|
|
||||||
029.jpg,1
|
|
||||||
030.jpg,1
|
|
||||||
031.jpg,1
|
|
||||||
032.jpg,1
|
|
||||||
033.jpg,1
|
|
||||||
034.jpg,1
|
|
||||||
035.jpg,1
|
|
||||||
036.jpg,1
|
|
||||||
037.jpg,0
|
|
||||||
038.jpg,1
|
|
||||||
039.jpg,1
|
|
||||||
040.jpg,1
|
|
||||||
041.jpg,1
|
|
||||||
042.jpg,1
|
|
||||||
043.jpg,0
|
|
||||||
044.jpg,1
|
|
||||||
045.jpg,1
|
|
||||||
046.jpg,1
|
|
||||||
047.jpg,1
|
|
||||||
048.jpg,0
|
|
||||||
049.jpg,1
|
|
||||||
050.jpg,1
|
|
||||||
051.jpg,1
|
|
||||||
052.jpg,1
|
|
||||||
053.jpg,1
|
|
||||||
054.jpg,1
|
|
||||||
055.jpg,1
|
|
||||||
056.jpg,1
|
|
||||||
057.jpg,1
|
|
||||||
058.jpg,1
|
|
||||||
059.jpg,0
|
|
||||||
060.jpg,1
|
|
||||||
061.jpg,1
|
|
||||||
062.jpg,1
|
|
||||||
063.jpg,1
|
|
||||||
064.jpg,1
|
|
||||||
065.jpg,0
|
|
||||||
066.jpg,1
|
|
||||||
067.jpg,0
|
|
||||||
068.jpg,1
|
|
||||||
069.jpg,0
|
|
||||||
070.jpg,1
|
|
||||||
071.jpg,1
|
|
||||||
072.jpg,1
|
|
||||||
073.jpg,1
|
|
||||||
074.jpg,1
|
|
||||||
075.jpg,1
|
|
||||||
076.jpg,0
|
|
||||||
077.jpg,1
|
|
||||||
078.jpg,1
|
|
||||||
079.jpg,1
|
|
||||||
080.jpg,1
|
|
||||||
081.jpg,1
|
|
||||||
082.jpg,1
|
|
||||||
083.jpg,1
|
|
||||||
084.jpg,1
|
|
||||||
085.jpg,0
|
|
||||||
086.jpg,1
|
|
||||||
087.jpg,1
|
|
||||||
088.jpg,0
|
|
||||||
089.jpg,1
|
|
||||||
090.jpg,1
|
|
||||||
091.jpg,1
|
|
||||||
092.jpg,1
|
|
||||||
093.jpg,1
|
|
||||||
094.jpg,1
|
|
||||||
095.jpg,1
|
|
||||||
096.jpg,1
|
|
||||||
097.jpg,1
|
|
||||||
098.jpg,1
|
|
||||||
099.jpg,1
|
|
||||||
100.jpg,1
|
|
||||||
101.jpg,1
|
|
||||||
102.jpg,1
|
|
||||||
103.jpg,1
|
|
||||||
104.jpg,1
|
|
||||||
105.jpg,1
|
|
||||||
106.jpg,1
|
|
||||||
107.jpg,0
|
|
||||||
108.jpg,1
|
|
||||||
109.jpg,0
|
|
||||||
110.jpg,1
|
|
||||||
111.jpg,1
|
|
||||||
112.jpg,1
|
|
||||||
113.jpg,1
|
|
||||||
114.jpg,1
|
|
||||||
115.jpg,1
|
|
||||||
116.jpg,1
|
|
||||||
117.jpg,1
|
|
||||||
118.jpg,0
|
|
||||||
119.jpg,1
|
|
||||||
120.jpg,1
|
|
||||||
121.jpg,0
|
|
||||||
122.jpg,1
|
|
||||||
123.jpg,1
|
|
||||||
124.jpg,1
|
|
||||||
125.jpg,1
|
|
||||||
126.jpg,0
|
|
||||||
127.jpg,1
|
|
||||||
128.jpg,0
|
|
||||||
129.jpg,0
|
|
||||||
130.jpg,0
|
|
||||||
131.jpg,1
|
|
||||||
132.jpg,1
|
|
||||||
133.jpg,1
|
|
||||||
134.jpg,1
|
|
||||||
135.jpg,1
|
|
||||||
136.jpg,1
|
|
||||||
137.jpg,1
|
|
||||||
138.jpg,1
|
|
||||||
139.jpg,1
|
|
||||||
140.jpg,0
|
|
||||||
141.jpg,0
|
|
||||||
142.jpg,1
|
|
||||||
143.jpg,1
|
|
||||||
144.jpg,1
|
|
||||||
145.jpg,1
|
|
||||||
146.jpg,1
|
|
||||||
147.jpg,0
|
|
||||||
148.jpg,1
|
|
||||||
149.jpg,1
|
|
||||||
150.jpg,0
|
|
||||||
151.jpg,1
|
|
||||||
152.jpg,1
|
|
||||||
153.jpg,1
|
|
||||||
154.jpg,1
|
|
||||||
155.jpg,0
|
|
||||||
156.jpg,1
|
|
||||||
157.jpg,0
|
|
||||||
158.jpg,1
|
|
||||||
159.jpg,0
|
|
||||||
160.jpg,1
|
|
||||||
161.jpg,0
|
|
||||||
162.jpg,1
|
|
||||||
163.jpg,0
|
|
||||||
164.jpg,1
|
|
||||||
165.jpg,0
|
|
||||||
166.jpg,1
|
|
||||||
167.jpg,1
|
|
||||||
168.jpg,0
|
|
||||||
169.jpg,1
|
|
||||||
170.jpg,1
|
|
||||||
171.jpg,0
|
|
||||||
172.jpg,1
|
|
||||||
173.jpg,1
|
|
||||||
174.jpg,1
|
|
||||||
175.jpg,0
|
|
||||||
176.jpg,1
|
|
||||||
177.jpg,1
|
|
||||||
178.jpg,1
|
|
||||||
179.jpg,1
|
|
||||||
180.jpg,0
|
|
||||||
181.jpg,1
|
|
||||||
182.jpg,0
|
|
||||||
183.jpg,1
|
|
||||||
184.jpg,1
|
|
||||||
185.jpg,1
|
|
||||||
186.jpg,0
|
|
||||||
187.jpg,1
|
|
||||||
188.jpg,1
|
|
||||||
189.jpg,1
|
|
||||||
190.jpg,0
|
|
||||||
191.jpg,1
|
|
||||||
192.jpg,1
|
|
||||||
193.jpg,1
|
|
||||||
194.jpg,1
|
|
||||||
195.jpg,1
|
|
||||||
196.jpg,1
|
|
||||||
197.jpg,1
|
|
||||||
198.jpg,1
|
|
||||||
199.jpg,1
|
|
||||||
200.jpg,1
|
|
||||||
201.jpg,1
|
|
||||||
202.jpg,1
|
|
||||||
203.jpg,1
|
|
||||||
204.jpg,1
|
|
||||||
205.jpg,0
|
|
||||||
206.jpg,1
|
|
||||||
207.jpg,1
|
|
||||||
208.jpg,1
|
|
||||||
209.jpg,1
|
|
||||||
210.jpg,1
|
|
||||||
211.jpg,1
|
|
||||||
212.jpg,1
|
|
||||||
213.jpg,1
|
|
||||||
214.jpg,0
|
|
||||||
215.jpg,0
|
|
||||||
216.jpg,1
|
|
||||||
217.jpg,1
|
|
||||||
218.jpg,1
|
|
||||||
219.jpg,1
|
|
||||||
220.jpg,1
|
|
||||||
221.jpg,1
|
|
||||||
222.jpg,1
|
|
||||||
223.jpg,0
|
|
||||||
224.jpg,0
|
|
||||||
225.jpg,1
|
|
||||||
226.jpg,1
|
|
||||||
227.jpg,1
|
|
||||||
228.jpg,1
|
|
||||||
229.jpg,0
|
|
||||||
230.jpg,1
|
|
||||||
231.jpg,1
|
|
||||||
232.jpg,1
|
|
||||||
233.jpg,0
|
|
||||||
234.jpg,1
|
|
||||||
235.jpg,0
|
|
||||||
236.jpg,1
|
|
||||||
237.jpg,1
|
|
||||||
238.jpg,1
|
|
||||||
239.jpg,1
|
|
||||||
240.jpg,1
|
|
||||||
241.jpg,1
|
|
||||||
242.jpg,1
|
|
||||||
243.jpg,1
|
|
||||||
244.jpg,1
|
|
||||||
245.jpg,1
|
|
||||||
246.jpg,0
|
|
||||||
247.jpg,1
|
|
||||||
248.jpg,0
|
|
||||||
249.jpg,0
|
|
||||||
250.jpg,1
|
|
||||||
251.jpg,1
|
|
||||||
252.jpg,1
|
|
||||||
253.jpg,1
|
|
||||||
254.jpg,1
|
|
||||||
255.jpg,1
|
|
||||||
256.jpg,0
|
|
||||||
257.jpg,1
|
|
||||||
258.jpg,1
|
|
||||||
259.jpg,1
|
|
||||||
260.jpg,0
|
|
||||||
261.jpg,1
|
|
||||||
262.jpg,0
|
|
||||||
263.jpg,1
|
|
||||||
264.jpg,1
|
|
||||||
265.jpg,0
|
|
||||||
266.jpg,0
|
|
||||||
267.jpg,0
|
|
||||||
268.jpg,1
|
|
||||||
269.jpg,1
|
|
||||||
270.jpg,1
|
|
||||||
271.jpg,1
|
|
||||||
272.jpg,1
|
|
||||||
273.jpg,1
|
|
||||||
274.jpg,1
|
|
||||||
275.jpg,1
|
|
||||||
276.jpg,1
|
|
||||||
277.jpg,1
|
|
||||||
278.jpg,1
|
|
||||||
279.jpg,1
|
|
||||||
280.jpg,0
|
|
||||||
281.jpg,1
|
|
||||||
282.jpg,1
|
|
||||||
283.jpg,1
|
|
||||||
284.jpg,1
|
|
||||||
285.jpg,1
|
|
||||||
286.jpg,0
|
|
||||||
287.jpg,1
|
|
||||||
288.jpg,1
|
|
||||||
289.jpg,1
|
|
||||||
290.jpg,1
|
|
||||||
291.jpg,1
|
|
||||||
292.jpg,1
|
|
||||||
293.jpg,0
|
|
||||||
294.jpg,1
|
|
||||||
295.jpg,1
|
|
||||||
296.jpg,0
|
|
||||||
297.jpg,1
|
|
||||||
298.jpg,1
|
|
||||||
299.jpg,0
|
|
||||||
300.jpg,1
|
|
||||||
301.jpg,1
|
|
||||||
302.jpg,0
|
|
||||||
303.jpg,1
|
|
||||||
304.jpg,0
|
|
||||||
305.jpg,1
|
|
||||||
306.jpg,1
|
|
||||||
307.jpg,1
|
|
||||||
308.jpg,1
|
|
||||||
309.jpg,1
|
|
||||||
310.jpg,1
|
|
||||||
311.jpg,0
|
|
||||||
312.jpg,1
|
|
||||||
313.jpg,1
|
|
||||||
314.jpg,0
|
|
||||||
315.jpg,1
|
|
||||||
316.jpg,1
|
|
||||||
317.jpg,1
|
|
||||||
318.jpg,1
|
|
||||||
319.jpg,1
|
|
||||||
320.jpg,1
|
|
||||||
321.jpg,0
|
|
||||||
322.jpg,1
|
|
||||||
323.jpg,1
|
|
||||||
324.jpg,0
|
|
||||||
325.jpg,1
|
|
||||||
326.jpg,1
|
|
||||||
327.jpg,0
|
|
||||||
328.jpg,1
|
|
||||||
329.jpg,1
|
|
||||||
330.jpg,1
|
|
||||||
331.jpg,1
|
|
||||||
332.jpg,0
|
|
||||||
333.jpg,1
|
|
||||||
334.jpg,0
|
|
||||||
335.jpg,1
|
|
||||||
336.jpg,1
|
|
||||||
337.jpg,1
|
|
||||||
338.jpg,1
|
|
||||||
339.jpg,1
|
|
||||||
340.jpg,1
|
|
||||||
341.jpg,1
|
|
||||||
342.jpg,1
|
|
||||||
343.jpg,1
|
|
||||||
344.jpg,1
|
|
||||||
345.jpg,0
|
|
||||||
346.jpg,1
|
|
||||||
347.jpg,1
|
|
||||||
348.jpg,1
|
|
||||||
349.jpg,1
|
|
||||||
350.jpg,1
|
|
||||||
351.jpg,1
|
|
||||||
352.jpg,1
|
|
||||||
353.jpg,1
|
|
||||||
354.jpg,0
|
|
||||||
355.jpg,1
|
|
||||||
356.jpg,0
|
|
||||||
357.jpg,1
|
|
||||||
358.jpg,0
|
|
||||||
359.jpg,0
|
|
||||||
360.jpg,1
|
|
||||||
361.jpg,0
|
|
||||||
362.jpg,1
|
|
||||||
363.jpg,0
|
|
||||||
364.jpg,1
|
|
||||||
365.jpg,1
|
|
||||||
366.jpg,1
|
|
||||||
367.jpg,1
|
|
||||||
368.jpg,1
|
|
||||||
369.jpg,0
|
|
||||||
370.jpg,0
|
|
||||||
371.jpg,1
|
|
||||||
372.jpg,0
|
|
||||||
373.jpg,1
|
|
||||||
374.jpg,1
|
|
||||||
375.jpg,1
|
|
||||||
376.jpg,1
|
|
||||||
377.jpg,0
|
|
||||||
378.jpg,0
|
|
||||||
379.jpg,1
|
|
||||||
380.jpg,1
|
|
||||||
381.jpg,1
|
|
||||||
382.jpg,1
|
|
||||||
383.jpg,1
|
|
||||||
384.jpg,1
|
|
||||||
385.jpg,1
|
|
||||||
386.jpg,1
|
|
||||||
387.jpg,1
|
|
||||||
388.jpg,1
|
|
||||||
389.jpg,1
|
|
||||||
390.jpg,0
|
|
||||||
391.jpg,0
|
|
||||||
392.jpg,1
|
|
||||||
393.jpg,1
|
|
||||||
394.jpg,1
|
|
||||||
395.jpg,1
|
|
||||||
396.jpg,1
|
|
||||||
397.jpg,1
|
|
||||||
398.jpg,0
|
|
||||||
399.jpg,1
|
|
||||||
400.jpg,1
|
|
||||||
401.jpg,0
|
|
||||||
402.jpg,1
|
|
||||||
403.jpg,1
|
|
||||||
404.jpg,1
|
|
||||||
405.jpg,0
|
|
||||||
406.jpg,0
|
|
||||||
407.jpg,1
|
|
||||||
408.jpg,1
|
|
||||||
409.jpg,1
|
|
||||||
410.jpg,1
|
|
||||||
411.jpg,0
|
|
||||||
412.jpg,1
|
|
||||||
413.jpg,1
|
|
||||||
414.jpg,1
|
|
||||||
415.jpg,1
|
|
||||||
416.jpg,1
|
|
||||||
417.jpg,1
|
|
||||||
418.jpg,1
|
|
||||||
419.jpg,1
|
|
||||||
420.jpg,1
|
|
||||||
421.jpg,0
|
|
||||||
422.jpg,0
|
|
||||||
423.jpg,1
|
|
||||||
424.jpg,1
|
|
||||||
425.jpg,0
|
|
||||||
426.jpg,1
|
|
||||||
427.jpg,1
|
|
||||||
428.jpg,1
|
|
||||||
429.jpg,0
|
|
||||||
430.jpg,0
|
|
||||||
431.jpg,1
|
|
||||||
432.jpg,1
|
|
||||||
433.jpg,1
|
|
||||||
434.jpg,1
|
|
||||||
435.jpg,1
|
|
||||||
436.jpg,1
|
|
||||||
437.jpg,1
|
|
||||||
438.jpg,1
|
|
||||||
439.jpg,1
|
|
||||||
440.jpg,1
|
|
||||||
441.jpg,1
|
|
||||||
442.jpg,1
|
|
||||||
443.jpg,1
|
|
||||||
444.jpg,1
|
|
||||||
445.jpg,1
|
|
||||||
446.jpg,1
|
|
||||||
447.jpg,1
|
|
||||||
448.jpg,1
|
|
||||||
449.jpg,1
|
|
||||||
450.jpg,1
|
|
||||||
451.jpg,1
|
|
||||||
452.jpg,1
|
|
||||||
453.jpg,1
|
|
||||||
454.jpg,1
|
|
||||||
455.jpg,1
|
|
||||||
456.jpg,1
|
|
||||||
457.jpg,1
|
|
||||||
458.jpg,1
|
|
||||||
459.jpg,1
|
|
||||||
460.jpg,1
|
|
||||||
461.jpg,1
|
|
||||||
462.jpg,1
|
|
||||||
463.jpg,0
|
|
||||||
464.jpg,1
|
|
||||||
465.jpg,1
|
|
||||||
466.jpg,1
|
|
||||||
467.jpg,1
|
|
||||||
468.jpg,1
|
|
||||||
469.jpg,1
|
|
||||||
470.jpg,1
|
|
||||||
471.jpg,1
|
|
||||||
472.jpg,1
|
|
||||||
473.jpg,1
|
|
||||||
474.jpg,1
|
|
||||||
475.jpg,1
|
|
||||||
476.jpg,1
|
|
||||||
477.jpg,1
|
|
||||||
478.jpg,1
|
|
||||||
479.jpg,1
|
|
||||||
480.jpg,1
|
|
||||||
481.jpg,1
|
|
||||||
482.jpg,1
|
|
||||||
483.jpg,1
|
|
||||||
484.jpg,0
|
|
||||||
485.jpg,1
|
|
||||||
486.jpg,1
|
|
||||||
487.jpg,1
|
|
||||||
488.jpg,1
|
|
||||||
489.jpg,1
|
|
||||||
490.jpg,1
|
|
||||||
491.jpg,1
|
|
||||||
492.jpg,0
|
|
||||||
493.jpg,1
|
|
||||||
494.jpg,1
|
|
||||||
495.jpg,1
|
|
||||||
496.jpg,1
|
|
||||||
497.jpg,1
|
|
||||||
498.jpg,1
|
|
||||||
499.jpg,1
|
|
||||||
500.jpg,1
|
|
||||||
501.jpg,1
|
|
||||||
502.jpg,1
|
|
||||||
503.jpg,1
|
|
||||||
504.jpg,1
|
|
||||||
505.jpg,1
|
|
||||||
506.jpg,0
|
|
||||||
507.jpg,0
|
|
||||||
508.jpg,0
|
|
||||||
509.jpg,1
|
|
||||||
510.jpg,1
|
|
||||||
511.jpg,1
|
|
||||||
512.jpg,0
|
|
||||||
513.jpg,0
|
|
||||||
514.jpg,1
|
|
||||||
515.jpg,1
|
|
||||||
516.jpg,1
|
|
||||||
517.jpg,1
|
|
||||||
518.jpg,1
|
|
||||||
519.jpg,1
|
|
||||||
520.jpg,0
|
|
|
@ -1,16 +0,0 @@
|
|||||||
import os
|
|
||||||
import pandas as pd
|
|
||||||
import random
|
|
||||||
|
|
||||||
train_list = set()
|
|
||||||
|
|
||||||
img_list = [i for i in os.listdir("raw/haze") if i.endswith(".jpg")]
|
|
||||||
random.shuffle(img_list)
|
|
||||||
for img in img_list[ : int(len(img_list) * 0.8)]:
|
|
||||||
train_list.add(img)
|
|
||||||
img_list.sort()
|
|
||||||
data = list()
|
|
||||||
for img in img_list:
|
|
||||||
data.append([img, 1 if img in train_list else 0])
|
|
||||||
|
|
||||||
pd.DataFrame(data=data, columns=["Image", "Train"]).to_csv("./split.csv", index=False)
|
|
@ -1,26 +0,0 @@
|
|||||||
import os
|
|
||||||
import random
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
train_list = list()
|
|
||||||
test_list = list()
|
|
||||||
|
|
||||||
root_dir = "raw"
|
|
||||||
class_index = 0
|
|
||||||
for vehicle in os.listdir(root_dir):
|
|
||||||
img_list = [i for i in os.listdir(os.path.join(root_dir, vehicle)) if i.endswith(".jpg")]
|
|
||||||
random.shuffle(img_list)
|
|
||||||
split_num = int(len(img_list) * 0.8)
|
|
||||||
for img in img_list[0 : split_num]:
|
|
||||||
train_list.append([os.path.join(root_dir, vehicle, img), class_index])
|
|
||||||
for img in img_list[split_num : ]:
|
|
||||||
test_list.append([os.path.join(root_dir, vehicle, img), class_index])
|
|
||||||
class_index += 1
|
|
||||||
|
|
||||||
train_list.sort()
|
|
||||||
test_list.sort()
|
|
||||||
|
|
||||||
pd.DataFrame(data=train_list, columns=["Vehicle", "Label"]).to_csv("./train.csv", index=False)
|
|
||||||
pd.DataFrame(data=test_list, columns=["Vehicle", "Label"]).to_csv("./test.csv", index=False)
|
|
||||||
|
|
||||||
|
|
@ -1,273 +0,0 @@
|
|||||||
Vehicle,Label
|
|
||||||
raw/bus/bus002.jpg,1
|
|
||||||
raw/bus/bus005.jpg,1
|
|
||||||
raw/bus/bus008.jpg,1
|
|
||||||
raw/bus/bus017.jpg,1
|
|
||||||
raw/bus/bus019.jpg,1
|
|
||||||
raw/bus/bus033.jpg,1
|
|
||||||
raw/bus/bus034.jpg,1
|
|
||||||
raw/bus/bus040.jpg,1
|
|
||||||
raw/bus/bus043.jpg,1
|
|
||||||
raw/bus/bus044.jpg,1
|
|
||||||
raw/bus/bus045.jpg,1
|
|
||||||
raw/bus/bus046.jpg,1
|
|
||||||
raw/bus/bus051.jpg,1
|
|
||||||
raw/bus/bus052.jpg,1
|
|
||||||
raw/bus/bus056.jpg,1
|
|
||||||
raw/bus/bus057.jpg,1
|
|
||||||
raw/bus/bus063.jpg,1
|
|
||||||
raw/bus/bus066.jpg,1
|
|
||||||
raw/bus/bus077.jpg,1
|
|
||||||
raw/bus/bus080.jpg,1
|
|
||||||
raw/bus/bus096.jpg,1
|
|
||||||
raw/bus/bus097.jpg,1
|
|
||||||
raw/bus/bus099.jpg,1
|
|
||||||
raw/bus/bus114.jpg,1
|
|
||||||
raw/bus/bus115.jpg,1
|
|
||||||
raw/bus/bus119.jpg,1
|
|
||||||
raw/bus/bus127.jpg,1
|
|
||||||
raw/bus/bus132.jpg,1
|
|
||||||
raw/bus/bus133.jpg,1
|
|
||||||
raw/bus/bus135.jpg,1
|
|
||||||
raw/bus/bus139.jpg,1
|
|
||||||
raw/bus/bus141.jpg,1
|
|
||||||
raw/bus/bus145.jpg,1
|
|
||||||
raw/bus/bus156.jpg,1
|
|
||||||
raw/bus/bus170.jpg,1
|
|
||||||
raw/bus/bus173.jpg,1
|
|
||||||
raw/bus/bus183.jpg,1
|
|
||||||
raw/bus/bus190.jpg,1
|
|
||||||
raw/bus/bus192.jpg,1
|
|
||||||
raw/bus/bus200.jpg,1
|
|
||||||
raw/bus/bus201.jpg,1
|
|
||||||
raw/bus/bus209.jpg,1
|
|
||||||
raw/bus/bus213.jpg,1
|
|
||||||
raw/bus/bus218.jpg,1
|
|
||||||
raw/car/car002.jpg,0
|
|
||||||
raw/car/car012.jpg,0
|
|
||||||
raw/car/car018.jpg,0
|
|
||||||
raw/car/car025.jpg,0
|
|
||||||
raw/car/car030.jpg,0
|
|
||||||
raw/car/car034.jpg,0
|
|
||||||
raw/car/car036.jpg,0
|
|
||||||
raw/car/car047.jpg,0
|
|
||||||
raw/car/car053.jpg,0
|
|
||||||
raw/car/car054.jpg,0
|
|
||||||
raw/car/car058.jpg,0
|
|
||||||
raw/car/car060.jpg,0
|
|
||||||
raw/car/car062.jpg,0
|
|
||||||
raw/car/car063.jpg,0
|
|
||||||
raw/car/car066.jpg,0
|
|
||||||
raw/car/car072.jpg,0
|
|
||||||
raw/car/car084.jpg,0
|
|
||||||
raw/car/car087.jpg,0
|
|
||||||
raw/car/car089.jpg,0
|
|
||||||
raw/car/car099.jpg,0
|
|
||||||
raw/car/car103.jpg,0
|
|
||||||
raw/car/car105.jpg,0
|
|
||||||
raw/car/car106.jpg,0
|
|
||||||
raw/car/car116.jpg,0
|
|
||||||
raw/car/car117.jpg,0
|
|
||||||
raw/car/car120.jpg,0
|
|
||||||
raw/car/car132.jpg,0
|
|
||||||
raw/car/car138.jpg,0
|
|
||||||
raw/car/car139.jpg,0
|
|
||||||
raw/car/car152.jpg,0
|
|
||||||
raw/car/car153.jpg,0
|
|
||||||
raw/car/car157.jpg,0
|
|
||||||
raw/car/car168.jpg,0
|
|
||||||
raw/car/car177.jpg,0
|
|
||||||
raw/car/car182.jpg,0
|
|
||||||
raw/car/car183.jpg,0
|
|
||||||
raw/car/car184.jpg,0
|
|
||||||
raw/car/car187.jpg,0
|
|
||||||
raw/car/car192.jpg,0
|
|
||||||
raw/car/car209.jpg,0
|
|
||||||
raw/car/car212.jpg,0
|
|
||||||
raw/car/car215.jpg,0
|
|
||||||
raw/car/car222.jpg,0
|
|
||||||
raw/car/car225.jpg,0
|
|
||||||
raw/car/car227.jpg,0
|
|
||||||
raw/car/car237.jpg,0
|
|
||||||
raw/car/car238.jpg,0
|
|
||||||
raw/car/car249.jpg,0
|
|
||||||
raw/car/car250.jpg,0
|
|
||||||
raw/car/car257.jpg,0
|
|
||||||
raw/car/car263.jpg,0
|
|
||||||
raw/car/car275.jpg,0
|
|
||||||
raw/car/car279.jpg,0
|
|
||||||
raw/car/car283.jpg,0
|
|
||||||
raw/car/car288.jpg,0
|
|
||||||
raw/car/car299.jpg,0
|
|
||||||
raw/car/car300.jpg,0
|
|
||||||
raw/car/car317.jpg,0
|
|
||||||
raw/car/car318.jpg,0
|
|
||||||
raw/car/car336.jpg,0
|
|
||||||
raw/car/car345.jpg,0
|
|
||||||
raw/car/car351.jpg,0
|
|
||||||
raw/car/car357.jpg,0
|
|
||||||
raw/car/car358.jpg,0
|
|
||||||
raw/car/car360.jpg,0
|
|
||||||
raw/car/car369.jpg,0
|
|
||||||
raw/car/car374.jpg,0
|
|
||||||
raw/car/car376.jpg,0
|
|
||||||
raw/car/car377.jpg,0
|
|
||||||
raw/car/car379.jpg,0
|
|
||||||
raw/car/car380.jpg,0
|
|
||||||
raw/car/car381.jpg,0
|
|
||||||
raw/car/car386.jpg,0
|
|
||||||
raw/car/car396.jpg,0
|
|
||||||
raw/car/car402.jpg,0
|
|
||||||
raw/car/car404.jpg,0
|
|
||||||
raw/car/car410.jpg,0
|
|
||||||
raw/car/car424.jpg,0
|
|
||||||
raw/car/car437.jpg,0
|
|
||||||
raw/car/car439.jpg,0
|
|
||||||
raw/car/car441.jpg,0
|
|
||||||
raw/car/car454.jpg,0
|
|
||||||
raw/car/car457.jpg,0
|
|
||||||
raw/car/car458.jpg,0
|
|
||||||
raw/car/car460.jpg,0
|
|
||||||
raw/car/car461.jpg,0
|
|
||||||
raw/car/car470.jpg,0
|
|
||||||
raw/car/car475.jpg,0
|
|
||||||
raw/car/car477.jpg,0
|
|
||||||
raw/car/car486.jpg,0
|
|
||||||
raw/car/car490.jpg,0
|
|
||||||
raw/car/car495.jpg,0
|
|
||||||
raw/car/car500.jpg,0
|
|
||||||
raw/car/car502.jpg,0
|
|
||||||
raw/car/car511.jpg,0
|
|
||||||
raw/car/car525.jpg,0
|
|
||||||
raw/car/car534.jpg,0
|
|
||||||
raw/car/car537.jpg,0
|
|
||||||
raw/car/car544.jpg,0
|
|
||||||
raw/car/car546.jpg,0
|
|
||||||
raw/car/car547.jpg,0
|
|
||||||
raw/car/car551.jpg,0
|
|
||||||
raw/car/car559.jpg,0
|
|
||||||
raw/car/car566.jpg,0
|
|
||||||
raw/car/car568.jpg,0
|
|
||||||
raw/car/car570.jpg,0
|
|
||||||
raw/car/car571.jpg,0
|
|
||||||
raw/car/car574.jpg,0
|
|
||||||
raw/car/car577.jpg,0
|
|
||||||
raw/car/car580.jpg,0
|
|
||||||
raw/car/car583.jpg,0
|
|
||||||
raw/car/car585.jpg,0
|
|
||||||
raw/car/car588.jpg,0
|
|
||||||
raw/car/car600.jpg,0
|
|
||||||
raw/car/car604.jpg,0
|
|
||||||
raw/car/car606.jpg,0
|
|
||||||
raw/car/car611.jpg,0
|
|
||||||
raw/car/car613.jpg,0
|
|
||||||
raw/car/car625.jpg,0
|
|
||||||
raw/car/car626.jpg,0
|
|
||||||
raw/car/car638.jpg,0
|
|
||||||
raw/car/car646.jpg,0
|
|
||||||
raw/car/car647.jpg,0
|
|
||||||
raw/car/car649.jpg,0
|
|
||||||
raw/car/car653.jpg,0
|
|
||||||
raw/car/car661.jpg,0
|
|
||||||
raw/car/car664.jpg,0
|
|
||||||
raw/car/car667.jpg,0
|
|
||||||
raw/car/car669.jpg,0
|
|
||||||
raw/car/car670.jpg,0
|
|
||||||
raw/car/car691.jpg,0
|
|
||||||
raw/car/car698.jpg,0
|
|
||||||
raw/car/car701.jpg,0
|
|
||||||
raw/car/car707.jpg,0
|
|
||||||
raw/car/car715.jpg,0
|
|
||||||
raw/car/car716.jpg,0
|
|
||||||
raw/car/car718.jpg,0
|
|
||||||
raw/car/car720.jpg,0
|
|
||||||
raw/car/car728.jpg,0
|
|
||||||
raw/car/car730.jpg,0
|
|
||||||
raw/car/car733.jpg,0
|
|
||||||
raw/car/car741.jpg,0
|
|
||||||
raw/car/car743.jpg,0
|
|
||||||
raw/car/car753.jpg,0
|
|
||||||
raw/car/car755.jpg,0
|
|
||||||
raw/car/car756.jpg,0
|
|
||||||
raw/car/car757.jpg,0
|
|
||||||
raw/car/car760.jpg,0
|
|
||||||
raw/car/car761.jpg,0
|
|
||||||
raw/car/car763.jpg,0
|
|
||||||
raw/car/car764.jpg,0
|
|
||||||
raw/car/car768.jpg,0
|
|
||||||
raw/car/car769.jpg,0
|
|
||||||
raw/car/car770.jpg,0
|
|
||||||
raw/car/car772.jpg,0
|
|
||||||
raw/car/car774.jpg,0
|
|
||||||
raw/truck/truck001.jpg,2
|
|
||||||
raw/truck/truck006.jpg,2
|
|
||||||
raw/truck/truck009.jpg,2
|
|
||||||
raw/truck/truck011.jpg,2
|
|
||||||
raw/truck/truck018.jpg,2
|
|
||||||
raw/truck/truck020.jpg,2
|
|
||||||
raw/truck/truck021.jpg,2
|
|
||||||
raw/truck/truck026.jpg,2
|
|
||||||
raw/truck/truck031.jpg,2
|
|
||||||
raw/truck/truck039.jpg,2
|
|
||||||
raw/truck/truck043.jpg,2
|
|
||||||
raw/truck/truck045.jpg,2
|
|
||||||
raw/truck/truck051.jpg,2
|
|
||||||
raw/truck/truck067.jpg,2
|
|
||||||
raw/truck/truck075.jpg,2
|
|
||||||
raw/truck/truck080.jpg,2
|
|
||||||
raw/truck/truck084.jpg,2
|
|
||||||
raw/truck/truck088.jpg,2
|
|
||||||
raw/truck/truck091.jpg,2
|
|
||||||
raw/truck/truck093.jpg,2
|
|
||||||
raw/truck/truck095.jpg,2
|
|
||||||
raw/truck/truck097.jpg,2
|
|
||||||
raw/truck/truck098.jpg,2
|
|
||||||
raw/truck/truck103.jpg,2
|
|
||||||
raw/truck/truck107.jpg,2
|
|
||||||
raw/truck/truck113.jpg,2
|
|
||||||
raw/truck/truck116.jpg,2
|
|
||||||
raw/truck/truck117.jpg,2
|
|
||||||
raw/truck/truck118.jpg,2
|
|
||||||
raw/truck/truck124.jpg,2
|
|
||||||
raw/truck/truck130.jpg,2
|
|
||||||
raw/truck/truck136.jpg,2
|
|
||||||
raw/truck/truck141.jpg,2
|
|
||||||
raw/truck/truck146.jpg,2
|
|
||||||
raw/truck/truck152.jpg,2
|
|
||||||
raw/truck/truck153.jpg,2
|
|
||||||
raw/truck/truck155.jpg,2
|
|
||||||
raw/truck/truck157.jpg,2
|
|
||||||
raw/truck/truck164.jpg,2
|
|
||||||
raw/truck/truck170.jpg,2
|
|
||||||
raw/truck/truck171.jpg,2
|
|
||||||
raw/truck/truck176.jpg,2
|
|
||||||
raw/truck/truck185.jpg,2
|
|
||||||
raw/truck/truck192.jpg,2
|
|
||||||
raw/truck/truck193.jpg,2
|
|
||||||
raw/truck/truck197.jpg,2
|
|
||||||
raw/truck/truck207.jpg,2
|
|
||||||
raw/truck/truck211.jpg,2
|
|
||||||
raw/truck/truck216.jpg,2
|
|
||||||
raw/truck/truck217.jpg,2
|
|
||||||
raw/truck/truck218.jpg,2
|
|
||||||
raw/truck/truck220.jpg,2
|
|
||||||
raw/truck/truck221.jpg,2
|
|
||||||
raw/truck/truck223.jpg,2
|
|
||||||
raw/truck/truck226.jpg,2
|
|
||||||
raw/truck/truck235.jpg,2
|
|
||||||
raw/truck/truck252.jpg,2
|
|
||||||
raw/truck/truck259.jpg,2
|
|
||||||
raw/truck/truck261.jpg,2
|
|
||||||
raw/truck/truck264.jpg,2
|
|
||||||
raw/truck/truck271.jpg,2
|
|
||||||
raw/truck/truck306.jpg,2
|
|
||||||
raw/truck/truck309.jpg,2
|
|
||||||
raw/truck/truck311.jpg,2
|
|
||||||
raw/truck/truck317.jpg,2
|
|
||||||
raw/truck/truck326.jpg,2
|
|
||||||
raw/truck/truck328.jpg,2
|
|
||||||
raw/truck/truck329.jpg,2
|
|
||||||
raw/truck/truck333.jpg,2
|
|
||||||
raw/truck/truck335.jpg,2
|
|
||||||
raw/truck/truck345.jpg,2
|
|
||||||
raw/truck/truck360.jpg,2
|
|
|
File diff suppressed because it is too large
Load Diff
1942
Lab4/卷积神经网络实验.ipynb
1942
Lab4/卷积神经网络实验.ipynb
File diff suppressed because one or more lines are too long
480
Lab5/1-RNN.ipynb
480
Lab5/1-RNN.ipynb
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
472
Lab5/3-GRU.ipynb
472
Lab5/3-GRU.ipynb
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
141
Lab5/dataset.py
141
Lab5/dataset.py
@ -1,141 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
import pandas as pd
|
|
||||||
import torch
|
|
||||||
import torch.utils.data as data
|
|
||||||
import warnings
|
|
||||||
warnings.filterwarnings("ignore")
|
|
||||||
|
|
||||||
|
|
||||||
# 定义dataset
|
|
||||||
class my_Dataset(data.Dataset):
|
|
||||||
def __init__(self, features, labels):
|
|
||||||
self.seqs = features
|
|
||||||
self.targets = labels
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
return self.seqs[index], self.targets[index]
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return self.seqs.shape[0]
|
|
||||||
|
|
||||||
|
|
||||||
# 空气质量数据集
|
|
||||||
class KrakowDataset:
|
|
||||||
def __init__(self, sensor:int=171, is_resample:bool=True):
|
|
||||||
# 选取几个月个月的数据
|
|
||||||
self.month = ['april-2017', 'august-2017', 'december-2017', 'february-2017',
|
|
||||||
'january-2017', 'july-2017', 'june-2017', 'march-2017',
|
|
||||||
'may-2017', 'november-2017', 'october-2017', 'september-2017']
|
|
||||||
raw_data = pd.concat([pd.read_csv(f'./dataset/Krakow-airquality/raw/{month}.csv') for month in self.month])
|
|
||||||
|
|
||||||
# 确定特征列
|
|
||||||
features = ['temperature', 'humidity', 'pressure', 'pm1', 'pm25', 'pm10']
|
|
||||||
self.sensor = sensor # 选取探测器,并非每个探测器都有数据
|
|
||||||
self.feature_col = ['UTC time'] + [f'{self.sensor}_{fea}' for fea in features]
|
|
||||||
data_df = raw_data[[col for col in raw_data.columns if col in self.feature_col]]
|
|
||||||
|
|
||||||
# 按时间戳排序
|
|
||||||
data_df['UTC time'] = pd.to_datetime(data_df['UTC time'])
|
|
||||||
data_df = data_df.set_index('UTC time').sort_index()
|
|
||||||
|
|
||||||
# 重采样、插分
|
|
||||||
if is_resample:
|
|
||||||
self.start_time, self.end_time = data_df.index.min(), data_df.index.max()
|
|
||||||
full_index = pd.date_range(self.start_time, self.end_time, freq='h')
|
|
||||||
data_df = data_df.reindex(full_index)
|
|
||||||
data_df = data_df.interpolate(method='linear')
|
|
||||||
else:
|
|
||||||
data_df = data_df.dropna()
|
|
||||||
|
|
||||||
# 数据标准化
|
|
||||||
self.min = data_df.min()
|
|
||||||
self.max = data_df.max()
|
|
||||||
self.data = (data_df - self.min) / (self.max - self.min)
|
|
||||||
|
|
||||||
def denormalize(self, x):
|
|
||||||
key = f'{self.sensor}_{self.target}'
|
|
||||||
return x * (self.max[key] - self.min[key]) + self.min[key]
|
|
||||||
|
|
||||||
def construct_set(self, train_por=0.6, test_por=0.2, window_size=12, target='pm25'):
|
|
||||||
train_x = []
|
|
||||||
train_y = []
|
|
||||||
val_x = []
|
|
||||||
val_y = []
|
|
||||||
test_x = []
|
|
||||||
test_y = []
|
|
||||||
self.target = target
|
|
||||||
self.feature_col.remove('UTC time')
|
|
||||||
self.data = self.data.reset_index()
|
|
||||||
|
|
||||||
len_train = int(self.data.shape[0] * train_por)
|
|
||||||
train_seqs = self.data[:len_train]
|
|
||||||
for i in range(train_seqs.shape[0] - window_size):
|
|
||||||
train_seq = train_seqs.loc[i:i + window_size]
|
|
||||||
train_x.append(train_seq.loc[i:i + window_size - 1][self.feature_col].values.tolist())
|
|
||||||
train_y.append(train_seq.loc[i + window_size][f'{self.sensor}_{target}'].tolist())
|
|
||||||
|
|
||||||
len_val = int(self.data.shape[0] * (train_por + test_por))
|
|
||||||
val_seqs = self.data[len_train:len_val]
|
|
||||||
val_seqs = val_seqs.reset_index()
|
|
||||||
for i in range(val_seqs.shape[0] - window_size):
|
|
||||||
val_seq = val_seqs.loc[i:i + window_size]
|
|
||||||
val_x.append(val_seq.loc[i:i + window_size - 1][self.feature_col].values.tolist())
|
|
||||||
val_y.append(val_seq.loc[i + window_size][f'{self.sensor}_{target}'].tolist())
|
|
||||||
|
|
||||||
test_seqs = self.data[len_val:]
|
|
||||||
test_seqs = test_seqs.reset_index()
|
|
||||||
for i in range(test_seqs.shape[0] - window_size):
|
|
||||||
test_seq = test_seqs.loc[i:i + window_size]
|
|
||||||
test_x.append(test_seq.loc[i:i + window_size - 1][self.feature_col].values.tolist())
|
|
||||||
test_y.append(test_seq.loc[i + window_size][f'{self.sensor}_{target}'].tolist())
|
|
||||||
|
|
||||||
train_set = my_Dataset(torch.Tensor(train_x), torch.Tensor(train_y))
|
|
||||||
val_set = my_Dataset(torch.Tensor(val_x), torch.Tensor(val_y))
|
|
||||||
test_set = my_Dataset(torch.Tensor(test_x), torch.Tensor(test_y))
|
|
||||||
return train_set, val_set, test_set
|
|
||||||
|
|
||||||
|
|
||||||
class TrafficDataset:
|
|
||||||
def __init__(self, sensor=10, target=0):
|
|
||||||
# 选取适当的检测器用作序列数据
|
|
||||||
self.raw_data = np.load('./dataset/traffic-flow/raw/traffic.npz')['data']
|
|
||||||
self.sensor = sensor
|
|
||||||
self.target = target
|
|
||||||
# 数据标准化
|
|
||||||
self.min = self.raw_data.min()
|
|
||||||
self.max = self.raw_data.max()
|
|
||||||
self.data = (self.raw_data - self.min) / (self.max - self.min)
|
|
||||||
|
|
||||||
def denormalize(self, x):
|
|
||||||
return x * (self.max - self.min) + self.min
|
|
||||||
|
|
||||||
def construct_set(self, train_por=0.6, test_por=0.2, window_size=12, label=0):
|
|
||||||
train_x = []
|
|
||||||
train_y = []
|
|
||||||
val_x = []
|
|
||||||
val_y = []
|
|
||||||
test_x = []
|
|
||||||
test_y = []
|
|
||||||
|
|
||||||
len_train = int(self.data.shape[0] * train_por)
|
|
||||||
train_seqs = self.data[0:len_train, self.sensor, :]
|
|
||||||
for i in range(len_train - window_size):
|
|
||||||
train_x.append(train_seqs[i:i + window_size - 1])
|
|
||||||
train_y.append(train_seqs[i + window_size][label])
|
|
||||||
|
|
||||||
len_val = int(self.data.shape[0] * test_por)
|
|
||||||
val_seqs = self.data[len_train:len_train + len_val, self.sensor, :]
|
|
||||||
for i in range(len_val - window_size):
|
|
||||||
val_x.append(val_seqs[i:i + window_size - 1])
|
|
||||||
val_y.append(val_seqs[i + window_size][label])
|
|
||||||
|
|
||||||
len_test = int(self.data.shape[0] * (1 - train_por - test_por))
|
|
||||||
test_seqs = self.data[len_train + len_val:, self.sensor, :]
|
|
||||||
for i in range(len_test - window_size):
|
|
||||||
test_x.append(test_seqs[i:i + window_size - 1])
|
|
||||||
test_y.append(test_seqs[i + window_size][label])
|
|
||||||
|
|
||||||
train_set = my_Dataset(torch.Tensor(train_x), torch.Tensor(train_y))
|
|
||||||
val_set = my_Dataset(torch.Tensor(val_x), torch.Tensor(val_y))
|
|
||||||
test_set = my_Dataset(torch.Tensor(test_x), torch.Tensor(test_y))
|
|
||||||
return train_set, val_set, test_set
|
|
176
Lab5/utils.py
176
Lab5/utils.py
@ -1,176 +0,0 @@
|
|||||||
import math
|
|
||||||
import torch
|
|
||||||
from torch.utils import data
|
|
||||||
import torch.nn as nn
|
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
import numpy as np
|
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
def mse_fn(y, pred):
|
|
||||||
return np.mean((np.array(y) - np.array(pred)) ** 2)
|
|
||||||
|
|
||||||
|
|
||||||
def mae_fn(y, pred):
|
|
||||||
return np.mean(np.abs(np.array(y) - np.array(pred)))
|
|
||||||
|
|
||||||
|
|
||||||
def mape_fn(y, pred):
|
|
||||||
mask = y != 0
|
|
||||||
y = y[mask]
|
|
||||||
pred = pred[mask]
|
|
||||||
mape = np.abs((y - pred) / y)
|
|
||||||
mape = np.mean(mape) * 100
|
|
||||||
return mape
|
|
||||||
|
|
||||||
|
|
||||||
def eval(y, pred):
|
|
||||||
y = y.cpu().numpy()
|
|
||||||
pred = pred.cpu().numpy()
|
|
||||||
mse = mse_fn(y, pred)
|
|
||||||
rmse = math.sqrt(mse)
|
|
||||||
mae = mae_fn(y, pred)
|
|
||||||
mape = mape_fn(y, pred)
|
|
||||||
return [rmse, mae, mape]
|
|
||||||
|
|
||||||
|
|
||||||
# 测试函数(用于分类)
|
|
||||||
def test(net, data_iter, loss_fn, denormalize_fn, device='cpu'):
|
|
||||||
rmse, mae, mape = 0, 0, 0
|
|
||||||
batch_count = 0
|
|
||||||
total_loss = 0.0
|
|
||||||
net.eval()
|
|
||||||
for seqs, targets in data_iter:
|
|
||||||
seqs = seqs.to(device).float()
|
|
||||||
targets = targets.to(device).float()
|
|
||||||
y_hat = net(seqs)
|
|
||||||
loss = loss_fn(y_hat, targets)
|
|
||||||
|
|
||||||
targets = denormalize_fn(targets)
|
|
||||||
y_hat = denormalize_fn(y_hat)
|
|
||||||
a, b, c = eval(targets.detach(), y_hat.detach())
|
|
||||||
rmse += a
|
|
||||||
mae += b
|
|
||||||
mape += c
|
|
||||||
total_loss += loss.detach().cpu().numpy().tolist()
|
|
||||||
batch_count += 1
|
|
||||||
return [rmse / batch_count, mae / batch_count, mape / batch_count], total_loss / batch_count
|
|
||||||
|
|
||||||
|
|
||||||
def train(net, train_iter, val_iter, test_iter, loss_fn, denormalize_fn, optimizer, num_epoch,
|
|
||||||
early_stop=10, device='cpu', num_print_epoch_round=0):
|
|
||||||
train_loss_lst = []
|
|
||||||
val_loss_lst = []
|
|
||||||
train_score_lst = []
|
|
||||||
val_score_lst = []
|
|
||||||
epoch_time = []
|
|
||||||
|
|
||||||
best_epoch = 0
|
|
||||||
best_val_rmse = 9999
|
|
||||||
early_stop_flag = 0
|
|
||||||
for epoch in range(num_epoch):
|
|
||||||
net.train()
|
|
||||||
epoch_loss = 0
|
|
||||||
batch_count = 0
|
|
||||||
batch_time = []
|
|
||||||
rmse, mae, mape = 0, 0, 0
|
|
||||||
for seqs, targets in train_iter:
|
|
||||||
batch_s = time.time()
|
|
||||||
seqs = seqs.to(device).float()
|
|
||||||
targets = targets.to(device).float()
|
|
||||||
optimizer.zero_grad()
|
|
||||||
y_hat = net(seqs)
|
|
||||||
loss = loss_fn(y_hat, targets)
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
|
|
||||||
targets = denormalize_fn(targets)
|
|
||||||
y_hat = denormalize_fn(y_hat)
|
|
||||||
a, b, c = eval(targets.detach(), y_hat.detach())
|
|
||||||
rmse += a
|
|
||||||
mae += b
|
|
||||||
mape += c
|
|
||||||
epoch_loss += loss.detach().cpu().numpy().tolist()
|
|
||||||
batch_count += 1
|
|
||||||
|
|
||||||
batch_time.append(time.time() - batch_s)
|
|
||||||
|
|
||||||
train_loss = epoch_loss / batch_count
|
|
||||||
train_loss_lst.append(train_loss)
|
|
||||||
train_score_lst.append([rmse/batch_count, mae/batch_count, mape/batch_count])
|
|
||||||
|
|
||||||
# 验证集
|
|
||||||
val_score, val_loss = test(net, val_iter, loss_fn, denormalize_fn, device)
|
|
||||||
val_score_lst.append(val_score)
|
|
||||||
val_loss_lst.append(val_loss)
|
|
||||||
|
|
||||||
epoch_time.append(np.array(batch_time).sum())
|
|
||||||
|
|
||||||
# 打印本轮训练结果
|
|
||||||
if num_print_epoch_round > 0 and (epoch+1) % num_print_epoch_round == 0:
|
|
||||||
print(
|
|
||||||
f"Epoch [{epoch + 1}/{num_epoch}],",
|
|
||||||
f"Train Loss: {train_loss:.4f},",
|
|
||||||
f"Train RMSE: {train_score_lst[-1][0]:.4f},",
|
|
||||||
f"Val Loss: {val_loss:.4f},",
|
|
||||||
f"Val RMSE: {val_score[0]:.6f},",
|
|
||||||
f"Time Use: {epoch_time[-1]:.3f}s"
|
|
||||||
)
|
|
||||||
|
|
||||||
# 早停
|
|
||||||
if val_score[0] < best_val_rmse:
|
|
||||||
best_val_rmse = val_score[0]
|
|
||||||
best_epoch = epoch
|
|
||||||
early_stop_flag = 0
|
|
||||||
else:
|
|
||||||
early_stop_flag += 1
|
|
||||||
if early_stop_flag == early_stop:
|
|
||||||
print(f'The model has not been improved for {early_stop} rounds. Stop early!')
|
|
||||||
break
|
|
||||||
|
|
||||||
# 输出最终训练结果
|
|
||||||
print(
|
|
||||||
f'Final result:',
|
|
||||||
f'Get best validation rmse {np.array(val_score_lst)[:, 0].min():.4f} at epoch {best_epoch},',
|
|
||||||
f'Total time {np.array(epoch_time).sum():.2f}s'
|
|
||||||
)
|
|
||||||
|
|
||||||
# 计算测试集效果
|
|
||||||
test_score, test_loss = test(net, test_iter, loss_fn, denormalize_fn, device)
|
|
||||||
print(
|
|
||||||
'Test result:',
|
|
||||||
f'Test RMSE: {test_score[0]},',
|
|
||||||
f'Test MAE: {test_score[1]},',
|
|
||||||
f'Test MAPE: {test_score[2]}'
|
|
||||||
)
|
|
||||||
return train_loss_lst, val_loss_lst, train_score_lst, val_score_lst, epoch
|
|
||||||
|
|
||||||
|
|
||||||
def visualize(num_epochs, train_data, test_data, x_label='epoch', y_label='loss'):
|
|
||||||
x = np.arange(0, num_epochs + 1).astype(dtype=np.int32)
|
|
||||||
plt.figure(figsize=(5, 3.5))
|
|
||||||
plt.plot(x, train_data, label=f"train_{y_label}", linewidth=1.5)
|
|
||||||
plt.plot(x, test_data, label=f"val_{y_label}", linewidth=1.5)
|
|
||||||
plt.xlabel(x_label)
|
|
||||||
plt.ylabel(y_label)
|
|
||||||
plt.legend()
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
|
|
||||||
def plot_metric(score_log):
|
|
||||||
score_log = np.array(score_log)
|
|
||||||
|
|
||||||
plt.figure(figsize=(13, 3.5))
|
|
||||||
plt.subplot(1, 3, 1)
|
|
||||||
plt.plot(score_log[:, 0], c='#d28ad4')
|
|
||||||
plt.ylabel('RMSE')
|
|
||||||
|
|
||||||
plt.subplot(1, 3, 2)
|
|
||||||
plt.plot(score_log[:, 1], c='#e765eb')
|
|
||||||
plt.ylabel('MAE')
|
|
||||||
|
|
||||||
plt.subplot(1, 3, 3)
|
|
||||||
plt.plot(score_log[:, 2], c='#6b016d')
|
|
||||||
plt.ylabel('MAPE')
|
|
||||||
|
|
||||||
plt.show()
|
|
41
README.md
41
README.md
@ -1,41 +0,0 @@
|
|||||||
# 深度学习课程实验报告-北京交通大学计算机学院
|
|
||||||
|
|
||||||
> 北京交通大学计算机学院开设的2023-2024学年秋季学期深度学习(本科)课程 课程实验报告
|
|
||||||
>
|
|
||||||
> 作者 柯劲帆
|
|
||||||
|
|
||||||
本实验报告绝大部分由个人完成,小部分参考课程材料和ChatGPT。
|
|
||||||
|
|
||||||
结课后整理,供后续选课同学参考。
|
|
||||||
|
|
||||||
## Getting Started 使用指南
|
|
||||||
|
|
||||||
### Installation 安装
|
|
||||||
|
|
||||||
Windows & Linux:
|
|
||||||
|
|
||||||
在命令行运行以下命令,获取项目源码:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
git clone https://github.com/typingbugs/School-DeepLearningCourse-Lab.git
|
|
||||||
```
|
|
||||||
|
|
||||||
pip安装python依赖包:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
pip install -r requirements.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
获取数据集(实验四和实验五需要):
|
|
||||||
|
|
||||||
链接:https://pan.baidu.com/s/13pELZTtqVu11Q1GhSFZUdg?pwd=8024
|
|
||||||
提取码:8024
|
|
||||||
|
|
||||||
解压命令:
|
|
||||||
```shell
|
|
||||||
tar -xzvf datasets.tar.gz
|
|
||||||
```
|
|
||||||
|
|
||||||
如果对你有帮助,麻烦点个**star** ~
|
|
||||||
|
|
||||||
Enjoy ~
|
|
@ -2,8 +2,8 @@ black
|
|||||||
ipdb
|
ipdb
|
||||||
jupyter
|
jupyter
|
||||||
numpy
|
numpy
|
||||||
torch
|
torch==2.1.0
|
||||||
torchvision
|
torchaudio==2.1.0
|
||||||
|
torchvision==0.16.0
|
||||||
tqdm
|
tqdm
|
||||||
matplotlib
|
matplotlib
|
||||||
pandas
|
|
Loading…
x
Reference in New Issue
Block a user