Skip to content

Instantly share code, notes, and snippets.

@sergeyprokudin
sergeyprokudin / count_flops.py
Last active October 24, 2019 13:36
Count trainable parameters and FLOPs per sample of a Keras model
import numpy as np
def count_conv_params_flops(conv_layer, verbose=1):
# out shape is n_cells_dim1 * (n_cells_dim2 * n_cells_dim3)
out_shape = conv_layer.output.shape.as_list()
n_cells_total = np.prod(out_shape[1:-1])
n_conv_params_total = conv_layer.count_params()
@05jd
05jd / flops.py
Created August 26, 2018 15:10
Counting FLOPs of a Keras model
"""Code Snippet for counting FLOPs of a model.
Not final version, it will be updated to improve the usability.
"""
import os.path
import tempfile
import tensorflow as tf
@jeremyjordan
jeremyjordan / sgdr.py
Last active December 4, 2023 13:41
Keras Callback for implementing Stochastic Gradient Descent with Restarts
from keras.callbacks import Callback
import keras.backend as K
import numpy as np
class SGDRScheduler(Callback):
'''Cosine annealing learning rate scheduler with periodic restarts.
# Usage
```python
schedule = SGDRScheduler(min_lr=1e-5,
import numpy as np
from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import classification_report, confusion_matrix
#Start
train_data_path = 'F://data//Train'
@codingPingjun
codingPingjun / create_prior_box.py
Created January 27, 2017 03:11
SSD prior box creation
import pickle
import numpy as np
import pdb
img_width, img_height = 300, 300
box_configs = [
{'layer_width': 38, 'layer_height': 38, 'num_prior': 3, 'min_size': 30.0,
'max_size': None, 'aspect_ratios': [1.0, 2.0, 1/2.0]},
{'layer_width': 19, 'layer_height': 19, 'num_prior': 6, 'min_size': 60.0,
'max_size': 114.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
@fmder
fmder / elastic_transform.py
Last active August 22, 2021 14:54
Elastic transformation of an image in Python
import numpy
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
def elastic_transform(image, alpha, sigma, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
@cadrev
cadrev / pandas_shuffle.py
Last active August 22, 2020 08:40
Shuffle the rows of a python pandas dataframe
'''
Title : Pandas Row Shuffler
Author : Felan Carlo Garcia
'''
import numpy as np
import pandas as pd
def shuffler(filename):
df = pd.read_csv(filename, header=0)
# return the pandas dataframe