コード例 #1
0
    def extract_features(self, dataset, sampler, cut_layer, epsilon=None):
        """Extracting features using layers before the cut_layer.

        dataset: The training or testing dataset.
        cut_layer: Layers before this one will be used for extracting features.
        epsilon: If epsilon is not None, local differential privacy should be
                applied to the features extracted.
        """
        self.model.set_train(False)

        tic = time.perf_counter()

        feature_dataset = []

        for inputs, targets in dataset:
            inputs = mindspore.Tensor(inputs)
            targets = mindspore.Tensor(targets)

            logits = self.model.forward_to(inputs, cut_layer)

            if epsilon is not None:
                logits = logits.asnumpy()
                logits = unary_encoding.encode(logits)
                logits = unary_encoding.randomize(logits, epsilon)
                logits = mindspore.Tensor(logits.astype('float32'))

            feature_dataset.append((logits.asnumpy(), targets.asnumpy()))

        toc = time.perf_counter()
        logging.info("[Client #%d] Features extracted from %s examples.",
                     self.client_id, len(feature_dataset))
        logging.info("[Client #{}] Time used: {:.2f} seconds.".format(
            self.client_id, toc - tic))

        return feature_dataset
コード例 #2
0
    def _process_layer(self, layer: torch.Tensor) -> torch.Tensor:

        if Config().algorithm.epsilon is None:
            return layer

        epsilon = Config().algorithm.epsilon

        layer = layer.detach().cpu().numpy()

        layer = unary_encoding.encode(layer)
        layer = unary_encoding.randomize(layer, epsilon)

        layer = torch.tensor(layer, dtype=torch.float32)

        return layer
コード例 #3
0
        def func(logits, targets):
            logits = unary_encoding.encode(logits)

            if Config().algorithm.epsilon is None:
                return logits, targets

            _randomize = getattr(self.trainer, "randomize", None)
            epsilon = Config().algorithm.epsilon

            if callable(_randomize):
                logits = self.trainer.randomize(logits, targets, epsilon)
            else:
                logits = unary_encoding.randomize(logits, epsilon)

            return logits, targets
コード例 #4
0
ファイル: yolo.py プロジェクト: TL-System/plato
    def test_model(self, config, testset):  # pylint: disable=unused-argument
        """The testing loop for YOLOv5.

        Arguments:
            config: Configuration parameters as a dictionary.
            testset: The test dataset.
        """
        assert Config().data.datasource == 'YOLO'
        test_loader = yolo.DataSource.get_test_loader(config['batch_size'],
                                                      testset)

        device = next(self.model.parameters()).device  # get model device

        # Configure
        self.model.eval()
        with open(Config().data.data_params) as f:
            data = yaml.load(f, Loader=yaml.SafeLoader)  # model dict
        check_dataset(data)  # check
        nc = Config().data.num_classes  # number of classes
        iouv = torch.linspace(0.5, 0.95,
                              10).to(device)  # iou vector for [email protected]:0.95
        niou = iouv.numel()

        seen = 0
        names = {
            k: v
            for k, v in enumerate(self.model.names if hasattr(
                self.model, 'names') else self.model.module.names)
        }
        s = ('%20s' + '%11s' * 6) % ('Class', 'Images', 'Labels', 'P', 'R',
                                     '[email protected]', '[email protected]:.95')
        dt, p, r, __, mp, mr, map50, map = [
            0.0, 0.0, 0.0
        ], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
        stats, ap = [], []
        pbar = tqdm(
            test_loader,
            desc=s,
            ncols=NCOLS,
            bar_format='{l_bar}{bar:10}{r_bar}{bar:-10b}')  # progress bar

        for __, (img, targets, paths, shapes) in enumerate(pbar):
            t1 = time_sync()
            img = img.to(device, non_blocking=True).float()
            img /= 255.0  # 0 - 255 to 0.0 - 1.0
            targets = targets.to(device)
            __, __, height, width = img.shape  # batch size, channels, height, width
            t2 = time_sync()
            dt[0] += t2 - t1

            with torch.no_grad():
                # Run model
                if Config().algorithm.type == 'mistnet':
                    logits = self.model.forward_to(img)
                    logits = logits.cpu().detach().numpy()
                    logits = unary_encoding.encode(logits)
                    logits = torch.from_numpy(logits.astype('float32'))
                    out, __ = self.model.forward_from(logits.to(device))
                else:
                    out, __ = self.model(img)

                # Run NMS
                targets[:,
                        2:] *= torch.Tensor([width, height, width,
                                             height]).to(device)  # to pixels

                lb = []  # for autolabelling
                out = non_max_suppression(out,
                                          conf_thres=0.001,
                                          iou_thres=0.6,
                                          labels=lb,
                                          multi_label=True)

            # Metrics
            for si, pred in enumerate(out):
                labels = targets[targets[:, 0] == si, 1:]
                nl = len(labels)
                tcls = labels[:, 0].tolist() if nl else []  # target class
                __, shape = Path(paths[si]), shapes[si][0]
                seen += 1

                if len(pred) == 0:
                    if nl:
                        stats.append((torch.zeros(0, niou, dtype=torch.bool),
                                      torch.Tensor(), torch.Tensor(), tcls))
                    continue

                # Predictions
                predn = pred.clone()
                scale_coords(img[si].shape[1:], predn[:, :4], shape,
                             shapes[si][1])  # native-space pred

                # Evaluate
                if nl:
                    tbox = xywh2xyxy(labels[:, 1:5])  # target boxes
                    scale_coords(img[si].shape[1:], tbox, shape,
                                 shapes[si][1])  # native-space labels
                    labelsn = torch.cat((labels[:, 0:1], tbox),
                                        1)  # native-space labels
                    correct = self.process_batch(predn, labelsn, iouv)
                else:
                    correct = torch.zeros(pred.shape[0],
                                          niou,
                                          dtype=torch.bool)
                stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:,
                                                                    5].cpu(),
                              tcls))  # (correct, conf, pcls, tcls)

        # Compute metrics
        stats = [np.concatenate(x, 0) for x in zip(*stats)]  # to numpy
        if len(stats) and stats[0].any():
            __, __, p, r, __, ap, __ = ap_per_class(*stats,
                                                    plot=False,
                                                    save_dir='',
                                                    names=names)
            ap50, ap = ap[:, 0], ap.mean(1)  # [email protected], [email protected]:0.95
            mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
            nt = np.bincount(stats[3].astype(np.int64),
                             minlength=nc)  # number of targets per class
        else:
            nt = torch.zeros(1)

        # Print results
        pf = '%20s' + '%11i' * 2 + '%11.3g' * 4  # print format
        print(pf % ('all', seen, nt.sum(), mp, mr, map50, map))

        return map50
