Ejemplo n.º 1
0
def main():
    args = arg()
    model = SiameseNetwork()
    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)

    train, test = chainer.datasets.get_mnist(ndim=3)

    train_iter = chainer.iterators.SerialIterator(train, args.batch)
    test_iter = chainer.iterators.SerialIterator(test, args.batch,
                                                 repeat=False, shuffle=False)

    updater = SiameseUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(extensions.LogReport())
    trainer.extend(extensions.PrintReport(
        ['epoch', 'elapsed_time', 'main/loss']))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(plot_testdata(model, test, args.batch), trigger=(1, 'epoch'))

    trainer.run()
def main():
    dataset_path = 'gen_simulated_data'
    use_augmentation = False
    learning_rate = 10e-4
    batch_size = 32
    # Learning Rate multipliers for each layer
    learning_rate_multipliers = {}
    learning_rate_multipliers['Conv1'] = 1
    learning_rate_multipliers['Conv2'] = 1
    learning_rate_multipliers['Conv3'] = 1
    learning_rate_multipliers['Conv4'] = 1
    #learning_rate_multipliers['Conv5'] = 1
    learning_rate_multipliers['Dense1'] = 1
    # l2-regularization penalization for each layer
    l2_penalization = {}
    l2_penalization['Conv1'] = 1e-2
    l2_penalization['Conv2'] = 1e-2
    l2_penalization['Conv3'] = 1e-2
    l2_penalization['Conv4'] = 1e-2
    #l2_penalization['Conv5'] = 1e-2
    l2_penalization['Dense1'] = 1e-4
    # Path where the logs will be saved
    tensorboard_log_path = './logs/siamese_net_lr10e-4'
    siamese_network = SiameseNetwork(
        dataset_path=dataset_path,
        learning_rate=learning_rate,
        batch_size=batch_size,
        use_augmentation=use_augmentation,
        learning_rate_multipliers=learning_rate_multipliers,
        l2_regularization_penalization=l2_penalization,
        tensorboard_log_path=tensorboard_log_path)
    # Final layer-wise momentum (mu_j in the paper)
    momentum = 0.9
    # linear epoch slope evolution
    momentum_slope = 0.01
    # support_set指支持一次"图像验证"的图像集合,即1:N image evaluation的N
    support_set_size = 20
    # evaluation时需要进行验证的图像数目
    number_of_tasks_per_bucket = 20
    evaluate_each = 500
    number_of_train_iterations = 1000000

    validation_accuracy = siamese_network.train_siamese_network(
        number_of_iterations=number_of_train_iterations,
        support_set_size=support_set_size,
        final_momentum=momentum,
        momentum_slope=momentum_slope,
        evaluate_each=evaluate_each,
        model_name='siamese_net_lr10e-4')
    if validation_accuracy == 0:
        evaluation_accuracy = 0
    else:
        # Load the weights with best validation accuracy
        siamese_network.model.load_weights('.models/siamese_net_lr10e-4.h5')
        evaluation_accuracy = siamese_network.chars_loader.one_shot_test(
            siamese_network.model, support_set_size,
            number_of_tasks_per_bucket, False)

    print('Final Evaluation Accuracy = ' + str(evaluation_accuracy))
    def __init__(self):
        self.siamese_network = SiameseNetwork()
        self.input_shape = self.INPUT_SHAPE
        self.triplet_model = self.siamese_network.model

        self.train_siamese_network()
        self.model = self.trained_model()
        self.save_model_weights()
