Skip to content

Instantly share code, notes, and snippets.

Avatar

Modar M. Alfadly ModarTensai

View GitHub Profile
@ModarTensai
ModarTensai / frechet.py
Last active Apr 17, 2021
Frechet's distance entirely in PyTorch with data batches streaming support.
View frechet.py
"""Frechet's distance between two multi-variate Gaussians"""
import torch
import torch.nn as nn
class FrechetDistance:
"""Frechet's distance between two multi-variate Gaussians
https://www.sciencedirect.com/science/article/pii/0047259X8290077X
"""
def __init__(self, double=True, num_iterations=20, eps=1e-12):
View neighbors.py
from typing import Tuple, Optional, Union, List
import torch
import torch.nn as nn
__all__ = [
'dot', 'get_neighbors', 'gather_features', 'point_sparsity',
'weighted_sampling'
]
View torch_benchmark.py
import itertools
from typing import Tuple, Optional
from contextlib import contextmanager
import torch
from torch.utils import benchmark
# @torch.jit.script
def nearest_neighbors(
View grid_utils.py
"""Utilities for argparse arguments."""
import os
import sys
from argparse import Namespace
from collections import OrderedDict
from itertools import product, chain
from typing import Union, Dict
__all__ = ['parse_grid']
View video_box_blur.py
#!/usr/bin/env python3
"""Ego4D video box blur."""
import gc
import json
from pathlib import Path
from argparse import ArgumentParser
# conda install av pillow tqdm -c conda-forge -c anaconda
import av # used versions: av=8.0.3 and ffmpeg=4.3.1
from tqdm import tqdm # used versions: tqdm=4.59.0
View yolov3.py
"""YOLOv3 object detector."""
import math
from pathlib import Path
from urllib.request import urlopen
from PIL import Image
from PIL import ImageColor, ImageOps
import matplotlib.pyplot as plt
import matplotlib.patches as patches
View diag_quadratic.py
from functools import partial
import torch
from torch import nn
from torch.autograd import grad
from torch.autograd.functional import jacobian, jvp
def rand_cov(vector):
"""Create a covariance matrix from the specs of a given vector."""
@ModarTensai
ModarTensai / color_toning.py
Created Mar 16, 2020
A color-toning transform made to match TorchVision implementations. Inspired by https://www.pyimagesearch.com/2014/06/30/super-fast-color-transfer-images/
View color_toning.py
import numpy as np
from PIL import Image
from skimage.color import lab2rgb, rgb2lab
class RandomColorToning:
def __init__(self, scale_mean, scale_std, shift_mean, shift_std):
self.scale_mean = scale_mean
self.scale_std = scale_std
@ModarTensai
ModarTensai / autoencoder.py
Last active Mar 17, 2020
PyTorch fully-convolutional auto-encoder for any arbitrary image sizes (including rectangles). Can, also, be used for a DCGAN.
View autoencoder.py
from torch import nn
class Generator(nn.Module):
def __init__(self, input_dim, image_shape, memory):
super().__init__()
self.memory = memory
self.input_dim = input_dim
self.image_shape = image_shape
@ModarTensai
ModarTensai / pytorch_experiment.py
Last active Mar 1, 2020
Modular experiment class to train a PyTorch module. It can be easily inherited or used with mixins to extend its functionality. We us it to train VGG on Cifar10.
View pytorch_experiment.py
import json
import math
from argparse import ArgumentParser
from contextlib import contextmanager
from pathlib import Path
import torch
import torchvision.transforms as T
from torch import nn
from torch.optim import lr_scheduler