Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kwotsin committed Aug 10, 2020
1 parent dc1f210 commit 1a54fb7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 64 deletions.
75 changes: 36 additions & 39 deletions examples/sngan_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import torch_mimicry as mmc
from torch_mimicry.nets import sngan


if __name__ == "__main__":
# Data handling objects
device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu")
dataset = mmc.datasets.load_dataset(root='./datasets', name='cifar10')
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=64, shuffle=True, num_workers=4)
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=64,
shuffle=True,
num_workers=4)

# Define models and optimizers
netG = sngan.SNGANGenerator32().to(device)
Expand All @@ -18,46 +19,42 @@
optG = optim.Adam(netG.parameters(), 2e-4, betas=(0.0, 0.9))

# Start training
trainer = mmc.training.Trainer(
netD=netD,
netG=netG,
optD=optD,
optG=optG,
n_dis=5,
num_steps=100000,
lr_decay='linear',
dataloader=dataloader,
log_dir='./log/example',
device=device)
trainer = mmc.training.Trainer(netD=netD,
netG=netG,
optD=optD,
optG=optG,
n_dis=5,
num_steps=100000,
lr_decay='linear',
dataloader=dataloader,
log_dir='./log/example',
device=device)
trainer.train()

# Evaluate fid
mmc.metrics.evaluate(
metric='fid',
log_dir='./log/example',
netG=netG,
dataset_name='cifar10',
num_real_samples=50000,
num_fake_samples=50000,
evaluate_step=100000,
device=device)
mmc.metrics.evaluate(metric='fid',
log_dir='./log/example',
netG=netG,
dataset_name='cifar10',
num_real_samples=50000,
num_fake_samples=50000,
evaluate_step=100000,
device=device)

# Evaluate kid
mmc.metrics.evaluate(
metric='kid',
log_dir='./log/example',
netG=netG,
dataset_name='cifar10',
num_subsets=50,
subset_size=1000,
evaluate_step=100000,
device=device)
mmc.metrics.evaluate(metric='kid',
log_dir='./log/example',
netG=netG,
dataset_name='cifar10',
num_subsets=50,
subset_size=1000,
evaluate_step=100000,
device=device)

# Evaluate inception score
mmc.metrics.evaluate(
metric='inception_score',
log_dir='./log/example',
netG=netG,
num_samples=50000,
evaluate_step=100000,
device=device)
mmc.metrics.evaluate(metric='inception_score',
log_dir='./log/example',
netG=netG,
num_samples=50000,
evaluate_step=100000,
device=device)
48 changes: 23 additions & 25 deletions examples/ssgan_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from torch_mimicry.modules import SNLinear
from torch_mimicry.modules import GBlock, DBlock, DBlockOptimized


#######################
# Models
#######################
Expand Down Expand Up @@ -118,8 +119,6 @@ def __init__(self, ndf=128, loss_type='hinge', **kwargs):
self.l_y = SNLinear(self.ndf, self.num_classes)
nn.init.xavier_uniform_(self.l_y.weight.data, 1.0)



def forward(self, x):
"""
Feedforwards a batch of real/fake images and produces a batch of GAN logits,
Expand All @@ -141,7 +140,6 @@ def forward(self, x):

return output, output_classes


def _rot_tensor(self, image, deg):
"""
Rotation for pytorch tensors using rotation matrix. Takes in a tensor of (C, H, W shape).
Expand Down Expand Up @@ -216,7 +214,7 @@ def train_step(self,
netG,
optD,
log_data,
device=None,
device=None,
global_step=None,
**kwargs):
"""
Expand Down Expand Up @@ -272,8 +270,10 @@ def train_step(self,
# Data handling objects
device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu")
dataset = mmc.datasets.load_dataset(root='./datasets', name='cifar10')
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=64, shuffle=True, num_workers=4)
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=64,
shuffle=True,
num_workers=4)

# Define models and optimizers
netG = SSGANGenerator().to(device)
Expand All @@ -282,28 +282,26 @@ def train_step(self,
optG = optim.Adam(netG.parameters(), 2e-4, betas=(0.0, 0.9))

# Start training
trainer = mmc.training.Trainer(
netD=netD,
netG=netG,
optD=optD,
optG=optG,
n_dis=2,
num_steps=100000,
dataloader=dataloader,
log_dir=log_dir,
device=device)
trainer = mmc.training.Trainer(netD=netD,
netG=netG,
optD=optD,
optG=optG,
n_dis=2,
num_steps=100000,
dataloader=dataloader,
log_dir=log_dir,
device=device)
trainer.train()

##########################
# Evaluation
##########################
# Evaluate fid
mmc.metrics.evaluate(
metric='fid',
log_dir=log_dir,
netG=netG,
dataset_name='cifar10',
num_real_samples=10000,
num_fake_samples=10000,
evaluate_step=100000,
device=device)
mmc.metrics.evaluate(metric='fid',
log_dir=log_dir,
netG=netG,
dataset_name='cifar10',
num_real_samples=10000,
num_fake_samples=10000,
evaluate_step=100000,
device=device)

0 comments on commit 1a54fb7

Please sign in to comment.