def main():
    dataset_path = 'Omniglot Dataset'
    use_augmentation = True
    learning_rate = 10e-4

    #batch_size = 128
    batch_size = 256
    # Learning Rate multipliers for each layer
    learning_rate_multipliers = {}
    learning_rate_multipliers['Conv1'] = 1
    learning_rate_multipliers['Conv2'] = 1
    learning_rate_multipliers['Conv3'] = 1
    learning_rate_multipliers['Conv4'] = 1
    learning_rate_multipliers['Dense1'] = 1
    # l2-regularization penalization for each layer
    l2_penalization = {}
    l2_penalization['Conv1'] = 1e-2
    l2_penalization['Conv2'] = 1e-2
    l2_penalization['Conv3'] = 1e-2
    l2_penalization['Conv4'] = 1e-2
    l2_penalization['Dense1'] = 1e-4
    # Path where the logs will be saved
    tensorboard_log_path = './logs/siamese_net_lr10e-4'
    siamese_network = SiameseNetwork(
        dataset_path=dataset_path,
        learning_rate=learning_rate,
        batch_size=batch_size,
        use_augmentation=use_augmentation,
        learning_rate_multipliers=learning_rate_multipliers,
        l2_regularization_penalization=l2_penalization,
        tensorboard_log_path=tensorboard_log_path)
    # Final layer-wise momentum (mu_j in the paper)
    momentum = 0.9
    # linear epoch slope evolution
    momentum_slope = 0.01
    support_set_size = 20
    evaluate_each = 1000

    #number_of_train_iterations = 250000
    number_of_train_iterations = 125000

    validation_accuracy = siamese_network.train_siamese_network(
        number_of_iterations=number_of_train_iterations,
        support_set_size=support_set_size,
        final_momentum=momentum,
        momentum_slope=momentum_slope,
        evaluate_each=evaluate_each,
        model_name='siamese_net_lr10e-4')
    if validation_accuracy == 0:
        evaluation_accuracy = 0
    else:
        # Load the weights with best validation accuracy
        siamese_network.model.load_weights('./models/siamese_net_lr10e-4.h5')
        evaluation_accuracy = siamese_network.omniglot_loader.one_shot_test(
            siamese_network.model, 20, 40, False)

    print('Final Evaluation Accuracy = ' + str(evaluation_accuracy))
def main():
    learning_rate = 10e-4
    batch_size = 32
    epochs = 15
    height = 256
    width = 256
    # Learning Rate multipliers for each layer
    learning_rate_multipliers = {}
    learning_rate_multipliers['Conv1'] = 1
    learning_rate_multipliers['Conv2'] = 1
    learning_rate_multipliers['Conv3'] = 1
    learning_rate_multipliers['Conv4'] = 1
    learning_rate_multipliers['Dense1'] = 1
    # l2-regularization penalization for each layer
    l2_penalization = {}
    l2_penalization['Conv1'] = 1e-2
    l2_penalization['Conv2'] = 1e-2
    l2_penalization['Conv3'] = 1e-2
    l2_penalization['Conv4'] = 1e-2
    l2_penalization['Dense1'] = 1e-4
    # Path where the logs will be saved
    tensorboard_log_path = './logs/siamese_net_lr10e-4'
    siamese_network = SiameseNetwork(
        learning_rate=learning_rate,
        batch_size=batch_size,
        epochs=epochs,
        learning_rate_multipliers=learning_rate_multipliers,
        l2_regularization_penalization=l2_penalization,
        tensorboard_log_path=tensorboard_log_path)

    # Data
    os.chdir("/data8t/ljq/whale_data/whale_data/siamese_networks/")
    train_data_temp = np.load('training_data.npy')
    train_data = [train_data_temp[0]]
    train_data.append(train_data_temp[1])
    train_label = np.load('training_label.npy')
    val_data_temp = np.load('validation_data.npy')
    val_data = [val_data_temp[0]]
    val_data.append(val_data_temp[1])
    val_label = np.load('validation_label.npy')
    # train_base_num = 1000
    # train_data = [np.zeros((train_base_num * 6, height, width, 3)) for j in range(2)]
    # train_label = np.zeros((train_base_num * 6, 1))
    # val_data = [np.ones((train_base_num * 2, height, width, 3)) for j in range(2)]
    # val_label = np.ones((train_base_num * 2, 1))

    siamese_network.train_siamese_network(model_name='siamese_net_whale',
                                          train_data=train_data,
                                          train_label=train_label,
                                          val_data=val_data,
                                          val_label=val_label)
