# Using Vocal-Based Emotions as a Human Error Prevention System with Convolutional Neural Networks
## 5 Conclusions
Appendix A Human Error Prevention System Based on Vocal Emotions Using Convolutional Neural Network Code  
Feature Extract  
import os  
import librosa  
import numpy as np  
audio_path = 'drive/My Drive/audiomodel/Audiodataset'  
tmpdir = 'drive/My Drive/audiomodel/temp'  
def preprocess(audio_path, tmpdir):  
audio_store_path = tmpdir + '/' + 'aud_features/'  
make_dir_aud = False  
if not os.path.exists(audio_store_path):  
os.mkdir(audio_store_path)  
make_dir_aud = True  
if (not make_dir_aud):  
return  
for subdir, dirs, files in os.walk(audio_path):  
for file in files:  
if file[6:8] in [\"03\",\"05\"] :  
print(file)  
try:X, sample_rate = librosa.load(os.path.join(subdir,file), res_type='kaiser_best') mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0) mfccs = np.asarray(mfccs) np.save(audio_store_path + file[0:-4], mfccs) except ValueError: continue  
Data split import os from sklearn.model_selection import train_test_split import numpy as np aud_file_path = 'drive/My Drive/audiomodel/temp/aud_features/' folders = [ '3', '5'] def create_test_train_val_sets(tmpdir):  
aud_file_path = tmpdir + '/' + 'aud_features/'  
aud_train_set = tmpdir + '/' + 'aud_train/'  
aud_val_set = tmpdir + '/' + 'aud_val/'  
aud_test_set = tmpdir + '/' + 'aud_test/' created = False paths = [aud_train_set, aud_val_set, aud_test_set]  
for path in paths: if not os.path.exists(path): os.mkdir(path) created = True for i in folders: subfolder = path + i os.mkdir(subfolder)  
print(created) if not created: return  
aud_data = []  
aud_files = []for file in os.listdir(aud_file_path) :  aud = np.load(os.path.join(aud_file_path, file))  aud_data.append(aud)  aud_files.append(file)  
aud_train, aud_test, aud_train_files, aud_test_files =  train_test_split(aud_data, aud_files, test_size=0.2, ran-  dom_state=42)  aud_val, aud_test, aud_val_files, aud_test_files =  train_test_split(aud_test, aud_test_files, test_size=0.5, ran-  dom_state=42)  
print(\"starting to write audio data\")  print(len(aud_train),\"TR\")  for i in range(len(aud_train)):  aud = aud_train[i]  aud = np.reshape(aud, (1, 40))  fp = aud_train_set + str(int(aud_train_files[i][7:8]) ) + '/'  np.save(fp + \"aud$d\" %i, aud)  
print(len(aud_test),\"Test\")  for i in range(len(aud_test)):  aud = aud_test[i]  aud = np.reshape(aud, (1, 40))  fp = aud_test_set + str(int(aud_test_files[i][7:8])) + '/'  np.save(fp + \"aud$d\" %i, aud)  
print(len(aud_val),\"VAL\")  
for i in range(len(aud_val)):  aud = aud_val[i]  aud = np.reshape(aud, (1, 40))  fp = aud_val_set + str(int(aud_val_files[i][7:8]) ) + '/'  np.save(fp + \"aud$d\" %i, aud)  
Audio model  
from _future_ import print_function, division  import torch  import torch.nn as nn  import torch.nn.functional as F import torchvision as tv  import torch.optim as optim  import numpy as np  from torchsummary import summary  class Net(nn.Module):  def__init__(self):  super(Net, self).__init__()  self.aud_conv1 = nn.Convld(1, 16, 3)  self.aud_pool = nn.MaxPoold(2)  self.aud_conv2 = nn.Convld(16, 32, 3)  self.aud_conv3 = nn.Convld(32, 64, 3)  self.fc1 = nn.Linear(64*15, 120)  self.d1 = nn.Dropout(0.2)  self.bn1 = nn.BatchNormNormald(64*15)  self.fc2 = nn.Linear(120, 84)  self.fc3 = nn.Linear(84, 2)  
def forward(self, x_aud):  x_aud = self.aud_pool(F.relu(self.aud_conv1(x_aud)))  x_aud = F.relu(self.aud_conv2(x_aud))  x_aud = F.relu(self.aud_conv3(x_aud))  x_aud = x_aud.view(-1, 64*15)  x_aud = self.bn1(x_aud)  x_aud = self.d1(x_aud)  x = F.relu(self.fc1(x_aud))  x = F.relu(self.fc2(x))  x = self.fc3(x)  return x  def train(aud_data_loader, criterion, net, device, optimizer):  t_acc = []  t_loss = []  v_acc = []  v_loss = []  for epoch in range (100):  running_loss = 0.0  l = 0  total = 0  correct = 0  for i, data in enumerate(aud_data_loader['aud_train']):  # get the inputs  aud_inputs, aud_labels = data  aud_inputs = aud_inputs.type(torch.FloatTensor)aud_inputs, aud_labels = aud_inputs.to(device), aud_labels.to(device)  # zero the parameter gradients  optimizer.zero_grad()  print(aud_inputs.shape)  outputs = net(aud_inputs)  loss = criterion(outputs, aud_labels)  _' predicted = torch.max(outputs.data, 1)  total += aud_labels.size(0)  correct += (predicted == aud_labels).sum().item()  loss.backward()  optimizer.step()