Skip to content

Instantly share code, notes, and snippets.

View Tony607's full-sized avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / Object_detection_cam.py
Last active August 26, 2023 04:44
Object detection with webcam on Raspberry Pi and TensorFlow 1.9
######## WebCam Object Detection Using Tensorflow Classifier #########
# Description:
# This program uses a TensorFlow classifier to perform object detection.
# It loads the classifier uses it to perform object detection on a WebCam feed.
# It draws boxes and scores around the objects of interest in each frame from
# the WebCam. It also can be used with picamera by adding "--picamera"
# when executing this script from the terminal.
# Import packages
import os
@Tony607
Tony607 / prune.py
Created May 19, 2019 12:20
How to compress your Keras model x5 smaller with TensorFlow model optimization | DLology
import numpy as np
import tensorflow as tf
from tensorflow_model_optimization.sparsity import keras as sparsity
# Backend agnostic way to save/restore models
# _, keras_file = tempfile.mkstemp('.h5')
# print('Saving model to: ', keras_file)
# tf.keras.models.save_model(model, keras_file, include_optimizer=False)
# Load the serialized model
@Tony607
Tony607 / draw.py
Created August 24, 2018 10:07
DIY Object Detection Doodle camera with Raspberry Pi | DLology
import gizeh as gz
from matplotlib import pyplot as plt
from PIL import Image
%matplotlib inline
from drawing_dataset import DrawingDataset
dataset = DrawingDataset('./data/quick_draw_pickles/', './data/label_mapping.jsonl')
dataset.setup()
strokes = dataset.get_drawing('cat', 0)
@Tony607
Tony607 / freeze_session.py
Created November 11, 2018 11:08
How to convert trained Keras model to a single TensorFlow .pb file and make prediction
from keras import backend as K
import tensorflow as tf
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
"""
Freezes the state of a session into a pruned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
pruned so subgraphs that are not necessary to compute the requested
@Tony607
Tony607 / warmup_cosine_decay_scheduler.py
Created December 31, 2018 10:18
Bag of Tricks for Image Classification with Convolutional Neural Networks in Keras | DLology
import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend as K
def cosine_decay_with_warmup(global_step,
learning_rate_base,
total_steps,
@Tony607
Tony607 / yolo_non_max_suppression.py
Created March 25, 2018 01:22
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
def yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):
"""
Applies Non-max suppression (NMS) to set of boxes
Arguments:
scores -- tensor of shape (None,), output of yolo_filter_boxes()
boxes -- tensor of shape (None, 4), output of yolo_filter_boxes() that have been scaled to the image size (see later)
classes -- tensor of shape (None,), output of yolo_filter_boxes()
max_boxes -- integer, maximum number of predicted boxes you'd like
iou_threshold -- real value, "intersection over union" threshold used for NMS filtering
@Tony607
Tony607 / freeze_graph.py
Created April 13, 2019 10:13
How to run Keras model on Jetson Nano | DLology
import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.keras.models import load_model
# Clear any previous session.
tf.keras.backend.clear_session()
save_pb_dir = './model'
model_fname = './model/model.h5'
@Tony607
Tony607 / get_mfccs.py
Created October 28, 2018 13:58
How to run GPU accelerated Signal Processing in TensorFlow | DLology
def get_mfccs(audio_file=None ,signals=None, sample_rate = 44100, num_mfccs = 13, frame_length=1024, frame_step=512, fft_length=1024, fmax=8000, fmin=80):
"""Compute the MFCCs for audio file
Keyword Arguments:
audio_file {str} -- audio wav file path (default: {None})
signals {tensor} -- input signals as tensor or np.array in float32 type (default: {None})
sample_rate {int} -- sampling rate (default: {44100})
num_mfccs {int} -- number of mfccs to keep (default: {13})
frame_length {int} -- frame length to compute STFT (default: {1024})
frame_step {int} -- frame step to compute STFT (default: {512})
@Tony607
Tony607 / get_frozen_graph.py
Created April 22, 2019 01:10
How to run TensorFlow Object Detection model on Jetson Nano | DLology
import tensorflow as tf
def get_frozen_graph(graph_file):
"""Read Frozen Graph file from disk."""
with tf.gfile.FastGFile(graph_file, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
return graph_def
# The TensorRT inference graph file downloaded from Colab or your local machine.
@Tony607
Tony607 / tensorflow-object-detection-training-colab.ipynb
Created February 12, 2019 02:50
tensorflow-object-detection-training-colab.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.