Ejemplo n.º 6
0
def main():
    # test = Image.open("./data/1.pgm")
    # test.show()
    # print (test)

    print("Training starts...")
    folder_dataset = dset.ImageFolder(root=Config.training_dir)
    siamese_dataset = Contrastive_Dataset(imageFolderDataset=folder_dataset,
                                          transform=transforms.Compose([
                                              transforms.Resize((100, 100)),
                                              transforms.ToTensor()
                                          ]),
                                          should_invert=False)

    train_dataloader = DataLoader(siamese_dataset,
                                  shuffle=True,
                                  num_workers=8,
                                  batch_size=Config.train_batch_size)

    net = SiameseNetwork()
    criterion = ContrastiveLoss()
    optimizer = optim.Adam(net.parameters(), lr=0.0005)

    counter = []
    loss_history = []
    iteration_number = 0

    for epoch in range(0, Config.train_number_epochs):
        for i, data in enumerate(train_dataloader, 0):
            img0, img1, label = data
            img0, img1, label = Variable(img0), Variable(img1), Variable(label)
            output1, output2 = net(img0, img1)
            optimizer.zero_grad()
            loss_contrastive = criterion(output1, output2, label)
            loss_contrastive.backward()
            optimizer.step()
            if i % 10 == 0:
                print("Epoch number {}\n Current loss {}\n".format(
                    epoch, loss_contrastive.data[0]))
                iteration_number += 10
                counter.append(iteration_number)
                loss_history.append(loss_contrastive.data[0])
    torch.save(net.state_dict(), "trained_weights.pt")
    # save_checkpoint({
    #     'epoch': epoch + 1,
    #     })

    show_plot(counter, loss_history)
Ejemplo n.º 7
0
def main():
    dataset_path = 'data/split_dataset_NOMASK/train'
    model_name = 'model_train_NOMASK'

    use_augmentation = True
    learning_rate = 1e-2
    batch_size = 16

    # Learning Rate multipliers for each layer
    learning_rate_multipliers = {'Conv1': 1, 'Conv2': 1, 'Conv3': 1, 'Conv4': 1, 'Dense1': 1}

    # l2-regularization penalization for each layer
    l2_penalization = {'Conv1': 1e-2, 'Conv2': 1e-2, 'Conv3': 1e-2, 'Conv4': 1e-2, 'Dense1': 1e-4}

    # Path where the logs will be saved
    tensorboard_log_path = f'./logs/{model_name}'
    siamese_network = SiameseNetwork(
        dataset_path=dataset_path,
        learning_rate=learning_rate,
        batch_size=batch_size, use_augmentation=use_augmentation,
        learning_rate_multipliers=learning_rate_multipliers,
        l2_regularization_penalization=l2_penalization,
        tensorboard_log_path=tensorboard_log_path
    )
    # Final layer-wise momentum (mu_j in the paper)
    momentum = 0.9
    # linear epoch slope evolution
    momentum_slope = 0.01
    support_set_size = 20
    evaluate_each = 500

    number_of_train_iterations = 30000

    validation_accuracy = siamese_network.train_siamese_network(number_of_iterations=number_of_train_iterations,
                                                                support_set_size=support_set_size,
                                                                final_momentum=momentum,
                                                                momentum_slope=momentum_slope,
                                                                evaluate_each=evaluate_each,
                                                                model_name=model_name)

    if validation_accuracy == 0:
        evaluation_accuracy = 0
    else:
        # Load the weights with best validation accuracy
        siamese_network.model.load_weights(f'./models/{model_name}.h5')
        evaluation_accuracy = siamese_network.omniglot_loader.one_shot_test(siamese_network.model, 20, 40, False)

    print('Final Evaluation Accuracy = ' + str(evaluation_accuracy))
