예제 #1
0
        return output

Net_D=Discriminator()
Net_G = Generator()

if GPU_NUMS > 0:
    Net_D.cuda()
    Net_G.cuda()

###########   LOSS & OPTIMIZER   ##########
optimizerD = t_optim.Adam(Net_D.parameters(),lr=0.0001, betas=(0.5, 0.999))
optimizerG = t_optim.Adam(Net_G.parameters(),lr=0.0001, betas=(0.5, 0.999))

dataset = j_data.Cifar10DataSetForPytorch(train=True, transform=tv.transforms.Compose(
    [
        tv.transforms.ToTensor(),
        # Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
     ]))
train_loader = t.utils.data.DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)

noise = t.randn(100, NOISE_DIM)
noise_var =  t_auto.Variable(noise.cuda() if GPU_NUMS > 0 else noise)

k = 0
proBar = j_bar.ProgressBar(EPOCHS, len(train_loader), "D Loss:%.3f;G Loss:%.3f")
for epoch in range(1, EPOCHS + 1):
    for index, (images,_) in enumerate(train_loader):
        mini_batch = images.shape[0]
        noise = t_auto.Variable(t.FloatTensor(mini_batch, NOISE_DIM, 1, 1).cuda() if GPU_NUMS > 0 else t.FloatTensor(mini_batch, NOISE_DIM, 1, 1))
        real =  t_auto.Variable(t.FloatTensor(mini_batch, IMAGE_CHANNEL, IMAGE_SIZE, IMAGE_SIZE).cuda() if GPU_NUMS > 0 else t.FloatTensor(mini_batch, IMAGE_CHANNEL, IMAGE_SIZE, IMAGE_SIZE))
        label =  t_auto.Variable(t.FloatTensor(1).cuda() if GPU_NUMS > 0 else t.FloatTensor(1))
예제 #2
0
MSE_LOSS = t.nn.MSELoss()
if CONFIG["GPU_NUMS"] > 0:
    NetG = NetG.cuda()
    NetD = NetD.cuda()
    MSE_LOSS = MSE_LOSS.cuda()

optimizerD = t_optim.Adam(NetD.parameters(),
                          lr=CONFIG["LEARNING_RATE"],
                          betas=(0.5, 0.999))
optimizerG = t_optim.Adam(NetG.parameters(),
                          lr=CONFIG["LEARNING_RATE"],
                          betas=(0.5, 0.999))

dataset = j_data.Cifar10DataSetForPytorch(transform=tv.transforms.Compose([
    # tv.transforms.Resize(CONFIG["IMAGE_SIZE"]),
    tv.transforms.ToTensor(),
    # tv.transforms.Normalize([0.5] * 3, [0.5] * 3)
]))
train_loader = t_data.DataLoader(dataset,
                                 batch_size=CONFIG["BATCH_SIZE"],
                                 shuffle=True)

fix_noise = t.randn(100, CONFIG["NOISE_DIM"])
fix_noise_var = t_auto.Variable(
    fix_noise.cuda() if CONFIG["GPU_NUMS"] > 0 else fix_noise)

bar = j_bar.ProgressBar(CONFIG["EPOCH"], len(train_loader),
                        "D Loss:%.3f;G Loss:%.3f")
NetD.train()
for epoch in range(1, CONFIG["EPOCH"] + 1):
    NetG.train()
예제 #3
0
NetD = Discriminator()
NetG = Generator()
NetD.apply(weight_init)
NetG.apply(weight_init)
if CONFIG["GPU_NUMS"] > 0:
    NetG = NetG.cuda()
    NetD = NetD.cuda()

optimizerD = t.optim.RMSprop(NetD.parameters(), lr=CONFIG["LEARNING_RATE"])
optimizerG = t.optim.RMSprop(NetG.parameters(), lr=CONFIG["LEARNING_RATE"])

dataset = j_data.Cifar10DataSetForPytorch(
    root=CONFIG["DATA_PATH"],
    train=True,
    transform=tv.transforms.Compose([
        tv.transforms.Resize(CONFIG["IMAGE_SIZE"]),
        tv.transforms.ToTensor(),
        tv.transforms.Normalize([0.5] * 3, [0.5] * 3)
    ]))
train_loader = t.utils.data.DataLoader(dataset,
                                       batch_size=CONFIG["BATCH_SIZE"],
                                       shuffle=True)

one = t.FloatTensor([1])
mone = -1 * one

one_var = t.autograd.Variable(one.cuda() if CONFIG["GPU_NUMS"] > 0 else one)
mone_var = t.autograd.Variable(mone.cuda() if CONFIG["GPU_NUMS"] > 0 else mone)

fix_noise = t.FloatTensor(100, CONFIG["NOISE_DIM"], 1, 1).normal_(0, 1)
fix_noise_var = t.autograd.Variable(