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
| def get_patches(img_arr, size=256, stride=256): | |
| ''' | |
| Takes single image or array of images and returns | |
| crops using sliding window method. | |
| If stride < size it will do overlapping. | |
| ''' | |
| # check size and stride | |
| if size % stride != 0: | |
| raise ValueError('size % stride must be equal 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 numpy as np | |
| import matplotlib.pyplot as plt | |
| def mask_to_red(mask, img_size=1024): | |
| ''' | |
| Converts binary segmentation mask from white to red color. | |
| Also adds alpha channel to make black background transparent. | |
| ''' | |
| c1 = mask.reshape(img_size,img_size) |
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
| ###################### unet ############################ | |
| from keras.models import Model | |
| from keras.layers import * | |
| def upsample_conv(filters, kernel_size, strides, padding): | |
| return Conv2DTranspose(filters, kernel_size, strides=strides, padding=padding) | |
| def upsample_simple(filters, kernel_size, strides, padding): |
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) |
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
| 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
| 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
| # 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
| 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
| 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() |
OlderNewer