How to find number of MAC operation in GAN

Hi ,

I would like to find number of MAC operations in my GAN. But , I am not able to find any way to do so.

Could anyone please help me ?

You can manually calculate it by hand.

For example, each 3x3 convolution layer (stride 1, padding 1) consists of c_in x c_out x 3 x 3 x h x w mac operations where h and w represent the size of input features.

Hi @sonsang , thanks a lot for such a prompt reply.

Could you please elaborate bit more ?

# G(z)
class generator(nn.Module):
    # initializers
    def __init__(self, d=128):
        super(generator, self).__init__()
        self.deconv1 = nn.ConvTranspose2d(100, d*8, 4, 1, 0)
        self.deconv1_bn = nn.BatchNorm2d(d*8)
        self.deconv2 = nn.ConvTranspose2d(d*8, d*4, 4, 2, 1)
        self.deconv2_bn = nn.BatchNorm2d(d*4)
        self.deconv3 = nn.ConvTranspose2d(d*4, d*2, 4, 2, 1)
        self.deconv3_bn = nn.BatchNorm2d(d*2)
        self.deconv4 = nn.ConvTranspose2d(d*2, d, 4, 2, 1)
        self.deconv4_bn = nn.BatchNorm2d(d)
        self.deconv5 = nn.ConvTranspose2d(d, 1, 4, 2, 1)
        ## add relu as nn class
        self.relu = nn.ReLU()

    # weight_init
    def weight_init(self, mean, std):
        for m in self._modules:
            normal_init(self._modules[m], mean, std)

    # forward method
    def forward(self, input):
        # x = F.relu(self.deconv1(input))
        #print(" Generator time distribution among various layers ")
        #global deconv1_list
        #print("Invoked generator forward")
        deconv1_start=time.time()
        tX=self.deconv1_bn(self.deconv1(input))
        deconv1_end=time.time()
        deconv1_time=deconv1_end-deconv1_start
        deconv1_list.append(deconv1_time)
                      
        deconv1_relu_start=time.time()    
        x = self.relu(tX)#F.relu(tX)
        deconv1_relu_end=time.time()
        deconv1_relu_time=deconv1_relu_end-deconv1_relu_start
        deconv1_relu_list.append(deconv1_relu_time)
        
        
        deconv2_start=time.time()
        t2_X=self.deconv2_bn(self.deconv2(x))
        deconv2_end=time.time()
        deconv2_time=deconv2_end-deconv2_start
        deconv2_list.append(deconv2_time)
        
        deconv2_relu_start=time.time()
        x = self.relu(t2_X)#F.relu(t2_X)
        deconv2_relu_end=time.time()
        deconv2_relu_time=deconv2_relu_end-deconv2_relu_start
        deconv2_relu_list.append(deconv2_relu_time)
        
        
        deconv3_start=time.time()
        t3_X=self.deconv3_bn(self.deconv3(x))
        deconv3_end=time.time()
        deconv3_time=deconv3_end-deconv3_start
        deconv3_list.append(deconv3_time)
        
        deconv3_relu_start=time.time()
        x = self.relu(t3_X)#F.relu(t3_X)
        deconv3_relu_end=time.time()
        deconv3_relu_time=deconv3_relu_end-deconv3_relu_start
        deconv3_relu_list.append(deconv3_relu_time)
                

        deconv4_start=time.time()
        t4_X=self.deconv4_bn(self.deconv4(x))
        deconv4_end=time.time()
        deconv4_list.append(deconv4_end-deconv4_start)
        
        deconv4_relu_start=time.time()
        x = self.relu(t4_X)#F.relu(t4_X)
        deconv4_relu_end=time.time()
        deconv4_relu_list.append(deconv4_relu_end-deconv4_relu_start)
        
        deconv5_start=time.time()
        t5_X=self.deconv5(x)
        deconv5_end=time.time()
        deconv5_list.append(deconv5_end-deconv5_start)
        
        deconv4_tanh_start=time.time()
        x = F.tanh(t5_X)
        deconv4_tanh_end=time.time()
        deconv5_tanh_list.append(deconv4_tanh_end-deconv4_tanh_start)

        #print("Generator forward end")
        return x

Can you please tell me how can I go about it , let’s say for layer deconv1 ?

Thanks in anticipation.

To my knowledge, you can apply the same formula to deconvolution, too.

Hi @sonsang , thanks again :blush:

So , in case of deconv1 , I am having 100x (128x8) x 4 x (h x w)

But , I am having padding as zero. Then how can I calculate ?

Also , please let me know , how can I find h , w ?