-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a02ae4
commit 914cf13
Showing
7 changed files
with
348 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import torch | ||
import torch.nn as nn | ||
import numpy as np | ||
|
||
def get_label(bs, device): | ||
label_r = torch.full((bs, ), 1, device = device) | ||
label_f = torch.full((bs, ), 0, device = device) | ||
return label_r, label_f | ||
|
||
class SGAN(nn.Module): | ||
def __init__(self, device): | ||
super(SGAN, self).__init__() | ||
self.criterion = nn.BCELoss() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, label_f = get_label(bs, self.device) | ||
return self.criterion(c_xr, label_r) + self.criterion(c_xf, label_f) | ||
|
||
def g_loss(self, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, _ = get_label(bs, self.device) | ||
return self.criterion(c_xf, label_r) | ||
|
||
class LSGAN(nn.Module): | ||
def __init__(self, device): | ||
super(LSGAN, self).__init__() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, label_f = get_label(bs, self.device) | ||
return torch.mean((c_xr - label_r) ** 2) + torch.mean((c_xf - label_f) ** 2) | ||
|
||
def g_loss(self, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, _ = get_label(bs, self.device) | ||
return torch.mean((c_xf - label_r) ** 2) | ||
|
||
class HINGEGAN(nn.Module): | ||
def __init__(self, device): | ||
super(HINGEGAN, self).__init__() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
return torch.mean(torch.nn.ReLU()(1-c_xr)) + torch.mean(torch.nn.ReLU()(1+c_xf)) | ||
|
||
def g_loss(self, c_xf): | ||
return -torch.mean(c_xf) | ||
|
||
class WGAN(nn.Module): | ||
def __init__(self, device): | ||
super(WGAN, self).__init__() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
return -torch.mean(c_xr) + torch.mean(c_xf) | ||
|
||
def g_loss(self, c_xf): | ||
return -torch.mean(c_xf) | ||
|
||
class RASGAN(nn.Module): | ||
def __init__(self, device): | ||
super(RASGAN, self).__init__() | ||
self.device = device | ||
self.criterion = nn.BCEWithLogitsLoss() | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, label_f = get_label(bs, self.device) | ||
return (self.criterion(c_xr - torch.mean(c_xf), label_r) + self.criterion(c_xf - torch.mean(c_xr), label_f)) / 2.0 | ||
|
||
def g_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, label_f = get_label(bs, self.device) | ||
return (self.criterion(c_xr - torch.mean(c_xf), label_f) + self.criterion(c_xf - torch.mean(c_xr), label_r)) / 2.0 | ||
|
||
class RALSGAN(nn.Module): | ||
def __init__(self, device): | ||
super(RALSGAN, self).__init__() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, _ = get_label(bs, self.device) | ||
return (torch.mean((c_xr - torch.mean(c_xf) - label_r)**2) + torch.mean((c_xf - torch.mean(c_xr) + label_r)**2)) / 2.0 | ||
|
||
def g_loss(self, c_xr, c_xf): | ||
bs = c_xf.shape[0] | ||
label_r, _ = get_label(bs, self.device) | ||
return (torch.mean((c_xf - torch.mean(c_xr) - label_r)**2) + torch.mean((c_xr - torch.mean(c_xf) + label_r)**2)) / 2.0 | ||
|
||
class RAHINGEGAN(nn.Module): | ||
def __init__(self, device): | ||
super(RAHINGEGAN, self).__init__() | ||
self.device = device | ||
|
||
def d_loss(self, c_xr, c_xf): | ||
return (torch.mean(torch.nn.ReLU()(1-(c_xr-torch.mean(c_xf)))) + torch.mean(torch.nn.ReLU()(1+(c_xf-torch.mean(c_xr))))) / 2.0 | ||
|
||
def g_loss(self, c_xr, c_xf): | ||
return (torch.mean(torch.nn.ReLU()(1-(c_xf-torch.mean(c_xr)))) + torch.mean(torch.nn.ReLU()(1+(c_xr-torch.mean(c_xf))))) / 2.0 | ||
|
||
class QPGAN(nn.Module): | ||
def __init__(self, device, norm_type = 'L1'): | ||
super(QPGAN, self).__init__() | ||
self.device = device | ||
self.norm_type = norm_type | ||
|
||
def d_loss(self, c_xr, c_xf, real_images, fake_images): | ||
if(self.norm_type == 'L1'): | ||
denominator = (real_images - fake_images).abs().mean() * 10 * 2 | ||
if(self.norm_type == 'L2'): | ||
denominator = (real_images - fake_images).mean().sqrt() * 10 * 2 | ||
|
||
errD_1 = torch.mean(c_xr) - torch.mean(c_xf) | ||
errD_2 = (errD_1 ** 2) / denominator | ||
return errD_2 - errD_1 | ||
|
||
def g_loss(self, c_xr, c_xf): | ||
return torch.mean(c_xr) - torch.mean(c_xf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import torch | ||
import torch.nn as nn | ||
from dataset import Dataset | ||
from architectures.architectures_pggan import PGGAN_D, PGGAN_G | ||
from trainers_advanced.trainer import Trainer | ||
from utils import save, load | ||
|
||
dir_name = 'data/celeba' | ||
basic_types = None | ||
|
||
lr_D, lr_G = 0.001, 0.001 | ||
sz, nc, nz = 128, 3, 256 | ||
use_sigmoid = False | ||
|
||
data = Dataset('data/celeba') | ||
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') | ||
netD = PGGAN_D(sz, nc, use_sigmoid, False, True).to(device) | ||
netG = PGGAN_G(sz, nz, nc, True, True).to(device) | ||
|
||
trainer = Trainer('SGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('LSGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('HINGEGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('WGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = 0.01, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('WGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = 10, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
|
||
trainer = Trainer('RASGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('RALSGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
trainer = Trainer('RAHINGEGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
|
||
trainer = Trainer('QPGAN', netD, netG, device, data, lr_D = lr_D, lr_G = lr_G, resample = True, weight_clip = None, use_gradient_penalty = False, drift = 0.001, loss_interval = 150, image_interval = 300, save_img_dir = 'saved_imges') | ||
|
||
trainer.train([4, 8, 8, 8, 8, 8], [0.5, 0.5, 0.5, 0.5, 0.5], [16, 16, 16, 16, 16, 16]) | ||
save('saved/cur_state.state', netD, netG, trainer.optimizerD, trainer.optimizerG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.