This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import HTML | |
import os, base64 | |
def GetLink(fname): | |
with open(fname, 'rb') as f: | |
data = base64.b64encode(f.read()) | |
return HTML('<a href="data:application/binary;base64,{0}" download={1}>{1}</a>'.format(data.decode(), os.path.basename(fname))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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] | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer