Skip to content

Instantly share code, notes, and snippets.

View CasiaFan's full-sized avatar
💭
Coding is life!

Zong Fan CasiaFan

💭
Coding is life!
  • UIUC
  • Champaign, Illinois, USA
  • 00:00 (UTC -12:00)
View GitHub Profile
@CasiaFan
CasiaFan / install.sh
Created April 27, 2018 07:36
Ubuntu 16.04 中国地区常用工具安装(包括换国内镜像,NVIDIA显卡驱动,chrome,sogou拼音,ss-qt5, atom编辑器,zsh, YCM )
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old
sudo echo "# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
@CasiaFan
CasiaFan / mat2tensor.cpp
Created May 30, 2018 09:03
Convert cv::Mat into tensorflow::Tensor with uint8 datatype using tensorflow c++ API
tensorflow::Tensor matToTensor(const cv::Mat &frame){
int height = mat.rows;
int width = mat.cols;
int depth = mat.channels();
Tensor inputTensor(tensorflow::DT_UINT8, tensorflow::TensorShape({1, height, width, depth}));
auto inputTensorMapped = inputTensor.tensor<tensorflow::uint8, 4>();
cv::Mat frame;
mat.convertTo(frame, CV_8UC3);
const tensorflow::uint8* source_data = (tensorflow::uint8*) frame.data;
@CasiaFan
CasiaFan / add_local_light_parallel.py
Last active January 11, 2022 16:48
Add local brightness adjustment in parallel for image augmentation
import cv2
import numpy as np
import random
from scipy.stats import norm
def generate_parallel_light_mask(mask_size,
position=None,
direction=None,
max_brightness=255,
min_brightness=0,
@CasiaFan
CasiaFan / add_local_light_radically.py
Created June 25, 2018 09:03
Add local brightness adjustment radically for image augmentation
import cv2
import numpy as np
import random
from scipy.stats import norm
def generate_spot_light_mask(mask_size,
position=None,
max_brightness=255,
min_brightness=0,
mode="gaussian",
@CasiaFan
CasiaFan / tf_hub_image_classification_example.py
Created November 22, 2018 08:01
An example of using tensorflow hub for image classification
from urllib.request import urlopen
from io import BytesIO
from PIL import Image, ImageOps
import cv2
def download_and_resize_image(url, filename, new_width=256, new_height=256):
response = urlopen(url)
image_data = response.read()
image_data = BytesIO(image_data)
pil_image = Image.open(image_data)
@CasiaFan
CasiaFan / tf_hub_biggan_example.py
Created November 22, 2018 08:03
An example of using tensorflow hub for image generation with BigGAN
import tensorflow as tf
import tensorflow_hub as hub
import cv2
import numpy as np
from scipy.stats import truncnorm
# MODULE_PATH = 'https://tfhub.dev/deepmind/biggan-128/2' # 128x128 BigGAN
MODULE_PATH = 'https://tfhub.dev/deepmind/biggan-256/2' # 256x256 BigGAN
# MODULE_PATH = 'https://tfhub.dev/deepmind/biggan-512/2' # 512x512 BigGAN
@CasiaFan
CasiaFan / asynchronous_caching_video_stream.py
Last active July 17, 2024 22:46
Asynchronous caching and analysis of video stream with opencv and multithreading
import cv2
from redis import ConnectionPool, Redis
import numpy as np
import json, time
from multithreading import Thread, Event
redis_config = {"server": "localhost",
"passwd": '',
"port": '6379',
"db": 0}
@CasiaFan
CasiaFan / tf_model_flops_measure.py
Created March 28, 2019 03:22
Measure model flops in tensorflow
import tensorflow as tf
def load_pb(pb_model):
with tf.gfile.GFile(pb_model, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
return graph
@CasiaFan
CasiaFan / tf_model_post_training_quantization.py
Last active June 14, 2022 08:15
post-training quantization tensorflow model to float16
import tensorflow as tf
from tensorflow.core.framework import types_pb2, graph_pb2, attr_value_pb2
from tensorflow.tools.graph_transforms import TransformGraph
from google.protobuf import text_format
import numpy as np
# object detection api input and output nodes
input_name = "image_tensor"
output_names = ["detection_boxes", "detection_classes", "detection_scores", "num_detections"]
# Const should be float32 in object detection api during nms (see here: https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/non-max-suppression-v4.html)
@CasiaFan
CasiaFan / ffmpeg_python_with_gpu_acceleration.py
Created September 8, 2019 16:35
Use pipe to read ffmped decoded video frames with NVIDIA GPU hardware acceleration
import subprocess as sp
import cv2
import numpy as np
from PIL import Image
import tensorflow as tf
ffmpeg_cmd_1 = ["./ffmpeg", "-y",
"-hwaccel", "nvdec",
"-c:v", "h264_cuvid",
"-vsync", "0",