text_html_output_1.png
from kaggle_secrets import UserSecretsClient

from logging import getLogger, INFO, FileHandler, Formatter, StreamHandler

from sklearn.metrics import accuracy_score

import json

import numpy as np

import os

import os

import pandas as pd

import random

import torch

import wandb

import warnings

import numpy as np
import pandas as pd
import math
import random
import time
from collections import OrderedDict
import tensorflow as tf
from tqdm import tqdm
import json
import os
import gc
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split, GroupKFold, StratifiedGroupKFold
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam, SGD, AdamW
from torch.optim.optimizer import Optimizer
import torchvision.models as models
from torch.nn.parameter import Parameter
from torch.utils.data import DataLoader, Dataset
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, ReduceLROnPlateau
from torchinfo import summary
import onnx
import onnx_tf
from onnx_tf.backend import prepare
import warnings
warnings.filterwarnings('ignore')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
VERSION = 12

def get_score(y_true, y_pred):
    score = accuracy_score(y_true, y_pred)
    return score
def seed_torch(seed=42):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
def load_relevant_data_subset_with_imputation(pq_path):
    data_columns = ['x', 'y', 'z']
    data = pd.read_parquet(pq_path, columns=data_columns)
    data.replace(np.nan, 0, inplace=True)
    n_frames = int(len(data) / CFG.rows_per_frame)
    data = data.values.reshape(n_frames, CFG.rows_per_frame, len(data_columns))
    return data.astype(np.float32)
def load_relevant_data_subset(pq_path):
    data_columns = ['x', 'y']
    data = pd.read_parquet(pq_path, columns=data_columns)
    n_frames = int(len(data) / CFG.rows_per_frame)
    data = data.values.reshape(n_frames, CFG.rows_per_frame, len(data_columns))
    return data.astype(np.float32)
def read_dict(file_path):
    path = os.path.expanduser(file_path)
    with open(path, 'r') as f:
        dic = json.load(f)
    return dic

class CFG:
    num_workers = 2
    apex = False
    scheduler = 'CosineAnnealingLR'
    epochs = 500
    print_freq = 200
    cosanneal_params = {'T_max': 5, 'eta_min': 3 * 1e-05, 'last_epoch': -1}
    reduce_params = {'mode': 'min', 'factor': 0.1, 'patience': 6, 'eps': 1e-06, 'verbose': True}
    cosanneal_res_params = {'T_0': 3, 'eta_min': 1e-06, 'T_mult': 1, 'last_epoch': -1}
    onecycle_params = {'pct_start': 0.1, 'div_factor': 10.0, 'max_lr': 0.001, 'steps_per_epoch': 3, 'epochs': 3}
    momentum = 0.9
    model_name = 'NN_ArcFace'
    lr = 3 * 0.0001
    weight_decay = 0.0001
    gradient_accumulation_steps = 1
    max_grad_norm = 1000
    data_path = '../input/asl-signs/'
    debug = False
    arcface = True
    use_aggregation_dataset = True
    target_size = 250
    rows_per_frame = 543
    batch_size = 512
    train = True
    early_stop = True
    target_col = 'label'
    scale = 30.0
    margin = 0.5
    easy_margin = False
    ls_eps = 0.0
    fc_dim = 512
    early_stopping_steps = 5
    grad_cam = False
    seed = 42

import os
OUTPUT_DIR = f'./{CFG.model_name}_version{VERSION}/'
if not os.path.exists(OUTPUT_DIR):
    os.makedirs(OUTPUT_DIR)
def init_logger(log_file=OUTPUT_DIR + 'train.log'):
    from logging import getLogger, INFO, FileHandler, Formatter, StreamHandler
    logger = getLogger(__name__)
    logger.setLevel(INFO)
    handler1 = StreamHandler()
    handler1.setFormatter(Formatter('%(message)s'))
    handler2 = FileHandler(filename=log_file)
    handler2.setFormatter(Formatter('%(message)s'))
    logger.addHandler(handler1)
    logger.addHandler(handler2)
    return logger
LOGGER = init_logger()

train = pd.read_csv(f'{CFG.data_path}train.csv')
label_index = read_dict(f'{CFG.data_path}sign_to_prediction_index_map.json')
index_label = dict([(label_index[key], key) for key in label_index])
train['label'] = train['sign'].map(lambda sign: label_index[sign])
if CFG.debug:
    CFG.epochs = 1
    train = train.sample(n=4000, random_state=CFG.seed).reset_index(drop=True)

from kaggle_secrets import UserSecretsClient
user_secrets = UserSecretsClient()
wandb_api = user_secrets.get_secret('wandb_key')
import wandb
wandb.login(key=wandb_api)
def class2dict(f):
    return dict(((name, getattr(f, name)) for name in dir(f) if not name.startswith('__')))
run = wandb.init(project='GISLR Competition', name=f'{CFG.model_name}_Version{VERSION}', config=class2dict(CFG), group=CFG.model_name, job_type='train')