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 keras.backend as K | |
| from keras.backend.tensorflow_backend import _to_tensor | |
| from keras.losses import binary_crossentropy | |
| def dice_coef_clipped(y_true, y_pred, smooth=1.0): | |
| y_true_f = K.flatten(K.round(y_true)) | |
| y_pred_f = K.flatten(K.round(y_pred)) | |
| intersection = K.sum(y_true_f * y_pred_f) | |
| return 100. * (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + 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
| def plot_keras_history(history, measures): | |
| """ | |
| history: Keras training history | |
| measures = list of names of measures | |
| """ | |
| rows = len(measures) // 2 + len(measures) % 2 | |
| fig, panels = plt.subplots(rows, 2, figsize=(15, 5)) | |
| plt.subplots_adjust(top = 0.99, bottom=0.01, hspace=0.4, wspace=0.2) | |
| try: | |
| panels = [item for sublist in panels for item in sublist] |
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
| # Custom metrics for Keras from Scikit-learn | |
| from sklearn.metrics import roc_auc_score | |
| from sklearn.metrics import average_precision_score | |
| def auroc(y_true, y_pred): | |
| return tf.py_func(roc_auc_score, (y_true, y_pred), tf.double) | |
| def mAP(y_true, y_pred): | |
| return tf.py_func(average_precision_score, (y_true, y_pred), tf.double) |
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 rle_encode(mask): | |
| """Encodes a mask in Run Length Encoding (RLE). | |
| Returns a string of space-separated values. | |
| """ | |
| assert mask.ndim == 2, "Mask must be of shape [Height, Width]" | |
| # Flatten it column wise | |
| m = mask.T.flatten() | |
| # Compute gradient. Equals 1 or -1 at transition points | |
| g = np.diff(np.concatenate([[0], m, [0]]), n=1) | |
| # 1-based indicies of transition points (where gradient != 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
| def from_p_to_logit(prob): | |
| return prob / (1 - prob) | |
| def from_logit_to_p(logit): | |
| return logit / (1 + logit) | |
| def recalibrate_prob(prob, old_baseline, new_baseline): | |
| """ | |
| Recalibrates the probability | |
| from a logistic regression |
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 random_crop(img, random_crop_size): | |
| # Note: image_data_format is 'channel_last' | |
| assert img.shape[2] == 3 | |
| height, width = img.shape[0], img.shape[1] | |
| dy, dx = random_crop_size | |
| x = np.random.randint(0, width - dx + 1) | |
| y = np.random.randint(0, height - dy + 1) | |
| return img[y:(y+dy), x:(x+dx), :] | |
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 sklearn.datasets import load_boston | |
| from sklearn.ensemble import GradientBoostingRegressor | |
| from sklearn.ensemble import BaggingRegressor | |
| import pandas as pd | |
| import numpy as np | |
| boston = load_boston() | |
| X = pd.DataFrame(boston['data'], columns=boston['feature_names']) | |
| y = boston['target'] |
NewerOlder