| 88 | return loss |
| 89 | |
| 90 | def parse_dynamic_params(params, channels, weight_nums, bias_nums): |
| 91 | assert params.dim() == 2 |
| 92 | assert len(weight_nums) == len(bias_nums) |
| 93 | assert params.size(1) == sum(weight_nums) + sum(bias_nums) |
| 94 | num_instances = params.size(0) |
| 95 | num_layers = len(weight_nums) |
| 96 | |
| 97 | params_splits = list( |
| 98 | torch.split_with_sizes(params, weight_nums + bias_nums, dim=1)) |
| 99 | |
| 100 | weight_splits = params_splits[:num_layers] |
| 101 | bias_splits = params_splits[num_layers:] |
| 102 | |
| 103 | for l in range(num_layers): |
| 104 | if l < num_layers - 1: |
| 105 | # out_channels x in_channels x 1 x 1 |
| 106 | weight_splits[l] = weight_splits[l].reshape( |
| 107 | num_instances * channels, -1, 1, 1) |
| 108 | bias_splits[l] = bias_splits[l].reshape(num_instances * channels) |
| 109 | else: |
| 110 | # out_channels x in_channels x 1 x 1 |
| 111 | weight_splits[l] = weight_splits[l].reshape( |
| 112 | num_instances * 1, -1, 1, 1) |
| 113 | bias_splits[l] = bias_splits[l].reshape(num_instances) |
| 114 | return weight_splits, bias_splits |
| 115 | |
| 116 | class SetCriterionDynamicK(nn.Module): |
| 117 | """ This class computes the loss for DiffusionInst. |