def convgru_forward(x, CGRU): len_r = len(reset_group) if len_r > 0: yyy = [] if reset_group[0][0] > 0: temp_x = x[0:reset_group[0][0]] xx = temp_x.view(-1, temp_x.size(0), temp_x.size(1), temp_x.size(2), temp_x.size(3)) xxx = CGRU(xx) yyy.append(xxx[1]) for item in reset_group: temp_x = x[item[0]:item[1]] xx = temp_x.view(-1, temp_x.size(0), temp_x.size(1), temp_x.size(2), temp_x.size(3)) xxx = CGRU(xx) yyy.append(xxx[1]) yyy = torch.cat(yyy, 0) yyy = F.relu(yyy, inplace=True) x = yyy.view(x.size()) return x else: xx = x.view(-1, x.size(0), x.size(1), x.size(2), x.size(3)) xxx = CGRU(xx) yyy = xxx[1] yyyy = F.relu(yyy, inplace=True) x = yyyy.view(x.size()) return x
def convgru_forward(x, CGRU, hidden_states, hi): len_r = len(reset_group) if len_r > 0: yyy = [] if reset_group[0][0] > 0: temp_x = x[0:reset_group[0][0]] xx = temp_x.view(-1, temp_x.size(0), temp_x.size(1), temp_x.size(2), temp_x.size(3)) len_hid = len(hidden_states[hi]) if len_hid > 0: temp = [(Variable(hidden_states[hi]).cuda())] xxx = CGRU(xx, temp) else: xxx = CGRU(xx) hidden_states[hi] = xxx[0][0].data yyy.append(xxx[1].data) for item in reset_group: hidden_states[hi] = [] temp_x = x[item[0]:item[1]] xx = temp_x.view(-1, temp_x.size(0), temp_x.size(1), temp_x.size(2), temp_x.size(3)) xxx = CGRU(xx) hidden_states[hi] = xxx[0][0].data yyy.append(xxx[1].data) third_tensor = yyy[0] if len(yyy) > 1: for yitem in yyy[1:]: third_tensor = torch.cat((third_tensor, yitem), 0) yyyy = Variable(third_tensor).cuda() yyyy = F.relu(yyyy, inplace=True) x = yyyy.view(x.size()) return x else: xx = x.view(-1, x.size(0), x.size(1), x.size(2), x.size(3)) len_hid = len(hidden_states[hi]) if len_hid > 0: temp = [(Variable(hidden_states[hi]).cuda())] xxx = CGRU(xx, temp) else: xxx = CGRU(xx) yyy = xxx[1] hidden_states[hi] = xxx[0][0].data yyyy = F.relu(yyy, inplace=True) x = yyyy.view(x.size()) return x
def __init__(self, base, extras, head, num_classes, use_gru = False): super(SSD, self).__init__() self.num_classes = num_classes # TODO: implement __call__ in PriorBox self.priorbox = PriorBox(v2) self.priors = Variable(self.priorbox.forward(), volatile=True) self.num_priors = self.priors.size(0) self.size = 300 self.hidden_states = [[]] # SSD network self.vgg = nn.ModuleList(base) clstm = [CLSTM(512, 512, 3, 1)] cgru = [CGRU(512, 512, 3, 1)] self.clstm = nn.ModuleList(clstm) self.use_gru = use_gru if self.use_gru: self.cgru = nn.ModuleList(cgru) print ("ConvGRU is used") else: print("ConvLSTM is used") # if self.use_gru: # self.hidden_states = [] # Layer learns to scale the l2 normalized features from conv4_3 self.L2Norm = L2Norm(512, 20) self.extras = nn.ModuleList(extras) self.loc = nn.ModuleList(head[0]) self.conf = nn.ModuleList(head[1]) self.softmax = nn.Softmax().cuda()
def multibox(vgg, extra_layers, cfg, num_classes): loc_layers = [] conf_layers = [] vgg_source = [24, -2] for k, v in enumerate(vgg_source): loc_layers += [ nn.Conv2d(vgg[v].out_channels, cfg[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(vgg[v].out_channels, cfg[k] * num_classes, kernel_size=3, padding=1) ] for k, v in enumerate(extra_layers[1::2], 2): loc_layers += [ nn.Conv2d(v.out_channels, cfg[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(v.out_channels, cfg[k] * num_classes, kernel_size=3, padding=1) ] clstm = [CLSTM(512, 512, 3, 1)] cgru = [CGRU(512, 512, 3, 1)] return vgg, extra_layers, clstm, cgru, (loc_layers, conf_layers)