示例#1
0
    def show(**kwargs):
        from vmaf import plt
        import matplotlib

        if matplotlib.rcParams['backend'] == 'agg':
            if 'write_to_dir' in kwargs:
                format = kwargs['format'] if 'format' in kwargs else 'png'
                filedir = kwargs['write_to_dir'] if kwargs[
                    'write_to_dir'] is not None else VmafConfig.workspace_path(
                        'output')
                os.makedirs(filedir, exist_ok=True)
                for fignum in plt.get_fignums():
                    fig = plt.figure(fignum)
                    fig.savefig(os.path.join(filedir,
                                             str(fignum) + '.' + format),
                                format=format)
            else:
                format = 'png'
                filedir = VmafConfig.workspace_path('output')
                os.makedirs(filedir, exist_ok=True)
                for fignum in plt.get_fignums():
                    fig = plt.figure(fignum)
                    fig.savefig(os.path.join(filedir,
                                             str(fignum) + '.' + format),
                                format=format)
        else:
            plt.show()
示例#2
0
 def show(**kwargs):
     from vmaf import plt
     if 'write_to_dir' in kwargs:
         format = kwargs['format'] if 'format' in kwargs else 'png'
         filedir = kwargs['write_to_dir'] if kwargs['write_to_dir'] is not None else VmafConfig.workspace_path('output')
         if not os.path.exists(filedir):
             os.makedirs(filedir)
         for fignum in plt.get_fignums():
             fig = plt.figure(fignum)
             fig.savefig(os.path.join(filedir, str(fignum) + '.' + format), format=format)
     else:
         plt.show()
示例#3
0
    def plot_explanations(cls, exps, assets=None, ys=None, ys_pred=None):

        # asserts
        N = cls.assert_explanations(exps, assets, ys, ys_pred)

        figs = []
        for n in range(N):
            weights = exps['feature_weights'][n]
            features = exps['features'][n]
            normalized = exps['features_normalized'][n]

            asset = assets[n] if assets is not None else None
            y = ys['label'][n] if ys is not None else None
            y_pred = ys_pred[n] if ys_pred is not None else None

            img = None
            if asset is not None:
                w, h = asset.dis_width_height
                with YuvReader(filepath=asset.dis_path,
                               width=w,
                               height=h,
                               yuv_type=asset.dis_yuv_type) as yuv_reader:
                    for yuv in yuv_reader:
                        img, _, _ = yuv
                        break
                assert img is not None

            title = ""
            if asset is not None:
                title += "{}\n".format(
                    get_file_name_without_extension(asset.ref_path))
            if y is not None:
                title += "ground truth: {:.3f}\n".format(y)
            if y_pred is not None:
                title += "predicted: {:.3f}\n".format(y_pred)
            if title != "" and title[-1] == '\n':
                title = title[:-1]

            assert len(weights) == len(features)
            M = len(weights)

            fig = plt.figure()

            ax_top = plt.subplot(2, 1, 1)
            ax_left = plt.subplot(2, 3, 4)
            ax_mid = plt.subplot(2, 3, 5, sharey=ax_left)
            ax_right = plt.subplot(2, 3, 6, sharey=ax_left)

            if img is not None:
                ax_top.imshow(img, cmap='Greys_r')
            ax_top.get_xaxis().set_visible(False)
            ax_top.get_yaxis().set_visible(False)
            ax_top.set_title(title)

            pos = np.arange(M) + 0.1
            ax_left.barh(pos, features, color='b', label='feature')
            ax_left.set_xticks(np.arange(0, 1.1, 0.2))
            ax_left.set_yticks(pos + 0.35)
            ax_left.set_yticklabels(exps['feature_names'])
            ax_left.set_title('feature')

            ax_mid.barh(pos, normalized, color='g', label='fnormal')
            ax_mid.get_yaxis().set_visible(False)
            ax_mid.set_title('fnormal')

            ax_right.barh(pos, weights, color='r', label='weight')
            ax_right.get_yaxis().set_visible(False)
            ax_right.set_title('weight')

            plt.tight_layout()

            figs.append(fig)

        return figs