id
int64
0
190k
prompt
stringlengths
21
13.4M
docstring
stringlengths
1
12k
182,737
import pickle import os import argparse import numpy as np from torch.utils.data import Dataset, DataLoader from mmpt.processors import PKLJSONStrTextProcessor from mmpt.utils import ShardedTensor, recursive_config class TokenizerDataset(Dataset): def __init__(self, config): def __getitem__(self, idx): def __len__(self): def tokenize(config, out_file): def collator(samples): return samples dataset = TokenizerDataset(config) data = {} for idx, batch in enumerate( DataLoader(dataset, collate_fn=collator, num_workers=16)): for video_id, caption in batch: data[video_id] = caption if idx % 5000 == 0: print(idx) with open(out_file, "wb") as fw: pickle.dump(data, fw, pickle.HIGHEST_PROTOCOL)
null
182,773
from __future__ import absolute_import, division, print_function, unicode_literals import re from collections import deque from enum import Enum import numpy as np class WERTransformer(object): def __init__(self, hyp_str, ref_str, verbose=True): def process(self, input): def report_result(self): def wer(self): def stats(self): def calc_wer_stats(hyp_str, ref_str): t = WERTransformer(hyp_str, ref_str, verbose=0) return t.stats()
null
182,811
from typing import Dict, List, NamedTuple, Optional import torch import torch.nn as nn from examples.simultaneous_translation.modules.monotonic_transformer_layer import ( TransformerMonotonicDecoderLayer, TransformerMonotonicEncoderLayer, ) from fairseq.models import ( register_model, register_model_architecture, ) from fairseq.models.transformer import ( TransformerModel, TransformerEncoder, TransformerDecoder, base_architecture, transformer_iwslt_de_en, transformer_vaswani_wmt_en_de_big, tiny_architecture ) from torch import Tensor def transformer_vaswani_wmt_en_de_big(args): def transformer_monotonic_vaswani_wmt_en_de_big(args): transformer_vaswani_wmt_en_de_big(args)
null
182,813
from typing import Dict, List, NamedTuple, Optional import torch import torch.nn as nn from examples.simultaneous_translation.modules.monotonic_transformer_layer import ( TransformerMonotonicDecoderLayer, TransformerMonotonicEncoderLayer, ) from fairseq.models import ( register_model, register_model_architecture, ) from fairseq.models.transformer import ( TransformerModel, TransformerEncoder, TransformerDecoder, base_architecture, transformer_iwslt_de_en, transformer_vaswani_wmt_en_de_big, tiny_architecture ) from torch import Tensor def transformer_iwslt_de_en(args): def transformer_unidirectional_iwslt_de_en(args): transformer_iwslt_de_en(args)
null
182,814
from typing import Dict, List, NamedTuple, Optional import torch import torch.nn as nn from examples.simultaneous_translation.modules.monotonic_transformer_layer import ( TransformerMonotonicDecoderLayer, TransformerMonotonicEncoderLayer, ) from fairseq.models import ( register_model, register_model_architecture, ) from fairseq.models.transformer import ( TransformerModel, TransformerEncoder, TransformerDecoder, base_architecture, transformer_iwslt_de_en, transformer_vaswani_wmt_en_de_big, tiny_architecture ) from torch import Tensor def base_monotonic_architecture(args): def tiny_architecture(args): def monotonic_tiny_architecture(args): tiny_architecture(args) base_monotonic_architecture(args)
null
182,832
import logging from typing import Dict, List, Optional from pathlib import Path import torch.nn as nn from torch import Tensor from fairseq import checkpoint_utils from fairseq.models import register_model, register_model_architecture from fairseq.utils import safe_hasattr from fairseq.models.speech_to_text.s2t_transformer import ( S2TTransformerModel, S2TTransformerEncoder, TransformerDecoderScriptable ) from fairseq.models.speech_to_text.s2t_transformer import base_architecture as s2t_base_architecture from ..modules.attn_head_selector import AttnHeadSelector from ..modules.head_selection_transformer_layer import HeadSelectionTransformerEncoderLayer from .head_selection_transformer import HeadSelectionTransformerDecoder def base_architecture(args): s2t_base_architecture(args) args.encoder_attn_head_select = getattr(args, "encoder_attn_head_select", False) args.decoder_self_attn_head_select = getattr(args, "decoder_self_attn_head_select", False) args.dec_enc_attn_head_select = getattr(args, "dec_enc_attn_head_select", False) args.total_encoder_attention_heads = getattr(args, "total_encoder_attention_heads", 8) args.total_decoder_attention_heads = getattr(args, "total_decoder_attention_heads", 8) args.attn_head_select_strategy = getattr(args, "attn_head_select_strategy", "group") def base_architecture(args): args.encoder_freezing_updates = getattr(args, "encoder_freezing_updates", 0) # Convolutional subsampler args.conv_kernel_sizes = getattr(args, "conv_kernel_sizes", "5,5") args.conv_channels = getattr(args, "conv_channels", 1024) # Transformer args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 512) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 2048) args.encoder_layers = getattr(args, "encoder_layers", 12) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 8) args.encoder_normalize_before = getattr(args, "encoder_normalize_before", True) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", args.encoder_embed_dim) args.decoder_ffn_embed_dim = getattr( args, "decoder_ffn_embed_dim", args.encoder_ffn_embed_dim ) args.decoder_layers = getattr(args, "decoder_layers", 6) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 8) args.decoder_normalize_before = getattr(args, "decoder_normalize_before", True) args.decoder_learned_pos = getattr(args, "decoder_learned_pos", False) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", args.dropout) args.activation_dropout = getattr(args, "activation_dropout", args.dropout) args.activation_fn = getattr(args, "activation_fn", "relu") args.adaptive_softmax_cutoff = getattr(args, "adaptive_softmax_cutoff", None) args.adaptive_softmax_dropout = getattr(args, "adaptive_softmax_dropout", 0) args.share_decoder_input_output_embed = getattr( args, "share_decoder_input_output_embed", False ) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.adaptive_input = getattr(args, "adaptive_input", False) args.decoder_layerdrop = getattr(args, "decoder_layerdrop", 0.0) args.decoder_output_dim = getattr( args, "decoder_output_dim", args.decoder_embed_dim ) args.decoder_input_dim = getattr(args, "decoder_input_dim", args.decoder_embed_dim) args.no_scale_embedding = getattr(args, "no_scale_embedding", False) args.quant_noise_pq = getattr(args, "quant_noise_pq", 0) def head_selection_s2t_transformer_s(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 256) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 256 * 8) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 4) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 4) args.dropout = getattr(args, "dropout", 0.1) base_architecture(args)
null
182,843
import io import os from pathlib import Path from typing import Optional, List, Dict import zipfile import tempfile from dataclasses import dataclass from itertools import groupby import torch import torch.nn.functional as F import numpy as np from tqdm import tqdm from examples.speech_to_text.data_utils import load_tsv_to_dicts from fairseq.data.audio.audio_utils import ( TTSSpectrogram, TTSMelScale, parse_path, read_from_stored_zip, is_npy_data ) def trim_or_pad_to_target_length( data_1d_or_2d: np.ndarray, target_length: int ) -> np.ndarray: assert len(data_1d_or_2d.shape) in {1, 2} delta = data_1d_or_2d.shape[0] - target_length if delta >= 0: # trim if being longer data_1d_or_2d = data_1d_or_2d[: target_length] else: # pad if being shorter if len(data_1d_or_2d.shape) == 1: data_1d_or_2d = np.concatenate( [data_1d_or_2d, np.zeros(-delta)], axis=0 ) else: data_1d_or_2d = np.concatenate( [data_1d_or_2d, np.zeros((-delta, data_1d_or_2d.shape[1]))], axis=0 ) return data_1d_or_2d class TTSSpectrogram(torch.nn.Module): def __init__( self, n_fft: int, win_length: int, hop_length: int, window_fn: callable = torch.hann_window, return_phase: bool = False ) -> None: super(TTSSpectrogram, self).__init__() self.n_fft = n_fft self.hop_length = hop_length self.return_phase = return_phase basis = get_fourier_basis(n_fft).unsqueeze(1) basis *= get_window(window_fn, n_fft, win_length) self.register_buffer('basis', basis) def forward( self, waveform: torch.Tensor ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: padding = (self.n_fft // 2, self.n_fft // 2) x = F.pad(waveform.unsqueeze(1), padding, mode='reflect') x = F.conv1d(x, self.basis, stride=self.hop_length) real_part = x[:, :self.n_fft // 2 + 1, :] imag_part = x[:, self.n_fft // 2 + 1:, :] magnitude = torch.sqrt(real_part ** 2 + imag_part ** 2) if self.return_phase: phase = torch.atan2(imag_part, real_part) return magnitude, phase return magnitude class TTSMelScale(torch.nn.Module): def __init__( self, n_mels: int, sample_rate: int, f_min: float, f_max: float, n_stft: int ) -> None: super(TTSMelScale, self).__init__() basis = get_mel_filters(sample_rate, (n_stft - 1) * 2, n_mels, f_min, f_max) self.register_buffer('basis', basis) def forward(self, specgram: torch.Tensor) -> torch.Tensor: return torch.matmul(self.basis, specgram) def extract_logmel_spectrogram( waveform: torch.Tensor, sample_rate: int, output_path: Optional[Path] = None, win_length: int = 1024, hop_length: int = 256, n_fft: int = 1024, win_fn: callable = torch.hann_window, n_mels: int = 80, f_min: float = 0., f_max: float = 8000, eps: float = 1e-5, overwrite: bool = False, target_length: Optional[int] = None ): if output_path is not None and output_path.is_file() and not overwrite: return spectrogram_transform = TTSSpectrogram( n_fft=n_fft, win_length=win_length, hop_length=hop_length, window_fn=win_fn ) mel_scale_transform = TTSMelScale( n_mels=n_mels, sample_rate=sample_rate, f_min=f_min, f_max=f_max, n_stft=n_fft // 2 + 1 ) spectrogram = spectrogram_transform(waveform) mel_spec = mel_scale_transform(spectrogram) logmel_spec = torch.clamp(mel_spec, min=eps).log() assert len(logmel_spec.shape) == 3 and logmel_spec.shape[0] == 1 logmel_spec = logmel_spec.squeeze().t() # D x T -> T x D if target_length is not None: logmel_spec = trim_or_pad_to_target_length(logmel_spec, target_length) if output_path is not None: np.save(output_path.as_posix(), logmel_spec) else: return logmel_spec
null
182,844
import io import os from pathlib import Path from typing import Optional, List, Dict import zipfile import tempfile from dataclasses import dataclass from itertools import groupby import torch import torch.nn.functional as F import numpy as np from tqdm import tqdm from examples.speech_to_text.data_utils import load_tsv_to_dicts from fairseq.data.audio.audio_utils import ( TTSSpectrogram, TTSMelScale, parse_path, read_from_stored_zip, is_npy_data ) def trim_or_pad_to_target_length( data_1d_or_2d: np.ndarray, target_length: int ) -> np.ndarray: assert len(data_1d_or_2d.shape) in {1, 2} delta = data_1d_or_2d.shape[0] - target_length if delta >= 0: # trim if being longer data_1d_or_2d = data_1d_or_2d[: target_length] else: # pad if being shorter if len(data_1d_or_2d.shape) == 1: data_1d_or_2d = np.concatenate( [data_1d_or_2d, np.zeros(-delta)], axis=0 ) else: data_1d_or_2d = np.concatenate( [data_1d_or_2d, np.zeros((-delta, data_1d_or_2d.shape[1]))], axis=0 ) return data_1d_or_2d def extract_pitch( waveform: torch.Tensor, sample_rate: int, output_path: Optional[Path] = None, hop_length: int = 256, log_scale: bool = True, phoneme_durations: Optional[List[int]] = None ): if output_path is not None and output_path.is_file(): return try: import pyworld except ImportError: raise ImportError("Please install PyWORLD: pip install pyworld") _waveform = waveform.squeeze(0).double().numpy() pitch, t = pyworld.dio( _waveform, sample_rate, frame_period=hop_length / sample_rate * 1000 ) pitch = pyworld.stonemask(_waveform, pitch, t, sample_rate) if phoneme_durations is not None: pitch = trim_or_pad_to_target_length(pitch, sum(phoneme_durations)) try: from scipy.interpolate import interp1d except ImportError: raise ImportError("Please install SciPy: pip install scipy") nonzero_ids = np.where(pitch != 0)[0] if len(nonzero_ids) == 0: print((f"{output_path} has all empty values in the pitch contour")) return elif len(nonzero_ids) == 1: print((f"{output_path} has only one non-zero values in the pitch contour")) return else: interp_fn = interp1d( nonzero_ids, pitch[nonzero_ids], fill_value=(pitch[nonzero_ids[0]], pitch[nonzero_ids[-1]]), bounds_error=False, ) pitch = interp_fn(np.arange(0, len(pitch))) d_cumsum = np.cumsum(np.concatenate([np.array([0]), phoneme_durations])) pitch = np.array( [ np.mean(pitch[d_cumsum[i-1]: d_cumsum[i]]) for i in range(1, len(d_cumsum)) ] ) assert len(pitch) == len(phoneme_durations) if log_scale: pitch = np.log(pitch + 1) if output_path is not None: np.save(output_path.as_posix(), pitch) else: return pitch
null
182,845
import io import os from pathlib import Path from typing import Optional, List, Dict import zipfile import tempfile from dataclasses import dataclass from itertools import groupby import torch import torch.nn.functional as F import numpy as np from tqdm import tqdm from examples.speech_to_text.data_utils import load_tsv_to_dicts from fairseq.data.audio.audio_utils import ( TTSSpectrogram, TTSMelScale, parse_path, read_from_stored_zip, is_npy_data ) def trim_or_pad_to_target_length( data_1d_or_2d: np.ndarray, target_length: int ) -> np.ndarray: def extract_energy( waveform: torch.Tensor, output_path: Optional[Path] = None, hop_length: int = 256, n_fft: int = 1024, log_scale: bool = True, phoneme_durations: Optional[List[int]] = None ): if output_path is not None and output_path.is_file(): return assert len(waveform.shape) == 2 and waveform.shape[0] == 1 waveform = waveform.view(1, 1, waveform.shape[1]) waveform = F.pad( waveform.unsqueeze(1), [n_fft // 2, n_fft // 2, 0, 0], mode="reflect" ) waveform = waveform.squeeze(1) fourier_basis = np.fft.fft(np.eye(n_fft)) cutoff = int((n_fft / 2 + 1)) fourier_basis = np.vstack( [np.real(fourier_basis[:cutoff, :]), np.imag(fourier_basis[:cutoff, :])] ) forward_basis = torch.FloatTensor(fourier_basis[:, None, :]) forward_transform = F.conv1d( waveform, forward_basis, stride=hop_length, padding=0 ) real_part = forward_transform[:, :cutoff, :] imag_part = forward_transform[:, cutoff:, :] magnitude = torch.sqrt(real_part ** 2 + imag_part ** 2) energy = torch.norm(magnitude, dim=1).squeeze(0).numpy() if phoneme_durations is not None: energy = trim_or_pad_to_target_length(energy, sum(phoneme_durations)) d_cumsum = np.cumsum(np.concatenate([np.array([0]), phoneme_durations])) energy = np.array( [ np.mean(energy[d_cumsum[i - 1]: d_cumsum[i]]) for i in range(1, len(d_cumsum)) ] ) assert len(energy) == len(phoneme_durations) if log_scale: energy = np.log(energy + 1) if output_path is not None: np.save(output_path.as_posix(), energy) else: return energy
null
182,850
import io import os from pathlib import Path from typing import Optional, List, Dict import zipfile import tempfile from dataclasses import dataclass from itertools import groupby import torch import torch.nn.functional as F import numpy as np from tqdm import tqdm from examples.speech_to_text.data_utils import load_tsv_to_dicts from fairseq.data.audio.audio_utils import ( TTSSpectrogram, TTSMelScale, parse_path, read_from_stored_zip, is_npy_data ) def is_npy_data(data: bytes) -> bool: return data[0] == 147 and data[1] == 78 def read_from_stored_zip(zip_path: str, offset: int, file_size: int) -> bytes: with open(zip_path, "rb") as f: f.seek(offset) data = f.read(file_size) return data def parse_path(path: str) -> Tuple[str, List[int]]: """Parse data path which is either a path to 1. a .npy/.wav/.flac/.ogg file 2. a stored ZIP file with slicing info: "[zip_path]:[offset]:[length]" Args: path (str): the data path to parse Returns: file_path (str): the file path slice_ptr (list of int): empty in case 1; byte offset and length for the slice in case 2 """ if Path(path).suffix in FEATURE_OR_SF_AUDIO_FILE_EXTENSIONS: _path, slice_ptr = path, [] else: _path, *slice_ptr = path.split(":") if not Path(_path).is_file(): raise FileNotFoundError(f"File not found: {_path}") assert len(slice_ptr) in {0, 2}, f"Invalid path: {path}" slice_ptr = [int(i) for i in slice_ptr] return _path, slice_ptr def get_feature_value_min_max(feature_paths: List[str]): v_min, v_max = 1e-8, -1e-8 for p in tqdm(feature_paths): _path, slice_ptr = parse_path(p) assert len(slice_ptr) == 2 byte_data = read_from_stored_zip(_path, slice_ptr[0], slice_ptr[1]) assert is_npy_data(byte_data) path_or_fp = io.BytesIO(byte_data) features = np.load(path_or_fp).squeeze() v_min = min(v_min, features.min().item()) v_max = max(v_max, features.max().item()) return v_min, v_max
null
182,851
import ast import logging import matplotlib.pyplot as plt import numpy as np from pathlib import Path import soundfile as sf import sys import torch import torchaudio from fairseq import checkpoint_utils, options, tasks, utils from fairseq.logging import progress_bar from fairseq.tasks.text_to_speech import plot_tts_output from fairseq.data.audio.text_to_speech_dataset import TextToSpeechDataset class TextToSpeechDataset(SpeechToTextDataset): def __init__( self, split: str, is_train_split: bool, cfg: S2TDataConfig, audio_paths: List[str], n_frames: List[int], src_texts: Optional[List[str]] = None, tgt_texts: Optional[List[str]] = None, speakers: Optional[List[str]] = None, src_langs: Optional[List[str]] = None, tgt_langs: Optional[List[str]] = None, ids: Optional[List[str]] = None, tgt_dict: Optional[Dictionary] = None, pre_tokenizer=None, bpe_tokenizer=None, n_frames_per_step=1, speaker_to_id=None, durations: Optional[List[List[int]]] = None, pitches: Optional[List[str]] = None, energies: Optional[List[str]] = None ): super(TextToSpeechDataset, self).__init__( split, is_train_split, cfg, audio_paths, n_frames, src_texts=src_texts, tgt_texts=tgt_texts, speakers=speakers, src_langs=src_langs, tgt_langs=tgt_langs, ids=ids, tgt_dict=tgt_dict, pre_tokenizer=pre_tokenizer, bpe_tokenizer=bpe_tokenizer, n_frames_per_step=n_frames_per_step, speaker_to_id=speaker_to_id ) self.durations = durations self.pitches = pitches self.energies = energies def __getitem__(self, index: int) -> TextToSpeechDatasetItem: s2t_item = super().__getitem__(index) duration, pitch, energy = None, None, None if self.durations is not None: duration = torch.tensor( self.durations[index] + [0], dtype=torch.long # pad 0 for EOS ) if self.pitches is not None: pitch = get_features_or_waveform(self.pitches[index]) pitch = torch.from_numpy( np.concatenate((pitch, [0])) # pad 0 for EOS ).float() if self.energies is not None: energy = get_features_or_waveform(self.energies[index]) energy = torch.from_numpy( np.concatenate((energy, [0])) # pad 0 for EOS ).float() return TextToSpeechDatasetItem( index=index, source=s2t_item.source, target=s2t_item.target, speaker_id=s2t_item.speaker_id, duration=duration, pitch=pitch, energy=energy ) def collater(self, samples: List[TextToSpeechDatasetItem]) -> Dict[str, Any]: if len(samples) == 0: return {} src_lengths, order = torch.tensor( [s.target.shape[0] for s in samples], dtype=torch.long ).sort(descending=True) id_ = torch.tensor([s.index for s in samples], dtype=torch.long).index_select(0, order) feat = _collate_frames( [s.source for s in samples], self.cfg.use_audio_input ).index_select(0, order) target_lengths = torch.tensor( [s.source.shape[0] for s in samples], dtype=torch.long ).index_select(0, order) src_tokens = fairseq_data_utils.collate_tokens( [s.target for s in samples], self.tgt_dict.pad(), self.tgt_dict.eos(), left_pad=False, move_eos_to_beginning=False, ).index_select(0, order) speaker = None if self.speaker_to_id is not None: speaker = torch.tensor( [s.speaker_id for s in samples], dtype=torch.long ).index_select(0, order).view(-1, 1) bsz, _, d = feat.size() prev_output_tokens = torch.cat( (feat.new_zeros((bsz, 1, d)), feat[:, :-1, :]), dim=1 ) durations, pitches, energies = None, None, None if self.durations is not None: durations = fairseq_data_utils.collate_tokens( [s.duration for s in samples], 0 ).index_select(0, order) assert src_tokens.shape[1] == durations.shape[1] if self.pitches is not None: pitches = _collate_frames([s.pitch for s in samples], True) pitches = pitches.index_select(0, order) assert src_tokens.shape[1] == pitches.shape[1] if self.energies is not None: energies = _collate_frames([s.energy for s in samples], True) energies = energies.index_select(0, order) assert src_tokens.shape[1] == energies.shape[1] src_texts = [self.tgt_dict.string(samples[i].target) for i in order] return { "id": id_, "net_input": { "src_tokens": src_tokens, "src_lengths": src_lengths, "prev_output_tokens": prev_output_tokens, }, "speaker": speaker, "target": feat, "durations": durations, "pitches": pitches, "energies": energies, "target_lengths": target_lengths, "ntokens": sum(target_lengths).item(), "nsentences": len(samples), "src_texts": src_texts, } def postprocess_results( dataset: TextToSpeechDataset, sample, hypos, resample_fn, dump_target ): def to_np(x): return None if x is None else x.detach().cpu().numpy() sample_ids = [dataset.ids[i] for i in sample["id"].tolist()] texts = sample["src_texts"] if "src_texts" in sample else [""] * len(hypos) attns = [to_np(hypo["attn"]) for hypo in hypos] eos_probs = [to_np(hypo.get("eos_prob", None)) for hypo in hypos] feat_preds = [to_np(hypo["feature"]) for hypo in hypos] wave_preds = [to_np(resample_fn(h["waveform"])) for h in hypos] if dump_target: feat_targs = [to_np(hypo["targ_feature"]) for hypo in hypos] wave_targs = [to_np(resample_fn(h["targ_waveform"])) for h in hypos] else: feat_targs = [None for _ in hypos] wave_targs = [None for _ in hypos] return zip(sample_ids, texts, attns, eos_probs, feat_preds, wave_preds, feat_targs, wave_targs)
null
182,866
import argparse import logging import os import csv import tempfile from collections import defaultdict from pathlib import Path import torchaudio try: import webrtcvad except ImportError: raise ImportError("Please install py-webrtcvad: pip install webrtcvad") import pandas as pd from tqdm import tqdm from examples.speech_synthesis.preprocessing.denoiser.pretrained import master64 import examples.speech_synthesis.preprocessing.denoiser.utils as utils from examples.speech_synthesis.preprocessing.vad import ( frame_generator, vad_collector, read_wave, write_wave, FS_MS, THRESHOLD, SCALE ) from examples.speech_to_text.data_utils import save_df_to_tsv log = logging.getLogger(__name__) PATHS = ["after_denoise", "after_vad"] MIN_T = 0.05 def generate_tmp_filename(extension="txt"): def convert_sr(inpath, sr, output_path=None): def apply_vad(vad, inpath): def write(wav, filename, sr=16_000): def master64(pretrained=True): def write_wave(path, audio, sample_rate): def save_df_to_tsv(dataframe, path: Union[str, Path]): def process(args): # making sure we are requested either denoise or vad if not args.denoise and not args.vad: log.error("No denoise or vad is requested.") return log.info("Creating out directories...") if args.denoise: out_denoise = Path(args.output_dir).absolute().joinpath(PATHS[0]) out_denoise.mkdir(parents=True, exist_ok=True) if args.vad: out_vad = Path(args.output_dir).absolute().joinpath(PATHS[1]) out_vad.mkdir(parents=True, exist_ok=True) log.info("Loading pre-trained speech enhancement model...") model = master64().to(args.device) log.info("Building the VAD model...") vad = webrtcvad.Vad(int(args.vad_agg_level)) # preparing the output dict output_dict = defaultdict(list) log.info(f"Parsing input manifest: {args.audio_manifest}") with open(args.audio_manifest, "r") as f: manifest_dict = csv.DictReader(f, delimiter="\t") for row in tqdm(manifest_dict): filename = str(row["audio"]) final_output = filename keep_sample = True n_frames = row["n_frames"] snr = -1 if args.denoise: output_path_denoise = out_denoise.joinpath(Path(filename).name) # convert to 16khz in case we use a differet sr tmp_path = convert_sr(final_output, 16000) # loading audio file and generating the enhanced version out, sr = torchaudio.load(tmp_path) out = out.to(args.device) estimate = model(out) estimate = (1 - args.dry_wet) * estimate + args.dry_wet * out write(estimate[0], str(output_path_denoise), sr) snr = utils.cal_snr(out, estimate) snr = snr.cpu().detach().numpy()[0][0] final_output = str(output_path_denoise) if args.vad: output_path_vad = out_vad.joinpath(Path(filename).name) sr = torchaudio.info(final_output).sample_rate if sr in [16000, 32000, 48000]: tmp_path = final_output elif sr < 16000: tmp_path = convert_sr(final_output, 16000) elif sr < 32000: tmp_path = convert_sr(final_output, 32000) else: tmp_path = convert_sr(final_output, 48000) # apply VAD segment, sample_rate = apply_vad(vad, tmp_path) if len(segment) < sample_rate * MIN_T: keep_sample = False print(( f"WARNING: skip {filename} because it is too short " f"after VAD ({len(segment) / sample_rate} < {MIN_T})" )) else: if sample_rate != sr: tmp_path = generate_tmp_filename("wav") write_wave(tmp_path, segment, sample_rate) convert_sr(tmp_path, sr, output_path=str(output_path_vad)) else: write_wave(str(output_path_vad), segment, sample_rate) final_output = str(output_path_vad) segment, _ = torchaudio.load(final_output) n_frames = segment.size(1) if keep_sample: output_dict["id"].append(row["id"]) output_dict["audio"].append(final_output) output_dict["n_frames"].append(n_frames) output_dict["tgt_text"].append(row["tgt_text"]) output_dict["speaker"].append(row["speaker"]) output_dict["src_text"].append(row["src_text"]) output_dict["snr"].append(snr) out_tsv_path = Path(args.output_dir) / Path(args.audio_manifest).name log.info(f"Saving manifest to {out_tsv_path.as_posix()}") save_df_to_tsv(pd.DataFrame.from_dict(output_dict), out_tsv_path)
null
182,872
import functools import logging from contextlib import contextmanager import inspect import time def copy_state(state): def serialize_model(model): args, kwargs = model._init_args_kwargs state = copy_state(model.state_dict()) return {"class": model.__class__, "args": args, "kwargs": kwargs, "state": state}
null
182,886
import logging from collections import namedtuple import torch import torch.nn as nn from fairseq import checkpoint_utils from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqDecoder, FairseqEncoderDecoderModel, register_model, register_model_architecture, ) from fairseq.models.fairseq_encoder import EncoderOut from fairseq.models.speech_to_text import ( TransformerDecoder, S2TTransformerEncoder, ) from fairseq.models.transformer import TransformerEncoder from fairseq.modules import ( TransformerEncoderLayer, GradMultiply, LayerNorm, ) def dualinputs2ttransformer_base(args): def dualinputs2ttransformer_s(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 256) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 256 * 4) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 4) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 4) args.dropout = getattr(args, "dropout", 0.1) args.speech_encoder_layers = getattr(args, "speech_encoder_layers", 7) args.text_encoder_layers = getattr(args, "text_encoder_layers", 7) args.decoder_layers = getattr(args, "decoder_layers", 7) dualinputs2ttransformer_base(args)
null
182,910
from typing import NamedTuple, List from urllib.parse import urlparse import os, sys import subprocess from subprocess import check_call, check_output import glob import wget import re import multiprocessing as mp from functools import partial import pathlib from collections import OrderedDict download_to = f'{to_data_path}/downloads' extract_to = f'{to_data_path}/extracted' def bar_custom(current, total, width=80): def get_extract_name(file_path): def extract_file(downloaded_file, extract_folder, get_extract_name=get_extract_name, debug=False): def extract_czeng17(extract_folder, debug=False): url = 'http://ufal.mff.cuni.cz/czeng/download.php?f=convert_czeng16_to_17.pl.zip' filename = f'{download_to}/convert_czeng16_to_17.pl.zip' extract_to = f'{extract_folder}/{get_extract_name(filename)}' script_path = f'{extract_to}/convert_czeng16_to_17.pl' if not os.path.exists(script_path): wget.download(url, filename, bar=bar_custom) extract_to = extract_file(f'{download_to}/convert_czeng16_to_17.pl.zip', extract_folder, get_extract_name=get_extract_name, debug=debug) return script_path
null
182,934
import math import sys from fractions import Fraction import warnings from collections import Counter from nltk.translate.bleu_score import modified_precision, closest_ref_length, brevity_penalty, SmoothingFunction def corpus_bleu( list_of_references, hypotheses, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=None, auto_reweigh=False, averaging_mode="geometric", no_length_penalty=False ): def sentence_bleu( references, hypothesis, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=None, auto_reweigh=False, averaging_mode="geometric", no_length_penalty=False ): return corpus_bleu( [references], [hypothesis], weights, smoothing_function, auto_reweigh, averaging_mode, no_length_penalty )
null
182,950
import torch from examples.textless_nlp.gslm.unit2speech.tacotron2.model import Tacotron2 from examples.textless_nlp.gslm.unit2speech.tacotron2.waveglow_denoiser import ( Denoiser, ) class Tacotron2(nn.Module): def __init__(self, hparams): def parse_batch(self, batch): def parse_output(self, outputs, output_lengths=None): def forward(self, inputs): def inference(self, inputs, obs_labels=None, lat=None, ret_has_eos=False): def load_tacotron(tacotron_model_path, max_decoder_steps): ckpt_dict = torch.load(tacotron_model_path) hparams = ckpt_dict["hparams"] hparams.max_decoder_steps = max_decoder_steps sr = hparams.sampling_rate model = Tacotron2(hparams) model.load_state_dict(ckpt_dict["model_dict"]) model = model.cuda().eval().half() return model, sr, hparams
null
183,011
import csv from pathlib import Path import zipfile from functools import reduce from multiprocessing import cpu_count from typing import Any, Dict, List, Optional, Union import io import numpy as np import pandas as pd import sentencepiece as sp from fairseq.data.audio.audio_utils import ( convert_waveform, _get_kaldi_fbank, _get_torchaudio_fbank, is_npy_data, is_sf_audio_data ) import torch import soundfile as sf from tqdm import tqdm def convert_waveform( waveform: Union[np.ndarray, torch.Tensor], sample_rate: int, normalize_volume: bool = False, to_mono: bool = False, to_sample_rate: Optional[int] = None ) -> Tuple[Union[np.ndarray, torch.Tensor], int]: """convert a waveform: - to a target sample rate - from multi-channel to mono channel - volume normalization Args: waveform (numpy.ndarray or torch.Tensor): 2D original waveform (channels x length) sample_rate (int): original sample rate normalize_volume (bool): perform volume normalization to_mono (bool): convert to mono channel if having multiple channels to_sample_rate (Optional[int]): target sample rate Returns: waveform (numpy.ndarray): converted 2D waveform (channels x length) sample_rate (float): target sample rate """ try: import torchaudio.sox_effects as ta_sox except ImportError: raise ImportError("Please install torchaudio: pip install torchaudio") effects = [] if normalize_volume: effects.append(["gain", "-n"]) if to_sample_rate is not None and to_sample_rate != sample_rate: effects.append(["rate", f"{to_sample_rate}"]) if to_mono and waveform.shape[0] > 1: effects.append(["channels", "1"]) if len(effects) > 0: is_np_input = isinstance(waveform, np.ndarray) _waveform = torch.from_numpy(waveform) if is_np_input else waveform converted, converted_sample_rate = ta_sox.apply_effects_tensor( _waveform, sample_rate, effects ) if is_np_input: converted = converted.numpy() return converted, converted_sample_rate return waveform, sample_rate def _get_kaldi_fbank( waveform: np.ndarray, sample_rate: int, n_bins=80 ) -> Optional[np.ndarray]: """Get mel-filter bank features via PyKaldi.""" try: from kaldi.feat.fbank import FbankOptions, Fbank from kaldi.feat.mel import MelBanksOptions from kaldi.feat.window import FrameExtractionOptions from kaldi.matrix import Vector mel_opts = MelBanksOptions() mel_opts.num_bins = n_bins frame_opts = FrameExtractionOptions() frame_opts.samp_freq = sample_rate opts = FbankOptions() opts.mel_opts = mel_opts opts.frame_opts = frame_opts fbank = Fbank(opts=opts) features = fbank.compute(Vector(waveform.squeeze()), 1.0).numpy() return features except ImportError: return None def _get_torchaudio_fbank( waveform: np.ndarray, sample_rate, n_bins=80 ) -> Optional[np.ndarray]: """Get mel-filter bank features via TorchAudio.""" try: import torchaudio.compliance.kaldi as ta_kaldi waveform = torch.from_numpy(waveform) features = ta_kaldi.fbank( waveform, num_mel_bins=n_bins, sample_frequency=sample_rate ) return features.numpy() except ImportError: return None def extract_fbank_features( waveform: torch.FloatTensor, sample_rate: int, output_path: Optional[Path] = None, n_mel_bins: int = 80, overwrite: bool = False, ): if output_path is not None and output_path.is_file() and not overwrite: return _waveform, _ = convert_waveform(waveform, sample_rate, to_mono=True) # Kaldi compliance: 16-bit signed integers _waveform = _waveform * (2 ** 15) _waveform = _waveform[0].numpy() features = _get_kaldi_fbank(_waveform, sample_rate, n_mel_bins) if features is None: features = _get_torchaudio_fbank(_waveform, sample_rate, n_mel_bins) if features is None: raise ImportError( "Please install pyKaldi or torchaudio to enable fbank feature extraction" ) if output_path is not None: np.save(output_path.as_posix(), features) return features
null
183,013
import csv from pathlib import Path import zipfile from functools import reduce from multiprocessing import cpu_count from typing import Any, Dict, List, Optional, Union import io import numpy as np import pandas as pd import sentencepiece as sp from fairseq.data.audio.audio_utils import ( convert_waveform, _get_kaldi_fbank, _get_torchaudio_fbank, is_npy_data, is_sf_audio_data ) import torch import soundfile as sf from tqdm import tqdm def is_npy_data(data: bytes) -> bool: return data[0] == 147 and data[1] == 78 def is_sf_audio_data(data: bytes) -> bool: is_wav = data[0] == 82 and data[1] == 73 and data[2] == 70 is_flac = data[0] == 102 and data[1] == 76 and data[2] == 97 is_ogg = data[0] == 79 and data[1] == 103 and data[2] == 103 return is_wav or is_flac or is_ogg def get_zip_manifest( zip_path: Path, zip_root: Optional[Path] = None, is_audio=False ): _zip_path = Path.joinpath(zip_root or Path(""), zip_path) with zipfile.ZipFile(_zip_path, mode="r") as f: info = f.infolist() paths, lengths = {}, {} for i in tqdm(info): utt_id = Path(i.filename).stem offset, file_size = i.header_offset + 30 + len(i.filename), i.file_size paths[utt_id] = f"{zip_path.as_posix()}:{offset}:{file_size}" with open(_zip_path, "rb") as f: f.seek(offset) byte_data = f.read(file_size) assert len(byte_data) > 1 if is_audio: assert is_sf_audio_data(byte_data), i else: assert is_npy_data(byte_data), i byte_data_fp = io.BytesIO(byte_data) if is_audio: lengths[utt_id] = sf.info(byte_data_fp).frames else: lengths[utt_id] = np.load(byte_data_fp).shape[0] return paths, lengths
null
183,023
import argparse import logging import os from pathlib import Path import shutil from itertools import groupby from tempfile import NamedTemporaryFile from typing import Tuple import pandas as pd import soundfile as sf from examples.speech_to_text.data_utils import ( create_zip, extract_fbank_features, filter_manifest_df, gen_config_yaml, gen_vocab, get_zip_manifest, load_df_from_tsv, save_df_to_tsv, ) import torch from torch.utils.data import Dataset from tqdm import tqdm from fairseq.data.audio.audio_utils import get_waveform, convert_waveform class mTEDx(Dataset): def __init__(self, root: str, lang: str, split: str) -> None: def __getitem__( self, n: int ) -> Tuple[torch.Tensor, int, str, str, str, str, str]: def __len__(self) -> int: def gen_vocab( input_path: Path, output_path_prefix: Path, model_type="bpe", vocab_size=1000, special_symbols: Optional[List[str]] = None ): def gen_config_yaml( manifest_root: Path, spm_filename: Optional[str] = None, vocab_name: Optional[str] = None, yaml_filename: str = "config.yaml", specaugment_policy: Optional[str] = "lb", prepend_tgt_lang_tag: bool = False, sampling_alpha: Optional[float] = None, input_channels: Optional[int] = 1, input_feat_per_channel: Optional[int] = 80, audio_root: str = "", cmvn_type: str = "utterance", gcmvn_path: Optional[Path] = None, extra=None ): def load_df_from_tsv(path: Union[str, Path]) -> pd.DataFrame: def process_joint(args): cur_root = Path(args.data_root) assert all((cur_root / f"{lang}").is_dir() for lang in mTEDx.LANGPAIRS), \ "do not have downloaded data available for all languages" # Generate vocab vocab_size_str = "" if args.vocab_type == "char" else str(args.vocab_size) spm_filename_prefix = f"spm_{args.vocab_type}{vocab_size_str}_{args.task}" with NamedTemporaryFile(mode="w") as f: for lang in mTEDx.LANGPAIRS: tsv_path = cur_root / f"{lang}" / f"train_{args.task}.tsv" df = load_df_from_tsv(tsv_path) for t in df["tgt_text"]: f.write(t + "\n") special_symbols = None if args.joint: # Add tgt_lang tags to dict special_symbols = list( {f'<lang:{lang.split("-")[1]}>' for lang in mTEDx.LANGPAIRS} ) gen_vocab( Path(f.name), cur_root / spm_filename_prefix, args.vocab_type, args.vocab_size, special_symbols=special_symbols ) # Generate config YAML gen_config_yaml( cur_root, spm_filename=spm_filename_prefix + ".model", yaml_filename=f"config_{args.task}.yaml", specaugment_policy="ld", prepend_tgt_lang_tag=(args.joint), ) # Make symbolic links to manifests for lang in mTEDx.LANGPAIRS: for split in mTEDx.SPLITS: src_path = cur_root / f"{lang}" / f"{split}_{args.task}.tsv" desc_path = cur_root / f"{split}_{lang}_{args.task}.tsv" if not desc_path.is_symlink(): os.symlink(src_path, desc_path)
null
183,036
from fairseq import tasks import numpy as np import logging import random from fairseq import options import torch import os import soundfile as sf from fairseq.data.audio.audio_utils import ( get_waveform, parse_path, ) def get_waveform( path_or_fp: Union[str, BinaryIO], normalization: bool = True, mono: bool = True, frames: int = -1, start: int = 0, always_2d: bool = True, output_sample_rate: Optional[int] = None, normalize_volume: bool = False ) -> Tuple[np.ndarray, int]: """Get the waveform and sample rate of a 16-bit WAV/FLAC/OGG Vorbis audio. Args: path_or_fp (str or BinaryIO): the path or file-like object normalization (bool): normalize values to [-1, 1] (Default: True) mono (bool): convert multi-channel audio to mono-channel one frames (int): the number of frames to read. (-1 for reading all) start (int): Where to start reading. A negative value counts from the end. always_2d (bool): always return 2D array even for mono-channel audios output_sample_rate (Optional[int]): output sample rate normalize_volume (bool): normalize volume Returns: waveform (numpy.ndarray): 1D or 2D waveform (channels x length) sample_rate (float): sample rate """ if isinstance(path_or_fp, str): ext = Path(path_or_fp).suffix if ext not in SF_AUDIO_FILE_EXTENSIONS: raise ValueError(f"Unsupported audio format: {ext}") try: import soundfile as sf except ImportError: raise ImportError("Please install soundfile: pip install soundfile") waveform, sample_rate = sf.read( path_or_fp, dtype="float32", always_2d=True, frames=frames, start=start ) waveform = waveform.T # T x C -> C x T waveform, sample_rate = convert_waveform( waveform, sample_rate, normalize_volume=normalize_volume, to_mono=mono, to_sample_rate=output_sample_rate ) if not normalization: waveform *= 2 ** 15 # denormalized to 16-bit signed integers if not always_2d: waveform = waveform.squeeze(axis=0) return waveform, sample_rate def parse_path(path: str) -> Tuple[str, List[int]]: """Parse data path which is either a path to 1. a .npy/.wav/.flac/.ogg file 2. a stored ZIP file with slicing info: "[zip_path]:[offset]:[length]" Args: path (str): the data path to parse Returns: file_path (str): the file path slice_ptr (list of int): empty in case 1; byte offset and length for the slice in case 2 """ if Path(path).suffix in FEATURE_OR_SF_AUDIO_FILE_EXTENSIONS: _path, slice_ptr = path, [] else: _path, *slice_ptr = path.split(":") if not Path(_path).is_file(): raise FileNotFoundError(f"File not found: {_path}") assert len(slice_ptr) in {0, 2}, f"Invalid path: {path}" slice_ptr = [int(i) for i in slice_ptr] return _path, slice_ptr The provided code snippet includes necessary dependencies for implementing the `load_dataset_raw_to_waveforms` function. Write a Python function `def load_dataset_raw_to_waveforms( file_name, dataset_size=None, need_waveform=True, sample_rate=16000, read_using_soundfile=False, )` to solve the following problem: Load raw dataset from w2v tsv file. Optionally get waveforms Here is the function: def load_dataset_raw_to_waveforms( file_name, dataset_size=None, need_waveform=True, sample_rate=16000, read_using_soundfile=False, ): """Load raw dataset from w2v tsv file. Optionally get waveforms""" data = [] with open(file_name, "r") as fp: lines = fp.readlines() data = [ os.path.join(lines[0].strip(), line.strip().split("\t")[0]) for line in lines[1:] ] if dataset_size: data = data[:dataset_size] if not need_waveform: return data features = [] if read_using_soundfile: for _i, d in enumerate(data): wav = sf.read(d)[0] if wav.ndim == 2: wav = wav.mean(-1) features.append(torch.from_numpy(wav).float().view(1, -1)) else: for i, d in enumerate(data): _path, slice_ptr = parse_path(d) if len(slice_ptr) == 0: feat = get_waveform( _path, always_2d=True, output_sample_rate=sample_rate )[0] features.append( { "id": i, "net_input": { "src_tokens": torch.tensor(feat), "src_lengths": torch.tensor([feat.shape[1]]), }, } ) else: raise Exception("Currently unsupported data format") return features
Load raw dataset from w2v tsv file. Optionally get waveforms
183,138
import torch.nn as nn from .learned_positional_embedding import LearnedPositionalEmbedding from .sinusoidal_positional_embedding import SinusoidalPositionalEmbedding class LearnedPositionalEmbedding(nn.Embedding): """ This module learns positional embeddings up to a fixed maximum size. Padding ids are ignored by either offsetting based on padding_idx or by setting padding_idx to None and ensuring that the appropriate position ids are passed to the forward function. """ def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx: int): super().__init__(num_embeddings, embedding_dim, padding_idx) self.onnx_trace = False if self.padding_idx is not None: self.max_positions = self.num_embeddings - self.padding_idx - 1 else: self.max_positions = self.num_embeddings def forward( self, input: Tensor, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, positions: Optional[Tensor] = None, ): """Input is expected to be of size [bsz x seqlen].""" assert (positions is None) or ( self.padding_idx is None ), "If positions is pre-computed then padding_idx should not be set." if positions is None: if incremental_state is not None and len(incremental_state) > 0: # positions is the same for every token when decoding a single step # Without the int() cast, it doesn't work in some cases when exporting to ONNX positions = torch.zeros( (1, 1), device=input.device, dtype=input.dtype ).fill_(int(self.padding_idx + input.size(1))) else: positions = utils.make_positions( input, self.padding_idx, onnx_trace=self.onnx_trace ) return F.embedding( positions, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse, ) class SinusoidalPositionalEmbedding(nn.Module): """This module produces sinusoidal positional embeddings of any length. Padding symbols are ignored. """ def __init__(self, embedding_dim, padding_idx, init_size=1024): super().__init__() self.embedding_dim = embedding_dim self.padding_idx = padding_idx if padding_idx is not None else 0 self.weights = SinusoidalPositionalEmbedding.get_embedding( init_size, embedding_dim, padding_idx ) self.onnx_trace = False self.register_buffer("_float_tensor", torch.FloatTensor(1)) self.max_positions = int(1e5) def prepare_for_onnx_export_(self): self.onnx_trace = True def get_embedding( num_embeddings: int, embedding_dim: int, padding_idx: Optional[int] = None ): """Build sinusoidal embeddings. This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of "Attention Is All You Need". """ half_dim = embedding_dim // 2 emb = math.log(10000) / (half_dim - 1) emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb) emb = torch.arange(num_embeddings, dtype=torch.float).unsqueeze( 1 ) * emb.unsqueeze(0) emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1).view( num_embeddings, -1 ) if embedding_dim % 2 == 1: # zero pad emb = torch.cat([emb, torch.zeros(num_embeddings, 1)], dim=1) if padding_idx is not None: emb[padding_idx, :] = 0 return emb def forward( self, input, incremental_state: Optional[Any] = None, timestep: Optional[Tensor] = None, positions: Optional[Any] = None, ): """Input is expected to be of size [bsz x seqlen].""" bspair = torch.onnx.operators.shape_as_tensor(input) bsz, seq_len = bspair[0], bspair[1] max_pos = self.padding_idx + 1 + seq_len if self.weights is None or max_pos > self.weights.size(0): # recompute/expand embeddings if needed self.weights = SinusoidalPositionalEmbedding.get_embedding( max_pos, self.embedding_dim, self.padding_idx ) self.weights = self.weights.to(self._float_tensor) if incremental_state is not None and len(incremental_state) > 0: # positions is the same for every token when decoding a single step pos = timestep.view(-1)[0] + 1 if timestep is not None else seq_len if self.onnx_trace: return ( self.weights.index_select(index=self.padding_idx + pos, dim=0) .unsqueeze(1) .repeat(bsz, 1, 1) ) return self.weights[self.padding_idx + pos, :].expand(bsz, 1, -1) positions = utils.make_positions( input, self.padding_idx, onnx_trace=self.onnx_trace ) if self.onnx_trace: flat_embeddings = self.weights.detach().index_select(0, positions.view(-1)) embedding_shape = torch.cat( (bsz.view(1), seq_len.view(1), torch.tensor([-1], dtype=torch.long)) ) embeddings = torch.onnx.operators.reshape_from_tensor_shape( flat_embeddings, embedding_shape ) return embeddings return ( self.weights.index_select(0, positions.view(-1)) .view(bsz, seq_len, -1) .detach() ) def PositionalEmbedding( num_embeddings: int, embedding_dim: int, padding_idx: int, learned: bool = False, ): if learned: # if padding_idx is specified then offset the embedding ids by # this index and adjust num_embeddings appropriately # TODO: The right place for this offset would be inside # LearnedPositionalEmbedding. Move this there for a cleaner implementation. if padding_idx is not None: num_embeddings = num_embeddings + padding_idx + 1 m = LearnedPositionalEmbedding(num_embeddings, embedding_dim, padding_idx) nn.init.normal_(m.weight, mean=0, std=embedding_dim ** -0.5) if padding_idx is not None: nn.init.constant_(m.weight[padding_idx], 0) else: m = SinusoidalPositionalEmbedding( embedding_dim, padding_idx, init_size=num_embeddings + padding_idx + 1, ) return m
null
183,139
import ast import collections import contextlib import logging import numpy as np import os import re import time import traceback from collections import OrderedDict from typing import Any, Dict, Optional, Union import torch from fairseq.data import data_utils from fairseq.dataclass.configs import CheckpointConfig from fairseq.dataclass.utils import ( convert_namespace_to_omegaconf, overwrite_args_by_name, ) from fairseq.distributed.fully_sharded_data_parallel import FSDP, has_FSDP from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.models import FairseqDecoder, FairseqEncoder from omegaconf import DictConfig, open_dict, OmegaConf from fairseq.ds_trainer import DeepSpeedTrainer logger = logging.getLogger(__name__) def save_checkpoint(cfg: CheckpointConfig, trainer, epoch_itr, val_loss): from fairseq import meters # only one worker should attempt to create the required dir if trainer.data_parallel_rank == 0: os.makedirs(cfg.save_dir, exist_ok=True) prev_best = getattr(save_checkpoint, "best", val_loss) if val_loss is not None: best_function = max if cfg.maximize_best_checkpoint_metric else min save_checkpoint.best = best_function(val_loss, prev_best) if cfg.no_save: return trainer.consolidate_optimizer() # TODO(SS): do we need this if no_save_optimizer_state extra_state = {"train_iterator": epoch_itr.state_dict(), "val_loss": val_loss} if hasattr(save_checkpoint, "best"): extra_state.update({"best": save_checkpoint.best}) if getattr(epoch_itr, "sharded_checkpoint", False): local_state_dict = extra_state["train_iterator"] all_state_dicts = distributed_utils.all_gather_list( local_state_dict, max_size=getattr(trainer.cfg.common, "all_gather_list_size", 16384), group=trainer.data_parallel_process_group, ) extra_state["train_iterator"] = all_state_dicts if not trainer.should_save_checkpoint_on_current_rank: if trainer.always_call_state_dict_during_save_checkpoint: trainer.state_dict() return write_timer = meters.StopwatchMeter() write_timer.start() epoch = epoch_itr.epoch end_of_epoch = epoch_itr.end_of_epoch() updates = trainer.get_num_updates() logger.info(f"Preparing to save checkpoint for epoch {epoch} @ {updates} updates") def is_better(a, b): return a >= b if cfg.maximize_best_checkpoint_metric else a <= b suffix = trainer.checkpoint_suffix checkpoint_conds = collections.OrderedDict() if isinstance(trainer, DeepSpeedTrainer): checkpoint_conds["checkpoints"] = ( not end_of_epoch and cfg.save_interval_updates > 0 and updates % cfg.save_interval_updates == 0 ) else: checkpoint_conds["checkpoint{}{}.pt".format(epoch, suffix)] = ( end_of_epoch and not cfg.no_epoch_checkpoints and epoch % cfg.save_interval == 0 ) checkpoint_conds["checkpoint_{}_{}{}.pt".format(epoch, updates, suffix)] = ( not end_of_epoch and cfg.save_interval_updates > 0 and updates % cfg.save_interval_updates == 0 ) checkpoint_conds["checkpoint_best{}.pt".format(suffix)] = val_loss is not None and ( not hasattr(save_checkpoint, "best") or is_better(val_loss, save_checkpoint.best) ) if val_loss is not None and cfg.keep_best_checkpoints > 0: worst_best = getattr(save_checkpoint, "best", None) chkpts = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint\.best_{}_(\d+\.?\d*){}\.pt".format( cfg.best_checkpoint_metric, suffix ), ) if len(chkpts) > 0: p = chkpts[-1] if cfg.maximize_best_checkpoint_metric else chkpts[0] worst_best = float(p.rsplit("_")[-1].replace("{}.pt".format(suffix), "")) # add random digits to resolve ties with data_utils.numpy_seed(epoch, updates, val_loss): rand_sfx = np.random.randint(0, cfg.keep_best_checkpoints) checkpoint_conds[ "checkpoint.best_{}_{:.3f}{}{}.pt".format( cfg.best_checkpoint_metric, val_loss, rand_sfx, suffix ) ] = worst_best is None or is_better(val_loss, worst_best) checkpoint_conds[ "checkpoint_last{}.pt".format(suffix) ] = not cfg.no_last_checkpoints checkpoints = [ os.path.join(cfg.save_dir, fn) for fn, cond in checkpoint_conds.items() if cond ] if len(checkpoints) > 0: trainer.save_checkpoint(checkpoints[0], extra_state) if not isinstance(trainer, DeepSpeedTrainer): for cp in checkpoints[1:]: if cfg.write_checkpoints_asynchronously: # TODO[ioPath]: Need to implement a delayed asynchronous # file copying/moving feature. logger.warning( f"ioPath is not copying {checkpoints[0]} to {cp} " "since async write mode is on." ) else: assert PathManager.copy( checkpoints[0], cp, overwrite=True ), f"Failed to copy {checkpoints[0]} to {cp}" write_timer.stop() logger.info( "Saved checkpoint {} (epoch {} @ {} updates, score {}) (writing took {} seconds)".format( checkpoints[0], epoch, updates, val_loss, write_timer.sum ) ) if not end_of_epoch and cfg.keep_interval_updates > 0: # remove old checkpoints; checkpoints are sorted in descending order if cfg.keep_interval_updates_pattern == -1: checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint_\d+_(\d+){}\.pt".format(suffix) ) else: checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint_\d+_(\d+){}\.pt".format(suffix), keep_match=True, ) checkpoints = [ x[0] for x in checkpoints if x[1] % cfg.keep_interval_updates_pattern != 0 ] for old_chk in checkpoints[cfg.keep_interval_updates :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) if cfg.keep_last_epochs > 0: # remove old epoch checkpoints; checkpoints are sorted in descending order checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint(\d+){}\.pt".format(suffix) ) for old_chk in checkpoints[cfg.keep_last_epochs :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) if cfg.keep_best_checkpoints > 0: # only keep the best N checkpoints according to validation metric checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint\.best_{}_(\d+\.?\d*){}\.pt".format( cfg.best_checkpoint_metric, suffix ), ) if not cfg.maximize_best_checkpoint_metric: checkpoints = checkpoints[::-1] for old_chk in checkpoints[cfg.keep_best_checkpoints :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) class CheckpointConfig(FairseqDataclass): save_dir: str = field( default="checkpoints", metadata={"help": "path to save checkpoints"} ) restore_file: str = field( default="checkpoint_last.pt", metadata={ "help": "filename from which to load checkpoint " "(default: <save-dir>/checkpoint_last.pt" }, ) finetune_from_model: Optional[str] = field( default=None, metadata={ "help": "finetune from a pretrained model; note that meters and lr scheduler will be reset" }, ) reset_dataloader: bool = field( default=False, metadata={ "help": "if set, does not reload dataloader state from the checkpoint" }, ) reset_lr_scheduler: bool = field( default=False, metadata={ "help": "if set, does not load lr scheduler state from the checkpoint" }, ) reset_meters: bool = field( default=False, metadata={"help": "if set, does not load meters from the checkpoint"}, ) reset_optimizer: bool = field( default=False, metadata={"help": "if set, does not load optimizer state from the checkpoint"}, ) optimizer_overrides: str = field( default="{}", metadata={ "help": "a dictionary used to override optimizer args when loading a checkpoint" }, ) save_interval: int = field( default=1, metadata={"help": "save a checkpoint every N epochs"} ) save_interval_updates: int = field( default=0, metadata={"help": "save a checkpoint (and validate) every N updates"} ) keep_interval_updates: int = field( default=-1, metadata={ "help": "keep the last N checkpoints saved with --save-interval-updates" }, ) keep_interval_updates_pattern: int = field( default=-1, metadata={ "help": "when used with --keep-interval-updates, skips deleting " "any checkpoints with update X where " "X %% keep_interval_updates_pattern == 0" }, ) keep_last_epochs: int = field( default=-1, metadata={"help": "keep last N epoch checkpoints"} ) keep_best_checkpoints: int = field( default=-1, metadata={"help": "keep best N checkpoints based on scores"} ) no_save: bool = field( default=False, metadata={"help": "don't save models or checkpoints"} ) no_epoch_checkpoints: bool = field( default=False, metadata={"help": "only store last and best checkpoints"} ) no_last_checkpoints: bool = field( default=False, metadata={"help": "don't store last checkpoints"} ) no_save_optimizer_state: bool = field( default=False, metadata={"help": "don't save optimizer-state as part of checkpoint"}, ) best_checkpoint_metric: str = field( default="loss", metadata={"help": 'metric to use for saving "best" checkpoints'} ) maximize_best_checkpoint_metric: bool = field( default=False, metadata={ "help": 'select the largest metric value for saving "best" checkpoints' }, ) patience: int = field( default=-1, metadata={ "help": ( "early stop training if valid performance doesn't " "improve for N consecutive validation runs; note " "that this is influenced by --validate-interval" ) }, ) checkpoint_suffix: str = field( default="", metadata={"help": "suffix to add to the checkpoint file name"} ) checkpoint_shard_count: int = field( default=1, metadata={ "help": "Number of shards containing the checkpoint - " "if the checkpoint is over 300GB, it is preferable " "to split it into shards to prevent OOM on CPU while loading " "the checkpoint" }, ) load_checkpoint_on_all_dp_ranks: bool = field( default=False, metadata={ "help": "load checkpoints on all data parallel devices " "(default: only load on rank 0 and broadcast to other devices)" }, ) write_checkpoints_asynchronously: bool = field( default=False, metadata={ "help": ( "Write checkpoints asynchronously in a separate " "thread. NOTE: This feature is currently being tested." ), "argparse_alias": "--save-async", }, ) model_parallel_size: int = II("common.model_parallel_size") class PathManager: """ Wrapper for insulating OSS I/O (using Python builtin operations) from iopath's PathManager abstraction (for transparently handling various internal backends). """ def open( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): if IOPathManager: return IOPathManager.open( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) return open( path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) def copy(src_path: str, dst_path: str, overwrite: bool = False) -> bool: if IOPathManager: return IOPathManager.copy( src_path=src_path, dst_path=dst_path, overwrite=overwrite ) return shutil.copyfile(src_path, dst_path) def get_local_path(path: str, **kwargs) -> str: if IOPathManager: return IOPathManager.get_local_path(path, **kwargs) return path def exists(path: str) -> bool: if IOPathManager: return IOPathManager.exists(path) return os.path.exists(path) def isfile(path: str) -> bool: if IOPathManager: return IOPathManager.isfile(path) return os.path.isfile(path) def ls(path: str) -> List[str]: if IOPathManager: return IOPathManager.ls(path) return os.listdir(path) def mkdirs(path: str) -> None: if IOPathManager: return IOPathManager.mkdirs(path) os.makedirs(path, exist_ok=True) def rm(path: str) -> None: if IOPathManager: return IOPathManager.rm(path) os.remove(path) def chmod(path: str, mode: int) -> None: if not PathManager.path_requires_pathmanager(path): os.chmod(path, mode) def register_handler(handler) -> None: if IOPathManager: return IOPathManager.register_handler(handler=handler) def copy_from_local( local_path: str, dst_path: str, overwrite: bool = False, **kwargs ) -> None: if IOPathManager: return IOPathManager.copy_from_local( local_path=local_path, dst_path=dst_path, overwrite=overwrite, **kwargs ) return shutil.copyfile(local_path, dst_path) def path_requires_pathmanager(path: str) -> bool: """Do we require PathManager to access given path?""" if IOPathManager: for p in IOPathManager._path_handlers.keys(): if path.startswith(p): return True return False def supports_rename(path: str) -> bool: # PathManager doesn't yet support renames return not PathManager.path_requires_pathmanager(path) def rename(src: str, dst: str): os.rename(src, dst) """ ioPath async PathManager methods: """ def opena( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): """ Return file descriptor with asynchronous write operations. """ global IOPathManager if not IOPathManager: logging.info("ioPath is initializing PathManager.") try: from iopath.common.file_io import PathManager IOPathManager = PathManager() except Exception: logging.exception("Failed to initialize ioPath PathManager object.") return IOPathManager.opena( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) def async_close() -> bool: """ Wait for files to be written and clean up asynchronous PathManager. NOTE: `PathManager.async_close()` must be called at the end of any script that uses `PathManager.opena(...)`. """ global IOPathManager if IOPathManager: return IOPathManager.async_close() return False The provided code snippet includes necessary dependencies for implementing the `load_checkpoint` function. Write a Python function `def load_checkpoint(cfg: CheckpointConfig, trainer, **passthrough_args)` to solve the following problem: Load a checkpoint and restore the training iterator. *passthrough_args* will be passed through to ``trainer.get_train_iterator``. Here is the function: def load_checkpoint(cfg: CheckpointConfig, trainer, **passthrough_args): """ Load a checkpoint and restore the training iterator. *passthrough_args* will be passed through to ``trainer.get_train_iterator``. """ reset_optimizer = cfg.reset_optimizer reset_lr_scheduler = cfg.reset_lr_scheduler optimizer_overrides = ast.literal_eval(cfg.optimizer_overrides) reset_meters = cfg.reset_meters reset_dataloader = cfg.reset_dataloader if cfg.finetune_from_model is not None and ( reset_optimizer or reset_lr_scheduler or reset_meters or reset_dataloader ): raise ValueError( "--finetune-from-model can not be set together with either --reset-optimizer" " or reset_lr_scheduler or reset_meters or reset_dataloader" ) suffix = trainer.checkpoint_suffix if isinstance(trainer, DeepSpeedTrainer): checkpoint_path = os.path.join(cfg.save_dir, "checkpoints/") else: if ( cfg.restore_file == "checkpoint_last.pt" ): # default value of restore_file is 'checkpoint_last.pt' checkpoint_path = os.path.join( cfg.save_dir, "checkpoint_last{}.pt".format(suffix) ) first_launch = not PathManager.exists(checkpoint_path) if cfg.finetune_from_model is not None and first_launch: # if there is no last checkpoint to restore, start the finetune from pretrained model # else just use usual logic to load checkpoint, e.g. restart from last checkpoint and etc. if PathManager.exists(cfg.finetune_from_model): checkpoint_path = cfg.finetune_from_model reset_optimizer = True reset_lr_scheduler = True reset_meters = True reset_dataloader = True logger.info( f"loading pretrained model from {checkpoint_path}: " "optimizer, lr scheduler, meters, dataloader will be reset" ) else: raise ValueError( f"--funetune-from-model {cfg.finetune_from_model} does not exist" ) elif suffix is not None: checkpoint_path = cfg.restore_file.replace(".pt", suffix + ".pt") else: checkpoint_path = cfg.restore_file if cfg.restore_file != "checkpoint_last.pt" and cfg.finetune_from_model: raise ValueError( "--finetune-from-model and --restore-file (non-default value) " "can not be specified together: " + str(cfg) ) extra_state = trainer.load_checkpoint( checkpoint_path, reset_optimizer, reset_lr_scheduler, optimizer_overrides, reset_meters=reset_meters, ) if ( extra_state is not None and "best" in extra_state and not reset_optimizer and not reset_meters ): save_checkpoint.best = extra_state["best"] if extra_state is not None and not reset_dataloader: # restore iterator from checkpoint itr_state = extra_state["train_iterator"] epoch_itr = trainer.get_train_iterator( epoch=itr_state.get("epoch", 1), load_dataset=True, **passthrough_args ) epoch_itr.load_state_dict(itr_state) else: epoch_itr = trainer.get_train_iterator( epoch=1, load_dataset=True, **passthrough_args ) trainer.lr_step(epoch_itr.epoch) return extra_state, epoch_itr
Load a checkpoint and restore the training iterator. *passthrough_args* will be passed through to ``trainer.get_train_iterator``.
183,140
import ast import collections import contextlib import logging import numpy as np import os import re import time import traceback from collections import OrderedDict from typing import Any, Dict, Optional, Union import torch from fairseq.data import data_utils from fairseq.dataclass.configs import CheckpointConfig from fairseq.dataclass.utils import ( convert_namespace_to_omegaconf, overwrite_args_by_name, ) from fairseq.distributed.fully_sharded_data_parallel import FSDP, has_FSDP from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.models import FairseqDecoder, FairseqEncoder from omegaconf import DictConfig, open_dict, OmegaConf from fairseq.ds_trainer import DeepSpeedTrainer def load_model_ensemble_and_task( filenames, arg_overrides: Optional[Dict[str, Any]] = None, task=None, strict=True, suffix="", num_shards=1, state=None, ): assert state is None or len(filenames) == 1 from fairseq import tasks assert not ( strict and num_shards > 1 ), "Cannot load state dict with strict=True and checkpoint shards > 1" ensemble = [] cfg = None for filename in filenames: orig_filename = filename model_shard_state = {"shard_weights": [], "shard_metadata": []} assert num_shards > 0 st = time.time() for shard_idx in range(num_shards): filename = get_maybe_sharded_checkpoint_filename( orig_filename, suffix, shard_idx, num_shards ) if not PathManager.exists(filename): raise IOError("Model file not found: {}".format(filename)) if state is None: state = load_checkpoint_to_cpu(filename, arg_overrides) if "args" in state and state["args"] is not None: cfg = convert_namespace_to_omegaconf(state["args"]) elif "cfg" in state and state["cfg"] is not None: cfg = state["cfg"] else: raise RuntimeError( f"Neither args nor cfg exist in state keys = {state.keys()}" ) if task is None: task = tasks.setup_task(cfg.task) if "task_state" in state: task.load_state_dict(state["task_state"]) if "fsdp_metadata" in state and num_shards > 1: model_shard_state["shard_weights"].append(state["model"]) model_shard_state["shard_metadata"].append(state["fsdp_metadata"]) # check FSDP import before the code goes too far if not has_FSDP: raise ImportError( "Cannot find FullyShardedDataParallel. " "Please install fairscale with: pip install fairscale" ) if shard_idx == num_shards - 1: consolidated_model_state = FSDP.consolidate_shard_weights( shard_weights=model_shard_state["shard_weights"], shard_metadata=model_shard_state["shard_metadata"], ) model = task.build_model(cfg.model) if ( "optimizer_history" in state and len(state["optimizer_history"]) > 0 and "num_updates" in state["optimizer_history"][-1] ): model.set_num_updates( state["optimizer_history"][-1]["num_updates"] ) model.load_state_dict( consolidated_model_state, strict=strict, model_cfg=cfg.model ) else: # model parallel checkpoint or unsharded checkpoint model = task.build_model(cfg.model) if ( "optimizer_history" in state and len(state["optimizer_history"]) > 0 and "num_updates" in state["optimizer_history"][-1] ): model.set_num_updates( state["optimizer_history"][-1]["num_updates"] ) model.load_state_dict( state["model"], strict=strict, model_cfg=cfg.model ) # reset state so it gets loaded for the next model in ensemble state = None if shard_idx % 10 == 0 and shard_idx > 0: elapsed = time.time() - st logger.info( f"Loaded {shard_idx} shards in {elapsed:.2f}s, {elapsed / (shard_idx+1):.2f}s/shard" ) # build model for ensemble ensemble.append(model) return ensemble, cfg, task The provided code snippet includes necessary dependencies for implementing the `load_model_ensemble` function. Write a Python function `def load_model_ensemble( filenames, arg_overrides: Optional[Dict[str, Any]] = None, task=None, strict=True, suffix="", num_shards=1, state=None, )` to solve the following problem: Loads an ensemble of models. Args: filenames (List[str]): checkpoint files to load arg_overrides (Dict[str,Any], optional): override model args that were used during model training task (fairseq.tasks.FairseqTask, optional): task to use for loading Here is the function: def load_model_ensemble( filenames, arg_overrides: Optional[Dict[str, Any]] = None, task=None, strict=True, suffix="", num_shards=1, state=None, ): """Loads an ensemble of models. Args: filenames (List[str]): checkpoint files to load arg_overrides (Dict[str,Any], optional): override model args that were used during model training task (fairseq.tasks.FairseqTask, optional): task to use for loading """ assert not ( strict and num_shards > 1 ), "Cannot load state dict with strict=True and checkpoint shards > 1" ensemble, args, _task = load_model_ensemble_and_task( filenames, arg_overrides, task, strict, suffix, num_shards, state, ) return ensemble, args
Loads an ensemble of models. Args: filenames (List[str]): checkpoint files to load arg_overrides (Dict[str,Any], optional): override model args that were used during model training task (fairseq.tasks.FairseqTask, optional): task to use for loading
183,141
import ast import collections import contextlib import logging import numpy as np import os import re import time import traceback from collections import OrderedDict from typing import Any, Dict, Optional, Union import torch from fairseq.data import data_utils from fairseq.dataclass.configs import CheckpointConfig from fairseq.dataclass.utils import ( convert_namespace_to_omegaconf, overwrite_args_by_name, ) from fairseq.distributed.fully_sharded_data_parallel import FSDP, has_FSDP from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.models import FairseqDecoder, FairseqEncoder from omegaconf import DictConfig, open_dict, OmegaConf from fairseq.ds_trainer import DeepSpeedTrainer logger = logging.getLogger(__name__) The provided code snippet includes necessary dependencies for implementing the `prune_state_dict` function. Write a Python function `def prune_state_dict(state_dict, model_cfg: Optional[DictConfig])` to solve the following problem: Prune the given state_dict if desired for LayerDrop (https://arxiv.org/abs/1909.11556). Training with LayerDrop allows models to be robust to pruning at inference time. This function prunes state_dict to allow smaller models to be loaded from a larger model and re-maps the existing state_dict for this to occur. It's called by functions that load models from checkpoints and does not need to be called directly. Here is the function: def prune_state_dict(state_dict, model_cfg: Optional[DictConfig]): """Prune the given state_dict if desired for LayerDrop (https://arxiv.org/abs/1909.11556). Training with LayerDrop allows models to be robust to pruning at inference time. This function prunes state_dict to allow smaller models to be loaded from a larger model and re-maps the existing state_dict for this to occur. It's called by functions that load models from checkpoints and does not need to be called directly. """ arch = None if model_cfg is not None: arch = ( model_cfg._name if isinstance(model_cfg, DictConfig) else getattr(model_cfg, "arch", None) ) if not model_cfg or arch is None or arch == "ptt_transformer": # args should not be none, but don't crash if it is. return state_dict encoder_layers_to_keep = getattr(model_cfg, "encoder_layers_to_keep", None) decoder_layers_to_keep = getattr(model_cfg, "decoder_layers_to_keep", None) if not encoder_layers_to_keep and not decoder_layers_to_keep: return state_dict # apply pruning logger.info( "Pruning model to specified layer configuration - this works best if the model was trained with LayerDrop" ) def create_pruning_pass(layers_to_keep, layer_name): keep_layers = sorted( int(layer_string) for layer_string in layers_to_keep.split(",") ) mapping_dict = {} for i in range(len(keep_layers)): mapping_dict[str(keep_layers[i])] = str(i) regex = re.compile(r"^{layer}.*\.layers\.(\d+)".format(layer=layer_name)) return {"substitution_regex": regex, "mapping_dict": mapping_dict} pruning_passes = [] if encoder_layers_to_keep: pruning_passes.append(create_pruning_pass(encoder_layers_to_keep, "encoder")) if decoder_layers_to_keep: pruning_passes.append(create_pruning_pass(decoder_layers_to_keep, "decoder")) new_state_dict = {} for layer_name in state_dict.keys(): match = re.search(r"\.layers\.(\d+)\.", layer_name) # if layer has no number in it, it is a supporting layer, such as an # embedding if not match: new_state_dict[layer_name] = state_dict[layer_name] continue # otherwise, layer should be pruned. original_layer_number = match.group(1) # figure out which mapping dict to replace from for pruning_pass in pruning_passes: if original_layer_number in pruning_pass["mapping_dict"] and pruning_pass[ "substitution_regex" ].search(layer_name): new_layer_number = pruning_pass["mapping_dict"][original_layer_number] substitution_match = pruning_pass["substitution_regex"].search( layer_name ) new_state_key = ( layer_name[: substitution_match.start(1)] + new_layer_number + layer_name[substitution_match.end(1) :] ) new_state_dict[new_state_key] = state_dict[layer_name] # Since layers are now pruned, *_layers_to_keep are no longer needed. # This is more of "It would make it work fix" rather than a proper fix. if isinstance(model_cfg, DictConfig): context = open_dict(model_cfg) else: context = contextlib.ExitStack() with context: if hasattr(model_cfg, "encoder_layers_to_keep"): model_cfg.encoder_layers_to_keep = None if hasattr(model_cfg, "decoder_layers_to_keep"): model_cfg.decoder_layers_to_keep = None return new_state_dict
Prune the given state_dict if desired for LayerDrop (https://arxiv.org/abs/1909.11556). Training with LayerDrop allows models to be robust to pruning at inference time. This function prunes state_dict to allow smaller models to be loaded from a larger model and re-maps the existing state_dict for this to occur. It's called by functions that load models from checkpoints and does not need to be called directly.
183,142
import ast import collections import contextlib import logging import numpy as np import os import re import time import traceback from collections import OrderedDict from typing import Any, Dict, Optional, Union import torch from fairseq.data import data_utils from fairseq.dataclass.configs import CheckpointConfig from fairseq.dataclass.utils import ( convert_namespace_to_omegaconf, overwrite_args_by_name, ) from fairseq.distributed.fully_sharded_data_parallel import FSDP, has_FSDP from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.models import FairseqDecoder, FairseqEncoder from omegaconf import DictConfig, open_dict, OmegaConf from fairseq.ds_trainer import DeepSpeedTrainer def load_checkpoint_to_cpu(path, arg_overrides=None, load_on_all_ranks=False): """Loads a checkpoint to CPU (with upgrading for backward compatibility). If doing single-GPU training or if the checkpoint is only being loaded by at most one process on each node (current default behavior is for only rank 0 to read the checkpoint from disk), load_on_all_ranks should be False to avoid errors from torch.distributed not having been initialized or torch.distributed.barrier() hanging. If all processes on each node may be loading the checkpoint simultaneously, load_on_all_ranks should be set to True to avoid I/O conflicts. There's currently no support for > 1 but < all processes loading the checkpoint on each node. """ local_path = PathManager.get_local_path(path) # The locally cached file returned by get_local_path() may be stale for # remote files that are periodically updated/overwritten (ex: # checkpoint_last.pt) - so we remove the local copy, sync across processes # (if needed), and then download a fresh copy. if local_path != path and PathManager.path_requires_pathmanager(path): try: os.remove(local_path) except FileNotFoundError: # With potentially multiple processes removing the same file, the # file being missing is benign (missing_ok isn't available until # Python 3.8). pass if load_on_all_ranks: torch.distributed.barrier() local_path = PathManager.get_local_path(path) with open(local_path, "rb") as f: state = torch.load(f, map_location=torch.device("cpu")) if "args" in state and state["args"] is not None and arg_overrides is not None: args = state["args"] for arg_name, arg_val in arg_overrides.items(): setattr(args, arg_name, arg_val) if "cfg" in state and state["cfg"] is not None: # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils old_primitive = _utils.is_primitive_type _utils.is_primitive_type = lambda _: True state["cfg"] = OmegaConf.create(state["cfg"]) _utils.is_primitive_type = old_primitive OmegaConf.set_struct(state["cfg"], True) if arg_overrides is not None: overwrite_args_by_name(state["cfg"], arg_overrides) state = _upgrade_state_dict(state) return state class PathManager: """ Wrapper for insulating OSS I/O (using Python builtin operations) from iopath's PathManager abstraction (for transparently handling various internal backends). """ def open( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): if IOPathManager: return IOPathManager.open( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) return open( path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) def copy(src_path: str, dst_path: str, overwrite: bool = False) -> bool: if IOPathManager: return IOPathManager.copy( src_path=src_path, dst_path=dst_path, overwrite=overwrite ) return shutil.copyfile(src_path, dst_path) def get_local_path(path: str, **kwargs) -> str: if IOPathManager: return IOPathManager.get_local_path(path, **kwargs) return path def exists(path: str) -> bool: if IOPathManager: return IOPathManager.exists(path) return os.path.exists(path) def isfile(path: str) -> bool: if IOPathManager: return IOPathManager.isfile(path) return os.path.isfile(path) def ls(path: str) -> List[str]: if IOPathManager: return IOPathManager.ls(path) return os.listdir(path) def mkdirs(path: str) -> None: if IOPathManager: return IOPathManager.mkdirs(path) os.makedirs(path, exist_ok=True) def rm(path: str) -> None: if IOPathManager: return IOPathManager.rm(path) os.remove(path) def chmod(path: str, mode: int) -> None: if not PathManager.path_requires_pathmanager(path): os.chmod(path, mode) def register_handler(handler) -> None: if IOPathManager: return IOPathManager.register_handler(handler=handler) def copy_from_local( local_path: str, dst_path: str, overwrite: bool = False, **kwargs ) -> None: if IOPathManager: return IOPathManager.copy_from_local( local_path=local_path, dst_path=dst_path, overwrite=overwrite, **kwargs ) return shutil.copyfile(local_path, dst_path) def path_requires_pathmanager(path: str) -> bool: """Do we require PathManager to access given path?""" if IOPathManager: for p in IOPathManager._path_handlers.keys(): if path.startswith(p): return True return False def supports_rename(path: str) -> bool: # PathManager doesn't yet support renames return not PathManager.path_requires_pathmanager(path) def rename(src: str, dst: str): os.rename(src, dst) """ ioPath async PathManager methods: """ def opena( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): """ Return file descriptor with asynchronous write operations. """ global IOPathManager if not IOPathManager: logging.info("ioPath is initializing PathManager.") try: from iopath.common.file_io import PathManager IOPathManager = PathManager() except Exception: logging.exception("Failed to initialize ioPath PathManager object.") return IOPathManager.opena( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) def async_close() -> bool: """ Wait for files to be written and clean up asynchronous PathManager. NOTE: `PathManager.async_close()` must be called at the end of any script that uses `PathManager.opena(...)`. """ global IOPathManager if IOPathManager: return IOPathManager.async_close() return False The provided code snippet includes necessary dependencies for implementing the `load_pretrained_component_from_model` function. Write a Python function `def load_pretrained_component_from_model( component: Union[FairseqEncoder, FairseqDecoder], checkpoint: str )` to solve the following problem: Load a pretrained FairseqEncoder or FairseqDecoder from checkpoint into the provided `component` object. If state_dict fails to load, there may be a mismatch in the architecture of the corresponding `component` found in the `checkpoint` file. Here is the function: def load_pretrained_component_from_model( component: Union[FairseqEncoder, FairseqDecoder], checkpoint: str ): """ Load a pretrained FairseqEncoder or FairseqDecoder from checkpoint into the provided `component` object. If state_dict fails to load, there may be a mismatch in the architecture of the corresponding `component` found in the `checkpoint` file. """ if not PathManager.exists(checkpoint): raise IOError("Model file not found: {}".format(checkpoint)) state = load_checkpoint_to_cpu(checkpoint) if isinstance(component, FairseqEncoder): component_type = "encoder" elif isinstance(component, FairseqDecoder): component_type = "decoder" else: raise ValueError( "component to load must be either a FairseqEncoder or " "FairseqDecoder. Loading other component types are not supported." ) component_state_dict = OrderedDict() for key in state["model"].keys(): if key.startswith(component_type): # encoder.input_layers.0.0.weight --> input_layers.0.0.weight component_subkey = key[len(component_type) + 1 :] component_state_dict[component_subkey] = state["model"][key] component.load_state_dict(component_state_dict, strict=True) return component
Load a pretrained FairseqEncoder or FairseqDecoder from checkpoint into the provided `component` object. If state_dict fails to load, there may be a mismatch in the architecture of the corresponding `component` found in the `checkpoint` file.
183,320
import logging import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.model_parallel.models.pipeline_parallel_transformer.layers import ( Embedding, TransformerDecoderEmbedding, TransformerDecoderLayer, TransformerDecoderOutputLayer, TransformerEncoderEmbedding, TransformerEncoderLayer, TransformerEncoderLayerNorm, ) from fairseq.models import ( BaseFairseqModel, FairseqDecoder, FairseqEncoder, register_model, register_model_architecture, ) from fairseq.models.fairseq_encoder import EncoderOut from fairseq.models.transformer import ( base_architecture, transformer_iwslt_de_en, transformer_wmt_en_de_big, ) from fairseq.modules import SinusoidalPositionalEmbedding import math import numpy as np def transformer_iwslt_de_en(args): def transformer_iwslt_de_en_dist(args): transformer_iwslt_de_en(args)
null
183,359
import math import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture, ) from fairseq.modules import ( AdaptiveSoftmax, DynamicConv, FairseqDropout, LayerNorm, LightweightConv, MultiheadAttention, PositionalEmbedding, ) from fairseq.utils import safe_hasattr def lightconv_wmt_en_de_big(args): def lightconv_wmt_en_fr_big(args): args.dropout = getattr(args, "dropout", 0.1) lightconv_wmt_en_de_big(args)
null
183,374
from dataclasses import dataclass, field from typing import Optional from fairseq import options, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import ( DEFAULT_MIN_PARAMS_TO_WRAP, Embedding, TransformerDecoder, ) from fairseq.modules import AdaptiveInput, CharacterTokenEmbedder from fairseq.utils import safe_getattr, safe_hasattr from omegaconf import II def base_lm_architecture(args): def safe_getattr(obj, k, default=None): def transformer_lm_gpt2_small(args): args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1024) args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", 4096) args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") base_lm_architecture(args)
null
183,378
from dataclasses import dataclass, field from typing import Optional from fairseq import options, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import ( DEFAULT_MIN_PARAMS_TO_WRAP, Embedding, TransformerDecoder, ) from fairseq.modules import AdaptiveInput, CharacterTokenEmbedder from fairseq.utils import safe_getattr, safe_hasattr from omegaconf import II def base_gpt3_architecture(args): def safe_getattr(obj, k, default=None): def transformer_lm_gpt3_small(args): # 125M params args.decoder_layers = safe_getattr(args, "decoder_layers", 12) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 768) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 12) base_gpt3_architecture(args)
null
183,381
from dataclasses import dataclass, field from typing import Optional from fairseq import options, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import ( DEFAULT_MIN_PARAMS_TO_WRAP, Embedding, TransformerDecoder, ) from fairseq.modules import AdaptiveInput, CharacterTokenEmbedder from fairseq.utils import safe_getattr, safe_hasattr from omegaconf import II def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr( args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4 ) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def transformer_lm_gpt3_xl(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) base_gpt3_architecture(args)
null
183,383
from dataclasses import dataclass, field from typing import Optional from fairseq import options, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import ( DEFAULT_MIN_PARAMS_TO_WRAP, Embedding, TransformerDecoder, ) from fairseq.modules import AdaptiveInput, CharacterTokenEmbedder from fairseq.utils import safe_getattr, safe_hasattr from omegaconf import II def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr( args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4 ) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def transformer_lm_gpt3_6_7(args): # 6.7B params args.decoder_layers = safe_getattr(args, "decoder_layers", 32) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 4096) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) base_gpt3_architecture(args)
null
183,401
import torch from fairseq.utils import new_arange def new_arange(x, *size): """ Return a Tensor of `size` filled with a range function on the device of x. If size is empty, using the size of the variable x. """ if len(size) == 0: size = x.size() return torch.arange(size[-1], device=x.device).expand(*size).contiguous() def _apply_ins_masks( in_tokens, in_scores, mask_ins_pred, padding_idx, unk_idx, eos_idx ): in_masks = in_tokens.ne(padding_idx) in_lengths = in_masks.sum(1) # HACK: hacky way to shift all the paddings to eos first. in_tokens.masked_fill_(~in_masks, eos_idx) mask_ins_pred.masked_fill_(~in_masks[:, 1:], 0) out_lengths = in_lengths + mask_ins_pred.sum(1) out_max_len = out_lengths.max() out_masks = new_arange(out_lengths, out_max_len)[None, :] < out_lengths[:, None] reordering = (mask_ins_pred + in_masks[:, 1:].long()).cumsum(1) out_tokens = ( in_tokens.new_zeros(in_tokens.size(0), out_max_len) .fill_(padding_idx) .masked_fill_(out_masks, unk_idx) ) out_tokens[:, 0] = in_tokens[:, 0] out_tokens.scatter_(1, reordering, in_tokens[:, 1:]) out_scores = None if in_scores is not None: in_scores.masked_fill_(~in_masks, 0) out_scores = in_scores.new_zeros(*out_tokens.size()) out_scores[:, 0] = in_scores[:, 0] out_scores.scatter_(1, reordering, in_scores[:, 1:]) return out_tokens, out_scores
null
183,425
import logging import math import os import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq.incremental_decoding_utils import with_incremental_state from fairseq.models import ( CompositeEncoder, FairseqDecoder, FairseqEncoder, FairseqEncoderDecoderModel, register_model, register_model_architecture, ) from fairseq.modules import ( DownsampledMultiHeadAttention, FairseqDropout, GradMultiply, LayerNorm, LearnedPositionalEmbedding, LinearizedConvolution, ) def base_architecture(args): def fconv_self_att_wp(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 256) args.encoder_layers = getattr( args, "encoder_layers", "[(128, 3)] * 2 + [(512,3)] * 1" ) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 256) args.decoder_layers = getattr( args, "decoder_layers", "[(512, 4)] * 4 + [(768, 4)] * 2 + [(1024, 4)] * 1" ) args.decoder_out_embed_dim = getattr(args, "decoder_out_embed_dim", 256) args.self_attention = getattr(args, "self_attention", "True") args.multihead_self_attention_nheads = getattr( args, "multihead_self_attention_nheads", 4 ) args.project_input = getattr(args, "project_input", "True") args.gated_attention = getattr(args, "gated_attention", "True") args.downsample = getattr(args, "downsample", "True") base_architecture(args)
null
183,444
import math import re from functools import partial from typing import List, Optional, Tuple import torch import torch.nn as nn from torch import Tensor from torch import device as Device from fairseq.models import FairseqEncoder from fairseq.models.speech_to_text.utils import ( NoOp, attention_suppression, layer_norm_backward_hook, lengths_to_padding_mask, segments_to_sequence, ) class NoSegAugmentedMemoryTransformerEncoderLayer(FairseqEncoder): def __init__( self, input_dim, num_heads, ffn_dim, num_layers=1, dropout_in_attn=0.0, dropout_on_attn=0.0, dropout_on_fc1=0.0, dropout_on_fc2=0.0, segment_size=128, context_config=(0, 0), max_memory_size=0, scaled_init=True, std_scale=None, activation_fn="relu", tanh_on_mem=False, mini_batches=False, negative_inf="-inf", deep_init=True, summarization_method="mean", max_relative_position=0, rpe_old_option=True, ): def set_mini_batches(self, mini_batches): def _get_relative_position( self, input: Tensor, max_relative_position: int, left_context_length: int, past_length: int, is_decoding: bool, ): def _get_attention_mask(self, input, past_length=0, left_context_cache=0): def init_state( self, batch_size: int, device: Optional[Device] = None ) -> List[Tensor]: def batch_state(self, states: List[List[Tensor]]) -> List[Tensor]: def reorder_state(self, state: List[Tensor], indices: Tensor) -> List[Tensor]: def reset_state(self, state: List[Tensor], indices: Tensor) -> List[Tensor]: def state_size(self) -> int: def batch_size_in_state( self, state: Optional[List[Tensor]], sloppy: bool = True ) -> Optional[int]: def gen_summary_queries(self, input): def _gen_right_context_padded_input(self, input): def _gen_segs_right_context(self, input, lengths): def forward( self, input: Tensor, padding_masks: Tensor, state: Optional[List[Tensor]] = None ) -> Tuple[Tensor, Tensor, List[Tensor], List[Tensor]]: def forward_jit_mini_batch_init( self, seg: Tensor, state: Optional[List[Tensor]] = None, is_decoding: bool = False, ): def state_update_before( self, layer: int, state: List[Tensor], past_length: int, past_left_context: int ): def state_update_after( self, layer: int, state: List[Tensor], mems: Tensor, next_key: Tensor, next_val: Tensor, mems_list: List[Tensor], lc_key_list: List[Tensor], lc_val_list: List[Tensor], ): def state_update_after_loop( self, state: List[Tensor], mems_list: List[Tensor], lc_key_list: List[Tensor], lc_val_list: List[Tensor], update_length: int, ): def forward_mini_batches( self, input: Tensor, lengths: Tensor, state: Optional[List[Tensor]] = None ) -> Tuple[Tensor, Tensor, List[Tensor], List[Tensor]]: def forward_jit_test( self, input: Tensor, lengths: Tensor, state: Optional[List[Tensor]] = None ) -> Tuple[Tensor, Tensor, List[Tensor]]: def forward_jit( self, input: Tensor, lengths: Tensor, state: Optional[List[Tensor]] = None ) -> Tuple[Tensor, Tensor, List[Tensor]]: def quantize_(self, params=None): def emformer_encoder(klass): class SpeechEncoder(klass): def __init__(self, args): super().__init__(args) stride = SpeechEncoder.conv_layer_stride(args) trf_left_context = args.segment_left_context // stride trf_right_context = args.segment_right_context // stride context_config = [trf_left_context, trf_right_context] self.transformer_layers = nn.ModuleList( [ NoSegAugmentedMemoryTransformerEncoderLayer( input_dim=args.encoder_embed_dim, num_heads=args.encoder_attention_heads, ffn_dim=args.encoder_ffn_embed_dim, num_layers=args.encoder_layers, dropout_in_attn=args.dropout, dropout_on_attn=args.dropout, dropout_on_fc1=args.dropout, dropout_on_fc2=args.dropout, activation_fn=args.activation_fn, context_config=context_config, segment_size=args.segment_length, max_memory_size=args.max_memory_size, scaled_init=True, # TODO: use constant for now. tanh_on_mem=args.amtrf_tanh_on_mem, ) ] ) def forward(self, src_tokens, src_lengths): encoder_out = super().forward(src_tokens, src_lengths) output = encoder_out["encoder_out"][0] encoder_padding_masks = encoder_out["encoder_padding_mask"][0] # This is because that in the original implementation # the output didn't consider the last segment as right context. encoder_padding_masks = encoder_padding_masks[:, : output.size(0)] return { "encoder_out": [output], "encoder_padding_mask": [encoder_padding_masks], "encoder_embedding": [], "encoder_states": [], "src_tokens": [], "src_lengths": [], } @staticmethod def conv_layer_stride(args): # TODO: make it configurable from the args return 4 SpeechEncoder.__name__ = klass.__name__ return SpeechEncoder
null
183,447
import logging import math from pathlib import Path from typing import Dict, List, Optional, Tuple import torch import torch.nn as nn from torch import Tensor from fairseq import checkpoint_utils, utils from fairseq.data.data_utils import lengths_to_padding_mask from fairseq.models import ( FairseqEncoder, FairseqEncoderDecoderModel, register_model, register_model_architecture, ) from fairseq.models.speech_to_text.hub_interface import S2THubInterface from fairseq.models.transformer import Embedding, TransformerDecoder from fairseq.modules import ( FairseqDropout, LayerNorm, PositionalEmbedding, TransformerEncoderLayer, ) def s2t_transformer_s(args): def s2t_transformer_sp(args): args.encoder_layers = getattr(args, "encoder_layers", 16) s2t_transformer_s(args)
null
183,490
from typing import Dict, List, Optional, Tuple import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture, ) from fairseq.modules import AdaptiveSoftmax, FairseqDropout from torch import Tensor def base_architecture(args): def lstm_luong_wmt_en_de(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1000) args.encoder_layers = getattr(args, "encoder_layers", 4) args.encoder_dropout_out = getattr(args, "encoder_dropout_out", 0) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1000) args.decoder_layers = getattr(args, "decoder_layers", 4) args.decoder_out_embed_dim = getattr(args, "decoder_out_embed_dim", 1000) args.decoder_dropout_out = getattr(args, "decoder_dropout_out", 0) base_architecture(args)
null
183,494
import logging import json from typing import Dict import numpy as np import torch from torch import nn import torch.nn.functional as F from fairseq.data.audio.audio_utils import ( get_window, get_fourier_basis, get_mel_filters, TTSSpectrogram, ) from fairseq.data.audio.speech_to_text_dataset import S2TDataConfig from fairseq.models.text_to_speech.codehifigan import CodeGenerator as CodeHiFiGANModel from fairseq.models.text_to_speech.hifigan import Generator as HiFiGANModel class GriffinLimVocoder(nn.Module): def __init__( self, sample_rate, win_size, hop_size, n_fft, n_mels, f_min, f_max, window_fn, spec_bwd_max_iter=32, fp16=False, ): super().__init__() self.inv_mel_transform = PseudoInverseMelScale( n_stft=n_fft // 2 + 1, n_mels=n_mels, sample_rate=sample_rate, f_min=f_min, f_max=f_max, ) self.gl_transform = GriffinLim( n_fft=n_fft, win_length=win_size, hop_length=hop_size, window_fn=window_fn, n_iter=spec_bwd_max_iter, ) if fp16: self.half() self.inv_mel_transform.half() self.gl_transform.half() else: self.float() self.inv_mel_transform.float() self.gl_transform.float() def forward(self, x): # x: (B x) T x D -> (B x) 1 x T # NOTE: batched forward produces noisier waveform. recommend running # one utterance at a time self.eval() x = x.exp().transpose(-1, -2) x = self.inv_mel_transform(x) x = self.gl_transform(x) return x def from_data_cfg(cls, args, data_cfg: S2TDataConfig): feat_cfg = data_cfg.config["features"] window_fn = getattr(torch, feat_cfg["window_fn"] + "_window") return cls( sample_rate=feat_cfg["sample_rate"], win_size=int(feat_cfg["win_len_t"] * feat_cfg["sample_rate"]), hop_size=int(feat_cfg["hop_len_t"] * feat_cfg["sample_rate"]), n_fft=feat_cfg["n_fft"], n_mels=feat_cfg["n_mels"], f_min=feat_cfg["f_min"], f_max=feat_cfg["f_max"], window_fn=window_fn, spec_bwd_max_iter=args.spec_bwd_max_iter, fp16=args.fp16, ) class HiFiGANVocoder(nn.Module): def __init__( self, checkpoint_path: str, model_cfg: Dict[str, str], fp16: bool = False ) -> None: super().__init__() self.model = HiFiGANModel(model_cfg) state_dict = torch.load(checkpoint_path) self.model.load_state_dict(state_dict["generator"]) if fp16: self.model.half() logger.info(f"loaded HiFiGAN checkpoint from {checkpoint_path}") def forward(self, x: torch.Tensor) -> torch.Tensor: # (B x) T x D -> (B x) 1 x T model = self.model.eval() if len(x.shape) == 2: return model(x.unsqueeze(0).transpose(1, 2)).detach().squeeze(0) else: return model(x.transpose(-1, -2)).detach() def from_data_cfg(cls, args, data_cfg: S2TDataConfig): vocoder_cfg = data_cfg.vocoder assert vocoder_cfg.get("type", "griffin_lim") == "hifigan" with open(vocoder_cfg["config"]) as f: model_cfg = json.load(f) return cls(vocoder_cfg["checkpoint"], model_cfg, fp16=args.fp16) class CodeHiFiGANVocoder(nn.Module): def __init__( self, checkpoint_path: str, model_cfg: Dict[str, str], fp16: bool = False ) -> None: super().__init__() self.model = CodeHiFiGANModel(model_cfg) state_dict = torch.load(checkpoint_path) self.model.load_state_dict(state_dict["generator"]) self.model.eval() if fp16: self.model.half() self.model.remove_weight_norm() logger.info(f"loaded CodeHiFiGAN checkpoint from {checkpoint_path}") def forward(self, x: Dict[str, torch.Tensor], dur_prediction=False) -> torch.Tensor: assert "code" in x x["dur_prediction"] = dur_prediction mask = x["code"] >= 0 # remove invalid code x["code"] = x["code"][mask].unsqueeze(dim=0) return self.model(**x).detach().squeeze() def from_data_cfg(cls, args, data_cfg): vocoder_cfg = data_cfg.vocoder assert vocoder_cfg is not None, "vocoder not specified in the data config" with open(vocoder_cfg["config"]) as f: model_cfg = json.load(f) return cls(vocoder_cfg["checkpoint"], model_cfg, fp16=args.fp16) class S2TDataConfig(object): """Wrapper class for data config YAML""" def __init__(self, yaml_path): try: import yaml except ImportError: print("Please install PyYAML to load YAML files for " "S2T data config") self.config = {} if op.isfile(yaml_path): try: with open(yaml_path) as f: self.config = yaml.load(f, Loader=yaml.FullLoader) except Exception as e: logger.info(f"Failed to load config from {yaml_path}: {e}") else: logger.info(f"Cannot find {yaml_path}") def vocab_filename(self): """fairseq vocabulary file under data root""" return self.config.get("vocab_filename", "dict.txt") def shuffle(self) -> bool: """Shuffle dataset samples before batching""" return self.config.get("shuffle", False) def pre_tokenizer(self) -> Dict: """Pre-tokenizer to apply before subword tokenization. Returning a dictionary with `tokenizer` providing the tokenizer name and the other items providing the tokenizer-specific arguments. Tokenizers are defined in `fairseq.data.encoders.*`""" return self.config.get("pre_tokenizer", {"tokenizer": None}) def bpe_tokenizer(self) -> Dict: """Subword tokenizer to apply after pre-tokenization. Returning a dictionary with `bpe` providing the tokenizer name and the other items providing the tokenizer-specific arguments. Tokenizers are defined in `fairseq.data.encoders.*`""" return self.config.get("bpe_tokenizer", {"bpe": None}) def prepend_tgt_lang_tag(self) -> bool: """Prepend target lang ID token as the target BOS (e.g. for to-many multilingual setting). During inference, this requires `--prefix-size 1` to force BOS to be lang ID token.""" return self.config.get("prepend_tgt_lang_tag", False) def input_feat_per_channel(self): """The dimension of input features (per audio channel)""" return self.config.get("input_feat_per_channel", 80) def input_channels(self): """The number of channels in the input audio""" return self.config.get("input_channels", 1) def sampling_alpha(self): """Hyper-parameter alpha = 1/T for temperature-based resampling. (alpha = 1 for no resampling)""" return self.config.get("sampling_alpha", 1.0) def use_audio_input(self): """Needed by the dataset loader to see if the model requires raw audio as inputs.""" return self.config.get("use_audio_input", False) def audio_root(self): """Audio paths in the manifest TSV can be relative and this provides the root path. Set this to empty string when using absolute paths.""" return self.config.get("audio_root", "") def get_feature_transforms(self, split, is_train): """Split-specific feature transforms. Allowing train set wildcard `_train`, evaluation set wildcard `_eval` and general wildcard `*` for matching.""" from copy import deepcopy cfg = deepcopy(self.config) _cur = cfg.get("transforms", {}) cur = _cur.get(split) cur = _cur.get("_train") if cur is None and is_train else cur cur = _cur.get("_eval") if cur is None and not is_train else cur cur = _cur.get("*") if cur is None else cur cfg["transforms"] = cur return cfg def get_vocoder(args, data_cfg: S2TDataConfig): if args.vocoder == "griffin_lim": return GriffinLimVocoder.from_data_cfg(args, data_cfg) elif args.vocoder == "hifigan": return HiFiGANVocoder.from_data_cfg(args, data_cfg) elif args.vocoder == "code_hifigan": return CodeHiFiGANVocoder.from_data_cfg(args, data_cfg) else: raise ValueError("Unknown vocoder")
null
183,522
import argparse import logging import math import os import sys from typing import Any, Callable, Dict, List, Optional, Tuple logger = logging.getLogger("fairseq_cli.train") import numpy as np import torch from omegaconf import DictConfig, OmegaConf from fairseq import checkpoint_utils, options, quantization_utils, tasks, utils from fairseq.data import data_utils, iterators from fairseq.data.plasma_utils import PlasmaStore from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.distributed import fsdp_enable_wrap, fsdp_wrap from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.logging import meters, metrics, progress_bar from fairseq.model_parallel.megatron_trainer import MegatronTrainer from fairseq.trainer import Trainer from multiprocessing.pool import ThreadPool def main(cfg: FairseqConfig) -> None: if isinstance(cfg, argparse.Namespace): cfg = convert_namespace_to_omegaconf(cfg) utils.import_user_module(cfg.common) if ( distributed_utils.is_master(cfg.distributed_training) and "job_logging_cfg" in cfg ): # make hydra logging work with ddp (see # see https://github.com/facebookresearch/hydra/issues/1126) logging.config.dictConfig(OmegaConf.to_container(cfg.job_logging_cfg)) assert ( cfg.dataset.max_tokens is not None or cfg.dataset.batch_size is not None ), "Must specify batch size either with --max-tokens or --batch-size" metrics.reset() if cfg.common.log_file is not None: handler = logging.FileHandler(filename=cfg.common.log_file) logger.addHandler(handler) np.random.seed(cfg.common.seed) utils.set_torch_seed(cfg.common.seed) ds_local_master = cfg.common.deepspeed and distributed_utils.is_local_master(cfg.distributed_training) if distributed_utils.is_master(cfg.distributed_training) or ds_local_master: checkpoint_utils.verify_checkpoint_directory(cfg.checkpoint.save_dir, rank=torch.distributed.get_rank()) ckp_copy_thread = ThreadPool(processes=1) else: ckp_copy_thread = None # if distributed_utils.is_master(cfg.distributed_training): # checkpoint_utils.verify_checkpoint_directory(cfg.checkpoint.save_dir) # Print args logger.info(cfg) if cfg.checkpoint.write_checkpoints_asynchronously: try: import iopath # noqa: F401 except ImportError: logging.exception( "Asynchronous checkpoint writing is specified but iopath is " "not installed: `pip install iopath`" ) return # Setup task, e.g., translation, language modeling, etc. task = tasks.setup_task(cfg.task) assert cfg.criterion, "Please specify criterion to train a model" # Build model and criterion if cfg.distributed_training.ddp_backend == "fully_sharded": with fsdp_enable_wrap(cfg.distributed_training): model = fsdp_wrap(task.build_model(cfg.model)) else: model = task.build_model(cfg.model) criterion = task.build_criterion(cfg.criterion) logger.info(model) logger.info("task: {}".format(task.__class__.__name__)) logger.info("model: {}".format(model.__class__.__name__)) logger.info("criterion: {}".format(criterion.__class__.__name__)) logger.info( "num. shared model params: {:,} (num. trained: {:,})".format( sum( p.numel() for p in model.parameters() if not getattr(p, "expert", False) ), sum( p.numel() for p in model.parameters() if not getattr(p, "expert", False) and p.requires_grad ), ) ) logger.info( "num. expert model params: {} (num. trained: {})".format( sum(p.numel() for p in model.parameters() if getattr(p, "expert", False)), sum( p.numel() for p in model.parameters() if getattr(p, "expert", False) and p.requires_grad ), ) ) # Load valid dataset (we load training data below, based on the latest checkpoint) # We load the valid dataset AFTER building the model data_utils.raise_if_valid_subsets_unintentionally_ignored(cfg) if cfg.dataset.combine_valid_subsets: task.load_dataset("valid", combine=True, epoch=1) else: for valid_sub_split in cfg.dataset.valid_subset.split(","): task.load_dataset(valid_sub_split, combine=False, epoch=1) # (optionally) Configure quantization if cfg.common.quantization_config_path is not None: quantizer = quantization_utils.Quantizer( config_path=cfg.common.quantization_config_path, max_epoch=cfg.optimization.max_epoch, max_update=cfg.optimization.max_update, ) else: quantizer = None # Build trainer if cfg.common.deepspeed: assert quantizer is None, "fairseq wuantizer is not currently supported on deepspeed" from fairseq.ds_trainer import DeepSpeedTrainer trainer = DeepSpeedTrainer(cfg, task, model, criterion) elif cfg.common.model_parallel_size == 1: trainer = Trainer(cfg, task, model, criterion, quantizer) else: trainer = MegatronTrainer(cfg, task, model, criterion) logger.info( "training on {} devices (GPUs/TPUs)".format( cfg.distributed_training.distributed_world_size ) ) logger.info( "max tokens per device = {} and max sentences per device = {}".format( cfg.dataset.max_tokens, cfg.dataset.batch_size, ) ) # Load the init checkpoint if cfg.checkpoint.init_from_file is not None and os.path.isfile(cfg.checkpoint.init_from_file): logger.info(f"load {cfg.checkpoint.init_from_file} for initialization") checkpoint_model = torch.load(cfg.checkpoint.init_from_file, map_location='cpu')['model'] # pdb.set_trace() model_state_dict = model.state_dict() for k in model_state_dict.keys(): if k in checkpoint_model: if model_state_dict[k].shape != checkpoint_model[k].shape: # ['gpt_model.decoder.embed_tokens.weight', 'gpt_model.decoder.output_projection.weight'] if k in ['gpt_model.decoder.embed_tokens.weight', 'gpt_model.decoder.output_projection.weight']: logger.info(f"Extending the {k} in checkpoint to fit the model weight") temp_tensor = model_state_dict[k].clone() temp_tensor[:checkpoint_model[k].shape[0], :] = checkpoint_model[k] checkpoint_model[k] = temp_tensor elif k in ['img_connector.latent_query']: model_shape = model_state_dict[k].shape checkpoint_shape = checkpoint_model[k].shape if model_shape > checkpoint_shape: logger.info(f"Extending the {k} in checkpoint to fit the model weight: {checkpoint_shape} -> {model_shape}") temp_tensor = model_state_dict[k].clone() temp_tensor[:checkpoint_shape[0], :] = checkpoint_model[k] checkpoint_model[k] = temp_tensor else: logger.info(f"Reducing the {k} in checkpoint to fit the model weight: {checkpoint_shape} -> {model_shape}") checkpoint_model[k] = checkpoint_model[k][:model_shape[0]] else: # import pdb; pdb.set_trace() _ = checkpoint_model.pop(k) # init the sub-model for moe elif 'img_model' in k and k.split('.')[1].isdigit(): remove_module_k = '.'.join(['img_model',]+k.split('.')[2:]) if remove_module_k in checkpoint_model: if model_state_dict[k].shape == checkpoint_model[remove_module_k].shape: checkpoint_model[k] = checkpoint_model[remove_module_k] del checkpoint_model[remove_module_k] # img_connector.latent_query elif 'img_connector' in k and k.split('.')[1].isdigit(): remove_module_k = '.'.join(['img_connector',]+k.split('.')[2:]) if remove_module_k in checkpoint_model: if model_state_dict[k].shape == checkpoint_model[remove_module_k].shape: checkpoint_model[k] = checkpoint_model[remove_module_k] del checkpoint_model[remove_module_k] elif remove_module_k == 'img_connector.latent_query': # import pdb; pdb.set_trace() model_shape = model_state_dict[k].shape[0] checkpoint_shape = checkpoint_model[remove_module_k].shape[0] if model_shape > checkpoint_shape: logger.info(f"Extending the {remove_module_k} in checkpoint to fit the model weight") temp_tensor = model_state_dict[k].clone() temp_tensor[:checkpoint_shape, :] = checkpoint_model[remove_module_k] checkpoint_model[k] = temp_tensor del checkpoint_model[remove_module_k] else: logger.info(f"Reducing the {remove_module_k} in checkpoint to fit the model weight") checkpoint_model[k] = checkpoint_model[remove_module_k][:model_shape] del checkpoint_model[remove_module_k] missing_keys, unexpected_keys = model.load_state_dict(checkpoint_model, strict=False) logger.info(f"missing keys: {missing_keys}") logger.info(f"unexpected keys: {unexpected_keys}") # Load the latest checkpoint if one is available and restore the # corresponding train iterator extra_state, epoch_itr = checkpoint_utils.load_checkpoint( cfg.checkpoint, trainer, # don't cache epoch iterators for sharded datasets disable_iterator_cache=task.has_sharded_data("train"), ) if cfg.common.tpu: import torch_xla.core.xla_model as xm xm.rendezvous("load_checkpoint") # wait for all workers max_epoch = cfg.optimization.max_epoch or math.inf lr = trainer.get_lr() train_meter = meters.StopwatchMeter() train_meter.start() while epoch_itr.next_epoch_idx <= max_epoch: if lr <= cfg.optimization.stop_min_lr: logger.info( f"stopping training because current learning rate ({lr}) is smaller " "than or equal to minimum learning rate " f"(--stop-min-lr={cfg.optimization.stop_min_lr})" ) break # train for one epoch valid_losses, should_stop = train(cfg, trainer, task, epoch_itr) if should_stop: break # only use first validation loss to update the learning rate lr = trainer.lr_step(epoch_itr.epoch, valid_losses[0]) epoch_itr = trainer.get_train_iterator( epoch_itr.next_epoch_idx, # sharded data: get train iterator for next epoch load_dataset=task.has_sharded_data("train"), # don't cache epoch iterators for sharded datasets disable_iterator_cache=task.has_sharded_data("train"), ) train_meter.stop() logger.info("done training in {:.1f} seconds".format(train_meter.sum)) # ioPath implementation to wait for all asynchronous file writes to complete. if cfg.checkpoint.write_checkpoints_asynchronously: logger.info( "ioPath PathManager waiting for all asynchronous checkpoint " "writes to finish." ) PathManager.async_close() logger.info("ioPath PathManager finished waiting.") class PlasmaStore: def __init__(self, path=DEFAULT_PLASMA_PATH, nbytes: int = GB100): self.server = self.start(path, nbytes) def __del__(self): self.server.kill() def start(path=DEFAULT_PLASMA_PATH, nbytes: int = GB100) -> subprocess.Popen: if not PYARROW_AVAILABLE: raise ImportError("please run pip install pyarrow to use --use_plasma_view") # best practice is to allocate more space than we need. The limitation seems to be the size of /dev/shm _server = subprocess.Popen(["plasma_store", "-m", str(nbytes), "-s", path]) plasma.connect(path, num_retries=200) # If we can't connect we fail immediately return _server def convert_namespace_to_omegaconf(args: Namespace) -> DictConfig: """Convert a flat argparse.Namespace to a structured DictConfig.""" # Here we are using field values provided in args to override counterparts inside config object overrides, deletes = override_module_args(args) # configs will be in fairseq/config after installation config_path = os.path.join("..", "config") GlobalHydra.instance().clear() with initialize(config_path=config_path): try: composed_cfg = compose("config", overrides=overrides, strict=False) except: logger.error("Error when composing. Overrides: " + str(overrides)) raise for k in deletes: composed_cfg[k] = None cfg = OmegaConf.create( OmegaConf.to_container(composed_cfg, resolve=True, enum_to_str=True) ) # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils with omegaconf_no_object_check(): if cfg.task is None and getattr(args, "task", None): cfg.task = Namespace(**vars(args)) from fairseq.tasks import TASK_REGISTRY _set_legacy_defaults(cfg.task, TASK_REGISTRY[args.task]) cfg.task._name = args.task if cfg.model is None and getattr(args, "arch", None): cfg.model = Namespace(**vars(args)) from fairseq.models import ARCH_MODEL_REGISTRY _set_legacy_defaults(cfg.model, ARCH_MODEL_REGISTRY[args.arch]) cfg.model._name = args.arch if cfg.optimizer is None and getattr(args, "optimizer", None): cfg.optimizer = Namespace(**vars(args)) from fairseq.optim import OPTIMIZER_REGISTRY _set_legacy_defaults(cfg.optimizer, OPTIMIZER_REGISTRY[args.optimizer]) cfg.optimizer._name = args.optimizer if cfg.lr_scheduler is None and getattr(args, "lr_scheduler", None): cfg.lr_scheduler = Namespace(**vars(args)) from fairseq.optim.lr_scheduler import LR_SCHEDULER_REGISTRY _set_legacy_defaults( cfg.lr_scheduler, LR_SCHEDULER_REGISTRY[args.lr_scheduler] ) cfg.lr_scheduler._name = args.lr_scheduler if cfg.criterion is None and getattr(args, "criterion", None): cfg.criterion = Namespace(**vars(args)) from fairseq.criterions import CRITERION_REGISTRY _set_legacy_defaults(cfg.criterion, CRITERION_REGISTRY[args.criterion]) cfg.criterion._name = args.criterion OmegaConf.set_struct(cfg, True) return cfg def cli_main( modify_parser: Optional[Callable[[argparse.ArgumentParser], None]] = None ) -> None: parser = options.get_training_parser() args = options.parse_args_and_arch(parser, modify_parser=modify_parser) cfg = convert_namespace_to_omegaconf(args) if cfg.common.use_plasma_view: server = PlasmaStore(path=cfg.common.plasma_path) logger.info( f"Started plasma server pid {server.server.pid} {cfg.common.plasma_path}" ) if args.profile: with torch.cuda.profiler.profile(): with torch.autograd.profiler.emit_nvtx(): distributed_utils.call_main(cfg, main) else: distributed_utils.call_main(cfg, main) # if cfg.common.use_plasma_view: # server.server.kill()
null
183,524
import ast import fileinput import logging import math import os import sys import time from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output Batch = namedtuple("Batch", "ids src_tokens src_lengths constraints") def pack_constraints(batch_constraints: List[List[torch.Tensor]]) -> torch.Tensor: def make_batches(lines, cfg, task, max_positions, encode_fn): def encode_fn_target(x): return encode_fn(x) if cfg.generation.constraints: # Strip (tab-delimited) contraints, if present, from input lines, # store them in batch_constraints batch_constraints = [list() for _ in lines] for i, line in enumerate(lines): if "\t" in line: lines[i], *batch_constraints[i] = line.split("\t") # Convert each List[str] to List[Tensor] for i, constraint_list in enumerate(batch_constraints): batch_constraints[i] = [ task.target_dictionary.encode_line( encode_fn_target(constraint), append_eos=False, add_if_not_exist=False, ) for constraint in constraint_list ] if cfg.generation.constraints: constraints_tensor = pack_constraints(batch_constraints) else: constraints_tensor = None tokens, lengths = task.get_interactive_tokens_and_lengths(lines, encode_fn) itr = task.get_batch_iterator( dataset=task.build_dataset_for_inference( tokens, lengths, constraints=constraints_tensor ), max_tokens=cfg.dataset.max_tokens, max_sentences=cfg.dataset.batch_size, max_positions=max_positions, ignore_invalid_inputs=cfg.dataset.skip_invalid_size_inputs_valid_test, ).next_epoch_itr(shuffle=False) for batch in itr: ids = batch["id"] src_tokens = batch["net_input"]["src_tokens"] src_lengths = batch["net_input"]["src_lengths"] constraints = batch.get("constraints", None) yield Batch( ids=ids, src_tokens=src_tokens, src_lengths=src_lengths, constraints=constraints, )
null
183,526
import logging import os import sys from argparse import Namespace from itertools import chain import torch from omegaconf import DictConfig from fairseq import checkpoint_utils, distributed_utils, options, utils from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.logging import metrics, progress_bar from fairseq.utils import reset_logging def main(cfg: DictConfig, override_args=None): def convert_namespace_to_omegaconf(args: Namespace) -> DictConfig: def cli_main(): parser = options.get_validation_parser() args = options.parse_args_and_arch(parser) # only override args that are explicitly given on the command line override_parser = options.get_validation_parser() override_args = options.parse_args_and_arch(override_parser, suppress_defaults=True) distributed_utils.call_main( convert_namespace_to_omegaconf(args), main, override_args=override_args )
null
183,538
import ast import logging import math import os import sys from argparse import Namespace from itertools import chain import numpy as np import torch from omegaconf import DictConfig from fairseq import checkpoint_utils, options, scoring, tasks, utils from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.logging import progress_bar from fairseq.logging.meters import StopwatchMeter, TimeMeter def get_symbols_to_strip_from_output(generator): def progress_bar( iterator, log_format: Optional[str] = None, log_interval: int = 100, log_file: Optional[str] = None, epoch: Optional[int] = None, prefix: Optional[str] = None, tensorboard_logdir: Optional[str] = None, default_log_format: str = "tqdm", wandb_project: Optional[str] = None, wandb_run_name: Optional[str] = None, azureml_logging: Optional[bool] = False, ): class TimeMeter(Meter): def __init__( self, init: int = 0, n: int = 0, round: Optional[int] = None, ): def reset(self, init=0, n=0): def update(self, val=1): def state_dict(self): def load_state_dict(self, state_dict): def avg(self): def elapsed_time(self): def smoothed_value(self) -> float: class StopwatchMeter(Meter): def __init__(self, round: Optional[int] = None): def start(self): def stop(self, n=1, prehook=None): def reset(self): def state_dict(self): def load_state_dict(self, state_dict): def avg(self): def elapsed_time(self): def smoothed_value(self) -> float: def _main(cfg: DictConfig, output_file): logging.basicConfig( format="%(asctime)s | %(levelname)s | %(name)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=os.environ.get("LOGLEVEL", "INFO").upper(), stream=output_file, ) logger = logging.getLogger("fairseq_cli.generate") utils.import_user_module(cfg.common) if cfg.dataset.max_tokens is None and cfg.dataset.batch_size is None: cfg.dataset.max_tokens = 12000 logger.info(cfg) # Fix seed for stochastic decoding if cfg.common.seed is not None and not cfg.generation.no_seed_provided: np.random.seed(cfg.common.seed) utils.set_torch_seed(cfg.common.seed) use_cuda = torch.cuda.is_available() and not cfg.common.cpu # Load dataset splits task = tasks.setup_task(cfg.task) # Set dictionaries try: src_dict = getattr(task, "source_dictionary", None) except NotImplementedError: src_dict = None tgt_dict = task.target_dictionary overrides = ast.literal_eval(cfg.common_eval.model_overrides) # Load ensemble logger.info("loading model(s) from {}".format(cfg.common_eval.path)) models, saved_cfg = checkpoint_utils.load_model_ensemble( utils.split_paths(cfg.common_eval.path), arg_overrides=overrides, task=task, suffix=cfg.checkpoint.checkpoint_suffix, strict=(cfg.checkpoint.checkpoint_shard_count == 1), num_shards=cfg.checkpoint.checkpoint_shard_count, ) # loading the dataset should happen after the checkpoint has been loaded so we can give it the saved task config task.load_dataset(cfg.dataset.gen_subset, task_cfg=saved_cfg.task) if cfg.generation.lm_path is not None: overrides["data"] = cfg.task.data try: lms, _ = checkpoint_utils.load_model_ensemble( [cfg.generation.lm_path], arg_overrides=overrides, task=None ) except: logger.warning( f"Failed to load language model! Please make sure that the language model dict is the same " f"as target dict and is located in the data dir ({cfg.task.data})" ) raise assert len(lms) == 1 else: lms = [None] # Optimize ensemble for generation for model in chain(models, lms): if model is None: continue if cfg.common.fp16: model.half() if use_cuda and not cfg.distributed_training.pipeline_model_parallel: model.cuda() model.prepare_for_inference_(cfg) # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) align_dict = utils.load_align_dict(cfg.generation.replace_unk) # Load dataset (possibly sharded) itr = task.get_batch_iterator( dataset=task.dataset(cfg.dataset.gen_subset), max_tokens=cfg.dataset.max_tokens, max_sentences=cfg.dataset.batch_size, max_positions=utils.resolve_max_positions( task.max_positions(), *[m.max_positions() for m in models] ), ignore_invalid_inputs=cfg.dataset.skip_invalid_size_inputs_valid_test, required_batch_size_multiple=cfg.dataset.required_batch_size_multiple, seed=cfg.common.seed, num_shards=cfg.distributed_training.distributed_world_size, shard_id=cfg.distributed_training.distributed_rank, num_workers=cfg.dataset.num_workers, data_buffer_size=cfg.dataset.data_buffer_size, ).next_epoch_itr(shuffle=False) progress = progress_bar.progress_bar( itr, log_format=cfg.common.log_format, log_interval=cfg.common.log_interval, default_log_format=("tqdm" if not cfg.common.no_progress_bar else "simple"), ) # Initialize generator gen_timer = StopwatchMeter() extra_gen_cls_kwargs = {"lm_model": lms[0], "lm_weight": cfg.generation.lm_weight} generator = task.build_generator( models, cfg.generation, extra_gen_cls_kwargs=extra_gen_cls_kwargs ) # Handle tokenization and BPE tokenizer = task.build_tokenizer(cfg.tokenizer) bpe = task.build_bpe(cfg.bpe) def decode_fn(x): if bpe is not None: x = bpe.decode(x) if tokenizer is not None: x = tokenizer.decode(x) return x scorer = scoring.build_scorer(cfg.scoring, tgt_dict) num_sentences = 0 has_target = True wps_meter = TimeMeter() for sample in progress: sample = utils.move_to_cuda(sample) if use_cuda else sample if "net_input" not in sample: continue prefix_tokens = None if cfg.generation.prefix_size > 0: prefix_tokens = sample["target"][:, : cfg.generation.prefix_size] constraints = None if "constraints" in sample: constraints = sample["constraints"] gen_timer.start() hypos = task.inference_step( generator, models, sample, prefix_tokens=prefix_tokens, constraints=constraints, ) num_generated_tokens = sum(len(h[0]["tokens"]) for h in hypos) gen_timer.stop(num_generated_tokens) for i, sample_id in enumerate(sample["id"].tolist()): has_target = sample["target"] is not None # Remove padding if "src_tokens" in sample["net_input"]: src_tokens = utils.strip_pad( sample["net_input"]["src_tokens"][i, :], tgt_dict.pad() ) else: src_tokens = None target_tokens = None if has_target: target_tokens = ( utils.strip_pad(sample["target"][i, :], tgt_dict.pad()).int().cpu() ) # Either retrieve the original sentences or regenerate them from tokens. if align_dict is not None: src_str = task.dataset(cfg.dataset.gen_subset).src.get_original_text( sample_id ) target_str = task.dataset(cfg.dataset.gen_subset).tgt.get_original_text( sample_id ) else: if src_dict is not None: src_str = src_dict.string(src_tokens, cfg.common_eval.post_process) else: src_str = "" if has_target: target_str = tgt_dict.string( target_tokens, cfg.common_eval.post_process, escape_unk=True, extra_symbols_to_ignore=get_symbols_to_strip_from_output( generator ), ) src_str = decode_fn(src_str) if has_target: target_str = decode_fn(target_str) if not cfg.common_eval.quiet: if src_dict is not None: print("S-{}\t{}".format(sample_id, src_str), file=output_file) if has_target: print("T-{}\t{}".format(sample_id, target_str), file=output_file) # Process top predictions for j, hypo in enumerate(hypos[i][: cfg.generation.nbest]): hypo_tokens, hypo_str, alignment = utils.post_process_prediction( hypo_tokens=hypo["tokens"].int().cpu(), src_str=src_str, alignment=hypo["alignment"], align_dict=align_dict, tgt_dict=tgt_dict, remove_bpe=cfg.common_eval.post_process, extra_symbols_to_ignore=get_symbols_to_strip_from_output(generator), ) detok_hypo_str = decode_fn(hypo_str) if not cfg.common_eval.quiet: score = hypo["score"] / math.log(2) # convert to base 2 # original hypothesis (after tokenization and BPE) print( "H-{}\t{}\t{}".format(sample_id, score, hypo_str), file=output_file, ) # detokenized hypothesis print( "D-{}\t{}\t{}".format(sample_id, score, detok_hypo_str), file=output_file, ) print( "P-{}\t{}".format( sample_id, " ".join( map( lambda x: "{:.4f}".format(x), # convert from base e to base 2 hypo["positional_scores"] .div_(math.log(2)) .tolist(), ) ), ), file=output_file, ) if cfg.generation.print_alignment == "hard": print( "A-{}\t{}".format( sample_id, " ".join( [ "{}-{}".format(src_idx, tgt_idx) for src_idx, tgt_idx in alignment ] ), ), file=output_file, ) if cfg.generation.print_alignment == "soft": print( "A-{}\t{}".format( sample_id, " ".join( [",".join(src_probs) for src_probs in alignment] ), ), file=output_file, ) if cfg.generation.print_step: print( "I-{}\t{}".format(sample_id, hypo["steps"]), file=output_file, ) if cfg.generation.retain_iter_history: for step, h in enumerate(hypo["history"]): _, h_str, _ = utils.post_process_prediction( hypo_tokens=h["tokens"].int().cpu(), src_str=src_str, alignment=None, align_dict=None, tgt_dict=tgt_dict, remove_bpe=None, ) print( "E-{}_{}\t{}".format(sample_id, step, h_str), file=output_file, ) # Score only the top hypothesis if has_target and j == 0: if ( align_dict is not None or cfg.common_eval.post_process is not None ): # Convert back to tokens for evaluation with unk replacement and/or without BPE target_tokens = tgt_dict.encode_line( target_str, add_if_not_exist=True ) hypo_tokens = tgt_dict.encode_line( detok_hypo_str, add_if_not_exist=True ) if hasattr(scorer, "add_string"): scorer.add_string(target_str, detok_hypo_str) else: scorer.add(target_tokens, hypo_tokens) wps_meter.update(num_generated_tokens) progress.log({"wps": round(wps_meter.avg)}) num_sentences += ( sample["nsentences"] if "nsentences" in sample else sample["id"].numel() ) logger.info("NOTE: hypothesis and token scores are output in base 2") logger.info( "Translated {:,} sentences ({:,} tokens) in {:.1f}s ({:.2f} sentences/s, {:.2f} tokens/s)".format( num_sentences, gen_timer.n, gen_timer.sum, num_sentences / gen_timer.sum, 1.0 / gen_timer.avg, ) ) if has_target: if cfg.bpe and not cfg.generation.sacrebleu: if cfg.common_eval.post_process: logger.warning( "BLEU score is being computed by splitting detokenized string on spaces, this is probably not what you want. Use --sacrebleu for standard 13a BLEU tokenization" ) else: logger.warning( "If you are using BPE on the target side, the BLEU score is computed on BPE tokens, not on proper words. Use --sacrebleu for standard 13a BLEU tokenization" ) # use print to be consistent with other main outputs: S-, H-, T-, D- and so on print( "Generate {} with beam={}: {}".format( cfg.dataset.gen_subset, cfg.generation.beam, scorer.result_string() ), file=output_file, ) return scorer
null
183,540
import sys import unilm import ast import fileinput import logging import math import os import sys import time import re from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image def buffered_read(input, buffer_size): buffer = [] with fileinput.input(files=[input], openhook=fileinput.hook_encoded("utf-8")) as h: for src_str in h: buffer.append(src_str.strip()) if len(buffer) >= buffer_size: yield buffer buffer = [] if len(buffer) > 0: yield buffer
null
183,541
import sys import unilm import ast import fileinput import logging import math import os import sys import time import re from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image Batch = namedtuple("Batch", "ids src_tokens src_lengths constraints img_src_tokens img_gpt_input_mask") def get_interactive_tokens_and_lengths(self, lines, encode_fn, tokenizer=None): """ line format: [image]path<tab>text<tab>[image]path model input: `<s> <image> image hidden </image> My cat looking very dignified.</s>` """ image_feature_length = self.args.image_feature_length bos_id = self.dictionary.bos() eos_id = self.dictionary.eos() boi_id = self.dictionary.index("<image>") eoi_id = self.dictionary.index("</image>") def convert_one_line(input_str): # TODO: input interleave image and text token = [] img_src_token = [] img_gpt_input_mask = [] segments = input_str.split('<tab>') token.append(bos_id) img_gpt_input_mask.append(0) for i, segment in enumerate(segments): if segment.startswith('[image]'): image_path = segment[7:] # read image and transform to tensor image = Image.open(image_path).convert("RGB") image_tensor = square_transform(self.args.input_resolution)(image) img_src_token.append(image_tensor) # token.extend([boi_id] + [boi_id] * image_feature_length + [eoi_id]) token.extend([boi_id] + list(range(4, image_feature_length+4)) + [eoi_id]) img_gpt_input_mask.extend([0] + [1] * image_feature_length + [0]) else: special_tokens = [self.source_dictionary[idx] for idx in range(tokenizer.vocab_size(), len(self.source_dictionary))] split_special_token_words = [] split_resutls = split_string(segment, special_tokens) for string in split_resutls: if string in special_tokens: split_special_token_words.append(string) else: encode_tokens = tokenizer.encode(string, out_type=str) split_special_token_words.extend(encode_tokens) segment = ' '.join(split_special_token_words) text_tokens = self.source_dictionary.encode_line( encode_fn(segment), add_if_not_exist=False ).tolist() text_tokens = text_tokens[:-1] # </s> in token token.extend(text_tokens) img_gpt_input_mask.extend([0] * (len(text_tokens))) # </s> in token token.append(eos_id) # img_gpt_input_mask = img_gpt_input_mask[:-1] assert len(token) == len(img_gpt_input_mask) + 1 token = torch.LongTensor(token) img_gpt_input_mask = torch.LongTensor(img_gpt_input_mask) img_src_token = torch.stack(img_src_token, dim=0) return token, img_src_token, img_gpt_input_mask tokens = [] img_src_tokens = [] img_gpt_input_masks = [] for src_str in lines: token, img_src_token, img_gpt_input_mask = convert_one_line(src_str) tokens.append(token) img_src_tokens.append(img_src_token) img_gpt_input_masks.append(img_gpt_input_mask) lengths = [t.numel() for t in tokens] return tokens, lengths, img_src_tokens, img_gpt_input_masks def pack_constraints(batch_constraints: List[List[torch.Tensor]]) -> torch.Tensor: """Takes a list of list of constraints in tensor form (a list of tensor constraints for each sentence) and transforms it into a packed Tensor. For example, here is a batch of size 3 with 3, 0, and 1 constraints: [ [ [3 1 2], [3], [4 5 6 7], ] [], [ [1 8 9 10 1 4 11 12], ] ] Its corresponding packed structure is: [ [ 3 3 1 2 0 3 0 4 5 6 7 0], [ 0 0 0 0 0 0 0 0 0 0 0 0], [ 1 1 8 9 10 1 4 11 12 0 0 0] ] The packed tensor has shape (batch size, maxlen), where maxlen is defined below. Each row contains concatenated constraint tokens for that sentence, with 0 appended after each constraint. The first item in each row is the number of constraints for that sentence. So maxlen is the maximum of (number of constraints) + (sum length of constraints) + 1. across all sentences in the batch. """ # The maximum word length of concatenated constraints for any sentence max_constraints_len = 1 for sentence_constraints in batch_constraints: if len(sentence_constraints): # number of constraints, plus sum of constrain lens, plus a zero after each constraints_len = ( 1 + sum([c.size(0) for c in sentence_constraints]) + len(sentence_constraints) ) max_constraints_len = max(max_constraints_len, constraints_len) batch_size = len(batch_constraints) constraints_tensor = torch.zeros((batch_size, max_constraints_len)).long() for i, sentence_constraints in enumerate(batch_constraints): constraints_tensor[i, 0] = len(sentence_constraints) offset = 1 for j, constraint in enumerate(sentence_constraints): this_len = constraint.size(0) constraints_tensor[i, offset : offset + this_len] = constraint offset += this_len + 1 return constraints_tensor.long() def make_batches(lines, cfg, task, max_positions, encode_fn): def encode_fn_target(x): return encode_fn(x) if cfg.generation.constraints: # Strip (tab-delimited) contraints, if present, from input lines, # store them in batch_constraints batch_constraints = [list() for _ in lines] for i, line in enumerate(lines): if "\t" in line: lines[i], *batch_constraints[i] = line.split("\t") # Convert each List[str] to List[Tensor] for i, constraint_list in enumerate(batch_constraints): batch_constraints[i] = [ task.target_dictionary.encode_line( encode_fn_target(constraint), append_eos=False, add_if_not_exist=False, ) for constraint in constraint_list ] if cfg.generation.constraints: constraints_tensor = pack_constraints(batch_constraints) else: constraints_tensor = None tokenizer = spm.SentencePieceProcessor() if os.path.exists('data/sentencepiece.bpe.model'): tokenizer.Load('data/sentencepiece.bpe.model') else: tokenizer = None tokens, lengths, img_src_tokens, img_gpt_input_mask = get_interactive_tokens_and_lengths(task, lines, encode_fn, tokenizer) itr = task.get_batch_iterator( dataset=task.build_dataset_for_caption_inference( tokens, lengths, img_src_tokens, img_gpt_input_mask, constraints=constraints_tensor ), max_tokens=cfg.dataset.max_tokens, max_sentences=cfg.dataset.batch_size, max_positions=max_positions, ignore_invalid_inputs=cfg.dataset.skip_invalid_size_inputs_valid_test, ).next_epoch_itr(shuffle=False) for batch in itr: ids = batch["id"] src_tokens = batch["net_input"]["src_tokens"] src_lengths = batch["net_input"]["src_lengths"] img_src_tokens = batch["net_input"]["img_src_tokens"] img_gpt_input_mask = batch["net_input"]["img_gpt_input_mask"] constraints = batch.get("constraints", None) yield Batch( ids=ids, src_tokens=src_tokens, src_lengths=src_lengths, img_src_tokens=img_src_tokens, img_gpt_input_mask=img_gpt_input_mask, constraints=constraints, )
null
183,542
import sys import unilm import ast import fileinput import logging import math import os import sys import time import re from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image def post_process_prediction( hypo_tokens, src_str, alignment, align_dict, tgt_dict, remove_bpe=None, extra_symbols_to_ignore=None, ): hypo_str = tgt_dict.string( hypo_tokens, remove_bpe, extra_symbols_to_ignore=extra_symbols_to_ignore ) if align_dict is not None: hypo_str = utils.replace_unk( hypo_str, src_str, alignment, align_dict, tgt_dict.unk_string() ) if align_dict is not None or remove_bpe is not None: # Convert back to tokens for evaluating with unk replacement or without BPE # Note that the dictionary can be modified inside the method. hypo_tokens = tgt_dict.encode_line(hypo_str, add_if_not_exist=False) return hypo_tokens, hypo_str, alignment
null
183,543
import sys import unilm import ast import fileinput import logging import math import os import sys import time import re from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image def main(cfg: FairseqConfig): if isinstance(cfg, Namespace): cfg = convert_namespace_to_omegaconf(cfg) start_time = time.time() total_translate_time = 0 utils.import_user_module(cfg.common) if cfg.interactive.buffer_size < 1: cfg.interactive.buffer_size = 1 if cfg.dataset.max_tokens is None and cfg.dataset.batch_size is None: cfg.dataset.batch_size = 1 assert ( not cfg.generation.sampling or cfg.generation.nbest == cfg.generation.beam ), "--sampling requires --nbest to be equal to --beam" assert ( not cfg.dataset.batch_size or cfg.dataset.batch_size <= cfg.interactive.buffer_size ), "--batch-size cannot be larger than --buffer-size" logger.info(cfg) # Fix seed for stochastic decoding if cfg.common.seed is not None and not cfg.generation.no_seed_provided: np.random.seed(cfg.common.seed) utils.set_torch_seed(cfg.common.seed) use_cuda = torch.cuda.is_available() and not cfg.common.cpu # Setup task, e.g., translation logger.info("Task: {}".format(cfg.task)) task = tasks.setup_task(cfg.task) # Load ensemble overrides = ast.literal_eval(cfg.common_eval.model_overrides) logger.info("loading model(s) from {}".format(cfg.common_eval.path)) models, _model_args = checkpoint_utils.load_model_ensemble( utils.split_paths(cfg.common_eval.path), arg_overrides=overrides, task=task, suffix=cfg.checkpoint.checkpoint_suffix, strict=(cfg.checkpoint.checkpoint_shard_count == 1), num_shards=cfg.checkpoint.checkpoint_shard_count, ) # Set dictionaries src_dict = task.source_dictionary tgt_dict = task.target_dictionary # Optimize ensemble for generation for model in models: if model is None: continue if cfg.common.fp16: model.half() if use_cuda and not cfg.distributed_training.pipeline_model_parallel: model.cuda() model.prepare_for_inference_(cfg) # Initialize generator generator = task.build_generator(models, cfg.generation) # Handle tokenization and BPE tokenizer = task.build_tokenizer(cfg.tokenizer) bpe = task.build_bpe(cfg.bpe) def encode_fn(x): if tokenizer is not None: x = tokenizer.encode(x) if bpe is not None: x = bpe.encode(x) return x def decode_fn(x): if bpe is not None: x = bpe.decode(x) if tokenizer is not None: x = tokenizer.decode(x) return x # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) align_dict = utils.load_align_dict(cfg.generation.replace_unk) max_positions = utils.resolve_max_positions( task.max_positions(), *[model.max_positions() for model in models] ) if cfg.generation.constraints: logger.warning( "NOTE: Constrained decoding currently assumes a shared subword vocabulary." ) if cfg.interactive.buffer_size > 1: logger.info("Sentence buffer size: %s", cfg.interactive.buffer_size) logger.info("NOTE: hypothesis and token scores are output in base 2") logger.info("Type the input sentence and press return:") start_id = 0 for inputs in buffered_read(cfg.interactive.input, cfg.interactive.buffer_size): print("inputs", inputs) results = [] for batch in make_batches(inputs, cfg, task, max_positions, encode_fn): bsz = batch.src_tokens.size(0) src_tokens = batch.src_tokens src_lengths = batch.src_lengths if generator.max_len_b > 2000: # use too long max_len_b to implement dynamic max_len_b generator.max_len_b = src_lengths.max().item() + generator.max_len_b - 2000 img_src_tokens = batch.img_src_tokens img_gpt_input_mask = batch.img_gpt_input_mask constraints = batch.constraints if use_cuda: src_tokens = src_tokens.cuda() src_lengths = src_lengths.cuda() if constraints is not None: constraints = constraints.cuda() sample = { "net_input": { "src_tokens": src_tokens, "src_lengths": src_lengths, "img_src_tokens": img_src_tokens, "img_gpt_input_mask": img_gpt_input_mask, }, } translate_start_time = time.time() translations = task.inference_step( generator, models, sample, constraints=constraints ) translate_time = time.time() - translate_start_time total_translate_time += translate_time list_constraints = [[] for _ in range(bsz)] if cfg.generation.constraints: list_constraints = [unpack_constraints(c) for c in constraints] for i, (id, hypos) in enumerate(zip(batch.ids.tolist(), translations)): src_tokens_i = utils.strip_pad(src_tokens[i], tgt_dict.pad()) constraints = list_constraints[i] results.append( ( start_id + id, src_tokens_i, hypos, { "constraints": constraints, "time": translate_time / len(translations), }, ) ) # sort output to match input order for id_, src_tokens, hypos, info in sorted(results, key=lambda x: x[0]): src_str = "" if src_dict is not None: # print(src_tokens) src_str = src_dict.string(src_tokens, cfg.common_eval.post_process) print("S-{}\t{}".format(id_, src_str)) print("ST-{}\t{}".format(id_, src_tokens.int().cpu().tolist())) print("W-{}\t{:.3f}\tseconds".format(id_, info["time"])) for constraint in info["constraints"]: print( "C-{}\t{}".format( id_, tgt_dict.string(constraint, cfg.common_eval.post_process), ) ) # Process top predictions for hypo in hypos[: min(len(hypos), cfg.generation.nbest)]: # hypo_tokens, hypo_str, alignment = utils.post_process_prediction( hypo_tokens, hypo_str, alignment = post_process_prediction( hypo_tokens=hypo["tokens"].int().cpu(), src_str=src_str, alignment=hypo["alignment"], align_dict=align_dict, tgt_dict=tgt_dict, remove_bpe=cfg.common_eval.post_process, extra_symbols_to_ignore=get_symbols_to_strip_from_output(generator), ) detok_hypo_str = decode_fn(hypo_str) score = hypo["score"] / math.log(2) # convert to base 2 # original hypothesis (after tokenization and BPE) print("HT-{}\t{}\t{}".format(id_, score, hypo["tokens"].int().cpu().tolist())) print("H-{}\t{}\t{}".format(id_, score, hypo_str)) # detokenized hypothesis print("D-{}\t{}\t{}".format(id_, score, detok_hypo_str)) print( "P-{}\t{}".format( id_, " ".join( map( lambda x: "{:.4f}".format(x), # convert from base e to base 2 hypo["positional_scores"].div_(math.log(2)).tolist(), ) ), ) ) if cfg.generation.print_alignment: alignment_str = " ".join( ["{}-{}".format(src, tgt) for src, tgt in alignment] ) print("A-{}\t{}".format(id_, alignment_str)) # update running id_ counter start_id += len(inputs) logger.info( "Total time: {:.3f} seconds; translation time: {:.3f}".format( time.time() - start_time, total_translate_time ) ) def convert_namespace_to_omegaconf(args: Namespace) -> DictConfig: """Convert a flat argparse.Namespace to a structured DictConfig.""" # Here we are using field values provided in args to override counterparts inside config object overrides, deletes = override_module_args(args) # configs will be in fairseq/config after installation config_path = os.path.join("..", "config") GlobalHydra.instance().clear() with initialize(config_path=config_path): try: composed_cfg = compose("config", overrides=overrides, strict=False) except: logger.error("Error when composing. Overrides: " + str(overrides)) raise for k in deletes: composed_cfg[k] = None cfg = OmegaConf.create( OmegaConf.to_container(composed_cfg, resolve=True, enum_to_str=True) ) # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils with omegaconf_no_object_check(): if cfg.task is None and getattr(args, "task", None): cfg.task = Namespace(**vars(args)) from fairseq.tasks import TASK_REGISTRY _set_legacy_defaults(cfg.task, TASK_REGISTRY[args.task]) cfg.task._name = args.task if cfg.model is None and getattr(args, "arch", None): cfg.model = Namespace(**vars(args)) from fairseq.models import ARCH_MODEL_REGISTRY _set_legacy_defaults(cfg.model, ARCH_MODEL_REGISTRY[args.arch]) cfg.model._name = args.arch if cfg.optimizer is None and getattr(args, "optimizer", None): cfg.optimizer = Namespace(**vars(args)) from fairseq.optim import OPTIMIZER_REGISTRY _set_legacy_defaults(cfg.optimizer, OPTIMIZER_REGISTRY[args.optimizer]) cfg.optimizer._name = args.optimizer if cfg.lr_scheduler is None and getattr(args, "lr_scheduler", None): cfg.lr_scheduler = Namespace(**vars(args)) from fairseq.optim.lr_scheduler import LR_SCHEDULER_REGISTRY _set_legacy_defaults( cfg.lr_scheduler, LR_SCHEDULER_REGISTRY[args.lr_scheduler] ) cfg.lr_scheduler._name = args.lr_scheduler if cfg.criterion is None and getattr(args, "criterion", None): cfg.criterion = Namespace(**vars(args)) from fairseq.criterions import CRITERION_REGISTRY _set_legacy_defaults(cfg.criterion, CRITERION_REGISTRY[args.criterion]) cfg.criterion._name = args.criterion OmegaConf.set_struct(cfg, True) return cfg def cli_main(): parser = options.get_interactive_generation_parser() args = options.parse_args_and_arch(parser) distributed_utils.call_main(convert_namespace_to_omegaconf(args), main)
null
183,544
import json from tqdm import tqdm def cook_data(input_file, image_path, locate_token=None, postfix='.out'): # read a json file print(input_file) dataset = json.load(open(input_file, 'r', encoding='utf-8')) with open(input_file + postfix, 'w', encoding='utf-8') as f: for ann in tqdm(dataset['annotations']): image_id = ann['image_id'] image_info = [img for img in dataset['images'] if img['id'] == image_id][0] file_name = image_info['file_name'] caption = image_info['caption'] # pdb.set_trace() if 'train2014' in file_name: dir_name = 'train2014' else: dir_name = 'val2014' if not locate_token: sentence = f'[image]{image_path}/{dir_name}/{file_name}<tab><phrase>{caption}</phrase>' else: sentence = f'[image]{image_path}/{dir_name}/{file_name}<tab>{locate_token}<phrase>{caption}</phrase>' f.write(sentence + '\n')
null
183,552
import sys import unilm import ast import fileinput import logging import math import os import sys import time import re from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image def main(cfg: FairseqConfig): if isinstance(cfg, Namespace): cfg = convert_namespace_to_omegaconf(cfg) start_time = time.time() total_translate_time = 0 utils.import_user_module(cfg.common) if cfg.interactive.buffer_size < 1: cfg.interactive.buffer_size = 1 if cfg.dataset.max_tokens is None and cfg.dataset.batch_size is None: cfg.dataset.batch_size = 1 assert ( not cfg.generation.sampling or cfg.generation.nbest == cfg.generation.beam ), "--sampling requires --nbest to be equal to --beam" assert ( not cfg.dataset.batch_size or cfg.dataset.batch_size <= cfg.interactive.buffer_size ), "--batch-size cannot be larger than --buffer-size" logger.info(cfg) # Fix seed for stochastic decoding if cfg.common.seed is not None and not cfg.generation.no_seed_provided: np.random.seed(cfg.common.seed) utils.set_torch_seed(cfg.common.seed) use_cuda = torch.cuda.is_available() and not cfg.common.cpu # Setup task, e.g., translation logger.info("Task: {}".format(cfg.task)) task = tasks.setup_task(cfg.task) # Load ensemble overrides = ast.literal_eval(cfg.common_eval.model_overrides) logger.info("loading model(s) from {}".format(cfg.common_eval.path)) models, _model_args = checkpoint_utils.load_model_ensemble( utils.split_paths(cfg.common_eval.path), arg_overrides=overrides, task=task, suffix=cfg.checkpoint.checkpoint_suffix, strict=(cfg.checkpoint.checkpoint_shard_count == 1), num_shards=cfg.checkpoint.checkpoint_shard_count, ) # Set dictionaries src_dict = task.source_dictionary tgt_dict = task.target_dictionary # Optimize ensemble for generation for model in models: if model is None: continue if cfg.common.fp16: model.half() if use_cuda and not cfg.distributed_training.pipeline_model_parallel: model.cuda() model.prepare_for_inference_(cfg) # Initialize generator generator = task.build_generator(models, cfg.generation) # Handle tokenization and BPE tokenizer = task.build_tokenizer(cfg.tokenizer) bpe = task.build_bpe(cfg.bpe) def encode_fn(x): if tokenizer is not None: x = tokenizer.encode(x) if bpe is not None: x = bpe.encode(x) return x def decode_fn(x): if bpe is not None: x = bpe.decode(x) if tokenizer is not None: x = tokenizer.decode(x) return x # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) align_dict = utils.load_align_dict(cfg.generation.replace_unk) max_positions = utils.resolve_max_positions( task.max_positions(), *[model.max_positions() for model in models] ) if cfg.generation.constraints: logger.warning( "NOTE: Constrained decoding currently assumes a shared subword vocabulary." ) if cfg.interactive.buffer_size > 1: logger.info("Sentence buffer size: %s", cfg.interactive.buffer_size) logger.info("NOTE: hypothesis and token scores are output in base 2") logger.info("Type the input sentence and press return:") start_id = 0 for inputs in buffered_read(cfg.interactive.input, cfg.interactive.buffer_size): print("inputs", inputs) results = [] for batch in make_batches(inputs, cfg, task, max_positions, encode_fn): bsz = batch.src_tokens.size(0) src_tokens = batch.src_tokens src_lengths = batch.src_lengths if generator.max_len_b > 2000: # use too long max_len_b to implement dynamic max_len_b generator.max_len_b = src_lengths.max().item() + generator.max_len_b - 2000 img_src_tokens = batch.img_src_tokens img_gpt_input_mask = batch.img_gpt_input_mask constraints = batch.constraints if use_cuda: src_tokens = src_tokens.cuda() src_lengths = src_lengths.cuda() if constraints is not None: constraints = constraints.cuda() sample = { "net_input": { "src_tokens": src_tokens, "src_lengths": src_lengths, "img_src_tokens": img_src_tokens, "img_gpt_input_mask": img_gpt_input_mask, }, } translate_start_time = time.time() translations = task.inference_step( generator, models, sample, constraints=constraints ) translate_time = time.time() - translate_start_time total_translate_time += translate_time list_constraints = [[] for _ in range(bsz)] if cfg.generation.constraints: list_constraints = [unpack_constraints(c) for c in constraints] for i, (id, hypos) in enumerate(zip(batch.ids.tolist(), translations)): src_tokens_i = utils.strip_pad(src_tokens[i], tgt_dict.pad()) constraints = list_constraints[i] results.append( ( start_id + id, src_tokens_i, hypos, { "constraints": constraints, "time": translate_time / len(translations), }, ) ) # sort output to match input order for id_, src_tokens, hypos, info in sorted(results, key=lambda x: x[0]): src_str = "" org_len = len(src_tokens) if src_dict is not None: # print(src_tokens) src_str = src_dict.string(src_tokens, cfg.common_eval.post_process) print("S-{}\t{}".format(id_, src_str)) print("ST-{}\t{}".format(id_, src_tokens.int().cpu().tolist())) print("W-{}\t{:.3f}\tseconds".format(id_, info["time"])) for constraint in info["constraints"]: print( "C-{}\t{}".format( id_, tgt_dict.string(constraint, cfg.common_eval.post_process), ) ) # Process top predictions for hypo in hypos[: min(len(hypos), cfg.generation.nbest)]: # hypo_tokens, hypo_str, alignment = utils.post_process_prediction( hypo_tokens, hypo_str, alignment = post_process_prediction( hypo_tokens=hypo["tokens"].int().cpu(), src_str=src_str, alignment=hypo["alignment"], align_dict=align_dict, tgt_dict=tgt_dict, remove_bpe=cfg.common_eval.post_process, extra_symbols_to_ignore=get_symbols_to_strip_from_output(generator), ) detok_hypo_str = decode_fn(hypo_str) score = hypo["score"] / math.log(2) # convert to base 2 # original hypothesis (after tokenization and BPE) print("HT-{}\t{}\t{}".format(id_, score, hypo["tokens"].int().cpu().tolist())) print("H-{}\t{}\t{}".format(id_, score, hypo_str)) # detokenized hypothesis print("D-{}\t{}\t{}".format(id_, score, detok_hypo_str)) print( "P-{}\t{}".format( id_, " ".join( map( lambda x: "{:.4f}".format(x), # convert from base e to base 2 hypo["positional_scores"][:org_len-1].tolist(), ) ), ) ) if cfg.generation.print_alignment: alignment_str = " ".join( ["{}-{}".format(src, tgt) for src, tgt in alignment] ) print("A-{}\t{}".format(id_, alignment_str)) # update running id_ counter start_id += len(inputs) logger.info( "Total time: {:.3f} seconds; translation time: {:.3f}".format( time.time() - start_time, total_translate_time ) ) def convert_namespace_to_omegaconf(args: Namespace) -> DictConfig: """Convert a flat argparse.Namespace to a structured DictConfig.""" # Here we are using field values provided in args to override counterparts inside config object overrides, deletes = override_module_args(args) # configs will be in fairseq/config after installation config_path = os.path.join("..", "config") GlobalHydra.instance().clear() with initialize(config_path=config_path): try: composed_cfg = compose("config", overrides=overrides, strict=False) except: logger.error("Error when composing. Overrides: " + str(overrides)) raise for k in deletes: composed_cfg[k] = None cfg = OmegaConf.create( OmegaConf.to_container(composed_cfg, resolve=True, enum_to_str=True) ) # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils with omegaconf_no_object_check(): if cfg.task is None and getattr(args, "task", None): cfg.task = Namespace(**vars(args)) from fairseq.tasks import TASK_REGISTRY _set_legacy_defaults(cfg.task, TASK_REGISTRY[args.task]) cfg.task._name = args.task if cfg.model is None and getattr(args, "arch", None): cfg.model = Namespace(**vars(args)) from fairseq.models import ARCH_MODEL_REGISTRY _set_legacy_defaults(cfg.model, ARCH_MODEL_REGISTRY[args.arch]) cfg.model._name = args.arch if cfg.optimizer is None and getattr(args, "optimizer", None): cfg.optimizer = Namespace(**vars(args)) from fairseq.optim import OPTIMIZER_REGISTRY _set_legacy_defaults(cfg.optimizer, OPTIMIZER_REGISTRY[args.optimizer]) cfg.optimizer._name = args.optimizer if cfg.lr_scheduler is None and getattr(args, "lr_scheduler", None): cfg.lr_scheduler = Namespace(**vars(args)) from fairseq.optim.lr_scheduler import LR_SCHEDULER_REGISTRY _set_legacy_defaults( cfg.lr_scheduler, LR_SCHEDULER_REGISTRY[args.lr_scheduler] ) cfg.lr_scheduler._name = args.lr_scheduler if cfg.criterion is None and getattr(args, "criterion", None): cfg.criterion = Namespace(**vars(args)) from fairseq.criterions import CRITERION_REGISTRY _set_legacy_defaults(cfg.criterion, CRITERION_REGISTRY[args.criterion]) cfg.criterion._name = args.criterion OmegaConf.set_struct(cfg, True) return cfg def cli_main(): parser = options.get_interactive_generation_parser() args = options.parse_args_and_arch(parser) distributed_utils.call_main(convert_namespace_to_omegaconf(args), main)
null
183,553
import json from tqdm import tqdm def cook_data_inline(input_file, image_path, locate_token=None, postfix='.inline.out'): # read a json file obj = json.load(open(input_file, 'r', encoding='utf-8')) with open(input_file + postfix, 'w', encoding='utf-8') as f: for item in tqdm(obj['images']): file_name = item["file_name"] caption = item["caption"] if locate_token is None: sentence_prefix = f'[image]{image_path}/{file_name}<tab>' else: sentence_prefix = f'[image]{image_path}/{file_name}<tab>{locate_token}' postive_item_pos = item['tokens_positive_eval'] for pos in postive_item_pos: if len(pos) > 1: print("> 1", postive_item_pos, pos) pos_start, pos_end = pos[0] phrase = caption[pos_start:pos_end] prefix_caption = caption[:pos_start] f.write(sentence_prefix + f'{prefix_caption} <phrase>{phrase}</phrase>\n')
null
183,554
import xml.etree.ElementTree as ET from collections import defaultdict from pathlib import Path from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import numpy as np from prettytable import PrettyTable from tqdm import tqdm from decode_string import decode_bbox_from_caption import json The provided code snippet includes necessary dependencies for implementing the `get_sentence_data` function. Write a Python function `def get_sentence_data(filename) -> List[Dict[str, Any]]` to solve the following problem: Parses a sentence file from the Flickr30K Entities dataset input: filename - full file path to the sentence file to parse output: a list of dictionaries for each sentence with the following fields: sentence - the original sentence phrases - a list of dictionaries for each phrase with the following fields: phrase - the text of the annotated phrase first_word_index - the position of the first word of the phrase in the sentence phrase_id - an identifier for this phrase phrase_type - a list of the coarse categories this phrase belongs to Here is the function: def get_sentence_data(filename) -> List[Dict[str, Any]]: """ Parses a sentence file from the Flickr30K Entities dataset input: filename - full file path to the sentence file to parse output: a list of dictionaries for each sentence with the following fields: sentence - the original sentence phrases - a list of dictionaries for each phrase with the following fields: phrase - the text of the annotated phrase first_word_index - the position of the first word of the phrase in the sentence phrase_id - an identifier for this phrase phrase_type - a list of the coarse categories this phrase belongs to """ with open(filename, "r") as f: sentences = f.read().split("\n") annotations = [] for sentence in sentences: if not sentence: continue first_word = [] phrases = [] phrase_id = [] phrase_type = [] words = [] current_phrase = [] add_to_phrase = False for token in sentence.split(): if add_to_phrase: if token[-1] == "]": add_to_phrase = False token = token[:-1] current_phrase.append(token) phrases.append(" ".join(current_phrase)) current_phrase = [] else: current_phrase.append(token) words.append(token) else: if token[0] == "[": add_to_phrase = True first_word.append(len(words)) parts = token.split("/") phrase_id.append(parts[1][3:]) phrase_type.append(parts[2:]) else: words.append(token) sentence_data = {"sentence": " ".join(words), "phrases": []} for index, phrase, p_id, p_type in zip(first_word, phrases, phrase_id, phrase_type): sentence_data["phrases"].append( {"first_word_index": index, "phrase": phrase, "phrase_id": p_id, "phrase_type": p_type} ) annotations.append(sentence_data) return annotations
Parses a sentence file from the Flickr30K Entities dataset input: filename - full file path to the sentence file to parse output: a list of dictionaries for each sentence with the following fields: sentence - the original sentence phrases - a list of dictionaries for each phrase with the following fields: phrase - the text of the annotated phrase first_word_index - the position of the first word of the phrase in the sentence phrase_id - an identifier for this phrase phrase_type - a list of the coarse categories this phrase belongs to
183,555
import xml.etree.ElementTree as ET from collections import defaultdict from pathlib import Path from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import numpy as np from prettytable import PrettyTable from tqdm import tqdm from decode_string import decode_bbox_from_caption import json The provided code snippet includes necessary dependencies for implementing the `get_annotations` function. Write a Python function `def get_annotations(filename) -> Dict[str, Union[int, List[str], Dict[str, List[List[int]]]]]` to solve the following problem: Parses the xml files in the Flickr30K Entities dataset input: filename - full file path to the annotations file to parse output: dictionary with the following fields: scene - list of identifiers which were annotated as pertaining to the whole scene nobox - list of identifiers which were annotated as not being visible in the image boxes - a dictionary where the fields are identifiers and the values are its list of boxes in the [xmin ymin xmax ymax] format height - int representing the height of the image width - int representing the width of the image depth - int representing the depth of the image Here is the function: def get_annotations(filename) -> Dict[str, Union[int, List[str], Dict[str, List[List[int]]]]]: """ Parses the xml files in the Flickr30K Entities dataset input: filename - full file path to the annotations file to parse output: dictionary with the following fields: scene - list of identifiers which were annotated as pertaining to the whole scene nobox - list of identifiers which were annotated as not being visible in the image boxes - a dictionary where the fields are identifiers and the values are its list of boxes in the [xmin ymin xmax ymax] format height - int representing the height of the image width - int representing the width of the image depth - int representing the depth of the image """ tree = ET.parse(filename) root = tree.getroot() size_container = root.findall("size")[0] anno_info: Dict[str, Union[int, List[str], Dict[str, List[List[int]]]]] = {} all_boxes: Dict[str, List[List[int]]] = {} all_noboxes: List[str] = [] all_scenes: List[str] = [] for size_element in size_container: assert size_element.text anno_info[size_element.tag] = int(size_element.text) for object_container in root.findall("object"): for names in object_container.findall("name"): box_id = names.text assert box_id box_container = object_container.findall("bndbox") if len(box_container) > 0: if box_id not in all_boxes: all_boxes[box_id] = [] xmin = int(box_container[0].findall("xmin")[0].text) ymin = int(box_container[0].findall("ymin")[0].text) xmax = int(box_container[0].findall("xmax")[0].text) ymax = int(box_container[0].findall("ymax")[0].text) all_boxes[box_id].append([xmin, ymin, xmax, ymax]) else: nobndbox = int(object_container.findall("nobndbox")[0].text) if nobndbox > 0: all_noboxes.append(box_id) scene = int(object_container.findall("scene")[0].text) if scene > 0: all_scenes.append(box_id) anno_info["boxes"] = all_boxes anno_info["nobox"] = all_noboxes anno_info["scene"] = all_scenes return anno_info
Parses the xml files in the Flickr30K Entities dataset input: filename - full file path to the annotations file to parse output: dictionary with the following fields: scene - list of identifiers which were annotated as pertaining to the whole scene nobox - list of identifiers which were annotated as not being visible in the image boxes - a dictionary where the fields are identifiers and the values are its list of boxes in the [xmin ymin xmax ymax] format height - int representing the height of the image width - int representing the width of the image depth - int representing the depth of the image
183,556
import xml.etree.ElementTree as ET from collections import defaultdict from pathlib import Path from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import numpy as np from prettytable import PrettyTable from tqdm import tqdm from decode_string import decode_bbox_from_caption import json def _box_inter_union(boxes1: np.array, boxes2: np.array) -> Tuple[np.array, np.array]: area1 = box_area(boxes1) area2 = box_area(boxes2) lt = np.maximum(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2] rb = np.minimum(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2] wh = (rb - lt).clip(min=0) # [N,M,2] inter = wh[:, :, 0] * wh[:, :, 1] # [N,M] union = area1[:, None] + area2 - inter return inter, union The provided code snippet includes necessary dependencies for implementing the `box_iou` function. Write a Python function `def box_iou(boxes1: np.array, boxes2: np.array) -> np.array` to solve the following problem: Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]) boxes2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 Here is the function: def box_iou(boxes1: np.array, boxes2: np.array) -> np.array: """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]) boxes2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ inter, union = _box_inter_union(boxes1, boxes2) iou = inter / union return iou
Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]) boxes2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
183,557
import xml.etree.ElementTree as ET from collections import defaultdict from pathlib import Path from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import numpy as np from prettytable import PrettyTable from tqdm import tqdm from decode_string import decode_bbox_from_caption import json The provided code snippet includes necessary dependencies for implementing the `_merge_boxes` function. Write a Python function `def _merge_boxes(boxes: List[List[int]]) -> List[List[int]]` to solve the following problem: Return the boxes corresponding to the smallest enclosing box containing all the provided boxes The boxes are expected in [x1, y1, x2, y2] format Here is the function: def _merge_boxes(boxes: List[List[int]]) -> List[List[int]]: """ Return the boxes corresponding to the smallest enclosing box containing all the provided boxes The boxes are expected in [x1, y1, x2, y2] format """ if len(boxes) == 1: return boxes np_boxes = np.asarray(boxes) return [[np_boxes[:, 0].min(), np_boxes[:, 1].min(), np_boxes[:, 2].max(), np_boxes[:, 3].max()]]
Return the boxes corresponding to the smallest enclosing box containing all the provided boxes The boxes are expected in [x1, y1, x2, y2] format
183,558
import re import numpy as np def find_patch_index_combinations(s): # The regular expression pattern for matching the required formats pattern = r'(?:(<phrase>([^<]+)</phrase>))?<object>((?:<patch_index_\d+><patch_index_\d+></delimiter_of_multi_objects/>)*<patch_index_\d+><patch_index_\d+>)</object>' # Find all matches in the given string matches = re.findall(pattern, s) # Initialize an empty list to store the valid patch_index combinations valid_combinations = [] for match in matches: phrase_tag, phrase, match_content = match if not phrase_tag: phrase = None # Split the match_content by the delimiter to get individual patch_index pairs patch_index_pairs = match_content.split('</delimiter_of_multi_objects/>') for pair in patch_index_pairs: # Extract the xxxx and yyyy values from the patch_index pair x = re.search(r'<patch_index_(\d+)>', pair) y = re.search(r'<patch_index_(\d+)>', pair[1:]) if x and y: if phrase: valid_combinations.append((phrase, int(x.group(1)), int(y.group(1)))) else: valid_combinations.append((f"<{x.group(1)}><{y.group(1)}>", int(x.group(1)), int(y.group(1)))) return valid_combinations def get_box_coords_from_index(P, ul_idx, lr_idx): """ Given a grid of length P and the indices of the upper-left and lower-right corners of a bounding box, returns the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2]. Args: - P (int): the length of the grid - ul_idx (int): the index of the grid cell that corresponds to the upper-left corner of the bounding box - lr_idx (int): the index of the grid cell that corresponds to the lower-right corner of the bounding box Returns: - box_coords (np.array of shape (4,)): the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2] """ # Compute the size of each cell in the grid cell_size = 1.0 / P # Compute the x and y indices of the upper-left and lower-right corners of the bounding box ul_x = ul_idx % P ul_y = ul_idx // P lr_x = lr_idx % P lr_y = lr_idx // P # Compute the normalized coordinates of the bounding box if ul_idx == lr_idx: x1 = ul_x * cell_size y1 = ul_y * cell_size x2 = lr_x * cell_size + cell_size y2 = lr_y * cell_size + cell_size elif ul_x == lr_x or ul_y == lr_y: x1 = ul_x * cell_size y1 = ul_y * cell_size x2 = lr_x * cell_size + cell_size y2 = lr_y * cell_size + cell_size else: x1 = ul_x * cell_size + cell_size / 2 y1 = ul_y * cell_size + cell_size / 2 x2 = lr_x * cell_size + cell_size / 2 y2 = lr_y * cell_size + cell_size / 2 return np.array([x1, y1, x2, y2]) def decode_bbox_from_caption(caption, quantized_size=32, **kwargs): valid_combinations = find_patch_index_combinations(caption) entity_names = list(map(lambda x: x[0], valid_combinations)) patch_index_coords = list(map(lambda pair: get_box_coords_from_index(quantized_size, pair[1], pair[2]), valid_combinations)) collect_entity_location = [] for entity_name, patch_index_coord in zip(entity_names, patch_index_coords): collect_entity_location.append([entity_name,] + patch_index_coord.tolist()) return collect_entity_location
null
183,559
import base64 import io import random import os from PIL import Image from tqdm import tqdm import json import string import pdb def convert_json_to_txt(json_path, image_path, txt_path, answer_path): json_ann = json.load(open(json_path, 'rb')) # pdb.set_trace() with open(txt_path, 'w', encoding='utf-8') as f, open(answer_path, 'w', encoding='utf-8') as fa: for index, item in tqdm(enumerate(json_ann['questions'])): if item['data_type'] != 'image': continue question = item['question'].replace('\n', ' ') image_file_path = os.path.join(image_path, item['data_id']) options = { 'A': item['choice_a'], 'B': item['choice_b'], 'C': item['choice_c'], 'D': item['choice_d'], } for key, choice in options.items(): choice = choice.replace('\n', ' ').strip() sentence = f'[image]{image_file_path}<tab>Question: {question} Answer: {choice}' f.write(sentence + '\n') fa.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(index, question, choice, item['answer'], item['question_id'], item['question_type_id']))
null
183,560
import ast import json from tqdm import tqdm, trange from collections import defaultdict import re import os, sys import pdb import sys, json def clean_special_tokens(input_string): def find_consecutive_int_indices(numbers): def eval(answer_file, json_file, result_file, split_str='Answer:'): question_type_dict = json.load(open(json_file, 'rb'))['question_type'] question_type_r_dict = {} for k,v in question_type_dict.items(): question_type_r_dict[v] = k split_str = 'Answer:' # ['Answer:', 'A:'][idx] all_answers = defaultdict(dict) all_index = [] with open(answer_file, 'r', encoding='utf-8') as reader: for line in reader: # pdb.set_trace() cols = line.strip().split("\t") all_index.append(cols[0]) all_answers[cols[0]]["answer"] = cols[3] all_answers[cols[0]]["question"] = cols[1] all_answers[cols[0]]["question_type_id"] = cols[-1] all_predictions = defaultdict(list) all_prediction_probs = defaultdict(list) answer_length = None answer_index = None with open(result_file, 'r', encoding='utf-8') as f: for i, line in enumerate(f): if line.startswith('ST-'): src_tokens = ast.literal_eval(line.split('\t')[-1]) answer_index = find_consecutive_int_indices(src_tokens) answer_length = len(src_tokens[answer_index:]) elif line.startswith('H-'): idx = line.split('\t')[0][2:] line = line.split('</image>')[-1] line = line.split('<image>')[0] answer = line.split(split_str)[1].strip() answer = clean_special_tokens(answer) all_predictions[all_index[int(idx)]].append(answer) elif line.startswith('P-'): idx = line.split('\t')[0][2:] scores_list = list(map(float, line.split('\t')[1].split(" "))) answer_scores_list = scores_list[(answer_index-1):] mean_score = sum(answer_scores_list) / len(answer_scores_list) all_prediction_probs[all_index[int(idx)]].append(mean_score) correct = 0 total = 0 answer_map_dict = {0:"A", 1:"B", 2:"C", 3:"D", 4:"E", 5:"F"} question_type_correct = {} question_type_total = {} for k,v in question_type_r_dict.items(): question_type_correct[k] = 0 question_type_total[k] = 0 for qid in all_answers: hit = True prediction = all_prediction_probs[qid].index(max(all_prediction_probs[qid])) if answer_map_dict[prediction] != all_answers[qid]["answer"]: hit = False if hit: correct += 1 question_type_id = int(all_answers[qid]["question_type_id"]) question_type_total[question_type_id] += 1 if hit: question_type_correct[question_type_id] += 1 total += 1 final_scores = {} final_scores["acc"] = correct / total * 100.0 print("{}\t{}\t{}".format(correct, total, final_scores)) for k,v in question_type_r_dict.items(): print(k, v, question_type_correct[k] / max(question_type_total[k], 1))
null
183,561
import os import gzip from sre_parse import SPECIAL_CHARS import numpy as np from random import Random from typing import Any, Callable, Dict, Generator, Iterable, Iterator, List, Optional, Tuple, Union import collections from infinibatch import iterators GRD_SYMBOL="<grounding>" BOP_SYMBOL="<phrase>" EOP_SYMBOL="</phrase>" BOO_SYMBOL="<object>" EOO_SYMBOL="</object>" DOM_SYMBOL="</delimiter_of_multi_objects/>" SPECIAL_SYMBOLS = [EOD_SYMBOL, BOI_SYMBOL, EOI_SYMBOL, EOC_SYMBOL, EOL_SYMBOL] def add_location_symbols(quantized_size, locate_special_token=0): custom_sp_symbols = [] for symbol in SPECIAL_SYMBOLS: custom_sp_symbols.append(symbol) for symbol in [BOP_SYMBOL, EOP_SYMBOL, BOO_SYMBOL, EOO_SYMBOL, DOM_SYMBOL]: custom_sp_symbols.append(symbol) if locate_special_token > 0: custom_sp_symbols.append(GRD_SYMBOL) for i in range(quantized_size ** 2): token_name = f"<patch_index_{str(i).zfill(4)}>" custom_sp_symbols.append(token_name) return custom_sp_symbols
null
183,563
import logging import math from argparse import Namespace from dataclasses import dataclass, field from fairseq.dataclass import ChoiceEnum, FairseqDataclass def _get_wds_dataset(wds_args, preprocess_img, is_train, epoch=0, floor=False): def get_wds_dataset(args, preprocess_img, is_train, epoch=0, floor=False, shard_id=0, num_shards=1, max_sentences=None): wds_args = Namespace( train_data=args.wds_train_data, val_data=args.wds_val_data, dataset_resampled=args.wds_dataset_resampled, train_num_samples=args.wds_train_num_samples, val_num_samples=args.wds_val_num_samples, seed=args.seed + shard_id, batch_size=max_sentences, workers=1, world_size=num_shards, ) return _get_wds_dataset(wds_args, preprocess_img, is_train, epoch=epoch, floor=floor)
null
183,564
import glob import os import numpy as np import time import json import random import itertools import hydra import copy import ast from PIL import Image, ImageDraw, ImageFont import torch import torchvision.transforms as T import torchvision.transforms.functional as F import base64 import io def centercrop_norm_bbox(original_size, resize_size, crop_size, normalized_box, drop_crop_thr=0): ori_img_w, ori_img_h = original_size nx1, ny1, nx2, ny2 = normalized_box ox1, oy1, ox2, oy2 = nx1 * ori_img_w, ny1 * ori_img_h, nx2 * ori_img_w, ny2 * ori_img_h # process resize # calculate the resize image if (ori_img_w <= ori_img_h and ori_img_w == resize_size) or (ori_img_h <= ori_img_w and ori_img_h == resize_size): resize_w, resize_h = ori_img_w, ori_img_h elif ori_img_w < ori_img_h: resize_w = resize_size resize_h = int(resize_size * ori_img_h / ori_img_w) else: resize_h = resize_size resize_w = int(resize_size * ori_img_w / ori_img_h) rx1, ry1, rx2, ry2 = nx1 * resize_w, ny1 * resize_h, nx2 * resize_w, ny2 * resize_h crop_top = (resize_h - crop_size) / 2. crop_left = (resize_w - crop_size) / 2. # re-normalized using original size and crop it max_size = torch.as_tensor([crop_size, crop_size], dtype=torch.float32) boxes = torch.as_tensor([rx1, ry1, rx2, ry2], dtype=torch.float32) cropped_boxes = boxes - torch.as_tensor([crop_left, crop_top, crop_left, crop_top]) cropped_boxes = torch.min(cropped_boxes.reshape(2, 2), max_size) cropped_boxes = cropped_boxes.clamp(min=0) # calculate original box area original_area = (rx2 - rx1) * (ry2 - ry1) cropped_boxes_tmp = cropped_boxes.reshape(4).tolist() cropped_boxes_area = (cropped_boxes_tmp[3] - cropped_boxes_tmp[1]) * (cropped_boxes_tmp[2] - cropped_boxes_tmp[0]) if cropped_boxes_area / original_area < drop_crop_thr: return None # normalized using current size and calculate the area size cropped_boxes /= max_size if torch.all(cropped_boxes[1, :] > cropped_boxes[0, :]): return cropped_boxes.reshape(4).tolist() else: return None def get_box_coords_index(P, box_coords): """ Given a grid of length P and the coordinates of a bounding box, returns the indices of the grid cells that correspond to the upper-left and lower-right corners of the bounding box. Args: - P (int): the length of the grid - box_coords (np.array of shape (4,)): the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2] Returns: - ul_idx (int): the index of the grid cell that corresponds to the upper-left corner of the bounding box - lr_idx (int): the index of the grid cell that corresponds to the lower-right corner of the bounding box """ # pdb.set_trace() # Compute the size of each cell in the grid cell_size = 1.0 / P # Compute the indices of the grid cells that correspond to the upper-left and lower-right corners of the bounding box ul_x = int(np.floor(max(box_coords[0], 0) / cell_size)) ul_y = int(np.floor(max(box_coords[1], 0) / cell_size)) ul_idx = ul_x + ul_y * P lr_x = int(np.floor(min(box_coords[2], 0.99999) / cell_size)) lr_y = int(np.floor(min(box_coords[3], 0.99999) / cell_size)) lr_idx = lr_x + lr_y * P return ul_idx, lr_idx def process_grounding_data(self, item, ori_img_size, mode='phrase', mode_switch_prob=0.5, drop_crop_thr=0., perform_centercrop=True, # default is Ture as in pretraining ): caption = item[1] obj_lists = ast.literal_eval(item[5]) ori_img_w, ori_img_h = ori_img_size cluster_obj_dict = {} if mode == 'phrase': if isinstance(obj_lists, dict): obj_lists = obj_lists['phrase'] for obj_list in obj_lists: phrase_start, phrase_end, x1_norm, y1_norm, x2_norm, y2_norm, score = obj_list if score < self.box_score_threshold: continue # phrase = caption[phrase_start:phrase_end] if perform_centercrop: # center-crop the box to align with the croped image after image transform croped_box = centercrop_norm_bbox((ori_img_w, ori_img_h), self.input_resolution, self.input_resolution, (x1_norm, y1_norm, x2_norm, y2_norm), drop_crop_thr=drop_crop_thr) if croped_box is None: # the box is outside current image area continue x1_norm, y1_norm, x2_norm, y2_norm = croped_box else: croped_box = [x1_norm, y1_norm, x2_norm, y2_norm] ul_idx, lr_idx = get_box_coords_index(self.quantized_size, np.array([x1_norm, y1_norm, x2_norm, y2_norm])) # if ul_idx == lr_idx: # continue # filter the too small boxes if (phrase_start, phrase_end) in cluster_obj_dict.keys(): cluster_obj_dict[(phrase_start, phrase_end)][0].append((ul_idx, lr_idx)) cluster_obj_dict[(phrase_start, phrase_end)][1].append(score) cluster_obj_dict[(phrase_start, phrase_end)][2].append(croped_box) else: cluster_obj_dict[(phrase_start, phrase_end)] = [[(ul_idx, lr_idx)], [score], [croped_box]] return cluster_obj_dict elif mode == 'expression': obj_lists = obj_lists['expression_v1'] filter_obj_lists = [] # filter some boxes that do not meet the conditions for obj_list in obj_lists: phrase_start, phrase_end, x1_norm, y1_norm, x2_norm, y2_norm, score = obj_list if score < self.box_score_threshold: continue if perform_centercrop: # center-crop the box to align with the croped image after image transform croped_box = centercrop_norm_bbox((ori_img_w, ori_img_h), self.input_resolution, self.input_resolution, (x1_norm, y1_norm, x2_norm, y2_norm), drop_crop_thr=drop_crop_thr) if croped_box is None: # the box is outside current image area continue x1_norm, y1_norm, x2_norm, y2_norm = croped_box else: croped_box = [x1_norm, y1_norm, x2_norm, y2_norm] filter_obj_lists.append([phrase_start, phrase_end, x1_norm, y1_norm, x2_norm, y2_norm, score]) obj_lists = filter_obj_lists obj_child_lists = [] for i, obj_list in enumerate(obj_lists): child_dict = {} phrase_start, phrase_end, x1_norm, y1_norm, x2_norm, y2_norm, score = obj_list child_dict['item'] = obj_list # child_dict['range'] = [phrase_start, phrase_end] child_dict['childs'] = [] child_dict['roots'] = [] for j, obj_list2 in enumerate(obj_lists): phrase_start2, phrase_end2, _, _, _, _, _ = obj_list2 # itself or multiple box for one phrase if phrase_start == phrase_start2 and phrase_end == phrase_end2: continue # phrase1 contains phrase2 elif phrase_start <= phrase_start2 and phrase_end >= phrase_end2: child_dict['childs'].append(caption[phrase_start2:phrase_end2]) # phrase2 contains phrase1 elif phrase_start2 <= phrase_start and phrase_end2 >= phrase_end: child_dict['roots'].append(caption[phrase_start2:phrase_end2]) else: continue obj_child_lists.append(child_dict) # we have multiple choices here because some phrase have both childs and roots # mode 1 # we choice the largest range phrase that have childs but not be child of others filter_obj_lists = [] for child_dict in obj_child_lists: if len(child_dict['roots']) == 0: filter_obj_lists.append(child_dict['item']) # turn it to patch-index format and return for i, obj_list in enumerate(filter_obj_lists): phrase_start, phrase_end, x1_norm, y1_norm, x2_norm, y2_norm, score = obj_list ul_idx, lr_idx = get_box_coords_index(self.quantized_size, np.array([x1_norm, y1_norm, x2_norm, y2_norm])) # if ul_idx == lr_idx: # continue # filter the too small boxes if (phrase_start, phrase_end) in cluster_obj_dict.keys(): cluster_obj_dict[(phrase_start, phrase_end)][0].append((ul_idx, lr_idx)) cluster_obj_dict[(phrase_start, phrase_end)][1].append(score) cluster_obj_dict[(phrase_start, phrase_end)][2].append([x1_norm, y1_norm, x2_norm, y2_norm]) else: cluster_obj_dict[(phrase_start, phrase_end)] = [[(ul_idx, lr_idx),], [score,], [[x1_norm, y1_norm, x2_norm, y2_norm],]] return cluster_obj_dict
null
183,565
import glob import os import numpy as np import time import json import random import itertools import hydra import copy import ast from PIL import Image, ImageDraw, ImageFont import torch import torchvision.transforms as T import torchvision.transforms.functional as F import base64 import io def plot_boxes_to_image(image_pil, tgt): H, W = tgt["size"] boxes = tgt["boxes"] labels = tgt["labels"] assert len(boxes) == len(labels), "boxes and labels must have same length" draw = ImageDraw.Draw(image_pil) mask = Image.new("L", image_pil.size, 0) mask_draw = ImageDraw.Draw(mask) # draw boxes and masks for box, label in zip(boxes, labels): # from 0..1 to 0..W, 0..H box = box * torch.Tensor([W, H, W, H]) # random color color = tuple(np.random.randint(0, 255, size=3).tolist()) # draw x0, y0, x1, y1 = box x0, y0, x1, y1 = int(x0), int(y0), int(x1), int(y1) draw.rectangle([x0, y0, x1, y1], outline=color, width=6) font = ImageFont.load_default() if hasattr(font, "getbbox"): bbox = draw.textbbox((x0, y0), str(label), font) else: w, h = draw.textsize(str(label), font) bbox = (x0, y0, w + x0, y0 + h) # bbox = draw.textbbox((x0, y0), str(label)) draw.rectangle(bbox, fill=color) draw.text((x0, y0), str(label), fill="white") mask_draw.rectangle([x0, y0, x1, y1], fill=255, width=6) print(f"Add {label}-{box.tolist()}") return image_pil def get_box_coords_from_index(P, ul_idx, lr_idx): """ Given a grid of length P and the indices of the upper-left and lower-right corners of a bounding box, returns the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2]. Args: - P (int): the length of the grid - ul_idx (int): the index of the grid cell that corresponds to the upper-left corner of the bounding box - lr_idx (int): the index of the grid cell that corresponds to the lower-right corner of the bounding box Returns: - box_coords (np.array of shape (4,)): the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2] """ # Compute the size of each cell in the grid cell_size = 1.0 / P # Compute the x and y indices of the upper-left and lower-right corners of the bounding box ul_x = ul_idx % P ul_y = ul_idx // P lr_x = lr_idx % P lr_y = lr_idx // P # Compute the normalized coordinates of the bounding box if ul_idx == lr_idx: x1 = ul_x * cell_size y1 = ul_y * cell_size x2 = lr_x * cell_size + cell_size y2 = lr_y * cell_size + cell_size elif ul_x == lr_x or ul_y == lr_y: x1 = ul_x * cell_size y1 = ul_y * cell_size x2 = lr_x * cell_size + cell_size y2 = lr_y * cell_size + cell_size else: x1 = ul_x * cell_size + cell_size / 2 y1 = ul_y * cell_size + cell_size / 2 x2 = lr_x * cell_size + cell_size / 2 y2 = lr_y * cell_size + cell_size / 2 return np.array([x1, y1, x2, y2]) The provided code snippet includes necessary dependencies for implementing the `visualize_normed_img_with_bbox` function. Write a Python function `def visualize_normed_img_with_bbox(image_np, cluster_obj_dict, bin_size, caption, name)` to solve the following problem: Args: image_np (_type_): np.array cluster_obj_dict (_type_): {(1, 10): [[(0, 1024)], [0.6991432]]} bin_size (_type_): 32 caption Here is the function: def visualize_normed_img_with_bbox(image_np, cluster_obj_dict, bin_size, caption, name): """ Args: image_np (_type_): np.array cluster_obj_dict (_type_): {(1, 10): [[(0, 1024)], [0.6991432]]} bin_size (_type_): 32 caption """ # (0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711) norm_mean = torch.tensor([0.48145466, 0.4578275, 0.40821073])[:, None, None] norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None] to_pil = T.ToPILImage() # pdb.set_trace() image_tensor = torch.from_numpy(image_np) image_tensor = image_tensor * norm_std + norm_mean # image_tensor = image_tensor[[2, 1, 0], ...] * 255 image = to_pil(image_tensor) image.save(os.path.join('output', 'debug', f"{name}.jpg")) # visualize original box after crop tgt = {} W, H = image.size tgt['size'] = (W, H) tgt['boxes'] = [] tgt['labels'] = [] for k, v in cluster_obj_dict.items(): label = caption[k[0]:k[1]] for _v in v[2]: # original box tgt['labels'].append(label) tgt['boxes'].append(torch.as_tensor(_v)) print(f"\n draw {tgt} on image {name}") draw_image = plot_boxes_to_image(image.copy(), tgt) draw_image.save(os.path.join('output', 'debug', f"{name}_with_orig_box.jpg")) # visualize quantized box tgt = {} W, H = image.size tgt['size'] = (W, H) tgt['boxes'] = [] tgt['labels'] = [] for k, v in cluster_obj_dict.items(): label = caption[k[0]:k[1]] for _v in v[0]: tgt['labels'].append(label) ul_idx, lr_idx = _v box = get_box_coords_from_index(bin_size, ul_idx, lr_idx) tgt['boxes'].append(torch.from_numpy(box)) print(f"\n draw {tgt} on image {name}") draw_image = plot_boxes_to_image(image.copy(), tgt) draw_image.save(os.path.join('output', 'debug', f"{name}_with_quan_box.jpg"))
Args: image_np (_type_): np.array cluster_obj_dict (_type_): {(1, 10): [[(0, 1024)], [0.6991432]]} bin_size (_type_): 32 caption
183,566
import json import os import multiprocessing import itertools import ast from infinibatch import iterators from functools import partial from tiktoken.core import Encoding import glob import os import torch import numpy as np import time import json import random import itertools import hydra import copy import torchvision.transforms as T from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from infinibatch import iterators from unilm.data.basic_loader import BaseBatchGen from unilm.data.utils import NativeCheckpointableIterator, WeightIterator from unilm.data.vl.vl_base_loader import VLBaseLoader from unilm.data.vl.laion2b_loader import Laion2BLoader, NumpyNormalize from unilm.data.vl.obj_utils import * from PIL import Image import base64 import io import logging import pdb def find_substring_pairs(input_str, pos_list, tokenizer): substring_positions = [] for (pos_start, pos_end) in pos_list: before_pos_string = input_str[:pos_start] before_pos_string_tokenized_list = tokenizer.encode(before_pos_string, out_type=str) after_pos_string = input_str[:pos_end] after_pos_string_tokenized_list = tokenizer.encode(after_pos_string, out_type=str) before_length = len(before_pos_string_tokenized_list) if before_pos_string_tokenized_list == after_pos_string_tokenized_list[:before_length]: substring_positions.append([before_length, len(after_pos_string_tokenized_list)-1]) elif before_pos_string_tokenized_list[:-1] == after_pos_string_tokenized_list[:before_length-1]: substring_positions.append([before_length-1, len(after_pos_string_tokenized_list)-1]) else: raise AssertionError(f"{before_pos_string_tokenized_list} is not contained in {after_pos_string_tokenized_list} when the pos_list is [{pos_start}, {pos_end}]") return substring_positions
null
183,567
import torch import torch.nn as nn from fairseq.modules import MultiheadAttention from fairseq import utils class SimpleConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim): super().__init__() self.dense = nn.Linear(input_dim, output_dim) def forward(self, features, **kwargs): x = self.dense(features) return x class ComplexConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim, activation_fn): super().__init__() self.dense = nn.Linear(input_dim, input_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.predict = nn.Linear(input_dim, output_dim) def forward(self, features, **kwargs): x = self.dense(features) x = self.activation_fn(x) x = self.predict(x) return x class XConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim, args,): super().__init__() self.dense = nn.Linear(input_dim, output_dim) self.latent_query = torch.nn.Parameter(torch.randn(args.latent_query_num, output_dim)) self.x_attn = MultiheadAttention( output_dim, args.decoder_attention_heads, kdim=output_dim, vdim=output_dim, dropout=args.attention_dropout, encoder_decoder_attention=True, ) def forward(self, features, **kwargs): x = self.dense(features) # x = attention_i(q=latent_query, kv=concat([x, latent_query])) # shape of x is [batch_size * seq_len, output_dim] -> [seq_len, batch_size, output_dim] x = x.view(-1, kwargs['src_len'], x.size(-1)).transpose(0, 1) bsz = x.size(1) latent_query = self.latent_query.unsqueeze(1).expand(-1, bsz, -1) x, _ = self.x_attn(latent_query, torch.cat([x, latent_query]), torch.cat([x, latent_query])) return x.transpose(0, 1).contiguous().view(-1, x.size(-1)) # [batch_size * seq_len, output_dim] def build_connector(args, input_dim, output_dim): if isinstance(args, str): connector_name = args else: connector_name = args.text_connector if hasattr(args, "text_connector") else args.connector if connector_name == "none": connector = None elif connector_name == "complex": connector = ComplexConnector(input_dim, output_dim, args.activation_fn) elif connector_name == "simple": connector = SimpleConnector(input_dim, output_dim) elif connector_name == "xconnector": connector = XConnector(input_dim, output_dim, args) else: raise ValueError("Invalid text connector type: {}".format(connector_name)) return connector
null
183,568
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def slice_tokens_for_mlm(A, indx, num_elem=2): all_indx = indx[:,None] + torch.arange(num_elem) return A[torch.arange(all_indx.shape[0])[:,None], all_indx]
null
183,569
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def gptmodel_small(args): # 125M params args.decoder_layers = safe_getattr(args, "decoder_layers", 12) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 768) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 12) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) base_gpt3_architecture(args) roberta_base_architecture(args)
null
183,570
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def gptmodel_medium(args): # 355M params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1024) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.pooler_dropout = safe_getattr(args, "pooler_dropout", 0.1) base_gpt3_architecture(args) roberta_base_architecture(args)
null
183,571
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def gptmodel_large(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1536) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args)
null
183,572
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def gptmodel_xl(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args)
null
183,573
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): def base_gpt3_architecture(args): def gptmodel_2B(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 36) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args)
null
183,574
from dataclasses import dataclass, field from typing import Optional from fairseq.dataclass import ChoiceEnum, FairseqDataclass import logging import numpy as np import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.data import Dictionary from fairseq.utils import safe_getattr, safe_hasattr from fairseq.modules import LayerNorm from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaEncoder, RobertaModel, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig from unilm.models.gpt_eval import GPTEvalmodel from torchscale.architecture.config import EncoderConfig from torchscale.model.BEiT3 import BEiT3 import pdb def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = safe_getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", True) args.dropout = safe_getattr(args, "dropout", 0.0) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.0) args.activation_fn = safe_getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) def gptmodel_6B(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 40) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 3584) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 28) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args)
null
183,575
import logging import os import torch from copy import deepcopy from typing import Tuple, Union, Callable, Optional from torch import nn from torch.nn import functional as F from open_clip.model import CLIP, CLIPVisionCfg, QuickGELU, TimmModel, ModifiedResNet, VisualTransformer, to_2tuple, LayerNorm, Transformer from open_clip.factory import _MODEL_CONFIGS, list_models, load_checkpoint, get_pretrained_url, download_pretrained, load_state_dict logger = logging.getLogger(__name__) class ClipVisualOnly(nn.Module): # text_cfg for compatibility with original CLIP def __init__(self, embed_dim, vision_cfg, text_cfg, quick_gelu=False): super().__init__() if isinstance(vision_cfg, dict): vision_cfg = CLIPVisionCfg(**vision_cfg) # OpenAI models are pretrained w/ QuickGELU but native nn.GELU is both faster and more # memory efficient in recent PyTorch releases (>= 1.10). # NOTE: timm models always use native GELU regardless of quick_gelu flag. act_layer = QuickGELU if quick_gelu else nn.GELU if vision_cfg.timm_model_name: raise NotImplementedError self.visual = TimmModel( vision_cfg.timm_model_name, pretrained=vision_cfg.timm_model_pretrained, pool=vision_cfg.timm_pool, proj=vision_cfg.timm_proj, embed_dim=embed_dim, image_size=vision_cfg.image_size) act_layer = nn.GELU # so that text transformer doesn't use QuickGELU w/ timm models elif isinstance(vision_cfg.layers, (tuple, list)): raise NotImplementedError vision_heads = vision_cfg.width * 32 // vision_cfg.head_width self.visual = ModifiedResNet( layers=vision_cfg.layers, output_dim=embed_dim, heads=vision_heads, image_size=vision_cfg.image_size, width=vision_cfg.width) else: vision_heads = vision_cfg.width // vision_cfg.head_width self.visual = VisualTransformer4Seq2Seq( image_size=vision_cfg.image_size, patch_size=vision_cfg.patch_size, width=vision_cfg.width, layers=vision_cfg.layers, heads=vision_heads, mlp_ratio=vision_cfg.mlp_ratio, output_dim=embed_dim, act_layer=act_layer,) self.init_parameters() def init_parameters(self): if hasattr(self.visual, 'init_parameters'): self.visual.init_parameters() def set_grad_checkpointing(self, enable=True): self.visual.set_grad_checkpointing(enable) def encode_image(self, image): return self.visual(image) def forward(self, image): image_features = self.encode_image(image) image_features = F.normalize(image_features, dim=-1) return image_features def load_checkpoint4vision_only(model, checkpoint_path, strict=True): state_dict = load_state_dict(checkpoint_path) incompatible_keys = model.load_state_dict(state_dict, strict=strict) return incompatible_keys def create_model( model_name: str, pretrained: str = '', jit: bool = False, force_quick_gelu: bool = False, pretrained_image: bool = False,): model_name = model_name.replace('/', '-') # for callers using old naming with / in ViT names if pretrained and pretrained.lower() == 'openai': raise NotImplementedError else: if model_name in _MODEL_CONFIGS: logger.info(f'Loading {model_name} model config.') model_cfg = deepcopy(_MODEL_CONFIGS[model_name]) else: logging.error(f'Model config for {model_name} not found; available models {list_models()}.') raise RuntimeError(f'Model config for {model_name} not found.') if force_quick_gelu: # override for use of QuickGELU on non-OpenAI transformer models model_cfg["quick_gelu"] = True if pretrained_image: if 'timm_model_name' in model_cfg.get('vision_cfg', {}): # pretrained weight loading for timm models set via vision_cfg model_cfg['vision_cfg']['timm_model_pretrained'] = True else: assert False, 'pretrained image towers currently only supported for timm models' model = ClipVisualOnly(**model_cfg) if not pretrained: # reload attn weights into ts attn dim = model.visual.transformer.resblocks[0].attn.in_proj_weight.shape[0] // 3 for resblock in model.visual.transformer.resblocks: resblock.ts_attn.q_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[:dim].clone()) resblock.ts_attn.q_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[:dim].clone()) resblock.ts_attn.k_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[dim:2*dim].clone()) resblock.ts_attn.k_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[dim:2*dim].clone()) resblock.ts_attn.v_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[2*dim:].clone()) resblock.ts_attn.v_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[2*dim:].clone()) resblock.ts_attn.out_proj.weight = nn.Parameter(resblock.attn.out_proj.weight.clone()) resblock.ts_attn.out_proj.bias = nn.Parameter(resblock.attn.out_proj.bias.clone()) resblock.attn = None if pretrained: logger.info(f'Loading {model_name} checkpoint from: {pretrained}') checkpoint_path = '' url = get_pretrained_url(model_name, pretrained) if url: checkpoint_path = download_pretrained(url) elif os.path.exists(pretrained): checkpoint_path = pretrained if checkpoint_path: logging.info(f'Loading pretrained {model_name} weights ({pretrained}).') # NOTE TODO remove, strict=True is only for debug load_checkpoint4vision_only(model, checkpoint_path, strict=False) # reload attn weights into ts attn dim = model.visual.transformer.resblocks[0].attn.in_proj_weight.shape[0] // 3 for resblock in model.visual.transformer.resblocks: resblock.ts_attn.q_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[:dim].clone()) resblock.ts_attn.q_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[:dim].clone()) resblock.ts_attn.k_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[dim:2*dim].clone()) resblock.ts_attn.k_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[dim:2*dim].clone()) resblock.ts_attn.v_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[2*dim:].clone()) resblock.ts_attn.v_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[2*dim:].clone()) resblock.ts_attn.out_proj.weight = nn.Parameter(resblock.attn.out_proj.weight.clone()) resblock.ts_attn.out_proj.bias = nn.Parameter(resblock.attn.out_proj.bias.clone()) resblock.attn = None else: logging.warning(f'Pretrained weights ({pretrained}) not found for model {model_name}.') raise RuntimeError(f'Pretrained weights ({pretrained}) not found for model {model_name}.') if jit: model = torch.jit.script(model) return model
null
183,576
import sys import unilm import ast import logging import math import os import sys import time import re import random from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image from draw_box import * import gradio as gr Batch = namedtuple("Batch", "ids src_tokens src_lengths constraints img_src_tokens img_gpt_input_mask") def get_interactive_tokens_and_lengths(self, lines, encode_fn, tokenizer=None): def pack_constraints(batch_constraints: List[List[torch.Tensor]]) -> torch.Tensor: def make_batches(lines, cfg, task, max_positions, encode_fn): def encode_fn_target(x): return encode_fn(x) if cfg.generation.constraints: # Strip (tab-delimited) contraints, if present, from input lines, # store them in batch_constraints batch_constraints = [list() for _ in lines] for i, line in enumerate(lines): if "\t" in line: lines[i], *batch_constraints[i] = line.split("\t") # Convert each List[str] to List[Tensor] for i, constraint_list in enumerate(batch_constraints): batch_constraints[i] = [ task.target_dictionary.encode_line( encode_fn_target(constraint), append_eos=False, add_if_not_exist=False, ) for constraint in constraint_list ] if cfg.generation.constraints: constraints_tensor = pack_constraints(batch_constraints) else: constraints_tensor = None tokenizer = spm.SentencePieceProcessor() if os.path.exists('data/sentencepiece.bpe.model'): tokenizer.Load('data/sentencepiece.bpe.model') else: tokenizer = None tokens, lengths, img_src_tokens, img_gpt_input_mask = get_interactive_tokens_and_lengths(task, lines, encode_fn, tokenizer) itr = task.get_batch_iterator( dataset=task.build_dataset_for_caption_inference( tokens, lengths, img_src_tokens, img_gpt_input_mask, constraints=constraints_tensor ), max_tokens=cfg.dataset.max_tokens, max_sentences=cfg.dataset.batch_size, max_positions=max_positions, ignore_invalid_inputs=cfg.dataset.skip_invalid_size_inputs_valid_test, ).next_epoch_itr(shuffle=False) for batch in itr: ids = batch["id"] src_tokens = batch["net_input"]["src_tokens"] src_lengths = batch["net_input"]["src_lengths"] img_src_tokens = batch["net_input"]["img_src_tokens"] img_gpt_input_mask = batch["net_input"]["img_gpt_input_mask"] constraints = batch.get("constraints", None) yield Batch( ids=ids, src_tokens=src_tokens, src_lengths=src_lengths, img_src_tokens=img_src_tokens, img_gpt_input_mask=img_gpt_input_mask, constraints=constraints, )
null
183,577
import sys import unilm import ast import logging import math import os import sys import time import re import random from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image from draw_box import * import gradio as gr def remove_special_fields(text): return re.sub('<.*?>', '', text) def find_phrases(text): phrases = re.finditer('<phrase>(.*?)</phrase>', text) return [(match.group(1), match.start(1), match.end(1)) for match in phrases] def adjust_phrase_positions(phrases, text): positions = [] for phrase, start, end in phrases: adjusted_start = len(remove_special_fields(text[:start])) adjusted_end = len(remove_special_fields(text[:end])) positions.append((phrase, adjusted_start, adjusted_end)) return positions def mark_words(text, phrases): marked_words = [] words = re.findall(r'\b\w+\b|[.,;?!:()"“”‘’\']', text) word_indices = [match.start() for match in re.finditer(r'\b\w+\b|[.,;?!:()"“”‘’\']', text)] for i, word in enumerate(words): if any(start <= word_indices[i] < end for _, start, end in phrases): marked_words.append((word, 'box')) else: marked_words.append((word, None)) return marked_words def merge_adjacent_words(marked_words): merged_words = [] current_word, current_flag = marked_words[0] for word, flag in marked_words[1:]: if flag == current_flag: current_word += " " + word else: merged_words.append((current_word, current_flag)) current_word = word current_flag = flag merged_words.append((current_word, current_flag)) return merged_words def mark_texts(text): cleaned_text = remove_special_fields(text) phrases = find_phrases(text) adjusted_phrases = adjust_phrase_positions(phrases, text) marked_words = mark_words(cleaned_text, adjusted_phrases) merge_words = merge_adjacent_words(marked_words) return merge_words
null
183,578
import sys import unilm import ast import logging import math import os import sys import time import re import random from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image from draw_box import * import gradio as gr def post_process_prediction( hypo_tokens, src_str, alignment, align_dict, tgt_dict, remove_bpe=None, extra_symbols_to_ignore=None, ): hypo_str = tgt_dict.string( hypo_tokens, remove_bpe, extra_symbols_to_ignore=extra_symbols_to_ignore ) if align_dict is not None: hypo_str = utils.replace_unk( hypo_str, src_str, alignment, align_dict, tgt_dict.unk_string() ) if align_dict is not None or remove_bpe is not None: # Convert back to tokens for evaluating with unk replacement or without BPE # Note that the dictionary can be modified inside the method. hypo_tokens = tgt_dict.encode_line(hypo_str, add_if_not_exist=False) return hypo_tokens, hypo_str, alignment
null
183,579
import sys import unilm import ast import logging import math import os import sys import time import re import random from argparse import Namespace from collections import namedtuple import numpy as np import torch from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.token_generation_constraints import pack_constraints, unpack_constraints from fairseq_cli.generate import get_symbols_to_strip_from_output import sentencepiece as spm from torchvision import transforms from PIL import Image from draw_box import * import gradio as gr def main(cfg: FairseqConfig): if isinstance(cfg, Namespace): cfg = convert_namespace_to_omegaconf(cfg) start_time = time.time() total_translate_time = 0 utils.import_user_module(cfg.common) if cfg.interactive.buffer_size < 1: cfg.interactive.buffer_size = 1 if cfg.dataset.max_tokens is None and cfg.dataset.batch_size is None: cfg.dataset.batch_size = 1 assert ( not cfg.generation.sampling or cfg.generation.nbest == cfg.generation.beam ), "--sampling requires --nbest to be equal to --beam" assert ( not cfg.dataset.batch_size or cfg.dataset.batch_size <= cfg.interactive.buffer_size ), "--batch-size cannot be larger than --buffer-size" logger.info(cfg) # Fix seed for stochastic decoding if cfg.common.seed is not None and not cfg.generation.no_seed_provided: np.random.seed(cfg.common.seed) utils.set_torch_seed(cfg.common.seed) use_cuda = torch.cuda.is_available() and not cfg.common.cpu # Setup task, e.g., translation logger.info("Task: {}".format(cfg.task)) task = tasks.setup_task(cfg.task) # Load ensemble overrides = ast.literal_eval(cfg.common_eval.model_overrides) logger.info("loading model(s) from {}".format(cfg.common_eval.path)) models, _model_args = checkpoint_utils.load_model_ensemble( utils.split_paths(cfg.common_eval.path), arg_overrides=overrides, task=task, suffix=cfg.checkpoint.checkpoint_suffix, strict=(cfg.checkpoint.checkpoint_shard_count == 1), num_shards=cfg.checkpoint.checkpoint_shard_count, ) # Set dictionaries src_dict = task.source_dictionary tgt_dict = task.target_dictionary # Optimize ensemble for generation for model in models: if model is None: continue if cfg.common.fp16: model.half() if use_cuda and not cfg.distributed_training.pipeline_model_parallel: model.cuda() model.prepare_for_inference_(cfg) # Initialize generator generator = task.build_generator(models, cfg.generation) # Handle tokenization and BPE tokenizer = task.build_tokenizer(cfg.tokenizer) bpe = task.build_bpe(cfg.bpe) def encode_fn(x): if tokenizer is not None: x = tokenizer.encode(x) if bpe is not None: x = bpe.encode(x) return x def decode_fn(x): if bpe is not None: x = bpe.decode(x) if tokenizer is not None: x = tokenizer.decode(x) return x # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) align_dict = utils.load_align_dict(cfg.generation.replace_unk) max_positions = utils.resolve_max_positions( task.max_positions(), *[model.max_positions() for model in models] ) if cfg.generation.constraints: logger.warning( "NOTE: Constrained decoding currently assumes a shared subword vocabulary." ) if cfg.interactive.buffer_size > 1: logger.info("Sentence buffer size: %s", cfg.interactive.buffer_size) logger.info("NOTE: hypothesis and token scores are output in base 2") logger.info("Type the input sentence and press return:") start_id = 0 def generate_predictions(image_input, text_input, do_sample, sampling_topp, sampling_temperature): if do_sample: cfg.generation.sampling = True cfg.generation.sampling_topp = sampling_topp cfg.generation.temperature = sampling_temperature cfg.generation.beam = 1 else: cfg.generation.sampling = False cfg.generation.sampling_topp = -1.0 cfg.generation.temperature = 1.0 cfg.generation.beam = 1 generator = task.build_generator(models, cfg.generation) if image_input is None: user_image_path = None else: user_image_path = "/tmp/user_input_test_image.jpg" image_input.save(user_image_path) if text_input.lower() == 'brief': inputs = f"[image]{user_image_path}<tab><grounding>An image of" else: inputs = f"[image]{user_image_path}<tab><grounding>Describe this image in detail:" print("inputs", inputs) inputs = [inputs,] results = [] for batch in make_batches(inputs, cfg, task, max_positions, encode_fn): bsz = batch.src_tokens.size(0) src_tokens = batch.src_tokens src_lengths = batch.src_lengths img_src_tokens = batch.img_src_tokens img_gpt_input_mask = batch.img_gpt_input_mask constraints = batch.constraints if use_cuda: src_tokens = src_tokens.cuda() src_lengths = src_lengths.cuda() if constraints is not None: constraints = constraints.cuda() sample = { "net_input": { "src_tokens": src_tokens, "src_lengths": src_lengths, "img_src_tokens": img_src_tokens, "img_gpt_input_mask": img_gpt_input_mask, }, } translate_start_time = time.time() translations = task.inference_step( generator, models, sample, constraints=constraints ) translate_time = time.time() - translate_start_time # total_translate_time += translate_time list_constraints = [[] for _ in range(bsz)] if cfg.generation.constraints: list_constraints = [unpack_constraints(c) for c in constraints] for i, (id, hypos) in enumerate(zip(batch.ids.tolist(), translations)): src_tokens_i = utils.strip_pad(src_tokens[i], tgt_dict.pad()) constraints = list_constraints[i] results.append( ( start_id + id, src_tokens_i, hypos, { "constraints": constraints, "time": translate_time / len(translations), }, ) ) global global_cnt global_cnt += 1 # sort output to match input order for id_, src_tokens, hypos, info in sorted(results, key=lambda x: x[0]): src_str = "" if src_dict is not None: src_str = src_dict.string(src_tokens, cfg.common_eval.post_process) print("S-{}\t{}".format(global_cnt, src_str)) print("W-{}\t{:.3f}\tseconds".format(global_cnt, info["time"])) for constraint in info["constraints"]: print( "C-{}\t{}".format( global_cnt, tgt_dict.string(constraint, cfg.common_eval.post_process), ) ) # Process top predictions for hypo in hypos[: min(len(hypos), cfg.generation.nbest)]: hypo_tokens, hypo_str, alignment = post_process_prediction( hypo_tokens=hypo["tokens"].int().cpu(), src_str=src_str, alignment=hypo["alignment"], align_dict=align_dict, tgt_dict=tgt_dict, remove_bpe=cfg.common_eval.post_process, extra_symbols_to_ignore=get_symbols_to_strip_from_output(generator), ) detok_hypo_str = decode_fn(hypo_str) # show the results on the image response_str = detok_hypo_str.split('</image>')[-1] if global_image_path is not None: basename = os.path.basename(global_image_path).split('.')[0] vis_image = visualize_results_on_image(global_image_path, response_str, task.args.location_bin_size, f"output/store_vis_results/show_box_on_{basename}.jpg", show=False) # if global_image_tensor is not None: # basename = os.path.basename(global_image_path).split('.')[0] # vis_image = visualize_results_on_image(global_image_tensor, response_str, task.args.location_bin_size, f"output/store_vis_results/show_box_on_{basename}.jpg", show=False) clean_response_str = re.sub('<[^>]*>', '', response_str) clean_response_str = ' '.join(clean_response_str.split()).strip() score = hypo["score"] / math.log(2) # convert to base 2 # original hypothesis (after tokenization and BPE) print("H-{}\t{}\t{}".format(global_cnt, score, hypo_str)) # detokenized hypothesis print("D-{}\t{}\t{}".format(global_cnt, score, detok_hypo_str)) print( "P-{}\t{}".format( global_cnt, " ".join( map( lambda x: "{:.4f}".format(x), # convert from base e to base 2 hypo["positional_scores"].div_(math.log(2)).tolist(), ) ), ) ) if cfg.generation.print_alignment: alignment_str = " ".join( ["{}-{}".format(src, tgt) for src, tgt in alignment] ) print("A-{}\t{}".format(global_cnt, alignment_str)) # return vis_image, str(clean_response_str), str(response_str) return vis_image, mark_texts(response_str) term_of_use = """ ### Terms of use By using this model, users are required to agree to the following terms: The model is intended for academic and research purposes. The utilization of the model to create unsuitable material is strictly forbidden and not endorsed by this work. The accountability for any improper or unacceptable application of the model rests exclusively with the individuals who generated such content. ### License This project is licensed under the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct). """ with gr.Blocks(title="Kosmos-2", theme=gr.themes.Base()).queue() as demo: gr.Markdown((""" # Kosmos-2: Grounding Multimodal Large Language Models to the World [[Paper]](https://arxiv.org/abs/2306.14824) [[Code]](https://github.com/microsoft/unilm/blob/master/kosmos-2) """)) with gr.Row(): with gr.Column(): image_input = gr.Image(type="pil", label="Test Image") text_input = gr.Radio(["Brief", "Detailed"], label="Description Type", value="Brief") do_sample = gr.Checkbox(label="Enable Sampling", info="(Please enable it before adjusting sampling parameters below)", value=False) with gr.Accordion("Sampling parameters", open=False) as sampling_parameters: sampling_topp = gr.Slider(minimum=0.1, maximum=1, step=0.01, value=0.9, label="Sampling: Top-P") sampling_temperature = gr.Slider(minimum=0.1, maximum=1, step=0.01, value=0.7, label="Sampling: Temperature") run_button = gr.Button(label="Run", visible=True) with gr.Column(): image_output = gr.Image(type="pil") text_output1 = gr.HighlightedText( label="Generated Description", combine_adjacent=False, show_legend=True, ).style(color_map={"box": "red"}) with gr.Row(): with gr.Column(): gr.Examples(examples=[ ["demo/images/two_dogs.jpg", "Detailed", False], ["demo/images/snowman.png", "Brief", False], ["demo/images/man_ball.png", "Detailed", False], ], inputs=[image_input, text_input, do_sample]) with gr.Column(): gr.Examples(examples=[ ["demo/images/six_planes.png", "Brief", False], ["demo/images/quadrocopter.jpg", "Brief", False], ["demo/images/carnaby_street.jpg", "Brief", False], ], inputs=[image_input, text_input, do_sample]) gr.Markdown(term_of_use) run_button.click(fn=generate_predictions, inputs=[image_input, text_input, do_sample, sampling_topp, sampling_temperature], outputs=[image_output, text_output1], show_progress=True, queue=True) demo.launch(share=True) def convert_namespace_to_omegaconf(args: Namespace) -> DictConfig: """Convert a flat argparse.Namespace to a structured DictConfig.""" # Here we are using field values provided in args to override counterparts inside config object overrides, deletes = override_module_args(args) # configs will be in fairseq/config after installation config_path = os.path.join("..", "config") GlobalHydra.instance().clear() with initialize(config_path=config_path): try: composed_cfg = compose("config", overrides=overrides, strict=False) except: logger.error("Error when composing. Overrides: " + str(overrides)) raise for k in deletes: composed_cfg[k] = None cfg = OmegaConf.create( OmegaConf.to_container(composed_cfg, resolve=True, enum_to_str=True) ) # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils with omegaconf_no_object_check(): if cfg.task is None and getattr(args, "task", None): cfg.task = Namespace(**vars(args)) from fairseq.tasks import TASK_REGISTRY _set_legacy_defaults(cfg.task, TASK_REGISTRY[args.task]) cfg.task._name = args.task if cfg.model is None and getattr(args, "arch", None): cfg.model = Namespace(**vars(args)) from fairseq.models import ARCH_MODEL_REGISTRY _set_legacy_defaults(cfg.model, ARCH_MODEL_REGISTRY[args.arch]) cfg.model._name = args.arch if cfg.optimizer is None and getattr(args, "optimizer", None): cfg.optimizer = Namespace(**vars(args)) from fairseq.optim import OPTIMIZER_REGISTRY _set_legacy_defaults(cfg.optimizer, OPTIMIZER_REGISTRY[args.optimizer]) cfg.optimizer._name = args.optimizer if cfg.lr_scheduler is None and getattr(args, "lr_scheduler", None): cfg.lr_scheduler = Namespace(**vars(args)) from fairseq.optim.lr_scheduler import LR_SCHEDULER_REGISTRY _set_legacy_defaults( cfg.lr_scheduler, LR_SCHEDULER_REGISTRY[args.lr_scheduler] ) cfg.lr_scheduler._name = args.lr_scheduler if cfg.criterion is None and getattr(args, "criterion", None): cfg.criterion = Namespace(**vars(args)) from fairseq.criterions import CRITERION_REGISTRY _set_legacy_defaults(cfg.criterion, CRITERION_REGISTRY[args.criterion]) cfg.criterion._name = args.criterion OmegaConf.set_struct(cfg, True) return cfg def cli_main(): parser = options.get_interactive_generation_parser() args = options.parse_args_and_arch(parser) distributed_utils.call_main(convert_namespace_to_omegaconf(args), main)
null
183,580
import re import numpy as np def find_patch_index_combinations(s): def get_box_coords_from_index(P, ul_idx, lr_idx): def decode_bbox_from_caption(caption, quantized_size=32, **kwargs): valid_combinations = find_patch_index_combinations(caption) entity_names = list(map(lambda x: x[0], valid_combinations)) patch_index_coords = list(map(lambda pair: get_box_coords_from_index(quantized_size, pair[1], pair[2]), valid_combinations)) collect_entity_location = [] for entity_name, patch_index_coord in zip(entity_names, patch_index_coords): collect_entity_location.append([entity_name,] + patch_index_coord.tolist()) # print(collect_entity_location) return collect_entity_location
null
183,581
import os import sys from pathlib import Path import textwrap import re import ast import os import numpy as np import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt import matplotlib.pylab as pylab import cv2 import base64 import io from decode_string import decode_bbox_from_caption BOP_SYMBOL="<phrase>" EOP_SYMBOL="</phrase>" BOO_SYMBOL="<object>" EOO_SYMBOL="</object>" DOM_SYMBOL="</delimiter_of_multi_objects/>" SPECIAL_SYMBOLS = [EOD_SYMBOL, BOI_SYMBOL, EOI_SYMBOL, EOC_SYMBOL, EOL_SYMBOL] def add_location_symbols(quantized_size): custom_sp_symbols = [] for symbol in SPECIAL_SYMBOLS: custom_sp_symbols.append(symbol) for symbol in [BOP_SYMBOL, EOP_SYMBOL, BOO_SYMBOL, EOO_SYMBOL, DOM_SYMBOL]: custom_sp_symbols.append(symbol) for i in range(quantized_size ** 2): token_name = f"<patch_index_{str(i).zfill(4)}>" custom_sp_symbols.append(token_name) return custom_sp_symbols
null
183,582
import os import sys from pathlib import Path import textwrap import re import ast import os import numpy as np import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt import matplotlib.pylab as pylab import cv2 import base64 import io from decode_string import decode_bbox_from_caption def imshow(img, file_name = "tmp.jpg", caption='test'): # Create figure and axis objects fig, ax = plt.subplots() # Show image on axis ax.imshow(img[:, :, [2, 1, 0]]) ax.set_axis_off() # Set caption text # Add caption below image # ax.text(0.5, -0.1, caption, ha='center', transform=ax.transAxes) ax.text(0.5, -0.1, '\n'.join(textwrap.wrap(caption, 120)), ha='center', transform=ax.transAxes, fontsize=18) plt.savefig(file_name) plt.close() def draw_entity_box_on_image(image, collect_entity_location): """_summary_ Args: image (_type_): image or image path collect_entity_location (_type_): _description_ """ if isinstance(image, Image.Image): image_h = image.height image_w = image.width image = np.array(image)[:, :, [2, 1, 0]] elif isinstance(image, str): if os.path.exists(image): pil_img = Image.open(image).convert("RGB") image = np.array(pil_img)[:, :, [2, 1, 0]] image_h = pil_img.height image_w = pil_img.width else: raise ValueError(f"invaild image path, {image}") elif isinstance(image, torch.Tensor): # pdb.set_trace() image_tensor = image.cpu() reverse_norm_mean = torch.tensor([0.48145466, 0.4578275, 0.40821073])[:, None, None] reverse_norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None] image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean pil_img = T.ToPILImage()(image_tensor) image_h = pil_img.height image_w = pil_img.width image = np.array(pil_img)[:, :, [2, 1, 0]] else: raise ValueError(f"invaild image format, {type(image)} for {image}") if len(collect_entity_location) == 0: return image new_image = image.copy() previous_locations = [] previous_bboxes = [] text_offset = 10 text_offset_original = 4 text_size = max(0.07 * min(image_h, image_w) / 100, 0.5) text_line = int(max(1 * min(image_h, image_w) / 512, 1)) box_line = int(max(2 * min(image_h, image_w) / 512, 2)) text_height = text_offset # init for (phrase, x1_norm, y1_norm, x2_norm, y2_norm) in collect_entity_location: x1, y1, x2, y2 = int(x1_norm * image_w), int(y1_norm * image_h), int(x2_norm * image_w), int(y2_norm * image_h) # draw bbox # random color color = tuple(np.random.randint(0, 255, size=3).tolist()) new_image = cv2.rectangle(new_image, (x1, y1), (x2, y2), color, box_line) # add phrase name # decide the text location first for x_prev, y_prev in previous_locations: if abs(x1 - x_prev) < abs(text_offset) and abs(y1 - y_prev) < abs(text_offset): y1 += text_height if y1 < 2 * text_offset: y1 += text_offset + text_offset_original # add text background (text_width, text_height), _ = cv2.getTextSize(phrase, cv2.FONT_HERSHEY_SIMPLEX, text_size, text_line) text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2 = x1, y1 - text_height - text_offset_original, x1 + text_width, y1 for prev_bbox in previous_bboxes: while is_overlapping((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2), prev_bbox): text_bg_y1 += text_offset text_bg_y2 += text_offset y1 += text_offset if text_bg_y2 >= image_h: text_bg_y1 = max(0, image_h - text_height - text_offset_original) text_bg_y2 = image_h y1 = max(0, image_h - text_height - text_offset_original + text_offset) break alpha = 0.5 for i in range(text_bg_y1, text_bg_y2): for j in range(text_bg_x1, text_bg_x2): if i < image_h and j < image_w: new_image[i, j] = (alpha * new_image[i, j] + (1 - alpha) * np.array(color)).astype(np.uint8) cv2.putText( new_image, phrase, (x1, y1 - text_offset_original), cv2.FONT_HERSHEY_SIMPLEX, text_size, (0, 0, 0), text_line, cv2.LINE_AA ) previous_locations.append((x1, y1)) previous_bboxes.append((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2)) return new_image def decode_bbox_from_caption(caption, quantized_size=32, **kwargs): valid_combinations = find_patch_index_combinations(caption) entity_names = list(map(lambda x: x[0], valid_combinations)) patch_index_coords = list(map(lambda pair: get_box_coords_from_index(quantized_size, pair[1], pair[2]), valid_combinations)) collect_entity_location = [] for entity_name, patch_index_coord in zip(entity_names, patch_index_coords): collect_entity_location.append([entity_name,] + patch_index_coord.tolist()) return collect_entity_location def visualize_results_on_image(img_path, caption, quantized_size=16, save_path=f"show_box_on_image.jpg", show=True): # collect_entity_location = decode_phrase_with_bbox_from_caption(caption, quantized_size=quantized_size) collect_entity_location = decode_bbox_from_caption(caption, quantized_size=quantized_size) image = draw_entity_box_on_image(img_path, collect_entity_location) if show: imshow(image, file_name=save_path, caption=caption) else: # return a PIL Image image = image[:, :, [2, 1, 0]] pil_image = Image.fromarray(image) return pil_image
null
183,590
import math import warnings import torch import torch.distributed as dist from fairseq.utils import multi_tensor_l2norm_available, multi_tensor_total_norm def multi_tensor_total_norm(grads, chunk_size=2048 * 32) -> torch.Tensor: per_device_grads = {} norms = [] for grad in grads: device = grad.device cur_device_grads = per_device_grads.get(device) if cur_device_grads is None: cur_device_grads = [] per_device_grads[device] = cur_device_grads cur_device_grads.append(grad) for device in per_device_grads.keys(): cur_device_grads = per_device_grads[device] if device.type == "cuda": # TODO(msb) return has_inf has_inf = torch.zeros((1, 1), dtype=torch.int, device=device) with torch.cuda.device(device): norm = multi_tensor_l2norm( chunk_size, has_inf, [cur_device_grads], False ) norms.append(norm[0].to(torch.cuda.current_device())) else: norms += [torch.norm(g, p=2, dtype=torch.float32) for g in cur_device_grads] total_norm = torch.norm(torch.stack(norms)) return total_norm def clip_grad_norm_( params, max_norm, moe_expert_count, aggregate_norm_fn=None ) -> torch.Tensor: def grad_exists(p): return p is not None and getattr(p, "grad", None) is not None if isinstance(params, torch.Tensor): params = [params] params = list(params) params = list(filter(grad_exists, params)) grads, expert_grads, base_expert_grads, sharded_grads = [], [], [], [] denom = math.sqrt(max(dist.get_global_world_size(), moe_expert_count)) for p in params: if hasattr(p, "expert"): expert_grads.append(p.grad.detach() / denom) elif hasattr(p, "base_expert"): base_expert_grads.append(p.grad.detach()) elif hasattr(p, "_is_sharded"): sharded_grads.append(p.grad.detach()) else: grads.append(p.grad.detach()) if len(grads) == 0: if len(params) > 0: total_norm = params[0].new_tensor(0.0) else: total_norm = torch.tensor(0.0) elif len(grads) == 1: total_norm = torch.norm(grads[0], p=2, dtype=torch.float32) else: if multi_tensor_l2norm_available: total_norm = multi_tensor_total_norm(grads) else: if torch.cuda.is_available(): warnings.warn( "amp_C fused kernels unavailable, disabling multi_tensor_l2norm; " "you may get better performance by installing NVIDIA's apex library" ) device = torch.cuda.current_device() elif grads[0].device.type == "xla": device = grads[0].device else: device = torch.device("cpu") total_norm = torch.norm( torch.stack( [torch.norm(g, p=2, dtype=torch.float32).to(device) for g in grads] ) ) # calculate split_norm and all_reduce with other workers norms = [total_norm] for split_grads in [expert_grads, sharded_grads]: if len(split_grads) == 0: continue split_norm = torch.norm( torch.stack([torch.norm(g, p=2, dtype=torch.float32) for g in split_grads]) ) if dist.is_initialized(): split_norm.pow_(2) dist.all_reduce(split_norm) split_norm.sqrt_() norms.append(split_norm) if len(norms) > 1: total_norm = torch.norm(torch.stack(norms)) if aggregate_norm_fn is not None: total_norm = aggregate_norm_fn(total_norm) if max_norm > 0: max_norm = float(max_norm) clip_coef = (max_norm / (total_norm + 1e-6)).clamp_(max=1) for g in grads + expert_grads + sharded_grads + base_expert_grads: g.mul_(clip_coef) return total_norm
null
183,593
import math import torch import torch.nn.functional as F from apex.normalization import FusedLayerNorm as LayerNorm from torch import nn from .multiway_network import MultiwayWrapper from xformers.ops import memory_efficient_attention, LowerTriangularMask, MemoryEfficientAttentionCutlassOp def rotate_every_two(x): x1 = x[:, :, ::2] x2 = x[:, :, 1::2] x = torch.stack((-x2, x1), dim=-1) return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)')\ def duplicate_interleave(m): """ A simple version of `torch.repeat_interleave` for duplicating a matrix while interleaving the copy. """ dim0 = m.shape[0] m = m.view(-1, 1) # flatten the matrix m = m.repeat(1, 2) # repeat all elements into the 2nd dimension m = m.view(dim0, -1) # reshape into a matrix, interleaving the copy return m def apply_rotary_pos_emb(x, sin, cos, scale=1): sin, cos = map(lambda t: duplicate_interleave(t * scale), (sin, cos)) # einsum notation for lambda t: repeat(t[offset:x.shape[1]+offset,:], "n d -> () n () (d j)", j=2) return (x * cos) + (rotate_every_two(x) * sin)
null
183,595
import logging import time from typing import Any, Tuple, cast import torch import torch.distributed as dist from torch import Tensor from torch.nn import Module, ModuleList def _find_my_group_index(grouped_ranks): my_rank = dist.get_rank() for i, group in enumerate(grouped_ranks): if my_rank in group: return i raise RuntimeError def get_all2all_group(moe_expert_count): if dist.is_initialized(): if not hasattr(get_all2all_group, "_all2all_groups"): world_size = dist.get_world_size() # more experts than world size if world_size <= moe_expert_count: assert moe_expert_count % world_size == 0 all2all_groups = [[i for i in range(world_size)]] # larger world than num experts else: assert world_size % moe_expert_count == 0 ranks_per_group = world_size // moe_expert_count all2all_groups = [ [i * moe_expert_count + j for j in range(moe_expert_count)] for i in range(ranks_per_group) ] get_all2all_group._all2all_group_idx = all2all_groups get_all2all_group._all2all_groups = [ dist.new_group(g) for g in all2all_groups ] my_group_idx = _find_my_group_index(get_all2all_group._all2all_group_idx) return get_all2all_group._all2all_groups[my_group_idx]
null
183,602
import os from typing import Any, Dict, List, Optional, Tuple import torch import torch.nn as nn from torch import Tensor from fairseq import checkpoint_utils from fairseq.models import register_model, register_model_architecture from fairseq.models.transformer import ( TransformerModel, TransformerDecoderBase, TransformerEncoderBase, ) from fairseq.models.transformer.transformer_config import ( TransformerConfig, DEFAULT_MAX_SOURCE_POSITIONS, DEFAULT_MAX_TARGET_POSITIONS, DEFAULT_MIN_PARAMS_TO_WRAP, ) from fairseq.modules.transformer_layer import ( TransformerDecoderLayerBase ) from fairseq.modules.multihead_attention import MultiheadAttention from fairseq.modules import LayerNorm from fairseq.modules.fairseq_dropout import FairseqDropout from fairseq.modules.quant_noise import quant_noise from fairseq import utils from fairseq.file_io import PathManager import logging def upgrade_state_dict_for_deltalm( state_dict: Dict[str, Any], pretrained_deltalm_checkpoint: str, is_encoder=True, ) -> Dict[str, Any]: if not os.path.exists(pretrained_deltalm_checkpoint): raise IOError("Model file not found: {}".format(pretrained_deltalm_checkpoint)) with open(pretrained_deltalm_checkpoint, "rb") as f: state = torch.load(f, map_location=torch.device("cpu")) deltalm_state_dict = state["weights"] new_deltalm_state_dict = {} for key in deltalm_state_dict.keys(): if is_encoder: if key.startswith('encoder.') or key.startswith('src_embedding.'): new_key = key.replace('encoder.', '') new_key = new_key.replace('src_embedding.', '') new_deltalm_state_dict[new_key] = deltalm_state_dict[key] else: if key.startswith('decoder.') or key.startswith('tgt_embedding.'): new_key = key.replace('decoder.', '') new_key = new_key.replace('tgt_embedding.', '') new_deltalm_state_dict[new_key] = deltalm_state_dict[key] deltalm_state_dict = new_deltalm_state_dict for key in deltalm_state_dict.keys(): map_key = key map_key = map_key.replace('.ffn_1.fc1', '.fc3') map_key = map_key.replace('.ffn_1.fc2', '.fc4') map_key = map_key.replace('.ffn_2', '') map_key = map_key.replace('.ffn.', '.') map_key = map_key.replace('emb_layer_norm', 'layernorm_embedding') assert map_key in state_dict, map_key if 'embed_positions' in key or 'embed_tokens' in key: left_size = state_dict[map_key].size(0) right_size = deltalm_state_dict[key].size(0) if left_size <= right_size: state_dict[map_key] = deltalm_state_dict[key][:left_size] else: state_dict[map_key][:right_size] = deltalm_state_dict[key] else: state_dict[map_key] = deltalm_state_dict[key] return state_dict
null
183,603
import os from typing import Any, Dict, List, Optional, Tuple import torch import torch.nn as nn from torch import Tensor from fairseq import checkpoint_utils from fairseq.models import register_model, register_model_architecture from fairseq.models.transformer import ( TransformerModel, TransformerDecoderBase, TransformerEncoderBase, ) from fairseq.models.transformer.transformer_config import ( TransformerConfig, DEFAULT_MAX_SOURCE_POSITIONS, DEFAULT_MAX_TARGET_POSITIONS, DEFAULT_MIN_PARAMS_TO_WRAP, ) from fairseq.modules.transformer_layer import ( TransformerDecoderLayerBase ) from fairseq.modules.multihead_attention import MultiheadAttention from fairseq.modules import LayerNorm from fairseq.modules.fairseq_dropout import FairseqDropout from fairseq.modules.quant_noise import quant_noise from fairseq import utils from fairseq.file_io import PathManager import logging def base_architecture(args): def large_architecture(args): base_architecture(args) args.encoder_embed_dim = 1024 args.encoder_ffn_embed_dim = 4096 args.encoder_layers = 24 args.encoder_attention_heads = 16 args.encoder_normalize_before = False args.decoder_embed_dim = 1024 args.decoder_ffn_embed_dim = 4096 args.decoder_layers = 12 args.decoder_attention_heads = 16 args.decoder_normalize_before = False args.layernorm_embedding = False
null
183,604
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import numpy as np from datasets import ClassLabel, load_dataset, load_metric import transformers from layoutlmft.data import DataCollatorForKeyValueExtraction from layoutlmft.data.xfund import xfund_dataset, XFund_label2ids from transformers import ( AutoConfig, AutoModelForTokenClassification, AutoTokenizer, HfArgumentParser, PreTrainedTokenizerFast, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process from transformers.utils import check_min_version def main(): # See all possible arguments in layoutlmft/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) if len(sys.argv) == 2 and sys.argv[1].endswith(".json"): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) else: model_args, data_args, training_args = parser.parse_args_into_dataclasses() # Detecting last checkpoint. last_checkpoint = None if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir: last_checkpoint = get_last_checkpoint(training_args.output_dir) if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0: raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. " "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None: logger.info( f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", handlers=[logging.StreamHandler(sys.stdout)], ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN) # Log on each process the small summary: logger.warning( f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info(f"Training/evaluation parameters {training_args}") # Set seed before initializing model. set_seed(training_args.seed) config = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path, num_labels=7, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, revision=model_args.model_revision, input_size=data_args.input_size, use_auth_token=True if model_args.use_auth_token else None, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, tokenizer_file=None, # avoid loading from a cached file of the pre-trained model in another machine cache_dir=model_args.cache_dir, use_fast=True, add_prefix_space=True, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) train_dataset, eval_dataset, test_dataset = None, None, None if training_args.do_train: train_dataset = xfund_dataset(data_args, tokenizer, 'train') if training_args.do_eval: eval_dataset = xfund_dataset(data_args, tokenizer, 'eval') model = AutoModelForTokenClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool(".ckpt" in model_args.model_name_or_path), config=config, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Tokenizer check: this script requires a fast tokenizer. if not isinstance(tokenizer, PreTrainedTokenizerFast): raise ValueError( "This example script only works for models that have a fast tokenizer. Checkout the big table of models " "at https://huggingface.co/transformers/index.html#bigtable to find the model types that meet this " "requirement" ) # Preprocessing the dataset # Padding strategy padding = "max_length" if data_args.pad_to_max_length else False # Data collator data_collator = DataCollatorForKeyValueExtraction( tokenizer, pad_to_multiple_of=8 if training_args.fp16 else None, padding=padding, max_length=512, ) def get_label_list(): label_list = [[key, val] for key, val in XFund_label2ids.items()] label_list = sorted(label_list, key=lambda x:x[1], reverse=False) label_list = [label for label, id in label_list] return label_list label_list = get_label_list() # Metrics metric = load_metric("seqeval") def compute_metrics(p): predictions, labels = p predictions = np.argmax(predictions, axis=2) # Remove ignored index (special tokens) true_predictions = [ [label_list[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] true_labels = [ [label_list[l] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] results = metric.compute(predictions=true_predictions, references=true_labels) if data_args.return_entity_level_metrics: # Unpack nested dictionaries final_results = {} for key, value in results.items(): if isinstance(value, dict): for n, v in value.items(): final_results[f"{key}_{n}"] = v else: final_results[key] = value return final_results else: return { "precision": results["overall_precision"], "recall": results["overall_recall"], "f1": results["overall_f1"], "accuracy": results["overall_accuracy"], } # Initialize our Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset if training_args.do_train else None, eval_dataset=eval_dataset if training_args.do_eval else None, tokenizer=tokenizer, data_collator=data_collator, compute_metrics=compute_metrics, ) # Training if training_args.do_train: checkpoint = last_checkpoint if last_checkpoint else None train_result = trainer.train(resume_from_checkpoint=checkpoint) metrics = train_result.metrics trainer.save_model() # Saves the tokenizer too for easy upload max_train_samples = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset) ) metrics["train_samples"] = min(max_train_samples, len(train_dataset)) trainer.log_metrics("train", metrics) trainer.save_metrics("train", metrics) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***") metrics = trainer.evaluate() max_val_samples = data_args.max_val_samples if data_args.max_val_samples is not None else len(eval_dataset) metrics["eval_samples"] = min(max_val_samples, len(eval_dataset)) trainer.log_metrics("eval", metrics) trainer.save_metrics("eval", metrics) # Predict if training_args.do_predict: logger.info("*** Predict ***") predictions, labels, metrics = trainer.predict(test_dataset) predictions = np.argmax(predictions, axis=2) # Remove ignored index (special tokens) true_predictions = [ [label_list[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] trainer.log_metrics("test", metrics) trainer.save_metrics("test", metrics) # Save predictions output_test_predictions_file = os.path.join(training_args.output_dir, "test_predictions.txt") if trainer.is_world_process_zero(): with open(output_test_predictions_file, "w") as writer: for prediction in true_predictions: writer.write(" ".join(prediction) + "\n") def _mp_fn(index): # For xla_spawn (TPUs) main()
null
183,611
import torch from detectron2.layers import ( ShapeSpec, ) from detectron2.modeling import Backbone, BACKBONE_REGISTRY, FPN from detectron2.modeling.backbone.fpn import LastLevelP6P7, LastLevelMaxPool from .beit import beit_base_patch16, dit_base_patch16, dit_large_patch16, beit_large_patch16 from .deit import deit_base_patch16, mae_base_patch16 from layoutlmft.models.layoutlmv3 import LayoutLMv3Model from transformers import AutoConfig def build_VIT_backbone(cfg): """ Create a VIT instance from config. Args: cfg: a detectron2 CfgNode Returns: A VIT backbone instance. """ # fmt: off name = cfg.MODEL.VIT.NAME out_features = cfg.MODEL.VIT.OUT_FEATURES drop_path = cfg.MODEL.VIT.DROP_PATH img_size = cfg.MODEL.VIT.IMG_SIZE pos_type = cfg.MODEL.VIT.POS_TYPE model_kwargs = eval(str(cfg.MODEL.VIT.MODEL_KWARGS).replace("`", "")) if 'layoutlmv3' in name: if cfg.MODEL.CONFIG_PATH != '': config_path = cfg.MODEL.CONFIG_PATH else: config_path = cfg.MODEL.WEIGHTS.replace('pytorch_model.bin', '') # layoutlmv3 pre-trained models config_path = config_path.replace('model_final.pth', '') # detection fine-tuned models else: config_path = None return VIT_Backbone(name, out_features, drop_path, img_size, pos_type, model_kwargs, config_path=config_path, image_only=cfg.MODEL.IMAGE_ONLY, cfg=cfg) The provided code snippet includes necessary dependencies for implementing the `build_vit_fpn_backbone` function. Write a Python function `def build_vit_fpn_backbone(cfg, input_shape: ShapeSpec)` to solve the following problem: Create a VIT w/ FPN backbone. Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. Here is the function: def build_vit_fpn_backbone(cfg, input_shape: ShapeSpec): """ Create a VIT w/ FPN backbone. Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_VIT_backbone(cfg) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelMaxPool(), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
Create a VIT w/ FPN backbone. Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`.
183,612
import copy import logging import numpy as np import torch from detectron2.data import detection_utils as utils from detectron2.data import transforms as T from layoutlmft import LayoutLMv3Tokenizer The provided code snippet includes necessary dependencies for implementing the `build_transform_gen` function. Write a Python function `def build_transform_gen(cfg, is_train, aug_flip_crop=True)` to solve the following problem: Create a list of :class:`TransformGen` from config. Returns: list[TransformGen] Here is the function: def build_transform_gen(cfg, is_train, aug_flip_crop=True): """ Create a list of :class:`TransformGen` from config. Returns: list[TransformGen] """ if is_train: min_size = cfg.INPUT.MIN_SIZE_TRAIN max_size = cfg.INPUT.MAX_SIZE_TRAIN sample_style = cfg.INPUT.MIN_SIZE_TRAIN_SAMPLING else: min_size = cfg.INPUT.MIN_SIZE_TEST max_size = cfg.INPUT.MAX_SIZE_TEST sample_style = "choice" if sample_style == "range": assert len(min_size) == 2, "more than 2 ({}) min_size(s) are provided for ranges".format(len(min_size)) logger = logging.getLogger(__name__) tfm_gens = [] if is_train and aug_flip_crop: tfm_gens.append(T.RandomFlip()) tfm_gens.append(T.ResizeShortestEdge(min_size, max_size, sample_style)) if is_train: logger.info("TransformGens used in training: " + str(tfm_gens)) return tfm_gens
Create a list of :class:`TransformGen` from config. Returns: list[TransformGen]
183,613
from detectron2.config import CfgNode as CN The provided code snippet includes necessary dependencies for implementing the `add_vit_config` function. Write a Python function `def add_vit_config(cfg)` to solve the following problem: Add config for VIT. Here is the function: def add_vit_config(cfg): """ Add config for VIT. """ _C = cfg _C.MODEL.VIT = CN() # CoaT model name. _C.MODEL.VIT.NAME = "" # Output features from CoaT backbone. _C.MODEL.VIT.OUT_FEATURES = ["layer3", "layer5", "layer7", "layer11"] _C.MODEL.VIT.IMG_SIZE = [224, 224] _C.MODEL.VIT.POS_TYPE = "shared_rel" _C.MODEL.VIT.DROP_PATH = 0. _C.MODEL.VIT.MODEL_KWARGS = "{}" _C.SOLVER.OPTIMIZER = "ADAMW" _C.SOLVER.BACKBONE_MULTIPLIER = 1.0 _C.AUG = CN() _C.AUG.DETR = False _C.MODEL.IMAGE_ONLY = True _C.PUBLAYNET_DATA_DIR_TRAIN = "" _C.PUBLAYNET_DATA_DIR_TEST = "" _C.ICDAR_DATA_DIR_TRAIN = "" _C.ICDAR_DATA_DIR_TEST = "" _C.CACHE_DIR = "" _C.MODEL.CONFIG_PATH = "" # effective update steps would be MAX_ITER/GRADIENT_ACCUMULATION_STEPS # maybe need to set MAX_ITER *= GRADIENT_ACCUMULATION_STEPS _C.SOLVER.GRADIENT_ACCUMULATION_STEPS = 1
Add config for VIT.
183,614
import argparse import logging import os import sys import time import weakref from collections import OrderedDict from typing import Optional import torch from fvcore.nn.precise_bn import get_bn_modules from omegaconf import OmegaConf from torch.nn.parallel import DistributedDataParallel import detectron2.data.transforms as T from detectron2.checkpoint import DetectionCheckpointer from detectron2.config import CfgNode, LazyConfig from detectron2.data import ( MetadataCatalog, build_detection_test_loader, build_detection_train_loader, ) from detectron2.evaluation import ( DatasetEvaluator, inference_on_dataset, print_csv_format, verify_results, ) from detectron2.modeling import build_model from detectron2.solver import build_lr_scheduler, build_optimizer from detectron2.utils import comm from detectron2.utils.collect_env import collect_env_info from detectron2.utils.env import seed_all_rng from detectron2.utils.events import CommonMetricPrinter, JSONWriter, TensorboardXWriter from detectron2.utils.file_io import PathManager from detectron2.utils.logger import setup_logger from detectron2.engine import hooks from detectron2.engine.train_loop import AMPTrainer, SimpleTrainer, TrainerBase from .mycheckpointer import MyDetectionCheckpointer from typing import Any, Dict, List, Set import itertools from detectron2.solver.build import maybe_add_gradient_clipping from .dataset_mapper import DetrDatasetMapper from .icdar_evaluation import ICDAREvaluator from detectron2.evaluation import COCOEvaluator The provided code snippet includes necessary dependencies for implementing the `create_ddp_model` function. Write a Python function `def create_ddp_model(model, *, fp16_compression=False, **kwargs)` to solve the following problem: Create a DistributedDataParallel model if there are >1 processes. Args: model: a torch.nn.Module fp16_compression: add fp16 compression hooks to the ddp object. See more at https://pytorch.org/docs/stable/ddp_comm_hooks.html#torch.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook kwargs: other arguments of :module:`torch.nn.parallel.DistributedDataParallel`. Here is the function: def create_ddp_model(model, *, fp16_compression=False, **kwargs): """ Create a DistributedDataParallel model if there are >1 processes. Args: model: a torch.nn.Module fp16_compression: add fp16 compression hooks to the ddp object. See more at https://pytorch.org/docs/stable/ddp_comm_hooks.html#torch.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook kwargs: other arguments of :module:`torch.nn.parallel.DistributedDataParallel`. """ # noqa if comm.get_world_size() == 1: return model if "device_ids" not in kwargs: kwargs["device_ids"] = [comm.get_local_rank()] ddp = DistributedDataParallel(model, **kwargs) if fp16_compression: from torch.distributed.algorithms.ddp_comm_hooks import default as comm_hooks ddp.register_comm_hook(state=None, hook=comm_hooks.fp16_compress_hook) return ddp
Create a DistributedDataParallel model if there are >1 processes. Args: model: a torch.nn.Module fp16_compression: add fp16 compression hooks to the ddp object. See more at https://pytorch.org/docs/stable/ddp_comm_hooks.html#torch.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook kwargs: other arguments of :module:`torch.nn.parallel.DistributedDataParallel`.
183,615
import argparse import logging import os import sys import time import weakref from collections import OrderedDict from typing import Optional import torch from fvcore.nn.precise_bn import get_bn_modules from omegaconf import OmegaConf from torch.nn.parallel import DistributedDataParallel import detectron2.data.transforms as T from detectron2.checkpoint import DetectionCheckpointer from detectron2.config import CfgNode, LazyConfig from detectron2.data import ( MetadataCatalog, build_detection_test_loader, build_detection_train_loader, ) from detectron2.evaluation import ( DatasetEvaluator, inference_on_dataset, print_csv_format, verify_results, ) from detectron2.modeling import build_model from detectron2.solver import build_lr_scheduler, build_optimizer from detectron2.utils import comm from detectron2.utils.collect_env import collect_env_info from detectron2.utils.env import seed_all_rng from detectron2.utils.events import CommonMetricPrinter, JSONWriter, TensorboardXWriter from detectron2.utils.file_io import PathManager from detectron2.utils.logger import setup_logger from detectron2.engine import hooks from detectron2.engine.train_loop import AMPTrainer, SimpleTrainer, TrainerBase from .mycheckpointer import MyDetectionCheckpointer from typing import Any, Dict, List, Set import itertools from detectron2.solver.build import maybe_add_gradient_clipping from .dataset_mapper import DetrDatasetMapper from .icdar_evaluation import ICDAREvaluator from detectron2.evaluation import COCOEvaluator The provided code snippet includes necessary dependencies for implementing the `default_argument_parser` function. Write a Python function `def default_argument_parser(epilog=None)` to solve the following problem: Create a parser with some common arguments used by detectron2 users. Args: epilog (str): epilog passed to ArgumentParser describing the usage. Returns: argparse.ArgumentParser: Here is the function: def default_argument_parser(epilog=None): """ Create a parser with some common arguments used by detectron2 users. Args: epilog (str): epilog passed to ArgumentParser describing the usage. Returns: argparse.ArgumentParser: """ parser = argparse.ArgumentParser( epilog=epilog or f""" Examples: Run on single machine: $ {sys.argv[0]} --num-gpus 8 --config-file cfg.yaml Change some config options: $ {sys.argv[0]} --config-file cfg.yaml MODEL.WEIGHTS /path/to/weight.pth SOLVER.BASE_LR 0.001 Run on multiple machines: (machine0)$ {sys.argv[0]} --machine-rank 0 --num-machines 2 --dist-url <URL> [--other-flags] (machine1)$ {sys.argv[0]} --machine-rank 1 --num-machines 2 --dist-url <URL> [--other-flags] """, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file") parser.add_argument( "--resume", action="store_true", help="Whether to attempt to resume from the checkpoint directory. " "See documentation of `MyTrainer.resume_or_load()` for what it means.", ) parser.add_argument("--eval-only", action="store_true", help="perform evaluation only") parser.add_argument("--num-gpus", type=int, default=1, help="number of gpus *per machine*") parser.add_argument("--num-machines", type=int, default=1, help="total number of machines") parser.add_argument( "--machine-rank", type=int, default=0, help="the rank of this machine (unique per machine)" ) # PyTorch still may leave orphan processes in multi-gpu training. # Therefore we use a deterministic way to obtain port, # so that users are aware of orphan processes by seeing the port occupied. port = 2 ** 15 + 2 ** 14 + hash(os.getuid() if sys.platform != "win32" else 1) % 2 ** 14 parser.add_argument( "--dist-url", default="tcp://127.0.0.1:{}".format(port), help="initialization URL for pytorch distributed backend. See " "https://pytorch.org/docs/stable/distributed.html for details.", ) parser.add_argument( "opts", help=""" Modify config options at the end of the command. For Yacs configs, use space-separated "PATH.KEY VALUE" pairs. For python-based LazyConfig, use "path.key=value". """.strip(), default=None, nargs=argparse.REMAINDER, ) return parser
Create a parser with some common arguments used by detectron2 users. Args: epilog (str): epilog passed to ArgumentParser describing the usage. Returns: argparse.ArgumentParser:
183,616
import argparse import logging import os import sys import time import weakref from collections import OrderedDict from typing import Optional import torch from fvcore.nn.precise_bn import get_bn_modules from omegaconf import OmegaConf from torch.nn.parallel import DistributedDataParallel import detectron2.data.transforms as T from detectron2.checkpoint import DetectionCheckpointer from detectron2.config import CfgNode, LazyConfig from detectron2.data import ( MetadataCatalog, build_detection_test_loader, build_detection_train_loader, ) from detectron2.evaluation import ( DatasetEvaluator, inference_on_dataset, print_csv_format, verify_results, ) from detectron2.modeling import build_model from detectron2.solver import build_lr_scheduler, build_optimizer from detectron2.utils import comm from detectron2.utils.collect_env import collect_env_info from detectron2.utils.env import seed_all_rng from detectron2.utils.events import CommonMetricPrinter, JSONWriter, TensorboardXWriter from detectron2.utils.file_io import PathManager from detectron2.utils.logger import setup_logger from detectron2.engine import hooks from detectron2.engine.train_loop import AMPTrainer, SimpleTrainer, TrainerBase from .mycheckpointer import MyDetectionCheckpointer from typing import Any, Dict, List, Set import itertools from detectron2.solver.build import maybe_add_gradient_clipping from .dataset_mapper import DetrDatasetMapper from .icdar_evaluation import ICDAREvaluator from detectron2.evaluation import COCOEvaluator def _try_get_key(cfg, *keys, default=None): """ Try select keys from cfg until the first key that exists. Otherwise return default. """ if isinstance(cfg, CfgNode): cfg = OmegaConf.create(cfg.dump()) for k in keys: none = object() p = OmegaConf.select(cfg, k, default=none) if p is not none: return p return default def _highlight(code, filename): try: import pygments except ImportError: return code from pygments.lexers import Python3Lexer, YamlLexer from pygments.formatters import Terminal256Formatter lexer = Python3Lexer() if filename.endswith(".py") else YamlLexer() code = pygments.highlight(code, lexer, Terminal256Formatter(style="monokai")) return code The provided code snippet includes necessary dependencies for implementing the `default_setup` function. Write a Python function `def default_setup(cfg, args)` to solve the following problem: Perform some basic common setups at the beginning of a job, including: 1. Set up the detectron2 logger 2. Log basic information about environment, cmdline arguments, and config 3. Backup the config to the output directory Args: cfg (CfgNode or omegaconf.DictConfig): the full config to be used args (argparse.NameSpace): the command line arguments to be logged Here is the function: def default_setup(cfg, args): """ Perform some basic common setups at the beginning of a job, including: 1. Set up the detectron2 logger 2. Log basic information about environment, cmdline arguments, and config 3. Backup the config to the output directory Args: cfg (CfgNode or omegaconf.DictConfig): the full config to be used args (argparse.NameSpace): the command line arguments to be logged """ output_dir = _try_get_key(cfg, "OUTPUT_DIR", "output_dir", "train.output_dir") if comm.is_main_process() and output_dir: PathManager.mkdirs(output_dir) rank = comm.get_rank() setup_logger(output_dir, distributed_rank=rank, name="fvcore") logger = setup_logger(output_dir, distributed_rank=rank) logger.info("Rank of current process: {}. World size: {}".format(rank, comm.get_world_size())) logger.info("Environment info:\n" + collect_env_info()) logger.info("Command line arguments: " + str(args)) if hasattr(args, "config_file") and args.config_file != "": logger.info( "Contents of args.config_file={}:\n{}".format( args.config_file, _highlight(PathManager.open(args.config_file, "r").read(), args.config_file), ) ) if comm.is_main_process() and output_dir: # Note: some of our scripts may expect the existence of # config.yaml in output directory path = os.path.join(output_dir, "config.yaml") if isinstance(cfg, CfgNode): logger.info("Running with full config:\n{}".format(_highlight(cfg.dump(), ".yaml"))) with PathManager.open(path, "w") as f: f.write(cfg.dump()) else: LazyConfig.save(cfg, path) logger.info("Full config saved to {}".format(path)) # make sure each worker has a different, yet deterministic seed if specified seed = _try_get_key(cfg, "SEED", "train.seed", default=-1) seed_all_rng(None if seed < 0 else seed + rank) # cudnn benchmark has large overhead. It shouldn't be used considering the small size of # typical validation set. if not (hasattr(args, "eval_only") and args.eval_only): torch.backends.cudnn.benchmark = _try_get_key( cfg, "CUDNN_BENCHMARK", "train.cudnn_benchmark", default=False )
Perform some basic common setups at the beginning of a job, including: 1. Set up the detectron2 logger 2. Log basic information about environment, cmdline arguments, and config 3. Backup the config to the output directory Args: cfg (CfgNode or omegaconf.DictConfig): the full config to be used args (argparse.NameSpace): the command line arguments to be logged
183,617
import argparse import logging import os import sys import time import weakref from collections import OrderedDict from typing import Optional import torch from fvcore.nn.precise_bn import get_bn_modules from omegaconf import OmegaConf from torch.nn.parallel import DistributedDataParallel import detectron2.data.transforms as T from detectron2.checkpoint import DetectionCheckpointer from detectron2.config import CfgNode, LazyConfig from detectron2.data import ( MetadataCatalog, build_detection_test_loader, build_detection_train_loader, ) from detectron2.evaluation import ( DatasetEvaluator, inference_on_dataset, print_csv_format, verify_results, ) from detectron2.modeling import build_model from detectron2.solver import build_lr_scheduler, build_optimizer from detectron2.utils import comm from detectron2.utils.collect_env import collect_env_info from detectron2.utils.env import seed_all_rng from detectron2.utils.events import CommonMetricPrinter, JSONWriter, TensorboardXWriter from detectron2.utils.file_io import PathManager from detectron2.utils.logger import setup_logger from detectron2.engine import hooks from detectron2.engine.train_loop import AMPTrainer, SimpleTrainer, TrainerBase from .mycheckpointer import MyDetectionCheckpointer from typing import Any, Dict, List, Set import itertools from detectron2.solver.build import maybe_add_gradient_clipping from .dataset_mapper import DetrDatasetMapper from .icdar_evaluation import ICDAREvaluator from detectron2.evaluation import COCOEvaluator The provided code snippet includes necessary dependencies for implementing the `default_writers` function. Write a Python function `def default_writers(output_dir: str, max_iter: Optional[int] = None)` to solve the following problem: Build a list of :class:`EventWriter` to be used. It now consists of a :class:`CommonMetricPrinter`, :class:`TensorboardXWriter` and :class:`JSONWriter`. Args: output_dir: directory to store JSON metrics and tensorboard events max_iter: the total number of iterations Returns: list[EventWriter]: a list of :class:`EventWriter` objects. Here is the function: def default_writers(output_dir: str, max_iter: Optional[int] = None): """ Build a list of :class:`EventWriter` to be used. It now consists of a :class:`CommonMetricPrinter`, :class:`TensorboardXWriter` and :class:`JSONWriter`. Args: output_dir: directory to store JSON metrics and tensorboard events max_iter: the total number of iterations Returns: list[EventWriter]: a list of :class:`EventWriter` objects. """ PathManager.mkdirs(output_dir) return [ # It may not always print what you want to see, since it prints "common" metrics only. CommonMetricPrinter(max_iter), JSONWriter(os.path.join(output_dir, "metrics.json")), TensorboardXWriter(output_dir), ]
Build a list of :class:`EventWriter` to be used. It now consists of a :class:`CommonMetricPrinter`, :class:`TensorboardXWriter` and :class:`JSONWriter`. Args: output_dir: directory to store JSON metrics and tensorboard events max_iter: the total number of iterations Returns: list[EventWriter]: a list of :class:`EventWriter` objects.
183,618
from detectron2.checkpoint import DetectionCheckpointer from typing import Any import torch import torch.nn as nn from fvcore.common.checkpoint import _IncompatibleKeys, _strip_prefix_if_present, TORCH_VERSION, quantization, \ ObserverBase, FakeQuantizeBase from torch import distributed as dist from scipy import interpolate import numpy as np import torch.nn.functional as F from collections import OrderedDict def append_prefix(k): prefix = 'backbone.bottom_up.backbone.' return prefix + k if not k.startswith(prefix) else k def modify_ckpt_state(model, state_dict, logger=None): # reshape absolute position embedding for Swin if state_dict.get(append_prefix('absolute_pos_embed')) is not None: absolute_pos_embed = state_dict[append_prefix('absolute_pos_embed')] N1, L, C1 = absolute_pos_embed.size() N2, C2, H, W = model.backbone.bottom_up.backbone.absolute_pos_embed.size() if N1 != N2 or C1 != C2 or L != H * W: logger.warning("Error in loading absolute_pos_embed, pass") else: state_dict[append_prefix('absolute_pos_embed')] = absolute_pos_embed.view(N2, H, W, C2).permute(0, 3, 1, 2) def get_dist_info(): if dist.is_available() and dist.is_initialized(): rank = dist.get_rank() world_size = dist.get_world_size() else: rank = 0 world_size = 1 return rank, world_size def resize_position_embeddings(max_position_embeddings, old_vocab_size, _k='backbone.bottom_up.backbone.embeddings.position_embeddings.weight', initializer_range=0.02, reuse_position_embedding=True): ''' Reference: unilm ALso see discussions: https://github.com/pytorch/fairseq/issues/1685 https://github.com/google-research/bert/issues/27 ''' new_position_embedding = state_dict[_k].data.new_tensor(torch.ones( size=(max_position_embeddings, state_dict[_k].shape[1])), dtype=torch.float) new_position_embedding = nn.Parameter(data=new_position_embedding, requires_grad=True) new_position_embedding.data.normal_(mean=0.0, std=initializer_range) if max_position_embeddings > old_vocab_size: logger.info("Resize > position embeddings !") max_range = max_position_embeddings if reuse_position_embedding else old_vocab_size shift = 0 while shift < max_range: delta = min(old_vocab_size, max_range - shift) new_position_embedding.data[shift: shift + delta, :] = state_dict[_k][:delta, :] logger.info(" CP [%d ~ %d] into [%d ~ %d] " % (0, delta, shift, shift + delta)) shift += delta state_dict[_k] = new_position_embedding.data del new_position_embedding elif max_position_embeddings < old_vocab_size: logger.info("Resize < position embeddings !") new_position_embedding.data.copy_(state_dict[_k][:max_position_embeddings, :]) state_dict[_k] = new_position_embedding.data del new_position_embedding rank, _ = get_dist_info() all_keys = list(state_dict.keys()) for key in all_keys: if "embeddings.position_embeddings.weight" in key: if key not in model.state_dict(): # image only models do not use this key continue max_position_embeddings = model.state_dict()[key].shape[0] old_vocab_size = state_dict[key].shape[0] if max_position_embeddings != old_vocab_size: resize_position_embeddings(max_position_embeddings, old_vocab_size,_k=key) if "relative_position_index" in key: state_dict.pop(key) if "relative_position_bias_table" in key: rel_pos_bias = state_dict[key] src_num_pos, num_attn_heads = rel_pos_bias.size() if key not in model.state_dict(): continue dst_num_pos, _ = model.state_dict()[key].size() dst_patch_shape = model.backbone.bottom_up.backbone.patch_embed.patch_shape if dst_patch_shape[0] != dst_patch_shape[1]: raise NotImplementedError() num_extra_tokens = dst_num_pos - (dst_patch_shape[0] * 2 - 1) * (dst_patch_shape[1] * 2 - 1) src_size = int((src_num_pos - num_extra_tokens) ** 0.5) dst_size = int((dst_num_pos - num_extra_tokens) ** 0.5) if src_size != dst_size: if rank == 0: print("Position interpolate for %s from %dx%d to %dx%d" % ( key, src_size, src_size, dst_size, dst_size)) extra_tokens = rel_pos_bias[-num_extra_tokens:, :] rel_pos_bias = rel_pos_bias[:-num_extra_tokens, :] def geometric_progression(a, r, n): return a * (1.0 - r ** n) / (1.0 - r) left, right = 1.01, 1.5 while right - left > 1e-6: q = (left + right) / 2.0 gp = geometric_progression(1, q, src_size // 2) if gp > dst_size // 2: right = q else: left = q # if q > 1.13492: # q = 1.13492 dis = [] cur = 1 for i in range(src_size // 2): dis.append(cur) cur += q ** (i + 1) r_ids = [-_ for _ in reversed(dis)] x = r_ids + [0] + dis y = r_ids + [0] + dis t = dst_size // 2.0 dx = np.arange(-t, t + 0.1, 1.0) dy = np.arange(-t, t + 0.1, 1.0) if rank == 0: print("x = {}".format(x)) print("dx = {}".format(dx)) all_rel_pos_bias = [] for i in range(num_attn_heads): z = rel_pos_bias[:, i].view(src_size, src_size).float().numpy() f = interpolate.interp2d(x, y, z, kind='cubic') all_rel_pos_bias.append( torch.Tensor(f(dx, dy)).contiguous().view(-1, 1).to(rel_pos_bias.device)) rel_pos_bias = torch.cat(all_rel_pos_bias, dim=-1) new_rel_pos_bias = torch.cat((rel_pos_bias, extra_tokens), dim=0) state_dict[key] = new_rel_pos_bias if append_prefix('pos_embed') in state_dict: pos_embed_checkpoint = state_dict[append_prefix('pos_embed')] embedding_size = pos_embed_checkpoint.shape[-1] num_patches = model.backbone.bottom_up.backbone.patch_embed.num_patches num_extra_tokens = model.backbone.bottom_up.backbone.pos_embed.shape[-2] - num_patches # height (== width) for the checkpoint position embedding orig_size = int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5) # height (== width) for the new position embedding # new_size = int(num_patches ** 0.5) new_size_w = model.backbone.bottom_up.backbone.patch_embed.num_patches_w new_size_h = model.backbone.bottom_up.backbone.patch_embed.num_patches_h # class_token and dist_token are kept unchanged if orig_size != new_size_h or orig_size != new_size_w: if rank == 0: print("Position interpolate from %dx%d to %dx%d" % (orig_size, orig_size, new_size_w, new_size_h)) extra_tokens = pos_embed_checkpoint[:, :num_extra_tokens] # only the position tokens are interpolated pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:] pos_tokens = pos_tokens.reshape(-1, orig_size, orig_size, embedding_size).permute(0, 3, 1, 2) pos_tokens = torch.nn.functional.interpolate( pos_tokens, size=(new_size_w, new_size_h), mode='bicubic', align_corners=False) pos_tokens = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2) new_pos_embed = torch.cat((extra_tokens, pos_tokens), dim=1) state_dict[append_prefix('pos_embed')] = new_pos_embed # interpolate position bias table if needed relative_position_bias_table_keys = [k for k in state_dict.keys() if "relative_position_bias_table" in k] for table_key in relative_position_bias_table_keys: table_pretrained = state_dict[table_key] if table_key not in model.state_dict(): continue table_current = model.state_dict()[table_key] L1, nH1 = table_pretrained.size() L2, nH2 = table_current.size() if nH1 != nH2: logger.warning(f"Error in loading {table_key}, pass") else: if L1 != L2: S1 = int(L1 ** 0.5) S2 = int(L2 ** 0.5) table_pretrained_resized = F.interpolate( table_pretrained.permute(1, 0).view(1, nH1, S1, S1), size=(S2, S2), mode='bicubic') state_dict[table_key] = table_pretrained_resized.view(nH2, L2).permute(1, 0) if append_prefix('rel_pos_bias.relative_position_bias_table') in state_dict and \ model.backbone.bottom_up.backbone.use_rel_pos_bias and \ not model.backbone.bottom_up.backbone.use_shared_rel_pos_bias and \ append_prefix('blocks.0.attn.relative_position_bias_table') not in state_dict: logger.info("[BEIT] Expand the shared relative position embedding to each transformer block. ") num_layers = model.backbone.bottom_up.backbone.get_num_layers() rel_pos_bias = state_dict[append_prefix("rel_pos_bias.relative_position_bias_table")] for i in range(num_layers): state_dict["blocks.%d.attn.relative_position_bias_table" % i] = rel_pos_bias.clone() state_dict.pop(append_prefix("rel_pos_bias.relative_position_bias_table")) return state_dict
null
183,620
import warnings import math import torch import torch.nn as nn import torch.utils.checkpoint as checkpoint from timm.models.layers import trunc_normal_, drop_path, to_2tuple from functools import partial def _cfg(url='', **kwargs): class ViT(nn.Module): def __init__(self, model_name='vit_base_patch16_224', img_size=384, patch_size=16, in_chans=3, embed_dim=1024, depth=24, num_heads=16, num_classes=19, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop_rate=0.1, attn_drop_rate=0., drop_path_rate=0., hybrid_backbone=None, norm_layer=partial(nn.LayerNorm, eps=1e-6), norm_cfg=None, pos_embed_interp=False, random_init=False, align_corners=False, use_checkpoint=False, num_extra_tokens=1, out_features=None, **kwargs, ): def fix_init_weight(self): def rescale(param, layer_id): def _init_weights(self, m): def get_num_layers(self): def no_weight_decay(self): def _conv_filter(self, state_dict, patch_size=16): def to_2D(self, x): def to_1D(self, x): def interpolate_pos_encoding(self, x, w, h): def prepare_tokens(self, x, mask=None): def forward_features(self, x): def forward(self, x): def mae_base_patch16(pretrained=False, **kwargs): model = ViT( patch_size=16, drop_rate=0., embed_dim=768, depth=12, num_heads=12, num_classes=1000, mlp_ratio=4., qkv_bias=True, use_checkpoint=True, num_extra_tokens=1, **kwargs) model.default_cfg = _cfg() return model
null
183,623
import os import xml.dom.minidom if os.path.exists("/mnt/localdata/Users/junlongli/projects/datasets/icdar2019"): PATH = "/mnt/localdata/Users/junlongli/projects/datasets/icdar2019/trackA_modern/test" else: PATH = "/mnt/data/data/icdar2019/trackA_modern/test" reg_gt_path_archival = os.path.abspath(PATH) import xml.dom.minidom from os.path import join as osj from .data_structure import * class eval: STR = "-str" REG = "-reg" DEFAULT_ENCODING = "UTF-8" # reg_gt_path = "./annotations/trackA/" # str_gt_path = "./annotations/trackB/" # reg_gt_path = os.path.abspath("data/test") # reg_gt_path_archival = os.path.abspath("data/test") # reg_gt_path_modern = os.path.abspath("data/test") # str_gt_path_1 = os.path.abspath("data/test") # str_gt_path_2 = os.path.abspath("data/test") # str_gt_path_archival = os.path.abspath("data/test") # str_gt_path_modern = os.path.abspath("data/test") # dummyDom = xml.dom.minidom.parse("./dummyXML.xml") def __init__(self, track, res_path): self.return_result = None self.reg = True self.str = False self.resultFile = res_path self.inPrefix = os.path.split(res_path)[-1].split(".")[0][:-7] if track == "-trackA": self.reg = True self.GTFile = osj(reg_gt_path, self.inPrefix + ".xml") # self.GTFile = osj(self.reg_gt_path, self.inPrefix) elif track == "-trackA1": # archival documents self.reg = True self.GTFile = osj(reg_gt_path_archival, self.inPrefix + ".xml") elif track == "-trackA2": # modern documents self.reg = True self.GTFile = osj(reg_gt_path_modern, self.inPrefix + ".xml") elif track == "-trackB1": self.str = True self.GTFile = osj(str_gt_path_1, self.inPrefix + ".xml") # self.GTFile = osj(self.str_gt_path_1, self.inPrefix) elif track == "-trackB2": self.str = True self.GTFile = osj(str_gt_path_2, self.inPrefix + ".xml") # print(self.GTFile) # self.GTFile = osj(self.str_gt_path_2, self.inPrefix) elif track == "-trackB2_a": self.str = True self.GTFile = osj(str_gt_path_archival, self.inPrefix + ".xml") elif track == "-trackB2_m": self.str = True self.GTFile = osj(str_gt_path_modern, self.inPrefix + ".xml") else: print(track) print("Not a valid track, please check your spelling.") # self.resultFile = res_path # self.inPrefix = os.path.split(res_path)[-1].split("-")[0] # if self.str: # # self.GTFile = osj(self.str_gt_path, self.inPrefix + "-str.xml") # self.GTFile = osj(self.str_gt_path, self.inPrefix + ".xml") # elif self.reg: # # self.GTFile = osj(self.reg_gt_path, self.inPrefix + "-reg.xml") # self.GTFile = osj(self.reg_gt_path, self.inPrefix + ".xml") # else: # print("Not a valid track, please check your spelling.") self.gene_ret_lst() def result(self): return self.return_result def gene_ret_lst(self): ret_lst = [] for iou in [0.6, 0.7, 0.8, 0.9]: temp = self.compute_retVal(iou) ret_lst.append(temp) # ret_lst.append(self.compute_retVal(iou)) ret_lst.append(self.inPrefix + ".xml") # ret_lst.append(self.inPrefix) # print("Done processing {}\n".format(self.resultFile)) self.return_result = ret_lst def compute_retVal(self, iou): gt_dom = xml.dom.minidom.parse(self.GTFile) # incorrect submission format handling try: result_dom = xml.dom.minidom.parse(self.resultFile) except Exception as e: # result_dom = xml.dom.minidom.parse(dummyDom) gt_tables = eval.get_table_list(gt_dom) retVal = ResultStructure(truePos=0, gtTotal=len(gt_tables), resTotal=0) return retVal # result_dom = xml.dom.minidom.parse(self.resultFile) if self.reg: ret = self.evaluate_result_reg(gt_dom, result_dom, iou) return ret if self.str: ret = self.evaluate_result_str(gt_dom, result_dom, iou) return ret def get_table_list(dom): """ return a list of Table objects corresponding to the table element of the DOM. """ return [Table(_nd) for _nd in dom.documentElement.getElementsByTagName("table")] def evaluate_result_reg(gt_dom, result_dom, iou_value): # parse the tables in input elements gt_tables = eval.get_table_list(gt_dom) result_tables = eval.get_table_list(result_dom) # duplicate result table list remaining_tables = result_tables.copy() # map the tables in gt and result file table_matches = [] # @param: table_matches - list of mapping of tables in gt and res file, in order (gt, res) for gtt in gt_tables: for rest in remaining_tables: if gtt.compute_table_iou(rest) >= iou_value: remaining_tables.remove(rest) table_matches.append((gtt, rest)) break assert len(table_matches) <= len(gt_tables) assert len(table_matches) <= len(result_tables) retVal = ResultStructure(truePos=len(table_matches), gtTotal=len(gt_tables), resTotal=len(result_tables)) return retVal def evaluate_result_str(gt_dom, result_dom, iou_value, table_iou_value=0.8): # parse the tables in input elements gt_tables = eval.get_table_list(gt_dom) result_tables = eval.get_table_list(result_dom) # duplicate result table list remaining_tables = result_tables.copy() gt_remaining = gt_tables.copy() # map the tables in gt and result file table_matches = [] # @param: table_matches - list of mapping of tables in gt and res file, in order (gt, res) for gtt in gt_remaining: for rest in remaining_tables: # note: for structural analysis, use 0.8 for table mapping if gtt.compute_table_iou(rest) >= table_iou_value: table_matches.append((gtt, rest)) remaining_tables.remove(rest) # unsafe... should be ok with the break below gt_remaining.remove(gtt) break total_gt_relation, total_res_relation, total_correct_relation = 0, 0, 0 for gt_table, ress_table in table_matches: # set up the cell mapping for matching tables cell_mapping = gt_table.find_cell_mapping(ress_table, iou_value) # set up the adj relations, convert the one for result table to a dictionary for faster searching gt_AR = gt_table.find_adj_relations() total_gt_relation += len(gt_AR) res_AR = ress_table.find_adj_relations() total_res_relation += len(res_AR) # Now map GT adjacency relations to result lMappedAR = [] for ar in gt_AR: try: resFromCell = cell_mapping[ar.fromText] resToCell = cell_mapping[ar.toText] # make a mapped adjacency relation lMappedAR.append(AdjRelation(resFromCell, resToCell, ar.direction)) except: # no mapping is possible pass # compare two list of adjacency relation correct_dect = 0 for ar1 in res_AR: for ar2 in lMappedAR: if ar1.isEqual(ar2): correct_dect += 1 break total_correct_relation += correct_dect # handle gt_relations in unmatched gt table for gtt_remain in gt_remaining: total_gt_relation += len(gtt_remain.find_adj_relations()) # handle gt_relation in unmatched res table for res_remain in remaining_tables: total_res_relation += len(res_remain.find_adj_relations()) retVal = ResultStructure(truePos=total_correct_relation, gtTotal=total_gt_relation, resTotal=total_res_relation) return retVal def process_missing_files(track, gt_file_lst, cur_gt_num): if track in ["-trackA", "-trackA1", "-trackA2"]: gt_file_lst_full = [osj(reg_gt_path, filename) for filename in gt_file_lst] for file in gt_file_lst_full: if os.path.split(file)[-1].split(".")[-1] == "xml": gt_dom = xml.dom.minidom.parse(file) gt_root = gt_dom.documentElement # tables = [] table_elements = gt_root.getElementsByTagName("table") for res_table in table_elements: # t = Table(res_table) # tables.append(t) cur_gt_num += 1 return cur_gt_num elif track == "-trackB1": gt_file_lst_full = [osj(str_gt_path_1, filename) for filename in gt_file_lst] for file in gt_file_lst_full: if os.path.split(file)[-1].split(".")[-1] == "xml": gt_dom = xml.dom.minidom.parse(file) gt_root = gt_dom.documentElement tables = [] table_elements = gt_root.getElementsByTagName("table") for res_table in table_elements: t = Table(res_table) tables.append(t) for table in tables: cur_gt_num += len(table.find_adj_relations()) return cur_gt_num elif track == "-trackB2": gt_file_lst_full = [osj(str_gt_path_2, filename) for filename in gt_file_lst] for file in gt_file_lst_full: if os.path.split(file)[-1].split(".")[-1] == "xml": gt_dom = xml.dom.minidom.parse(file) gt_root = gt_dom.documentElement tables = [] table_elements = gt_root.getElementsByTagName("table") for res_table in table_elements: t = Table(res_table) tables.append(t) for table in tables: cur_gt_num += len(table.find_adj_relations()) return cur_gt_num def calc(F1): sum_a = 0.6 * F1[0] + 0.7 * F1[1] + 0.8 * F1[2] + 0.9 * F1[3] sum_b = 0.6 + 0.7 + 0.8 + 0.9 return sum_a / sum_b def calc_table_score(result_path): # measure = eval(*sys.argv[1:]) gt_file_lst = os.listdir(reg_gt_path_archival) track = "-trackA1" untar_path = result_path res_lst = [] for root, files, dirs in os.walk(untar_path): for name in dirs: if name.split(".")[-1] == "xml": cur_filepath = osj(os.path.abspath(root), name) res_lst.append(eval(track, cur_filepath)) # printing for debug # print("Processing... {}".format(name)) # print("DONE WITH FILE PROCESSING\n") # note: results are stored as list of each when iou at [0.6, 0.7, 0.8, 0.9, gt_filename] # gt number should be the same for all files gt_num = 0 correct_six, res_six = 0, 0 correct_seven, res_seven = 0, 0 correct_eight, res_eight = 0, 0 correct_nine, res_nine = 0, 0 for each_file in res_lst: # print(each_file) try: gt_file_lst.remove(each_file.result[-1]) if each_file.result[-1].replace('.xml', '.jpg') in gt_file_lst: gt_file_lst.remove(each_file.result[-1].replace('.xml', '.jpg')) correct_six += each_file.result[0].truePos gt_num += each_file.result[0].gtTotal res_six += each_file.result[0].resTotal # print("{} {} {}".format(each_file.result[0].truePos, each_file.result[0].gtTotal, each_file.result[0].resTotal)) correct_seven += each_file.result[1].truePos res_seven += each_file.result[1].resTotal correct_eight += each_file.result[2].truePos res_eight += each_file.result[2].resTotal correct_nine += each_file.result[3].truePos res_nine += each_file.result[3].resTotal except: print("Error occur in processing result list.") print(each_file.result[-1]) break # print(each_file.result[-1]) # print(each_file) # for file in gt_file_lst: # if file.split(".") != "xml": # gt_file_lst.remove(file) # # print(gt_file_lst) for i in range(len(gt_file_lst) - 1, -1, -1): if gt_file_lst[i].split(".")[-1] != "xml": del gt_file_lst[i] if len(gt_file_lst) > 0: print("\nWarning: missing result annotations for file: {}\n".format(gt_file_lst)) gt_total = process_missing_files(track, gt_file_lst, gt_num) else: gt_total = gt_num try: # print("Evaluation of {}".format(track.replace("-", ""))) # iou @ 0.6 p_six = correct_six / res_six r_six = correct_six / gt_total f1_six = 2 * p_six * r_six / (p_six + r_six) print("IOU @ 0.6 -\nprecision: {}\nrecall: {}\nf1: {}".format(p_six, r_six, f1_six)) print("correct: {}, gt: {}, res: {}\n".format(correct_six, gt_total, res_six)) # iou @ 0.7 p_seven = correct_seven / res_seven r_seven = correct_seven / gt_total f1_seven = 2 * p_seven * r_seven / (p_seven + r_seven) print("IOU @ 0.7 -\nprecision: {}\nrecall: {}\nf1: {}".format(p_seven, r_seven, f1_seven)) print("correct: {}, gt: {}, res: {}\n".format(correct_seven, gt_total, res_seven)) # iou @ 0.8 p_eight = correct_eight / res_eight r_eight = correct_eight / gt_total f1_eight = 2 * p_eight * r_eight / (p_eight + r_eight) print("IOU @ 0.8 -\nprecision: {}\nrecall: {}\nf1: {}".format(p_eight, r_eight, f1_eight)) print("correct: {}, gt: {}, res: {}\n".format(correct_eight, gt_total, res_eight)) # iou @ 0.9 p_nine = correct_nine / res_nine r_nine = correct_nine / gt_total f1_nine = 2 * p_nine * r_nine / (p_nine + r_nine) print("IOU @ 0.9 -\nprecision: {}\nrecall: {}\nf1: {}".format(p_nine, r_nine, f1_nine)) print("correct: {}, gt: {}, res: {}".format(correct_nine, gt_total, res_nine)) F1 = [f1_six, f1_seven, f1_eight, f1_nine] wF1 = calc(F1) print("Average weight F1: {}".format(wF1)) return { 'p_six':p_six * 100, "r_six":r_six * 100, "f1_six":f1_six * 100, "p_seven":p_seven * 100, "r_seven":r_seven * 100, "f1_seven":f1_seven * 100, "p_eight":p_eight * 100, "r_eight":r_eight * 100, "f1_eight":f1_eight * 100, "p_nine":p_nine * 100, "r_nine":r_nine * 100, "f1_nine":f1_nine * 100, "wF1":wF1 * 100 } except ZeroDivisionError: print( "Error: zero devision error found, (possible that no adjacency relations are found), please check the file input.") return {"wF1": 0}
null
183,626
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import numpy as np from datasets import ClassLabel, load_dataset, load_metric import transformers from layoutlmft.data import DataCollatorForKeyValueExtraction from transformers import ( AutoConfig, AutoModelForTokenClassification, AutoTokenizer, HfArgumentParser, PreTrainedTokenizerFast, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process from transformers.utils import check_min_version from layoutlmft.data.image_utils import RandomResizedCropAndInterpolationWithTwoPic, pil_loader, Compose from timm.data.constants import \ IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_INCEPTION_MEAN, IMAGENET_INCEPTION_STD from torchvision import transforms import torch def main(): # See all possible arguments in layoutlmft/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) if len(sys.argv) == 2 and sys.argv[1].endswith(".json"): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) else: model_args, data_args, training_args = parser.parse_args_into_dataclasses() # Detecting last checkpoint. last_checkpoint = None if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir: last_checkpoint = get_last_checkpoint(training_args.output_dir) if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0: raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. " "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None: logger.info( f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", handlers=[logging.StreamHandler(sys.stdout)], ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN) # Log on each process the small summary: logger.warning( f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info(f"Training/evaluation parameters {training_args}") # Set seed before initializing model. set_seed(training_args.seed) if data_args.dataset_name == 'funsd': # datasets = load_dataset("nielsr/funsd") import layoutlmft.data.funsd datasets = load_dataset(os.path.abspath(layoutlmft.data.funsd.__file__), cache_dir=model_args.cache_dir) elif data_args.dataset_name == 'cord': import layoutlmft.data.cord datasets = load_dataset(os.path.abspath(layoutlmft.data.cord.__file__), cache_dir=model_args.cache_dir) else: raise NotImplementedError() if training_args.do_train: column_names = datasets["train"].column_names features = datasets["train"].features else: column_names = datasets["test"].column_names features = datasets["test"].features text_column_name = "words" if "words" in column_names else "tokens" label_column_name = ( f"{data_args.task_name}_tags" if f"{data_args.task_name}_tags" in column_names else column_names[1] ) remove_columns = column_names # In the event the labels are not a `Sequence[ClassLabel]`, we will need to go through the dataset to get the # unique labels. def get_label_list(labels): unique_labels = set() for label in labels: unique_labels = unique_labels | set(label) label_list = list(unique_labels) label_list.sort() return label_list if isinstance(features[label_column_name].feature, ClassLabel): label_list = features[label_column_name].feature.names # No need to convert the labels since they are already ints. label_to_id = {i: i for i in range(len(label_list))} else: label_list = get_label_list(datasets["train"][label_column_name]) label_to_id = {l: i for i, l in enumerate(label_list)} num_labels = len(label_list) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. config = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path, num_labels=num_labels, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, revision=model_args.model_revision, input_size=data_args.input_size, use_auth_token=True if model_args.use_auth_token else None, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, tokenizer_file=None, # avoid loading from a cached file of the pre-trained model in another machine cache_dir=model_args.cache_dir, use_fast=True, add_prefix_space=True, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) model = AutoModelForTokenClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool(".ckpt" in model_args.model_name_or_path), config=config, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Tokenizer check: this script requires a fast tokenizer. if not isinstance(tokenizer, PreTrainedTokenizerFast): raise ValueError( "This example script only works for models that have a fast tokenizer. Checkout the big table of models " "at https://huggingface.co/transformers/index.html#bigtable to find the model types that meet this " "requirement" ) # Preprocessing the dataset # Padding strategy padding = "max_length" if data_args.pad_to_max_length else False if data_args.visual_embed: imagenet_default_mean_and_std = data_args.imagenet_default_mean_and_std mean = IMAGENET_INCEPTION_MEAN if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_MEAN std = IMAGENET_INCEPTION_STD if not imagenet_default_mean_and_std else IMAGENET_DEFAULT_STD common_transform = Compose([ # transforms.ColorJitter(0.4, 0.4, 0.4), # transforms.RandomHorizontalFlip(p=0.5), RandomResizedCropAndInterpolationWithTwoPic( size=data_args.input_size, interpolation=data_args.train_interpolation), ]) patch_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize( mean=torch.tensor(mean), std=torch.tensor(std)) ]) # Tokenize all texts and align the labels with them. def tokenize_and_align_labels(examples, augmentation=False): tokenized_inputs = tokenizer( examples[text_column_name], padding=False, truncation=True, return_overflowing_tokens=True, # We use this argument because the texts in our dataset are lists of words (with a label for each word). is_split_into_words=True, ) labels = [] bboxes = [] images = [] for batch_index in range(len(tokenized_inputs["input_ids"])): word_ids = tokenized_inputs.word_ids(batch_index=batch_index) org_batch_index = tokenized_inputs["overflow_to_sample_mapping"][batch_index] label = examples[label_column_name][org_batch_index] bbox = examples["bboxes"][org_batch_index] previous_word_idx = None label_ids = [] bbox_inputs = [] for word_idx in word_ids: # Special tokens have a word id that is None. We set the label to -100 so they are automatically # ignored in the loss function. if word_idx is None: label_ids.append(-100) bbox_inputs.append([0, 0, 0, 0]) # We set the label for the first token of each word. elif word_idx != previous_word_idx: label_ids.append(label_to_id[label[word_idx]]) bbox_inputs.append(bbox[word_idx]) # For the other tokens in a word, we set the label to either the current label or -100, depending on # the label_all_tokens flag. else: label_ids.append(label_to_id[label[word_idx]] if data_args.label_all_tokens else -100) bbox_inputs.append(bbox[word_idx]) previous_word_idx = word_idx labels.append(label_ids) bboxes.append(bbox_inputs) if data_args.visual_embed: ipath = examples["image_path"][org_batch_index] img = pil_loader(ipath) for_patches, _ = common_transform(img, augmentation=augmentation) patch = patch_transform(for_patches) images.append(patch) tokenized_inputs["labels"] = labels tokenized_inputs["bbox"] = bboxes if data_args.visual_embed: tokenized_inputs["images"] = images return tokenized_inputs if training_args.do_train: if "train" not in datasets: raise ValueError("--do_train requires a train dataset") train_dataset = datasets["train"] if data_args.max_train_samples is not None: train_dataset = train_dataset.select(range(data_args.max_train_samples)) train_dataset = train_dataset.map( tokenize_and_align_labels, batched=True, remove_columns=remove_columns, num_proc=data_args.preprocessing_num_workers, load_from_cache_file=not data_args.overwrite_cache, ) if training_args.do_eval: validation_name = "test" if validation_name not in datasets: raise ValueError("--do_eval requires a validation dataset") eval_dataset = datasets[validation_name] if data_args.max_val_samples is not None: eval_dataset = eval_dataset.select(range(data_args.max_val_samples)) eval_dataset = eval_dataset.map( tokenize_and_align_labels, batched=True, remove_columns=remove_columns, num_proc=data_args.preprocessing_num_workers, load_from_cache_file=not data_args.overwrite_cache, ) if training_args.do_predict: if "test" not in datasets: raise ValueError("--do_predict requires a test dataset") test_dataset = datasets["test"] if data_args.max_test_samples is not None: test_dataset = test_dataset.select(range(data_args.max_test_samples)) test_dataset = test_dataset.map( tokenize_and_align_labels, batched=True, remove_columns=remove_columns, num_proc=data_args.preprocessing_num_workers, load_from_cache_file=not data_args.overwrite_cache, ) # Data collator data_collator = DataCollatorForKeyValueExtraction( tokenizer, pad_to_multiple_of=8 if training_args.fp16 else None, padding=padding, max_length=512, ) # Metrics metric = load_metric("seqeval") def compute_metrics(p): predictions, labels = p predictions = np.argmax(predictions, axis=2) # Remove ignored index (special tokens) true_predictions = [ [label_list[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] true_labels = [ [label_list[l] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] results = metric.compute(predictions=true_predictions, references=true_labels) if data_args.return_entity_level_metrics: # Unpack nested dictionaries final_results = {} for key, value in results.items(): if isinstance(value, dict): for n, v in value.items(): final_results[f"{key}_{n}"] = v else: final_results[key] = value return final_results else: return { "precision": results["overall_precision"], "recall": results["overall_recall"], "f1": results["overall_f1"], "accuracy": results["overall_accuracy"], } # Initialize our Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset if training_args.do_train else None, eval_dataset=eval_dataset if training_args.do_eval else None, tokenizer=tokenizer, data_collator=data_collator, compute_metrics=compute_metrics, ) # Training if training_args.do_train: checkpoint = last_checkpoint if last_checkpoint else None train_result = trainer.train(resume_from_checkpoint=checkpoint) metrics = train_result.metrics trainer.save_model() # Saves the tokenizer too for easy upload max_train_samples = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset) ) metrics["train_samples"] = min(max_train_samples, len(train_dataset)) trainer.log_metrics("train", metrics) trainer.save_metrics("train", metrics) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***") metrics = trainer.evaluate() max_val_samples = data_args.max_val_samples if data_args.max_val_samples is not None else len(eval_dataset) metrics["eval_samples"] = min(max_val_samples, len(eval_dataset)) trainer.log_metrics("eval", metrics) trainer.save_metrics("eval", metrics) # Predict if training_args.do_predict: logger.info("*** Predict ***") predictions, labels, metrics = trainer.predict(test_dataset) predictions = np.argmax(predictions, axis=2) # Remove ignored index (special tokens) true_predictions = [ [label_list[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] trainer.log_metrics("test", metrics) trainer.save_metrics("test", metrics) # Save predictions output_test_predictions_file = os.path.join(training_args.output_dir, "test_predictions.txt") if trainer.is_world_process_zero(): with open(output_test_predictions_file, "w") as writer: for prediction in true_predictions: writer.write(" ".join(prediction) + "\n") def _mp_fn(index): # For xla_spawn (TPUs) main()
null
183,627
import json import os from pathlib import Path import datasets from layoutlmft.data.image_utils import load_image, normalize_bbox def quad_to_box(quad): # test 87 is wrongly annotated box = ( max(0, quad["x1"]), max(0, quad["y1"]), quad["x3"], quad["y3"] ) if box[3] < box[1]: bbox = list(box) tmp = bbox[3] bbox[3] = bbox[1] bbox[1] = tmp box = tuple(bbox) if box[2] < box[0]: bbox = list(box) tmp = bbox[2] bbox[2] = bbox[0] bbox[0] = tmp box = tuple(bbox) return box
null
183,628
import json import os from pathlib import Path import datasets from layoutlmft.data.image_utils import load_image, normalize_bbox def _get_drive_url(url): base_url = 'https://drive.google.com/uc?id=' split_url = url.split('/') return base_url + split_url[5]
null
183,629
import torchvision.transforms.functional as F import warnings import math import random import numpy as np from PIL import Image import torch from detectron2.data.detection_utils import read_image from detectron2.data.transforms import ResizeTransform, TransformList def normalize_bbox(bbox, size): return [ int(1000 * bbox[0] / size[0]), int(1000 * bbox[1] / size[1]), int(1000 * bbox[2] / size[0]), int(1000 * bbox[3] / size[1]), ]
null
183,630
import torchvision.transforms.functional as F import warnings import math import random import numpy as np from PIL import Image import torch from detectron2.data.detection_utils import read_image from detectron2.data.transforms import ResizeTransform, TransformList def load_image(image_path): image = read_image(image_path, format="BGR") h = image.shape[0] w = image.shape[1] img_trans = TransformList([ResizeTransform(h=h, w=w, new_h=224, new_w=224)]) image = torch.tensor(img_trans.apply_image(image).copy()).permute(2, 0, 1) # copy to make it writeable return image, (w, h)
null
183,631
import torchvision.transforms.functional as F import warnings import math import random import numpy as np from PIL import Image import torch from detectron2.data.detection_utils import read_image from detectron2.data.transforms import ResizeTransform, TransformList def clamp(num, min_value, max_value): return max(min(num, max_value), min_value) def crop(image, i, j, h, w, boxes=None): cropped_image = F.crop(image, i, j, h, w) if boxes is not None: # Currently we cannot use this case since when some boxes is out of the cropped image, # it may be better to drop out these boxes along with their text input (instead of min or clamp) # which haven't been implemented here max_size = torch.as_tensor([w, h], dtype=torch.float32) cropped_boxes = torch.as_tensor(boxes) - torch.as_tensor([j, i, j, i]) cropped_boxes = torch.min(cropped_boxes.reshape(-1, 2, 2), max_size) cropped_boxes = cropped_boxes.clamp(min=0) boxes = cropped_boxes.reshape(-1, 4) return cropped_image, boxes
null
183,632
import torchvision.transforms.functional as F import warnings import math import random import numpy as np from PIL import Image import torch from detectron2.data.detection_utils import read_image from detectron2.data.transforms import ResizeTransform, TransformList def resize(image, size, interpolation, boxes=None): # It seems that we do not need to resize boxes here, since the boxes will be resized to 1000x1000 finally, # which is compatible with a square image size of 224x224 rescaled_image = F.resize(image, size, interpolation) if boxes is None: return rescaled_image, None ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(rescaled_image.size, image.size)) ratio_width, ratio_height = ratios # boxes = boxes.copy() scaled_boxes = boxes * torch.as_tensor([ratio_width, ratio_height, ratio_width, ratio_height]) return rescaled_image, scaled_boxes
null