File size: 2,245 Bytes
d262279 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | """Treino da SNN classificadora de blobs. Uso: python -m src.train_player"""
import argparse
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from .player_snn import PlayerSNN
class DS(Dataset):
def __init__(self):
d = np.load("data_players/players.npz")
self.x = torch.from_numpy(d["x"])
self.y = torch.from_numpy(d["y"]).long()
def __len__(self):
return len(self.y)
def __getitem__(self, i):
return self.x[i], self.y[i]
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--epochs", type=int, default=25)
ap.add_argument("--cpu", action="store_true")
args = ap.parse_args()
device = torch.device("cuda" if torch.cuda.is_available()
and not args.cpu else "cpu")
ds = DS()
n_te = max(1, int(0.15 * len(ds)))
tr, te = random_split(ds, [len(ds) - n_te, n_te],
generator=torch.Generator().manual_seed(0))
dl = DataLoader(tr, batch_size=256, shuffle=True)
dl_te = DataLoader(te, batch_size=256)
print(f"train={len(tr)} test={len(te)} device={device}")
model = PlayerSNN().to(device)
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4)
w = torch.tensor([1.2, 1.0], device=device)
crit = nn.CrossEntropyLoss(weight=w)
best = 0.0
for ep in range(1, args.epochs + 1):
model.train()
for x, y in dl:
x, y = x.to(device), y.to(device)
loss = crit(model(x), y)
opt.zero_grad(set_to_none=True)
loss.backward()
opt.step()
model.eval()
acc = 0.0
with torch.no_grad():
for x, y in dl_te:
acc += (model(x.to(device)).argmax(1) == y.to(device)).float().mean().item()
acc /= len(dl_te)
if acc > best:
best = acc
torch.save(model.state_dict(), "checkpoints/player_snn.pt")
if ep % 5 == 0 or ep == 1:
print(f"epoch {ep}: acc={acc:.3f} best={best:.3f}")
print(f"salvo | best_acc={best:.3f}")
if __name__ == "__main__":
main()
|