I have a CNN structure in LibTorch but the dimensions are not ok. My objective is to input a 3 channel 64x64 image and output a logistic regression float for a DGAN. Last layer I set as input channels 36 because if I remove that layer the output neuron had 6x6 dimension so I guesses that was the required dimension for the input of the fully connected. I would like to know:
- What do you normally do to check dimensions in LibTorch or Pytorch (i.e. check the required size for the last module, check how many trainable parameters has each layer ...)
- What is the error in this case
#include <torch/torch.h>
#include "parameters.h"
using namespace torch;
class DCGANDiscriminatorImpl: public nn::Module {
private:
nn::Conv2d conv1, conv2, conv3, conv4;
nn::BatchNorm2d batch_norm1, batch_norm2;
nn::Linear fc1;
public:
DCGANDiscriminatorImpl()
:conv1(nn::Conv2dOptions(3, 64, 4).stride(2).padding(1).bias(false)),
conv2(nn::Conv2dOptions(64, 128, 4).stride(2).padding(1).bias(false)),
batch_norm1(128),
conv3(nn::Conv2dOptions(128, 256, 4).stride(2).padding(1).bias(false)),
batch_norm2(256),
conv4(nn::Conv2dOptions(256, 1, 3).stride(1).padding(0).bias(false)),
fc1(6*6, 1)
{
register_module("conv1", conv1);
register_module("conv2", conv2);
register_module("conv3", conv3);
register_module("conv4", conv4);
register_module("batch_norm1", batch_norm1);
register_module("batch_norm2", batch_norm2);
register_module("fc1", fc1);
}
Tensor forward(torch::Tensor x)
{
x = leaky_relu(conv1(x), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm1(conv2(x)), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm2(conv3(x)), cte::NEGATIVE_SLOPE);
x = sigmoid(fc1(x));
return x;
}
};
TORCH_MODULE(DCGANDiscriminator);
The error I get is:
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: size mismatch, m1: [131072 x 8], m2: [36 x 1] at ../aten/src/TH/generic/THTensorMath.cpp:136