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
@psycharo-zz
psycharo-zz / read_seq.py
Created May 15, 2014 16:11
reading .seq files from caltech pedestrian dataset
def read_seq(path):
def read_header(ifile):
feed = ifile.read(4)
norpix = ifile.read(24)
version = struct.unpack('@i', ifile.read(4))
length = struct.unpack('@i', ifile.read(4))
assert(length != 1024)
descr = ifile.read(512)
params = [struct.unpack('@i', ifile.read(4))[0] for i in range(0,9)]
@psycharo-zz
psycharo-zz / arma2opencv.hpp
Last active May 4, 2020 10:54
convertions between armadillo and opencv
// convert an OpenCV multi-channel matrix to Armadillo cube. A copy is made
template <typename T, int NC>
Cube<T> to_arma(const cv::Mat_<cv::Vec<T, NC>> &src)
{
vector<cv::Mat_<T>> channels;
Cube<T> dst(src.cols, src.rows, NC);
for (int c = 0; c < NC; ++c)
channels.push_back({src.rows, src.cols, dst.slice(c).memptr()});
cv::split(src, channels);
return dst;
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')
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 / tf_inputs.py
Created January 23, 2017 14:57
example of an efficient and simple input pipeline in tensorflow
import threading
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _convert_example(rgb_path, label_path):
# rgb_png = tf.gfile.GFile(rgb_path, 'rb').read()
@psycharo-zz
psycharo-zz / tf_pairwise.py
Last active October 23, 2018 05:43
pairwise functions for TF
def pdist(X):
"""
Computes pairwise distance between each pair of points
Args:
X - [N,D] matrix representing N D-dimensional vectors
Returns:
[N,N] matrix of (squared) Euclidean distances
"""
x2 = tf.reduce_sum(X * X, 1, True)
return x2 - 2 * tf.matmul(X, tf.transpose(X)) + tf.transpose(x2)
@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 / softmax.py
Created June 3, 2016 08:32
N-dim softmax in numpy
import numpy as np
def softmax(x, axis=None):
e_x = np.exp(x - np.max(x, axis=axis, keepdims=True))
return e_x / np.sum(e_x, axis=axis, keepdims=True)
@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)