示例#1
0
    def predict(self, chips, windows, tmp_dir):
        """Return predictions for a chip using model.

        Args:
            chips: [[height, width, channels], ...] numpy array of chips
            windows: List of boxes that are the windows aligned with the chips.

        Return:
            Labels object containing predictions
        """
        self.load_model(tmp_dir)

        # (batch_size, h, w, nchannels) --> (batch_size, nchannels, h, w)
        chips = torch.Tensor(chips).permute((0, 3, 1, 2)) / 255.
        chips = chips.to(self.device)

        model = self.inf_learner.model.eval()
        preds = model(chips).detach().cpu()

        labels = ChipClassificationLabels()

        for class_probs, window in zip(preds, windows):
            # Add 1 to class_id since they start at 1.
            class_id = int(class_probs.argmax() + 1)

            labels.set_cell(window, class_id, class_probs)

        return labels
示例#2
0
    def predict(self, chips, windows, tmp_dir):
        """Return predictions for a batch of chips.

        Args:
            chips: (numpy.ndarray) of shape (n, height, width, nb_channels)
                containing a batch of chips
            windows: (List<Box>) windows that are aligned with the chips which
                are aligned with the chips.

        Return:
            (ChipClassificationLabels) containing predictions
        """
        self.load_model(tmp_dir)

        # (batch_size, h, w, nchannels) --> (batch_size, nchannels, h, w)
        chips = torch.Tensor(chips).permute((0, 3, 1, 2)) / 255.
        chips = chips.to(self.device)
        model = self.model.eval()

        with torch.no_grad():
            out = model(chips).cpu()
            labels = ChipClassificationLabels()

            for class_probs, window in zip(out, windows):
                # Add 1 to class_id since they start at 1.
                class_id = int(class_probs.argmax() + 1)
                labels.set_cell(window, class_id, class_probs.numpy())

        return labels
    def make_labels(self, class_ids):
        """Make 2x2 grid label store.

        Args:
            class_ids: 2x2 array of class_ids to use
        """
        cell_size = 200
        y_cells = 2
        x_cells = 2
        labels = ChipClassificationLabels()

        for yind in range(y_cells):
            for xind in range(x_cells):
                ymin = yind * cell_size
                xmin = xind * cell_size
                ymax = ymin + cell_size
                xmax = xmin + cell_size
                window = Box(ymin, xmin, ymax, xmax)
                class_id = class_ids[yind][xind]
                new_labels = ChipClassificationLabels()
                new_labels.set_cell(window, class_id)
                labels.extend(new_labels)

        return labels
示例#4
0
 def empty_labels(self):
     return ChipClassificationLabels()