Skip to content

Instantly share code, notes, and snippets.

View jdhao's full-sized avatar
:octocat:
Swimming 🏊 in the sea of code~~

jdhao jdhao

:octocat:
Swimming 🏊 in the sea of code~~
View GitHub Profile
@jdhao
jdhao / max_pooling.py
Last active June 4, 2026 16:38
A naive implementation just for illustrating how forward and backward pass of max-pooling layer in CNN works
import numpy as np
import numba
class MaxPooling(object):
def __init__(self, X, kernel_size=(2,2), stride=(2,2)):
if len(X.shape) != 4:
raise ValueError("Input must have be a tensor of shape N*C*H*W!")
@jdhao
jdhao / crop_rectangle.py
Last active June 2, 2025 11:05
Given an image and a 4 points in the image (no 3 points are co-linear). Find the rotated rectangle enclosing the polygon formed by the 4 points and crop the rotated rectangle from the image. All done by OpenCV.
import cv2
import numpy as np
def main():
img = cv2.imread("test_image.jpg")
# assume coord is a list with 8 float values, the points of the rectangle area should
# have be clockwise
x1, y1, x2, y2, x3, y3, x4, y4 = coord
@jdhao
jdhao / grep_unicode.md
Created September 17, 2019 04:14
How to grep unicode character in command line

How do I grep unicode characters using its code point?

For example, if we want to grep a(unicode code point is u+0041), we can use the following trick:

grep "$(printf '\u0041')" my_file.txt

References

@jdhao
jdhao / C++.sublime-build
Last active November 23, 2024 16:36
Sublime text 3 C++ build system to run C++ executable within sublime text or in a terminal emulator in cases that the program need input from standard input. This build system works on Linux, Windows and MacOS
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
{
"name": "Run in Terminal",
@jdhao
jdhao / Install-feh-on-centos.sh
Last active September 2, 2024 07:20
This script will install feh -- the simple image viewer, on your CentOS 7 system. Please make sure that you are using CentOS 7. See https://jdhao.github.io/2017/05/06/install-feh-image-viewer-on-centos/ for more details.
echo "Installing dependency packages..."
# install packages which can be found by yum
yum -y install libcurl-devel libX11-devel libXt-devel libXinerama-devel libpng-devel
# download and install packages which are not in yum repo
wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/Kenzy:/modified:/C7/CentOS_7/x86_64/imlib2-1.4.6-2.1.x86_64.rpm
wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/Kenzy:/modified:/C7/CentOS_7/x86_64/imlib2-devel-1.4.6-2.1.x86_64.rpm
wget https://jaist.dl.sourceforge.net/project/libjpeg-turbo/1.5.1/libjpeg-turbo-official-1.5.1.x86_64.rpm
yum --nogpgcheck localinstall imlib2-1.4.6-2.1.x86_64.rpm imlib2-devel-1.4.6-2.1.x86_64.rpm libjpeg-turbo-official-1.5.1.x86_64.rpm
@jdhao
jdhao / Sublime_text_regex_cheat_sheet.md
Last active April 23, 2024 08:58
Sublime Text regular expression cheat sheet, you can also find it in https://jdhao.github.io/2019/02/28/sublime_text_regex_cheat_sheet/

Special characters

expression Description
. Match any character
^ Match line begin
$ Match line end
* Match previous RE 0 or more times greedily
*? Match previous RE 0 or more times non-greedily
+ Match previous RE 1 or more times greedily
@jdhao
jdhao / utils.vim
Created September 12, 2019 09:57
My utils functions used in init.vim
" Remove trailing white space, see https://vi.stackexchange.com/a/456/15292
function utils#StripTrailingWhitespaces() abort
let l:save = winsaveview()
" vint: next-line -ProhibitCommandRelyOnUser -ProhibitCommandWithUnintendedSideEffect
keeppatterns %s/\v\s+$//e
call winrestview(l:save)
endfunction
" Create command alias safely, see https://bit.ly/2ImFOpL.
" The following two functions are taken from answer below on SO:
@jdhao
jdhao / gcc-5.4.0-install.sh
Last active February 20, 2024 08:46
The script will install GCC 5.4.0 on your CentOS 7 system, make sure you have root right. See https://jdhao.github.io/2017/09/04/install-gcc-newer-version-on-centos/ for more details.
echo "Downloading gcc source files..."
curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O
echo "extracting files..."
tar xvfj gcc-5.4.0.tar.bz2
echo "Installing dependencies..."
yum -y install gmp-devel mpfr-devel libmpc-devel
echo "Configure and install..."
@jdhao
jdhao / str_byte_convert.md
Created May 31, 2019 10:30
Convert Unicode string to bytes and convert bytes back to Unicode string in Python 3

Conversion between bytes and string in Python 3

To convert Unicode string to bytes object, you can use two methods:

  • 'hello'.encode('utf-8')
  • bytes('hello', encoding='utf-8')

To convert bytes back to Unicode string, you can use two methods:

  • b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf-8')
@jdhao
jdhao / calculate_trainset_mean_std.py
Last active September 20, 2023 06:36
This snippet will calculate the per-channel image mean and std in the train image set. It is plain simple and may not be efficient for large scale dataset.
"""
in this script, we calculate the image per channel mean and standard
deviation in the training set, do not calculate the statistics on the
whole dataset, as per here http://cs231n.github.io/neural-networks-2/#datapre
"""
import numpy as np
from os import listdir
from os.path import join, isdir
from glob import glob