import numpy as np from preprocessings import get_mfccs, load_from_pickle, save_to_pickle from GenderClassifier import GenderClassifier import torch from torch.autograd import Variable x_train_mfccs = np.asarray(get_mfccs(pickle_file="X-train-mfccs.pkl")) / 100 x_test_mfccs = np.asarray(get_mfccs(pickle_file="X-test-mfccs.pkl")) / 100 y_train = load_from_pickle(filename="y-train.pkl") y_test = load_from_pickle(filename="y-test.pkl") x_train_tensor = Variable(torch.Tensor(x_train_mfccs), requires_grad=False) x_test_tensor = Variable(torch.Tensor(x_test_mfccs), requires_grad=False) y_train_tensor = Variable(torch.LongTensor(y_train), requires_grad=False) y_test_tensor = Variable(torch.LongTensor(y_test), requires_grad=False) model = GenderClassifier() model.load_state_dict(torch.load('model_state_dict.pkl')) model.eval() fc_train_in = model.get_fc_in(x_train_tensor) fc_test_in = model.get_fc_in(x_test_tensor) save_to_pickle(fc_train_in.detach().numpy(), "fc-train-in.pkl") save_to_pickle(fc_test_in.detach().numpy(), "fc-test-in.pkl")
import pickle from GenderClassifier import Args, GenderClassifier from preprocessings import get_datases, get_mfccs, save_to_pickle, load_from_pickle, regularize_data from constants import CLASSES import matplotlib.pyplot as plt feature_loading = 'load_from_pkl' # option: load_from_flac or load_from_pkl feature_store = True model_loading = 'load_from_pkl' # option: load_from_pkl or train if feature_loading == 'load_from_flac': (x_train, y_train), (x_test, y_test) = get_datases(class_type='speaker') x_train_mfccs = get_mfccs(x_train) x_test_mfccs = get_mfccs(x_test) if feature_store: save_to_pickle(x_train_mfccs, "X-train-mfccs.pkl") save_to_pickle(x_test_mfccs, "X-test-mfccs.pkl") save_to_pickle(y_train, "y-train.pkl") save_to_pickle(y_test, "y-test.pkl") else: x_train_mfccs = get_mfccs(pickle_file="X-train-mfccs.pkl") x_test_mfccs = get_mfccs(pickle_file="X-test-mfccs.pkl") y_train = load_from_pickle(filename="y-train.pkl") y_test = load_from_pickle(filename="y-test.pkl") x_train_mfccs = regularize_data(x_train_mfccs)
lstm_bias_ih = model.state_dict()[layer_list[6]].detach().numpy() lstm_bias_hh = model.state_dict()[layer_list[7]].detach().numpy() fc1_weight = model.state_dict()[layer_list[8]].detach().numpy() fc1_bias = model.state_dict()[layer_list[9]].detach().numpy() fc2_weight = model.state_dict()[layer_list[10]].detach().numpy() fc2_bias = model.state_dict()[layer_list[11]].detach().numpy() fc3_weight = model.state_dict()[layer_list[12]].detach().numpy() fc3_bias = model.state_dict()[layer_list[13]].detach().numpy() args = Args() #======================================================= # Load Data #======================================================= x_train_mfccs = get_mfccs(pickle_file="X-train-mfccs.pkl") y_train = load_from_pickle(filename="y-train.pkl") x_train_tensor = Variable(torch.Tensor(x_train_mfccs), requires_grad=False) y_train_tensor = Variable(torch.LongTensor(y_train), requires_grad=False) x_valid_mfccs = get_mfccs(pickle_file="X-valid-mfccs.pkl") y_valid = load_from_pickle(filename="y-valid.pkl") x_valid_tensor = Variable(torch.Tensor(x_valid_mfccs), requires_grad=False) y_valid_tensor = Variable(torch.LongTensor(y_valid), requires_grad=False) x_test_mfccs = get_mfccs(pickle_file="X-test-mfccs.pkl") y_test = load_from_pickle(filename="y-test.pkl") x_test_tensor = Variable(torch.Tensor(x_test_mfccs), requires_grad=False) y_test_tensor = Variable(torch.LongTensor(y_test), requires_grad=False) x_data_tensor = x_test_tensor
model.eval() layer_list = list(model.state_dict().keys()) print(layer_list) conv1_weights = model.state_dict()[layer_list[0]].detach().numpy() lstm_weight_ih = model.state_dict()[layer_list[1]].detach().numpy() lstm_weight_hh = model.state_dict()[layer_list[2]].detach().numpy() fc1_weight = model.state_dict()[layer_list[3]].detach().numpy() args = Args() #======================================================= # Load Data #======================================================= x_test_mfccs = np.asarray(get_mfccs(pickle_file="X-test-mfccs.pkl")) y_test = load_from_pickle(filename="y-test.pkl") x_test_mfccs = regularize_data(x_test_mfccs) x_test_tensor = Variable(torch.Tensor(x_test_mfccs), requires_grad=False) y_test_tensor = Variable(torch.Tensor(y_test), requires_grad=False) x_data_tensor = x_test_tensor y_data_tensor = y_test_tensor x_data = x_data_tensor.numpy() y_data = y_data_tensor.numpy() #======================================================= # Accuracy of valid data #======================================================= print("------------- Test Accuracy -------------")