def get_matched_boxes(images, othreshold, cthreshold, target):
    _, T_feature, out_T = T_model(images)
    out_T_test = detect(out_T[0], softmax(out_T[1]), out_T[2])  # [1, num_cls, top_k, 5]
    # print(out_T_test)
    T_ROI, T_score = ROI_gen(out_T_test)
    T_feature = ChannelPool(T_feature)
    out7_T = roi_pooling_2d(Variable(T_feature, requires_grad=False),
                            Variable(T_ROI, requires_grad=False),
                            output_size=(7, 7), spatial_scale=8)
    input_d_T = Variable(transform(out7_T), requires_grad=False)
    cls_match, loc_match, mask, prev_loc, num_match = match_process(out_T_test, target, othreshold, cthreshold,
                                                                    device)

    out_T_test_cls = [] # [match] out_T_test对应标签
    j = 0
    for i in range(sum(mask)):
        j = list(mask).index(1, j+1)
        k = j // top_k - 1
        out_T_test_cls.append(k)
    if num_match == 0:
        raise Exception("no boxes matched")
    cls_match = cls_match.view(-1, 1)
    cls_match = cls_match.long()
    cls_match = cls_match.squeeze(1)[mask]     # [match]
    loc_match = loc_match.view(-1, 4)[mask]    # [match, 4]
    out_T_test = out_T_test.view(-1, 5)[mask]  # [match, 5]
    feature_t = input_d_T.view(-1, input_d_T.size(-3), input_d_T.size(-2), input_d_T.size(-1))[mask]
    _, d_cls_t, d_loc_t = discriminator(feature_t)  # [match, 21], [match, 4]
    d_cls_t = softmax(d_cls_t)
    d_conf, d_cls = d_cls_t.max(1, keepdim=True)
    d_cls = d_cls.squeeze(1)  # [match]
    d_loc = d_loc_t + out_T_test[:, 1:]
    d_loc[d_loc < 0] = 0
    d_loc[d_loc > 1] = 1
    return d_conf, d_cls, d_loc, cls_match, loc_match, out_T_test, out_T_test_cls
Exemple #2
0
    def check_forward(self, x, rois):
        y_var = roi_pooling_2d(x,
                               rois,
                               self.output_size,
                               spatial_scale=self.spatial_scale)
        self.assertIsInstance(y_var.data, torch.cuda.FloatTensor)

        d_output_shape = (self.n_rois, self.n_channels, *self.output_size)
        self.assertEqual(d_output_shape, tuple(y_var.data.size()))
Exemple #3
0
 def check_forward_functional(self, x, rois):
     """crosscheck forward with naive roi_pooling"""
     # Set scale to 1.0 given that casting may be different
     prev_scale = self.spatial_scale
     self.spatial_scale = 1.0
     y_var = roi_pooling_2d(x,
                            rois,
                            self.output_size,
                            spatial_scale=self.spatial_scale)
     d_y = roi_pooling_2d_pytorch(x, rois, self.output_size,
                                  self.spatial_scale)
     np.testing.assert_almost_equal(y_var.data.cpu().numpy(),
                                    d_y.data.cpu().numpy())
     self.spatial_scale = prev_scale
Exemple #4
0
    def forward(self, x, adj, rois, objs):
        # x1 = self.conv11(x)
        x = self.conv1(x)
        # x = self.relu(x+x1)
        # x = self.relu(self.conv12(x)+x)
        x = self.max_pool(x)
        # x2 = self.conv22(x)
        x = self.conv2(x)
        # x = self.relu(x + x2)
        # x = self.relu(self.conv23(x)+x)
        x = self.max_pool(x)
        # x = F.dropout(x, 0.5, training=self.training)

        # x = self.base(x)
        rois = rois * 0.25
        roi_features = roi_pooling_2d(x,
                                      rois,
                                      self.output_size,
                                      spatial_scale=self.spatial_scale)
        # print(roi_features.shape)

        objs = objs.unsqueeze(1)
        # print(objs.shape)
        roi_features = roi_features * objs

        z = self.squeeze(roi_features)
        z = self.sigmoid(z)
        # print(z.shape)
        z = z.view((roi_features.shape[0], -1))

        roi_features = self.gap(roi_features)

        roi_features = roi_features.squeeze(-1).squeeze(-1)

        # print(roi_features.shape, z.shape)

        roi_features = torch.cat((roi_features, z), 1)

        adj = adj.squeeze(0)
        # print(roi_features.shape, adj.shape)
        y = self.gat(roi_features, adj)
        return y
Exemple #5
0
import numpy as np
from roi_pooling.functions.roi_pooling import roi_pooling_2d

# Data parameters and fixed-random ROIs
batch_size = 3
n_channels = 4
input_size = (12, 8)
output_size = (5, 7)
spatial_scale = 0.6
rois = torch.FloatTensor([
    [0, 1, 1, 6, 6],
    [2, 6, 2, 7, 11],
    [1, 3, 1, 5, 10],
    [0, 3, 3, 3, 3]
])

# Generate random input tensor
x_np = np.arange(batch_size * n_channels *
                 input_size[0] * input_size[1],
                 dtype=np.float32)
x_np = x_np.reshape((batch_size, n_channels, *input_size))
np.random.shuffle(x_np)

# torchify and gpu transfer
x = torch.from_numpy(2 * x_np / x_np.size - 1)
x = x.cuda()
rois = rois.cuda()

# Actual ROIpoling operation
y = roi_pooling_2d(x, rois, output_size,
                   spatial_scale=spatial_scale)
Exemple #6
0
                _, S_feature, out_S = S_model(images)
                T_feature, S_feature = Variable(T_feature), Variable(
                    S_feature, requires_grad=True)

                out_T_test = detect(out_T[0], softmax(out_T[1]), out_T[2])
                out_T_test = Variable(out_T_test)

                out_feat_T, out_feat_S, roi = roi_extract(
                    T_feature, S_feature, out_T_test)
                out_feat_T, out_feat_S, roi = Variable(
                    out_feat_T, requires_grad=False), Variable(
                        out_feat_S,
                        requires_grad=True), Variable(roi, requires_grad=False)

                out7_T = roi_pooling_2d(out_feat_T,
                                        roi,
                                        output_size=(7, 7),
                                        spatial_scale=38.0)
                out7_S = roi_pooling_2d(out_feat_S,
                                        roi,
                                        output_size=(7, 7),
                                        spatial_scale=38.0)

                input_d_T = transform(out7_T)
                input_d_T = Variable(input_d_T, requires_grad=False)

                # student inference

                input_d_S = transform(out7_S)
                input_d_S = Variable(input_d_S, requires_grad=True)
                """
                Loss part