Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rocking5566
rocking5566 / Layernorm_backward.py
Last active August 30, 2023 19:35
Layernorm_backward
import numpy as np
import torch
def layernorm_forward(x, gamma, beta, eps):
out, cache = None, None
M, _ = x.shape
x_mean = np.mean(x, axis=1).reshape(M, 1)
x_var = np.var(x, axis=1).reshape(M, 1)
@rocking5566
rocking5566 / Batchnorm_backward.py
Last active August 30, 2023 19:33
Batchnorm backward
import numpy as np
def batchnorm_forward(x, gamma, beta, eps):
N, D = x.shape
# step1: calculate mean
mu = 1./N * np.sum(x, axis=0)
# step2: subtract mean vector of every trainings example
@rocking5566
rocking5566 / keras_quant.py
Last active December 8, 2020 09:40
Quantization aware training in keras
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Conv2D, Flatten
from tensorflow.keras.optimizers import RMSprop
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
@rocking5566
rocking5566 / pb_get_all_layer.py
Created April 10, 2019 09:21
Get tensorflow pb tensor
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
from PIL import Image
import numpy as np
import tensorflow as tf
#include <iostream>
#include <vector>
using namespace std;
typedef vector< vector<float>> matrix;
void PrintMatrix(const matrix& mat)
{
for (int i = 0, row = mat.size(); i < row; ++i)
{
#include <math.h>
using namespace std;
using namespace cv;
cv::Mat GetMask(const Mat& img)
{
Mat ret;
Mat imgGray;
cvtColor(img, imgGray, CV_BGR2GRAY);
@rocking5566
rocking5566 / tf_objdet_api_webcam.py
Created March 22, 2018 02:57
Tensorflow Object Detection API in WebCam
import sys
sys.path.append("/opt/tf_model/research")
sys.path.append("/opt/tf_model/research/object_detection")
import os
import cv2
import numpy as np
import tensorflow as tf
from utils import label_map_util