Skip to content

Instantly share code, notes, and snippets.

View alkalait's full-sized avatar
🛰️

Freddie Kalaitzis alkalait

🛰️
View GitHub Profile
@alkalait
alkalait / layer_output_shape.py
Last active January 30, 2020 15:30
Output shape of a PyTorch layer
import torch # 1.3.1
import torch.nn as nn
import torchvision # 0.4.2
# for example
resnet = torchvision.models.resnet50(pretrained=False)
# strip away its FC layer
resnet_noFC = nn.Sequential(*list(resnet.children())[:-1])
# Suppose you want to use it for binary classification.
@alkalait
alkalait / shape.py
Created March 6, 2021 14:25
Yet another pretty shape print
import torch
import numpy
def shape(name : str) -> None:
'''
yaarr = torch.ones(25, 1, 1, 5, 5) # or np.ones
shape('yaarr')
>> yaarr : (25, 1, 1, 5, 5)
'''
val = eval(name)
@alkalait
alkalait / show.py
Last active July 18, 2021 19:12
show: pretty `imshow`s with commonly used arguments
# Author: Freddie Kalaitzis
# License: MIT
# Source: https://gist.github.com/alkalait/1497032fb601997efd9be4b90dddc63b
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import torch
import xarray as xr
sns.set_style('white')
@alkalait
alkalait / is_broadcastable.py
Created May 17, 2021 15:08
Are the shapes of two tensors compatible for broadcasting?
from torch import Tensor
def is_broadcastable(x: Tensor, y: Tensor) -> bool:
""" Are the shapes of two tensors compatible for broadcasting? """
if not x.ndim == y.ndim:
return False
n_same_dim = (torch.as_tensor(x.shape) == torch.as_tensor(y.shape)).sum()
return (int(n_same_dim) - x.ndim) <= 1
@alkalait
alkalait / SN7Dataset.py
Last active June 3, 2021 19:33
PyTorch SpaceNet7 Dataset
# Author: Freddie Kalaitzis
# License: MIT
# Source: https://gist.github.com/alkalait/c99213c164df691b5e37cd96d5ab9ab2
import functools
import itertools
import os
import re
import warnings
@alkalait
alkalait / revisitconv2d.py
Created December 1, 2021 15:15
Minimal ways to learn with multiple revisits
# batch, time, bands, height, width
B, T, C, H, W = x.shape
C_o = 32 # hyperparam
F = lambda i, o, g=1:
nn.Conv2d(in_channels=i,
out_channels=o,
kernel_size=3,
padding='same',
groups=g)