Unittest the pytorch forward function
I want to unittest the overridden forward function of my Network modell in Pytorch. So I loaded my model (pretrained from Zoo) with the setUp method, loaded a seed and created some random batch. In my method testForward I tested the result of forward against shape and numel, but I also want to check a specific value which a apears to be 0. I wasn't shure about that so checked my params in setUp also, which appears not to be 0.
import unittest
import torch
from SemanticSegmentation.models.fcn8 import FCN8


class TestFCN8(unittest.TestCase):

    def setUp(self):
        self.model = FCN8(8, pretrained=True)
        torch.manual_seed(0)
        self.x = torch.rand((4, 3, 45, 45))
        for param in self.model.parameters():
            print(param.data)

    def testForward(self):
        self.assertEqual(self.model.forward(self.x).shape.numel(), 64800)
        self.assertEqual(str(self.model.forward(self.x).shape), 'torch.Size([4, 8, 45, 45])')
        print(self.model.named_parameters)


if __name__ == &quot;__main__&quot;:
    unittest.main()


So my Question is: the sahpe of the forward return tensor is what I expect but why is this tensor completly zero? I expected at least a few values.
The imported modell is based on an VGG16 network with upscoring the after ConvLayer 4 , 8 and 16. If needed I could also present the modell code.

Ok after tinkering around and debugging the forward function I came to following explanation:
Some Information about the architecture
If you do classes from Andrew Ng or others you learn not to initialize the weights to the same value, as example &quot;0&quot;. This is what the writers of the original Paper of FCNs do and they say, because it doesn't change the performance or didn't yield to faster convergence (FCN-Paper).
My Solution
So for testing purpose I initlize in the testing module to seeded random values, which I can test against:
import unittest
import torch
from SemanticSegmentation.models.fcn8 import FCN8


class TestFCN8(unittest.TestCase):

    def setUp(self):
        self.model = FCN8(8, pretrained=True)
        torch.manual_seed(0)
        # instead of zero init for score tensors use random init
        self.model.score_fr[6].weight.data.random_()
        self.model.score_fr[6].bias.data.random_()
        self.model.score_pool3.weight.data.random_()
        self.model.score_pool3.bias.data.random_()
        self.model.score_pool4.weight.data.random_()
        self.model.score_pool4.bias.data.random_()
        self.x = torch.rand((4, 3, 45, 45))

    def testForward(self):
        self.assertEqual(
            self.model.forward(self.x).shape.numel(), 64800)
        self.assertEqual(
            list(self.model.forward(self.x).shape), [4, 8, 45, 45])
        self.assertEqual(
            float(self.model.forward(self.x)[3][4][44][4]), 2277257216.0))

if __name__ == &quot;__main__&quot;:
    unittest.main()

