Skip to content

Instantly share code, notes, and snippets.

View tejaskhot's full-sized avatar
🎯
Focusing

Tejas Khot tejaskhot

🎯
Focusing
View GitHub Profile
@tejaskhot
tejaskhot / soft_argmax.py
Last active February 11, 2023 18:33
soft argmax in python
# argmax is not differentiable, so the hack to get argmax is
# softmax(x)^T * range(indices)
# numpy
beta = 12
y_est = np.array([[1.1, 3.0, 1.1, 1.3, 0.8]])
# multiplying by some large constant beta to make the resulting
# distribution more peaky near the max
a = np.exp(beta*y_est)
b = np.sum(np.exp(beta*y_est))
import faiss
# example data
xb = np.random.rand(200000, 32).astype('float32')
xq = np.random.rand(500, 32).astype('float32')
# get reference result with index
index = faiss.IndexFlatL2(xb.shape[1])
index.add(xb)
lims, D, I = index.range_search(xq, 1.5)
@tejaskhot
tejaskhot / focal_loss_pytorch.py
Created November 25, 2018 05:46
pytorch implementation of focal loss which address the class imbalance issue for training one-stage dense detector
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, logits=False, reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, inputs, targets):
if self.logits:
@tejaskhot
tejaskhot / README-Template.md
Created September 6, 2018 13:30 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@tejaskhot
tejaskhot / bash_rename.sh
Created August 19, 2018 18:27
bash rename multiple files in folder
# This would replaces names of all files in folder having the string ".obj.ply" with ".ply"
for filename in *; do newname=`echo $filename | sed 's/.obj.ply/.ply/g'`; mv $filename $newname; done
@tejaskhot
tejaskhot / s3dis_class_names
Created August 18, 2018 03:25
S3DIS class name to id mapping
classnames = {
0:'ceiling',
1:'floor',
2:'wall',
3:'beam',
4:'column',
5:'window',
6:'door',
7:'table',
8:'chair',
@tejaskhot
tejaskhot / image_montage
Created August 11, 2018 00:23
Generating a horizontal image montage
Source:
https://stackoverflow.com/questions/2853334/glueing-tile-images-together-using-imagemagicks-montage-command-without-resizin
montage -mode concatenate -tile 12x *.jpg out.jpg
@tejaskhot
tejaskhot / Chamfer_Distance_Pytorch.py
Created August 10, 2018 19:30 — forked from WangZixuan/Chamfer_Distance_Pytorch.py
Use Pytorch to calculate Chamfer distance
import torch
def chamfer_distance_without_batch(p1, p2, debug=False):
'''
Calculate Chamfer Distance between two point sets
:param p1: size[1, N, D]
:param p2: size[1, M, D]
:param debug: whether need to output debug info
@tejaskhot
tejaskhot / exp_lr_scheduler.py
Last active April 19, 2022 20:59
exponential learning rate decay in pytorch
def exp_lr_scheduler(optimizer, global_step, init_lr, decay_steps, decay_rate, lr_clip, staircase=True):
"""Decay learning rate by a factor of 0.1 every lr_decay_epoch epochs."""
if staircase:
lr = init_lr * decay_rate**(global_step // decay_steps)
else:
lr = init_lr * decay_rate**(global_step / decay_steps)
lr = max(lr, lr_clip)
if global_step % decay_steps == 0:
print('LR is set to {}'.format(lr))
@tejaskhot
tejaskhot / softmax_cross_entropy_with_logits.py
Created July 14, 2018 22:51
Pytorch softmax cross entropy with logits
# pytorch function to replicate tensorflow's tf.nn.softmax_cross_entropy_with_logits
# works for soft targets or one-hot encodings
import torch
import torch.nn.functional as F
logits = model(input)
loss = torch.sum(- target * F.log_softmax(logits, -1), -1)
mean_loss = loss.mean()