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 faiss | |
| class FaissNearestNeighbors: | |
| def __init__(self, k=5): | |
| self.index = None | |
| self.y = None | |
| self.k = k |
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
| # Original source code: https://github.com/bklim5/python_video_indexer_lib | |
| import re | |
| import time | |
| import datetime | |
| import requests | |
| def get_retry_after_from_message(message): |
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.callbacks import ModelCheckpoint | |
| checkpoint_callback = ModelCheckpoint( | |
| "c3d_v5_chkpt-{epoch:02d}-{val_loss:.2f}-{val_acc:.2f}.hdf5", | |
| monitor='val_loss', | |
| verbose=1, | |
| save_best_only=True, | |
| save_weights_only=False, | |
| mode='auto', | |
| period=1) |
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.backend.tensorflow_backend import set_session | |
| from keras.backend.tensorflow_backend import clear_session | |
| from keras.backend.tensorflow_backend import get_session | |
| import tensorflow | |
| # Reset Keras Session | |
| def reset_keras(): | |
| sess = get_session() | |
| clear_session() | |
| sess.close() |
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 sys | |
| # These are the usual ipython objects, including this one you are creating | |
| ipython_vars = ['In', 'Out', 'exit', 'quit', 'get_ipython', 'ipython_vars'] | |
| # Get a sorted list of the objects and their sizes | |
| sorted([(x, sys.getsizeof(globals().get(x))) for x in dir() if not x.startswith('_') and x not in sys.modules and x not in ipython_vars], key=lambda x: x[1], reverse=True) |
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
| # https://lars76.github.io/neural-networks/object-detection/losses-for-segmentation/ | |
| def weighted_cross_entropy(beta): | |
| def convert_to_logits(y_pred): | |
| # see https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3525 | |
| y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon()) | |
| return tf.log(y_pred / (1 - y_pred)) | |
| def loss(y_true, y_pred): |
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.preprocessing.image import ImageDataGenerator | |
| # Runtime data augmentation | |
| def get_augmented( | |
| X_train, | |
| Y_train, | |
| X_val, | |
| Y_val, | |
| batch_size=32, |
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 | |
| def plot_segm_history(history): | |
| # summarize history for iou | |
| plt.figure(figsize=(12,6)) | |
| plt.plot(history.history['iou'], linewidth=3) | |
| plt.plot(history.history['val_iou'], linewidth=3) | |
| plt.suptitle('iou metric', fontsize=20) |
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
| #https://github.com/keras-team/keras-contrib/blob/master/keras_contrib/losses/jaccard.py | |
| from keras import backend as K | |
| def jaccard_distance(y_true, y_pred, smooth=100): | |
| """Jaccard distance for semantic segmentation. | |
| Also known as the intersection-over-union loss. |
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
| ################### metrics ############################ | |
| from keras import backend as K | |
| SMOOTH = 1e-12 | |
| def iou(true, pred): | |
| y_pred_pos = K.round(K.clip(pred, 0, 1)) | |
| intersection = true * y_pred_pos | |
| union = true + y_pred_pos | |
| return K.sum(intersection + SMOOTH) / K.sum(union - intersection + SMOOTH) |
NewerOlder