<jupyter_start><jupyter_text>Models and loss<jupyter_code>def get_denoise_model(shape, do = 0, activate = 'selu'):
  
    inputs = Input(shape)

    ## Encoder starts
    conv1 = Conv2D(16, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    ## Bottleneck
    conv2 = Conv2D(32, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)

    ## Now the decoder starts
    up3 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv2))
    merge3 = concatenate([conv1,up3], axis = -1)
    conv3 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge3)

    conv4 = Conv2D(1, 3,  padding = 'same')(conv3)

    shallow_net = Model(inputs = inputs, outputs = conv4)

    return shallow_net



def get_descriptor_model(shape, activate= 'relu'):
  
    '''Architecture copies HardNet architecture'''

    init_weights = keras.initializers.he_normal()

    descriptor_model = Sequential()
    descriptor_model.add(Conv2D(32, 3, padding='same', input_shape=shape, use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))

    descriptor_model.add(Conv2D(32, 3, padding='same', use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))

    descriptor_model.add(Conv2D(64, 3, padding='same', strides=2, use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))

    descriptor_model.add(Conv2D(64, 3, padding='same', use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))

    descriptor_model.add(Conv2D(128, 3, padding='same', strides=2,  use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))

    descriptor_model.add(Conv2D(128, 3, padding='same', use_bias = True, kernel_initializer=init_weights))
    descriptor_model.add(BatchNormalization(axis = -1))
    descriptor_model.add(Activation(activate))
    descriptor_model.add(Dropout(0.3))

    descriptor_model.add(Conv2D(128, 8, padding='valid', use_bias = True, kernel_initializer=init_weights))

    # Final descriptor reshape
    descriptor_model.add(Reshape((128,)))

    return descriptor_model
  

def triplet_loss(x):
  
    output_dim = 128
    a, p, n = x
    _alpha = 1.0
    positive_distance = K.mean(K.square(a - p), axis=-1)
    negative_distance = K.mean(K.square(a - n), axis=-1)

    return K.expand_dims(K.maximum(0.0, positive_distance - negative_distance + _alpha), axis = 1)<jupyter_output><empty_output><jupyter_text>Denoising Image Patches<jupyter_code>from keras.layers import LeakyReLU
shape = (32, 32, 1)
denoise_model = keras.models.load_model('./denoise_base.h5')<jupyter_output><empty_output><jupyter_text>Vary Learning Rate<jupyter_code>from keras.layers import Lambda
shape = (32, 32, 1)
xa = Input(shape=shape, name='a')
xp = Input(shape=shape, name='p')
xn = Input(shape=shape, name='n')

descriptor_model = get_descriptor_model( shape)
ea = descriptor_model(xa)
ep = descriptor_model(xp)
en = descriptor_model(xn)
loss = Lambda(triplet_loss)([ea, ep, en])


sgd1 = keras.optimizers.SGD(lr=0.00001, momentum=0.9, nesterov=True)
sgd2 = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
sgd3 = keras.optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True)
sgd4 = keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True)
sgd5 = keras.optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True)

descriptor_model_trip_sgd1 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd2 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd3 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd4 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd5 = Model(inputs=[xa, xp, xn], outputs=loss)

descriptor_model_trip_sgd1.compile(loss='mean_absolute_error', optimizer=sgd1)
descriptor_model_trip_sgd2.compile(loss='mean_absolute_error', optimizer=sgd2)
descriptor_model_trip_sgd3.compile(loss='mean_absolute_error', optimizer=sgd3)
descriptor_model_trip_sgd4.compile(loss='mean_absolute_error', optimizer=sgd4)
descriptor_model_trip_sgd5.compile(loss='mean_absolute_error', optimizer=sgd5)
### Descriptor loading and training
# Loading images
hPatches = HPatches(train_fnames=train_fnames, test_fnames=test_fnames,
                    denoise_model=denoise_model, use_clean=False)

# Creating training generator
training_generator = DataGeneratorDesc(*hPatches.read_image_file(hpatches_dir, train=1), num_triplets=10000)
# Creating validation generator
val_generator = DataGeneratorDesc(*hPatches.read_image_file(hpatches_dir, train=0), num_triplets=10000)
plot_triplet(training_generator)
#epochs = 1
### As with the denoising model, we use a loop to save for each epoch 
## #the weights in an external website in case colab stops. 
### reset, so e.g. calling 5 times fit(epochs=1) behave as fit(epochs=5)

### If you have a model saved from a previous training session
### Load it in the next line
# descriptor_model_trip.set_weights(keras.models.load_model('./descriptor.h5').get_weights())
# descriptor_model_trip.optimizer = keras.models.load_model('./descriptor.h5').optimizer

#for e in range(epochs):
  
descriptor_history_sgd1 = descriptor_model_trip_sgd1.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd2 = descriptor_model_trip_sgd2.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd3 = descriptor_model_trip_sgd3.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd4 = descriptor_model_trip_sgd4.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd5 = descriptor_model_trip_sgd5.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)<jupyter_output><empty_output><jupyter_text>Plot Losses<jupyter_code>import matplotlib.pyplot as plt
def plot_history(history, history2, history3, history4, history5, metric = None):
  # Plots the loss history of training and validation (if existing)
  # and a given metric
  
  if metric != None:
    fig, axes = plt.subplots(2,1, figsize=(8, 10))
    #axes[0].plot(history.history[metric])
    #axes[0].plot(history2.history[metric])
    #axes[0].plot(history3.history[metric])
    #axes[0].plot(history4.history[metric])
    #axes[0].plot(history5.history[metric])
    #axes[0].plot(history6.history[metric])
    try:
      #axes[0].plot(history.history['val_'+metric])
      #axes[0].plot(history2.history['val2_'+metric])
      #axes[0].plot(history3.history['val3_'+metric])
      axes[0].legend(['lr=1e-5', 'lr=1e-4', 'lr=1e-3', 'lr=1e-2', 'lr=1e-1'], loc='upper right')
    except:
      pass
    axes[0].set_title('MAE Vs. No of Epochs for Various Learning Rates')
    axes[0].set_ylabel('Mean Absolute Error')
    axes[0].set_xlabel('Epoch')
    fig.subplots_adjust(hspace=0.5)
    axes[1].plot(history.history['loss'])
    axes[1].plot(history2.history['loss'])
    axes[1].plot(history3.history['loss'])
    axes[1].plot(history4.history['loss'])
    axes[1].plot(history5.history['loss'])
    try:
      #axes[1].plot(history.history['val_loss'])
      axes[1].legend(['lr=1e-5', 'lr=1e-4', 'lr=1e-3', 'lr=1e-2', 'lr=1e-1'], loc='upper right')
    except:
      pass
    axes[1].set_title('MAE Vs. No of Epochs for Various Learning Rates')
    axes[1].set_ylabel('Mean Absolute Error')
    axes[1].set_xlabel('Epoch')
  else:
    plt.plot(history.history['loss'])
    try:
      plt.plot(history.history['val_loss'])
      plt.legend(['Train', 'Val'])
    except:
      pass
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
  
