Пример #1
0
    def test_sequencial_model_newdata(self):
        dataset = FaceDataset.load('tests/fixtures/faces/newdata/testset.pkl')
        dataset = dataset.create_unknown_class()

        modeldir = os.path.join(Config.SAVED_MODELS,
                                datetime.datetime.now().strftime('%Y-%m-%d'))
        trainset = FaceDataset.load(
            os.path.join(modeldir, Config.TRAINSET_FILE))

        seqmodel = SequentialModel()
        seqmodel.load(modeldir)

        results = score(seqmodel, dataset, trainset)
        self.assertGreater(results, 0.8)
 def test_add_imageclass(self):
     data = FaceDataset.load('tests/fixtures/faces/testset.pkl')
     init_value = data.classes_length
     images = FaceImageClass('new_class')
     images.append(np.zeros((3, 512)))
     data._add_imageclass(images)
     self.assertEqual(data.classes_length, init_value + 1)
Пример #3
0
    def test_knn_model_newdata(self):
        """

        :return:
        """
        dataset = FaceDataset.load('tests/fixtures/faces/newdata/testset.pkl')
        dataset = dataset.create_unknown_class()

        modeldir = os.path.join(Config.SAVED_MODELS,
                                datetime.datetime.now().strftime('%Y-%m-%d'))
        trainset = FaceDataset.load(
            os.path.join(modeldir, Config.TRAINSET_FILE))

        knnmodel = KnnModel.load(modeldir)

        result = score(knnmodel, dataset, trainset)
        self.assertGreater(result, 0.8)
 def test_load(self):
     data = FaceDataset()
     data.read_dataset('tests/fixtures/faces/newdata')
     data.save('tests/fixtures/faces/newdata/newdata.pkl')
     new_data = FaceDataset.load('tests/fixtures/faces/newdata/newdata.pkl')
     self.assertEqual(data.classes_length, new_data.classes_length)
     for cls in data.get_classes():
         self.assertTrue(cls in new_data.get_classes())
         self.assertEqual(data[cls].length, new_data[cls].length)
Пример #5
0
    def test_sequencial_model(self):
        dataset = FaceDataset.load(
            'tests/fixtures/faces/same_faces/testset.pkl')
        X, y = dataset.get_distance_vectors()

        modeldir = os.path.join(Config.SAVED_MODELS,
                                datetime.datetime.now().strftime('%Y-%m-%d'))

        kmodel = SequentialModel()
        kmodel.load(modeldir)

        score = kmodel.evaluate(X, y)
        self.assertGreater(score, 0.8)
Пример #6
0
    def test_knn_model(self):
        """
        Tests the data loading from a json file.
        :return:
        """
        dataset = FaceDataset.load(
            'tests/fixtures/faces/same_faces/testset.pkl')
        modeldir = os.path.join(Config.SAVED_MODELS,
                                datetime.datetime.now().strftime('%Y-%m-%d'))

        knnmodel = KnnModel.load(modeldir)

        X, y = dataset.get_embeddings()
        score = knnmodel.evaluate(X, y)
        self.assertGreater(score, 0.8)
 def test_balance_downsample(self):
     data = FaceDataset.load('tests/fixtures/faces/testset.pkl')
     X, y = data.get_distance_vectors()
     X_bal, y_bal = dataset.balance_classes(X, y, Balance.DOWNSAMPLE)
     self.assertEqual(y_bal[y_bal == 0].shape, y_bal[y_bal == 1].shape)
     self.assertEqual(y_bal.shape[0], y[y == 1].shape[0] * 2)
Пример #8
0
    parser.add_argument("-m",
                        "--model",
                        dest="model_dir",
                        help="Path to the input model.")
    parser.add_argument("-a",
                        "--approach",
                        dest="approach",
                        type=Models,
                        choices=list(Models),
                        default=Models.SEQUENTIAL,
                        help="Path to the input model.")

    args = parser.parse_args()
    logger.info(sys.argv)

    trainset = FaceDataset.load(
        os.path.join(args.model_dir, Config.TRAINSET_FILE))
    testset = FaceDataset.load(os.path.join(args.input, Config.TESTSET_FILE))
    newpeopleset = FaceDataset.load(
        os.path.join(args.input, Config.NEWDATA_FILE))
    newpeopleset = newpeopleset.create_unknown_class()

    if args.approach == Models.SEQUENTIAL:
        logger.info("Load the sequencial model.")
        model = SequentialModel()
        model.load(args.model_dir)
    else:
        logger.info("Load the knn model.")
        model = KnnModel().load(args.model_dir)

    logger.info("Evaluate the model on the test set.")
    acc = score(model, testset, trainset)
from config import Config
from src.data.dataset import FaceDataset, Balance
from src.model.sequencial_model import SequentialModel

logging.config.fileConfig(Config.LOGGING_FILE)
logger = logging.getLogger()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input", dest="input", help="Path to the input folder.")
    parser.add_argument("-b", "--balance", dest="balance", type=Balance, choices=list(Balance),
                        help="Approach for class balancing.")
    args = parser.parse_args()
    logger.info(sys.argv)

    trainset = FaceDataset.load(os.path.join(args.input, Config.TRAINSET_FILE))
    testset = FaceDataset.load(os.path.join(args.input, Config.TESTSET_FILE))

    x_train, y_train = trainset.get_distance_vectors(balance=args.balance)
    x_test, y_test = testset.get_distance_vectors()

    kmodel = SequentialModel()
    kmodel.train(input_size=x_train.shape[1], x_train=x_train, y_train=y_train, x_test=x_test, y_test=y_test)

    output_dir = os.path.join(Config.SAVED_MODELS, datetime.datetime.now().strftime('%Y-%m-%d'))
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    kmodel.save(output_dir)
    shutil.copyfile(os.path.join(args.input, Config.TRAINSET_FILE), os.path.join(output_dir, Config.TRAINSET_FILE))