Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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