Skip to content

Instantly share code, notes, and snippets.

@stengoes
stengoes / cosine_decay_with_warmup.py
Created December 21, 2021 07:43
CosineDecayWithWarmup
class CosineDecayWithWarmup(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(
self,
warmup_steps,
total_steps,
base_lr=0.001,
):
super(CosineDecayWithWarmup, self).__init__()
self.warmup_steps = warmup_steps
@stengoes
stengoes / models.py
Last active November 23, 2020 10:08
Comparison of defining models
import tensorflow as tf
# Reset states (to be sure)
tf.keras.backend.clear_session()
# Method 1
x = tf.keras.Input(shape=(224, 224, 3), dtype=tf.uint8)
c = tf.keras.layers.Flatten()(x)
y = tf.keras.layers.Dense(units=2, activation="softmax")(c)
model1 = tf.keras.Model(inputs=x, outputs=y)
@stengoes
stengoes / image_pad_or_crop.js
Created October 5, 2020 10:02
image_pad_or_crop
function image_pad_or_crop(image, output_height, output_width) {
// Pads or crops an image so that the height and width dimensions match output_height and output_width.
// This function is compatible with tf.image.resize_with_crop_or_pad(image, target_height, target_width)
// Validate inputs
const image_size = image.height * image.width * image.depth;
if (image.data.length != image_size) {
console.log("Error: image dimensions do not match rgba buffer size!");
return null;
}
@stengoes
stengoes / zeropad.js
Last active August 31, 2020 13:32
Pads an image with a specified amount of zeros on the top, left, bottom and right.
function zeropad(image, top, bottom, left, right) {
// Pads an image with a specified amount of zeros on the top, left, bottom and right.
// Validate inputs
const image_size = image.height * image.width * image.depth;
if (image.data.length != image_size) {
console.log("Error: image dimensions do not match rgba buffer size!");
return null;
}
if (image.data.constructor !== Uint8ClampedArray && image.data.constructor !== Float32Array) {
@stengoes
stengoes / example_static_library_in_c++.md
Last active July 4, 2020 17:51
A minimal example for creating, compiling and using a static library in C++.

A minimal example for creating, compiling and using a static library in C++.

1. Create a test library

// test.h
#pragma once 
int add(int a, int b);
int mult(int a, int b);
@stengoes
stengoes / example_dynamic_library_in_c++.md
Last active July 4, 2020 17:51
A minimal example for creating, compiling and using a dynamic library in C++.

A minimal example for creating, compiling and using a dynamic library in C++.

1. Create a test library

// test.h
#pragma once 
int add(int a, int b);
int mult(int a, int b);
@stengoes
stengoes / balance.js
Last active June 9, 2020 10:40
Balance recordings
function distribute_recordings_over_lines(num_recordings, distribution, lines) {
// This function distributes number of recordings optimally
// given the current distribution and the lines on which you want to record.
function find_level(n, distribution, lines, low, high) {
// Compute the level
const level = (low + high) / 2;
// Compute search statistic
let s = 0;
@stengoes
stengoes / resize.js
Created March 2, 2020 09:22
resizeBicubic
function resizeBicubic(image, newHeight, newWidth) {
/* Helper functions */
function extrapolateImageBoundaries(image) {
const height = image.length;
const width = image[0].length;
// Linearly extrapolate top and bottom of the image
image[-2] = [];
@stengoes
stengoes / demosaic.js
Last active February 17, 2020 14:04
Demosaic images with Uint8ClampedArray
function demosaic(raw) {
// Demosaics a Color Filter Array (CFA) into a RGBA color array.
// For implementation details see: http://research.microsoft.com/pubs/102068/demosaicing_icassp04.pdf
// Validate inputs
const raw_size = raw.height * raw.width * raw.depth;
if (raw.data.length != raw_size) {
console.log("Error: raw image dimensions do not match raw buffer size!");
return null;
}
@stengoes
stengoes / index.html
Created February 13, 2020 08:37
Example using html5 canvas tags to display rgb arrays.
<html>
<body>
<canvas id="mycanvas" width="2048" height="512"></canvas>
</body>
</html>
<script>
const height = 512;