Esempio n. 1
0
    def individual_param_quadratic(self, test_loader, layer, idxs):
        """
        Method interpolates individual parameter of the model and evaluates the performance of the model when the
        interpolated parameter replaces its original in the parameters of the model

        :param test_loader: test dataset loader
        :param layer: layer of parameter
        :param idxs: position of parameter
        """

        loss_res = Path("{}_{}_{}_q".format(paths.svloss_path, layer,
                                            convert_list2str(idxs)))
        loss_img = Path("{}_{}_{}_q".format(paths.svloss_img_path, layer,
                                            convert_list2str(idxs)))

        acc_res = Path("{}_{}_{}_q".format(paths.sacc_path, layer,
                                           convert_list2str(idxs)))
        acc_img = Path("{}_{}_{}_q".format(paths.sacc_img_path, layer,
                                           convert_list2str(idxs)))

        logger.debug(f"Result files:\n" f"{loss_res}\n" f"{acc_res}\n")
        logger.debug(f"Img files:\n" f"{loss_img}\n" f"{acc_img}\n")

        if not loss_res.exists() or not acc_res.exists():
            logger.debug(
                "Files with results not found - beginning interpolation.")

            v_loss_list = []
            acc_list = []

            start_a = 0
            mid_a = 0.5
            end_a = 1
            logger.debug(f"Start: {start_a}\n"
                         f"Mid: {mid_a}\n"
                         f"End: {end_a}")

            mid_check = self.__get_mid_point(paths.checkpoints)

            start_p = self.theta_i[layer + ".weight"][idxs].cpu()
            mid_p = copy.deepcopy(
                torch.load(Path(
                    os.path.join(paths.checkpoints,
                                 mid_check))))[layer + ".weight"][idxs].cpu()
            end_p = self.theta_f[layer + ".weight"][idxs].cpu()

            logger.debug(f"Start loss: {start_p}\n"
                         f"Mid loss: {mid_p}\n"
                         f"End loss: {end_p}")

            start = [start_a, start_p]
            mid = [mid_a, mid_p]
            end = [end_a, end_p]
            logger.debug(f"Start: {start}\n" f"Mid: {mid}\n" f"End: {end}")

            self.model.load_state_dict(self.theta_f)
            for alpha_act in tqdm(
                    self.alpha,
                    desc=f"Parameter {layer}/{idxs} Level Quadratic",
                    dynamic_ncols=True):
                self.__calc_theta_single_q(layer + ".weight", idxs, alpha_act,
                                           start, mid, end)

                self.model.load_state_dict(self.theta)

                logger.debug(
                    f"Getting validation loss and accuracy for alpha = {alpha_act}"
                )
                val_loss, acc = net.test(self.model, test_loader, self.device)
                acc_list.append(acc)
                v_loss_list.append(val_loss)

            logger.debug(f"Saving results to files ({loss_res}, {acc_res})")

            np.savetxt(loss_res, v_loss_list)
            np.savetxt(acc_res, acc_list)
            self.model.load_state_dict(self.theta_f)

        logger.debug(f"Saving results to figures {loss_img}, {acc_img} ...")
        plot.plot_metric(self.alpha, np.loadtxt(loss_res), loss_img, "loss")
        plot.plot_metric(self.alpha, np.loadtxt(acc_res), acc_img, "acc")

        self.model.load_state_dict(self.theta_f)

        return
