batch_size = 50
base_lr = 0.6
max_epoch = 20
model_path = './tttdensenet.pth'
# 將圖片轉為tensor
transforms = Compose([ToTensor()])
train_dataset = CaptchaData('./pic_train2', './answer/answer_train_v2.csv',transform=transforms)
# dataloader讀取batchsize資料
train_data_loader = DataLoader(train_dataset, batch_size=batch_size, num_workers=0,shuffle=True, drop_last=True)
test_data = CaptchaData('./pic_test2','./answer/answer_test_v2.csv',transform=transforms)
test_data_loader = DataLoader(test_data, batch_size=batch_size,num_workers=0, shuffle=True, drop_last=True)
# 讀取預訓練模型densenet201
cnn = models.densenet201(num_classes=180)
if torch.cuda.is_available():
cnn.cuda()
if restor:
cnn.load_state_dict(torch.load(model_path))
# 採用SGD + momentum當優化器
optimizer = torch.optim.SGD(cnn.parameters(), lr=base_lr, momentum=0.9)
criterion = nn.MultiLabelSoftMarginLoss()
acc_history_train = []
loss_history_train = []
loss_history_test = []
acc_history_test = []
for epoch in range(max_epoch):
start_ = time.time()
loss_history = []
acc_history = []
cnn.train()
for img, target in train_data_loader:
img = Variable(img)
target = Variable(target)
if torch.cuda.is_available():
img = img.cuda()
target = target.cuda()
output = cnn(img)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
acc = calculat_acc(output, target)
acc_history.append(float(acc))
loss_history.append(float(loss))
print('train_loss: {:.4}|train_acc: {:.4}'.format(
torch.mean(torch.Tensor(loss_history)),
torch.mean(torch.Tensor(acc_history)),
))
acc_history_train.append((torch.mean(torch.Tensor(acc_history))).float())
loss_history_train.append((torch.mean(torch.Tensor(loss_history))).float())
loss_history = []
acc_history = []
cnn.eval()
for img, target in test_data_loader:
img = Variable(img)
target = Variable(target)
if torch.cuda.is_available():
img = img.cuda()
target = target.cuda()
output = cnn(img)
acc = calculat_acc(output, target)
acc_history.append(float(acc))
loss_history.append(float(loss))
print('test_loss: {:.4}|test_acc: {:.4}'.format(
torch.mean(torch.Tensor(loss_history)),
torch.mean(torch.Tensor(acc_history)),
))
acc_history_test.append((torch.mean(torch.Tensor(acc_history))).float())
loss_history_test.append((torch.mean(torch.Tensor(loss_history))).float())
print('epoch: {}|time: {:.4f}'.format(epoch, time.time() - start_))
print("========================================")
torch.save(cnn.state_dict(), model_path)
# 畫出acc學習曲線
acc = acc_history_train
epoches = range(1, len(acc) + 1)
val_acc = acc_history_test
plt.plot(epoches, acc, 'b', label='Training acc')
plt.plot(epoches, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend(loc='lower right')
plt.grid()
# 儲存acc學習曲線
plt.savefig('./acc.png')
plt.show()
# 畫出loss學習曲線
loss = loss_history_train
val_loss = loss_history_test
plt.plot(epoches, loss, 'b', label='Training loss')
plt.plot(epoches, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend(loc='upper right')
plt.grid()
# 儲存loss學習曲線
plt.savefig('loss.png')
plt.show()
Adam
只有將優化器改為Adam,其餘都一樣。
# 需要修改的只有這個code,將optimizer改成Adam就好。
optimizer = torch.optim.Adam(cnn.parameters(), lr=base_lr)
Adadelta
學習率設置0.01時,訓練很慢,連續10個epoch都為0,故將學習率調為0.8。
optimizer = torch.optim.Adadelta( cnn.parameters(),lr=base_lr, rho=0.9, eps=1e-06, weight_decay=0)
學習曲線
這邊我認為他的loss下降較慢,準確度還是有在緩緩上升,多練幾個epoches可能會更好,但看似有點過擬和。
Adagrad
學習率設置為0.01。
optimizer = torch.optim.Adagrad(cnn.parameters(), lr=0.01, lr_decay=0, weight_decay=0)
Adamax
學習率設置0.02(預設)。
optimizer = torch.optim.Adamax(cnn.parameters(), lr=0.002, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
學習曲線
ASGD
學習率設置0.01(預設)。
optimizer = torch.optim.ASGD(cnn.parameters(), lr=0.01, lambd=0.0001, alpha=0.75, t0=1000000.0, weight_decay=0)
學習曲線
ASGD收斂較緩慢,設置了40個epoches,可以看出loss值下降較緩。
RMSprop
全都照預設去設置。
optimizer = torch.optim.RMSprop(cnn.parameters(), lr=0.01, alpha=0.99, eps=1e-08, weight_decay=0, momentum=0, centered=False)
學習曲線
可以發現這個優化器,準確度很震盪。
Rprop
全都照預設去設置。
optimizer = torch.optim.Rprop(cnn.parameters(), lr=0.01, etas=(0.5, 1.2), step_sizes=(1e-06, 50))
或許是這個優化器參數我不會調整,練出來準確度都是0,希望有大神可以教我。很抱歉這個優化器沒能給大家帶來示範。
SGD+momentum
Adam
Adadelta
Adagrad
Adamax
ASGD
RMSprop
每個優化器都各有好壞,碰上各種樣本,都沒有一定的優劣,建議多嘗試,找出最好的優化器,建出最好的模型。
今天比較的都是固定學習率,明天來搭配CosineAnnealing來訓練看看,是否準確度會有所變化呢?