Skip to content

Instantly share code, notes, and snippets.

View whklwhkl's full-sized avatar
📍
position zero!

HAO WANG whklwhkl

📍
position zero!
  • Hangzhou
View GitHub Profile
@whklwhkl
whklwhkl / cache.py
Created December 27, 2024 11:44
a function decorator for caching (input, output) pairs on the run and save them on your disk to cut off costs of calling the original function
import atexit
import pickle
class Cache:
def __init__(self, path: str):
self.path = path
try:
with open(path, 'rb') as file:
@whklwhkl
whklwhkl / worker.py
Last active June 26, 2023 07:54
This decorator decouples a function result from the function call, so that we can do other things while waiting for the result.
from threading import Thread
from queue import Queue
class threading(Thread):
'''
```Python
@threading
def foo():
return 42
@whklwhkl
whklwhkl / flask_app.py
Created June 15, 2022 08:47
This script automatically deploys all python functions in a given module with Flask.
'''
################################# README #######################################
This script deploys all functions within a module (i.e. src).
Note that the typing of functions is a necessity for deployment.
For instance, we have a module defined as src.
src
├── __init__.py
└── foo.py
@whklwhkl
whklwhkl / onnx_rebuild_hard_swish.py
Last active October 20, 2022 02:46
Hard swish is a common activation function being used in many neural networks. However, after exporting to the ONNX format, we can see hard swish is decomposed into 4 operators, which may lead to inferior efficiency. This script rebuilds all hard swish activation functions in the given ONNX model.
import onnx
import onnx_graphsurgeon as gs # https://github.com/NVIDIA/TensorRT/tree/main/tools/onnx-graphsurgeon
@gs.Graph.register()
def rebuild_hard_swish(self, *nodes):
_add, _clip, _mul, _div = nodes
# delete div node
_mul.outputs = _div.outputs[:]
_div.outputs.clear()
@whklwhkl
whklwhkl / hds_sample.py
Created June 3, 2021 10:20
uniformly samples data points on high-dimension unit sphere
from functools import lru_cache, partial
import numpy as np
def hds_sample(d=4):
out = [1]
d -= 2
for i in range(d, -1, -1):
theta = inv_cdf(i)(np.random.rand())
@whklwhkl
whklwhkl / nvidia-docker2.txt
Last active August 23, 2019 06:28
install nvidia-docker2
apt-get install docker.io
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
apt-get update
apt-get install -y nvidia-docker2
pkill -SIGHUP dockerd
apt-get install nvidia-418
@whklwhkl
whklwhkl / weighted_hausdorff_distance.py
Created June 23, 2019 17:58
train heat maps with points' coordinates directly
# This is a pytorch implementation of the [Locating Objects Without Bounding Boxes](https://arxiv.org/pdf/1806.07564.pdf)
class WeightedHausdorffDistance(torch.nn.Module):
def __init__(self, alpha=-5):
"""note: this distance metric takes effect when the input heatmap values are within [0,1]"""
super().__init__()
self.soft_min_fn = lambda x:x.pow(alpha).mean(0).mean(0).pow(1/alpha) # alpha --> -infi ==> min_func
def forward(self, heat_map, points):
@whklwhkl
whklwhkl / opencv_deep_sort_tracking.py
Last active June 18, 2019 03:18
opencv_deep_sort_tracking
import cv2
import numpy as np
from PIL import Image
import os.path as osp, os
## from https://github.com/nwojke/deep_sort
import tracker
import detection
import nn_metric
##