def test_make_infinite(): all_elems = list(range(7)) inf_gen = utils.make_infinite(lambda elems: elems, all_elems) for _ in range(42): yielded = set() for i in all_elems: yielded.add(next(inf_gen)) assert (yielded == set(all_elems))
print(c.shape) return [f, c] images_train = random.sample(all_image_names, int(len(all_image_names) * 0.9)) images_test = [i for i in all_image_names if i not in set(images_train)] def couples_generator(images): for i in images: yield load_pair(i), labels[i] def gen_func(images): return couples_generator(images) train_couples_len = sum(1 for e in gen_func(images_train)) test_couples_len = sum(1 for e in gen_func(images_test)) data_gen = utils.get_ImageDataGenerator(all_images, input_shape) train_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_train), input_shape, data_gen, BATCH_SIZE) test_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_test), input_shape, data_gen, BATCH_SIZE) model = network.create(input_shape, args.network) network.compile(model, args.optimizer) model.fit_generator(train_iterator, steps_per_epoch=train_couples_len / BATCH_SIZE, epochs=EPOCHS) score = model.evaluate_generator(test_iterator, steps=test_couples_len / BATCH_SIZE) print(score)
def couples_generator(images): for i in images: yield load_pair(i), labels[i] def gen_func(images): return couples_generator(images) train_couples_len = sum(1 for e in gen_func(images_train)) test_couples_len = sum(1 for e in gen_func(images_test)) data_gen = utils.get_ImageDataGenerator(all_images, input_shape) train_iterator = utils.CouplesIterator( utils.make_infinite(gen_func, images_train), input_shape, data_gen, BATCH_SIZE) test_iterator = utils.CouplesIterator( utils.make_infinite(gen_func, images_test), input_shape, data_gen, BATCH_SIZE) model = network.create(input_shape) network.compile(model) model.fit_generator(train_iterator, steps_per_epoch=train_couples_len / BATCH_SIZE, epochs=EPOCHS) score = model.evaluate_generator(test_iterator, steps=test_couples_len / BATCH_SIZE) print(score)
def gen_func(images): return utils.balance(couples_generator(images)) train_couples_len = sum(1 for e in gen_func(images_train)) test_couples_len = sum(1 for e in gen_func(images_test)) print('Training with %d couples.' % train_couples_len) print('Testing with %d couples.' % test_couples_len) print(input_shape) data_gen = utils.get_ImageDataGenerator(all_images, input_shape) train_iterator = utils.CouplesIterator( utils.make_infinite(gen_func, images_train), input_shape, data_gen, BATCH_SIZE) test_iterator = utils.CouplesIterator( utils.make_infinite(gen_func, images_test), input_shape, data_gen, BATCH_SIZE) model = network.create(input_shape) network.compile(model) model.save('pretrain.h5') model.fit_generator(train_iterator, steps_per_epoch=train_couples_len / BATCH_SIZE, epochs=EPOCHS) model.save('pretrain.h5')
def gen_func(images): return utils.balance(couples_generator(images)) train_couples_len = sum(1 for e in gen_func(images_train)) validation_couples_len = sum(1 for e in gen_func(images_validation)) test_couples_len = sum(1 for e in gen_func(images_test)) print('Training with %d couples.' % train_couples_len) print('Validation with %d couples.' % validation_couples_len) print('Testing with %d couples.' % test_couples_len) print(input_shape) data_gen = utils.get_ImageDataGenerator(all_images, input_shape) train_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_train), input_shape, data_gen, BATCH_SIZE) validation_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_validation), input_shape, data_gen, BATCH_SIZE) test_iterator = utils.CouplesIterator(utils.make_infinite(gen_func, images_test), input_shape, data_gen, BATCH_SIZE) model = network.create(input_shape, args.network) network.compile(model, args.optimizer) callbacks_list = [ModelCheckpoint('best_pretrain_model.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')] if args.early_stopping: callbacks_list.append(EarlyStopping(monitor='val_accuracy', patience=2)) model.fit_generator(train_iterator, callbacks=callbacks_list, validation_data=validation_iterator, steps_per_epoch=train_couples_len / BATCH_SIZE, validation_steps=validation_couples_len / BATCH_SIZE, epochs=EPOCHS) score = model.evaluate_generator(test_iterator, steps=test_couples_len / BATCH_SIZE) print(score)