def _single_conversion(filename, model, one_hot_emo):
    '''
    THIS WON'T WORK RIGHT NOW, USE THE WORLD CONVERSION LOOP IN MAIN
    
    Call only from __main__ section in this module. Generates sample converted
    into each emotion.

    (str) filename - name.wav file to be converted
    (StarGAN-emo-VC1) model - pretrained model to perform conversion
    (torch.Tensor(long)) one_hot_emo - one hot encoding of emotion to convert to
    '''
    wav, labels = pp.get_wav_and_labels(filenames[5],
                                        config['data']['dataset_dir'])
    wav = np.array(wav, dtype=np.double)

    f0, ap, sp, coded_sp = preprocess_world.cal_mcep(wav)

    coded_sp = coded_sp.T

    coded_sp_torch = torch.Tensor(coded_sp).unsqueeze(0).unsqueeze(0).to(
        device=device)

    fake = model.G(coded_sp_torch, one_hot_emo.unsqueeze(0))
    fake = fake.squeeze()

    print("Sampled size = ", fake.size())

    converted_sp = fake.cpu().detach().numpy()
    converted_sp = np.array(converted_sp, dtype=np.float64)

    sample_length = converted_sp.shape[0]
    if sample_length != ap.shape[0]:
        ap = np.ascontiguousarray(ap[0:sample_length, :], dtype=np.float64)
        f0 = np.ascontiguousarray(f0[0:sample_length], dtype=np.float64)

    f0 = np.ascontiguousarray(f0[20:-20], dtype=np.float64)
    ap = np.ascontiguousarray(ap[20:-20, :], dtype=np.float64)
    converted_sp = np.ascontiguousarray(converted_sp[40:-40, :],
                                        dtype=np.float64)

    coded_sp = np.ascontiguousarray(coded_sp[20:-20, :], dtype=np.float64)

    target = np.argmax(one_hot_emo)
    out_name = filename[:-4] + str(labels[1]) + "to" + target + ".wav"

    audio_utils.save_world_wav([f0, ap, sp, converted_sp],
                               model.name + '_converted', out_name)
Esempio n. 2
0
c1 = np.hstack([c, np.zeros_like(c)])
c2 = np.hstack([np.zeros_like(c), c])

## Data
train_loader = torch.utils.data.DataLoader(datasets.MNIST(
    './data',
    train=True,
    download=False,
    transform=transforms.Compose(
        [transforms.Resize(64), transforms.ToTensor()])),
                                           batch_size=Batch,
                                           shuffle=True)

## main
model_G = model.G().to(device).apply(model.weights_init)
model_D = model.D(c_size).to(device).apply(model.weights_init)
model_Q = model.Q(c_size).to(device).apply(model.weights_init)

opt_D = torch.optim.Adam([{
    'params': model_D.parameters()
}],
                         betas=(0.5, 0.99),
                         lr=Lr_d)
opt_G = torch.optim.Adam([{
    'params': model_G.parameters()
}, {
    'params': model_Q.parameters()
}],
                         lr=Lr_g,
                         betas=(0.5, 0.99))
import torch
import time
from torch.utils.data import DataLoader
import model
import config
import numpy as np

if __name__ == '__main__':
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    # Load data
    #trips = model.TripsData(tripPath = 'E:\\key\\final\\0417_train.csv',labelPath = 'E:\\key\\final\\real0417t_nor.csv')
    trips = model.TripsData(tripPath='E:\\key\\final\\0417_train.csv',
                            labelPath='E:\\key\\final\\real0417_nnor.csv')
    loader = DataLoader(dataset=trips,
                        batch_size=config.BATCH_SIZE,
                        collate_fn=model.CollateFn)
    #Test
    g = model.G(subModelPath='E:\\key\\final\\SUBG\\0403t_train.pth')
    g.load_state_dict(torch.load('E:\\key\\final\\G_D\\0403_g.pth'))
    g.eval()
    embeddings = []
    for i, [data, length, real] in enumerate(loader):
        data = torch.nn.utils.rnn.pack_padded_sequence(data,
                                                       length,
                                                       batch_first=True)
        ansG, embedding = g(data.float())  # batchsize*labeldim 128*242
        embeddings.extend(embedding.detach().numpy())
    embeddings = np.array(embeddings, dtype=float)
    embeddings = embeddings.reshape(-1, config.EMBEDDING_DIM)
    np.savetxt('E:\\key\\final\\RES\\0417.csv', embeddings, delimiter=',')
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
Esempio n. 4
0
parser.add_argument('--nz', type=int, help='size of random vector', default=64)
parser.add_argument('--imsize', type=int, help='size of image', default=128)
parser.add_argument('--gamma', type=float, help='gamma', default=0.5)
parser.add_argument('--lr', type=float, help='learning rate', default=0.0001)
parser.add_argument('--comment', help='comments', default='')

