Пример #1
0
    def plotfn(self, data, attn_dict, outdir, suffix="png", savefn=None):
        """Plot multi head attentions.

        Args:
            data (dict): Utts info from json file.
            attn_dict (dict): Multi head attention dict.
                Values should be numpy.ndarray (H, L, T)
            outdir (str): Directory name to save figures.
            suffix (str): Filename suffix including image type (e.g., png).
            savefn (function): Function to save figures.

        """
        import matplotlib.pyplot as plt
        for name, att_ws in attn_dict.items():
            for idx, att_w in enumerate(att_ws):
                filename = "%s/%s.%s.%s" % (outdir, data[idx][0], name, suffix)
                if "fbank" in name:
                    fig = plt.Figure()
                    ax = fig.subplots(1, 1)
                    ax.imshow(att_w, aspect="auto")
                    ax.set_xlabel("frames")
                    ax.set_ylabel("fbank coeff")
                    fig.tight_layout()
                else:
                    fig = _plot_and_save_attention(att_w, filename)
                savefn(fig, filename)
        def plotfn(
            self, data_dict, uttid_list, attn_dict, outdir, suffix="png", savefn=None
        ):
            """Plot multi head attentions.

            Args:
                data_dict (dict): Utts info from json file.
                uttid_list (list): List of utt_id.
                attn_dict (dict): Multi head attention dict.
                    Values should be numpy.ndarray (H, L, T)
                outdir (str): Directory name to save figures.
                suffix (str): Filename suffix including image type (e.g., png).
                savefn (function): Function to save figures.

            """
            import matplotlib.pyplot as plt
            from espnet.nets.pytorch_backend.transformer.plot import (
                _plot_and_save_attention,  # noqa: H301
            )

            for name, att_ws in attn_dict.items():
                for utt_id, att_w in zip(uttid_list, att_ws):
                    filename = "%s/%s.%s.%s" % (outdir, utt_id, name, suffix)
                    if "fbank" in name:
                        fig = plt.Figure()
                        ax = fig.subplots(1, 1)
                        ax.imshow(att_w, aspect="auto")
                        ax.set_xlabel("frames")
                        ax.set_ylabel("fbank coeff")
                        fig.tight_layout()
                    else:
                        fig = _plot_and_save_attention(att_w, filename)
                    savefn(fig, filename)
    def plotfn(self, data, attn_dict, outdir, suffix="png", savefn=None):

        import matplotlib.pyplot as plt

        for name, att_ws in attn_dict.items():
            for idx, att_w in enumerate(att_ws):
                filename = "%s/%s.%s.%s" % (outdir, data[idx][0], name, suffix)
                if "fbank" in name:
                    fig = plt.Figure()
                    ax = fig.subplots(1, 1)
                    ax.imshow(att_w, aspect="auto")
                    ax.set_xlabel("frames")
                    ax.set_ylabel("fbank coeff")
                    fig.tight_layout()
                else:
                    fig = _plot_and_save_attention(att_w, filename)
                savefn(fig, filename)
    def plotfn(self, data, attn_dict, outdir, suffix="png", savefn=None):
        """Plot multi head attentions

        :param dict data: utts info from json file
        :param dict[str, torch.Tensor] attn_dict: multi head attention dict.
            values should be torch.Tensor (head, input_length, output_length)
        :param str outdir: dir to save fig
        :param str suffix: filename suffix including image type (e.g., png)
        :param savefn: function to save
        """
        import matplotlib.pyplot as plt
        for name, att_ws in attn_dict.items():
            for idx, att_w in enumerate(att_ws):
                filename = "%s/%s.%s.%s" % (outdir, data[idx][0], name, suffix)
                if "fbank" in name:
                    fig = plt.Figure()
                    ax = fig.subplots(1, 1)
                    ax.imshow(att_w, aspect="auto")
                    ax.set_xlabel("frames")
                    ax.set_ylabel("fbank coeff")
                    fig.tight_layout()
                else:
                    fig = _plot_and_save_attention(att_w, filename)
                savefn(fig, filename)