crnn.py 2.8 KB
Newer Older
dengjf's avatar
dengjf committed
1
import torch.nn as nn
dengjb's avatar
dengjb committed
2
import torch.nn.functional as F
dengjf's avatar
dengjf committed
3
4
5
6
7
8

class BidirectionalLSTM(nn.Module):

    def __init__(self, nIn, nHidden, nOut):
        super(BidirectionalLSTM, self).__init__()

dengjb's avatar
dengjb committed
9
        self.rnn = nn.LSTM(nIn, nHidden, bidirectional=True, batch_first=True)
dengjf's avatar
dengjf committed
10
11
12
13
        self.embedding = nn.Linear(nHidden * 2, nOut)

    def forward(self, input):
        recurrent, _ = self.rnn(input)
dengjb's avatar
dengjb committed
14
15
16
17
        # T, b, h = recurrent.size()
        b, T, h = recurrent.size()
        # t_rec = recurrent.view(T * b, h)
        t_rec = recurrent.reshape(T * b, h)
dengjf's avatar
dengjf committed
18
19

        output = self.embedding(t_rec)  # [T * b, nOut]
dengjb's avatar
dengjb committed
20
21
        # output = output.view(T, b, -1)
        output = output.view(b, T, -1)
dengjf's avatar
dengjf committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

        return output


class CRNN(nn.Module):

    def __init__(self, imgH, nc, nclass, nh, n_rnn=2, leakyRelu=False):
        super(CRNN, self).__init__()
        assert imgH % 16 == 0, 'imgH has to be a multiple of 16'

        ks = [3, 3, 3, 3, 3, 3, 2]
        ps = [1, 1, 1, 1, 1, 1, 0]
        ss = [1, 1, 1, 1, 1, 1, 1]
        nm = [64, 128, 256, 256, 512, 512, 512]

        cnn = nn.Sequential()

        def convRelu(i, batchNormalization=False):
            nIn = nc if i == 0 else nm[i - 1]
            nOut = nm[i]
            cnn.add_module('conv{0}'.format(i),
                           nn.Conv2d(nIn, nOut, ks[i], ss[i], ps[i]))
            if batchNormalization:
                cnn.add_module('batchnorm{0}'.format(i), nn.BatchNorm2d(nOut))
            if leakyRelu:
                cnn.add_module('relu{0}'.format(i),
                               nn.LeakyReLU(0.2, inplace=True))
            else:
                cnn.add_module('relu{0}'.format(i), nn.ReLU(True))

        convRelu(0)
        cnn.add_module('pooling{0}'.format(0), nn.MaxPool2d(2, 2))  # 64x16x64
        convRelu(1)
        cnn.add_module('pooling{0}'.format(1), nn.MaxPool2d(2, 2))  # 128x8x32
        convRelu(2, True)
        convRelu(3)
        cnn.add_module('pooling{0}'.format(2),
                       nn.MaxPool2d((2, 2), (2, 1), (0, 1)))  # 256x4x16
        convRelu(4, True)
        convRelu(5)
        cnn.add_module('pooling{0}'.format(3),
                       nn.MaxPool2d((2, 2), (2, 1), (0, 1)))  # 512x2x16
        convRelu(6, True)  # 512x1x16

        self.cnn = cnn
        self.rnn = nn.Sequential(
            BidirectionalLSTM(512, nh, nh),
            BidirectionalLSTM(nh, nh, nclass))

    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)
dengjb's avatar
dengjb committed
77
78
        # conv = conv.permute(2, 0, 1)  # [w, b, c]
        conv = conv.permute(0, 2, 1)  # [b, w, c]
dengjf's avatar
dengjf committed
79
80
81
82

        # rnn features
        output = self.rnn(conv)

dengjb's avatar
dengjb committed
83
84
85
        # add log_softmax to converge output
        output = F.log_softmax(output, dim=2)

dengjf's avatar
dengjf committed
86
        return output