def _update_model_name(self):
     """
     Generates a unique model name based on parameters passed and put it on `self.model_name`.
     This is used when saving the model.
     """
     # get first letters of emotions, for instance:
     # ["sad", "neutral", "happy"] => 'HNS' (sorted alphabetically)
     emotions_str = get_first_letters(self.emotions)
     # 'c' for classification & 'r' for regression
     problem_type = 'c' if self.classification else 'r'
     dropout_str = get_dropout_str(self.dropout, n_layers=self.n_dense_layers + self.n_rnn_layers)
     self.model_name = f"{emotions_str}-{problem_type}-{self.cell.__name__}-layers-{self.n_rnn_layers}-{self.n_dense_layers}-units-{self.rnn_units}-{self.dense_units}-dropout-{dropout_str}.h5"
Exemplo n.º 2
0
 def load_metadata_from_desc_file(self, desc_files, partition):
     """Read metadata from a CSV file & Extract and loads features of audio files
     Params:
         desc_files (list): list of description files (csv files) to read from
         partition (str): whether is "train" or "test"
     """
     # empty dataframe
     df = pd.DataFrame({'path': [], 'emotion': []})
     for desc_file in desc_files:
         # concat dataframes
         df = pd.concat((df, pd.read_csv(desc_file)), sort=False)
     if self.verbose:
         print("[*] Loading audio file paths and its corresponding labels...")
     # get columns
     audio_paths, emotions = list(df['path']), list(df['emotion'])
     # if not classification, convert emotions to numbers
     if not self.classification:
         # so naive and need to be implemented
         # in a better way
         if len(self.emotions) == 3:
             self.categories = {'sad': 1, 'neutral': 2, 'happy': 3}
         elif len(self.emotions) == 5:
             self.categories = {'angry': 1, 'sad': 2, 'neutral': 3, 'ps': 4, 'happy': 5}
         else:
             raise TypeError("Regression is only for either ['sad', 'neutral', 'happy'] or ['angry', 'sad', 'neutral', 'ps', 'happy']")
         emotions = [ self.categories[e] for e in emotions ]
     # make features folder if does not exist
     if not os.path.isdir(self.features_folder_name):
         os.mkdir(self.features_folder_name)
     # get label for features
     label = get_label(self.audio_config)
     # construct features file name
     n_samples = len(audio_paths)
     first_letters = get_first_letters(self.emotions)
     name = os.path.join(self.features_folder_name, f"{partition}_{label}_{first_letters}_{n_samples}.npy")
     if os.path.isfile(name):
         # if file already exists, just load then
         if self.verbose:
             print("[+] Feature file already exists, loading...")
         features = np.load(name)
     else:
         # file does not exist, extract those features and dump them into the file
         features = []
         append = features.append
         for audio_file in tqdm.tqdm(audio_paths, f"Extracting features for {partition}"):
             feature = extract_feature(audio_file, **self.audio_config)
             if self.input_dimension is None:
                 self.input_dimension = feature.shape[0]
             append(feature)
         # convert to numpy array
         features = np.array(features)
         # save it
         np.save(name, features)
     if partition == "train":
         try:
             self.train_audio_paths
         except AttributeError:
             self.train_audio_paths = audio_paths
             self.train_emotions = emotions
             self.train_features = features
         else:
             if self.verbose:
                 print("[*] Adding additional training samples")
             self.train_audio_paths += audio_paths
             self.train_emotions += emotions
             self.train_features = np.vstack((self.train_features, features))
     elif partition == "test":
         try:
             self.test_audio_paths
         except AttributeError:
             self.test_audio_paths = audio_paths
             self.test_emotions = emotions
             self.test_features = features
         else:
             if self.verbose:
                 print("[*] Adding additional testing samples")
             self.test_audio_paths += audio_paths
             self.test_emotions += emotions
             self.test_features = np.vstack((self.test_features, features))
     else:
         raise TypeError("Invalid partition, must be either train/test")
Exemplo n.º 3
0
 def _update_model_name(self):
     emotions_str = get_first_letters(self.emotions)
     # 'c' for classification & 'r' for regression
     problem_type = 'c' if self.classification else 'r'
     dropout_str = get_dropout_str(self.dropout, n_layers=self.n_dense_layers + self.n_rnn_layers)
     self.model_name = f"{emotions_str}-{problem_type}-{self.cell.__name__}-layers-{self.n_rnn_layers}-{self.n_dense_layers}-units-{self.rnn_units}-{self.dense_units}-dropout-{dropout_str}.h5"