|
| 1 | +# Imports |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +import os, sys, inspect |
| 5 | +from six.moves import cPickle as pickle |
| 6 | +import scipy.misc as misc |
| 7 | + |
| 8 | +# Parâmetros |
| 9 | +IMAGE_SIZE = 48 |
| 10 | +NUM_LABELS = 7 |
| 11 | + |
| 12 | +# Usando 10% dos dados para validação |
| 13 | +VALIDATION_PERCENT = 0.1 |
| 14 | + |
| 15 | +# Normalização |
| 16 | +IMAGE_LOCATION_NORM = IMAGE_SIZE // 2 |
| 17 | + |
| 18 | +# Seed |
| 19 | +np.random.seed(0) |
| 20 | + |
| 21 | +# For training |
| 22 | +train_error_list = [] |
| 23 | +train_step_list = [] |
| 24 | + |
| 25 | +# For validation |
| 26 | +valid_error_list = [] |
| 27 | +valid_step_list = [] |
| 28 | + |
| 29 | +# Emoções |
| 30 | +emotion = {0:'anger', |
| 31 | + 1:'disgust', |
| 32 | + 2:'fear', |
| 33 | + 3:'happy', |
| 34 | + 4:'sad', |
| 35 | + 5:'surprise', |
| 36 | + 6:'neutral'} |
| 37 | + |
| 38 | +# Classe para o resultado em teste |
| 39 | +class testResult: |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + self.anger = 0 |
| 43 | + self.disgust = 0 |
| 44 | + self.fear = 0 |
| 45 | + self.happy = 0 |
| 46 | + self.sad = 0 |
| 47 | + self.surprise = 0 |
| 48 | + self.neutral = 0 |
| 49 | + |
| 50 | + def evaluate(self,label): |
| 51 | + if (0 == label): |
| 52 | + self.anger = self.anger+1 |
| 53 | + if (1 == label): |
| 54 | + self.disgust = self.disgust+1 |
| 55 | + if (2 == label): |
| 56 | + self.fear = self.fear+1 |
| 57 | + if (3 == label): |
| 58 | + self.happy = self.happy+1 |
| 59 | + if (4 == label): |
| 60 | + self.sad = self.sad+1 |
| 61 | + if (5 == label): |
| 62 | + self.surprise = self.surprise+1 |
| 63 | + if (6 == label): |
| 64 | + self.neutral = self.neutral+1 |
| 65 | + |
| 66 | + def display_result(self,evaluations): |
| 67 | + print("anger = " + str((self.anger/float(evaluations))*100) + "%") |
| 68 | + print("disgust = " + str((self.disgust/float(evaluations))*100) + "%") |
| 69 | + print("fear = " + str((self.fear/float(evaluations))*100) + "%") |
| 70 | + print("happy = " + str((self.happy/float(evaluations))*100) + "%") |
| 71 | + print("sad = " + str((self.sad/float(evaluations))*100) + "%") |
| 72 | + print("surprise = " + str((self.surprise/float(evaluations))*100) + "%") |
| 73 | + print("neutral = " + str((self.neutral/float(evaluations))*100) + "%") |
| 74 | + |
| 75 | + |
| 76 | +# Função para leitura dos dados |
| 77 | +def read_data(data_dir, force=False): |
| 78 | + def create_onehot_label(x): |
| 79 | + label = np.zeros((1, NUM_LABELS), dtype=np.float32) |
| 80 | + label[:, int(x)] = 1 |
| 81 | + return label |
| 82 | + |
| 83 | + pickle_file = os.path.join(data_dir, "EmotionDetectorData.pickle") |
| 84 | + if force or not os.path.exists(pickle_file): |
| 85 | + train_filename = os.path.join(data_dir, "train.csv") |
| 86 | + data_frame = pd.read_csv(train_filename) |
| 87 | + data_frame['Pixels'] = data_frame['Pixels'].apply(lambda x: np.fromstring(x, sep=" ") / 255.0) |
| 88 | + data_frame = data_frame.dropna() |
| 89 | + print("Lendo train.csv ...") |
| 90 | + |
| 91 | + train_images = np.vstack(data_frame['Pixels']).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 1) |
| 92 | + print(train_images.shape) |
| 93 | + train_labels = np.array(list(map(create_onehot_label, data_frame['Emotion'].values))).reshape(-1, NUM_LABELS) |
| 94 | + print(train_labels.shape) |
| 95 | + |
| 96 | + permutations = np.random.permutation(train_images.shape[0]) |
| 97 | + train_images = train_images[permutations] |
| 98 | + train_labels = train_labels[permutations] |
| 99 | + validation_percent = int(train_images.shape[0] * VALIDATION_PERCENT) |
| 100 | + validation_images = train_images[:validation_percent] |
| 101 | + validation_labels = train_labels[:validation_percent] |
| 102 | + train_images = train_images[validation_percent:] |
| 103 | + train_labels = train_labels[validation_percent:] |
| 104 | + |
| 105 | + print("Lendo test.csv ...") |
| 106 | + test_filename = os.path.join(data_dir, "test.csv") |
| 107 | + data_frame = pd.read_csv(test_filename) |
| 108 | + data_frame['Pixels'] = data_frame['Pixels'].apply(lambda x: np.fromstring(x, sep=" ") / 255.0) |
| 109 | + data_frame = data_frame.dropna() |
| 110 | + test_images = np.vstack(data_frame['Pixels']).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 1) |
| 111 | + |
| 112 | + with open(pickle_file, "wb") as file: |
| 113 | + try: |
| 114 | + print('\nSalvando ...') |
| 115 | + save = { |
| 116 | + "train_images": train_images, |
| 117 | + "train_labels": train_labels, |
| 118 | + "validation_images": validation_images, |
| 119 | + "validation_labels": validation_labels, |
| 120 | + "test_images": test_images, |
| 121 | + } |
| 122 | + pickle.dump(save, file, pickle.HIGHEST_PROTOCOL) |
| 123 | + |
| 124 | + except: |
| 125 | + print("Não foi possível salvar :/") |
| 126 | + |
| 127 | + with open(pickle_file, "rb") as file: |
| 128 | + save = pickle.load(file) |
| 129 | + train_images = save["train_images"] |
| 130 | + train_labels = save["train_labels"] |
| 131 | + validation_images = save["validation_images"] |
| 132 | + validation_labels = save["validation_labels"] |
| 133 | + test_images = save["test_images"] |
| 134 | + |
| 135 | + return train_images, train_labels, validation_images, validation_labels, test_images |
0 commit comments