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 / 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 / custom.js
Created June 9, 2016 08:45
jupyter python config with emacs editing mode
// register a callback when the IPython.notebook instance is created.
$([IPython.events]).on('app_initialized.NotebookApp', function(){
function to(mode) {
// this can be either 'vim' or 'emacs'
var mode = mode || 'emacs';
// first let's apply mode to all current cells
function to_mode(c) { return c.code_mirror.setOption('keyMap', mode);};
var cells = IPython.notebook.get_cells();
if (cells != null) {
@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 / channels.hpp
Created January 27, 2015 17:18
simple channel feature computation
vector<cv::Mat> gradient_histogram(const cv::Mat &M, const cv::Mat &O, int num_channels)
{
vector<cv::Mat> channels;
double step = 2 * M_PI / num_channels;
for (int t = 0; t < num_channels; ++t)
{
cv::Mat C;
cv::inRange(O, t*step, t*step+1, C);
C.convertTo(C, CV_64F);
C /= 255.0;
@psycharo-zz
psycharo-zz / integral_image.hpp
Last active September 23, 2015 14:36
simplistic integral image in armadillo
inline arma::mat integral_image(const arma::mat &a)
{
using namespace arma;
mat a_ii = zeros(a.n_rows+1, a.n_cols+1);
for (size_t j = 0; j < a.n_rows; ++j)
{
double sum = 0.0;
for (size_t i = 0; i < a.n_cols; ++i)
{
sum += a(j,i);
@psycharo-zz
psycharo-zz / heap.cpp
Last active August 29, 2015 14:02
simple c++11ish heap
map<double, size_t> top;
size_t max_size = 100;
auto top_add = [&top,max_size](double value, size_t id)
{
if (top.size() < max_size)
top.insert(make_pair(value, id));
else
{
if (top.begin()->first < value)
@psycharo-zz
psycharo-zz / loadmat.py
Created May 15, 2014 17:05
loading matlab matrices
def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
data = sio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)
@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)]
string string_format(const string fmt, ...)
{
int size = 512;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);