Close Menu
    Trending
    • Transform Complexity into Opportunity with Digital Engineering
    • OpenAI Is Fighting Back Against Meta Poaching AI Talent
    • Lessons Learned After 6.5 Years Of Machine Learning
    • Handling Big Git Repos in AI Development | by Rajarshi Karmakar | Jul, 2025
    • National Lab’s Machine Learning Project to Advance Seismic Monitoring Across Energy Industries
    • HP’s PCFax: Sustainability Via Re-using Used PCs
    • Mark Zuckerberg Reveals Meta Superintelligence Labs
    • Prescriptive Modeling Makes Causal Bets – Whether You Know it or Not!
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»Deep Convolutional GANs (DCGANs): From Theory to Implementation with Pytorch | by Anuj Krishna Phuyal | Dec, 2024
    Machine Learning

    Deep Convolutional GANs (DCGANs): From Theory to Implementation with Pytorch | by Anuj Krishna Phuyal | Dec, 2024

    Team_AIBS NewsBy Team_AIBS NewsDecember 30, 2024No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Introduction

    In my earlier article on GANs (link to basic GAN article), we explored the foundational ideas of Generative Adversarial Networks (GANs). At present, we’ll dive into one of the important developments in GAN structure—deep Convolutional GANs (DCGANs), launched by Radford et al. of their seminal 2015 paper.

    What are DCGANs?

    DCGANs characterize a vital development in GAN structure by incorporating convolutional neural networks into the generator and discriminator. Whereas conventional GANs struggled with stability and picture high quality, DCGANs launched architectural tips that made coaching extra steady and improved the standard of generated photos considerably.

    The DCGAN structure follows these core rules:

    1. Substitute Pooling with Strided Convolutions: As a substitute of utilizing pooling layers, DCGANs use strided convolutions within the discriminator and fractional-strided convolutions within the generator. This enables the community to study its personal spatial downsampling/upsampling.
    2. Batch Normalization: Each generator and discriminator use batch normalization to stabilize studying. Nevertheless, it’s not utilized to the generator’s output layer and discriminator’s enter layer.
    3. Take away Absolutely Linked Hidden Layers: The mannequin makes use of convolutional options all through, eliminating dense layers aside from enter/output.
    4. Activation Features:
    • Generator: ReLU activation in all layers besides the output (which makes use of Tanh)
    • Discriminator: LeakyReLU activation in all layers
    DCGAN Generator Structure

    The Generator Structure

    The generator transforms a random noise vector into a picture via a collection of upsampling layers:

    1. Enter: Random noise vector (z) of dimension 100
    2. Challenge and reshape into 4×4×1024 characteristic maps
    3. Collection of fractional-strided convolutions to regularly enhance the spatial dimension
    4. Remaining layer outputs a picture with Tanh’s activation

    The Discriminator Structure

    The discriminator follows a conventional CNN structure:
    1. enter: Picture (e.g., 64×64×3)
    2. Collection of strided convolutions with LeakyReLU
    3. Flatten and output a single sigmoid chance

    Let’s implement DCGAN utilizing PyTorch. We’ll break down the implementation into key elements:

    Generator Class

    class Generator(nn.Module):
    def __init__(self, noise_channels, input_channels, output_channels, input_features):
    tremendous(Generator, self).__init__()
    self.gen = nn.Sequential(
    # Preliminary projection and reshape
    self.transposed_convolution_block(noise_channels, input_features*16, 4, 1, 0),
    # Collection of transposed convolutions
    self.transposed_convolution_block(input_features*16, input_features*8, 4, 2, 1),
    self.transposed_convolution_block(input_features*8, input_features*4, 4, 2, 1),
    self.transposed_convolution_block(input_features*4, input_features*2, 4, 2, 1),
    nn.ConvTranspose2d(input_features*2, output_channels, kernel_size=4, stride=2, padding=1),
    nn.Tanh()
    )

    def transposed_convolution_block(self, in_filters, out_filters, kernel_size, stride, padding):
    return nn.Sequential(
    nn.ConvTranspose2d(in_filters, out_filters, kernel_size, stride, padding, bias=False),
    nn.BatchNorm2d(out_filters),
    nn.ReLU()
    )

    def ahead(self, x):
    return self.gen(x)

    Discriminator Class

    class Discriminator(nn.Module):
    def __init__(self, input_channels, input_features):
    tremendous(Discriminator, self).__init__()
    self.disc = nn.Sequential(
    # Preliminary convolution
    nn.Conv2d(input_channels, input_features, kernel_size=4, stride=2, padding=1),
    nn.LeakyReLU(0.2),
    # Collection of convolution blocks
    self.convolution_block(input_features, input_features*2, 4, 2, 1),
    self.convolution_block(input_features*2, input_features*4, 4, 2, 1),
    self.convolution_block(input_features*4, input_features*8, 4, 2, 1),
    self.convolution_block(input_features*8, input_features*16, 4, 2, 1),
    nn.Conv2d(input_features*16, 1, kernel_size=4, stride=2, padding=1),
    nn.Sigmoid()
    )

    def convolution_block(self, in_filters, out_filters, kernel_size, stride, padding):
    return nn.Sequential(
    nn.Conv2d(in_filters, out_filters, kernel_size, stride, padding, bias=False),
    nn.BatchNorm2d(out_filters),
    nn.LeakyReLU(0.2)
    )

    def ahead(self, x):
    return self.disc(x)

    The coaching course of follows these key steps:

    1. Hyperparameters:
    gadget = torch.gadget("cuda" if torch.cuda.is_available() else "cpu")
    BATCH_SIZE = 64
    LR = 2e-4
    image_size = 64
    img_channels = 3
    Discriminator_features = 64
    Generator_features = 64
    Noise_dimensions = 100
    epochs = 100

    2. Information preprocessing and cleansing

    knowledge ="BasicGAN/knowledge/img_align_celeba.zip"

    transforms = transforms.Compose(
    [
    transforms.Resize((image_size, image_size)),
    transforms.ToTensor(),
    transforms.Normalize(
    [0.5 for _ in range(img_channels)], [0.5 for _ in range(img_channels)]
    ),
    ]
    )

    datasets = datasets.ImageFolder(root=knowledge, rework=transforms)

    dataloader = DataLoader(datasets, batch_size=BATCH_SIZE, shuffle=True)

    3. Coaching Loop:

    generator = Generator(Noise_dimensions, img_channels, img_channels, Generator_features).to(gadget)
    discriminator = Discriminator(img_channels, Discriminator_features).to(gadget)
    initialize_weights(generator)
    initialize_weights(discriminator)

    criterion = nn.BCELoss()
    generator_optimizer = optim.Adam(generator.parameters(), lr=LR, betas=(0.5, 0.999))
    discriminator_optimizer = optim.Adam(discriminator.parameters(), lr=LR, betas=(0.5, 0.999))

    fixed_noise = torch.randn(32, Noise_dimensions, 1, 1).to(gadget)

    for epoch in vary(epochs):
    for idx, (actual, _) in enumerate(dataloader):
    actual = actual.to(gadget)
    noise = torch.randn(BATCH_SIZE, Noise_dimensions, 1, 1).to(gadget)
    # print(actual.form)

    pretend = generator(noise)
    disc_real = discriminator(actual).reshape(-1)
    disc_fake = discriminator(pretend).reshape(-1)

    #for discriminator
    loss_disc_real = criterion(disc_real, torch.ones_like(disc_real))
    loss_disc_fake = criterion(disc_fake, torch.zeros_like(disc_fake))
    loss_Discriminator = (loss_disc_real + loss_disc_fake) / 2
    discriminator.zero_grad()
    loss_Discriminator.backward(retain_graph=True)
    discriminator_optimizer.step()

    #for generator

    output = discriminator(pretend).reshape(-1)
    loss_Generator = criterion(output, torch.ones_like(disc_fake))
    generator.zero_grad()
    loss_Generator.backward()
    generator_optimizer.step()

    writer_fake = SummaryWriter(f"logs/pretend")
    writer_real = SummaryWriter(f"logs/actual")
    step =0
    if idx % 100 == 0:
    print(
    f"Epoch [{epoch}/{epochs}] Batch {idx}/{len(dataloader)}
    Loss D: {loss_Discriminator:.4f}, loss G: {loss_Generator:.4f}"
    )

    with torch.no_grad():
    pretend = generator(fixed_noise)
    # take out (as much as) 32 examples
    img_grid_real = torchvision.utils.make_grid(actual[:32], normalize=True)
    img_grid_fake = torchvision.utils.make_grid(pretend[:32], normalize=True)

    writer_real.add_image("Actual", img_grid_real, global_step=step)
    writer_fake.add_image("Pretend", img_grid_fake, global_step=step)

    step += 1



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTrump’s Administration Will Impact AI, Energy, Crypto and More
    Next Article This Is the Secret Marketing Tool Your Small Business Needs to Compete With the Big Brands
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    Handling Big Git Repos in AI Development | by Rajarshi Karmakar | Jul, 2025

    July 1, 2025
    Machine Learning

    A Technical Overview of the Attention Mechanism in Deep Learning | by Silva.f.francis | Jun, 2025

    June 30, 2025
    Machine Learning

    Tone Awareness: Setting the Right Energy for Digital Spaces | by Fred’s Bytes | Jun, 2025

    June 30, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Transform Complexity into Opportunity with Digital Engineering

    July 1, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Tom Green, Former Provocateur, Is Building Something New

    January 25, 2025

    Inside China’s electric-vehicle-to-humanoid-robot pivot | MIT Technology Review

    February 18, 2025

    🚀 𝗔𝗜 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴 𝗶𝘀 𝘁𝗵𝗲 𝗙𝘂𝘁𝘂𝗿𝗲 — 𝗔𝗿𝗲 𝗬𝗼𝘂 𝗥𝗲𝗮𝗱𝘆? 🤖💡 | by Vivian Aranha | Feb, 2025

    February 6, 2025
    Our Picks

    Transform Complexity into Opportunity with Digital Engineering

    July 1, 2025

    OpenAI Is Fighting Back Against Meta Poaching AI Talent

    July 1, 2025

    Lessons Learned After 6.5 Years Of Machine Learning

    July 1, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.