def _transform(self, orig:ndarray, tfms:[Transform], model:nn.Module, sz:int): for tfm in tfms: orig,_=tfm(orig, False) _,val_tfms = tfms_from_stats(inception_stats, sz, crop_type=CropType.NO, aug_tfms=[]) val_tfms.tfms = [tfm for tfm in val_tfms.tfms if not isinstance(tfm, NoCrop)] orig = val_tfms(orig) return orig
def get_data(train_names, val_names, test_names, target_size, batch_size, n_workers=5): aug_tfms = [ RandomRotate(30, tfm_y=TfmType.NO), RandomDihedral(tfm_y=TfmType.NO), RandomLighting(0.05, 0.05, tfm_y=TfmType.NO) ] # std and var stats = A([0.08069, 0.05258, 0.05487, 0.08282], [0.13704, 0.10145, 0.15313, 0.13814]) tfms = tfms_from_stats(stats, target_size, crop_type=CropType.NO, tfm_y=TfmType.NO, aug_tfms=aug_tfms) datasets = ImageData.get_ds( HPADataset, (train_names[:-(len(train_names) % batch_size)], cfg.train_dir), (val_names, cfg.train_dir), tfms, test=(test_names, cfg.test_dir)) img_ds = ImageData('./', datasets, batch_size, num_workers=n_workers, classes=None) return img_ds
def _transform(self, orig: ndarray, sz: int): for tfm in self.tfms: orig, _ = tfm(orig, False) _, val_tfms = tfms_from_stats(inception_stats, sz, crop_type=CropType.NO, aug_tfms=[]) val_tfms.tfms = [ tfm for tfm in val_tfms.tfms if not (isinstance(tfm, NoCrop) or isinstance(tfm, Scale)) ] orig = val_tfms(orig) return orig
class SetupModel(object): model = classification_model() labels = get_labels(LABELS_PATH) tfms = tfms_from_stats(STATS, SZ)[-1] def __init__(self, f): self.f = f file_path = f'/tmp/{STATE_DICT_NAME}' download_file(BUCKET_NAME, STATE_DICT_NAME, file_path) state_dict = torch.load(file_path, map_location=lambda storage, loc: storage) self.model.load_state_dict(state_dict), self.model.eval() os.remove(file_path) def __call__(self, *args, **kwargs): return self.f(*args, **kwargs)
import os, json, traceback import urllib.parse import torch import numpy as np from lib.models import classification_model from lib.utils import download_file, get_labels, open_image_url from fastai.core import A, T, VV_ from fastai.transforms import tfms_from_stats BUCKET_NAME = os.environ['BUCKET_NAME'] STATE_DICT_NAME = os.environ['STATE_DICT_NAME'] STATS = A(*eval(os.environ['IMAGE_STATS'])) SZ = int(os.environ['IMAGE_SIZE']) TFMS = tfms_from_stats(STATS, SZ)[-1] class SetupModel(object): model = classification_model() labels = get_labels(os.environ['LABELS_PATH']) def __init__(self, f): self.f = f file_path = f'/tmp/{STATE_DICT_NAME}' download_file(BUCKET_NAME, STATE_DICT_NAME, file_path) state_dict = torch.load(file_path, map_location=lambda storage, loc: storage) self.model.load_state_dict(state_dict), self.model.eval() os.remove(file_path)