def benchmark(config): model = SiameseNetwork('inference', config['model']) params = { 'batch_size': config['benchmark']['batch_size'], 'shuffle': False, 'dim': config['model']['input_shape'] } dataset_path = config['benchmark']['dataset_path'] train_dataset = ImageDataset(dataset_path, 'train') train_dataset.prepare(config['benchmark']['test_cases'] // 2) train_generator = DataGenerator(train_dataset, **params) test_dataset = ImageDataset(dataset_path, 'validation') test_dataset.prepare(config['benchmark']['test_cases'] // 2) test_generator = DataGenerator(test_dataset, **params) preds = np.array([]) gts = np.array([]) for i in tqdm(range(len(train_generator))): batch = train_generator[i] pred = model.predict(batch[0]) preds = np.append(preds, pred.flatten()) gts = np.append(gts, batch[1]) # if config['vis_output'] and not i % config['test_cases']//(5*config['batch_size']): # show_output(batch[0][0], batch[0][1], pred, batch[1]) tr_acc = compute_accuracy(preds, gts) print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc)) evaluation_times = [] preds = np.array([]) gts = np.array([]) for i in tqdm(range(len(test_generator))): batch = test_generator[i] start_t = time.time() pred = model.predict(batch[0]) evaluation_times.append((time.time() - start_t) / len(batch)) preds = np.append(preds, pred.flatten()) gts = np.append(gts, batch[1]) if config['benchmark']['vis_output'] and not i % config['benchmark'][ 'test_cases'] // (5 * config['benchmark']['batch_size']): show_output(batch[0][0], batch[0][1], pred, batch[1]) te_acc = compute_accuracy(preds, gts) print('* Accuracy on test set: %0.2f%%' % (100 * te_acc)) print("Average Evaluation Time Per Sample: " + str(np.mean(evaluation_times))) print(preds) print(gts)
import numpy as np from siamese import SiameseNetwork, create_pairs, compute_accuracy # Set constants BATCH_SIZE = 128 N_EPOCHS = 20 CLASS_DIM = 120 # Construct input data with open('/book/working/data/inter_emb.np', 'rb') as f: X_train = np.array(np.load(f), dtype=np.float32) with open('/book/working/data/labels.np', 'rb') as f: y_train = np.array(np.load(f), dtype=np.int8) digit_indices = [np.where(y_train == i)[0] for i in range(CLASS_DIM)] tr_pairs, tr_y = create_pairs(X_train, digit_indices, CLASS_DIM) # Construct Siamese network model, base_network = SiameseNetwork() model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y, batch_size=BATCH_SIZE, epochs=N_EPOCHS) # Compute final accuracy on training set y_pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]]) tr_acc = compute_accuracy(tr_y, y_pred) print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc)) # Save model.save('/book/working/models/siamese.h5')