plot_history(descriptor_history_sgd1, descriptor_history_sgd2, descriptor_history_sgd3, descriptor_history_sgd4, descriptor_history_sgd5, 'mean_absolute_error')
def plot_val_history(history, history2, history3, history4, history5, metric = None):
  # Plots the loss history of training and validation (if existing)
  # and a given metric
  
  if metric != None:
    fig, axes = plt.subplots(2,1, figsize=(8, 10))
    #axes[0].plot(history.history[metric])
    #axes[0].plot(history2.history[metric])
    #axes[0].plot(history3.history[metric])
    try:
      #axes[0].plot(history.history['val_'+metric])
      #axes[0].plot(history2.history['val_'+metric])
      #axes[0].plot(history3.history['val_'+metric])
      #axes[0].plot(history4.history['val_'+metric])
      #axes[0].plot(history5.history['val_'+metric])
      #axes[0].plot(history6.history['val_'+metric])
      axes[0].legend(['lr=1e-5', 'lr=1e-4', 'lr=1e-3', 'lr=1e-2', 'lr=1e-1'], loc='upper right')

    except:
      pass
    axes[0].set_title('Validation Loss Vs. No of Epochs for Various Learning Rates')
    axes[0].set_ylabel('Validation Loss')
    axes[0].set_xlabel('Epoch')
    fig.subplots_adjust(hspace=0.5)
    #axes[1].plot(history.history['loss'])
    #axes[1].plot(history2.history['loss'])
    #axes[1].plot(history3.history['loss'])
    try:
      axes[1].plot(history.history['val_loss'])
      axes[1].plot(history2.history['val_loss'])
      axes[1].plot(history3.history['val_loss'])
      axes[1].plot(history4.history['val_loss'])
      axes[1].plot(history5.history['val_loss'])
      #axes[1].plot(history6.history['val_loss'])
      axes[1].legend(['lr=1e-5', 'lr=1e-4', 'lr=1e-3', 'lr=1e-2', 'lr=1e-1'], loc='upper right')

    except:
      pass
    axes[1].set_title('Validation Loss Vs. No of Epochs for Various Learning Rates')
    axes[1].set_ylabel('Validation Loss')
    axes[1].set_xlabel('Epoch')
  else:
    plt.plot(history.history['loss'])
    try:
      plt.plot(history.history['val_loss'])
      plt.legend(['Train', 'Val'])
    except:
      pass
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
  
plot_val_history(descriptor_history_sgd1, descriptor_history_sgd2, descriptor_history_sgd3, descriptor_history_sgd4, descriptor_history_sgd5, 'mean_absolute_error')<jupyter_output><empty_output><jupyter_text>Vary Momentum<jupyter_code>sgd1 = keras.optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True)
sgd2 = keras.optimizers.SGD(lr=0.1, momentum=0.8, nesterov=True)
sgd3 = keras.optimizers.SGD(lr=0.1, momentum=0.7, nesterov=True)
sgd4 = keras.optimizers.SGD(lr=0.1, momentum=0.6, nesterov=True)
sgd5 = keras.optimizers.SGD(lr=0.1, momentum=0.5, nesterov=True)

descriptor_model_trip_sgd1 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd2 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd3 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd4 = Model(inputs=[xa, xp, xn], outputs=loss)
descriptor_model_trip_sgd5 = Model(inputs=[xa, xp, xn], outputs=loss)

descriptor_model_trip_sgd1.compile(loss='mean_absolute_error', optimizer=sgd1)
descriptor_model_trip_sgd2.compile(loss='mean_absolute_error', optimizer=sgd2)
descriptor_model_trip_sgd3.compile(loss='mean_absolute_error', optimizer=sgd3)
descriptor_model_trip_sgd4.compile(loss='mean_absolute_error', optimizer=sgd4)
descriptor_model_trip_sgd5.compile(loss='mean_absolute_error', optimizer=sgd5)
#epochs = 1
### As with the denoising model, we use a loop to save for each epoch 
## #the weights in an external website in case colab stops. 
### reset, so e.g. calling 5 times fit(epochs=1) behave as fit(epochs=5)

### If you have a model saved from a previous training session
### Load it in the next line
# descriptor_model_trip.set_weights(keras.models.load_model('./descriptor.h5').get_weights())
# descriptor_model_trip.optimizer = keras.models.load_model('./descriptor.h5').optimizer

#for e in range(epochs):
  
