Skip to content

Instantly share code, notes, and snippets.

@kevinzakka
kevinzakka / spatial_softmax.py
Last active September 12, 2023 08:55
Pytorch Spatial Soft Argmax
import torch
import torch.nn as nn
import torch.nn.functional as F
class SpatialSoftArgmax(nn.Module):
"""Spatial softmax as defined in [1].
Concretely, the spatial softmax of each feature
map is used to compute a weighted mean of the pixel
@kevinzakka
kevinzakka / time.py
Last active October 7, 2019 20:47
Matrix Exponential: Padé vs. Closed Form
import time
import numpy as np
from scipy.linalg import expm
def angvel2skewsym(w):
"""Converts an angular velocity to a skew-symmetric representation.
"""
return np.array([
[0, -w[2], w[1]],
@kevinzakka
kevinzakka / rot.py
Last active April 11, 2019 00:58
Rotate a Tensor in PyTorch
import numpy as np
import torch
import torch.nn.functional as F
def rotz(theta):
return np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1],
@kevinzakka
kevinzakka / data_loader.py
Last active March 19, 2024 05:15
Train, Validation and Test Split for torchvision Datasets
"""
Create train, valid, test iterators for CIFAR-10 [1].
Easily extended to MNIST, CIFAR-100 and Imagenet.
[1]: https://discuss.pytorch.org/t/feedback-on-pytorch-for-kaggle-competitions/2252/4
"""
import torch
import numpy as np