| 1016 | |
| 1017 | |
| 1018 | class SelfReg(ERM): |
| 1019 | |
| 1020 | def __init__(self, input_shape, num_classes, num_domains, hparams): |
| 1021 | super(SelfReg, self).__init__(input_shape, num_classes, num_domains, hparams) |
| 1022 | self.num_classes = num_classes |
| 1023 | self.MSEloss = nn.MSELoss() |
| 1024 | input_feat_size = self.featurizer.n_outputs |
| 1025 | hidden_size = input_feat_size if input_feat_size == 2048 else input_feat_size * 2 |
| 1026 | |
| 1027 | self.cdpl = nn.Sequential( |
| 1028 | nn.Linear(input_feat_size, hidden_size), nn.BatchNorm1d(hidden_size), |
| 1029 | nn.ReLU(inplace=True), nn.Linear(hidden_size, hidden_size), nn.BatchNorm1d(hidden_size), |
| 1030 | nn.ReLU(inplace=True), nn.Linear(hidden_size, input_feat_size), |
| 1031 | nn.BatchNorm1d(input_feat_size) |
| 1032 | ) |
| 1033 | |
| 1034 | def update(self, minibatches, unlabeled=None): |
| 1035 | |
| 1036 | all_x = torch.cat([x for x, y in minibatches]) |
| 1037 | all_y = torch.cat([y for _, y in minibatches]) |
| 1038 | |
| 1039 | lam = np.random.beta(0.5, 0.5) |
| 1040 | |
| 1041 | batch_size = all_y.size()[0] |
| 1042 | |
| 1043 | # cluster and order features into same-class group |
| 1044 | with torch.no_grad(): |
| 1045 | sorted_y, indices = torch.sort(all_y) |
| 1046 | sorted_x = torch.zeros_like(all_x) |
| 1047 | for idx, order in enumerate(indices): |
| 1048 | sorted_x[idx] = all_x[order] |
| 1049 | intervals = [] |
| 1050 | ex = 0 |
| 1051 | for idx, val in enumerate(sorted_y): |
| 1052 | if ex == val: |
| 1053 | continue |
| 1054 | intervals.append(idx) |
| 1055 | ex = val |
| 1056 | intervals.append(batch_size) |
| 1057 | |
| 1058 | all_x = sorted_x |
| 1059 | all_y = sorted_y |
| 1060 | |
| 1061 | feat = self.featurizer(all_x) |
| 1062 | proj = self.cdpl(feat) |
| 1063 | |
| 1064 | output = self.classifier(feat) |
| 1065 | |
| 1066 | # shuffle |
| 1067 | output_2 = torch.zeros_like(output) |
| 1068 | feat_2 = torch.zeros_like(proj) |
| 1069 | output_3 = torch.zeros_like(output) |
| 1070 | feat_3 = torch.zeros_like(proj) |
| 1071 | ex = 0 |
| 1072 | for end in intervals: |
| 1073 | shuffle_indices = torch.randperm(end - ex) + ex |
| 1074 | shuffle_indices2 = torch.randperm(end - ex) + ex |
| 1075 | for idx in range(end - ex): |
nothing calls this directly
no outgoing calls
no test coverage detected