Exemplo n.º 1
0
    def _get_compression_legend_kwargs(cls, configs, legend_loc, font_name, compression_keys):
        custom_lines = list([])

        for key in compression_keys:
            custom_lines.append(cls.get_line({**configs[key], 'marker': 'None', 'color': 'black', 'lw': 1.2}))

        return dict(fancybox=False,
                    framealpha=0.4,
                    handles=custom_lines,
                    labels=[COMPRESSION_NAMES[k] for k in compression_keys],
                    loc=legend_loc,
                    bbox_to_anchor=(0.0, 0.02, 1., .102),
                    prop=get_font(font_name, FONTSIZES.medium),
                    ncol=2)
Exemplo n.º 2
0
    def _get_legend_kwargs(configs, labels, legend_loc, font_name,
                           compression_colors):
        def get_line(_cfg=None,
                     key=None,
                     color=None,
                     linestyle=None,
                     marker=None,
                     **kwargs):
            if _cfg is not None:
                marker = _cfg['marker']
                color = compression_colors[key]
                linestyle = _cfg['linestyle']

            return Line2D([0], [0],
                          color=color,
                          lw=1.2,
                          linestyle=linestyle,
                          marker=marker,
                          **kwargs)

        # legend
        custom_lines_compressors = [get_line(cfg, k) for k, cfg in configs]
        marker_kwargs = dict(markerfacecolor='red', markeredgecolor='red')
        custom_lines_compressors += [
            get_line(color='dimgrey',
                     linestyle='--',
                     marker='^',
                     **marker_kwargs)
        ]

        return dict(fancybox=False,
                    framealpha=0.4,
                    handles=custom_lines_compressors,
                    labels=labels,
                    loc=legend_loc,
                    prop=get_font(font_name, FONTSIZES.big),
                    ncol=1)
Exemplo n.º 3
0
    def make_plot(self,
                  classifier,
                  show,
                  save,
                  compression_keys,
                  topk=1,
                  make_title=False,
                  xmin=0.032,
                  xmax=None,
                  ymin=0,
                  ymax=100,
                  figsize=(6, 3.75),
                  logscale=True,
                  font_name='times_roman',
                  legend_loc='lower right',
                  bpp_min=None):

        if compression_keys is None:
            raise ValueError('compression_keys must not be None')

        if not isinstance(compression_keys, list):
            compression_keys = list([compression_keys])

        if int(topk) != 1 and int(topk) != 5:
            raise ValueError('topk must either be `1` or `5`')

        topkacc_kw = LogsParser.TOP1ACC_KW if int(
            topk) == 1 else LogsParser.TOP5ACC_KW
        topkacc_idx = 1 if int(topk) == 1 else 2

        configs = {
            ckey: self.load_config(ckey, classifier)
            for ckey in compression_keys
        }
        csv_original = os.path.join(
            self._csv_dir, CSV_FILE_BASE.format('original', classifier))
        corrupted_logfiles = []

        if os.path.isfile(csv_original):
            acc_data_lossless = np.array(read_csv(csv_original),
                                         dtype=np.float32)
        else:
            print('WARNING! {} not found.'.format(csv_original))
            corrupted_logfiles.append('original')
            acc_data_lossless = None

        # ========= parse csv files
        parsed_data = {}
        for key in compression_keys:
            cfg = configs[key]
            csv_file = cfg['csv_file']

            if not os.path.isfile(csv_file):
                print('WARNING! {} not found.'.format(csv_file))
                corrupted_logfiles.append(key)
                continue

            acc_data = np.array(read_csv(csv_file), dtype=np.float32)

            # sort data
            acc_data = acc_data[acc_data[:, 0].argsort()]

            if bpp_min is not None:
                include_idx = np.where(acc_data[:, 0] >= float(bpp_min))[0]
                acc_data = acc_data[include_idx, :]

            parsed_data[key] = acc_data[:, 0], 100.0 * acc_data[:, topkacc_idx]

        if len(parsed_data) == 0:
            print('parsed_data empty')
            return

        compression_keys = list(
            [k for k in compression_keys if k not in corrupted_logfiles])
        configs = {k: configs[k] for k in compression_keys}
        compression_colors = self.keys_to_color([k for k in compression_keys])

        # ========= make plot
        # determine plot boundaries
        all_bpp_data = np.unique(
            np.concatenate([data[0] for data in parsed_data.values()]))
        if xmax is None:
            xmax = all_bpp_data.max() + 2.0
        x_lim = xmin, xmax
        y_lim = ymin, ymax

        # setup fig
        fig = plt.figure(figsize=figsize)

        # plot data
        ax = plt.gca()
        plot_func = ax.semilogx if logscale else ax.plot
        for key in compression_keys:
            bpp_array, acc_array = parsed_data[key]
            cfg = configs[key]

            plot_func(bpp_array,
                      acc_array,
                      lw=cfg['lw'],
                      color=compression_colors[key],
                      label=COMPRESSION_NAMES[key],
                      marker=cfg['marker'],
                      markersize=3 * cfg['lw'],
                      linestyle=cfg['linestyle'])

        if acc_data_lossless is not None:
            ax.axhline(100.0 * acc_data_lossless[0, topkacc_idx],
                       xmin=0,
                       xmax=24,
                       color='dimgrey',
                       lw=0.75,
                       linestyle='--')
            plot_func(acc_data_lossless[:, 0],
                      100.0 * acc_data_lossless[:, topkacc_idx],
                      marker='^',
                      markersize=6,
                      color='red')

        # format plot
        plt.xlim(x_lim)
        plt.ylim(y_lim)
        ax.set_xlabel('bpp',
                      fontproperties=get_font(font_name, FONTSIZES.Large))
        ax.set_ylabel('Validation Accuracy (%)',
                      fontproperties=get_font(font_name, FONTSIZES.Large))
        ax.grid(True, color=(0.91, 0.91, 0.91), linewidth=0.5)

        ax.yaxis.set_minor_locator(MultipleLocator(5))
        ax.yaxis.set_major_locator(MultipleLocator(10))
        ax.yaxis.set_major_formatter(FormatStrFormatter('%.1d'))

        if logscale:
            ax.xaxis.set_minor_locator(
                LogLocator(base=2, subs=(1.2, 1.4, 1.6, 1.8)))
            ax.xaxis.set_minor_formatter(NullFormatter())
            ax.xaxis.set_major_locator(LogLocator(base=2))
            ax.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
        else:
            ax.xaxis.set_major_locator(MultipleLocator(0.125))
            ax.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
            ax.xaxis.set_minor_locator(MultipleLocator(0.125))

        ax.tick_params(which='minor', width=0.4)

        ax.set_facecolor(FACECOLOR)
        for label in ax.get_xticklabels():
            label.set_fontproperties(get_font(font_name, FONTSIZES.large))
        for label in ax.get_yticklabels():
            label.set_fontproperties(get_font(font_name, FONTSIZES.large))
        for spine in ['top', 'bottom', 'left', 'right']:
            ax.spines[spine].set_color('black')

        # legend
        legend_labels = [COMPRESSION_NAMES[k] for k in compression_keys]
        legend_labels += ['Original']
        legend = plt.legend(
            **self._get_legend_kwargs(configs=[(k, configs[k])
                                               for k in compression_keys],
                                      labels=legend_labels,
                                      legend_loc=legend_loc,
                                      font_name=font_name,
                                      compression_colors=compression_colors))
        ax.add_artist(legend)

        # title
        if make_title:
            plt.suptitle(t='Validation Accuracy on {}, Top-{}, %'.format(
                DATASET_NAMES[self._dataset],
                1 if topkacc_kw == LogsParser.TOP1ACC_KW else 5),
                         fontproperties=get_font(font_name, FONTSIZES.Large))
            plt.title(CLASSIFIER_NAMES[classifier],
                      fontproperties=get_font(font_name, FONTSIZES.large))
            plt.subplots_adjust(left=0.08, right=0.97, bottom=0.12, top=0.86)

        else:
            fig.tight_layout()

        if show:
            plt.show()

        if save:
            if not os.path.exists(self._plots_save_dir):
                os.makedirs(self._plots_save_dir)

            save_as = '{}_accuracy_{}_{}.png'.format(self._dataset, classifier,
                                                     topkacc_kw)
            fig.savefig(os.path.join(self._plots_save_dir, save_as), dpi=200)
            print('plot saved as {}'.format(
                os.path.join(self._plots_save_dir, save_as)))

        plt.close(fig)
