generative-models
Collection of generative models, e.g. GAN, VAE in Pytorch and Tensorflow.
Top Related Projects
Models and examples built with TensorFlow
A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc.
PyTorch implementations of Generative Adversarial Networks.
Image-to-Image Translation in PyTorch
CLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image
PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
Quick Overview
The wiseodd/generative-models repository is a collection of PyTorch implementations of various generative models in machine learning. It includes implementations of popular models such as Variational Autoencoders (VAE), Generative Adversarial Networks (GAN), and their variants. The repository serves as a valuable resource for researchers and practitioners interested in generative modeling.
Pros
- Comprehensive collection of generative models in one place
- Well-organized code structure with separate implementations for each model
- Includes both PyTorch and TensorFlow implementations for some models
- Provides clear and concise implementations, making it easier to understand the models
Cons
- Some implementations may not be up-to-date with the latest advancements in the field
- Limited documentation and explanations for each model
- Lack of extensive hyperparameter tuning or optimization techniques
- May require additional dependencies or setup for certain models
Code Examples
- Loading and preprocessing data:
import torch
from torchvision import datasets, transforms
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
mnist = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(mnist, batch_size=64, shuffle=True)
- Defining a simple VAE model:
import torch.nn as nn
class VAE(nn.Module):
def __init__(self, input_dim, latent_dim):
super(VAE, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_dim, 512),
nn.ReLU(),
nn.Linear(512, latent_dim * 2)
)
self.decoder = nn.Sequential(
nn.Linear(latent_dim, 512),
nn.ReLU(),
nn.Linear(512, input_dim),
nn.Sigmoid()
)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
h = self.encoder(x.view(-1, 784))
mu, logvar = h.chunk(2, dim=1)
z = self.reparameterize(mu, logvar)
return self.decoder(z), mu, logvar
- Training loop for a GAN:
import torch.optim as optim
def train_gan(generator, discriminator, dataloader, num_epochs):
g_optimizer = optim.Adam(generator.parameters(), lr=0.0002)
d_optimizer = optim.Adam(discriminator.parameters(), lr=0.0002)
criterion = nn.BCELoss()
for epoch in range(num_epochs):
for real_images, _ in dataloader:
batch_size = real_images.size(0)
real_labels = torch.ones(batch_size, 1)
fake_labels = torch.zeros(batch_size, 1)
# Train Discriminator
d_optimizer.zero_grad()
outputs = discriminator(real_images)
d_loss_real = criterion(outputs, real_labels)
z = torch.randn(batch_size, 100)
fake_images = generator(z)
outputs = discriminator(fake_images.detach())
d_loss_fake = criterion(outputs, fake_labels)
d_loss = d_loss_real + d_loss_fake
d_loss.backward()
d_optimizer.step()
# Train Generator
g_optimizer.zero_grad()
outputs = discriminator(fake_images)
g_loss = criterion(outputs, real_labels)
g_loss.backward()
g_optimizer.step()
print(f"Epoch [{epoch+1}/{num_epochs}], D Loss: {d_loss
Competitor Comparisons
Models and examples built with TensorFlow
Pros of models
- Comprehensive collection of official TensorFlow models and examples
- Regularly updated with new models and features
- Extensive documentation and community support
Cons of models
- Large repository size, potentially overwhelming for beginners
- Focuses primarily on TensorFlow, limiting flexibility for other frameworks
- May require more setup and dependencies compared to simpler repositories
Code comparison
generative-models:
def sample_Z(m, n):
return np.random.uniform(-1., 1., size=[m, n])
def generator(Z):
h1 = tf.nn.relu(tf.matmul(Z, G_W1) + G_b1)
return tf.matmul(h1, G_W2) + G_b2
models:
def create_model(num_classes):
model = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes, activation='softmax')
])
return model
Summary
generative-models is a focused repository for implementing various generative models, while models is a comprehensive collection of TensorFlow models and examples. generative-models may be more suitable for those specifically interested in generative models, while models offers a broader range of machine learning applications but with a steeper learning curve.
A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc.
Pros of examples
- Officially maintained by PyTorch, ensuring up-to-date and optimized implementations
- Covers a wide range of machine learning tasks beyond generative models
- Includes more advanced and complex examples, suitable for experienced practitioners
Cons of examples
- Less focused on generative models specifically
- May be more challenging for beginners to understand and modify
- Examples are not as neatly organized by model type
Code Comparison
examples (DCGAN):
netG = Generator(ngpu).to(device)
netG.apply(weights_init)
if opt.netG != '':
netG.load_state_dict(torch.load(opt.netG))
print(netG)
generative-models (DCGAN):
generator = Generator()
discriminator = Discriminator()
g_optimizer = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999))
d_optimizer = optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999))
The examples repository provides more detailed initialization and device handling, while generative-models focuses on a simpler, more readable implementation of model and optimizer setup.
PyTorch implementations of Generative Adversarial Networks.
Pros of PyTorch-GAN
- Focuses specifically on GANs, offering a wider variety of GAN implementations
- Uses PyTorch, which is more popular and actively maintained
- Includes more recent GAN architectures and techniques
Cons of PyTorch-GAN
- Limited to GANs, while generative-models covers a broader range of generative models
- May be more complex for beginners due to its focus on advanced GAN techniques
- Less comprehensive documentation compared to generative-models
Code Comparison
generative-models (TensorFlow):
def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
return tf.random_normal(shape=size, stddev=xavier_stddev)
X = tf.placeholder(tf.float32, shape=[None, X_dim])
D_W1 = tf.Variable(xavier_init([X_dim, h_dim]))
PyTorch-GAN:
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find("BatchNorm2d") != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
model.apply(weights_init_normal)
Image-to-Image Translation in PyTorch
Pros of pytorch-CycleGAN-and-pix2pix
- Focuses on specific image-to-image translation tasks, providing specialized implementations
- Offers pre-trained models and datasets for quick experimentation
- Includes a comprehensive training and testing pipeline
Cons of pytorch-CycleGAN-and-pix2pix
- Limited to CycleGAN and pix2pix architectures, less diverse than generative-models
- May be more complex for beginners due to its specialized nature
- Requires more computational resources for training and inference
Code Comparison
generative-models (TensorFlow):
def generator(z, hidden_size):
h0 = tf.nn.relu(linear(z, hidden_size))
return tf.nn.sigmoid(linear(h0, X_dim))
pytorch-CycleGAN-and-pix2pix (PyTorch):
class ResnetGenerator(nn.Module):
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, padding_type='reflect'):
super(ResnetGenerator, self).__init__()
model = [nn.ReflectionPad2d(3),
nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0),
norm_layer(ngf),
nn.ReLU(True)]
The code snippets highlight the difference in frameworks (TensorFlow vs. PyTorch) and the level of complexity, with pytorch-CycleGAN-and-pix2pix offering a more specialized implementation for image-to-image translation tasks.
CLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image
Pros of CLIP
- More advanced and state-of-the-art model for connecting text and images
- Backed by OpenAI, with extensive documentation and community support
- Offers broader applications in image-text understanding and retrieval
Cons of CLIP
- More complex to implement and requires more computational resources
- Less focused on generative models, which is the primary focus of generative-models
- Steeper learning curve for beginners in machine learning
Code Comparison
CLIP example:
import torch
from PIL import Image
import clip
model, preprocess = clip.load("ViT-B/32", device="cuda")
image = preprocess(Image.open("image.jpg")).unsqueeze(0).to("cuda")
text = clip.tokenize(["a dog", "a cat"]).to("cuda")
with torch.no_grad():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
generative-models example:
import torch
import torch.nn as nn
import torch.nn.functional as F
class VAE(nn.Module):
def __init__(self, x_dim, h_dim1, h_dim2, z_dim):
super(VAE, self).__init__()
self.fc1 = nn.Linear(x_dim, h_dim1)
self.fc2 = nn.Linear(h_dim1, h_dim2)
self.fc31 = nn.Linear(h_dim2, z_dim)
self.fc32 = nn.Linear(h_dim2, z_dim)
PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
Pros of pytorch3d
- Comprehensive 3D deep learning library with advanced features for 3D computer vision tasks
- Actively maintained by Facebook Research with regular updates and improvements
- Extensive documentation and tutorials for ease of use
Cons of pytorch3d
- Steeper learning curve due to its focus on complex 3D operations
- Larger codebase and dependencies, potentially increasing project complexity
- Primarily designed for 3D tasks, which may be overkill for simpler generative models
Code Comparison
pytorch3d example:
import torch
from pytorch3d.structures import Meshes
from pytorch3d.renderer import Textures
verts = torch.randn(4, 3)
faces = torch.tensor([[0, 1, 2], [1, 2, 3]])
mesh = Meshes(verts=[verts], faces=[faces])
generative-models example:
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.model = nn.Sequential(
nn.Linear(100, 256),
nn.ReLU(),
nn.Linear(256, 784),
nn.Tanh()
)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Generative Models
Collection of generative models, e.g. GAN, VAE in Pytorch and Tensorflow. Also present here are RBM and Helmholtz Machine.
Note:
Generated samples will be stored in GAN/{gan_model}/out
(or VAE/{vae_model}/out
, etc) directory during training.
What's in it?
Generative Adversarial Nets (GAN)
- Vanilla GAN
- Conditional GAN
- InfoGAN
- Wasserstein GAN
- Mode Regularized GAN
- Coupled GAN
- Auxiliary Classifier GAN
- Least Squares GAN
- Boundary Seeking GAN
- Energy Based GAN
- f-GAN
- Generative Adversarial Parallelization
- DiscoGAN
- Adversarial Feature Learning & Adversarially Learned Inference
- Boundary Equilibrium GAN
- Improved Training for Wasserstein GAN
- DualGAN
- MAGAN: Margin Adaptation for GAN
- Softmax GAN
- GibbsNet
Variational Autoencoder (VAE)
Restricted Boltzmann Machine (RBM)
Helmholtz Machine
Dependencies
- Install miniconda http://conda.pydata.org/miniconda.html
- Do
conda env create
- Enter the env
source activate generative-models
- Install Tensorflow
- Install Pytorch
Top Related Projects
Models and examples built with TensorFlow
A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc.
PyTorch implementations of Generative Adversarial Networks.
Image-to-Image Translation in PyTorch
CLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image
PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot