Compare commits

...

2 Commits

Author SHA1 Message Date
ClF3 a484bfa9ad correct generate_proposal 2024-11-19 14:11:54 +08:00
ClF3 36651b22de shared linear 2024-11-19 13:46:49 +08:00
2 changed files with 33 additions and 16 deletions

View File

@ -71,18 +71,16 @@ class FastRCNN(nn.Module):
# hidden_dim -> hidden_dim. #
##############################################################################
# Replace "pass" statement with your code
self.cls_head = nn.Sequential(
self.shared_fc = nn.Sequential(
nn.Linear(in_dim, hidden_dim),
nn.Dropout(drop_ratio),
nn.ReLU(),
nn.Linear(hidden_dim, num_classes+1)
)
self.bbox_head = nn.Sequential(
nn.Linear(in_dim, hidden_dim),
nn.Dropout(drop_ratio),
nn.ReLU(),
nn.Linear(hidden_dim, 4)
nn.Linear(hidden_dim, hidden_dim)
)
self.cls_head = nn.Linear(hidden_dim, self.num_classes+1) # The cls head is a Linear layer that predicts num_classes + 1 (background).
self.bbox_head = nn.Linear(hidden_dim, 4)# The det head is a Linear layer that predicts offsets(dim=4).
##############################################################################
# END OF YOUR CODE #
##############################################################################
@ -139,8 +137,9 @@ class FastRCNN(nn.Module):
# print(feat.shape)
# forward heads, get predicted cls scores & offsets
cls_scores=self.cls_head(feat)
bbox_offsets=self.bbox_head(feat)
shared_feat = self.shared_fc(feat)
cls_scores=self.cls_head(shared_feat)
bbox_offsets=self.bbox_head(shared_feat)
# print(cls_scores.shape, bbox_offsets.shape)
# assign targets with proposals
@ -216,11 +215,11 @@ class FastRCNN(nn.Module):
# perform RoI Pool & mean pool
feat=torchvision.ops.roi_pool(feat, torch.cat((proposal_batch_ids.unsqueeze(1), proposals),dim=1), output_size=(self.roi_output_w, self.roi_output_h))
feat = feat.mean(dim=[2, 3])
shared_feat = self.shared_fc(feat)
# forward heads, get predicted cls scores & offsets
cls_scores = self.cls_head(feat)
cls_scores = self.cls_head(shared_feat)
# print(cls_scores.shape)
bbox_offsets = self.bbox_head(feat)
bbox_offsets = self.bbox_head(shared_feat)
# print(bbox_offsets.shape)
# get predicted boxes & class label & confidence probability
proposals = generate_proposal(proposals, bbox_offsets)

View File

@ -234,9 +234,27 @@ def generate_proposal(anchors, offsets):
# compute the proposal coordinates using the transformation formulas above. #
##############################################################################
# Replace "pass" statement with your code
proposals = torch.zeros_like(anchors)
proposals[:, :2] = anchors[:, :2] + offsets[:, :2] * (anchors[:, 2:4] - anchors[:, :2])
proposals[:, 2:4] = anchors[:, 2:4] * torch.exp(offsets[:, 2:4])
x1, y1, x2, y2 =anchors[:, 0], anchors[:, 1], anchors[:, 2], anchors[:, 3]
pw = x2 - x1
ph = y2 - y1
px = x1 + 0.5 * pw
py = y1 + 0.5 * ph
tx, ty, tw, th = offsets[:, 0], offsets[:, 1], offsets[:, 2], offsets[:, 3]
proposal_x = px + tx * pw
proposal_y = py + ty * ph
proposal_w = pw * torch.exp(tw)
proposal_h = ph * torch.exp(th)
proposal_x1 = proposal_x - 0.5 * proposal_w
proposal_y1 = proposal_y - 0.5 * proposal_h
proposal_x2 = proposal_x + 0.5 * proposal_w
proposal_y2 = proposal_y + 0.5 * proposal_h
proposals = torch.stack((proposal_x1, proposal_y1, proposal_x2, proposal_y2),dim=1)
##############################################################################