Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance #7

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ traditional_method.py

!models/1
!models/1/**

__pycache__
.ipynb_checkpoints
9 changes: 4 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ dataset:
test_ratio: 0.05

train:
lr: 0.0008
lr: 0.0004
lr_scheduler:
T_0: 5
T_mult: 2
loss:
lambda_cos: 0.24
exponent: 2
epoches: 35
batch_size: 48
steps: 135
epoches: 3000
batch_size: 128

evaluate:
batch_size: 64
batch_size: 128
Binary file removed models/1/34.pth
Binary file not shown.
Binary file removed models/1/eval_loss.png
Binary file not shown.
Binary file removed models/1/lr.png
Binary file not shown.
35 changes: 0 additions & 35 deletions models/1/train.log

This file was deleted.

Binary file removed models/1/train_loss.png
Binary file not shown.
16 changes: 12 additions & 4 deletions rotate_captcha_crack/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ def __init__(self, train: bool = True) -> None:
self.backbone = models.regnet_x_1_6gf(pretrained=train)

fc_channels = self.backbone.fc.in_features
self.backbone.fc = nn.Linear(fc_channels, 1)
self.fc0 = nn.Linear(fc_channels, fc_channels)
self.act = nn.LeakyReLU()
self.fc1 = nn.Linear(fc_channels, 1)
del self.backbone.fc

if train:
nn.init.normal_(self.backbone.fc.weight, mean=0.0, std=0.01)
nn.init.zeros_(self.backbone.fc.bias)
nn.init.normal_(self.fc0.weight, mean=0.0, std=0.01)
nn.init.normal_(self.fc1.weight, mean=0.0, std=0.01)
nn.init.zeros_(self.fc0.bias)
nn.init.zeros_(self.fc1.bias)

def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.backbone.stem(x)
x = self.backbone.trunk_output(x)

x = self.backbone.avgpool(x)
x = x.flatten(start_dim=1)
x = self.backbone.fc(x)

x = self.fc0(x)
x = self.act(x)
x = self.fc1(x)

x.squeeze_(dim=1)
return x
25 changes: 17 additions & 8 deletions train.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import os
from pathlib import Path

import numpy as np
Expand All @@ -10,7 +11,6 @@

batch_size: int = CONFIG['train']['batch_size']
epoches: int = CONFIG['train']['epoches']
steps: int = CONFIG['train']['steps']
lr: float = CONFIG['train']['lr']
lambda_cos: float = CONFIG['train']['loss']['lambda_cos']
exponent: float = CONFIG['train']['loss']['exponent']
Expand Down Expand Up @@ -39,10 +39,14 @@
lr_vec = np.empty(epoches, dtype=np.float64)
train_loss_vec = np.empty(epoches, dtype=np.float64)
eval_loss_vec = np.empty(epoches, dtype=np.float64)
best_eval_loss = 10000000.0
previous_checkpoint_path = None

for epoch_idx in range(epoches):
model.train()
total_train_loss: float = 0
steps = 0

for step_idx, (source, target) in enumerate(train_dataloader):
source: torch.Tensor = source.to(device)
target: torch.Tensor = target.to(device)
Expand All @@ -53,13 +57,11 @@
loss.backward()
total_train_loss += loss.cpu().item()
optmizer.step()

if step_idx + 1 == steps:
break
steps += 1

scheduler.step()
lr_vec[epoch_idx] = scheduler.get_last_lr()[0]

train_loss = total_train_loss / steps
train_loss_vec[epoch_idx] = train_loss

Expand All @@ -81,9 +83,16 @@
LOG.info(
f"Epoch#{epoch_idx}. time_cost: {time.time()-start_time:.2f} s. train_loss: {train_loss:.8f}. eval_loss: {eval_loss:.4f} degrees"
)

if epoch_idx >= epoches / 2:
torch.save(model.state_dict(), str(model_dir / f"{epoch_idx}.pth"))

torch.save(model.state_dict(), str(model_dir / "last.pth"))
if eval_loss < best_eval_loss:
best_eval_loss = eval_loss
new_checkpoint_path = str(model_dir / f"{epoch_idx}_{eval_loss:.4f}.pth")
torch.save(model.state_dict(), new_checkpoint_path)
if previous_checkpoint_path is not None:
os.remove(previous_checkpoint_path)

previous_checkpoint_path = new_checkpoint_path

x = np.arange(epoches, dtype=np.int16)

Expand Down