Exemplo n.º 4
0
    def make_plot(self,
                  perception_metric,
                  show,
                  save,
                  compression_keys,
                  xmin=0.0,
                  xmax=1.0,
                  ymin=0,
                  ymax=1,
                  figsize=(6, 3.75),
                  font_name='times_roman',
                  legend_loc='lower right',
                  bpp_min=None):

        assert perception_metric in [
            LogsParser.L1_KW, LogsParser.MSE_KW, LogsParser.MSSSIM_KW,
            LogsParser.PSNR_KW
        ]

        if compression_keys is None:
            raise ValueError('compression_keys must not be None')

        if not isinstance(compression_keys, list):
            compression_keys = list([compression_keys])

        configs = {ckey: self.load_config(ckey) for ckey in compression_keys}
        corrupted_logfiles = []

        # ========= parse csv files
        parsed_data = {}
        for key in compression_keys:
            cfg = configs[key]
            csv_file = cfg['csv_file']

            if not os.path.isfile(csv_file):
                print('WARNING! {} not found.'.format(csv_file))
                corrupted_logfiles.append(key)
                continue

            # parse csv
            hvs_data = read_csv(csv_file)
            idx = hvs_data[0].index(perception_metric)
            hvs_data = np.array(hvs_data[1:], dtype=np.float32)
            hvs_data = hvs_data[:, [0, idx]]

            # sort data
            hvs_data.sort(axis=0)

            if bpp_min is not None:
                include_idx = np.where(hvs_data[:, 0] >= float(bpp_min))[0]
                hvs_data = hvs_data[include_idx, :]

            parsed_data[key] = hvs_data[:, 0], hvs_data[:, 1]

        if len(parsed_data) == 0:
            print('parsed data empty')
            return

        compression_keys = list(
            [k for k in compression_keys if k not in corrupted_logfiles])
        configs = {k: configs[k] for k in compression_keys}
        compression_colors = self.keys_to_color([k for k in compression_keys])

        # ========= make plot
        # determine plot boundaries
        x_lim = xmin, xmax
        y_lim = ymin, ymax

        # setup fig
        fig = plt.figure(figsize=figsize)

        # plot data
        ax = plt.gca()
        for key in compression_keys:
            bpp_array, hvs_array = parsed_data[key]
            cfg = configs[key]

            ax.plot(bpp_array,
                    hvs_array,
                    lw=cfg['lw'],
                    color=compression_colors[key],
                    label=COMPRESSION_NAMES[key],
                    marker=cfg['marker'],
                    markersize=3 * cfg['lw'],
                    linestyle=cfg['linestyle'])

        # format plot
        plt.xlim(x_lim)
        plt.ylim(y_lim)
        ax.set_xlabel('bpp',
                      fontproperties=get_font(font_name, FONTSIZES.Large))
        ax.set_ylabel(HVS_METRIC_NAMES[perception_metric],
                      fontproperties=get_font(font_name, FONTSIZES.Large))
        ax.grid(True, color=(0.91, 0.91, 0.91), linewidth=0.5)

        tick_multiples = TICK_MULTIPLES[perception_metric]
        ax.yaxis.set_minor_locator(MultipleLocator(tick_multiples))
        ax.yaxis.set_major_locator(MultipleLocator(2 * tick_multiples))
        ax.yaxis.set_major_formatter(
            FormatStrFormatter('%.2f' if tick_multiples < 1 else '%d'))

        ax.xaxis.set_major_locator(MultipleLocator(0.25))
        ax.xaxis.set_minor_locator(MultipleLocator(0.125))
        ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

        ax.tick_params(which='minor', width=0.4)

        ax.set_facecolor(FACECOLOR)
        for label in ax.get_xticklabels():
            label.set_fontproperties(get_font(font_name, FONTSIZES.large))
        for label in ax.get_yticklabels():
            label.set_fontproperties(get_font(font_name, FONTSIZES.large))
        for spine in ['top', 'bottom', 'left', 'right']:
            ax.spines[spine].set_color('black')

        # legend
        legend_labels = [COMPRESSION_NAMES[k] for k in compression_keys]
        legend = plt.legend(
            **self._get_legend_kwargs(configs=[(k, configs[k])
                                               for k in compression_keys],
                                      labels=legend_labels,
                                      legend_loc=legend_loc,
                                      font_name=font_name,
                                      compression_colors=compression_colors))
        ax.add_artist(legend)

        plt.tight_layout()

        if show:
            plt.show()

        if save:
            if not os.path.exists(self._plots_save_dir):
                os.makedirs(self._plots_save_dir)

            save_as = 'hvs_{}.png'.format(perception_metric)
            fig.savefig(os.path.join(self._plots_save_dir, save_as), dpi=200)
            print('plot saved as {}'.format(
                os.path.join(self._plots_save_dir, save_as)))

        plt.close(fig)
