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
output = Lambda(euclidean_distance, name="output_layer")([vect_output_a, vect_output_b]) |
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
INPUT_SHAPE = (INPUT_SIZE, INPUT_SIZE, 3) | |
def initialize_base_network(): | |
inputs = tf.keras.layers.Input(INPUT_SHAPE) | |
base_model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=INPUT_SHAPE, include_top=False, weights='imagenet') | |
base_model.trainable = True | |
fine_tune_at = len(base_model.layers)-int(len(base_model.layers)*.10) | |
for layer in base_model.layers[:fine_tune_at]: |
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 buid_model(): | |
base_network = initialize_base_network() | |
input_a = Input(shape=INPUT_SHAPE, name="left_input") | |
vect_output_a = base_network(input_a) | |
input_b = Input(shape=INPUT_SHAPE, name="right_input") | |
vect_output_b = base_network(input_b) |
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
VALIDATION_BATCH_SIZE = 2 | |
def build_validation_dataset(): | |
pairs_tensor = tf.convert_to_tensor(validation_pairs) | |
labels_tensor = tf.convert_to_tensor(validation_pairs_labels) | |
result = tf.data.Dataset.from_tensor_slices((pairs_tensor, labels_tensor)) | |
result = result.map(lambda pair, label: (load_images(pair), label)) |
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
INPUT_SIZE = 244 | |
TRAINING_BATCH_SIZE = 8 | |
def load_image(file_name): | |
raw = tf.io.read_file(file_name) | |
image = tf.io.decode_image(raw, expand_animations = False, channels=3) | |
image = tf.image.resize(image, size=(INPUT_SIZE, INPUT_SIZE), preserve_aspect_ratio=True) | |
image = tf.image.resize_with_crop_or_pad(image, INPUT_SIZE, INPUT_SIZE) | |
image = tf.cast(image, tf.float32) / 255.0 | |
return image |
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 build_training_dataset(): | |
pairs_tensor = tf.convert_to_tensor(training_pairs) | |
labels_tensor = tf.convert_to_tensor(training_pairs_labels) | |
result = tf.data.Dataset.from_tensor_slices((pairs_tensor, labels_tensor)) | |
result = result.map(lambda pair, label: (load_images(pair), label)) | |
result = result.shuffle(100, reshuffle_each_iteration=True) | |
result = result.repeat() |
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
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience = 10) | |
history = model.fit(train_ds, validation_data=validation_ds, epochs=EPOCHS, callbacks=[early_stop]) |
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
data_augmentation = tf.keras.Sequential([ | |
tf.keras.layers.RandomFlip("horizontal"), | |
tf.keras.layers.RandomRotation(0.01), | |
tf.keras.layers.RandomBrightness(factor=0.2, value_range=(0., 1.)), | |
tf.keras.layers.GaussianNoise(0.002), | |
tf.keras.layers.RandomZoom(height_factor=(-0.1, 0.1)) | |
]) |
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
from tensorflow.keras import backend as K | |
def euclidean_distance(x, y): | |
sum_square = K.sum(K.square(x - y), axis=1, keepdims=True) | |
return K.sqrt(K.maximum(sum_square, K.epsilon())) |
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
plt.figure(figsize=(12, 10)) | |
test_list = list(test_ds.take(20).as_numpy_iterator()) | |
image, labels = test_list[0] | |
for i in range(len(test_list)): | |
ax = plt.subplot(4, 5, i + 1) | |
image, labels = test_list[i] |