Ejemplo n.º 8
0
def test(model_path, data_path, target_tag, pair_num, save_result=False):
    model = SiameseNetwork()
    model.load_state_dict(
        torch.load(model_path, map_location='cpu')['model_state_dict'])
    result = []

    for i in range(0, pair_num):
        im1, im2, is_same, first_image, second_image = random_data_generator(
            data_path, target_tag)
        predict = model(im1.unsqueeze(0), im2.unsqueeze(0)).item()
        result.append((first_image, second_image, predict, is_same))

    if save_result:
        plt.figure(figsize=(4, 12), dpi=300)
        plt.subplots_adjust(left=None,
                            bottom=None,
                            right=None,
                            top=None,
                            wspace=None,
                            hspace=1.5)

        for i, item in enumerate(result):
            first_image, second_image, predict, is_same = item[0], item[
                1], item[2], item[3]
            img1 = Image.open(first_image)
            img2 = Image.open(second_image)
            img1 = img1.convert('RGB')
            img2 = img2.convert('RGB')
            plt.subplot(pair_num, 2, 2 * i + 1)
            plt.axis('off')
            plt.title('Label=%s, Prediction=%s' % (str(is_same), str(predict)),
                      fontsize=4)
            plt.imshow(img1)
            plt.subplot(pair_num, 2, 2 * i + 2)
            plt.axis('off')
            plt.title('Real Label=%s' % second_image.split('/')[-2],
                      fontsize=4)
            plt.imshow(img2)

        plt.savefig('./result.png', dpi=300)

    return result
Ejemplo n.º 9
0
def run_combination(l, bs, ep, pat, md, seed, train_path, test_path):
    """
    This function gets the parameters and run the experiment.
    :return: loss - loss on the testing set, accuracy - accuracy on the testing set
    """
    # file types
    model_save_type = 'h5'
    # files paths
    initialize_seed(seed)
    parameters_name = f'seed_{seed}_lr_{l}_bs_{bs}_ep_{ep}_val_{validation_size}_' \
                      f'es_{early_stopping}_pa_{pat}_md_{md}'
    print(f'Running combination with {parameters_name}')
    # A path for the weights
    load_weights_path = os.path.join(
        data_path, 'weights', f'weights_{parameters_name}.{model_save_type}')

    siamese = SiameseNetwork(seed=seed,
                             width=WIDTH,
                             height=HEIGHT,
                             cells=CEELS,
                             loss=loss_type,
                             metrics=['accuracy'],
                             optimizer=Adam(lr=l),
                             dropout_rate=0.4)
    siamese.fit(weights_file=load_weights_path,
                train_path=train_path,
                validation_size=validation_size,
                batch_size=bs,
                epochs=ep,
                early_stopping=early_stopping,
                patience=pat,
                min_delta=md)
    loss, accuracy = siamese.evaluate(test_file=test_path,
                                      batch_size=bs,
                                      analyze=True)
    print(f'Loss on Testing set: {loss}')
    print(f'Accuracy on Testing set: {accuracy}')
    # predict_pairs(model)
    return loss, accuracy
Ejemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-img', type=str, default='test.jpg')
    # parser.add_argument('-ques', type=str, default='What vechile is in the picture?')
    args = parser.parse_args()
    img = args.img
    folder_dataset_test = dset.ImageFolder(root=Config.testing_dir)
    siamese_dataset = SiameseNetworkDataset(
        imageFolderDataset=folder_dataset_test,
        transform=transforms.Compose(
            [transforms.Resize((100, 100)),
             transforms.ToTensor()]),
        should_invert=False)

    img = Image.open(img)
    img = img.convert("L")
    transform = transforms.Compose(
        [transforms.Resize((100, 100)),
         transforms.ToTensor()])
    img = transform(img)
    # Add a dimension to image to make padding possible.
    img = img[None, :, :, :]

    test_dataloader = DataLoader(siamese_dataset, num_workers=3, batch_size=1)
    dataiter = iter(test_dataloader)
    net = SiameseNetwork()
    net.load_state_dict(torch.load("trained_weights.pt"))

    for i in range(4):
        _, x1, label2 = next(dataiter)
        concatenated = torch.cat((img, x1), 0)

        output1, output2 = net(Variable(img), Variable(x1))
        euclidean_distance = F.pairwise_distance(output1, output2)
        imshow(
            torchvision.utils.make_grid(concatenated), 'Dissimilarity: \
					{:.2f}'.format(euclidean_distance.data.numpy()[0][0]))
