Skip to content

Instantly share code, notes, and snippets.

View rreece's full-sized avatar
🙈
doing things

Ryan Reece rreece

🙈
doing things
View GitHub Profile
@rreece
rreece / save_pbtxt.py
Last active September 13, 2018 20:01
import tensorflow as tf
from google.protobuf import text_format
##______________________________________________________________________________
def model_fn(features, labels, mode, params, pbtxt):
...
optimizer = tf.train.AdamOptimizer(learning_rate=params['lr'])
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
@rreece
rreece / git_tricks.sh
Last active October 28, 2021 21:00
git tricks i always forget
git clone git@github.com:GitUserOrOrg/repo.git
# create a new branch
git checkout -b rreece/branch1
# set my branch to track the master when i do git pull
#git branch --set-upstream rreece/branch1 origin/master # deprecated
git branch --set-upstream-to origin/master
# show branches
"""
Should follow:
https://en.wikipedia.org/wiki/ISO_8601
See also:
- https://stackoverflow.com/questions/2150739/iso-time-iso-8601-in-python
"""
import time
timestamp = time.strftime('%Y-%m-%d-%Hh%M') # '2019-02-14-16h20'
@rreece
rreece / crop_surrounding_whitespace.py
Created February 17, 2019 23:35
Crop the surrounding whitespace from an image with python
"""
Taken from here:
https://github.com/thumbor/thumbor/issues/116
"""
def crop_surrounding_whitespace(image):
"""Remove surrounding empty space around an image.
This implemenation assumes that the surrounding space has the same colour
as the top leftmost pixel.
@rreece
rreece / pool.py
Created March 19, 2019 01:10 — forked from nfaggian/pool.py
Multiprocessing example
from __future__ import print_function
import multiprocessing
import ctypes
import numpy as np
def shared_array(shape):
"""
Form a shared memory numpy array.
import tensorflow as tf
from google.protobuf.json_format import MessageToJson
filename = 'myfile.tfrecords'
for record_str in tf.python_io.tf_record_iterator(filename):
example = tf.train.Example.FromString(record_str)
json = MessageToJson(example)
print(json)
@rreece
rreece / print_total_parameters.py
Created February 7, 2020 19:05
print all the trainable parameters in a tensorflow model in current scope
def print_total_parameters():
total_parameters = 0
for variable in tf.trainable_variables():
# shape is an array of tf.Dimension
shape = variable.get_shape()
variable_parameters = 1
for dim in shape:
variable_parameters *= dim.value
print('%s dim=%i shape=%s params=%i' % (
variable.name,
@rreece
rreece / segment_mean.py
Created March 11, 2020 20:24
XLA-compatible version of tf.segment_mean
import tensorflow as tf
def segment_mean(x, bounds):
n = len(bounds)
segs = [
tf.reduce_mean(x[:,bounds[i]:bounds[i+1]], axis=1) for i in range(n-1)]
y = tf.stack(segs, axis=1)
return y
"""
Main training script.
Call by:
python3 train.py --params=params.yaml
"""
import argparse
import numpy as np
@rreece
rreece / delete_git_submodule.md
Created March 13, 2020 18:32 — forked from myusuf3/delete_git_submodule.md
How effectively delete a git submodule.

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule