Skip to content

Instantly share code, notes, and snippets.

View ncullen93's full-sized avatar

Nicholas Cullen, PhD ncullen93

View GitHub Profile
import torch as th
from torch.autograd import Variable
from torch.autograd.function import Function
class Symeig(Function):
def __init__(self, eigenvectors=True, upper=True):
self.eigenvectors = eigenvectors
self.upper = upper
@ncullen93
ncullen93 / multisampler.py
Created April 27, 2017 01:22
MultiSampler for Pytorch
class MultiSampler(Sampler):
"""Samples elements more than once in a single pass through the data.
This allows the number of samples per epoch to be larger than the number
of samples itself, which can be useful for data augmentation.
"""
def __init__(self, nb_samples, desired_samples, shuffle=False):
self.data_samples = nb_samples
self.desired_samples = desired_samples
self.shuffle
def pearsonr(x, y):
"""
Mimics `scipy.stats.pearsonr`
Arguments
---------
x : 1D torch.Tensor
y : 1D torch.Tensor
Returns
"""
Custom datasets from both in-memory and out-of-memory data
"""
import torch.utils.data as data
from PIL import Image
import os
import os.path
"""
Affine transforms implemented on torch tensors, and
only requiring one interpolation
Included:
- Affine()
- AffineCompose()
- Rotation()
- Translation()
- Shear()
def transform_matrix_offset_center(matrix, x, y):
o_x = float(x) / 2 + 0.5
o_y = float(y) / 2 + 0.5
offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
return transform_matrix
def apply_transform(x, transform_matrix, channel_axis=2, fill_mode='nearest', fill_value=0.):
x = np.rollaxis(x, channel_axis, 0)