def predict_save(self): relabel = relabel() x_test, test_fname = data_util.load_test() label_index = [ 'down', 'go', 'left', 'no', 'off', 'on', 'right', 'silence', 'stop', 'unknown', 'up', 'yes' ] print("start predicting...") predList = [] model = self.fitted_model if (model == None): print('model not fitted yet!') return predicts = model.predict(x_test) predicts = np.argmax(predicts, axis=1) predicts = [label_index[p] for p in predicts] if not relabel: predicts = label_transform(predicts, relabel=True, get_dummies=False) df = pd.DataFrame(columns=['fname', 'label']) df['fname'] = test_fname df['label'] = predicts df.to_csv(Configure.submission_path, index=False) print('done!')
if len(samples) > 16000: n_samples = chop_audio(samples, num=chopNum) else: n_samples = [samples] for samples in n_samples: if new_sample_rate >= sample_rate: resampled = samples else: resampled = signal.resample( samples, int(new_sample_rate / sample_rate * samples.shape[0])) _, _, specgram = log_specgram(resampled, sample_rate=new_sample_rate) y_train.append(label) x_train.append(specgram) x_train = np.array(x_train) x_train = x_train.reshape(tuple(list(x_train.shape) + [1])) y_train = label_transform(y_train, relabel=relabel, get_dummies=True) label_index = y_train.columns.values y_train = y_train.values y_train = np.array(y_train) del labels, fnames gc.collect() print('x_train:', x_train.shape, ', y_train:', y_train.shape) print("Save train data...") data_util.save_dataset(x_train, y_train) del x_train, y_train gc.collect() batch = 16
weight = [] #weight = [i/sum(weight) for i in weight] label_index = [ 'down', 'go', 'left', 'no', 'off', 'on', 'right', 'silence', 'stop', 'unknown', 'up', 'yes' ] modelPaths = glob(os.path.join(Configure.model_path, '*74*')) modelList = [] for modelFile in modelPaths: model = load_model(modelFile) modelList.append(model) print("start averaging...") predList = [] for model in modelList: predicts = model.predict(x_test) predList.append(predicts) if len(predList) == len(weight) and sum(weight) == 1.0: predList = [a * b for a, b in zip(weight, predList)] predicts = sum(predList) / len(modelPaths) predicts = np.argmax(predicts, axis=1) predicts = [label_index[p] for p in predicts] if not relabel: predicts = label_transform(predicts, relabel=True, get_dummies=False) del x_test gc.collect() df = pd.DataFrame(columns=['fname', 'label']) df['fname'] = test_fname df['label'] = predicts df.to_csv(Configure.submission_path, index=False)