Skip to content

Instantly share code, notes, and snippets.

@avmoldovan
avmoldovan / tf_ring_buffer.py
Created June 12, 2020 18:47 — forked from louiskirsch/tf_ring_buffer.py
A tensorflow ring buffer implementation
class RingBuffer:
def __init__(self, scope_name, components, size):
"""
Create a new ring buffer of size `size`.
Each item in the ring buffer is a tuple of variables of size `len(components)`.
:param scope_name: A scope name for the newly created variables
:param components: Defines the type of items in the buffer. An iterable of tuples (name: str, shape: Iterable, dtype)
:param size: The maximum size of the buffer
@avmoldovan
avmoldovan / hook_activations.py
Created June 9, 2020 20:29 — forked from Tushar-N/hook_activations.py
Pytorch code to save activations for specific layers over an entire dataset
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as tmodels
from functools import partial
import collections
# dummy data: 10 batches of images with batch size 16
dataset = [torch.rand(16,3,224,224).cuda() for _ in range(10)]
@avmoldovan
avmoldovan / memory_strided_im2col.py
Last active January 15, 2020 09:29
Memory strided im2col
#from https://gist.githubusercontent.com/anirudhshenoy/5d99c087f0943b1306bbfedf4cc879a0/raw/2fe0cbbd4d70157db39a8eea1cefe77d960f4b06/memory_strided_im2col.py
from skimage.util.shape import view_as_windows
def memory_strided_im2col(x, kernel):
output_shape = (x.shape[0] - kernel.shape[0]) + 1
return view_as_windows(x, kernel.shape).reshape(output_shape*output_shape, kernel.shape[0]*2)
#view_as_windows has an additional step parameter that can be used with different strides
### usage:
# input_matrix = np.array([[3,9,0], [2, 8, 1], [1,4,8]])
#from https://gist.githubusercontent.com/anirudhshenoy/d4e4228ebb884066efd65299f1d169cb/raw/bbe787ecbcf014e6e64e0ee8b9217923167c626d/naive_im2col.py
def im2col(x, kernel):
kernel_shape = kernel.shape[0]
rows = []
# Assuming Padding = 0, stride = 1
for row in range(x.shape[0] - 1):
for col in range(x.shape[1] - 1):
window = x[row: row + kernel_shape, col: col + kernel_shape]
rows.append(window.flatten())
@avmoldovan
avmoldovan / conv_2d.py
Created January 15, 2020 08:38
Naive Conv2D implementation
#from here https://gist.githubusercontent.com/anirudhshenoy/4d5e579d3585159f133ca7804cdca25a/raw/c7314527d27bc70ab38b061b5e33b086704252e0/conv_2d.py
def conv_2d(x, kernel, bias):
kernel_shape = kernel.shape[0]
# Assuming Padding = 0, stride = 1
output_shape = x.shape[0] - kernel_shape + 1
result = np.zeros((output_shape, output_shape))
for row in range(x.shape[0] - 1):
for col in range(x.shape[1] - 1):
@avmoldovan
avmoldovan / feature_visualizer.py
Last active January 10, 2020 22:47
Pytorch feature visualizer
#copy: https://towardsdatascience.com/how-to-visualize-convolutional-features-in-40-lines-of-code-70b7d87b0030
from fastai.conv_learner import *
from cv2 import resize
%matplotlib inline
class SaveFeatures():
def __init__(self, module):
self.hook = module.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output):
@avmoldovan
avmoldovan / conv_output.py
Last active April 18, 2020 15:45
Pytorch convolution output
#shamelessly copied from here https://discuss.pytorch.org/t/utility-function-for-calculating-the-shape-of-a-conv-output/11173/7
# original docs at https://pytorch.org/docs/master/nn.html#conv2d
import math
def num2tuple(num):
return num if isinstance(num, tuple) else (num, num)
def conv2d_output_shape(h_w, kernel_size=1, stride=1, pad=0, dilation=1):
h_w, kernel_size, stride, pad, dilation = num2tuple(h_w), \