Kaggle competition: State Farm Distracted Driver Detection
<SOS> metric: MulticlassLoss Kaggle data source: state-farm-distracted-driver-detection<save_to_csv>
import pandas as pd
import subprocess
import torch
import torch.optim as optim
from torch import nn
import torchvision
from torchvision import transforms
import os
from glob import glob
import numpy as np
from tqdm.notebook import tqdm
import random
import cv2
import sys
train[target_cols] = oof
train[['sig_id']+target_cols].to_csv('oof.csv', index=False)
 test[target_cols] = predictions
test[['sig_id']+target_cols].to_csv('pred.csv', index=False )<compute_test_metric>
EPOCH = 100
BATCH_SIZE = 16
PATIENCE = 3
cum_patience = 0
best_valid_loss = 999
DATA_PATH = '.. /input/state-farm-distracted-driver-detection'
WIDTH = 224
HEIGHT = 224
IMGNET_MEAN = [0.485, 0.456, 0.406]
IMGNET_STD = [0.229, 0.224, 0.225]
classes = [f'c{i}' for i in range(10)]
MODE = 'adv_data_aug'
os.mkdir(MODE)
 MODEL_NAME = 'resnet50'
assert MODEL_NAME in ['resnet50', 'resnet101', 'vgg19', 'densenet161']
<save_to_csv><EOS> 
driver_list = pd.read_csv(f'{DATA_PATH}/driver_imgs_list.csv')
 drivers = np.unique(driver_list['subject'].values)
 valid_driver = drivers[0]
prepare_dirs(MODE)
 trn_cnt, val_cnt = softlink_images(valid_driver, driver_list)
 trn_loader, val_loader = load_loaders(MODE)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model_conv = get_model(MODEL_NAME)
 criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_conv.parameters() , lr=0.0001, weight_decay=1e-6, momentum=0.9)
 softmax = nn.Softmax(dim=1)
 for epoch in range(EPOCH):
print(f'
train(model_conv, trn_loader, optimizer, criterion, trn_cnt)
 valid_loss = evaluate(model_conv, val_loader, criterion)
 model_save_path = f'{MODE}.{MODEL_NAME}.{valid_driver}'
cum_patience, best_valid_loss = save_best_model(model_save_path, cum_patience, valid_loss, best_valid_loss)
 if cum_patience == PATIENCE:
break
test_save_path = 'submission.csv'
predict_test(test_save_path, model_save_path, device )