Skip to content

Instantly share code, notes, and snippets.

View psycharo-zz's full-sized avatar

Timur psycharo-zz

  • EPFL
  • Lausanne, Switzerland
View GitHub Profile
n_masks = 100
n_channels = 100
# do not sample more masks than this
n_max_samples = 50
target_corr = 0.1
target_corr_eps = 1.0e-2
rate = target_corr
@psycharo-zz
psycharo-zz / mesh_io.py
Created October 3, 2018 14:32
obj reader
def read_obj(fname, max_degree=6, dtype=np.float32):
'''
Reads a mesh from an obj file.
Faces are converted into triangles.
Arguments:
fname: path or file-like object
max_degree: maximum degree for the adjacency
dtype: type for the vertex array
Returns:
(verts, faces)
@psycharo-zz
psycharo-zz / tf_permutohedral.py
Created February 6, 2018 13:56
tensorflow implementation of permutohedral filtering
# now, let's do tf implementation
def ph_splat(inputs, offsets, weights, nbs):
N, C = inputs.shape
F = weights.shape[1] - 1
M = nbs.shape[0]
weighted_inputs = tf.matmul(weights[:N,:,tf.newaxis],
inputs[:N,tf.newaxis,:])
weighted_inputs = tf.reshape(weighted_inputs, [-1, C])
idxs = tf.reshape(offsets[:N,:F+1], [-1,1])+1
# TODO: the only thing is the unknown shape of M?
@psycharo-zz
psycharo-zz / np_permutohedral.py
Last active February 6, 2018 13:09
permutohedral lattice filtering in numpy
def np_splat(inputs, offsets, weights, nbs):
N, V = inputs.shape
F = weights.shape[1] - 1
M = nbs.shape[0]
# splatting
## compute inputs multiplied by the weights
weighted_inputs = np.matmul(weights[:N,:,np.newaxis],
inputs[:N,np.newaxis,:])
weighted_inputs = weighted_inputs.reshape([-1, V])
## sum up at corresponding indices (update with duplicatess)
def mmul(*tensors):
return tf.foldl(tf.matmul, tensors)
def msym(X):
return (X + tf.matrix_transpose(X)) / 2
def mdiag(X):
return tf.matrix_diag(tf.matrix_diag_part(X))
@tf.RegisterGradient('Svd')
@psycharo-zz
psycharo-zz / theano_tf_convd2_transpose.py
Created May 24, 2017 14:48
converting theano transpose convolution filters to tensorflow format
w = w.transpose([2, 3, 1, 0])
w = w[::-1,::-1,:,:]
@psycharo-zz
psycharo-zz / feeding_runner.py
Created May 8, 2017 09:01
custom multi-threading runner for tensorflow
import threading
import numpy as np
import tensorflow as tf
class FeedingRunner(object):
"""Takes care of feeding/dequeueing data into the queue
Based on tf.train.QueueRunner
"""
def __init__(self, generator, dtypes, shapes, names, num_threads,
@psycharo-zz
psycharo-zz / tf_inputs_cityscapes.py
Created March 1, 2017 17:02
custom queue runner to read cityscapes
def instance_to_regression_map(instances, cids):
"""Convert instance label map to the regression map
Args:
instances: instance label mask
cids: ids of classes to load
"""
# TODO: for all the classes that have instances, we can compute this
image_size = instances.shape[:2]
reg = np.zeros(image_size + (4,), dtype=np.uint16)
# instead of this, we can simply ???
@psycharo-zz
psycharo-zz / tf_instance_regression.py
Last active February 3, 2017 13:07
regression masks from instance segmentation masks
def _next_instance(mask, iid, instances):
"""Process single instance and add it to the mask
Args:
mask: source mask
iid: instance id
instances: instance segmentation mask
Returns:
updated mask
"""
yx = tf.to_int32(tf.where(tf.equal(instances, iid)))
class ImageCoder(object):
"""Helper class for handling images in TensorFlow."""
def __init__(self, channels=3, config=None):
# Create a single TensorFlow Session for all image decoding calls.
self._sess = tf.Session(config=config)
# TensorFlow ops for JPEG decoding.
self._src_png = tf.placeholder(dtype=tf.string)
self._dst_raw = tf.image.decode_png(self._src_png, channels=channels)