This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def tf_ds_cutmix(ds, shuffling=1024): | |
ds_shuffled = ds.shuffle(shuffling) | |
def cutmix(p1, p2): | |
img_1, label_1 = p1 | |
img_2, label_2 = p2 | |
lambda_ = tf.random.uniform((1,)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow.keras.losses import categorical_crossentropy | |
@tf.function | |
def ohem_crossentropy_loss(y_true, y_pred): | |
# You can apply OHEM with any loss you want | |
# To do so, you just need to change the base loss below | |
cross_entropy = categorical_crossentropy(y_true, y_pred) | |
# You can tune how many examples are rejected by modifying the `80` value below |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let img = await webcam.capture(); | |
let rChannel = img.slice([0,0,0],[192,192,1]).reshape([192,192]); | |
let gChannel = img.slice([0,0,1],[192,192,1]).reshape([192,192]); | |
let bChannel = img.slice([0,0,2],[192,192,1]).reshape([192,192]); | |
img = tf.stack([bChannel, gChannel, rChannel], 2); | |
img = tf.mul(img, 1/255); | |
img = tf.add(img, -.5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.keras.models import Sequential | |
import tensorflow.keras.layers as keras_layers | |
clone = Sequential() | |
# Here, layer[1] is a model. | |
layers = [model.layers[0]] + model.layers[1].layers + model.layers[2:] | |
for layer in layers: | |
if str(type(layer)).endswith("InputLayer'>"): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from tqdm.notebook import tqdm | |
from shutil import copyfile | |
def split_dir(source_dir, target_dir_train, target_dir_test, ratio=.2): | |
files = os.listdir(source_dir) | |
to_test = int(len(files) * ratio + .5) | |
assert files[:to_test] + files[to_test:] == files | |
for file in files[:to_test]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Generator | |
import tensorflow as tf | |
def image_generator_to_tf_ds(generator: Generator) -> tf.data.Dataset: | |
"""Converts a initialized keras Image Data Generator to an equivalent tf Dataset. | |
Example usage: | |
>>> img_generator = ImageDataGenerator() | |
>>> img_generator = img_generator.flow_from_directory( |