def forward(self, x): x0 = self.wR2.module.features[0](x) _x1 = self.wR2.module.features[1](x0) x2 = self.wR2.module.features[2](_x1) _x3 = self.wR2.module.features[3](x2) x4 = self.wR2.module.features[4](_x3) _x5 = self.wR2.module.features[5](x4) x6 = self.wR2.module.features[6](_x5) x7 = self.wR2.module.features[7](x6) x8 = self.wR2.module.features[8](x7) x9 = self.wR2.module.features[9](x8) x9 = x9.view(x9.size(0), -1) boxLoc = self.wR2.module.classifier(x9) h1, w1 = _x1.data.size()[2], _x1.data.size()[3] p1 = Variable( torch.FloatTensor([[w1, 0, 0, 0], [0, h1, 0, 0], [0, 0, w1, 0], [0, 0, 0, h1]]).cuda(), requires_grad=False, ) h2, w2 = _x3.data.size()[2], _x3.data.size()[3] p2 = Variable( torch.FloatTensor([[w2, 0, 0, 0], [0, h2, 0, 0], [0, 0, w2, 0], [0, 0, 0, h2]]).cuda(), requires_grad=False, ) h3, w3 = _x5.data.size()[2], _x5.data.size()[3] p3 = Variable( torch.FloatTensor([[w3, 0, 0, 0], [0, h3, 0, 0], [0, 0, w3, 0], [0, 0, 0, h3]]).cuda(), requires_grad=False, ) # x, y, w, h --> x1, y1, x2, y2 assert boxLoc.data.size()[1] == 4 postfix = Variable( torch.FloatTensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]]).cuda(), requires_grad=False, ) boxNew = boxLoc.mm(postfix).clamp(min=0, max=1) # input = Variable(torch.rand(2, 1, 10, 10), requires_grad=True) # rois = Variable(torch.LongTensor([[0, 1, 2, 7, 8], [0, 3, 3, 8, 8], [1, 3, 3, 8, 8]]), requires_grad=False) roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8)) roi2 = roi_pooling_ims(_x3, boxNew.mm(p2), size=(16, 8)) roi3 = roi_pooling_ims(_x5, boxNew.mm(p3), size=(16, 8)) rois = torch.cat((roi1, roi2, roi3), 1) _rois = rois.view(rois.size(0), -1) y0 = self.classifier1(_rois) y1 = self.classifier2(_rois) y2 = self.classifier3(_rois) y3 = self.classifier4(_rois) y4 = self.classifier5(_rois) y5 = self.classifier6(_rois) y6 = self.classifier7(_rois) return boxLoc, [y0, y1, y2, y3, y4, y5, y6]
def forward(self, x): x0 = self.wR2.module.features[0](x) _x1 = self.wR2.module.features[1](x0) x2 = self.wR2.module.features[2](_x1) _x3 = self.wR2.module.features[3](x2) x4 = self.wR2.module.features[4](_x3) _x5 = self.wR2.module.features[5](x4) x6 = self.wR2.module.features[6](_x5) x7 = self.wR2.module.features[7](x6) x8 = self.wR2.module.features[8](x7) x9 = self.wR2.module.features[9](x8) x9 = x9.view(x9.size(0), -1) #print(x9.shape) boxLoc = self.wR2.module.classifier(x9) h1, w1 = _x1.data.size()[2], _x1.data.size()[3] if use_gpu: p1 = Variable(torch.FloatTensor([[w1,0,0,0],[0,h1,0,0],[0,0,w1,0],[0,0,0,h1]]).cuda(), requires_grad=False) else: p1 = Variable(torch.FloatTensor([[w1,0,0,0],[0,h1,0,0],[0,0,w1,0],[0,0,0,h1]]), requires_grad=False) h2, w2 = _x3.data.size()[2], _x3.data.size()[3] if use_gpu: p2 = Variable(torch.FloatTensor([[w2,0,0,0],[0,h2,0,0],[0,0,w2,0],[0,0,0,h2]]).cuda(), requires_grad=False) else: p2 = Variable(torch.FloatTensor([[w2,0,0,0],[0,h2,0,0],[0,0,w2,0],[0,0,0,h2]]), requires_grad=False) h3, w3 = _x5.data.size()[2], _x5.data.size()[3] if use_gpu: p3 = Variable(torch.FloatTensor([[w3,0,0,0],[0,h3,0,0],[0,0,w3,0],[0,0,0,h3]]).cuda(), requires_grad=False) else: p3 = Variable(torch.FloatTensor([[w3,0,0,0],[0,h3,0,0],[0,0,w3,0],[0,0,0,h3]]), requires_grad=False) # x, y, w, h --> x1, y1, x2, y2 assert boxLoc.data.size()[1] == 4 if use_gpu: postfix = Variable(torch.FloatTensor([[1,0,1,0],[0,1,0,1],[-0.5,0,0.5,0],[0,-0.5,0,0.5]]).cuda(), requires_grad=False) else: postfix = Variable(torch.FloatTensor([[1,0,1,0],[0,1,0,1],[-0.5,0,0.5,0],[0,-0.5,0,0.5]]), requires_grad=False) boxNew = boxLoc.mm(postfix).clamp(min=0, max=1) roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8)) roi2 = roi_pooling_ims(_x3, boxNew.mm(p2), size=(16, 8)) roi3 = roi_pooling_ims(_x5, boxNew.mm(p3), size=(16, 8)) rois = torch.cat((roi1, roi2, roi3), 1)#(N,416,8,16) rois_r = rois.view(rois.shape[0],rois.shape[1],-1) #(N,416,1,128) rois_r = rois_r.permute(0,2,1) #(N,128,416) #Pass to sequence modeller output = self.rnn(rois_r) #(N,128,nout) return boxLoc, output
def forward(self, x): x0 = self.wR2.module.features[0](x) # [1,3,480,480]--->[1,48,121,121] _x1 = self.wR2.module.features[1](x0) # [1,48,121,121]--->[1,64,122,122] x2 = self.wR2.module.features[2](_x1) # [1,64,122,122]--->[1,128,62,62] _x3 = self.wR2.module.features[3](x2) # [1,128,62,62]--->[1,160,63,63] x4 = self.wR2.module.features[4](_x3) # [1,160,63,63]--->[1,192,32,32] _x5 = self.wR2.module.features[5](x4) # [1,192,32,32]--->[1,192,33,33] x6 = self.wR2.module.features[6](_x5) # [1,192,33,33]--->[1,192,17,17] x7 = self.wR2.module.features[7](x6) # [1,192,17,17]--->[1,192,18,18] x8 = self.wR2.module.features[8](x7) # [1,192,18,18]--->[1,192,10,10] x9 = self.wR2.module.features[9](x8) # [1,192,10,10]--->[1,192,11,11] x9 = x9.view(x9.size(0), -1) # [1,192,11,11]--->[1,23232] boxLoc = self.wR2.module.classifier(x9) # [1,23232]--->[1,4] #预测车牌位置 # 多尺度卷积:在不同层数的feature map在通道维数上进行拼接,然后预测 with torch.no_grad(): h1, w1 = _x1.data.size()[2], _x1.data.size()[3] p1 = torch.FloatTensor([[w1, 0, 0, 0], [0, h1, 0, 0], [0, 0, w1, 0], [0, 0, 0, h1]]).cuda() h2, w2 = _x3.data.size()[2], _x3.data.size()[3] p2 = torch.FloatTensor([[w2, 0, 0, 0], [0, h2, 0, 0], [0, 0, w2, 0], [0, 0, 0, h2]]).cuda() h3, w3 = _x5.data.size()[2], _x5.data.size()[3] p3 = torch.FloatTensor([[w3, 0, 0, 0], [0, h3, 0, 0], [0, 0, w3, 0], [0, 0, 0, h3]]).cuda() # x, y, w, h --> x1, y1, x2, y2 assert boxLoc.data.size()[1] == 4 postfix = torch.FloatTensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]]).cuda() boxNew = boxLoc.mm(postfix).clamp(min=0, max=1) # input = torch.rand(2, 1, 10, 10) # rois = torch.LongTensor([[0, 1, 2, 7, 8], [0, 3, 3, 8, 8], [1, 3, 3, 8, 8]]) roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8)) # [1,64,16,8] roi2 = roi_pooling_ims(_x3, boxNew.mm(p2), size=(16, 8)) # [1,160,16,8] roi3 = roi_pooling_ims(_x5, boxNew.mm(p3), size=(16, 8)) # [1,192,16,8] rois = torch.cat((roi1, roi2, roi3), 1) # [1,416,16,8] _rois = rois.view(rois.size(0), -1) # [1,53248] y0 = self.classifier1(_rois) # [1,38] 预测车牌的第一个字符:省份 y1 = self.classifier2(_rois) # [1,25] 预测车牌的第二个字符:市 y2 = self.classifier3(_rois) # [1,35] 预测车牌的第三个字符 y3 = self.classifier4(_rois) # [1,35] 预测车牌的第四个字符 y4 = self.classifier5(_rois) # [1,35] 预测车牌的第五个字符 y5 = self.classifier6(_rois) # [1,35] 预测车牌的第六个字符 y6 = self.classifier7(_rois) # [1,35] 预测车牌的第七个字符 return boxLoc, [y0, y1, y2, y3, y4, y5, y6]
def forward(self, x): x0 = self.wR2.module.features[0](x) _x1 = self.wR2.module.features[1](x0) x2 = self.wR2.module.features[2](_x1) _x3 = self.wR2.module.features[3](x2) x4 = self.wR2.module.features[4](_x3) _x5 = self.wR2.module.features[5](x4) x6 = self.wR2.module.features[6](_x5) x7 = self.wR2.module.features[7](x6) x8 = self.wR2.module.features[8](x7) x9 = self.wR2.module.features[9](x8) x9 = x9.view(x9.size(0), -1) boxLoc = self.wR2.module.classifier(x9) #print('BoxLox is {} and shape is {}'.format(boxLoc, boxLoc.shape)) h1, w1 = _x1.data.size()[2], _x1.data.size()[3] p1 = Variable(torch.FloatTensor([[w1, 0, 0, 0], [0, h1, 0, 0], [0, 0, w1, 0], [0, 0, 0, h1]]).cuda(), requires_grad=False) h2, w2 = _x3.data.size()[2], _x3.data.size()[3] p2 = Variable(torch.FloatTensor([[w2, 0, 0, 0], [0, h2, 0, 0], [0, 0, w2, 0], [0, 0, 0, h2]]).cuda(), requires_grad=False) h3, w3 = _x5.data.size()[2], _x5.data.size()[3] p3 = Variable(torch.FloatTensor([[w3, 0, 0, 0], [0, h3, 0, 0], [0, 0, w3, 0], [0, 0, 0, h3]]).cuda(), requires_grad=False) # x, y, w, h --> x1, y1, x2, y2 assert boxLoc.data.size()[1] == 4 postfix = Variable(torch.FloatTensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]]).cuda(), requires_grad=False) boxNew = boxLoc.mm(postfix).clamp(min=0, max=1) #print('boxNew is {} and shape is {}'.format(boxNew , boxNew .shape)) # input = Variable(torch.rand(2, 1, 10, 10), requires_grad=True) # rois = Variable(torch.LongTensor([[0, 1, 2, 7, 8], [0, 3, 3, 8, 8], [1, 3, 3, 8, 8]]), requires_grad=False) #print(' boxNew.mm(p1) shape: {}'.format( torch.mm(boxNew,p1))) roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8)) #(N,64,8,16) roi2 = roi_pooling_ims(_x3, boxNew.mm(p2), size=(16, 8)) #(N,160,8,16) roi3 = roi_pooling_ims(_x5, boxNew.mm(p3), size=(16, 8)) #(N,192,8,16) rois = torch.cat((roi1, roi2, roi3), 1) #(N,416,8,16) #_rois = rois.view(rois.size(0), -1) rois_r = rois.view(rois.shape[0], rois.shape[1], -1) #(N,416,1,128) #rois_r = rois_r.squeeze(2) #(N,416,128) rois_r = rois_r.permute(0, 2, 1) #(N,128,416) #print('Input to RNN: {}'.format(rois_r.shape)) output = self.rnn(rois_r) #(128,N,nout) # y0 = self.classifier1(_rois) # y1 = self.classifier2(_rois) # y2 = self.classifier3(_rois) # y3 = self.classifier4(_rois) # y4 = self.classifier5(_rois) # y5 = self.classifier6(_rois) # y6 = self.classifier7(_rois) #return boxLoc, [y0, y1, y2, y3, y4, y5, y6] #print('output.shape: {}'.format(output.shape)) return boxLoc, output