x-vector-pytorch icon indicating copy to clipboard operation
x-vector-pytorch copied to clipboard

Dataset do not work in 'valid' or 'test' mode

Open Vadim2S opened this issue 2 years ago • 1 comments

In 'train' mode you make exactly [400,257] data frames. In other mode you make [x,257] data frames, where x - length of mag phase spectrogramm.

    if mode=='train':
        randtime = np.random.randint(0, mag_T.shape[1]-spec_len)
        spec_mag = mag_T[:, randtime:randtime+spec_len]
    else:
        spec_mag = mag_T

In validating mode you get list of different-sized tensors and of course - error during

features = torch.from_numpy(np.asarray([torch_tensor.numpy().T for torch_tensor in sample_batched[0]])).float()

TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64

I am wonder about you train\valid approach in dataset. Can you repair project and clarify it?

Vadim2S avatar Jan 30 '23 09:01 Vadim2S

I encounter the same issue with you.There might exist some solutions.During validation and test procedure,the model can't process the data in batch because they don't have the same length. This function can run succeessfully run in my computer,which iteratively run the model.Test module can be adjusted a little based on this function.

def validation(dataloader_val,epoch):
    model.eval()
    with torch.no_grad():
        val_loss_list=[]
        full_preds=[]
        full_gts=[]
        for i_batch, sample_batched in enumerate(dataloader_val):
            # Unpack tensor and label directly from sample_batched
            for idx in range(len(sample_batched[0])):
                features=sample_batched[0][idx].float().to(device).T.unsqueeze(0)
                labels= sample_batched[1][idx].type(torch.LongTensor)
                pred_logits,x_vec = model(features)
                #### CE loss
                loss = loss_fun(pred_logits,labels)
                val_loss_list.append(loss.item())
                #train_acc_list.append(accuracy)
                predictions = np.argmax(pred_logits.detach().cpu().numpy(),axis=1)
                for pred in predictions:
                    full_preds.append(pred)
                for lab in labels.detach().cpu().numpy():
                    full_gts.append(lab)                
        mean_acc = accuracy_score(full_gts,full_preds)
        mean_loss = np.mean(np.asarray(val_loss_list))
        print('Total validation loss {} and Validation accuracy {} after {} epochs'.format(mean_loss,mean_acc,epoch))
        
        model_save_path = os.path.join('save_model', 'best_check_point_'+str(epoch)+'_'+str(mean_loss))
        state_dict = {'model': model.state_dict(),'optimizer': optimizer.state_dict(),'epoch': epoch}
        torch.save(state_dict, model_save_path)

TingyanCN avatar Dec 08 '23 11:12 TingyanCN