Esempio n. 2
0
    def layers_quadratic(self, test_loader, layer):
        """
        Method examines the parameters on the level of layers using the quadratic interpolation.

        :param test_loader: test data set loader
        :param layer: layer to be examined
        """
        loss_res = Path("{}_{}_q".format(paths.vvloss_path, layer))
        loss_img = Path("{}_{}_q".format(paths.vvloss_img_path, layer))

        acc_res = Path("{}_{}_q".format(paths.vacc_path, layer))
        acc_img = Path("{}_{}_q".format(paths.vacc_img_path, layer))

        logger.debug(f"Result files:\n" f"{loss_res}\n" f"{acc_res}")
        logger.debug(f"Img files:\n" f"{loss_img}\n" f"{acc_img}")

        if not loss_res.exists() or not acc_res.exists():
            logger.debug("Result files not found - beginning interpolation.")

            v_loss_list = []
            acc_list = []

            start_a = 0
            mid_a = 0.5
            end_a = 1
            logger.debug(f"Start: {start_a}\n"
                         f"Mid: {mid_a}\n"
                         f"End: {end_a}")

            mid_check = self.__get_mid_point(paths.checkpoints)

            start_p = self.theta_i[layer + ".weight"].cpu()
            mid_p = copy.deepcopy(
                torch.load(os.path.join(paths.checkpoints,
                                        mid_check))[layer + ".weight"]).cpu()
            end_p = self.theta_f[layer + ".weight"].cpu()

            start_w = [start_a, start_p]
            mid_w = [mid_a, mid_p]
            end_w = [end_a, end_p]

            start_pb = self.theta_i[layer + ".bias"].cpu()
            mid_pb = copy.deepcopy(
                torch.load(os.path.join(
                    paths.checkpoints,
                    mid_check))[layer + ".bias"]).cpu()  # TODO AUTO MID
            end_pb = self.theta_f[layer + ".bias"].cpu()

            start_b = [start_a, start_pb]
            mid_b = [mid_a, mid_pb]
            end_b = [end_a, end_pb]

            for alpha_act in tqdm(self.alpha,
                                  desc=f"Layer {layer} Level Quadratic Path",
                                  dynamic_ncols=True):
                self.__calc_theta_vec_q(layer + ".weight", alpha_act, start_w,
                                        mid_w, end_w)
                self.__calc_theta_vec_q(layer + ".bias", alpha_act, start_b,
                                        mid_b, end_b)

                self.model.load_state_dict(self.theta)
                logger.debug(
                    f"Getting validation loss and accuracy for alpha = {alpha_act}"
                )

                vloss, acc = net.test(self.model, test_loader, self.device)
                v_loss_list.append(vloss)
                acc_list.append(acc)

            logger.debug(f"Saving results to files ({loss_res}, {acc_res})")
            np.savetxt(loss_res, v_loss_list)
            np.savetxt(acc_res, acc_list)

        logger.debug(f"Saving results to figures {loss_img}, {acc_img} ...")
        plot.plot_metric(self.alpha, np.loadtxt(loss_res), loss_img, "loss")
        plot.plot_metric(self.alpha, np.loadtxt(acc_res), acc_img, "acc")

        self.model.load_state_dict(self.theta_f)

        return
Esempio n. 3
0
    def layers_linear(self, test_loader, layer):
        """
        Method interpolates parameters of selected layer of the model and evaluates the model after each interpolation
        step

        :param test_loader: test loader
        :param layer: layer to be interpolated
        """

        loss_res = Path("{}_{}".format(paths.vvloss_path, layer))
        loss_img = Path("{}_{}".format(paths.vvloss_img_path, layer))

        acc_res = Path("{}_{}".format(paths.vacc_path, layer))
        acc_img = Path("{}_{}".format(paths.vacc_img_path, layer))

        dist = Path("{}_{}_{}".format(paths.vvloss_path, layer, "distance"))

        logger.debug(f"Result files:\n" f"{loss_res}\n" f"{acc_res}")
        logger.debug(f"Img files:\n" f"{loss_img}\n" f"{acc_img}")
        logger.debug(f"Dist file:\n" f"{dist}")

        if not loss_res.exists() or not acc_res.exists():
            logger.debug("Result files not found - beginning interpolation.")

            v_loss_list = []
            acc_list = []

            self.model.load_state_dict(self.theta_f)
            for alpha_act in tqdm(self.alpha,
                                  desc=f"Layer {layer} Level Linear",
                                  dynamic_ncols=True):
                self.__calc_theta_vec(layer + ".weight", alpha_act)
                self.__calc_theta_vec(layer + ".bias", alpha_act)

                self.model.load_state_dict(self.theta)
                logger.debug(
                    f"Getting validation loss and accuracy for alpha = {alpha_act}"
                )

                vloss, acc = net.test(self.model, test_loader, self.device)
                v_loss_list.append(vloss)
                acc_list.append(acc)

            logger.debug(f"Saving results to files ({loss_res}, {acc_res})")
            np.savetxt(loss_res, v_loss_list)
            np.savetxt(acc_res, acc_list)

        if not dist.exists():
            logger.info(f"Calculating distance for: {layer}")

            distance = self.calc_distance(layer + ".weight")
            logger.info(f"Distance: {distance}")

            with open(dist, 'w') as fd:
                fd.write("{}".format(distance))

        logger.debug(f"Saving results to figures {loss_img}, {acc_img} ...")
        plot.plot_metric(self.alpha, np.loadtxt(loss_res), loss_img, "loss")
        plot.plot_metric(self.alpha, np.loadtxt(acc_res), acc_img, "acc")

        self.model.load_state_dict(self.theta_f)

        return