Skip to content

Instantly share code, notes, and snippets.

Avatar
Solving environment /

Scott H. Hawley drscotthawley

Solving environment /
View GitHub Profile
@drscotthawley
drscotthawley / get_1cycle_schedule.py
Last active Jul 13, 2021
Implementation of 1cycle learning rate schedule, but without fast.ai
View get_1cycle_schedule.py
import numpy as np
def get_1cycle_schedule(lr_max=1e-3, n_data_points=8000, epochs=200, batch_size=40, verbose=0):
"""
Creates a look-up table of learning rates for 1cycle schedule with cosine annealing
See @sgugger's & @jeremyhoward's code in fastai library: https://github.com/fastai/fastai/blob/master/fastai/train.py
Wrote this to use with my Keras and (non-fastai-)PyTorch codes.
Note that in Keras, the LearningRateScheduler callback (https://keras.io/callbacks/#learningratescheduler) only operates once per epoch, not per batch
So see below for Keras callback
@drscotthawley
drscotthawley / webcam_nerverot.py
Created Jun 2, 2021
change webcam settings in realtime
View webcam_nerverot.py
#!/usr/bin/env python3
# Do violence to the Logitech C920s webcam settings in realtime
# To annoy your Zoom-mates by constantly changing the image
from pynput.keyboard import Listener
import os, sys
import random
import time
@drscotthawley
drscotthawley / kfold_swap.py
Last active Jan 25, 2021
Swaps Validation set with a section of Training set, given a value for k
View kfold_swap.py
# In case you didn't think to add k-fold cross-validation until late in your
# ML project,...
# This is built for a situation where datasets are arrays of, say, images.
def kfold_swap(train_X, train_Y, val_X, val_Y, k):
"""
Swaps val with a section of train, given a value for k
"Duct tape" approach used to "retro-fit" k-fold cross-validation while minimally
disturbing the rest of the code, while avoiding reloading data from disk and
keeping RAM use manageable. (e.g. np.append() is bad b/c it would copy all of train)
@drscotthawley
drscotthawley / oscilloscope.py
Last active Jan 14, 2021
Realtime oscilloscope in 20 lines of Python, via soundcard & OpenCV
View oscilloscope.py
import numpy as np
import cv2
import soundcard as sc # Get it from https://github.com/bastibe/SoundCard
imWidth, imHeight = 1024, 512 # screen size
def draw_wave(screen, mono_audio, xs, title="oscilloscope", gain=5):
screen *= 0 # clear the screen
ys = imHeight/2*(1 - np.clip( gain * mono_audio[0:len(xs)], -1, 1)) # the y-values of the waveform
pts = np.array(list(zip(xs,ys))).astype(np.int) # pair up xs & ys
cv2.polylines(screen,[pts],False,(0,255,0)) # connect points w/ lines
cv2.imshow(title, screen) # show what we've got
@drscotthawley
drscotthawley / greenscreen.py
Last active Jan 10, 2021
greenscreen.py: Greenscreen effect without a physical green screen, via OpenCV and Python
View greenscreen.py
#! /usr/bin/env python
'''
greenscreen.py: Greenscreen effect without a physical green screen
This performs background subtraction, and sets the background to "green" for use with "key frame" video editing software
Author: Scott Hawley, https://github.com/drscotthawley
Requirements:
Python, NumPy and OpenCV
I got these via Macports, but Homebrew, etc. work.
@drscotthawley
drscotthawley / grpc.patch
Last active Nov 4, 2020
Patch for GRPC to work with tensorflow, created from main grpc/ directory
View grpc.patch
diff --git a/src/core/lib/gpr/log_linux.cc b/src/core/lib/gpr/log_linux.cc
index 561276f0c2..1af0935e1f 100644
--- a/src/core/lib/gpr/log_linux.cc
+++ b/src/core/lib/gpr/log_linux.cc
@@ -40,7 +40,7 @@
#include <time.h>
#include <unistd.h>
-static long gettid(void) { return syscall(__NR_gettid); }
+static long sys_gettid(void) { return syscall(__NR_sys_gettid); }
@drscotthawley
drscotthawley / text_shortener.py
Last active Aug 27, 2020
Shorten text by applying various shortening rules
View text_shortener.py
#!/usr/bin/env python
# Replaces lengthy words/phrases with shorter variants
# Author: Scott Hawley
import pandas as pd
import re
import os
View beeman_gpu_pendula.py
#! /usr/bin/env python
'''
Simulation of a 'magnetic' pendulum. Actually we'll just use
electrostatic charges instead of magnets, but..good enough, right?
Plots an image showing which source the object is closest to after a certain time
(Note: Depending on params like maxiter, the object may still be moving at the
end of the simulation, so the 'ending position' may not be where it comes to rest.)
This code integrates all the (non-interacting) test objects at once, on the GPU.
View FastAICustomModelExample.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@drscotthawley
drscotthawley / newpost.py
Last active Jul 28, 2020
Little script I use to easily start a new Fastpages/Jekyll/Markdown blog entry
View newpost.py
#! /usr/bin/env python
# Little script I use to start a new Markdown blog entry.
# Run from the parent directory above the blog directory,
# or supply a full path to run from anywhere.
#
# Usage: newpost.py <title>
# It automatically figures out what the current date is to
# create a new entry, and supplies a default header with a title
# and a bibliography placement