Skip to content

Instantly share code, notes, and snippets.

View monklof's full-sized avatar

Chao Liu monklof

View GitHub Profile
@monklof
monklof / pyio.cc
Last active July 9, 2020 06:31
Python C API
/**
* just for demo purpose, bugs maybe found
* g++ -shared -fPIC pyio.cc -o pyio.so -std=c++11 -I/usr/include/python2.7/
* equilavent python code:
def write(filename, string):
global error
if not string: error = “empty string"
with open(filename, ‘w’) as f:
f.write(string)
@monklof
monklof / pdf2jpg
Created March 13, 2020 06:03
pdf 2 jpg, multi page
# coding: utf-8
import math
from PIL import Image
from pdf2image import convert_from_path, convert_from_bytes
import sys,os
print(sys.argv)
inputpath = sys.argv[1]
outputpath = sys.argv[2]
filename = '.'.join(inputpath.split('/')[-1].split('.')[:-1])
@monklof
monklof / py
Created August 29, 2019 09:37
tf_timeline.py
import tensorflow as tf
from tensorflow.python.client import timeline
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
predictions = use_sess.run(use_out, {'DecodeJpeg/contents:0': image_file.file.getvalue()}, options=run_options, run_metadata=run_metadata)
# Create the Timeline object, and write it to a json
tl = timeline.Timeline(run_metadata.step_stats)
@monklof
monklof / tf_savedmodel_to_checkpoint.py
Created March 17, 2019 07:38
tensorflow convert savedmodel to checkpoint
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@monklof
monklof / tf_checkpoint_to_savedmodel.py
Last active June 8, 2022 08:34
convert tensorflow's checkpoint to savedmodel (ResNetV2)
# -*- coding: utf-8 -*-
import os.path
# This is a placeholder for a Google-internal import.
import tensorflow as tf
import tensorflow.contrib.slim as slim
from nets import resnet_v2
from preprocessing import vgg_preprocessing as vgg
@monklof
monklof / tf_graph_inspect.py
Created March 17, 2019 03:40
inspect graph structure
def show_graph_details(graph, mark):
import tensorflow as tf
import pprint
if isinstance(graph, tf.GraphDef):
with tf.Session(graph=tf.Graph()) as sess:
tf.import_graph_def(graph)
graph = sess.graph
elif isinstance(graph, tf.MetaGraphDef):
with tf.Session(graph=tf.Graph()) as sess:
tf.import_graph_def(graph.graph_def)
@monklof
monklof / tf_avaiable_gpu.py
Created January 7, 2019 05:29
tf_avaiable_gpu
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
@monklof
monklof / latency.txt
Created September 5, 2017 09:35 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
#! /bin/sh -
# Build one or more packages in parallel on one or more build hosts.
#
# Usage:
# build-all [ --? ]
# [ --all "..." ]
# [ --cd "..." ]
# [ --check "..." ]
# [ --configure "..." ]
# [ --environment "..." ]
@monklof
monklof / flask_base_msg_processing.py
Created April 14, 2016 14:19
some basic processing protocols for flask I flavored in my projects
import os, sys
from flask import jsonify
import datetime
def convert_datatype(val):
if type(val) == dict:
converted = {}
for k in val:
if type(k) != unicode and type(k) != str:
raise TypeError('the key of dict must be strings.')