Skip to content

Instantly share code, notes, and snippets.

View markloyman's full-sized avatar

Mark Loyman markloyman

  • Israel
View GitHub Profile
@markloyman
markloyman / hubness.py
Created May 2, 2020 11:13
Hubness calculation (index defined by Miloš Radovanovi´c et al, 2010)
import numpy as np
from scipy.stats import skew
def k_occurrences(nbrs_indices):
"""
Calculate k-occurrences distribution
Miloš Radovanovi´c et al,
Hubs in space: Popular nearest neighbors in high-dimensional data.
Journal of Machine Learning Research, 11(Sep):24872531, 2010
@markloyman
markloyman / pearson_loss.py
Created May 2, 2020 10:15
Keras Pearson Correlation Loss
def l2_distance_matrix(x, exact=True):
"""
Calculate an L2 distance matrix from embedding vectors)
:param
x: embedding tensor size (batch_size, embedding_length)
exact: if False, skip the final sqrt
:return: l2 distance matrix tensor tensor size (batch_size, batch_size)
"""
r = K.sum(x*x, 1)
r = K.reshape(r, [-1, 1]) # turn r into column vector
@markloyman
markloyman / cost_volume_viz.py
Last active October 23, 2018 06:58
Cost Volume Visualization (for stereo depth estimation debuging and exploration)
import numpy as np
import matplotlib.pyplot as plt
from volume_viz import VolumeViz
class CostVolumeViz:
def __init__(self, left, right, disparity):
self.left = left
self.right = right
self.disparity = self.StandardizeDisparity(disparity)
@markloyman
markloyman / volume_viz.py
Created September 21, 2018 09:04
prototype for interactive 3d volume visualization
import numpy as np
import matplotlib.pyplot as plt
class VolumeViz:
def __init__(self, fig, volume):
self.volume = volume
self.figure = fig
self.ax1 = fig.add_subplot(121)
self.ax2 = fig.add_subplot(122)
self.ax1.set_title('click')
@markloyman
markloyman / runtime_stats_keras.py
Created September 5, 2018 12:17
Use TensorBoard's runtime statistcs with a Keras model
import numpy as np
import tensorflow as tf
sess = tf.Session()
from keras import backend as K
K.set_session(sess)
from keras.objectives import mean_squared_error
from keras.layers import Dense
def load_dummy_data(n = 1000):
@markloyman
markloyman / triplet_loss.py
Created January 23, 2018 20:50
Triplet Loss
def triplet_loss(_, y_pred, triplet_margin=1):
'''
Assume: y_pred shape is (batch_size, 2)
Example for how to construct such a corresponding triplet network:
def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
@markloyman
markloyman / crop_random_with_mask.py
Created September 28, 2017 19:58
Random crop of an image and it's mask (crop is aware of mask region)
def crop(image, mask, min_size= 128):
# Determine New Size on Y axis
max_size_y = image.shape[0]
mask_y = np.where(np.sum(mask, axis=1))
min_size_y = np.max(mask_y) - np.min(mask_y)
new_size_y = np.random.rand() * (max_size_y - min_size_y)
new_size_y = np.maximum(new_size_y, min_size).astype('uint16')
# Determine New Size on X axis
max_size_x = image.shape[1]
mask_x = np.where(np.sum(mask, axis=0))