Skip to content

Instantly share code, notes, and snippets.

View tldrafael's full-sized avatar

Rafael Toledo tldrafael

View GitHub Profile
@tldrafael
tldrafael / example_keras_inputgenerator.py
Created January 17, 2020 15:18
Generic example of input generator for Keras
import numpy as np
class inputGen:
def __init__(self, batch_size, X, y, shuffle=True):
self.batch_size = batch_size
self.X = X
self.y = y
self.cursor = 0
self.n_samples = X.shape[0]
self.ids_sequence = np.arange(X.shape[0])
from glob import iglob
def return_impaths(dpath):
impaths = [list(iglob('{}/*.{}'.format(dpath, e))) for e in ['png', 'jpg', 'jpeg']]
impaths = sum(impaths, [])
return impaths
@tldrafael
tldrafael / squeezenet.py
Last active February 11, 2020 15:54
Keras SqueezeNet architecture
# https://arxiv.org/pdf/1602.07360.pdf
#
from keras import backend as K
from keras.layers import Input, Convolution2D, MaxPooling2D, Activation, concatenate
from keras.layers import GlobalAveragePooling2D
from keras.models import Model
class SqueezeNet:
def __init__(self, input_shape, n_classes):
@tldrafael
tldrafael / LineProfiler.py
Created February 12, 2020 16:19
Decorator to profile python functions
from line_profiler import LineProfiler
def do_profile(follow=[]):
def inner(func):
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
profiler.add_function(func)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
@tldrafael
tldrafael / CONFIGURE
Last active February 18, 2020 17:40
Configuring EC2 AWS g4 GPUs machine
# Install cuda-10.0 because the newest one (for now the version 10.2) mismatchs the tensorflow 1.14.0 requirements
wget https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd6
dpkg -i cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd6
apt-key add /var/cuda-repo-10-0-local-10.0.130-410.48/7fa2af80.pub
apt-get install cuda
# This cuda version install nvidia v410 drivers. Remove it.
@tldrafael
tldrafael / log.py
Last active May 13, 2022 17:56
Basic python logging configuration
import logging
from logging.handlers import RotatingFileHandler
import traceback
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('app.log', mode='a', maxBytes=50e6, backupCount=2)
formatter = logging.Formatter('%(asctime)s,%(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
@tldrafael
tldrafael / try_except_traceback.py
Created March 5, 2020 14:39
Example to get traceback info
import traceback
import sys
try:
pass
except Exception as e:
traceback_info = ''.join(traceback.format_exception(*sys.exc_info()))
logger.info(traceback_info)
xgb_params = {'objective': 'multi:softmax',
'num_class': 19,
'tree_method': 'exact',
# 'max_bin': 64,
'colsample_bytree': 0.75,
'subsample': 0.75,
# 'lambda': 2,
# 'alpha': 2,
# 'min_child_weight': 10,
# 'max_delta_step': 10,
@tldrafael
tldrafael / deltaE2000_loss.py
Created June 8, 2020 12:51
deltaE2000 loss function
# Code adapted from skimage
# https://github.com/riaanvddool/scikits-image/blob/master/skimage/color/delta_e.py#L123
import numpy as np
import tensorflow as tf
def tf_deg2rad(deg):
return (deg / 180) * np.pi
def tf_rad2deg(rad):
@tldrafael
tldrafael / argparse_example
Created November 9, 2020 21:45
Simple snippet to use argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-d', type=str, help='directory path', dest='dpath')
parser.add_argument('-f', type=str, help='filepath', dest='fpath')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
if not any([args.dpath, args.fpath]):