<jupyter_start><jupyter_text>Imports<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.nn as nn
import torch.optim as opt

from util import losses as loss
from util import loaders as load
from util import training as train
from util import helpers as helper
from models import networks as n<jupyter_output><empty_output><jupyter_text>Setup Network, Data Generator, Optimizer, Losses<jupyter_code>############################################################################
# Network
############################################################################

res_net = n.CustomResnet(51)
res_net.cuda()

############################################################################
# Loss
############################################################################

mse_crit = nn.MSELoss()
point_crit = loss.PointLoss().cuda()

############################################################################
# Optimizer
############################################################################

lr = 1e-3
lr_array = np.array([lr/100,lr/10,lr])
lr_groups = res_net.lr_groups()
opt_params = [{"params":lr_groups[0].parameters(),"lr":lr_array[0]},
              {"params":lr_groups[1].parameters(),"lr":lr_array[1]},
              {"params":lr_groups[2].parameters(),"lr":lr_array[2]}]

adam_opt = opt.Adam(opt_params,betas=(0.9, 0.999),weight_decay= .0001)

############################################################################
# Data Generators
############################################################################

transform = load.NormDenorm([.5, .5, .5], [.5, .5, .5])
train_data= load.LandMarkGenerator( transform, output_res=96, size = 10000, rand_rot = .7)
test_data = load.LandMarkGenerator( transform, output_res=96, size = 300, rand_rot = .7)   
batch_size = 8
mtrain = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=8, shuffle=True, drop_last=True)
mtest  = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=8, shuffle=False, drop_last=True)

############################################################################
# Train Dictionary
############################################################################

train_dict = {
    "train_loader":mtrain,
    "test_loader":mtest,
    "opt":adam_opt,
    "net":res_net,
    "mse_crit":mse_crit,
    "point_crit":point_crit
}

output_name = "test_"<jupyter_output><empty_output><jupyter_text>Example Training Data<jupyter_code>% matplotlib inline
helper.view_dataset(train_data,transform)<jupyter_output><empty_output><jupyter_text>Training Round One<jupyter_code>lr = 1e-3  
lr_array = np.array([lr/5,lr/3,lr])

for i in range(1,7):
    print (f'Run #{i}')
    epoch_count = i
    lr_list = train.set_lr_sched(epoch_count,mtrain.__len__()//batch_size,2.0)
    helper.lr_preview(lr_list)
    freeze = (i-1)*2
    if i == 4:
        freeze = 100
    train.one_run(train_dict,freeze,lr_list,lr_array)
    

helper.view_predictions(res_net,train_data)
helper.export_onnx(res_net,'output/'+output_name+'run_1')<jupyter_output><empty_output><jupyter_text>Training Round Two<jupyter_code>lr = 1e-4  
lr_array = np.array([lr/5,lr/3,lr])

for i in range(1,7):
    print (f'Run #{i}')
    epoch_count = i
    lr_list = train.set_lr_sched(epoch_count,mtrain.__len__()//batch_size,2.0)
    helper.lr_preview(lr_list)

    freeze = 100
    train.one_run(train_dict,freeze,lr_list,lr_array)


helper.view_predictions(res_net,train_data)
helper.export_onnx(res_net,'output/'+output_name+'run_2')<jupyter_output><empty_output><jupyter_text>Training Round Three (Test )<jupyter_code>lr = 1e-5  
lr_array = np.array([lr/5,lr/3,lr])

for i in range(1,7):
    print (f'Run #{i}')
    epoch_count = i
    lr_list = train.set_lr_sched(epoch_count,mtrain.__len__()//batch_size,2.0)
    helper.lr_preview(lr_list)

    freeze = 100
    train.one_run(train_dict,freeze,lr_list,lr_array)
    

helper.view_predictions(res_net,train_data)
helper.export_onnx(res_net,'output/'+output_name+'run_3')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2995085995085995, 'fraction_numerical': 0.021375921375921376, 'mean_word_length': 6.8895348837209305, 'pattern_counts': {'":': 12, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '372069', 'license': 'MIT', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1349, 'n_words': 205, 'path': 'face_pose_net.ipynb', 'repo_name': 'NeuralVFX/face-pose-estimation-pytorch', 'size': '4070'}