Skip to content

Instantly share code, notes, and snippets.

View RaphaelMeudec's full-sized avatar

Raphael Meudec RaphaelMeudec

View GitHub Profile
@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 / occlusion_sensitivity.py
Created July 18, 2019 08:55
Perform occlusion sensitivity with Tensorflow 2.0
import numpy as np
import tensorflow as tf
# Create function to apply a grey patch on an image
def apply_grey_patch(image, top_left_x, top_left_y, patch_size):
patched_image = np.array(image, copy=True)
patched_image[top_left_y:top_left_y + patch_size, top_left_x:top_left_x + patch_size, :] = 127.5
return patched_image
@RaphaelMeudec
RaphaelMeudec / kernel_visualization.py
Last active May 14, 2020 18:44
Visualize convolutional kernels with Tensorflow 2.0
import numpy as np
import tensorflow as tf
# Layer name to inspect
layer_name = 'block3_conv1'
epochs = 100
step_size = 1.
filter_index = 0
@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 / 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 / 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) {