Ejemplo n.º 11
0
class TrainModel():
    def __init__(self):
        self.siamese_network = SiameseNetwork()
        self.input_shape = self.INPUT_SHAPE
        self.triplet_model = self.siamese_network.model

        self.train_siamese_network()
        self.model = self.trained_model()
        self.save_model_weights()

    def train_siamese_network(self, batchsize=32, no_of_epochs=20):
        triplet_pairs = self.siamese_network.get_data()
        call_backs = siamese_network.get_callback()
        self.triplet_model.fit(x=[
            triplet_pairs[:, 0, :], triplet_pairs[:, 1, :], triplet_pairs[:,
                                                                          2, :]
        ],
                               y=np.zeros(shape=(triplet_pairs.shape[0],
                                                 12288)),
                               batch_size=batchsize,
                               callbacks=call_backs,
                               epochs=no_of_epochs,
                               shuffle=True,
                               validation_split=0.14)

    def trained_model(self):
        triplet_model_required_layers = self.triplet_model.layers[-2]
        anchor_input = Input(self.input_shape, name='anchor_input')
        encoded_anchor = triplet_model_required_layers(anchor_input)
        return Model(anchor_input, encoded_anchor)

    def save_model_weights(self):
        # serialize model to JSON
        model_json = self.model.to_json()
        with open(MODEL_WEIGHT_PATH + "model.json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights(MODEL_WEIGHT_PATH + "model.h5")
        print("Saved model to disk")
Ejemplo n.º 12
0
import matplotlib.pyplot as plt
import numpy as np
import scikitplot as skplt
import sklearn

from omniglot_loader import OmniglotLoader
from siamese_network import SiameseNetwork

model_wo_tf = './trained_models/wo_transform/model.h5'
model_w_tf = './trained_models/w_transform/model.h5'

if __name__ == '__main__':

    # First test case, training without transformations, testing without transformations
    omg = OmniglotLoader(use_transformations=False)
    network = SiameseNetwork(model_location=model_wo_tf)

    y_pred, te_y = network.get_predictions(omg)
    y_pred = np.array(y_pred)
    te_y = np.array(te_y)
    fpr, tpr, _ = skplt.metrics.roc_curve(te_y.flatten(), y_pred.flatten())
    auc = sklearn.metrics.roc_auc_score(te_y.flatten(), y_pred.flatten())

    # Second test case, training without transformations, testing with transformations
    omg = OmniglotLoader(use_transformations=True)
    y_pred, te_y = network.get_predictions(omg)
    y_pred = np.array(y_pred)
    te_y = np.array(te_y)
    fpr_te_tf, tpr_te_tf, _ = skplt.metrics.roc_curve(te_y.flatten(),
                                                      y_pred.flatten())
    auc_te_tf = sklearn.metrics.roc_auc_score(te_y.flatten(), y_pred.flatten())
Ejemplo n.º 13
0
from torchvision import transforms
from siamese_dataset import SiameseNetworkDataset
from siamese_network import SiameseNetwork
from helpers import imshow
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
from torch.autograd import Variable
import torch.nn.functional as F


class Config():
    testing_dir = "/home/wingman2/datasets/personas/test/"
    # testing_dir = "/home/wingman2/code/Facial-Similarity-with-Siamese-Networks-in-Pytorch/data/faces/testing/"


model = SiameseNetwork().cuda()
model.load_state_dict(torch.load('/home/wingman2/models/siamese-faces-160.pt'))
model.eval()

data_transforms_test = transforms.Compose(
    [transforms.Resize((100, 100)),
     transforms.ToTensor()])

folder_dataset_test = ImageFolder(root=Config.testing_dir)
siamese_dataset = SiameseNetworkDataset(imageFolderDataset=folder_dataset_test,
                                        transform=data_transforms_test,
                                        should_invert=False)
test_dataloader = DataLoader(siamese_dataset,
                             num_workers=8,
                             batch_size=1,
                             shuffle=True)
Ejemplo n.º 14
0
    def bayesian_optimization_function(x):
        dataset_path = 'Omniglot Dataset'

        current_learning_rate = float(x[:, 0])
        current_momentum = float(x[:, 1])
        current_momentum_slope = float(x[:, 2])
        current_conv1_multiplier = float(x[:, 3])
        current_conv2_multiplier = float(x[:, 4])
        current_conv3_multiplier = float(x[:, 5])
        current_conv4_multiplier = float(x[:, 6])
        current_dense1_multiplier = float(x[:, 7])
        current_conv1_penalization = float(x[:, 8])
        current_conv2_penalization = float(x[:, 9])
        current_conv3_penalization = float(x[:, 10])
        current_conv4_penalization = float(x[:, 11])
        current_dense1_penalization = float(x[:, 12])

        model_name = 'siamese_net_lr_' + str(current_learning_rate) + \
            'momentum_' + str(current_momentum) + '_slope_' + \
            str(current_momentum_slope)

        global current_model_number
        current_model_number += 1
        tensorboard_log_path = './logs/' + str(current_model_number)

        # Learning Rate multipliers for each layer
        learning_rate_multipliers = {}
        learning_rate_multipliers['Conv1'] = current_conv1_multiplier
        learning_rate_multipliers['Conv2'] = current_conv2_multiplier
        learning_rate_multipliers['Conv3'] = current_conv3_multiplier
        learning_rate_multipliers['Conv4'] = current_conv4_multiplier
        learning_rate_multipliers['Dense1'] = current_dense1_multiplier
        # l2-regularization penalization for each layer
        l2_penalization = {}
        l2_penalization['Conv1'] = current_conv1_penalization
        l2_penalization['Conv2'] = current_conv2_penalization
        l2_penalization['Conv3'] = current_conv3_penalization
        l2_penalization['Conv4'] = current_conv4_penalization
        l2_penalization['Dense1'] = current_dense1_penalization
        K.clear_session()
        siamese_network = SiameseNetwork(
            dataset_path=dataset_path,
            learning_rate=current_learning_rate,
            batch_size=32,
            use_augmentation=True,
            learning_rate_multipliers=learning_rate_multipliers,
            l2_regularization_penalization=l2_penalization,
            tensorboard_log_path=tensorboard_log_path)

        current_model_number += 1

        support_set_size = 20
        evaluate_each = 500
        number_of_train_iterations = 100000

        validation_accuracy = siamese_network.train_siamese_network(
            number_of_iterations=number_of_train_iterations,
            support_set_size=support_set_size,
            final_momentum=current_momentum,
            momentum_slope=current_momentum_slope,
            evaluate_each=evaluate_each,
            model_name=model_name)

        if validation_accuracy == 0:
            evaluation_accuracy = 0
        else:
            # Load the weights with best validation accuracy
            siamese_network.model.load_weights('models/' + model_name + '.h5')
            evaluation_accuracy = siamese_network.data_loader.one_shot_test(
                siamese_network.model, 20, 40, False)
        print("Model: " + model_name + ' | Accuracy: ' +
              str(evaluation_accuracy))
        K.clear_session()
        return 1 - evaluation_accuracy
Ejemplo n.º 15
0
    #测试集
    siamese_dataset_test = SiameseNetworkDataset(sample_path=Config.sample_test_path,
                                        transform=transforms.Compose([transforms.ToTensor()])
                                       ,should_invert=False)

    print("len(siamese_dataset_test) = ", siamese_dataset_test.__len__())

    test_dataloader = DataLoader(siamese_dataset_test,
                        shuffle=False,#siamese_dataset 重排后再取数据
                        num_workers=5,
                        batch_size=360)
    

    #模型训练
    net = SiameseNetwork() #网络结构
    criterion = ContrastiveLoss() #损失函数
    optimizer = optim.Adam(net.parameters(),lr = 0.0005) #参数优化函数

    train_loss_history = []
    test_loss_history = []
    for epoch in range(0, Config.train_number_epochs):#整个样本集的迭代
        list_loss_epoch_c = []
        for i, data in enumerate(train_dataloader,0):#batch迭代
            img0, img1, label = data

            optimizer.zero_grad() #模型参数梯度设为0
            output1,output2 = net(img0,img1)
            loss_contrastive = criterion(output1,output2,label)

            loss_contrastive.backward() #反向传播