This file contains hidden or 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
| # Grab a batch of real images from the dataloader | |
| real_batch = next(iter(dataloader)) | |
| # Plot the real images | |
| plt.figure(figsize=(15,15)) | |
| plt.subplot(1,2,1) | |
| plt.axis("off") | |
| plt.title("Real Images") | |
| plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=5, normalize=True).cpu(),(1,2,0))) |
This file contains hidden or 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
| plt.figure(figsize=(10,5)) | |
| plt.title("Generator and Discriminator Loss During Training") | |
| plt.plot(G_losses,label="G") | |
| plt.plot(D_losses,label="D") | |
| plt.xlabel("iterations") | |
| plt.ylabel("Loss") | |
| plt.legend() | |
| plt.show() |
This file contains hidden or 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
| # Initialize BCELoss function | |
| criterion = nn.BCELoss() | |
| # Create batch of latent vectors that we will use to visualize | |
| # the progression of the generator | |
| fixed_noise = torch.randn(64, nz, 1, 1, device=device) | |
| # Establish convention for real and fake labels during training | |
| real_label = 1 | |
| fake_label = 0 |
This file contains hidden or 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
| class Discriminator(nn.Module): | |
| def __init__(self, ngpu): | |
| super(Discriminator, self).__init__() | |
| self.ngpu = ngpu | |
| self.main = nn.Sequential( | |
| # input is (nc) x 64 x 64 | |
| nn.Conv2d(nc, ndf, 4, 2, 1, bias=False), | |
| nn.LeakyReLU(0.2, inplace=True), | |
| # state size. (ndf) x 32 x 32 | |
| nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False), |
This file contains hidden or 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
| # Generator Code | |
| class Generator(nn.Module): | |
| def __init__(self, ngpu): | |
| super(Generator, self).__init__() | |
| self.ngpu = ngpu | |
| self.main = nn.Sequential( | |
| # input is Z, going into a convolution | |
| nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False), | |
| nn.BatchNorm2d(ngf * 8), |
This file contains hidden or 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
| # custom weights initialization called on netG and netD | |
| def weights_init(m): | |
| classname = m.__class__.__name__ | |
| if classname.find('Conv') != -1: | |
| nn.init.normal_(m.weight.data, 0.0, 0.02) | |
| elif classname.find('BatchNorm') != -1: | |
| nn.init.normal_(m.weight.data, 1.0, 0.02) | |
| nn.init.constant_(m.bias.data, 0) |
This file contains hidden or 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
| # We can use an image folder dataset the way we have it setup. | |
| # Create the dataset | |
| dataset = dset.ImageFolder(root=dataroot, | |
| transform=transforms.Compose([ | |
| transforms.Resize(image_size), | |
| transforms.CenterCrop(image_size), | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), | |
| ])) | |
| # Create the dataloader |
This file contains hidden or 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
| # Root directory for dataset | |
| dataroot = "/home/ubuntu/cropped/" | |
| # Number of workers for dataloader | |
| workers = 2 | |
| # Batch size during training | |
| batch_size = 128 | |
| # Spatial size of training images. All images will be resized to this |
This file contains hidden or 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
| from __future__ import print_function | |
| #%matplotlib inline | |
| import argparse | |
| import os | |
| import random | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.parallel | |
| import torch.backends.cudnn as cudnn | |
| import torch.optim as optim |
This file contains hidden or 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
| for i in sorted_inds: | |
| bbox = boxes[i, :4] | |
| score = boxes[i, -1] | |
| if score < thresh: | |
| continue | |
| ## added code starting from here | |
| print ('bounding box: {}'.format(bbox)) | |
| class_string = get_class_string(classes[i], score, dataset) | |
| class_string = class_string.split(" ")[0] |