descriptor_history_sgd1 = descriptor_model_trip_sgd1.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd2 = descriptor_model_trip_sgd2.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd3 = descriptor_model_trip_sgd3.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd4 = descriptor_model_trip_sgd4.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)

descriptor_history_sgd5 = descriptor_model_trip_sgd5.fit_generator(generator=training_generator, epochs=5, verbose=1, validation_data=val_generator)


### Saves optimizer and weights
#descriptor_model_trip.save('descriptor.h5') 
### Uploads files to external hosting
#!curl -F "file=@descriptor.h5" https://file.io<jupyter_output><empty_output><jupyter_text>Plot Losses<jupyter_code>def plot_history(history, history2, history3, history4, history5, metric = None):
  # Plots the loss history of training and validation (if existing)
  # and a given metric
  
  if metric != None:
    fig, axes = plt.subplots(2,1, figsize=(8, 10))
    #axes[0].plot(history.history[metric])
    #axes[0].plot(history2.history[metric])
    #axes[0].plot(history3.history[metric])
    #axes[0].plot(history4.history[metric])
    #axes[0].plot(history5.history[metric])
    
    try:
      #axes[0].plot(history.history['val_'+metric])
      #axes[0].plot(history2.history['val2_'+metric])
      #axes[0].plot(history3.history['val3_'+metric])
      axes[0].legend(['0.9', '0.8', '0.7', '0.6', '0.5'], loc='best')
    except:
      pass
    axes[0].set_title('MAE Vs. No of Epochs for Various Momentum Values')
    axes[0].set_ylabel('Mean Absolute Error')
    axes[0].set_xlabel('Epoch')
    fig.subplots_adjust(hspace=0.5)
    axes[1].plot(history.history['loss'])
    axes[1].plot(history2.history['loss'])
    axes[1].plot(history3.history['loss'])
    axes[1].plot(history4.history['loss'])
    axes[1].plot(history5.history['loss'])
    try:
      #axes[1].plot(history.history['val_loss'])
      axes[1].legend(['0.9', '0.8', '0.7', '0.6', '0.5'], loc='best')
    except:
      pass
    axes[1].set_title('MAE Vs. No of Epochs for Various Momentum Values')
    axes[1].set_ylabel('Mean Absolute Error')
    axes[1].set_xlabel('Epoch')
  else:
    plt.plot(history.history['loss'])
    try:
      plt.plot(history.history['val_loss'])
      plt.legend(['Train', 'Val'])
    except:
      pass
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
  
plot_history(descriptor_history_sgd1, descriptor_history_sgd2, descriptor_history_sgd3, descriptor_history_sgd4, descriptor_history_sgd5, 'mean_absolute_error')
def plot_val_history(history, history2, history3, history4, history5, metric = None):
  # Plots the loss history of training and validation (if existing)
  # and a given metric
  
  if metric != None:
    fig, axes = plt.subplots(2,1, figsize=(8, 10))
    #axes[0].plot(history.history[metric])
    #axes[0].plot(history2.history[metric])
    #axes[0].plot(history3.history[metric])
    try:
      #axes[0].plot(history.history['val_'+metric])
      #axes[0].plot(history2.history['val_'+metric])
      #axes[0].plot(history3.history['val_'+metric])
      #axes[0].plot(history4.history['val_'+metric])
      #axes[0].plot(history5.history['val_'+metric])
      axes[0].legend(['0.9', '0.8', '0.7', '0.6', '0.5'], loc='best')
    except:
      pass
    axes[0].set_title('Validation Loss Vs. No of Epochs for for Various Momentum Values')
    axes[0].set_ylabel('Validation Loss')
    axes[0].set_xlabel('Epoch')
    fig.subplots_adjust(hspace=0.5)
    #axes[1].plot(history.history['loss'])
    #axes[1].plot(history2.history['loss'])
    #axes[1].plot(history3.history['loss'])
    try:
      axes[1].plot(history.history['val_loss'])
      axes[1].plot(history2.history['val_loss'])
      axes[1].plot(history3.history['val_loss'])
      axes[1].plot(history4.history['val_loss'])
      axes[1].plot(history5.history['val_loss'])
      axes[1].legend(['0.9', '0.8', '0.7', '0.6', '0.5'], loc='best')
    except:
      pass
    axes[1].set_title('Validation Loss Vs. No of Epochs for Various Momentum Values')
    axes[1].set_ylabel('Validation Loss')
    axes[1].set_xlabel('Epoch')
  else:
    plt.plot(history.history['loss'])
    try:
      plt.plot(history.history['val_loss'])
      plt.legend(['Train', 'Val'])
    except:
      pass
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    
plot_val_history(descriptor_history_sgd1, descriptor_history_sgd2, descriptor_history_sgd3, descriptor_history_sgd4, descriptor_history_sgd5, 'mean_absolute_error')<jupyter_output><empty_output><jupyter_text>Save Baseline Model<jupyter_code>sgd1 = keras.optimizers.SGD(lr=0.1, momentum=0.7, nesterov=True)

