| 169 | } |
| 170 | |
| 171 | std::vector<torch::Tensor> YOLOLossImpl::forward(torch::Tensor input, std::vector<torch::Tensor> targets) |
| 172 | { |
| 173 | //inputΪbs, 3 * (5 + num_classes), 13, 13 |
| 174 | //һ��������ͼƬ |
| 175 | auto bs = input.size(0); |
| 176 | //������ĸ� |
| 177 | auto in_h = input.size(2); |
| 178 | //������Ŀ� |
| 179 | auto in_w = input.size(3); |
| 180 | |
| 181 | //���㲽����ÿһ���������Ӧԭ����ͼƬ�϶��ٸ����ص� |
| 182 | //���������Ϊ13x13�Ļ���ԭͼ416x416������£�һ��������Ͷ�Ӧԭ����ͼƬ�ϵ�32�����ص� |
| 183 | auto stride_h = image_size[1] / in_h; |
| 184 | auto stride_w = image_size[0] / in_w; |
| 185 | |
| 186 | //�������ijߴ�������������С����ʽ |
| 187 | //�������������������϶�Ӧ�Ŀ��� |
| 188 | auto scaled_anchors = anchors.clone(); |
| 189 | scaled_anchors.select(1, 0) = scaled_anchors.select(1, 0) / stride_w; |
| 190 | scaled_anchors.select(1, 1) = scaled_anchors.select(1, 1) / stride_h; |
| 191 | |
| 192 | //bs, 3 * (5 + num_classes), 13, 13->bs, 3, 13, 13, (5 + num_classes) |
| 193 | auto prediction = input.view({bs, int(num_anchors / 2), bbox_attrs, in_h, in_w}).permute({0, 1, 3, 4, 2}).contiguous(); |
| 194 | //��predictionԤ����е��� |
| 195 | auto conf = torch::sigmoid(prediction.select(-1,4));// # Conf |
| 196 | auto pred_cls = torch::sigmoid(prediction.narrow(-1, 5, num_classes)); //Cls pred. |
| 197 | |
| 198 | //�ҵ���Щ������ڲ��������� |
| 199 | auto temp = get_target(targets, scaled_anchors, in_w, in_h, ignore_threshold); |
| 200 | auto BoolType = torch::ones(1).to(torch::kBool).to(device).options(); |
| 201 | auto FloatType = torch::ones(1).to(torch::kFloat).to(device).options(); |
| 202 | auto mask = temp[0].to(BoolType); |
| 203 | auto noobj_mask = temp[1].to(device); |
| 204 | auto t_box = temp[2]; |
| 205 | auto tconf = temp[3]; |
| 206 | auto tcls = temp[4]; |
| 207 | auto box_loss_scale_x = temp[5]; |
| 208 | auto box_loss_scale_y = temp[6]; |
| 209 | |
| 210 | auto temp_ciou = get_ignore(prediction, targets, scaled_anchors, in_w, in_h, noobj_mask); |
| 211 | noobj_mask = temp_ciou[0]; |
| 212 | auto pred_boxes_for_ciou = temp_ciou[1]; |
| 213 | |
| 214 | |
| 215 | mask = mask.to(device); |
| 216 | noobj_mask = noobj_mask.to(device); |
| 217 | box_loss_scale_x = box_loss_scale_x.to(device); |
| 218 | box_loss_scale_y = box_loss_scale_y.to(device); |
| 219 | tconf = tconf.to(device); |
| 220 | tcls = tcls.to(device); |
| 221 | pred_boxes_for_ciou = pred_boxes_for_ciou.to(device); |
| 222 | t_box = t_box.to(device); |
| 223 | |
| 224 | |
| 225 | auto box_loss_scale = 2 - box_loss_scale_x * box_loss_scale_y; |
| 226 | auto ciou = (1 - box_ciou(pred_boxes_for_ciou.index({mask}), t_box.index({ mask })))* box_loss_scale.index({ mask }); |
| 227 | auto loss_loc = torch::sum(ciou / bs); |
| 228 |
nothing calls this directly
no test coverage detected