def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            feat_half1 (np.array): features for the 1st half.
            label_half1 (np.array): labels (one-hot) for the 1st half.
        """

        # Retrieve the game index and the anchor
        game_index = self.game_anchors[index][0]
        anchor = self.game_anchors[index][1]

        # Compute the shift
        shift = np.random.randint(-self.chunk_size + self.receptive_field,
                                  -self.receptive_field)
        start = anchor + shift
        if start < 0:
            start = 0
        if start + self.chunk_size >= self.game_feats[game_index].shape[0]:
            start = self.game_feats[game_index].shape[0] - self.chunk_size - 1

        # Extract the clips
        clip_feat = self.game_feats[game_index][start:start + self.chunk_size]
        clip_labels = self.game_labels[game_index][start:start +
                                                   self.chunk_size]
        clip_change_labels = self.game_change_labels[game_index][start:start +
                                                                 self.
                                                                 chunk_size]

        # Put loss to zero outside receptive field
        # clip_labels[0:int(np.ceil(self.receptive_field/2)),:] = -1
        # clip_labels[-int(np.ceil(self.receptive_field/2)):,:] = -1

        # clip_change_labels[0:int(np.ceil(self.receptive_field/2)),:] = -1
        # clip_change_labels[-int(np.ceil(self.receptive_field/2)):,:] = -1

        # Get the spotting target
        #print(clip_change_labels.shape)
        clip_targets = getTimestampTargets(np.array([clip_change_labels]),
                                           self.num_detections)[0]
        #clip_targets =clip_change_labels
        #print(clip_targets.shape)
        return torch.from_numpy(clip_feat.copy()), torch.from_numpy(
            clip_labels.copy()), torch.from_numpy(clip_targets.copy())
Beispiel #2
0
    def __getitem__(self, index):

        # Retrieve the game index and the anchor
        class_selection = random.randint(0, self.num_classes)
        event_selection = random.randint(
            0,
            len(self.game_anchors[class_selection]) - 1)
        game_index = self.game_anchors[class_selection][event_selection][0]
        anchor = self.game_anchors[class_selection][event_selection][1]

        # Compute the shift for event chunks
        if class_selection < self.num_classes:
            shift = np.random.randint(-self.chunk_size + self.receptive_field,
                                      -self.receptive_field)
            start = anchor + shift
        # Compute the shift for non-event chunks
        else:
            start = random.randint(anchor[0], anchor[1] - self.chunk_size)
        if start < 0:
            start = 0
        if start + self.chunk_size >= self.game_feats[game_index].shape[0]:
            start = self.game_feats[game_index].shape[0] - self.chunk_size - 1

        # Extract the clips
        clip_feat = self.game_feats[game_index][start:start + self.chunk_size]
        clip_labels = self.game_labels[game_index][start:start +
                                                   self.chunk_size]

        # Put loss to zero outside receptive field
        clip_labels[0:int(np.ceil(self.receptive_field / 2)), :] = -1
        clip_labels[-int(np.ceil(self.receptive_field / 2)):, :] = -1

        # Get the spotting target
        clip_targets = getTimestampTargets(np.array([clip_labels]),
                                           self.num_detections)[0]

        return torch.from_numpy(clip_feat), torch.from_numpy(
            clip_labels), torch.from_numpy(clip_targets)
Beispiel #3
0
    def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            clip_feat (np.array): clip of features.
            clip_labels (np.array): clip of labels for the segmentation.
            clip_targets (np.array): clip of targets for the spotting.
        """
        clip_all = list()
        replay_clip_all = list()
        clip_target_all = list()
        replay_mask_all = list()

        # First retrieve a replay with its action
        for iloop in np.arange(self.loop):

            replay_anchor = self.replay_anchors[index]
            game_index = replay_anchor[0]
            replay_sequence_start = replay_anchor[1]
            replay_sequence_stop = replay_anchor[2]
            event_anchor = replay_anchor[3]
            event_anchor_class = replay_anchor[4]

            TSE_labels = np.arange(
                self.game_feats[game_index].shape[0]) - event_anchor

            # Load the replay chunk
            # [REPLAY_LOADING] THE FOLLOWING SET OF LINES COULD AND SHOULD BE CHANGED
            replay_clip = np.zeros(
                (self.chunk_size, self.game_feats[game_index].shape[-1]),
                dtype=self.game_feats[game_index].dtype)
            # Make sure that it is not > chunk_size
            replay_chunk_small = self.game_feats[game_index][
                replay_sequence_start:min(
                    replay_sequence_stop, replay_sequence_start +
                    self.chunk_size)]
            replay_clip, replay_mask = replay_size_maker(
                replay_chunk_small, self.replay_size)
            # replay_size = len(replay_chunk_small)
            # fill_start = 0
            # while fill_start + replay_size < self.chunk_size:
            #     replay_clip[fill_start:fill_start+replay_size] = replay_chunk_small
            #     fill_start += replay_size
            """
            replay_clip[0:replay_size] = replay_chunk_small
            """

            selection = np.random.randint(0, 2)

            start = 0
            # Load a positive chunk
            if iloop in np.arange(1):
                shift = np.random.randint(
                    -self.chunk_size + self.receptive_field,
                    -self.receptive_field)
                start = event_anchor + shift

            # Load a negative chunk
            if iloop in np.arange(1, self.loop):
                list_events = self.game_anchors[game_index][event_anchor_class]
                selection_2 = np.random.randint(0, 100)
                # Take one of the same class
                if len(list_events) > 0 and iloop in np.arange(
                        2, int(self.loop * self.hard_negative_weight)):
                    event_selection = random.randint(0, len(list_events) - 1)
                    anchor = list_events[event_selection][1]
                    shift = np.random.randint(
                        -self.chunk_size + self.receptive_field,
                        -self.receptive_field)
                    start = anchor + shift
                # Take one randomly from the game
                else:
                    start = random.randint(
                        0, self.game_feats[game_index].shape[0] - 1)

            # Make sure that the chunk does not go out of the video.
            if start < 0:
                start = 0
            if start + self.chunk_size >= self.game_feats[game_index].shape[0]:
                start = self.game_feats[game_index].shape[
                    0] - self.chunk_size - 1

            clip = self.game_feats[game_index][start:start + self.chunk_size]
            clip_TSE = TSE_labels[start:start + self.chunk_size]
            clip_target = getTimestampTargets(clip_TSE)

            clip_all.append(torch.from_numpy(clip).unsqueeze(0))
            replay_clip_all.append(torch.from_numpy(replay_clip).unsqueeze(0))
            clip_target_all.append(torch.from_numpy(clip_target))
            replay_mask_all.append(torch.from_numpy(replay_mask))

        # adaptive_pool = nn.AdaptiveMaxPool2d((30,512))
        # adaptive_pool = nn.AdaptiveAvgPool2d((30,512))
        # clip_stacked = np.stack([clip, replay_clip], axis=0)

        # return adaptive_pool(torch.from_numpy(clip).unsqueeze(0)), adaptive_pool(torch.from_numpy(replay_clip).unsqueeze(0)), torch.from_numpy(clip_target)
        return clip_all, replay_clip_all, clip_target_all, replay_mask_all
    def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            clip_feat (np.array): clip of features.
            clip_labels (np.array): clip of labels for the segmentation.
            clip_targets (np.array): clip of targets for the spotting.
        """

        # First retrieve a replay with its action
        replay_anchor = self.replay_anchors[index]
        game_index = replay_anchor[0]
        replay_sequence_start = replay_anchor[1]
        replay_sequence_stop = replay_anchor[2]
        event_anchor = replay_anchor[3]
        event_anchor_class = replay_anchor[4]

        TSE_labels = np.arange(
            self.game_feats[game_index].shape[0]) - event_anchor

        # Load the replay chunk
        # [REPLAY_LOADING] THE FOLLOWING SET OF LINES COULD AND SHOULD BE CHANGED
        replay_clip = np.zeros(
            (self.chunk_size, self.game_feats[game_index].shape[-1]),
            dtype=self.game_feats[game_index].dtype)
        # Make sure that it is not > chunk_size
        replay_chunk_small = self.game_feats[
            game_index][replay_sequence_start:min(
                replay_sequence_stop, replay_sequence_start + self.chunk_size)]
        replay_size = len(replay_chunk_small)
        fill_start = 0
        while fill_start + replay_size < self.chunk_size:
            replay_clip[fill_start:fill_start +
                        replay_size] = replay_chunk_small
            fill_start += replay_size
        """
        replay_clip[0:replay_size] = replay_chunk_small
        """

        selection = np.random.randint(0, 2)

        start = 0
        # Load a positive chunk
        if selection == 0:
            shift = np.random.randint(-self.chunk_size + self.receptive_field,
                                      -self.receptive_field)
            start = event_anchor + shift

        # Load a negative chunk
        if selection == 1:
            list_events = self.game_anchors[game_index][event_anchor_class]
            selection_2 = np.random.randint(0, 2)
            # Take one of the same class
            if len(list_events) > 0 and selection_2 == 0:
                event_selection = random.randint(0, len(list_events) - 1)
                anchor = list_events[event_selection][1]
                shift = np.random.randint(
                    -self.chunk_size + self.receptive_field,
                    -self.receptive_field)
                start = anchor + shift
            # Take one randomly from the game
            else:
                start = random.randint(
                    0, self.game_feats[game_index].shape[0] - 1)

        # Make sure that the chunk does not go out of the video.
        if start < 0:
            start = 0
        if start + self.chunk_size >= self.game_feats[game_index].shape[0]:
            start = self.game_feats[game_index].shape[0] - self.chunk_size - 1

        clip = self.game_feats[game_index][start:start + self.chunk_size]
        clip_TSE = TSE_labels[start:start + self.chunk_size]
        clip_target = getTimestampTargets(clip_TSE)

        clip_stacked = np.stack([clip, replay_clip], axis=0)

        return torch.from_numpy(clip_stacked), torch.from_numpy(
            np.expand_dims(clip_TSE, axis=-1)), torch.from_numpy(clip_target)