Skip to content

Instantly share code, notes, and snippets.

View drscotthawley's full-sized avatar
Solving environment /

Scott H. Hawley drscotthawley

Solving environment /
View GitHub Profile
@drscotthawley
drscotthawley / kfold_swap.py
Last active January 25, 2021 15:33
Swaps Validation set with a section of Training set, given a value for k
# 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 / webcam_nerverot.py
Created June 2, 2021 19:38
change webcam settings in realtime
#!/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 / grpc.patch
Last active October 19, 2021 04:36
Patch for GRPC to work with tensorflow, created from main grpc/ directory
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 / Tabular_Spotify.drawio.svg
Created November 8, 2021 23:39
my svg file of tabular data model
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@drscotthawley
drscotthawley / greenscreen.py
Last active December 10, 2021 07:21
greenscreen.py: Greenscreen effect without a physical green screen, via OpenCV and Python
#! /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 / get_1cycle_schedule.py
Last active May 31, 2022 16:51
Implementation of 1cycle learning rate schedule, but without fast.ai
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 / usagebot.py
Last active October 1, 2022 22:12
SLURM cluster usage tracker Discord bot
#! /usr/bin/env python3
"""
SLURM usage tracker Discord bot by drscotthawley & rom1504
Requires external file token_channel.csv to connect to Discord
Syntax of that file should be:
token,channel
<DISCORD_BOT_TOKEN>,<CHANNEL_ID>
@drscotthawley
drscotthawley / magic_mult.py
Last active October 4, 2022 12:12
Tries to multiply two arrays/matrices in a variety of ways; returns what "works"
def magic_mult(a, b):
"""
Tries to multiply two arrays/matrices in a variety of ways
Returns all possible working combos as a dict, with the shapes of their respective outputs
Author: Scott H. Hawley, @drscotthawley
"""
combos = ['a*b', 'a*b.T', 'a.T*b', 'a.T*b.T','b*a', 'b*a.T', 'b.T*a', 'b.T*a.T'] # elementwise multiplications
combos += [s.replace('*',' @ ') for s in combos] # matrix multiplications (I like the space here)
working_combos = {}
for s in combos:
@drscotthawley
drscotthawley / Gamepad_Eyes_OSC.pde
Last active October 4, 2022 18:25
"Gamepad" Input example for Wekinator, e.g. for Xbox 360 controller
// Gamepad_Eyes_OSC: demo of using game controller for OSC input.
// Uses the two thumb-sticks and either the left or right bumper buttons.
//
// Peter Lager maintains the Game Control Plus library (for Processing),
// which provides control data for joysticks and other game controllers
// ...such as my Xbox 360 controller.
//
// This is a quick mash-up using Lager's Gcp_gamepad animated eyes example code,
// (which is in the GCP library: In Processing, go to File > Examples..., then Contributed Libraries > Game Control Plus)
// and Rebecca Fiebrink's Simple_Mouse_DraggedObject_2Inputs example code for Wekinator.
@drscotthawley
drscotthawley / rearrangewrapper.py
Last active October 30, 2022 01:52
Wrapper to give einops.rearrange an "inverse"
from einops import rearrange as _rearrange
class RearrangeWrapper():
"wrapper to endow einops.rearrange with an 'inverse' operation"
def __init__(self):
self.shape, self.s = None, None # just in case someone tries to call inverse first
def __call__(self, x, s:str, **kwargs): # this 'forward' call is lightweight to preserve original usage
self.shape, self.s = x.shape, s
return _rearrange(x, s, **kwargs)