opt = parser.parse_args()
print(opt)
opt.batchSize = opt.batchSize * torch.cuda.device_count()



dataset = ImageFolderSR(root=opt.dataroot, HRsize=opt.imsize, is_crop=False)#, transform=inputTF, target_transform=GTTF)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize, shuffle=True, num_workers=int(opt.workers), pin_memory=True, drop_last=True)
assert dataset
netG = model.G(n=128, h=opt.nz).cuda()
netD = model.D(n=128, h=opt.nz).cuda()

if opt.imsize==128:
    netG = model128.G(n=128, h=opt.nz).cuda()
    netD = model128.D(n=128, h=opt.nz).cuda()

netG = makeParallel(netG)
netD = makeParallel(netD)


optimG = torch.optim.Adam(netG.parameters(), lr = opt.lr, betas=(0.5, 0.999))
optimD = torch.optim.Adam(netD.parameters(), lr = opt.lr, betas=(0.5, 0.999))

if opt.workers==0:
    expname = ''
        coded_sp = coded_sp.T
        coded_sp = torch.Tensor(coded_sp).unsqueeze(0).unsqueeze(0).to(device = device)

        with torch.no_grad():
            # print(emo_targets)
            for i in range (0, emo_targets.size(0)):


                f0 = np.copy(f0_real)
                ap = np.copy(ap_real)
                # coded_sp_temp_copy = np.copy(coded_sp_temp)
                # coded_sp = np.copy(coded_sp)
                f0 = audio_utils.f0_pitch_conversion(f0, (labels[0],labels[1]),
                                                         (i, labels[1]))

                fake = model.G(coded_sp, emo_targets[i].unsqueeze(0))

                # print(f"Converting {f[0:-4]}.")
                # filename_wav =  f[0:-4] + str(int(labels[0].item())) + "to" + \
                #             str(i) + ".wav"
                filename_wav = "stairway" + str(int(labels[0].item())) + "to" + \
                            str(i) + ".wav"

                fake = fake.squeeze()
                # print("Sampled size = ",fake.size())
                # f = fake.data()
                converted_sp = fake.cpu().numpy()
                converted_sp = np.array(converted_sp, dtype = np.float64)

                sample_length = converted_sp.shape[0]
                if sample_length != ap.shape[0]:
Esempio n. 6
0
    def __init__(self,
                 opt=None,
                 train_dt=None,
                 train_dt_warm=None,
                 dis_list=[],
                 val_dt_warm=None):
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")

        self.opt = opt

        self.visualizer = Visualizer(opt)

        num_gpus = torch.cuda.device_count()
        #dis_list[1]
        print(dis_list)
        #torch.cuda.device_count()
        self.rank = dis_list[0]
        print(self.rank)

        #=====START: ADDED FOR DISTRIBUTED======
        if num_gpus > 1:
            #init_distributed(rank, num_gpus, group_name, **dist_config)
            dist_config = dis_list[3]
            init_distributed(dis_list[0], dis_list[1], dis_list[2],
                             **dist_config)
        #=====END:   ADDED FOR DISTRIBUTED======

        if opt.ge_net == "srfeat":
            self.netG = model.G()
        elif opt.ge_net == "carn":
            self.netG = model.G1()
        elif opt.ge_net == "carnm":
            self.netG = model.G2()
        else:
            raise Exception("unknow ")

        self.netD_vgg = model.D(input_c=512, input_width=18)

        self.netD = model.D()

        if opt.vgg_type == "style":
            self.vgg = load_vgg16(opt.vgg_model_path + '/models')
        elif opt.vgg_type == "classify":
            self.vgg = model.vgg19_withoutbn_customefinetune()

        self.vgg.eval()
        for param in self.vgg.parameters():
            param.requires_grad = False

#         for p in self.vgg.parameters():
#             p.requires_grad = False

        init_weights(self.netD, init_type=opt.init)
        init_weights(self.netD_vgg, init_type=opt.init)
        init_weights(self.netG, init_type=opt.init)

        self.vgg = self.vgg.to(self.device)
        self.netD = self.netD.to(self.device)
        self.netD_vgg = self.netD_vgg.to(self.device)
        self.netG = self.netG.to(self.device)

        #=====START: ADDED FOR DISTRIBUTED======
        if num_gpus > 1:
            #self.vgg = apply_gradient_allreduce(self.vgg)
            self.netD_vgg = apply_gradient_allreduce(self.netD_vgg)
            self.netD = apply_gradient_allreduce(self.netD)
            self.netG = apply_gradient_allreduce(self.netG)

        #=====END:   ADDED FOR DISTRIBUTED======

        print(opt)

        self.optim_G= torch. optim.Adam(filter(lambda p: p.requires_grad, self.netG.parameters()),\
         lr=opt.warm_opt.lr, betas=opt.warm_opt.betas, weight_decay=0.0)

        #        self.optim_G= torch.optim.Adam(filter(lambda p: p.requires_grad, self.netG.parameters()),\
        #         lr=opt.gen.lr, betas=opt.gen.betas, weight_decay=0.0)

        if opt.dis.optim == "sgd":
            self.optim_D= torch.optim.SGD( filter(lambda p: p.requires_grad, \
                itertools.chain(self.netD_vgg.parameters(),self.netD.parameters() ) ),\
                lr=opt.dis.lr,
             )
        elif opt.dis.optim == "adam":
            self.optim_D= torch.optim.Adam( filter(lambda p: p.requires_grad, \
                itertools.chain(self.netD_vgg.parameters(),self.netD.parameters() ) ),\
                lr=opt.dis.lr,betas=opt.dis.betas, weight_decay=0.0
             )
        else:
            raise Exception("unknown")

        print("create schedule ")

        lr_sc_G = get_scheduler(self.optim_G, opt.gen)
        lr_sc_D = get_scheduler(self.optim_D, opt.dis)

        self.schedulers = []

        self.schedulers.append(lr_sc_G)
        self.schedulers.append(lr_sc_D)

        # =====START: ADDED FOR DISTRIBUTED======
        train_dt = torch.utils.data.ConcatDataset([train_dt, train_dt_warm])

        train_sampler = DistributedSampler(train_dt) if num_gpus > 1 else None
        val_sampler_warm = DistributedSampler(
            val_dt_warm) if num_gpus > 1 else None
        # =====END:   ADDED FOR DISTRIBUTED======

        kw = {
            "pin_memory": True,
            "num_workers": 8
        } if torch.cuda.is_available() else {}
        dl_c =t_data.DataLoader(train_dt ,batch_size=opt.batch_size,\
             sampler=train_sampler , drop_last=True, **kw )

        dl_val_warm = t_data.DataLoader(
            val_dt_warm,
            batch_size=opt.batch_size
            if not hasattr(opt, "batch_size_warm") else opt.batch_size_warm,
            sampler=val_sampler_warm,
            drop_last=True,
            **kw)

        self.dt_train = dl_c
        self.dt_val_warm = dl_val_warm

        if opt.warm_opt.loss_fn == "mse":
            self.critic_pixel = torch.nn.MSELoss()
        elif opt.warm_opt.loss_fn == "l1":
            self.critic_pixel = torch.nn.L1Loss()
        elif opt.warm_opt.loss_fn == "smooth_l1":
            self.critic_pixel = torch.nn.SmoothL1Loss()
        else:
            raise Exception("unknown")

        self.critic_pixel = self.critic_pixel.to(self.device)

        self.gan_loss = GANLoss(gan_mode=opt.gan_loss_fn).to(self.device)
        print("init ....")

        self.save_dir = os.path.dirname(self.visualizer.log_name)