Model forward fails

I’ve been facing this problem since several days now.
I have converted my model just like this:

model = DenoisingAutoencoderSheetEdges()
model.load_state_dict(torch.load('models/model_3.pth').state_dict())
model.threshold = 0.75
model.hard_mode = True
model.eval()

qmodel = quantization.convert(module=model)
qmodel = torch.jit.script(qmodel)
qmodel.save('mobile/android_edge_model_2.4.pt')

my model:

class DenoisingAutoencoderSheetEdges(nn.Module):

    def __init__(self):
        super(DenoisingAutoencoderSheetEdges, self).__init__()
        self.hard_mode = False
        self.threshold = 0.75

        self.encoder = nn.Sequential(
            nn.ConvTranspose2d(1, 6, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(6, 16, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(16, 25, kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(25, 50, kernel_size=5),
            nn.ReLU(True)
        )

        self.decoder = nn.Sequential(
            nn.Conv2d(50, 25, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(25, 16, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(16, 6, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(6, 1, kernel_size=5),
            nn.Sigmoid(),
        )

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
       
        if self.hard_mode:
            x_tensor = torch.ones(x.shape)
            y_tensor = torch.zeros(x.shape)
            x = torch.where(x > self.threshold, x_tensor, y_tensor)

        return x

The model is loaded correctly, but the following error occurs during forwarding:

     Caused by: java.lang.RuntimeException: [enforce fail at CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 642720000 bytes. Buy new RAM!
    
    The above operation failed in interpreter.
    Traceback (most recent call last):
    Serialized   File "code/__torch__/models.py", line 11
      def forward(self: __torch__.models.DenoisingAutoencoderSheetEdges,
        x: Tensor) -> Tensor:
        x0 = (self.encoder).forward(x, )
              ~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        x1 = (self.decoder).forward(x0, )
        if self.hard_mode:
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\container.py", line 100
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
    Serialized   File "code/__torch__/torch/nn/modules/container.py", line 30, in forward
        input5 = (_5).forward(input4, )
        input6 = (_6).forward(input5, None, )
        return (_7).forward(input6, )
                ~~~~~~~~~~~ <--- HERE
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\container.py", line 100
        def forward(self, input):
            for module in self:
                input = module(input)
                        ~~~~~~ <--- HERE
            return input
    Serialized   File "code/__torch__/torch/nn/modules/container.py", line 29, in forward
        input4 = (_4).forward(input3, None, )
        input5 = (_5).forward(input4, )
        input6 = (_6).forward(input5, None, )
                  ~~~~~~~~~~~ <--- HERE
        return (_7).forward(input6, )
      File "G:\Anaconda3\envs\PyTorch_pip2\lib\site-packages\torch\nn\modules\conv.py", line 776
            output_padding = self._output_padding(input, output_size, self.stride, self.padding, self.kernel_size)
    
            return F.conv_transpose2d(
                   ~~~~~~~~~~~~~~~~~~ <--- HERE
                input, self.weight, self.bias, self.stride, self.padding,
                output_padding, self.groups, self.dilation)
    Serialized   File "code/__torch__/torch/nn/modules/conv/___torch_mangle_2.py", line 16, in forward
          pass
        output_padding = (self)._output_padding(input, output_size, [1, 1], [0, 0], [5, 5], )
        _0 = torch.conv_transpose2d(input, self.weight, self.bias, [1, 1], [0, 0], output_padding, 1, [1, 1])
             ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        return _0
      def _output_padding(self: __torch__.torch.nn.modules.conv.___torch_mangle_2.ConvTranspose2d,
    
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(Module.java:37)
        at com.example.scannerapp_0.DenoiseEdge.getEdgeData(DenoiseEdge.java:43)
        at com.example.scannerapp_0.MainActivity.net(MainActivity.java:99)
        	... 13 more

I do not understand what the error is trying to tell me or what I need to improve.
I would appreciate any hint or explination.

Greetings,
Unity

From the beginning of the stack trace, it seems like you’re trying to make a Tensor which is too large (600MB). Is that expected? How much memory does it use if you run it on your machine?

Ye, that actually caused the error.
I was wondering why the error occured on my emulator device with 2gb memory
since the device in idle state uses only about 800mb, that’s why I thought the cause would he more complex.
However with a 4gb memory device it worked fine.

1 Like