| 160 | return x, pad_h, pad_w |
| 161 | |
| 162 | def wrapper(self, x): |
| 163 | batch_size, _, ori_h, ori_w = x.size() |
| 164 | if self.training and self.use_aux: |
| 165 | outputs_all_scales = Variable(torch.zeros((batch_size, self.num_classes, ori_h, ori_w))).cuda() |
| 166 | aux_all_scales = Variable(torch.zeros((batch_size, self.num_classes, ori_h, ori_w))).cuda() |
| 167 | for s in self.scales: |
| 168 | new_size = (int(ori_h * s), int(ori_w * s)) |
| 169 | scaled_x = F.upsample(x, size=new_size, mode='bilinear') |
| 170 | scaled_x = Variable(scaled_x).cuda() |
| 171 | scaled_h, scaled_w = scaled_x.size()[2:] |
| 172 | long_size = max(scaled_h, scaled_w) |
| 173 | print(scaled_x.size()) |
| 174 | |
| 175 | if long_size > self.crop_size: |
| 176 | count = torch.zeros((scaled_h, scaled_w)) |
| 177 | outputs = Variable(torch.zeros((batch_size, self.num_classes, scaled_h, scaled_w))).cuda() |
| 178 | aux_outputs = Variable(torch.zeros((batch_size, self.num_classes, scaled_h, scaled_w))).cuda() |
| 179 | stride = int(ceil(self.crop_size * self.stride_rate)) |
| 180 | h_step_num = int(ceil((scaled_h - self.crop_size) / stride)) + 1 |
| 181 | w_step_num = int(ceil((scaled_w - self.crop_size) / stride)) + 1 |
| 182 | for yy in range(h_step_num): |
| 183 | for xx in range(w_step_num): |
| 184 | sy, sx = yy * stride, xx * stride |
| 185 | ey, ex = sy + self.crop_size, sx + self.crop_size |
| 186 | x_sub = scaled_x[:, :, sy: ey, sx: ex] |
| 187 | x_sub, pad_h, pad_w = _pad(x_sub, self.crop_size) |
| 188 | print(x_sub.size()) |
| 189 | outputs_sub, aux_sub = single_forward(self, x_sub) |
| 190 | |
| 191 | if sy + self.crop_size > scaled_h: |
| 192 | outputs_sub = outputs_sub[:, :, : -pad_h, :] |
| 193 | aux_sub = aux_sub[:, :, : -pad_h, :] |
| 194 | |
| 195 | if sx + self.crop_size > scaled_w: |
| 196 | outputs_sub = outputs_sub[:, :, :, : -pad_w] |
| 197 | aux_sub = aux_sub[:, :, :, : -pad_w] |
| 198 | |
| 199 | outputs[:, :, sy: ey, sx: ex] = outputs_sub |
| 200 | aux_outputs[:, :, sy: ey, sx: ex] = aux_sub |
| 201 | |
| 202 | count[sy: ey, sx: ex] += 1 |
| 203 | count = Variable(count).cuda() |
| 204 | outputs = (outputs / count) |
| 205 | aux_outputs = (outputs / count) |
| 206 | else: |
| 207 | scaled_x, pad_h, pad_w = _pad(scaled_x, self.crop_size) |
| 208 | outputs, aux_outputs = single_forward(self, scaled_x) |
| 209 | outputs = outputs[:, :, : -pad_h, : -pad_w] |
| 210 | aux_outputs = aux_outputs[:, :, : -pad_h, : -pad_w] |
| 211 | outputs_all_scales += outputs |
| 212 | aux_all_scales += aux_outputs |
| 213 | return outputs_all_scales / len(self.scales), aux_all_scales |
| 214 | else: |
| 215 | outputs_all_scales = Variable(torch.zeros((batch_size, self.num_classes, ori_h, ori_w))).cuda() |
| 216 | for s in self.scales: |
| 217 | new_size = (int(ori_h * s), int(ori_w * s)) |
| 218 | scaled_x = F.upsample(x, size=new_size, mode='bilinear') |
| 219 | scaled_h, scaled_w = scaled_x.size()[2:] |