Skip to content

Instantly share code, notes, and snippets.

View devforfu's full-sized avatar

Ilia devforfu

View GitHub Profile
@devforfu
devforfu / accuracy_callback.py
Created December 7, 2018 10:19
Iterative accuracy computation
def accuracy(out, y_true):
y_hat = out.argmax(dim=-1).view(y_true.size(0), -1)
y_true = y_true.view(y_true.size(0), -1)
match = y_hat == y_true
return match.float().mean()
class Accuracy(Callback):
def epoch_started(self, **kwargs):
@devforfu
devforfu / running_loss.py
Created December 7, 2018 09:27
Smoothed loss callback
class RollingLoss(Callback):
def __init__(self, smooth=0.98):
self.smooth = smooth
def batch_ended(self, phase, **kwargs):
prev = phase.rolling_loss
a = self.smooth
avg_loss = a * prev + (1 - a) * phase.batch_loss
debias_loss = avg_loss / (1 - a ** phase.batch_index)
@devforfu
devforfu / callbacks_train.py
Created December 7, 2018 09:10
A better training loop
def train(model, opt, phases, callbacks=None, epochs=1, device=default_device, loss_fn=F.nll_loss):
model.to(device)
cb = callbacks
cb.training_started(phases=phases, optimizer=opt)
for epoch in range(1, epochs + 1):
cb.epoch_started(epoch=epoch)
@devforfu
devforfu / simple_train.py
Last active December 7, 2018 07:27
Simple training loop pseudocode
model = create_model(params)
phases = create_train_valid_data()
opt = optim.SGD(model.params, lr=1e-3)
model.to(device)
for epoch in range(1, epochs + 1):
for phase in phases:
n = len(phase.loader)
@devforfu
devforfu / fer2013.py
Created December 1, 2018 04:49
FER2013 dataset parsing from CSV into folder with images.
"""
Converting FER2013 dataset from CSV representation into folder with images.
The dataset is taken from:
https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge
Encoding:
(0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).
"""
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@devforfu
devforfu / train.py
Created November 25, 2018 14:45
fastai/pytorch parallel workers
"""
Training ResNet18 model on 50000 samples per category.
"""
import sys
from fastai import defaults
from fastai.vision import create_cnn, get_transforms
from fastai.metrics import accuracy
from fastai.callbacks import EarlyStoppingCallback, SaveModelCallback, CSVLogger
from fastai.vision.data import ImageItemList, imagenet_stats
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.