Skip to content

Instantly share code, notes, and snippets.

View tldrafael's full-sized avatar

Rafael Toledo tldrafael

View GitHub Profile
@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 / 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):
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 / 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])
@tldrafael
tldrafael / pdb_autocomplete.py
Created October 31, 2019 12:43
Pdb autocomple
import rlcompleter
import pdb
pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
pdb.set_trace()
# Oneline to insert into code
import rlcompleter; import pdb; pdb.Pdb.complete = rlcompleter.Completer(locals()).complete; pdb.set_trace()
@tldrafael
tldrafael / using_parseargs.py
Last active October 30, 2019 15:53
Snippet to use parseargs
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-e', type=str, help='Environment variable', required=True, dest='var1',
choices=['staging', 'production'])
parser.add_argument('--hypertuning', help='Run hypertuning process', action='store_true', dest='var2')
return parser.parse_args()
@tldrafael
tldrafael / annotation_image_tools.txt
Created October 26, 2019 21:05
Annotation Tools for Image Dectation - Open Source
# Easy to go tools
- http://www.robots.ox.ac.uk/~vgg/software/via/via-1.0.6.html
- https://github.com/virajmavani/semi-auto-image-annotation-tool
- https://labelbox.com/ -> Free until 2500 images per year.
# More references
- https://github.com/jsbroks/awesome-dataset-tools
- https://www.datasetlist.com/tools/
@tldrafael
tldrafael / plot.py
Created October 20, 2019 18:34
Plotting subplots snippet
import matplotlib.pyplot as plt
fig, axs = plt.subplots(4, figsize=(12, 12))
for i in range(4):
axs[i].imshow(sample_masks[:, :, i])
axs[i].axis('off')
# Code snippet to train lightgbm models quickly, using the number of iterations based on CV
import lightgbm as lgb
lgb_dftrain = lgb.Dataset(features_train, target_train)
params = {'boosting_type': 'gbdt',
'objective': 'binary',
'enable_bundle': True,
'max_conflict_rate': 0,
'max_depth': 20,
@tldrafael
tldrafael / pretrainedmodels_webcam.py
Last active July 11, 2019 20:35
Run Keras ResNet50 in Webcam
import cv2
import numpy as np
from keras.applications import ResNet50
from keras.applications import imagenet_utils
from keras.preprocessing.image import img_to_array
model = ResNet50(weights='imagenet')
camera = cv2.VideoCapture(0)
# If possible control the FPS to constrain the labels appearances