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 PIL import Image | |
import io | |
import base64 | |
def encode_to_base64_str(img): | |
img = Image.fromarray(img) | |
with io.BytesIO() as f: | |
img.save(f, format='jpeg') | |
return base64.b64encode(f.getvalue()) | |
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 numpy as np | |
def softmax(xs, axis=-1): | |
xs = xs - np.max(xs, axis=axis, keepdims=True) | |
xs_exp = np.exp(xs) | |
return xs_exp / xs_exp.sum(axis=axis, keepdims=True) |
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
def index_dict_of_lists(d, idxs): | |
""" | |
Args: | |
d: dictionary of lists (or subdictionaries of lists) | |
idxs: list/array of indices | |
Returns: | |
Dictionary where all lists/arrays are indexed by idxs. | |
""" | |
idx = np.array(idxs) | |
subset_dict = dict() |
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
__all__ = ['collate_dicts'] | |
from typing import List, Dict, Any | |
from collections import defaultdict | |
def collate_dicts(ds: List[Dict[Any, Any]]) -> Dict[Any, List[Any]]: | |
""" | |
Args: | |
ds: A list of dictionarys all with the same keys. |
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 pandas as pd | |
from csv import DictReader | |
from collections import defaultdict | |
import numpy as np | |
def read_charades_csv(path): | |
def parse_action(action_str): | |
cls, start, stop = action_str.split(' ') | |
return { | |
'class': int(cls[1:]), | |
'start': float(start), |
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 matplotlib.pyplot as plt | |
import seaborn as sns | |
sns.set(context='paper', style='whitegrid') | |
def grid_show(grid, horizontal_labels, vertical_labels=None, cmap='binary', grid_color='#efefef'): | |
ax = plt.gca() | |
image = ax.imshow(grid, |
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 numpy as np | |
from PIL import Image | |
def vstack(images): | |
if len(images) == 0: | |
raise ValueError("Need 0 or more images") | |
if isinstance(images[0], np.ndarray): | |
images = [Image.fromarray(img) for img in images] | |
width = max([img.size[0] for img in images]) |
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
#!/bin/bash | |
set +ex | |
KEEP_LAST_N=2 | |
OLD_KERNELS=$(dpkg -l | grep -i 'linux-image-[[:digit:]]' | awk '{ print $2 }' | sort -rV | tail -n "+$(($KEEP_LAST_N + 1))") | |
if [ -n "$OLD_KERNELS" ]; then | |
apt-get -qy remove --purge $OLD_KERNELS | |
fi | |
apt-get -qy autoremove --purge |