Hi. I’m implementing Faster R-CNN from scratch and am having gradient issues. My cls_loc layer in my pooling layer has no grad, despite its requires_grad = True. After looking over my code I cannot find where the graph is broken or anything, but my model can’t update. Any help on this would be greatly appreciated. Here’s the module as it is now:
class PoolingCNN(nn.Module):
def init(self,
           sample_layer_params = {

                  "n_sample": 128,

                  "pos_ratio": 0.25,

                  "pos_iou_thresh": 0.25,

                  "neg_iou_thresh": 0.5,

                  "neg_iou_thresh_hi": 0.5,

                  "neg_iou_thresh_lo": 0.0,

                  "mode": "train"

                },

           ):



super(PoolingCNN, self).__init__()

self.roi_head_classifier = nn.Sequential(*[nn.Linear(25088, 4096),

                                  nn.Linear(4096, 4096)])

self.cls_loc = nn.Linear(4096, 2 * 4) # (VOC 20 classes + 1 background. Each will have 4 co-ordinates)

self.cls_loc.weight.data.normal_(0, 0.01)

self.cls_loc.bias.data.zero_()

self.score = nn.Linear(4096, 2) # (VOC 20 classes + 1 background)

def forward(self, pred_bounds, gt_bounds, labels, out_map):
gt_roi_labels, gt_roi_locs, sample_roi = sample_rois(gt_bounds, pred_bounds, labels)

k = pooling_layer(sample_roi, out_map)

print(k.shape)

#print(k.type)

k = self.roi_head_classifier(k)

print(k.shape)

roi_cls_loc = self.cls_loc(k)

roi_cls_score = self.score(k)

return gt_roi_labels, gt_roi_locs, roi_cls_loc, roi_cls_score

def predict(self, out_map, roi):
#print(roi.shape)

k = pooling_layer(roi, out_map)

k = self.roi_head_classifier(k)

roi_cls_loc = self.cls_loc(k)

roi_cls_score = self.score(k)

index = np.array(roi_cls_score.detach().argmax(axis=1))

#print("index", index)

#

corr_bound = np.asarray([roi_cls_loc[i, (index[i]*4):(index[i]*4+4)].detach().numpy() for i in range(len(roi_cls_loc))])

#for i in range(len(roi_cls_loc)):

#  print(roi_cls_loc[i, (index[i]*4):(index[i]*4+4)])

#print(corr_bound.shape)



#print(corr_bound)

bboxes = roi_to_bbox(np.asarray(roi), corr_bound)

#print(bboxes)

return roi_cls_loc, roi_cls_score, bboxes