Skip to content

Instantly share code, notes, and snippets.

@qfgaohao
qfgaohao / ssd_priors.py
Last active November 27, 2019 08:06
Generate SSD Prior Boxes.
import collections
import numpy as np
import itertools
SSDBoxSizes = collections.namedtuple('SSDBoxSizes', ['min', 'max'])
Spec = collections.namedtuple('Spec', ['feature_map_size', 'shrinkage', 'box_sizes', 'aspect_ratios'])
# the SSD orignal specs
specs = [
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
n = strip_def.node.add()
n.MergeFrom(n0)
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
@qfgaohao
qfgaohao / pb_utils.cc
Last active April 6, 2023 11:05
Demonstrate how to save and read a protobuf message AddressBook to/from pb or pbtxt files.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <google/protobuf/text_format.h>
// for read_from_pbtxt_nocopy
#include <fcntl.h>
@qfgaohao
qfgaohao / caffe2_workflow.py
Last active December 18, 2018 06:02
demonstrates how to train a model, init weights from another source (transfer learning), save models to pb and pbtxt files.
import numpy as np
from caffe2.python import (
brew,
model_helper,
optimizer,
workspace,
utils,
)
from caffe2.proto import caffe2_pb2
@qfgaohao
qfgaohao / parse_pytorch_graph.py
Last active November 24, 2019 14:09
demonstrate how to trace/parse a pytorch graph
import torch
from torchvision import models
def parse(net, inputs = torch.randn(1, 3, 224, 224)):
with torch.onnx.set_training(net, False):
trace = torch.onnx.utils._trace(net, inputs)
graph = trace.graph()
for n in graph.nodes():
print(n.scopeName(), n.kind())
@qfgaohao
qfgaohao / create_hellotensor.py
Created April 30, 2019 04:40 — forked from omimo/create_hellotensor.py
A simple example for saving a tensorflow model and preparing it for using on Android
# Create a simple TF Graph
# By Omid Alemi - Jan 2017
# Works with TF <r1.0
import tensorflow as tf
I = tf.placeholder(tf.float32, shape=[None,3], name='I') # input
W = tf.Variable(tf.zeros_initializer(shape=[3,2]), dtype=tf.float32, name='W') # weights
b = tf.Variable(tf.zeros_initializer(shape=[2]), dtype=tf.float32, name='b') # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O') # activation / output