Skip to content

Instantly share code, notes, and snippets.

View dsevero's full-sized avatar
🎲

Daniel Severo dsevero

🎲
View GitHub Profile
@dsevero
dsevero / compute_bpd.py
Last active October 13, 2023 19:26
Helper scripts to compute bits-per-dimension of images in a directory.
"""
Helper script to compute BPD of images in a directory.
The script will never modify any files.
All conversions are done in memory on a copy.
Usage: python compute_bpd.py your_glob_pattern [extension] [colorspace] [psnr-check]
Args:
- extension: any valid PIL image extension (e.g., PNG, WebP, JPEG).
- colorspace: any valid PIL colorspace plus the lossless YCoCg.
- psnr-check (flag): if set, will compute PSNR of saved image with respect ot the original colorspace.
@dsevero
dsevero / patchify_padded.py
Last active October 11, 2023 03:16
Helper script to patchify images in a directory
'''
Usage: python patchify_padded.py 'my_dir/*.png' output_dir 32
This script:
1) Loads all images matching the glob 'my_dir/*.png'
2) Padds them with zeros such that the height and width are divisible by the specified patch size (in this case, 32)
3) Saves them to disk in parallel to the output directory (output_dir)
'''
from PIL import Image
@dsevero
dsevero / rans.py
Last active February 22, 2023 12:34
Asymmetric Numeral Systems (ANS) codec in pure Python
def push(state, symbol, cdf_func, prec):
cdf_low, cdf_high = cdf_func(symbol)
freq = cdf_high - cdf_low
return prec*(state // freq) + (state % freq) + cdf_low
def pop(state, icdf_func, cdf_func, prec):
cdf_value = state % prec
symbol, cdf_low, cdf_high = icdf_func(cdf_value)
freq = cdf_high - cdf_low
return symbol, freq*(state // prec) + cdf_value - cdf_low
@dsevero
dsevero / rle.py
Last active May 8, 2021 20:39
Vectorized Run-Length Encoding
import numpy as np
def encode(v):
changes = np.flatnonzero(np.diff(v)) + 1
values = np.r_[v[0], v[changes]]
runlengths = np.diff(np.r_[0, changes, len(v)])
return runlengths, values
def decode(runlengths, values):
return np.repeat(values, runlengths)
@dsevero
dsevero / gumbel-softmax-trick.py
Last active June 17, 2021 16:08
Gumbel-Softmax Trick
# We can produce samples from an un-normalized distribution by adding
# iid Gumbel noise together with the argmax operator; which is denoted as the Gumbel-Max Trick.
# However, argmax doesn't produce meaningful gradient signals, so we replace argmax
# by softmax, with a temperature parameter (Gumbel-Softmax Trick).
#
# I didn't invent any of this, all credit goes to the following papers:
# - https://arxiv.org/abs/1611.00712
# - https://arxiv.org/abs/1611.01144
import numpy as np
@dsevero
dsevero / gist:8a885dbe0a547507a8e20ba922ffdbd6
Last active March 25, 2021 05:13
time profiling with contextmanager
from time import time
from contextlib import contextmanager
import json
import torch
import logging
logging.basicConfig(stream=sys.stdout,
level=logging.INFO, format='%(asctime)s %(name)s %(levelname)s:%(message)s')
logger = logging.getLogger()
'''
This file is heavily inspired by https://github.com/j-towns/ans-notes/blob/master/rans.py
We describe a variant of bits-back coding called BB-Huffman. This file is meant
purely as an educational tool and is in no way optimized. The goals are to
1. illustrate how bits-back coding can be used with Huffman-like codecs;
2. and how the cost of using a selector to toggle between codebooks can be greatly reduced.
- Symbols are integers between 0 and 2;
@dsevero
dsevero / init.vim
Last active January 20, 2021 02:34
Init file for nvim and vim
call plug#begin()
Plug 'lervag/vimtex'
Plug 'manicmaniac/coconut.vim'
Plug 'goerz/jupytext.vim'
Plug 'skanehira/preview-markdown.vim'
call plug#end()
tnoremap <Esc> <C-\><C-n>
tnoremap <C-A> pwd\|xclip -selection clipboard<CR><C-\><C-n>:cd <C-r>+<CR>i
vnoremap <C-c> "+y
@dsevero
dsevero / Persisting lru_cache to disk while using hashable pandas objects for parallel experiments.ipynb
Last active March 28, 2024 20:32
Persisting lru_cache to disk while using hashable pandas objects for parallel experiments
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pandas as pd
from hashlib import sha256
from pandas.util import hash_pandas_object
from functools import lru_cache
class HashableDataFrame(pd.DataFrame):
def __init__(self, obj):
super().__init__(obj)