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 / 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.
"""
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 / Dockerfile.centos7.python36.pipenv
Created August 3, 2018 01:07 — forked from pvergain/Dockerfile.centos7.python36.pipenv
Dockerfile based on centos:7 Python3.6 and pipenv
# Use an official centos7 image
FROM centos:7
RUN localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
ENV LANG fr_FR.utf8
# gcc because we need regex and pyldap
# openldap-devel because we need pyldap
RUN yum update -y \
&& yum install -y https://centos7.iuscommunity.org/ius-release.rpm \
@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
import argparse
import numpy as np
import tensorflow as tf
from tensorflow.core.protobuf import rewriter_config_pb2
from model import model_fn
from data import input_fn
from utils import (
DEFAULT_PARAMS_FILE,
get_params,