def __getitem__(self, index): 'Generates one sample of data' np.random.seed() namex = self.listx[index] x, _ = sf.read(self.rootx + str(namex) + '/0.wav') assert (_ == 16000) y, _ = sf.read(self.rootx + str(namex) + '/1.wav') assert (_ == 16000) assert (x.shape == y.shape) x = mu_law_encode(x) y = mu_law_encode(y) if (x.shape[0] <= sampleSize): while (x.shape[0] <= sampleSize): x = np.concatenate((x, x)) y = np.concatenate((y, y)) assert (x.shape == y.shape) start = np.random.randint(0, x.shape[0] - sampleSize + 1, size=1)[0] x = x[start:start + sampleSize] y = y[start:start + sampleSize] x = torch.from_numpy(x.reshape(1, -1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex, x, y
def __getitem__(self, index): 'Generates one sample of data' np.random.seed() namex = self.listx[index] h5f = h5py.File('ccmixter3/' + str(namex) + '.h5', 'r') x, y = h5f['x'][:], h5f['y'][:] #xstd = x.std() #x /= xstd x = mu_law_encode(x) if(self.quan):y = quan_mu_law_encode(y) else:y = mu_law_encode(y) start = np.random.randint(0, x.shape[0] - sampleSize - 1, size=1)[0] x = x[start:start + sampleSize] y = y[start:start + sampleSize] x = torch.from_numpy(x.reshape(1,-1)).type(torch.float32) if (self.quan): y = torch.from_numpy(y.reshape(-1)).long() else: y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex,x,y
def __getitem__(self, index): 'Generates one sample of data' np.random.seed() namex = self.listx[index] h5f = h5py.File('ccmixter3/' + str(namex) + '.h5', 'r') x, y = h5f['x'][:], h5f['y'][:] x = mu_law_encode(x) y = mu_law_encode(y) if (x.shape[0] > sampleSize): start = np.random.randint(0, x.shape[0] - sampleSize + 1, size=1)[0] x = x[start:start + sampleSize] y = y[start:start + sampleSize] else: x = np.pad(x, (0, sampleSize - x.shape[0]), 'constant', constant_values=(0)) y = np.pad(y, (0, sampleSize - y.shape[0]), 'constant', constant_values=(0)) print('xy', x.shape, y.shape, namex) x = torch.from_numpy(x.reshape(1, -1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex,x,y
def __getitem__(self, index): #print('dataset',np.random.get_state()[1][0]) np.random.seed() namex = self.listx[index] h5f = h5py.File(self.rootx + str(namex) + '.h5', 'r') x, y, z = h5f['x'][:], h5f['y'][:],h5f['z'][:] h5f.close() factor0 = np.random.uniform(low=0.83, high=1.0) factor1 = np.random.uniform(low=0.83, high=1.0) #print(factor0,factor1) z = z*factor0 y = y*factor1 x = (y + z) x = mu_law_encode(x) y = mu_law_encode(y) if (x.shape[0] > sampleSize): start = np.random.randint(0, x.shape[0] - sampleSize + 1, size=1)[0] x = x[start:start + sampleSize] y = y[start:start + sampleSize] else: x = np.pad(x, (0, sampleSize - x.shape[0]), 'constant', constant_values=(0)) y = np.pad(y, (0, sampleSize - y.shape[0]), 'constant', constant_values=(0)) print('xy', x.shape, y.shape, namex) x = torch.from_numpy(x.reshape(1,-1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex,x, y
def __getitem__(self, index): 'Generates one sample of data' namex = self.listx[index] x, _ = sf.read(self.rootx + str(namex) + '/0.wav') assert (_ == 16000) y, _ = sf.read(self.rootx + str(namex) + '/1.wav') assert (_ == 16000) assert (x.shape == y.shape) x = mu_law_encode(x) y = mu_law_encode(y) x = torch.from_numpy(x.reshape(1, -1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex, x, y
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 __getitem__(self, index): 'Generates one sample of data' namex = self.listx[index] y, _ = sf.read('piano/piano{}.wav'.format(namex)) #factor1 = np.random.uniform(low=0.83, high=1.0) #y = y*factor1 if normalized: ymean = y.mean() ystd = y.std() y = (y - ymean) / ystd y = mu_law_encode(y) #y = torch.from_numpy(y.reshape(-1)).type(torch.LongTensor) print('test piano{}.wav,train audio shape{},rate{}'.format( namex, y.shape, _)) y = torch.from_numpy(y.reshape(-1)[int(16000 * 1):]).type( torch.LongTensor) print("first second as seed") #y = torch.randint(0, 256, (100000,)).type(torch.LongTensor) #print("random init") #y = F.pad(y, (self.pad, self.pad), mode='constant', value=127) return namex, y
def __getitem__(self, index): 'Generates one sample of data' namex = self.listx[index] h5f = h5py.File('ccmixter3/' + str(namex) + '.h5', 'r') x, y = h5f['x'][:], h5f['y'][:] x = mu_law_encode(x) y = mu_law_encode(y) x = torch.from_numpy(x.reshape(1, -1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex,x,y
def __getitem__(self, index): 'Generates one sample of data' namex = self.listx[index] h5f = h5py.File('ccmixter3/' + str(namex) + '.h5', 'r') x, y = h5f['x'][:], h5f['y'][:] #xstd = x.std() #x /= xstd x = mu_law_encode(x) if (self.quan): y = quan_mu_law_encode(y) else: y = mu_law_encode(y) x = torch.from_numpy(x.reshape(1,-1)).type(torch.float32) if (self.quan): y = torch.from_numpy(y.reshape(-1)).long() else: y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex,x,y
def __getitem__(self, index): #print('dataset',np.random.get_state()[1][0]) np.random.seed() namex = self.listx[index] x, _ = sf.read(self.rootx + 'x/' + str(namex) + '.wav') assert (_ == 16000) y, _ = sf.read(self.rootx + 'y/' + str(namex) + '.wav') assert (_ == 16000) z, _ = sf.read(self.rootx + 'z/' + str(namex) + '.wav') assert (_ == 16000) assert (x.shape == y.shape) factor0 = np.random.uniform(low=0.83, high=1.0) factor1 = np.random.uniform(low=0.83, high=1.0) #print(factor0,factor1) z = z * factor0 y = y * factor1 x = (y + z) x = mu_law_encode(x) y = mu_law_encode(y) if (x.shape[0] <= sampleSize): while (x.shape[0] <= sampleSize): x = np.concatenate((x, x)) y = np.concatenate((y, y)) assert (x.shape == y.shape) #print('xy', x.shape, y.shape, namex) start = np.random.randint(0, x.shape[0] - sampleSize + 1, size=1)[0] x = x[start:start + sampleSize] y = y[start:start + sampleSize] x = torch.from_numpy(x.reshape(1, -1)).type(torch.float32) y = torch.from_numpy(y.reshape(1, -1)).type(torch.float32) return namex, x, y
def __getitem__(self, index): np.random.seed() namex = self.listx[index] h5f = h5py.File(self.rootx + str(namex) + '.h5', 'r') x = h5f['x'][:] factor1 = np.random.uniform(low=0.83, high=1.0) x = x * factor1 x = mu_law_encode(x) x = torch.from_numpy(x.reshape(-1)).type(torch.LongTensor) #x = F.pad(y, (self.pad, self.pad), mode='constant', value=127) return namex, x.type(torch.LongTensor)
def __getitem__(self, index): 'Generates one sample of data' namex = self.listx[index] h5f = h5py.File(self.rootx + str(namex) + '.h5', 'r') y = h5f['y'][:] queue = [] for i in self.dilations1: queue.append( torch.normal(torch.zeros(64, i), std=1).to(self.device)) #queue.append(torch.zeros((64,i), dtype=torch.float32).to(self.device)) y = mu_law_encode(y) y = torch.from_numpy(y.reshape(-1)).type(torch.LongTensor) #y = (torch.randint(0, 255, (self.field)).long()) return namex, y, queue
def __getitem__(self, index): np.random.seed() namex = self.listx[index] y, _ = sf.read('piano/piano{}.wav'.format(namex)) print('train piano{}.wav,train audio shape{},rate{}'.format( namex, y.shape, _)) #factor1 = np.random.uniform(low=0.83, high=1.0) #y = y*factor1 if normalized: ymean = y.mean() ystd = y.std() y = (y - ymean) / ystd y = mu_law_encode(y) y = torch.from_numpy(y.reshape(-1)).type(torch.LongTensor) #y = F.pad(y, (self.pad, self.pad), mode='constant', value=127) return namex, y
def train(epoch): for iloader, ytrain in loadtr: Fs = 16000 f = 4000 sample = 16000 * 4 x = np.arange(sample) ytrain = np.sin(2 * np.pi * f * x / Fs) ytrain = mu_law_encode(ytrain) ytrain = torch.from_numpy(ytrain.reshape(1, -1)).type(torch.LongTensor) iloader = int(iloader) #startx = np.random.randint(0,sampleSize) startx = 0 idx = np.arange(startx + field, ytrain.shape[-1] - field - sampleSize, sampleSize) np.random.shuffle(idx) lens = 30 #lens = 10 idx = idx[:lens] cnt, aveloss, aveacc = 0, 0, 0 start_time = time.time() model.train() for i, ind in enumerate(idx): optimizer.zero_grad() target0 = ytrain[:, ind - field:ind + sampleSize - 1].to(device) target1 = ytrain[:, ind:ind + sampleSize].to(device) output = model(target0) a = output.max(dim=1, keepdim=True)[1].view(-1) b = target1.view(-1) assert (a.shape[0] == b.shape[0]) aveacc += float( float(torch.sum(a.long() == b.long())) / float(a.shape[0])) loss = criterion(output, target1) loss.backward() optimizer.step() aveloss += float(loss) if (float(loss) > 10): print(float(loss)) cnt += 1 lossrecord.append(float(loss)) global sampleCnt sampleCnt += 1 if sampleCnt % 10000 == 0 and sampleCnt > 0: for param in optimizer.param_groups: param['lr'] *= 0.98 print( 'loss for train:{:.4f},acc:{:.4f},num{},epoch{},({:.3f} sec/step)'. format(aveloss / cnt, aveacc / cnt, iloader, epoch, time.time() - start_time)) if not os.path.exists('lossRecord/'): os.makedirs('lossRecord/') with open("lossRecord/" + lossname, "w") as f: for s in lossrecord: f.write(str(s) + "\n") if not os.path.exists('model/'): os.makedirs('model/') state = { 'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict() } if (epoch % 4 == 0 and epoch != 0): torch.save(state, resumefile) print('write finish') print('epoch finished')