Skip to content

Instantly share code, notes, and snippets.

View RaphaelMeudec's full-sized avatar

Raphael Meudec RaphaelMeudec

View GitHub Profile
@RaphaelMeudec
RaphaelMeudec / workplaceVideos.js
Created February 27, 2018 18:35
Extract all videos from Workplace and download it as csv
// Add replaceAll method to string to remove new line
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
// Add a console.save function
(function(console){
console.save = function(data, filename){
if(!data) {
@RaphaelMeudec
RaphaelMeudec / activations_visualization.py
Last active July 18, 2019 08:56
Visualize outputs of activations with Tensorflow 2.0
import numpy as np
import tensorflow as tf
layers_name = ['activation_1']
IMAGE_PATH = './cat.jpg'
# Model to examine
model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', include_top=True)
# Image to pass as input
@RaphaelMeudec
RaphaelMeudec / register_gradient.py
Last active July 18, 2019 14:56
How to register gradient with TF2
@tf.RegisterGradient("GuidedRelu")
def _GuidedReluGrad(op, grad):
gate_f = tf.cast(op.outputs[0] > 0, "float32") # Filter must be activated
gate_R = tf.cast(grad > 0, "float32") # Grads must be positive
return gate_f * gate_R * grad
with tf.Graph().as_default() as g:
model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', include_top=True)
with g.gradient_override_map({"Relu": "GuidedRelu"}):
@RaphaelMeudec
RaphaelMeudec / grad_cam_no_guided_backprop.py
Last active July 18, 2019 15:10
Grad CAM (without Guided Backpropagation) with Tensorflow 2
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)
@RaphaelMeudec
RaphaelMeudec / visual_logging.py
Last active July 19, 2019 14:47
Image logging with the VisualLogging library
import logging
import cv2
import numpy as np
import vlogging
logger = logging.getLogger("demo")
fh = logging.FileHandler('test.html', mode="w")
logger.setLevel(logging.DEBUG)
@RaphaelMeudec
RaphaelMeudec / k_way_n_shots_dataset.py
Created January 14, 2020 14:56
How to create a k-way n-shot tf.data.Dataset
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)
@RaphaelMeudec
RaphaelMeudec / optimized_tensorflow_dataset_for_image.py
Created January 16, 2020 11:17
Create an optimized version of tf.data Dataset for an image deblurring task
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
@RaphaelMeudec
RaphaelMeudec / mixed_preciision.py
Created January 16, 2020 16:07
Lines to turn float32 into mixed precision training
policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
tf.keras.mixed_precision.experimental.set_policy(policy)
@RaphaelMeudec
RaphaelMeudec / guided_backprop.py
Created July 18, 2019 15:12
Guided Backpropagation at inference time with Tensorflow 2
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')
# 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(...)