Exemplo n.º 5
0
    def make_plot(self, classifier, show, save, target_bitrate, hvs_metric, ymin, ymax, figsize, baselines=None,
                  topk=1, font_name='times_roman'):

        # hvs_color = 'darkturquoise'
        # hvs_color = 'palevioletred'
        hvs_color = 'grey'
        acc_color = 'midnightblue'

        if baselines is None:
            baselines = []

        # ========= get data
        # get rnn compression data
        rnn_accuracy_data = self.get_accuracy_data_rnn(target_bitrate, classifier, topk, True)
        rnn_hvs_data = self.get_hvs_data_rnn(target_bitrate, hvs_metric)

        baselines_accuracy_data = self.get_accuracy_data_baselines(target_bitrate, classifier, topk, baselines, True,
                                                                   True)
        baselines_hvs_data = self.get_hvs_data_baselines(target_bitrate, hvs_metric, baselines, True)

        # ========= make plot
        fig, ax1 = plt.subplots(figsize=figsize)
        ax2 = ax1.twinx()

        # baseline data
        for baseline_hvs, key in zip(baselines_hvs_data, baselines):
            ax1.axhline(float(baseline_hvs[_BASELINE_IDX_VAL]), color=hvs_color,
                        linestyle=convert_linestyle(self._configs[key]['linestyle']), label=COMPRESSION_NAMES[key],
                        lw=self._configs[key]['lw'])

        for baseline_acc, key in zip(baselines_accuracy_data, baselines):
            ax1.axhline(float(baseline_acc[_BASELINE_IDX_VAL]), color=acc_color,
                        linestyle=convert_linestyle(convert_linestyle(self._configs[key]['linestyle'])),
                        label=COMPRESSION_NAMES[key], lw=self._configs[key]['lw'])

        # == fill in data RNN compression
        # left y-axis (MS-SSIM)
        ax1.set_xlabel(r'$\alpha$', fontproperties=get_font(font_name, FONTSIZES.Large))
        ax1.set_ylabel('MS-SSIM', fontproperties=get_font(font_name, FONTSIZES.Large), labelpad=10, color=hvs_color)
        ax1.plot(rnn_hvs_data[:, _RNN_IDX_ALPHA], rnn_hvs_data[:, _RNN_IDX_VAL], color=hvs_color,
                 lw=self._configs['rnn']['lw'], linestyle=self._configs['rnn']['linestyle'],
                 markersize=3 * self._configs['rnn']['lw'], marker=self._configs['rnn']['marker'])
        ax1.tick_params(axis='y', labelcolor=hvs_color)
        ax1.tick_params(axis='y', which='minor', width=0.7, colors=hvs_color)
        ax1.tick_params(axis='y', which='major', colors=hvs_color)

        # right y-axis (Accuracy)
        ax2.set_ylabel('Preserved Val. Accuracy', fontproperties=get_font(font_name, FONTSIZES.Large), labelpad=10,
                       color=acc_color)
        ax2.plot(rnn_accuracy_data[:, _RNN_IDX_ALPHA], rnn_accuracy_data[:, _RNN_IDX_VAL], color=acc_color,
                 lw=self._configs['rnn']['lw'], linestyle=self._configs['rnn']['linestyle'],
                 markersize=3 * self._configs['rnn']['lw'], marker=self._configs['rnn']['marker'])
        ax2.tick_params(axis='y', labelcolor=acc_color)
        ax2.tick_params(axis='y', which='minor', width=0.7, colors=acc_color)
        ax2.tick_params(axis='y', which='major', colors=acc_color)

        # == format
        # axis limits
        ax1.set_xlim((-0.05, 1.05))
        ax1.set_ylim((ymin, ymax))
        ax2.set_ylim((ymin, ymax))

        # ticks
        ax1.yaxis.set_major_locator(MultipleLocator(0.05))
        ax1.yaxis.set_minor_locator(MultipleLocator(0.025))
        ax1.set_xticks(np.arange(0, 1.25, 0.25))
        ax2.yaxis.set_major_locator(MultipleLocator(0.05))
        ax2.yaxis.set_minor_locator(MultipleLocator(0.025))
        ax2.set_xticks(np.arange(0, 1.25, 0.25))

        # fontprops
        for labelx in ax1.get_xticklabels():
            labelx.set_fontproperties(get_font(font_name, FONTSIZES.large))

        for labely1 in ax1.get_yticklabels():
            labely1.set_fontproperties(get_font(font_name, FONTSIZES.large))
            labely1.set_color(hvs_color)

        for labely2 in ax2.get_yticklabels():
            labely2.set_fontproperties(get_font(font_name, FONTSIZES.large))
            labely2.set_color(acc_color)

        # grid, facecolor
        ax1.grid(True, color=_GRID_COLOR, linewidth=0.5)
        ax1.set_facecolor(_FACECOLOR)
        ax2.spines['right'].set_visible(True)
        ax2.spines['right'].set_color(acc_color)
        ax2.spines['left'].set_visible(True)
        ax2.spines['left'].set_color(hvs_color)

        # legend for compression method
        legend1 = ax1.legend(**self._get_compression_legend_kwargs(configs=self._configs,
                                                                   legend_loc='lower center',
                                                                   font_name=font_name,
                                                                   compression_keys=['rnn', *baselines]))
        ax1.add_artist(legend1)
        fig.tight_layout()

        if show:
            plt.show()

        if save:
            if not os.path.exists(self._plots_save_dir):
                os.makedirs(self._plots_save_dir)

            save_as = '{}_tradeoff_{}_{}_{}bpp.png'.format(self._dataset, hvs_metric, classifier, target_bitrate)
            fig.savefig(os.path.join(self._plots_save_dir, save_as), dpi=200)
            print('plot saved as {}'.format(os.path.join(self._plots_save_dir, save_as)))

        plt.close(fig)