descriptor_model_trip = Model(inputs=[xa, xp, xn], outputs=loss)

descriptor_model_trip.compile(loss='mean_absolute_error', optimizer=sgd1)
#epochs = 1
### As with the denoising model, we use a loop to save for each epoch 
## #the weights in an external website in case colab stops. 
### reset, so e.g. calling 5 times fit(epochs=1) behave as fit(epochs=5)

### If you have a model saved from a previous training session
### Load it in the next line
# descriptor_model_trip.set_weights(keras.models.load_model('./descriptor.h5').get_weights())
# descriptor_model_trip.optimizer = keras.models.load_model('./descriptor.h5').optimizer

#for e in range(epochs):
  
descriptor_history = descriptor_model_trip.fit_generator(generator=training_generator, epochs=20, verbose=1, validation_data=val_generator)
descriptor_model_trip.save('descriptor_base.h5') 

### Saves optimizer and weights
#descriptor_model_trip.save('descriptor.h5') 
### Uploads files to external hosting
#!curl -F "file=@descriptor.h5" https://file.io<jupyter_output><empty_output><jupyter_text>Test mAP on Baseline Approach generate_desc_csv(descriptor_model, seqs_test, denoise_model=denoise_model, use_clean=False)  Verification<jupyter_code>!python ./hpatches-benchmark/hpatches_eval.py --descr-name=custom --descr-dir=/content/keras_triplet_descriptor/out/ --task=verification --delimiter=";"
!python ./hpatches-benchmark/hpatches_results.py --descr=custom --results-dir=./hpatches-benchmark/results/ --task=verification<jupyter_output><empty_output><jupyter_text>Matching<jupyter_code>!python ./hpatches-benchmark/hpatches_eval.py --descr-name=custom --descr-dir=/content/keras_triplet_descriptor/out/ --task=matching --delimiter=";"
!python ./hpatches-benchmark/hpatches_results.py --descr=custom --results-dir=./hpatches-benchmark/results/ --task=matching<jupyter_output><empty_output><jupyter_text>Retrieval<jupyter_code>!python ./hpatches-benchmark/hpatches_eval.py --descr-name=custom --descr-dir=/content/keras_triplet_descriptor/out/ --task=retrieval --delimiter=";"
!python ./hpatches-benchmark/hpatches_results.py --descr=custom --results-dir=./hpatches-benchmark/results/ --task=retrieval<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14793021575307114, 'fraction_numerical': 0.02947248890265304, 'mean_word_length': 5.601362862010221, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '97023', 'license': 'MIT', 'n_tokens_mistral': 7163, 'n_tokens_neox': 6825, 'n_words': 1117, 'path': 'Tests/Baseline_approach_test.ipynb', 'repo_name': 'RajanPatel97/Denoising-and-Representation-Learning', 'size': '19374'}