Skip to content

Instantly share code, notes, and snippets.

@bkj
bkj / ablr.py
Last active January 19, 2024 23:37
#!/usr/bin/env python
"""
ablr.py
"""
import numpy as np
import torch
from torch import nn
#!/usr/bin/env python
"""
pytorch_dlib_metric_loss.py
"""
import torch
import torch.nn as nn
from torch.autograd import Variable
@bkj
bkj / pytorch_lifted_loss.py
Last active April 28, 2022 07:12
pytorch lifted loss
#!/usr/bin/env python
"""
pytorch_lifted_loss.py
"""
import torch
import torch.nn as nn
from torch.autograd import Variable
import h5py
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
class H5Dataset(Dataset):
def __init__(self, h5_path):
self.h5_path = h5_path
self.h5_file = h5py.File(h5_path, 'r')
function download_google_drive {
SRC=$1
DST=$2
echo "$SRC -> $DST"
curl -c /tmp/cookies "https://drive.google.com/uc?export=download&id=$SRC" > /tmp/intermezzo.html
DL_LINK=$(cat /tmp/intermezzo.html |\
grep -Po 'uc-download-link" [^>]* href="\K[^"]*' |\
sed 's/\&/\&/g'
)
import numpy as np
from numba import njit, parallel
@njit(parallel=True)
def cross_exchange(route1, route2, dist):
best_savings = -np.inf
for offset11 in prange(len(route1) - 3):
i1, i2 = route1[offset11], route1[offset11 + 1]
dii = dist[i1, i2]
@bkj
bkj / ee.py
Created February 12, 2020 16:39
import ee
import urllib
import numpy as np
from skimage import io
from PIL import Image
from zipfile import ZipFile
from matplotlib import pyplot as plt
ee.Initialize()
@bkj
bkj / fast_argmax.py
Created November 12, 2019 16:42
fast_argmax.py
#!/usr/bin/env python
"""
fast_argmax.py
"""
import numpy as np
from time import time
from numba import jit, prange
#!/usr/bin/env python
"""
prep-CUB200.py
"""
import os
import shutil
import pandas as pd
from tqdm import tqdm
def rle2mask(rle, height, width):
rle = [int(xx) for xx in rle.split(' ')]
offsets, runs = rle[0::2], rle[1::2]
tmp = np.zeros(height * width, dtype=np.uint8)
for offset, run in zip(offsets, runs):
tmp[offset:offset + run] = 1
return tmp.reshape(width, height).T