Spaces:
Build error
Build error
File size: 3,432 Bytes
b70a393 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
"""
****This file contain utlities functions****
@author : Wish Suharitdamrong
"""
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import argparse
import math
from sklearn.metrics import accuracy_score
def save_logs(model_name,savename,**kwargs):
"""
*********
save_logs : Save a logs in .csv files
*********
@author: Wish Suharitdamrong
------
inputs :
------
model_name : name of a model
savename : name of a file saving as a .csv
**kwagrs : array containing values of metrics such as accuracy and loss
-------
outputs :
-------
"""
path = "./logs/{}/".format(model_name)
if not os.path.exists(path):
os.mkdir(path)
df = pd.DataFrame()
for log in kwargs:
df[log] = kwargs[log]
savepath = os.path.join(path,savename)
df.to_csv(savepath, index=False)
def load_logs(model_name,savename, epoch, type_model=None):
"""
*********
load_logs : Load a logs file from csv
*********
@author: Wish Suharitdamrong
------
inputs :
------
model_name : name of a model
savename : name of a logs in .csv files
epoch : number of iteration continue from checkpoint
-------
outputs :
-------
train_loss : array containing training loss
train_acc : array containing training accuracy
vali_loss : array containing validation loss
vali_acc : array containing validation accuracy
"""
path = "./logs/{}/".format(model_name)
savepath = os.path.join(path,savename)
if type_model is None:
raise ValueError("Type of model should be specified Generator or SyncNet")
if not os.path.exists(savepath):
print("Logs file does not exists !!!!")
exit()
df = pd.read_csv(savepath)[:epoch+1]
if type_model == "syncnet":
train_loss = df["train_loss"]
train_acc = df['train_acc']
vali_loss = df['vali_loss']
vali_acc = df['vali_acc']
return train_loss, train_acc, vali_loss, vali_acc
elif type_model == "generator":
train_loss = df["train_loss"]
vali_loss = df["vali_loss"]
return train_loss, vali_loss
else :
raise ValueError(" Argument type of model (type_model) should be either 'generator' or 'syncnet' !!!!")
def get_accuracy(y_pred,y_true):
"""
*********
get_accuracy : calcualte accuracy of a model
*********
@author: Wish Suharitdamrong
------
inputs :
------
y_pred : predicted label
y_true : ground truth of a label
-------
outputs :
-------
acc : accuracy of a model
"""
acc = accuracy_score(y_pred,y_true, normalize=True) * 100
return acc
def procrustes(fl):
transformation = {}
fl, mean = translation(fl)
fl, scale = scaling(fl)
#fl , rotate = rotation(fl)
transformation['translate'] = mean
transformation['scale'] = scale
#transformation['rotate'] = rotate
return fl , transformation
def translation(fl):
mean = np.mean(fl, axis=0)
fl = fl - mean
return fl , mean
def scaling(fl):
scale = np.sqrt(np.mean(np.sum(fl**2, axis=1)))
fl = fl/scale
return fl , scale
|