Skip to content

Instantly share code, notes, and snippets.

@usr-ein
usr-ein / luhn_checker.py
Created April 11, 2021 17:33
Checks for the validity of numbers following the Luhn algorithm
#!/usr/bin/env python3
"""
Checks for the validity of numbers following the Luhn algorithm
Samuel Prevost 2021-04-01
"""
import os
import sys
import numpy as np
@usr-ein
usr-ein / path_type.py
Last active June 19, 2021 15:01
Path type for use with `argparse`
from typing import Callable, Union
from enum import Enum
from argparse import ArgumentTypeError as ArgumentError
import os
class ValType(Enum):
ANY = 0
FILE = 1
DIR = 2
@usr-ein
usr-ein / pshape.py
Last active August 5, 2021 10:17
Prints a NumPy-like array's shape, as well as the name of its input variable outside the functions' scope
# Pshape is now a fully fledged Python library !
# Check it out at https://github.com/sam1902/pshape
# and install it anywhere with
#
# pip3 install pshape
import inspect, re
def pshape(arr):
frame = inspect.currentframe()
@usr-ein
usr-ein / main.py
Created May 9, 2021 12:41
Train EfficientNet on ImageNet
# Samuel Prevost 2021-02-25
# Released under GNU General Public License v3.0
import os
import functools
from datetime import datetime
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
@usr-ein
usr-ein / receptive_field_calc.py
Created May 16, 2021 12:38
Compute the receptive field of a CNN from any starting depth
#!/usr/bin/env python3
# Distributed under GNU GPLv3
"""Compute the receptive field of a CNN from any starting depth"""
import numpy as np
import torch
from torch import nn
from numba import njit
from numba.types import uint64
@usr-ein
usr-ein / view.py
Created June 3, 2021 14:27
View numpy arrays images side by side in Jupyter
from PIL import Image
import io
from ipywidgets import widgets
import IPython.display as display
def view(*arrs):
ims = []
for arr in arrs:
output = io.BytesIO()
im = Image.fromarray(arr).resize((500,500), Image.NEAREST)
@usr-ein
usr-ein / avgres.sh
Last active June 28, 2021 13:08
Average Resolution - Computes the average, min and max height/width of all images passed as arguments
#!/usr/bin/env bash
if [ "$#" -lt 1 ] || [ ! -f "$1" ]; then
echo "Usage: $0 1.jpg 2.jpg ..."
exit 1
fi
sizes=$(identify -format "%h %w\n" "$@" | awk -F ' ' '(NR==1){minh=$1;minw=$2;maxh=$1;maxw=$2}; (NR>0){if(minh>$1) minh=$1; if(maxh<$1) maxh=$1;if(minw>$2) minw=$2; if(maxw<$2) maxw=$2;}; {height += $1} {width += $2} END {print height/NR; print width/NR; print maxh; print minh; print maxw; print minw;}')
h=$(echo "$sizes" | sed -n 1p)
@usr-ein
usr-ein / wget_gdrive.sh
Last active June 29, 2021 08:15
wget any public file from Google Drive
wgetGdrive() {
if [ $# -lt 2 ]; then
echo "Usage $0 FILE_ID filename"
echo "FILE_ID looks like 1UkzAZMBG1U8kTqO3yhO2nTtoRNtEvyRq"
exit 1
fi
# This is the proper way to create temp files
trap 'rm -f "$TMPFILE"' EXIT
COOKIES=$(mktemp) || exit 1
@usr-ein
usr-ein / test_pytorch.py
Created June 30, 2021 14:41
Test if different DL frameworks work well with your current CUDA installation
#!/usr/bin/env python3
"""Tests PyTorch CUDA capabilities"""
import sys
import torch
def main():
"""Main function"""
@usr-ein
usr-ein / reinforce.py
Created July 23, 2021 15:45
REINFORCE algorithm in PyTorch
import argparse
import gym
import numpy as np
from itertools import count
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical