Compare commits
No commits in common. "a484bfa9add460abe5e70a032b647742e634bf15" and "b4eb23917e6168f56faa99749971a963235d5762" have entirely different histories.
a484bfa9ad
...
b4eb23917e
25
model.py
25
model.py
|
@ -71,16 +71,18 @@ class FastRCNN(nn.Module):
|
||||||
# hidden_dim -> hidden_dim. #
|
# hidden_dim -> hidden_dim. #
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# Replace "pass" statement with your code
|
# Replace "pass" statement with your code
|
||||||
self.shared_fc = nn.Sequential(
|
self.cls_head = nn.Sequential(
|
||||||
nn.Linear(in_dim, hidden_dim),
|
nn.Linear(in_dim, hidden_dim),
|
||||||
nn.Dropout(drop_ratio),
|
nn.Dropout(drop_ratio),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
nn.Linear(hidden_dim, hidden_dim)
|
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)
|
||||||
)
|
)
|
||||||
|
|
||||||
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 #
|
# END OF YOUR CODE #
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
@ -137,9 +139,8 @@ class FastRCNN(nn.Module):
|
||||||
# print(feat.shape)
|
# print(feat.shape)
|
||||||
|
|
||||||
# forward heads, get predicted cls scores & offsets
|
# forward heads, get predicted cls scores & offsets
|
||||||
shared_feat = self.shared_fc(feat)
|
cls_scores=self.cls_head(feat)
|
||||||
cls_scores=self.cls_head(shared_feat)
|
bbox_offsets=self.bbox_head(feat)
|
||||||
bbox_offsets=self.bbox_head(shared_feat)
|
|
||||||
# print(cls_scores.shape, bbox_offsets.shape)
|
# print(cls_scores.shape, bbox_offsets.shape)
|
||||||
|
|
||||||
# assign targets with proposals
|
# assign targets with proposals
|
||||||
|
@ -215,11 +216,11 @@ class FastRCNN(nn.Module):
|
||||||
# perform RoI Pool & mean pool
|
# 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=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])
|
feat = feat.mean(dim=[2, 3])
|
||||||
shared_feat = self.shared_fc(feat)
|
|
||||||
# forward heads, get predicted cls scores & offsets
|
# forward heads, get predicted cls scores & offsets
|
||||||
cls_scores = self.cls_head(shared_feat)
|
cls_scores = self.cls_head(feat)
|
||||||
# print(cls_scores.shape)
|
# print(cls_scores.shape)
|
||||||
bbox_offsets = self.bbox_head(shared_feat)
|
bbox_offsets = self.bbox_head(feat)
|
||||||
# print(bbox_offsets.shape)
|
# print(bbox_offsets.shape)
|
||||||
# get predicted boxes & class label & confidence probability
|
# get predicted boxes & class label & confidence probability
|
||||||
proposals = generate_proposal(proposals, bbox_offsets)
|
proposals = generate_proposal(proposals, bbox_offsets)
|
||||||
|
|
24
utils.py
24
utils.py
|
@ -234,27 +234,9 @@ def generate_proposal(anchors, offsets):
|
||||||
# compute the proposal coordinates using the transformation formulas above. #
|
# compute the proposal coordinates using the transformation formulas above. #
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# Replace "pass" statement with your code
|
# Replace "pass" statement with your code
|
||||||
x1, y1, x2, y2 =anchors[:, 0], anchors[:, 1], anchors[:, 2], anchors[:, 3]
|
proposals = torch.zeros_like(anchors)
|
||||||
|
proposals[:, :2] = anchors[:, :2] + offsets[:, :2] * (anchors[:, 2:4] - anchors[:, :2])
|
||||||
pw = x2 - x1
|
proposals[:, 2:4] = anchors[:, 2:4] * torch.exp(offsets[:, 2:4])
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
Loading…
Reference in New Issue