For every example, we will select two images. There are two cases, positive and negative examples. For positive examples, we will have two images from the same class. For negative examples, we will have two images from different classes.
(self, index)
| 116 | return self.data.shape[0] |
| 117 | |
| 118 | def __getitem__(self, index): |
| 119 | """ |
| 120 | For every example, we will select two images. There are two cases, |
| 121 | positive and negative examples. For positive examples, we will have two |
| 122 | images from the same class. For negative examples, we will have two images |
| 123 | from different classes. |
| 124 | |
| 125 | Given an index, if the index is even, we will pick the second image from the same class, |
| 126 | but it won't be the same image we chose for the first class. This is used to ensure the positive |
| 127 | example isn't trivial as the network would easily distinguish the similarity between same images. However, |
| 128 | if the network were given two different images from the same class, the network will need to learn |
| 129 | the similarity between two different images representing the same class. If the index is odd, we will |
| 130 | pick the second image from a different class than the first image. |
| 131 | """ |
| 132 | |
| 133 | # pick some random class for the first image |
| 134 | selected_class = random.randint(0, 9) |
| 135 | |
| 136 | # pick a random index for the first image in the grouped indices based of the label |
| 137 | # of the class |
| 138 | random_index_1 = random.randint(0, self.grouped_examples[selected_class].shape[0]-1) |
| 139 | |
| 140 | # pick the index to get the first image |
| 141 | index_1 = self.grouped_examples[selected_class][random_index_1] |
| 142 | |
| 143 | # get the first image |
| 144 | image_1 = self.data[index_1].clone().float() |
| 145 | |
| 146 | # same class |
| 147 | if index % 2 == 0: |
| 148 | # pick a random index for the second image |
| 149 | random_index_2 = random.randint(0, self.grouped_examples[selected_class].shape[0]-1) |
| 150 | |
| 151 | # ensure that the index of the second image isn't the same as the first image |
| 152 | while random_index_2 == random_index_1: |
| 153 | random_index_2 = random.randint(0, self.grouped_examples[selected_class].shape[0]-1) |
| 154 | |
| 155 | # pick the index to get the second image |
| 156 | index_2 = self.grouped_examples[selected_class][random_index_2] |
| 157 | |
| 158 | # get the second image |
| 159 | image_2 = self.data[index_2].clone().float() |
| 160 | |
| 161 | # set the label for this example to be positive (1) |
| 162 | target = torch.tensor(1, dtype=torch.float) |
| 163 | |
| 164 | # different class |
| 165 | else: |
| 166 | # pick a random class |
| 167 | other_selected_class = random.randint(0, 9) |
| 168 | |
| 169 | # ensure that the class of the second image isn't the same as the first image |
| 170 | while other_selected_class == selected_class: |
| 171 | other_selected_class = random.randint(0, 9) |
| 172 | |
| 173 | |
| 174 | # pick a random index for the second image in the grouped indices based of the label |
| 175 | # of the class |
nothing calls this directly
no outgoing calls
no test coverage detected