Skip to content

Instantly share code, notes, and snippets.

View applenob's full-sized avatar
🏋️‍♂️
Focusing

Javen_陈俊文 applenob

🏋️‍♂️
Focusing
View GitHub Profile
@kklemon
kklemon / iterable_dataset_dist.py
Last active June 6, 2024 08:05
PyTorch IterableDataset implementation with multiprocessing and distributed training support
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.utils.data import IterableDataset, DataLoader
class DistributedIterableDataset(IterableDataset):
"""
Example implementation of an IterableDataset that handles both multiprocessing (num_workers > 0)
@applenob
applenob / collective_all_reduce_strategy_demo_tf1.15.py
Last active August 27, 2020 03:18
tensorflow distribute learning demo for multi-worker with `collective_all_reduce_strategy`.
import threading
import contextlib
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.contrib.distribute.python import collective_all_reduce_strategy
from tensorflow.python.distribute import multi_worker_test_base
from tensorflow.python.training import coordinator
from tensorflow.python.training import server_lib
from tensorflow.python.eager import context
@applenob
applenob / freeze_model_to_pb.py
Last active January 7, 2019 08:25
Save a tensorflow model to a pb file.
# coding=utf-8
"""Save a tensorflow model to a pb file."""
# Build the model, then train it or load weights from somewhere else.
# ...
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
@applenob
applenob / freeze_ckpt_to_pb.py
Created January 7, 2019 08:00
freeze tensorflow checkpoint file to pb format.
"""
freeze tensorflow checkpoint file to pb format.
"""
import argparse
import os
import tensorflow as tf
import logging
from tensorflow.python.framework import graph_util
@applenob
applenob / tf_serving.sh
Created January 7, 2019 02:33 — forked from jarutis/tf_serving.sh
Install Tensorflow Serving on Centos 7 (CPU)
sudo su
# Java
yum -y install java-1.8.0-openjdk-devel
# Build Esentials (minimal)
yum -y install gcc gcc-c++ kernel-devel make automake autoconf swig git unzip libtool binutils
# Extra Packages for Enterprise Linux (EPEL) (for pip, zeromq3)
yum -y install epel-release
@applenob
applenob / setup-zeromq.sh
Created December 18, 2018 09:23 — forked from katopz/setup-zeromq.sh
Setup zeromq in Ubuntu 16.04
#!/usr/bin/bash
# Download zeromq
# Ref http://zeromq.org/intro:get-the-software
wget https://github.com/zeromq/libzmq/releases/download/v4.2.2/zeromq-4.2.2.tar.gz
# Unpack tarball package
tar xvzf zeromq-4.2.2.tar.gz
# Install dependency
@frnsys
frnsys / distributed_estimator.py
Created November 26, 2018 15:47
example of a custom tensorflow estimator with distributed training
"""
Tensorflow estimator API example
References:
- <https://www.tensorflow.org/guide/custom_estimators>
- <https://github.com/tensorflow/models/blob/master/samples/core/get_started/custom_estimator.py>
- <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/distribute/README.md>
"""
import numpy as np
import tensorflow as tf
@applenob
applenob / keras_f1_metric.py
Last active November 9, 2018 03:15
F1 metric for keras.
"""Reference: https://stackoverflow.com/questions/43547402/how-to-calculate-f1-macro-in-keras"""
from keras import backend as K
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
@applenob
applenob / top_queue.py
Created June 13, 2018 08:48
Keep top n number in a list with ascend order.
class TopQueue:
"""keep top n num in a list with ascend order."""
def __init__(self, n):
self.n = n
self.queue = []
def add(self, new_val):
bigger_index = -1
for i, one in enumerate(self.queue):
if one > new_val:
@applenob
applenob / masked_softmax_tf.py
Last active April 24, 2018 23:49
Tensorflow Masked Softmax with mask after the exp operation.
import tensorflow as tf
def masked_softmax(logits, mask, axis):
"""softmax with mask after the exp operation"""
e_logits = tf.exp(logits)
masked_e = tf.multiply(e_logits, mask)
sum_masked_e = tf.reduce_sum(masked_e, axis, keep_dims=True)
# be careful when the sum is 0
ones = tf.ones_like(sum_masked_e)
sum_masked_e_safe = tf.where(tf.equal(sum_masked_e, 0), ones, sum_masked_e)