Skip to content

Instantly share code, notes, and snippets.

View prabindh's full-sized avatar
💭
I may be slow to respond.

Prabindh Sundareson prabindh

💭
I may be slow to respond.
View GitHub Profile
// Call like below
// boxedMat will contain resized image
// boxedMat = __LetterBoxResize(floatMat, net->w, net->h);
//
// Taken mostly from https://jdhao.github.io/2017/11/06/resize-image-to-square-with-padding/
cv::Mat ArapahoV2::__LetterBoxResize(cv::Mat img, int w, int h)
{
cv::Mat intermediateImg, outputImg;
int delta_w, delta_h, top, left, bottom, right;
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 13 16:59:53 2016
From - https://stats.stackexchange.com/questions/218407/encoding-angle-data-for-neural-network
@author: Ari
"""
from numpy import savetxt, loadtxt, round, zeros, sin, cos, arctan2, clip, pi, tanh, exp, arange, dot, outer, array, shape, zeros_like, reshape, mean, median, max, min
from numpy.random import rand, shuffle
import matplotlib.pyplot as plt
# https://stackoverflow.com/questions/56069411
# The TF code
score_inputs = tf.placeholder(np.float32, shape=(None, 100))
targets = tf.placeholder(np.float32, shape=(None), name="targets")
l2 = tf.contrib.layers.l2_regularizer(0.01)
first_layer = tf.layers.dense(score_inputs, 100, activation=tf.nn.relu, kernel_regularizer=l2)
outputs = tf.layers.dense(first_layer, 1, activation = None, kernel_regularizer=l2)
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
def normalize_angles(phases):
phases = phases + np.pi
phases /= (2 * np.pi)
return phases
def build_fourier_mnist():
CuDNNGRU stateful implementation, TensorFlow backend
Layers: (300,80,150) -- (encoder, latent, decoder) -- (selu, tanh, tanh)
return_sequences=True for all layers
GaussianNoise + Dropout at (=after) input, AlphaDropout at encoder, Dropout at latent
BatchNormalization between encoder and latent, latent and decoder
Output: TimeDistributed(Dense(units=input_dim, activation='linear'))
batch_size=25, timesteps=400, input_dim=16 - 25 separate, 10-min sequences fed 400 timesteps (=1 sec) at a time (as 10*60=600 'windows' in parallel, non-shuffled)
reset_states() applied before testing on new x25 10-min sequences
model.fit, or train_on_batch for training
from keras.datasets import mnist
from keras.layers import Dense, Input, concatenate,subtract, Lambda
from keras.losses import binary_crossentropy
from keras.optimizers import SGD
(train_x, train_y), (test_x, test_y) = mnist.load_data()
train_x = (train_x / 255.0).reshape(-1, 28*28)
test_x = (test_x / 255.0).reshape(-1, 28*28)
inp1 = Input(shape=(28*28,))
@prabindh
prabindh / conv2dtricks.py
Created May 14, 2019 05:11
numpy conv2d tricks
import numpy as np
from numpy.lib.stride_tricks import as_strided
import tensorflow as tf
import time
def conv2dTrickster(a, b):
a = as_strided(a,(len(a),a.shape[1]-len(b)+1,a.shape[2]-b.shape[1]+1,len(b),b.shape[1],a.shape[3]),a.strides[:3]+a.strides[1:])
return np.einsum('abcijk,ijkd', a, b[::-1,::-1])
def conv2dSimple(image, filter):
@prabindh
prabindh / spikechecker.py
Created May 17, 2019 03:56
spike checker
# conv1d for timeseries spike
model = models.Sequential()
model.add(layers.Conv1D(filters = 1,
kernel_size = 10,
activation = 'relu',
input_shape=(timesteps,1)))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Flatten())
model.add(layers.Dense(1, activation = 'sigmoid'))
@prabindh
prabindh / gitdiff.diff
Created May 28, 2019 13:44
darknet master and alexey fork diff yolov3
$ diff ~/Downloads/yolov3.cfg-master.txt ~/Downloads/yolov3.cfg-alexeyab.txt
3,4c3,4
< # batch=1
< # subdivisions=1
---
> batch=1
> subdivisions=1
6,9c6,9
< batch=64
< subdivisions=16
// Prabindh Sundareson 2020
// Age of the lockdown
#include "pch.h" // include #include "httplib.h" in this file
#include <iostream>
static httplib::Server svr;
void pressSpace()
{