flyai.exe train -p=1 -b=64 -e=6000
将net_add_conv5_conv6_py 换成 net_conv1_conv2_conv3_conv4_py
score : 82.21
flyai.exe train -p=1 -b=64 -e=6000
score : 85.15
修改模型保存方式,将
# 若测试准确率高于当前最高准确率,则保存模型
train_accuracy = eval(model, x_test, y_test)
# if train_accuracy > best_accuracy:
# best_accuracy = train_accuracy
# model.save_model(cnn, MODEL_PATH, overwrite=True)
# print("step %d, best accuracy %g" % (i, best_accuracy))
改为
if i == args.EPOCHS - 1:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == args.EPOCHS:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
print(str(i) + "/" + str(args.EPOCHS))
main.py
和net_conv5_conv6的main.py一样
cnn = Net().to(device)
optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 选用AdamOptimizer
"""
实现Adam算法。
它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。
参数:
params (iterable) – 用于优化的可以迭代参数或定义参数组
lr (float, 可选) – 学习率(默认:1e-3)
betas (Tuple[float, float], 可选) – 用于计算梯度运行平均值及其平方的系数(默认:0.9,0.999)
eps (float, 可选) – 增加分母的数值以提高数值稳定性(默认:1e-8)
weight_decay (float, 可选) – 权重衰减(L2范数)(默认: 0)
"""
# optimizer = Adam(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 选用SGD_Optimizer(Stochastic Gradient Descent)
# 自适应优化算法训练出来的结果通常都不如SGD,尽管这些自适应优化算法在训练时表现的看起来更好。 使用者应当慎重使用自适应优化算法。
"""
利用惯性momentum,即当前梯度与上次梯度进行加权,
- 如果方向一致,则累加导致更新步长变大;
- 如果方向不同,则相互抵消中和导致更新趋向平衡。
"""
loss_fn = nn.CrossEntropyLoss() # 定义损失函数
net.py
# build CNN
from torch import nn
# build CNN
class Net(nn.Module):
#def __init__(self,num_classes=10):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2)
self.relu1=nn.ReLU(True)
self.bn1=nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)
self.relu2=nn.ReLU(True)
self.bn2=nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
self.relu3=nn.ReLU(True)
self.bn3=nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128*8*8, 1024)
self.relu5=nn.ReLU(True)
self.fc2 = nn.Linear(1024,6)
def forward(self, input):
output = self.conv1(input)
output = self.relu1(output)
output = self.bn1(output)
output = self.pool1(output)
output = self.conv2(output)
output = self.relu2(output)
output = self.bn2(output)
output = self.pool2(output)
output = self.conv3(output)
output = self.relu3(output)
output = self.bn3(output)
output = self.pool3(output)
output = self.conv4(output)
output = self.relu4(output)
output = self.bn4(output)
output = self.pool4(output)
output = output.view(-1, 128*8*8)
output = self.fc1(output)
output = self.relu5(output)
output = self.fc2(output)
return output
flyai.exe train -p=1 -b=64 -e=8000
score : 85.38
main.py
# -*- coding: utf-8 -*
import argparse
import torch
import torch.nn as nn
from flyai.dataset import Dataset
from torch.optim import Adam
from model import Model
from net import Net
from path import MODEL_PATH
# 数据获取辅助类
dataset = Dataset()
# 模型操作辅助类
model = Model(dataset)
# 超参
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--EPOCHS", default=1000, type=int, help="train epochs")
parser.add_argument("-b", "--BATCH", default=256, type=int, help="batch size")
parser.add_argument("-lr", "--learning_rate", default=0.001, type=float, help="learning_rate")
args = parser.parse_args()
# 判断gpu是否可用
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
device = torch.device(device)
def eval(model, x_test, y_test):
cnn.eval()
batch_eval = model.batch_iter(x_test, y_test)
total_acc = 0.0
data_len = len(x_test)
for x_batch, y_batch in batch_eval:
batch_len = len(x_batch)
outputs = cnn(x_batch)
_, prediction = torch.max(outputs.data, 1)
correct = (prediction == y_batch).sum().item()
acc = correct / batch_len
total_acc += acc * batch_len
return total_acc / data_len
#cnn = Net().to(device)
#optimizer = Adam(cnn.parameters(), lr=0.001, betas=(0.9, 0.999)) # 选用AdamOptimizer
#optimizer = Adam(cnn.parameters(), lr=0.00005, betas=(0.999999, 0.99999999999)) # 选用AdamOptimizer
#loss_fn = nn.CrossEntropyLoss() # 定义损失函数
cnn = Net().to(device)
optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 选用AdamOptimizer
"""
实现Adam算法。
它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。
参数:
params (iterable) – 用于优化的可以迭代参数或定义参数组
lr (float, 可选) – 学习率(默认:1e-3)
betas (Tuple[float, float], 可选) – 用于计算梯度运行平均值及其平方的系数(默认:0.9,0.999)
eps (float, 可选) – 增加分母的数值以提高数值稳定性(默认:1e-8)
weight_decay (float, 可选) – 权重衰减(L2范数)(默认: 0)
"""
# optimizer = Adam(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 选用SGD_Optimizer(Stochastic Gradient Descent)
# 自适应优化算法训练出来的结果通常都不如SGD,尽管这些自适应优化算法在训练时表现的看起来更好。 使用者应当慎重使用自适应优化算法。
"""
利用惯性momentum,即当前梯度与上次梯度进行加权,
- 如果方向一致,则累加导致更新步长变大;
- 如果方向不同,则相互抵消中和导致更新趋向平衡。
"""
loss_fn = nn.CrossEntropyLoss() # 定义损失函数
# 训练并评估模型
best_accuracy = 0
for i in range(args.EPOCHS):
cnn.train()
x_train, y_train, x_test, y_test = dataset.next_batch(args.BATCH) # 读取数据
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
x_train = x_train.float().to(device)
y_train = y_train.long().to(device)
x_test = torch.from_numpy(x_test)
y_test = torch.from_numpy(y_test)
x_test = x_test.float().to(device)
y_test = y_test.long().to(device)
outputs = cnn(x_train)
_, prediction = torch.max(outputs.data, 1)
optimizer.zero_grad()
loss = loss_fn(outputs, y_train)
loss.backward()
optimizer.step()
# 若测试准确率高于当前最高准确率,则保存模型
train_accuracy = eval(model, x_test, y_test)
# if train_accuracy > best_accuracy:
# best_accuracy = train_accuracy
# model.save_model(cnn, MODEL_PATH, overwrite=True)
# print("step %d, best accuracy %g" % (i, best_accuracy))
if i == 5000:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == 6000:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == args.EPOCHS - 1:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == args.EPOCHS:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
print(str(i) + "/" + str(args.EPOCHS))
net.py
## build CNN
from torch import nn
## build CNN
class Net(nn.Module):
#def __init__(self,num_classes=10):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2)
self.relu1=nn.ReLU(True)
self.bn1=nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)
self.relu2=nn.ReLU(True)
self.bn2=nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
self.relu3=nn.ReLU(True)
self.bn3=nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
#
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128*8*8, 1024)
#
self.relu5=nn.ReLU(True)
self.fc2 = nn.Linear(1024,6)
def forward(self, input):
output = self.conv1(input)
output = self.relu1(output)
output = self.bn1(output)
output = self.pool1(output)
output = self.conv2(output)
output = self.relu2(output)
output = self.bn2(output)
output = self.pool2(output)
output = self.conv3(output)
output = self.relu3(output)
output = self.bn3(output)
output = self.pool3(output)
output = self.conv4(output)
output = self.relu4(output)
output = self.bn4(output)
output = self.pool4(output)
output = output.view(-1, 128*8*8)
output = self.fc1(output)
output = self.relu5(output)
output = self.fc2(output)
return output
./flyai train -p=1 -b=64 -e=8000
score : 85.24
## build CNN
from torch import nn
## build CNN
class Net(nn.Module):
#def __init__(self,num_classes=10):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2)
self.relu1=nn.ReLU(True)
self.bn1=nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)
self.relu2=nn.ReLU(True)
self.bn2=nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
self.relu3=nn.ReLU(True)
self.bn3=nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128*8*8, 1024)
self.relu5=nn.ReLU(True)
self.fc2 = nn.Linear(1024,6)
def forward(self, input):
output = self.conv1(input)
output = self.relu1(output)
output = self.bn1(output)
output = self.pool1(output)
output = self.conv2(output)
output = self.relu2(output)
output = self.bn2(output)
output = self.pool2(output)
output = self.conv3(output)
output = self.relu3(output)
output = self.bn3(output)
output = self.pool3(output)
output = self.conv4(output)
output = self.relu4(output)
output = self.bn4(output)
output = self.pool4(output)
output = output.view(-1, 128*8*8)
output = self.fc1(output)
output = self.relu5(output)
output = self.fc2(output)
return output
./flyai train -p=1 -b=64 -e=8000
score : 83.24
将AdamOptimizer换成SGD_Optimizer(Stochastic Gradient Descent)
main.py
# -*- coding: utf-8 -*
import argparse
import torch
import torch.nn as nn
from flyai.dataset import Dataset
from torch.optim import Adam
from torch.optim import SGD
from model import Model
from net import Net
from path import MODEL_PATH
# 数据获取辅助类
dataset = Dataset()
# 模型操作辅助类
model = Model(dataset)
# 超参
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--EPOCHS", default=1000, type=int, help="train epochs")
parser.add_argument("-b", "--BATCH", default=256, type=int, help="batch size")
parser.add_argument("-lr", "--learning_rate", default=0.001, type=float, help="learning_rate")
parser.add_argument("-m", "--momentum", default=0.9, type=int, help="momentum")
# parser.add_argument("-
args = parser.parse_args()
# 判断gpu是否可用
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
device = torch.device(device)
def eval(model, x_test, y_test):
cnn.eval()
batch_eval = model.batch_iter(x_test, y_test)
total_acc = 0.0
data_len = len(x_test)
for x_batch, y_batch in batch_eval:
batch_len = len(x_batch)
outputs = cnn(x_batch)
_, prediction = torch.max(outputs.data, 1)
correct = (prediction == y_batch).sum().item()
acc = correct / batch_len
total_acc += acc * batch_len
return total_acc / data_len
#cnn = Net().to(device)
#optimizer = Adam(cnn.parameters(), lr=0.001, betas=(0.9, 0.999)) # 选用AdamOptimizer
#optimizer = Adam(cnn.parameters(), lr=0.00005, betas=(0.999999, 0.99999999999)) # 选用AdamOptimizer
#loss_fn = nn.CrossEntropyLoss() # 定义损失函数
cnn = Net().to(device)
# optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 选用AdamOptimizer
"""
实现Adam算法。
它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。
参数:
params (iterable) – 用于优化的可以迭代参数或定义参数组
lr (float, 可选) – 学习率(默认:1e-3)
betas (Tuple[float, float], 可选) – 用于计算梯度运行平均值及其平方的系数(默认:0.9,0.999)
eps (float, 可选) – 增加分母的数值以提高数值稳定性(默认:1e-8)
weight_decay (float, 可选) – 权重衰减(L2范数)(默认: 0)
"""
optimizer = SGD(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 选用SGD_Optimizer(Stochastic Gradient Descent)
# 自适应优化算法训练出来的结果通常都不如SGD,尽管这些自适应优化算法在训练时表现的看起来更好。 使用者应当慎重使用自适应优化算法。
"""
利用惯性momentum,即当前梯度与上次梯度进行加权,
- 如果方向一致,则累加导致更新步长变大;
- 如果方向不同,则相互抵消中和导致更新趋向平衡。
"""
loss_fn = nn.CrossEntropyLoss() # 定义损失函数
# 训练并评估模型
best_accuracy = 0
for i in range(args.EPOCHS):
cnn.train()
x_train, y_train, x_test, y_test = dataset.next_batch(args.BATCH) # 读取数据
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
x_train = x_train.float().to(device)
y_train = y_train.long().to(device)
x_test = torch.from_numpy(x_test)
y_test = torch.from_numpy(y_test)
x_test = x_test.float().to(device)
y_test = y_test.long().to(device)
outputs = cnn(x_train)
_, prediction = torch.max(outputs.data, 1)
optimizer.zero_grad()
loss = loss_fn(outputs, y_train)
loss.backward()
optimizer.step()
# 若测试准确率高于当前最高准确率,则保存模型
train_accuracy = eval(model, x_test, y_test)
# if train_accuracy > best_accuracy:
# best_accuracy = train_accuracy
# model.save_model(cnn, MODEL_PATH, overwrite=True)
# print("step %d, best accuracy %g" % (i, best_accuracy))
if i == 5000:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == 6000:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == args.EPOCHS - 1:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
if i == args.EPOCHS:
model.save_model(cnn, MODEL_PATH, overwrite=True)
print("step %d, the model is saved" % (i))
print(str(i) + "/" + str(args.EPOCHS))
net.py
## build CNN
from torch import nn
## build CNN
class Net(nn.Module):
#def __init__(self,num_classes=10):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2)
self.relu1=nn.ReLU(True)
self.bn1=nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)
self.relu2=nn.ReLU(True)
self.bn2=nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
self.relu3=nn.ReLU(True)
self.bn3=nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(2, 2)
self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.relu4=nn.ReLU(True)
self.bn4=nn.BatchNorm2d(128)
self.pool4 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128*8*8, 1024)
self.relu5=nn.ReLU(True)
self.fc2 = nn.Linear(1024,6)
def forward(self, input):
output = self.conv1(input)
output = self.relu1(output)
output = self.bn1(output)
output = self.pool1(output)
output = self.conv2(output)
output = self.relu2(output)
output = self.bn2(output)
output = self.pool2(output)
output = self.conv3(output)
output = self.relu3(output)
output = self.bn3(output)
output = self.pool3(output)
output = self.conv4(output)
output = self.relu4(output)
output = self.bn4(output)
output = self.pool4(output)
output = output.view(-1, 128*8*8)
output = self.fc1(output)
output = self.relu5(output)
output = self.fc2(output)
return output