This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from detectron2.solver.build import get_default_optimizer_params | |
from detectron2.solver.build import maybe_add_gradient_clipping | |
class MyTrainer(DefaultTrainer): | |
@classmethod | |
def build_optimizer(cls, cfg, model): | |
""" | |
Build an optimizer from config. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PIL, os | |
from tqdm import tqdm | |
import numpy as np | |
import torch | |
from torch.utils.data import Dataset | |
class ImgDataset(Dataset): | |
def __init__(self, data_dir, extension='.jpg', transform=None): | |
self.data_dir = data_dir | |
self.image_files = [f for f in os.listdir(data_dir) if f.endswith(extension)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from detectron2.modeling import build_model | |
from detectron2.checkpoint import DetectionCheckpointer | |
import detectron2.data.transforms as T | |
from detectron2.data.detection_utils import read_image | |
class MyPredictor: | |
""" | |
source: https://github.com/facebookresearch/detectron2/blob/08f617cc2c9276a48e7e7dc96ae946a5df23af3f/detectron2/engine/defaults.py#L252 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyTrainer(DefaultTrainer): | |
@classmethod | |
def build_train_loader(cls, cfg): | |
mapper_train = DatasetMapper(cfg, | |
is_train=True, | |
augmentations=[ #Apply a sequence of augmentations. | |
T.RandomFlip(prob=0.5, horizontal=False, vertical=True), | |
T.RandomFlip(prob=0.5, horizontal=True, vertical=False), | |
T.RandomRotation(angle=[0, 90, 180, 270], sample_style="choice"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import json | |
from pathlib import Path | |
from transformers import AutoConfig, AutoTokenizer, AutoModel | |
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased') | |
class Tagger(nn.Module): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import torch | |
import torch.nn as nn | |
from transformers import AutoConfig, AutoTokenizer, AutoModel | |
class Tagger(nn.Module): | |
""" | |
Minimal model class just for inference | |
""" | |
def __init__(self): |