Example GAN usage in Noxer

Generative Adversarial Neural Networks are able to learn to sample from high dimensional distributions. For example, generation of realistic looking images of car designs is an instance of such learning problem.

In this example, we will train GAN to generate such complex structures.

Data used

For our experiments, we will use the "Cars Dataset" from Stanford, which you can find here: http://ai.stanford.edu/~jkrause/cars/car_dataset.html


In [1]:
# load the data
import numpy as np

X = np.random.randint(0, 10, size=10000)
Y = np.zeros((10000, 28, 28, 1))

Run the training!


In [2]:
from noxer.gm.gan import ACGANCategoryToImageGenerator

model = ACGANCategoryToImageGenerator(verbose=1)
model.fit(X, Y)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-7a48c3c0ae46> in <module>()
      2 
      3 model = ACGANCategoryToImageGenerator(verbose=1)
----> 4 model.fit(X, Y)

~/noxer/noxer/gm/acgan.py in fit(self, X, Y, **kwargs)
    279         )
    280 
--> 281         self.net.train(X, Y)
    282 
    283     def predict_noise(self, X, Z, **kwargs):

~/noxer/noxer/gm/acgan.py in train(self, X, Y)
    193                 self.D_optimizer.zero_grad()
    194 
--> 195                 D_real, C_real = self.D(x_)
    196                 D_real_loss = self.BCE_loss(D_real, self.y_real_)
    197                 C_real_loss = self.CE_loss(C_real, torch.max(y_vec_, 1)[1])

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/noxer/noxer/gm/acgan.py in forward(self, input)
     89 
     90     def forward(self, input):
---> 91         x = self.conv(input)
     92         x = x.view(-1, 128 * (self.input_height // 4) * (self.input_width // 4))
     93         x = self.fc1(x)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/container.py in forward(self, input)
     65     def forward(self, input):
     66         for module in self._modules.values():
---> 67             input = module(input)
     68         return input
     69 

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    252     def forward(self, input):
    253         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 254                         self.padding, self.dilation, self.groups)
    255 
    256 

/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py in conv2d(input, weight, bias, stride, padding, dilation, groups)
     50     f = ConvNd(_pair(stride), _pair(padding), _pair(dilation), False,
     51                _pair(0), groups, torch.backends.cudnn.benchmark, torch.backends.cudnn.enabled)
---> 52     return f(input, weight, bias)
     53 
     54 

RuntimeError: Need input.size[1] == 1 but got 28 instead.