Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
ruslangrimov / perlin_noise.py
Last active October 27, 2017 12:19
A python realization of the 2D Perlin noise
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from itertools import product, count
# generate uniform unit vectors
def generate_grid_vectors(n):
'Generates matrix NxN of unit length vectors'
v = np.random.uniform(-1, 1, (n, n, 2))
@ruslangrimov
ruslangrimov / keras-tensorflow-model-profiling.py
Last active October 15, 2019 20:51
Profiling a Keras-TensorFlow model
import tensorflow as tf
from tensorflow.python.client import timeline
from keras import backend as K
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
model = ... # A Keras model
fn = K.function(model.inputs, model.outputs, options=run_options, run_metadata=run_metadata)
from keras import optimizers
from keras import losses
import numpy as np
input_img_data = np.random.random((1,) + K.int_shape(model.inputs[0])[1:])
input_img = K.variable(input_img_data)
inp = Input(tensor=input_img, batch_shape=input_img_data.shape)
out = model(inp)
class MyCallBack(Callback):
def __init__(self, x_v, y_v):
super(MyCallBack, self).__init__()
self.x_v, self.y_v = x_v, y_v
self.ps = []
def on_epoch_end(self, epoch, logs={}):
p = self.model.predict(self.x_v).flatten()
self.ps.append(p)
logs['val_loss'] = log_loss(self.x_y, p)
@ruslangrimov
ruslangrimov / show_base64_link.py
Created February 6, 2018 18:29
Shows base64 link to download a file from a jupyter notebook
@ruslangrimov
ruslangrimov / keras_tf_sess_gpu_mem.py
Created May 30, 2018 02:33
Make Keras use only a fraction of GPU memory
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.33
set_session(tf.Session(config=config))
@ruslangrimov
ruslangrimov / get_gpu_info.py
Created June 16, 2018 21:25
GPU free memory
from subprocess import Popen, PIPE
import pandas as pd
SYMBOLS = {
'customary': ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
'zetta', 'iotta'),
'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
'iec_60027_2': ('BiB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB',
'YiB'),
@ruslangrimov
ruslangrimov / calculate_receptive_field_size.py
Last active July 16, 2018 19:54
Get receptive field size of a neuron
'''
Code from https://medium.com/mlreview/a-guide-to-receptive-field-arithmetic-for-convolutional-neural-networks-e0f514068807
'''
import math
# [kernel_size, stride, pad_size], ...
convnet = [[8, 1, 0], [2, 2, 0],
[5, 1, 0], [2, 2, 0],
[3, 1, 0], [2, 2, 0],
[2, 1, 0], [2, 2, 0]
]
@ruslangrimov
ruslangrimov / keras-theano-to-tf.py
Last active July 16, 2018 19:56
Convert Keras Theano model to TensorFlow model
import os
import keras.backend as K
from keras.models import load_model, model_from_json
import numpy as np
import json
def switch_backend(backend):
if backend == 'theano':
# os.environ['KERAS_BACKEND'] = 'theano'
@ruslangrimov
ruslangrimov / keras_split_last_layer.py
Last active August 28, 2018 18:11
Split the last layer of a keras model into a linear layer and an activation layer
import tempfile
from keras.models import load_model
from keras import activations
from keras.layers import Activation
def split_last_layer(model):
def apply_modifications(model, custom_objects=None):
model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5')
try:
model.save(model_path)