Skip to content

Instantly share code, notes, and snippets.

View doleron's full-sized avatar
💭
I may be slow to respond.

Luiz doleron doleron

💭
I may be slow to respond.
View GitHub Profile
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
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")
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)
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))
model.compile(loss=contrastive_loss_with_margin(margin=1), optimizer=tf.keras.optimizers.RMSprop(),
metrics=[Custom_Accuracy(), Custom_Precision(), Custom_Recall()])
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):
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,
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
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]]]
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