Skip to content

Instantly share code, notes, and snippets.

@madebyollin
madebyollin / make_audiobook.py
Last active March 23, 2024 17:17
Converts an epub or text file to audiobook via Google Cloud TTS
#!/usr/bin/env python3
"""
To use:
1. install/set-up the google cloud api and dependencies listed on https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/texttospeech/cloud-client
2. install pandoc and pypandoc, also tqdm
3. create and download a service_account.json ("Service account key") from https://console.cloud.google.com/apis/credentials
4. run GOOGLE_APPLICATION_CREDENTIALS=service_account.json python make_audiobook.py book_name.epub
"""
import re
import sys
#!/usr/bin/env python
import numpy as np
import cv2
from scipy.signal import convolve2d
from skimage import color, data, restoration
import console
# read input
frame = cv2.imread("input.jpg").astype(np.float32) / 255.0
#!/usr/bin/env python
filename = "output.txt"
with open(filename, 'w') as fout:
x = 0
for x in range(5):
fout.write(""" def function_number{}(): \n
""".format(x))
@madebyollin
madebyollin / compressor.js
Created August 24, 2018 00:59
Snippet to add an audio compressor to all videos on the current page
function addCompressor(el) {
var AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var source = context.createMediaElementSource(el);
var compressor = context.createDynamicsCompressor();
var makeupGain = context.createGain();
// Set compressor params here
compressor.threshold.value = -40;
compressor.knee.value = 40;
@madebyollin
madebyollin / stable_diffusion_m1.py
Last active February 10, 2024 02:25
Stable Diffusion on Apple Silicon GPUs via CoreML; 2s / step on M1 Pro
# ------------------------------------------------------------------
# EDIT: I eventually found a faster way to run SD on macOS, via MPSGraph (~0.8s / step on M1 Pro):
# https://github.com/madebyollin/maple-diffusion
# The original CoreML-related code & discussion is preserved below :)
# ------------------------------------------------------------------
# you too can run stable diffusion on the apple silicon GPU (no ANE sadly)
#
# quick test portraits (each took 50 steps x 2s / step ~= 100s on my M1 Pro):
# * https://i.imgur.com/5ywISvm.png
@madebyollin
madebyollin / list_of_good_image_generator_training_logs.md
Last active May 2, 2024 14:28
List of good image generator training logs

List of good image generator training logs

A list of public training logs from neural network image generation models, since I think they're interesting.

The Criteria

  • Publicly accessible link
  • Losses plotted every so often
  • Samples generated every so often
  • Nontrivial dataset (i.e. not MNIST - 64x64 output RGB or better)

Variational Autoencoders Will Never Work

So you want to generate images with neural networks. You're in luck! VAEs are here to save the day. They're simple to implement, they generate images in one inference step (unlike those awful slow autoregressive models) and (most importantly) VAEs are 🚀🎉🎂🥳 theoretically grounded 🚀🎉🎂🥳 (unlike those scary GANs - don't look at the GANs)!

The idea

The idea of VAE is so simple, even an AI chatbot could explain it:

  1. Your goal is to train a "decoder" neural network that consumes blobs of random noise from a fixed distribution (like torch.randn(1024)), interprets that noise as decisions about what to generate, and produces corresponding real-looking images. You want to train this network with nice simple image-space MSE loss against your dataset of real images.
@madebyollin
madebyollin / nearest_neighbor_upsample_bf16.py
Created July 16, 2023 15:00
bfloat16 nearest neighbor upsample code, for when F.interpolate / nn.Upsample don't work
# PyTorch <=2.0 doesn't support bfloat16 F.interpolate natively.
# so, we have to do things the old fashioned way.
import torch
import torch.nn as nn
# functional implementation
def nearest_neighbor_upsample(x: torch.Tensor, scale_factor: int):
"""Upsample {x} (NCHW) by scale factor {scale_factor} using nearest neighbor interpolation."""
s = scale_factor
@madebyollin
madebyollin / compare_safetensors.py
Created July 29, 2023 02:36
script for comparing the contents of safetensors files
#!/usr/bin/env python3
from pathlib import Path
from safetensors.torch import load_file
def summarize_tensor(x):
if x is None:
return "None"
x = x.float()
return f"({x.min().item():.3f}, {x.mean().item():.3f}, {x.max().item():.3f})"
@madebyollin
madebyollin / model_activation_printer.py
Created July 30, 2023 17:47
Helper for logging output activation-map statistics for a PyTorch module, using forward hooks
def summarize_tensor(x):
return f"\033[34m{str(tuple(x.shape)).ljust(24)}\033[0m (\033[31mmin {x.min().item():+.4f}\033[0m / \033[32mmean {x.mean().item():+.4f}\033[0m / \033[33mmax {x.max().item():+.4f}\033[0m)"
class ModelActivationPrinter:
def __init__(self, module, submodules_to_log):
self.id_to_name = {
id(module): str(name) for name, module in module.named_modules()
}
self.submodules = submodules_to_log