Skip to content

Instantly share code, notes, and snippets.

@wentongli
Forked from kylemcdonald/Triplet Loss.ipynb
Created May 25, 2018 17:38
Show Gist options
  • Select an option

  • Save wentongli/70ec450d279ffa3badfcbee44ff9fd1c to your computer and use it in GitHub Desktop.

Select an option

Save wentongli/70ec450d279ffa3badfcbee44ff9fd1c to your computer and use it in GitHub Desktop.
Experimenting with triplet loss embeddings.
import numpy as np
def build_rainbow(n, curve=None):
rgb = []
width = 2 * np.pi
for i in range(3):
offset = -i * width / 3
cur = np.cos(np.linspace(offset, offset + width, n))
rgb.append(cur)
rainbow = (1 + np.vstack(rgb)) / 2
if curve:
rainbow = curve(rainbow)
rainbow = np.minimum(rainbow * 256, 255).astype(int)
return rainbow.T
import numpy as np
def map_range(x, in_min, in_max, out_min, out_max):
return out_min + (out_max - out_min) * (x - in_min) / (in_max - in_min)
def plot_images(images, xy, blend=np.maximum, canvas_shape=(512,512), fill=0):
h,w = images.shape[1:3]
if images.ndim == 4:
canvas_shape = (canvas_shape[0], canvas_shape[1], images.shape[3])
min_xy = np.amin(xy, 0)
max_xy = np.amax(xy, 0)
min_canvas = np.array((0, 0))
max_canvas = np.array((canvas_shape[0] - h, canvas_shape[1] - w))
canvas = np.full(canvas_shape, fill)
for image, pos in zip(images, xy):
x_off, y_off = map_range(pos, min_xy, max_xy, min_canvas, max_canvas).astype(int)
sub_canvas = canvas[y_off:y_off+h, x_off:x_off+w]
sub_image = image[:h, :w]
canvas[y_off:y_off+h, x_off:x_off+w] = blend(sub_canvas, sub_image)
return canvas
try: # Python 2
from cStringIO import StringIO as BytesIO
except: # Python 3
from io import BytesIO
import numpy as np
import PIL.Image
import IPython.display
import shutil
from math import sqrt
def show_array(a, fmt='png', filename=None, retina=False, zoom=None):
if len(a.shape) == 1:
n = len(a)
side = int(sqrt(n))
if (side * side) == n:
a = a.reshape(side, side)
else:
raise ValueError('input is one-dimensional', a.shape)
a = np.uint8(np.clip(a, 0, 255))
image_data = BytesIO()
PIL.Image.fromarray(a).save(image_data, fmt)
if filename is None:
height, width = a.shape[:2]
if zoom is not None:
width *= zoom
height *= zoom
IPython.display.display(IPython.display.Image(data=image_data.getvalue(),
width=width,
height=height,
retina=retina))
else:
with open(filename, 'wb') as f:
image_data.seek(0)
shutil.copyfileobj(image_data, f)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment