Skip to content

Instantly share code, notes, and snippets.

View Multihuntr's full-sized avatar

Brandon Victor Multihuntr

  • La Trobe University
View GitHub Profile
@Multihuntr
Multihuntr / random_queue.py
Created October 20, 2017 07:55
Blocking Thread-safe Random Queue
import queue
import random
class RandomQueue(queue.Queue):
def _put(self, item):
n = len(self.queue)
i = random.randint(0, n)
self.queue.insert(i, item)
@Multihuntr
Multihuntr / minimal_nvvl_tests.py
Created June 11, 2018 03:45
Minimal examples of using NVIDIA/nvvl
# Simplest case (note: VideoDataset.__get__ puts frames on CPU)
import nvvl
d = nvvl.VideoDataset(['prepared.mp4'], 3)
fr = d[0]
print(type(fr))
print(fr.shape)
# Custom processing (note: VideoDataset.__get__ puts frames on CPU)
import nvvl
d = nvvl.VideoDataset(['prepared.mp4'], 3, processing={'a': nvvl.ProcessDesc()})
@Multihuntr
Multihuntr / accumulate_grads.py
Last active August 10, 2018 08:25
Accumulating gradients to reduce memory requirement per forward pass (using MNIST)
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def simple_model(input):
# This ensures that the model will always be instantiated the same, for comparison.
hidden_initializer = tf.constant_initializer(np.random.uniform(-0.025, 0.025, size=[784,100]))
hidden = tf.layers.dense(input, 100, kernel_initializer=hidden_initializer)
out_initializer = tf.constant_initializer(np.random.uniform(-0.025, 0.025, size=[100,10]))
@Multihuntr
Multihuntr / gauss_smooth.py
Created August 2, 2019 07:15
Numpy gaussian smoothing
# Super simple 1D smoothing with just numpy. Just smooths a few sharp edges.
import math
import numpy as np
def gaussian_kernel(n=5):
x = np.arange(n)/n
g = math.e ** -(x**2/((n - 2)**2)) # drop the constant factor at the start
return g/g.sum() # normalise so it sums to 1
@Multihuntr
Multihuntr / pytorch_tensor_to_image.py
Created September 2, 2019 03:35
One liner to save a GPU pytorch tensor to disk as an image using PIL
from PIL import Image
# Assumes tensor is shaped [c, h, w]
Image.fromarray(tensor.detach().cpu().numpy().transpose([1, 2, 0])).save('test.png')
# Assumes tensor is shaped [n, c, h, w]
Image.fromarray(tensor[0].detach().cpu().numpy().transpose([1, 2, 0])).save('test.png')
# Yeah, pretty simple, but annoying to remember...
@Multihuntr
Multihuntr / derive.py
Last active December 17, 2019 06:10
Decorator for lazy-loaded derived values
def derive(_from, _coll='_derived', name=None):
'''
Creates a decorator that caches derived values.
Utilises a property on the object to keep a collection of derived properties,
but requires the calling obj to manage that collection (clearing, instantiation, etc).
Args:
_from (str): Property this property is derived from
_coll (str, optional): Collection name of derived property names
name (str, optional): Overwrite the property used to cache
@Multihuntr
Multihuntr / multi_level_index.py
Created May 7, 2020 03:01
For flat indexing into a nested structure without flattening that structure.
import bisect
import numpy as np
import collections.abc
class TwoLevelIndex(collections.abc.Sequence):
def __init__(self, coll):
counts = [len(thing) for thing in coll]
self.cum_counts = np.cumsum(counts)
def __getitem__(self, idx):
seg_idx = bisect.bisect(self.cum_counts, idx)
@Multihuntr
Multihuntr / listlike.py
Created June 18, 2020 06:04
A list-like wrapper for dictionary-like objects
import collections
class ListLike(collections.abc.MutableSequence):
'''
A list-like interface wrapping dictionary-like objects.
Uses integer keys, like you would for a list, has range checking,
negative indexing and slicing working as you'd expect.
i.e.
with shelve.open('foo.dat') as sf:
@Multihuntr
Multihuntr / accumulate_grads_min.py
Created January 23, 2018 07:21
A minimal example of how you can accumulate gradients across batches, allowing you to train using much larger batch sizes than can fit in memory at the cost of speed.
import numpy as np
import tensorflow as tf
import sys
from tensorflow.examples.tutorials.mnist import input_data
n_pseudo_batches = int(sys.argv[1]) if len(sys.argv) > 1 else 128
actual_batch_size = int(sys.argv[2]) if len(sys.argv) > 2 else 32
iterations = int(sys.argv[3]) if len(sys.argv) > 3 else 10
@Multihuntr
Multihuntr / partitioned.py
Created February 4, 2021 02:47
Sqlalchemy create partitioned tables in a for loop in Postgresql database
# BIG NOTE:
# I made this becuase I thought the method was interesting. It's not useful in it's current form.
# If you can, use an index instead. https://stackoverflow.com/a/27895337
from sqlalchemy import Column, Integer, Float, ForeignKey, event, DDL
from sqlalchemy.schema import CreateSchema
from sqlalchemy.dialects.postgresql import DOUBLE_PRECISION
from sqlalchemy.ext.declarative import declarative_base, declared_attr
Base = declarative_base()