AssertionError: the height of conv must be 1

Viewed 12

I want to train my own OCR Model (based on pytorch) and I used this repo:
https://github.com/Deepayan137/Adapting-OCR

Deepayan did a fantastic job and with the demo data (generated images with a height of 32 pixels) everything works fine except for this one thing:

"I had fixed the height to 32 pixels."

Unfortunaltely the images I use are a larger than 32 pixels and resizing made everything worse:

This is the original image 1080x1920: enter image description here

Then I cropped only the relevant part: enter image description here

And if I resize the (cropped) image to a height of 32 pixels, its blurred: enter image description here

I am sorry, but I am not able to get this work. I don't know how to adjust the height.

Any help would be highly appreciated!

Here is the part where he adjusts the width (but not the height).

class SynthCollator(object):
    
    def __call__(self, batch):
        width = [item['img'].shape[2] for item in batch]
        indexes = [item['idx'] for item in batch]
        imgs = torch.ones([len(batch), batch[0]['img'].shape[0], batch[0]['img'].shape[1], 
                           max(width)], dtype=torch.float32)
        for idx, item in enumerate(batch):
            try:
                imgs[idx, :, :, 0:item['img'].shape[2]] = item['img']
            except:
                print(imgs.shape)
        item = {'img': imgs, 'idx':indexes}
        if 'label' in batch[0].keys():
            labels = [item['label'] for item in batch]
            item['label'] = labels
        return item

And this is the part where it breaks:

    def forward(self, input):
        # conv features
        conv = self.cnn(input)
        b, c, h, w = conv.size()
        assert h == 1, "the height of conv must be 1"
        conv = conv.squeeze(2)
        conv = conv.permute(2, 0, 1)  # [w, b, c]
        # rnn features
        output = self.rnn(conv)
        output = output.transpose(1, 0)  # Tbh to bth
        return output

PS: I emailed Deepayan already, but he suggest to resize my images to 32 pixels. As you can see in the above images, this did not work for me.

0 Answers
Related