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
#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), \ |
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
#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): |
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 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): |
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 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()) |
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 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]]) |
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 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)] |
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
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 |