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
#!/usr/bin/python3 | |
from datetime import datetime | |
import psutil | |
DEFAULT_DRAGO_NICENESS_VALUE = 5 | |
DURATION_BEFORE_RENICING = 30 | |
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
""" | |
Performance comparison between ImageDataGenerator and tf.data.Dataset on a simple task | |
Dependencies available at https://www.github.com/sicara/tf2-evonorm | |
""" | |
import math | |
from pathlib import Path | |
import click | |
import tensorflow as tf |
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
policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16') | |
tf.keras.mixed_precision.experimental.set_policy(policy) |
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
# Define multi-gpu strategy | |
mirrored_strategy = tf.distribute.MirroredStrategy() | |
# Update batch size value | |
batch_size *= mirrored_strategy.num_replicas_in_sync | |
# Create strategy scope to perform training | |
with mirrored_strategy.scope(): | |
model = [...] | |
model.fit(...) |
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 pathlib import Path | |
import tensorflow as tf | |
def select_patch(sharp, blur, patch_size_x, patch_size_y): | |
""" | |
Select a patch on both sharp and blur images at the same localization. | |
Args: | |
sharp (tf.Tensor): Tensor for the sharp image | |
blur (tf.Tensor): Tensor for the blur image |
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 pathlib import Path | |
import tensorflow as tf | |
def select_patch(sharp, blur, patch_size_x, patch_size_y): | |
""" | |
Select a patch on both sharp and blur images at the same localization. | |
Args: | |
sharp (tf.Tensor): Tensor for the sharp image |
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 build_k_way_n_shot_dataset(annotations, n_shot, k_way, classes=None, to_categorical=True, training=True): | |
"""Build a dataset where each batch contains only N elements of K classes among all classes""" | |
# Prepare a dataframe with "image_path", "x1", "x2", "y1", "y2" columns | |
annotations = annotations.assign(label=pd.Categorical(annotations.label, categories=classes)) | |
# Prepare labels as one hot vectors | |
targets = annotations.label.cat.codes | |
if to_categorical: | |
targets = ( | |
pd.get_dummies(targets) |
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
with tf.GradientTape() as tape: | |
conv_outputs, predictions = grad_model(np.array([img])) | |
loss = predictions[:, CAT_CLASS_INDEX] | |
output = conv_outputs[0] | |
grads = tape.gradient(loss, conv_outputs)[0] | |
# Apply guided backpropagation | |
gate_f = tf.cast(output > 0, 'float32') | |
gate_r = tf.cast(grads > 0, 'float32') |
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 cv2 | |
import numpy as np | |
import tensorflow as tf | |
IMAGE_PATH = './cat.jpg' | |
LAYER_NAME = 'block5_conv3' | |
CAT_CLASS_INDEX = 281 | |
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(224, 224)) | |
img = tf.keras.preprocessing.image.img_to_array(img) |
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 cv2 | |
import numpy as np | |
import tensorflow as tf | |
IMAGE_PATH = './cat.jpg' | |
LAYER_NAME = 'block5_conv3' | |
CAT_CLASS_INDEX = 281 | |
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(224, 224)) | |
img = tf.keras.preprocessing.image.img_to_array(img) |
NewerOlder