def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): avesdr = 0 numSongs = 0 sdrmedian = np.zeros(50) for iloader, xtrain, ytrain in loadtest: iloader=iloader.item() listofpred0 = [] cnt,aveloss=0,0 for ind in range(0, xtrain.shape[-1] - sampleSize, sampleSize): if (xtrain[0, 0, ind:ind + sampleSize].shape[0] < (sampleSize)): break output = model(xtrain[:, :,ind:ind + sampleSize].to(device)) listofpred0.append(output.reshape(-1).cpu().numpy()) loss = criterion(output, (ytrain[:, :,ind:ind + sampleSize].to(device))) cnt+=1 aveloss += float(loss) aveloss /= cnt print('loss for test:{},num{},epoch{}'.format(aveloss, iloader,epoch)) ans0 = mu_law_decode(np.concatenate(listofpred0)) if(iloader >= 150): sdr = bss_eval_sources(mu_law_decode(ytrain[0,0,:ans0.shape[0]].cpu().numpy()), ans0) avesdr += sdr[0][0] sdrmedian[iloader-150] = sdr[0][0] #print('each ele of median',sdrmedian[iloader-150],iloader-150) numSongs += 1 if(iloader > 160):continue if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') sf.write(savemusic.format(iloader), ans0, sample_rate) print('test stored done', np.round(time.time() - start_time)) print('sdr mean:', avesdr / numSongs) print('sdr median:', np.median(sdrmedian))
def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): for iloader, y, queue in loadval: iloader = iloader.item() Fs = 16000 f = 500 sample = 16000 * 7 x = np.arange(sample) ytrain = np.sin(2 * np.pi * f * x / Fs) ytrain = mu_law_encode(ytrain) y = torch.from_numpy(ytrain.reshape(1, -1)).type(torch.LongTensor) #music = model.infer(queue,device) music = model.slowInfer(queue, device, y, 16000 * 0.001) music = model.slowInfer(queue, device, y, 16000 * 0.01) music = model.slowInfer(queue, device, y, 16000 * 0.1) #music = model.slowInfer(queue, device, y, 16000 * 1) music = model.slowInfer(queue, device, y, 16000 * 3) print(music[:100]) ans0 = mu_law_decode(music.numpy().astype('int')) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') write(savemusic.format(iloader), sample_rate, ans0) print('test stored done', np.round(time.time() - start_time))
def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): for iloader, xtrain, ytrain in loadval: iloader = iloader.item() listofpred0 = [] cnt, aveloss = 0, 0 for ind in range(pad, xtrain.shape[-1] - pad - sampleSize, sampleSize): output0 = model(xtrain[:, :, ind - pad:ind + sampleSize + pad].to(device)) loss = criterion(output0, (ytrain[:, ind:ind + sampleSize].to(device))) pred0 = output0.max(1, keepdim=True)[1].cpu().numpy().reshape(-1) listofpred0.append(pred0) cnt += 1 aveloss += loss.item() aveloss /= cnt print('loss for validation:{},num{},epoch{}'.format( aveloss, iloader, epoch)) #if(aveloss < maxloss[iloader]): maxloss[iloader] = aveloss ans0 = mu_law_decode(np.concatenate(listofpred0)) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') sf.write(savemusic.format(iloader), ans0, sample_rate) print('test stored done', np.round(time.time() - start_time))
def test(iloader, xtrain): # testing data model.eval() start_time = time.time() with torch.no_grad(): listofpred0, listofpred1 = [], [] for ind in range(pad, xtrain.shape[-1] - pad, sampleSize): output0, output1 = model(xtrain[:, :, ind - pad:ind + sampleSize + pad].to(device)) pred0 = output0.max(1, keepdim=True)[1].cpu().numpy().reshape(-1) pred1 = output1.max(1, keepdim=True)[1].cpu().numpy().reshape(-1) listofpred0.append(pred0) listofpred1.append(pred1) ans0 = mu_law_decode(np.concatenate(listofpred0)) ans1 = mu_law_decode(np.concatenate(listofpred1)) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') sf.write(savemusic0.format(iloader), ans0, sample_rate) sf.write(savemusic1.format(iloader), ans1, sample_rate) print('test stored done', np.round(time.time() - start_time))
def test(xtrain,iloader): # testing data model.eval() start_time = time.time() with torch.no_grad(): #for iloader, (xtest, _) in enumerate(loadval): listofpred = [] for ind in range(pad, xtrain.shape[-1] - pad, sampleSize): output = model(xtrain[:, :, ind - pad:ind + sampleSize + pad].to(device)) pred = output.max(1, keepdim=True)[1].cpu().numpy().reshape(-1) listofpred.append(pred) ans = mu_law_decode(np.concatenate(listofpred)) sf.write(savemusic.format(iloader), ans, sample_rate) print('test stored done', time.time() - start_time)
def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): for iloader, xtrain in loadtest: iloader = iloader.item() print(iloader) listofpred0 = [] for ind in range(0, xtrain.shape[-1] - sampleSize, sampleSize): output = model(xtrain[:, :, ind:ind + sampleSize].to(device)) listofpred0.append(output.reshape(-1)) ans0 = mu_law_decode(np.concatenate(listofpred0)) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') sf.write(savemusic.format(iloader), ans0, sample_rate) print('test stored done', np.round(time.time() - start_time))
def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): for iloader, y in loadval: iloader = iloader.item() #music = model.infer(queue) #music = model.slowInfer(queue, y, 16000 * 0.001) #music = model.slowInfer(queue, y, 16000 * 0.01) #music = model.slowInfer(queue, y, 16000 * 0.1) music = model.slowInfer(None, y, 16000 * 3) print(music[:1000]) ans0 = mu_law_decode(music.numpy().astype('int')) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') write(savemusic.format(epoch * 6 + iloader), sample_rate, ans0) print('test stored done', np.round(time.time() - start_time))
def test(epoch): # testing data model.eval() start_time = time.time() with torch.no_grad(): for iloader, xtrain, ytrain in loadtest: iloader = iloader.item() listofpred0 = [] cnt, aveloss = 0, 0 for ind in range(0, xtrain.shape[-1] - sampleSize, sampleSize): output = model(xtrain[:, :, ind:ind + sampleSize].to(device)) listofpred0.append(output.reshape(-1)) loss = criterion( output, (ytrain[:, :, ind:ind + sampleSize].to(device))) cnt += 1 aveloss += float(loss) aveloss /= cnt print('loss for test:{},num{},epoch{}'.format( aveloss, iloader, epoch)) ans0 = mu_law_decode(np.concatenate(listofpred0)) if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/') sf.write(savemusic.format(iloader), ans0, sample_rate) print('test stored done', np.round(time.time() - start_time))