def calc_metrics(loader, actor, device): pesq_all = [] stoi_all = [] for batch in loader: x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) out_r, out_i = actor(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() #print("Y:", y.shape) #source, targets, preds = inverse(t, y, m, x) targets, preds = inverse(t, y, m, x) for j in range(len(targets)): curr_pesq = pesq(targets[j].detach().cpu().numpy(), preds[j].detach().cpu().numpy(), 16000) curr_stoi = stoi(targets[j].detach().cpu().numpy(), preds[j].detach().cpu().numpy(), 16000) pesq_all.append(curr_pesq) stoi_all.append(curr_stoi) PESQ = torch.mean(torch.tensor(pesq_all)) STOI = torch.mean(torch.tensor(stoi_all)) return PESQ, STOI
def upload(self): """ uploading video file from FileChooser widget and predicting sign number :return: None """ file_path = self.file_chooser.selection[0] # get path from widget try: cur_sign = prepare_video_capture( file_path) # get an array of frames cur_sign = np.expand_dims(cur_sign.reshape(*cur_sign.shape, 1), axis=0) res = predict(model, cur_sign) # get prediction self.predict_label.text = f"predicted sign: {DICT[res]}" # output except Exception as e: # if the selected file was in the wrong format popup = Popup(title='Error', content=Label(text="invalid file"), size_hint=(None, None), size=(300, 250)) popup.open() # playback the video file self.video_playback.source = file_path self.video_playback.play = True
def update_actor(actor, critic, loader, optimizer, criterion, device): epoch_loss = 0 critic.eval() for batch in loader: x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) out_r, out_i = actor(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() x = x.squeeze() y = torch.cat((y, t), 2) pred_scores = critic(y) loss = criterion(pred_scores) optimizer.zero_grad() loss.backward() optimizer.step() loss = loss.detach().cpu().numpy() epoch_loss += loss critic.train() return epoch_loss / len(loader)
def update_critic(actor, critic, loader, optimizer, criterion, device): epoch_loss = 0 for batch in loader: x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) actor.eval() with torch.no_grad(): out_r, out_i = actor(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() x = x.squeeze() disc_input_y = torch.cat((y, t), 2) disc_input_t = torch.cat((t, t), 2) disc_input_x = torch.cat((x, t), 2) pred_scores = [] pred_scores.append(critic(disc_input_x)) pred_scores.append(critic(disc_input_y)) pred_scores.append(critic(disc_input_t)) pred_scores = torch.transpose(torch.stack(pred_scores).squeeze(), 0, 1) loss = criterion(x, y, t, m, pred_scores, device) optimizer.zero_grad() loss.backward() optimizer.step() loss = loss.detach().cpu().numpy() epoch_loss += loss actor.train() return epoch_loss / len(loader)
def inference(clean_path, noisy_path, model_path, out_path): device = torch.device("cuda:1") model = Actor() model = nn.DataParallel(model, device_ids=[1, 2]) model.load_state_dict(torch.load(model_path + 'actor_best.pth')) model = model.to(device) dataset = Data(clean_path, noisy_path, mode='Test') loader = data.DataLoader(dataset, batch_size=32, shuffle=False, collate_fn=collate_custom) fnames = os.listdir(noisy_path) print("Num files:", len(fnames)) pesq_all = [] stoi_all = [] fcount = 0 for batch in tqdm(loader): x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) out_r, out_i = model(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() x = x.squeeze() source, targets, preds = inverse(t, y, m, x) for j in range(len(targets)): t_j = targets[j].detach().cpu().numpy() p_j = preds[j].detach().cpu().numpy() p_j = 10 * (p_j / np.linalg.norm(p_j)) curr_pesq = pesq(t_j, p_j, 16000) curr_stoi = stoi(t_j, p_j, 16000) pesq_all.append(curr_pesq) stoi_all.append(curr_stoi) try: sf.write(os.path.join(out_path, fnames[fcount]), p_j, 16000) except IndexError: print("Fcount:", fcount, len(fnames)) fcount += 1 PESQ = torch.mean(torch.tensor(pesq_all)) STOI = torch.mean(torch.tensor(stoi_all)) print("PESQ: ", PESQ, "STOI: ", STOI) with open(os.path.join(model_path, 'test_scores.txt'), 'w') as fo: fo.write("Avg PESQ: " + str(float(PESQ)) + " Avg STOI: " + str(float(STOI)))
def train_curriculum(clean_path, noisy_path, model_path, num_epochs): device = torch.device("cuda:1") model = Actor() model.cuda() model = model.to(device) model.apply(init_weights) model = nn.DataParallel(model, device_ids=[1, 2]) criterion = SDRLoss() criterion.cuda() losses = [] val_losses = [] best = copy.deepcopy(model.state_dict()) prev_val = 99999 dataset = Data(clean_path, noisy_path, os.path.join(model_path, 'train.tsv')) loader = data.DataLoader(dataset, batch_size=32, shuffle=False, collate_fn=collate_custom) for epoch in range(1, num_epochs + 1): if epoch <= 100: lr = 0.0001 else: lr = lr / 100 optimizer = optim.Adam(model.parameters(), lr=lr) epoch_loss = 0 for i, batch in enumerate(loader): x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) out_r, out_i = model(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() x = x.squeeze() source, targets, preds = inverse(t, y, m, x) loss = criterion(source, targets, preds) optimizer.zero_grad() loss.backward() optimizer.step() loss = loss.detach().cpu().numpy() print("Step:{}/{}".format(i + 1, len(loader)), loss) epoch_loss += loss losses.append(epoch_loss / len(loader)) np.save(os.path.join(model_path, "loss_actor_curr.npy"), np.array(losses)) print('Epoch:{:2} Training loss:{:>4f}'.format( epoch, epoch_loss / len(loader))) if epoch % 5 == 0: ##Validation overall_val_loss = 0 dataset = Data(clean_path, noisy_path, os.path.join(model_path, 'dev.tsv')) loader = data.DataLoader(dataset, batch_size=32, shuffle=True, collate_fn=collate_custom) for batch in loader: x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) m = batch["mask"].to(device) out_r, out_i = model(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() m = m.squeeze() x = x.squeeze() source, targets, preds = inverse(t, y, m, x) loss = criterion(source, targets, preds) overall_val_loss += loss.detach().cpu().numpy() curr_val_loss = overall_val_loss / len(loader) val_losses.append(curr_val_loss) print('Validation loss: ', curr_val_loss) np.save(os.path.join(model_path, 'val_loss_actor_curr.npy'), np.array(val_losses)) if curr_val_loss < prev_val: torch.save(best, os.path.join(model_path, 'actor_best.pth')) prev_val = curr_val_loss torch.save(best, os.path.join(model_path, "actor_last.pth"))
# minimize the cost_function print 'Training Neural Network ...' result_nn_params = op.minimize(fun=nn_cost_function, x0=initial_nn_params, method='CG', jac=True, options={ 'maxiter': max_iter, 'disp': True }, args=(input_layer_size, hidden_layer_size, num_labels, X, y, param_lambda)) # Obtain Theta1 and Theta2 back from nn_params Theta1 = result_nn_params.x[0:hidden_layer_size * (input_layer_size + 1)].reshape( hidden_layer_size, (input_layer_size + 1)) Theta2 = result_nn_params.x[hidden_layer_size * (input_layer_size + 1):].reshape( num_labels, (hidden_layer_size + 1)) # Prediction and its accuracy for training set pred = predict(Theta1, Theta2, X) accuracy = np.mean(((pred == y.T) * 1) * 100) print '\nTraining Set Accuracy: ' + str(accuracy) + '\n' # Prediction and its accuracy for cross validation set pred_cval = predict(Theta1, Theta2, X_cval) accuracy_cval = np.mean(((pred_cval == y_cval.T) * 1) * 100) print 'Cross Validation Set Accuracy: ' + str(accuracy_cval) + '\n'
def train(clean_path, noisy_path, model_path, num_epochs, elite_size=200, population=1000, val_size=300): device = torch.device("cuda:1") model = Actor() model.cuda() model = model.to(device) model.apply(init_weights) model = nn.DataParallel(model, device_ids=[1, 3]) criterion = ES_MSE() criterion.cuda() optimizer = optim.Adam(model.parameters(), lr=0.0001) losses = [] val_losses = [] best = copy.deepcopy(model.state_dict()) prev_val = 99999 for epoch in range(1, num_epochs + 1): dataset = Data(clean_path, noisy_path, population) loader = data.DataLoader(dataset, batch_size=8, shuffle=False, collate_fn=collate_custom) model.train() individual_losses = [] print("Steps:", len(loader)) for i, batch in enumerate(loader): x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) out_r, out_i = model(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() batch_losses = criterion(torch.abs(y), torch.abs(t)) individual_losses.extend(batch_losses) ### Select elite set and backpropagate from N best ### elite_set = sorted(elite_set)[:elite_size] elite_set_loss = torch.sum(torch.tensor([i[1] for i in elite_set ])) / elite_size elite_set_loss.requires_grad = True optimizer.zero_grad() elite_set_loss.backward() optimizer.step() losses.append(elite_set_loss) np.save(os.path.join(model_path, "elite_loss_train.npy"), np.array(losses)) print('Epoch:{:2} Training loss:{:>4f}'.format( epoch, elite_set_loss.detach().cpu().numpy())) if epoch % 5 == 0: ##Validation model.eval() val_loss = 0 print("Validation...") dataset = Data(clean_path, noisy_path, val_size) loader = data.DataLoader(dataset, batch_size=8, shuffle=False, collate_fn=collate_custom) print("Steps:", len(loader)) for i, batch in enumerate(loader): x = batch["noisy"].unsqueeze(1).to(device) t = batch["clean"].unsqueeze(1).to(device) out_r, out_i = model(x) out_r = torch.transpose(out_r, 1, 2) out_i = torch.transpose(out_i, 1, 2) y = predict(x.squeeze(1), (out_r, out_i)) t = t.squeeze() batch_losses = criterion(torch.abs(y), torch.abs(t)) batch_losses = torch.sum( batch_losses / len(batch_losses)).detach().cpu().numpy() val_loss += batch_losses val_losses.append(val_loss / len(loader)) print('Validation loss:', val_loss) np.save(os.path.join(model_path, 'val_loss.npy'), np.array(val_losses)) if val_loss < prev_val: torch.save(best, os.path.join(model_path, 'es_best.pth')) prev_val = val_loss torch.save(best, os.path.join(model_path, "es_last.pth"))
import pandas as pd from modules import train, predict # import train and predict modules #training on weather dataset df = pd.read_csv('weather.nominal.arff.csv') ans = train(df) #get the count of frequencies rand_sample_1 = { 'outlook': 'rainy', 'temperature': 'cool', 'humidity': 'high', 'windy': True } print("First sample for weather data is") print(rand_sample_1) [most_prob_class, max_prob] = predict(ans, rand_sample_1) #predict the class print("Most probable class for sample 1 is \'" + most_prob_class + "\' with maximum probability " + str(max_prob)) rand_sample_2 = { 'outlook': 'overcast', 'temperature': 'mild', 'humidity': 'normal', 'windy': False } print("second sample for weather data is") print(rand_sample_2) [most_prob_2, max_prob_2] = predict(ans, rand_sample_2) print("Most probable class for sample 2 is \'" + most_prob_2 + "\' with maximum probability " + str(max_prob_2)) #training on second dataset (train.csv) df = pd.read_csv('train_data.csv')