This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: sb3 | |
channels: | |
- pytorch | |
- nvidia | |
- conda-forge | |
dependencies: | |
- diffusers | |
- transformers | |
- pytorch | |
- torchvision |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Utility functions for managing 3x3 matrices for cv2.warpAffine in pure numpy | |
import numpy as np | |
def identity(): | |
return np.eye(3, dtype=np.float64) | |
def affine(A=None, t=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reimplementation of: https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/object_detection/metrics/mean_avg_precision.py | |
# Now with more vectorisation! | |
def precision_recall_curve_th(is_tp, confs, n_true, eps=1e-8): | |
# Sort by confs | |
order = (-confs).argsort() | |
is_tp = is_tp[order] | |
confs = confs[order] | |
# Cumulative sum true positives and number of predictions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I keep properties on my main nn.Modules. e.g. a list of the training statistics the model is tracking. | |
# I wanted to perform a set of extra actions across multiple different modules without having to | |
# - write those steps into each of the 5+ different model definitions, or | |
# - explicitly expose those values on the wrapper module. | |
# It's fairly trivial, but if you don't use the try: super(), it doesn't keep the `wrapped` property. | |
import torch | |
import torch.nn as nn | |
class Wrapper(nn.Module): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
NewerOlder