Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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>
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 / 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 = [
@qfgaohao
qfgaohao / image_folder.py
Created October 27, 2017 09:19
Convert an ImageNet like dataset into tfRecord files, provide a method get_dataset to read the created files. It has similar functions as ImageFolder in Pytorch. Modified from https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/download_and_convert_flowers.py https://github.com/tensorflow/models/blob/master/research…
r"""Convert an ImageNet like dataset into tfRecord files, provide a method get_dataset to read the created files.
It has similar functions as ImageFolder in Pytorch.
Modified from
https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/download_and_convert_flowers.py
https://github.com/tensorflow/models/blob/master/research/slim/datasets/flowers.py
"""
from __future__ import absolute_import
@qfgaohao
qfgaohao / average_precision_score.py
Last active October 20, 2017 09:49
It computes average precision based on the definition of Pascal Competition. It computes the under curve area of precision and recall. Recall follows the normal definition. Precision is a variant. pascal_precision[i] = typical_precision[i:].max()
import numpy as np
def compute_average_precision(test_results, num_true_cases):
"""
It computes average precision based on the definition of Pascal Competition. It computes the under curve area
of precision and recall. Recall follows the normal definition. Precision is a variant.
pascal_precision[i] = typical_precision[i:].max()
Usage:
test_results = np.array([True, False, True, False, True, False, False, False, False, True])
@qfgaohao
qfgaohao / timer.py
Created October 17, 2017 04:10
A simple Timer.
from datetime import datetime
import json
class Timer:
def __init__(self, outputFile):
self.fout = open(outputFile, 'w')
self.cache = []
self.records = {}
def start(self, event="time"):
@qfgaohao
qfgaohao / bank_conflicts_test.cu
Last active October 17, 2017 04:13
It tests the time overhead of shared memory bank conflicts.
#include <stdio.h>
#define N (32)
__global__ void increment(int* time) {
__shared__ float s[1024];
for (int i = 0; i < 1024; i++) {
s[i] = 1.0f;
}