コード例 #5
0
    def extract_features(self, dataset, sampler, cut_layer: str, epsilon=None):
        """Extracting features using layers before the cut_layer.

        dataset: The training or testing dataset. This datasets does not based on
                torch.utils.data.Datasets.
        cut_layer: Layers before this one will be used for extracting features.
                TODO: This cannot be changed dynamically due to the static properties of OM file.
        epsilon: If epsilon is not None, local differential privacy should be
                applied to the features extracted.
        """

        tic = time.perf_counter()

        feature_dataset = []

        _randomize = getattr(self.trainer, "randomize", None)

        features_shape = self.features_shape()

        check_features = []
        step = 0

        for inputs, targets, *__ in dataset:
            assert inputs.shape[1] == Config().data.input_height and inputs.shape[2] == Config().data.input_width, \
                "The input shape is not consistent with the requirement predefined model."
            step += 1
            cv2.imwrite("./image{}.jpg".format(step),
                        np.moveaxis(inputs, 0, -1))
            inputs = inputs.astype(np.float32)
            inputs = inputs / 255.0  # normalize image and convert image type at the same time
            logits = self.model.forward(inputs)
            logits = np.reshape(logits, features_shape)
            check_features.append(logits)
            targets = np.expand_dims(
                targets, axis=0
            )  # add batch axis to make sure self.train.randomize correct
            if epsilon is not None:
                logits = unary_encoding.encode(logits)
                if callable(_randomize):
                    logits = self.trainer.randomize(logits, targets, epsilon)
                else:
                    logits = unary_encoding.randomize(logits, epsilon)
                # Pytorch is currently not supported on A500 and we cannot convert
                # numpy array to tensor
                if self.trainer.device != 'cpu':
                    logits = logits.astype('float16')
                else:
                    logits = logits.astype('float32')

            for i in np.arange(logits.shape[0]):  # each sample in the batch
                feature_dataset.append((logits[i], targets[i]))

        toc = time.perf_counter()
        logging.info("[Client #%d] Features extracted from %s examples.",
                     self.client_id, len(feature_dataset))
        logging.info("[Client #{}] Time used: {:.2f} seconds.".format(
            self.client_id, toc - tic))

        save_features = np.array(check_features)
        print("save feature shapes ", save_features.shape)
        np.save("./cutlayer4_features.npy", save_features)
        return feature_dataset