def get_params(args): params = {} for name in model.parameters.keys(): params[name] = getattr(args, name) return Struct(**params)
def collate_batch(batch): r"""Puts each data field into a tensor with outer dimension batch size""" elem = batch[0] if type(elem) is Table: return cat_tables(batch) if type(elem) is Struct: d = { key: collate_batch([d[key] for d in batch]) for key in elem.keys() } return Struct(d) elif isinstance(elem, str): return batch elif elem is None: return batch elif isinstance(elem, collections.abc.Mapping): return {key: collate([d[key] for d in batch]) for key in elem} elif isinstance(elem, collections.abc.Sequence): transposed = zip(*batch) return [collate_batch(samples) for samples in transposed] else: return default_collate(batch) raise TypeError( "batch must contain Table, numbers, dicts or lists; found {}".format( elem_type))
def confidence_iou_table(datasets): all_points = [ p for dataset in datasets.values() for p in get_annotation_ious(dataset) ] ranges = ([0.0, 0.7, 1.01], [0.0, 0.85, 1.01]) def compute(points): conf, iou, label = zip(*points) hist = np.histogram2d(np.array(conf), np.array(iou), bins=ranges)[0] / len(points) return struct(high_imprecise=hist[1, 0], low_precise=hist[0, 1], high_precise=hist[1, 1]) tables = {k: compute(get_annotation_ious(d)) for k, d in datasets.items()} tables = Struct(tables)._extend(total=compute(all_points)) return Struct(transpose_dicts(tables))
def read_log(file): entries = [to_structs(json.loads(line)) for line in open(file, mode="r")] steps = {} tags = {} for entry in entries: step = steps.get(entry.step) or {} step[entry.tag] = struct(value=entry.value, time=entry.time) tags[entry.tag] = True steps[entry.step] = step return struct (tags=tags.keys(), steps={i : Struct(step) for i, step in steps.items()})
# # def selu(x): # alpha = 1.6732632423543772848170429916717 # scale = 1.0507009873554804934193349852946 # # return scale * (F.relu(x) + alpha * (F.elu(-1*F.relu(-1*x)))) parameters = Struct( growth = (2.0, "growth factor in layer features"), depth = (3, "number of layers in encoder/decoder"), kernel_size = (5, "kernel size for convolutions"), dropout = (0, "dropout per convolution"), discrim_bias = (1, "factor of features for discriminator vs generator"), nglobal = (0, "size of global noise vector"), nlocal = (32, "size of local noise vector"), nperiodic = (0, "size of periodic input vector"), hidden = (32, "size of features in hidden layer for estimating period") ) def create(args): class Conv(nn.Module): def __init__(self, in_size, out_size, kernel=args.kernel_size, dilation=1): super().__init__() self.norm = identity #nn.BatchNorm2d(out_size) self.conv = nn.Conv2d(in_size, out_size, kernel, dilation=dilation, padding=(kernel//2) * dilation)
def training_cycle(): if env == None or len(env.dataset.train_images) == 0: return None if args.max_epochs is not None and env.epoch > args.max_epochs: raise UserCommand('pause') log = EpochLogger(env.log, env.epoch) model = env.model.to(env.device) encoder = env.encoder.to(env.device) log.scalars("dataset", Struct(env.dataset.count_categories())) train_images = env.dataset.train_images if args.incremental is True: t = env.epoch / args.max_epochs n = max(1, min(int(t * len(train_images)), len(train_images))) train_images = train_images[:n] print("training {} on {} images:".format(env.epoch, len(train_images))) train_stats = trainer.train(env.dataset.sample_train_on( train_images, args, env.encoder), evaluate.eval_train(model.train(), env.encoder, env.debug, device=env.device), env.optimizer, hook=train_update) evaluate.summarize_train("train", train_stats, env.dataset.classes, env.epoch, log=log) send_command('training', report_training(train_stats)) score, thresholds = run_testing('validate', env.dataset.validate_images, model, env, hook=update('validate')) if env.args.eval_split: run_testing('validate_split', env.dataset.validate_images, model, env, split=True, hook=update('validate')) is_best = score >= env.best.score if is_best: env.best = struct(model=copy.deepcopy(model), score=score, thresholds=thresholds, epoch=env.epoch) current = struct(state=model.state_dict(), epoch=env.epoch, thresholds=thresholds, score=score) best = struct(state=env.best.model.state_dict(), epoch=env.best.epoch, thresholds=env.best.thresholds, score=env.best.score) # run_testing('test', env.dataset.test_images, model, env, hook=update('test')) for test_name in env.tests: run_testing(test_name, env.dataset.get_images(test_name), model, env, hook=update('test'), thresholds=env.best.thresholds) save_checkpoint = struct(current=current, best=best, args=env.model_args, run=env.run) torch.save(save_checkpoint, env.model_path) send_command("checkpoint", ((env.run, env.epoch), score, is_best)) env.epoch = env.epoch + 1 if (args.detections > 0) and conn: detect_images = least_recently_evaluated(env.dataset.new_images, n=args.detections) results = run_detections(model, env, detect_images, hook=update('detect')) send_command('detections', results) if args.detect_all and conn: detect_images = env.dataset.get_images() results = run_detections(model, env, detect_images, hook=update('detect'), variation_window=args.variation_window) send_command('detections', results) # if env.best.epoch < env.epoch - args.validation_pause: # raise UserCommand('pause') if env.pause_time is not None: env.pause_time = env.pause_time - 1 if env.pause_time == 0: raise UserCommand('pause') log.flush()
return (x1, y1), (x1 + tw, y1 + th) def random_crop(dim, border=0): def crop(image): h, w, c = image.size() assert dim[0] + border <= w and dim[1] + border <= h pos, _ = random_region(image, dim, border) return image.narrow(0, pos[1], dim[1]).narrow(1, pos[0], dim[0]) return crop def load_rgb(filename): return cv.imread(filename) default_statistics = Struct(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) def normalize(image, mean=default_statistics.mean, std=default_statistics.mean): image = image.float().div_(255) for i in range(0, 2): image.select(2, i).sub_(mean[i]).div_(std[i]) return image.permute(0, 3, 1, 2) def un_normalize(image, mean=default_statistics.mean, std=default_statistics.mean): image = image.clone() for i in range(0, 2): image.select(2, i).mul_(std[i]).add_(mean[i]) image = image.mul_(255).clamp(0, 255).byte()
#!/usr/bin/env python from __future__ import division from figen import fixImage, varImage, colors from tools import Struct, Timer img = Struct( width = 256, height = 256 ) @fixImage(img.width, img.height) def fib(x, y) : a = b = 1 for _ in range(x - 1) : a, b = b, a + b return (b % 255,) * 3
#!/usr/bin/env python from __future__ import division from figen import fixImage, varImage, colors from tools import Struct, Timer img = Struct(width=2560, height=1440, limit=512, zoom=0.9, c=-0.8 - 0.156j) @fixImage(img.width, img.height) def julia(x, y): ratio = img.width / img.height def iterate(): zx = (x - (img.width / 2)) / (img.width / 2 * img.zoom) * ratio zy = (y - (img.height / 2)) / (img.height / 2 * img.zoom) z = zx + zy * 1j c = img.c for it in range(img.limit): if z.real**2 + z.imag**2 <= 4: z = z**2 + c else: return it return img.limit val = iterate() return colors.ice(val, 150)
#!/usr/bin/env python from __future__ import division from random import randint from figen import fixImage, varImage, colors from tools import Struct, Timer img = Struct(width=512, height=512, n=40) img.points = {(randint(1, img.width), randint(1, img.height)): color for color in (colors.helpers.convertHSV(randint(60, 300), 0.8) for _ in range(img.n))} @fixImage(img.width, img.height) def voronoi(x, y): d = nearest = None dist = lambda a, b: ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5 for point in img.points.keys(): dn = dist(point, (x, y)) if d is None or d > dn: d = dn nearest = point color = img.points[nearest] return color
def threshold_count(confidence, thresholds): d = {k: (confidence > t).sum().item() for k, t in thresholds.items()} return Struct(d)
def count_struct(values, keys): d = count_dict(values) return Struct({k:d.get(k, 0) for k in keys})
#!/usr/bin/env python from __future__ import division from random import randint from figen import fixImage, varImage, colors from tools import Struct, Timer img = Struct( width = 512, height = 512, n = 40 ) img.points = { (randint(1, img.width), randint(1, img.height)) : color for color in ( colors.helpers.convertHSV(randint(60, 300), 0.8) for _ in range(img.n) ) } @fixImage(img.width, img.height) def voronoi(x, y) : d = nearest = None dist = lambda a, b : ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5 for point in img.points.keys() : dn = dist(point, (x, y)) if d is None or d > dn : d = dn nearest = point
def forward(self, input): output = {k: module(input) for k, module in self.named_children()} return Struct(output)
from tools import Struct import torch import torch.nn as nn import torch.nn.functional as F input_channels = 3 parameters = Struct( noise_size = (512, "noise vector size"), gen_features = (64, "hidden features in generator"), disc_features = (64, "hidden features in discriminator"), bias = (False, "use bias in convolutions") ) def create(args): def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0) bias = args.bias class Generator(nn.Module):