This file contains 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 RandomInvert(tf.keras.layers.Layer): | |
def __init__(self, max_value = 255, factor=0.5, **kwargs): | |
super().__init__(**kwargs) | |
self.factor = factor | |
self.max_value = max_value | |
def call(self, x): | |
if tf.random.uniform([]) < self.factor: | |
x = (self.max_value - x) | |
return x |
This file contains 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 checking_wrong_predictions(): | |
for images, labels in validation_ds: | |
predictions = model.predict(images, verbose = 0) | |
for i in range(VALIDATION_BATCH_SIZE): | |
img_A = (images[0][i].numpy()*255).astype("uint8") | |
img_B = (images[1][i].numpy()*255).astype("uint8") |
This file contains 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 | |
y_pred_test = model.predict(validation_ds) | |
y_pred_int = tf.cast(tf.math.less(y_pred_test, 0.5), tf.int32) | |
y_true = np.concatenate([y for x, y in validation_ds], axis=0) | |
tf.math.confusion_matrix(y_true, y_pred_int) |
This file contains 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 = model.evaluate(validation_ds) | |
test_loss = metrics[0] | |
test_accuracy = metrics[1] | |
test_precision = metrics[2] | |
test_recall = metrics[3] | |
print("Test Loss = {:.4f}, Test Accuracy = {:.4f}, Test Precision = {:.4f}, Test Recall = {:.4f}" | |
.format(test_loss, test_accuracy, test_precision, test_recall)) |
This file contains 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
model.compile(loss=contrastive_loss_with_margin(margin=1), optimizer=tf.keras.optimizers.RMSprop(), | |
metrics=[Custom_Accuracy(), Custom_Precision(), Custom_Recall()]) |
This file contains 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 Custom_Precision(tf.keras.metrics.Precision): | |
def update_state(self, y_true, y_pred, sample_weight=None): | |
y_pred_fix = tf.math.less(y_pred, 0.5) | |
y_pred_fix = tf.cast(y_pred_fix, y_pred.dtype) | |
return super().update_state(y_true, y_pred_fix, sample_weight) | |
class Custom_Recall(tf.keras.metrics.Recall): |
This file contains 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
EPOCHS = 200 | |
model.compile(loss=contrastive_loss_with_margin(margin=1), optimizer=tf.keras.optimizers.RMSprop(), | |
metrics=[Custom_Accuracy(), Custom_Precision(), Custom_Recall()]) | |
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience = 30, start_from_epoch = 10) | |
checkpoint = tf.keras.callbacks.ModelCheckpoint(model_file, monitor="val_loss", mode="min", save_best_only=True, verbose=1) | |
history = model.fit(train_ds, |
This file contains 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 contrastive_loss_with_margin(margin=1): | |
def contrastive_loss(y_true, y_pred): | |
square_pred = K.square(y_pred) | |
margin_square = K.square(K.maximum(margin - y_pred, 0)) | |
return (y_true * square_pred + (1 - y_true) * margin_square) | |
return contrastive_loss |
This file contains 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 create_pairs(x, digit_indices): | |
pairs = [] | |
labels = [] | |
n = min([len(digit_indices[d]) for d in range(CLASSES_SIZE)]) - 1 | |
for d in range(CLASSES_SIZE): | |
for i in range(n): | |
z1, z2 = digit_indices[d][i], digit_indices[d][i + 1] | |
pairs += [[x[z1], x[z2]]] |
This file contains 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
training_files = [] | |
training_labels = [] | |
validation_files = [] | |
validation_labels = [] | |
for label in range(CLASSES_SIZE): | |
indexes = np.where(labels == label)[0] | |
threshold = len(indexes) * 80 // 100 |
NewerOlder