Skip to content

Instantly share code, notes, and snippets.

@kevinzakka
kevinzakka / spatial_softmax.py
Last active November 6, 2024 10:31
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 / data_loader.py
Last active August 6, 2025 12:37
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