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
// 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) { |
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 logging | |
import cv2 | |
import numpy as np | |
import vlogging | |
logger = logging.getLogger("demo") | |
fh = logging.FileHandler('test.html', mode="w") | |
logger.setLevel(logging.DEBUG) |
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 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 |
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 numpy as np | |
import tensorflow as tf | |
# Layer name to inspect | |
layer_name = 'block3_conv1' | |
epochs = 100 | |
step_size = 1. | |
filter_index = 0 |
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 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 |
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
@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"}): |
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) |
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
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) |
OlderNewer