Beispiel #1
0
    def test_embedding_model_siamese(self):

        if self.server_dir is None:
            unittest.TestCase.skipTest(
                self,
                "DLPY_DATA_DIR_SERVER is not set in the environment variables")

        # test default
        resnet18_model = ResNet18_Caffe(self.s,
                                        width=224,
                                        height=224,
                                        random_flip='HV',
                                        random_mutation='random')
        branch = resnet18_model.to_functional_model(
            stop_layers=resnet18_model.layers[-1])
        model = EmbeddingModel.build_embedding_model(branch)
        res = model.print_summary()
        # print(res)
        self.assertEqual(
            res[res['Layer'].str.contains(
                model.embedding_layer_name_prefix)].shape[0], 2)

        # test options
        embedding_layer = Dense(n=10)
        model1 = EmbeddingModel.build_embedding_model(
            branch,
            model_table='test',
            embedding_model_type='siamese',
            margin=3.0,
            embedding_layer=embedding_layer)
        res1 = model1.print_summary()
        # print(res1)
        self.assertEqual(
            res1[res1['Layer'].str.contains(
                model1.embedding_layer_name_prefix)].shape[0], 2)

        # test passing in a sequential model
        model2 = EmbeddingModel.build_embedding_model(resnet18_model)
        res2 = model2.print_summary()
        self.assertEqual(
            res2[res2['Layer'].str.contains(
                model2.embedding_layer_name_prefix)].shape[0], 2)

        model3 = EmbeddingModel.build_embedding_model(
            resnet18_model,
            model_table='test2',
            embedding_model_type='siamese',
            margin=3.0,
            embedding_layer=embedding_layer)
        res3 = model3.print_summary()
        self.assertEqual(
            res3[res3['Layer'].str.contains(
                model3.embedding_layer_name_prefix)].shape[0], 2)
Beispiel #2
0
    def test_siamese_fit_1(self):

        if self.server_dir is None:
            unittest.TestCase.skipTest(
                self,
                "DLPY_DATA_DIR_SERVER is not set in the environment variables")

        # test using one data table
        resnet18_model = ResNet18_Caffe(self.s,
                                        width=224,
                                        height=224,
                                        random_crop='RESIZETHENCROP',
                                        random_flip='HV',
                                        random_mutation='random')
        embedding_layer = Dense(n=4)
        model1 = EmbeddingModel.build_embedding_model(
            resnet18_model,
            model_table='test1',
            embedding_model_type='siamese',
            margin=3.0,
            embedding_layer=embedding_layer)
        res1 = model1.print_summary()
        self.assertEqual(
            res1[res1['Layer'].str.contains(
                model1.embedding_layer_name_prefix)].shape[0], 2)

        img_path = self.server_dir + 'DogBreed_small'
        my_images = ImageEmbeddingTable.load_files(
            self.s,
            path=img_path,
            n_samples=64,
            embedding_model_type='siamese')
        solver = AdamSolver(lr_scheduler=StepLR(learning_rate=0.0001,
                                                step_size=20),
                            clip_grad_max=100,
                            clip_grad_min=-100)
        optimizer = Optimizer(algorithm=solver,
                              mini_batch_size=8,
                              log_level=3,
                              max_epochs=2,
                              reg_l2=0.0001)
        gpu = Gpu(devices=1)
        train_res = model1.fit_embedding_model(optimizer=optimizer,
                                               n_threads=1,
                                               gpu=gpu,
                                               path=img_path,
                                               n_samples=64,
                                               max_iter=2,
                                               seed=1234,
                                               record_seed=23435)
        print(train_res)
        score_res = model1.predict(data=my_images,
                                   gpu=gpu,
                                   random_crop='RESIZETHENCROP')
        print(score_res)
Beispiel #3
0
    def test_embedding_model_siamese_1(self):

        if self.server_dir is None:
            unittest.TestCase.skipTest(
                self,
                "DLPY_DATA_DIR_SERVER is not set in the environment variables")

        # test passing in a functional model
        vgg16 = Model(self.s)
        vgg16.load(path=self.data_dir + 'vgg16.sashdat')
        model = EmbeddingModel.build_embedding_model(vgg16)
        res = model.print_summary()
        self.assertEqual(
            res[res['Layer'].str.contains(
                model.embedding_layer_name_prefix)].shape[0], 2)
Beispiel #4
0
    def test_quartet_fit(self):

        if self.server_dir is None:
            unittest.TestCase.skipTest(
                self,
                "DLPY_DATA_DIR_SERVER is not set in the environment variables")

        # test using one data table
        resnet18_model = ResNet18_Caffe(self.s,
                                        width=224,
                                        height=224,
                                        random_crop='RESIZETHENCROP',
                                        random_flip='HV',
                                        random_mutation='random')
        embedding_layer = Dense(n=4)
        model1 = EmbeddingModel.build_embedding_model(
            resnet18_model,
            model_table='test1',
            embedding_model_type='quartet',
            margin=-3.0,
            embedding_layer=embedding_layer)
        res1 = model1.print_summary()
        print(res1)

        img_path = self.server_dir + 'DogBreed_small'
        my_images = ImageEmbeddingTable.load_files(
            self.s,
            path=img_path,
            n_samples=64,
            embedding_model_type='quartet')
        solver = AdamSolver(lr_scheduler=StepLR(learning_rate=0.0001,
                                                step_size=20),
                            clip_grad_max=100,
                            clip_grad_min=-100)
        optimizer = Optimizer(algorithm=solver,
                              mini_batch_size=8,
                              log_level=3,
                              max_epochs=5,
                              reg_l2=0.0001)
        gpu = Gpu(devices=1)
        train_res = model1.fit_embedding_model(data=my_images,
                                               optimizer=optimizer,
                                               n_threads=1,
                                               gpu=gpu,
                                               seed=1234,
                                               record_seed=23435)
        print(train_res)
        score_res = model1.predict(data=my_images,
                                   gpu=gpu,
                                   random_crop='RESIZETHENCROP')
        print(score_res)

        # test deploy as astore
        self.s.loadactionset('astore')
        my_images_test = ImageEmbeddingTable.load_files(
            self.s,
            path=img_path,
            n_samples=5,
            embedding_model_type='quartet',
            resize_width=224,
            resize_height=224)

        # case 1: deploy the full model as astore
        model1.deploy_embedding_model(output_format='astore',
                                      model_type='full',
                                      path=self.local_dir)

        full_astore = os.path.join(self.local_dir,
                                   model1.model_name + '.astore')
        with open(full_astore, mode='rb') as file:
            file_content = file.read()

        store_ = swat.blob(file_content)
        self.s.astore.upload(rstore=dict(name='test1_full', replace=True),
                             store=store_)

        # run with one gpu
        self.s.score(
            rstore=dict(name='test1_full'),
            table=my_images_test,
            nthreads=1,
            # _debug=dict(ranks=0),
            copyvars=['_path_', '_path_1', '_path_2', '_path_3'],
            options=[
                dict(name='usegpu', value='1'),
                dict(name='NDEVICES', value='1'),
                dict(name='DEVICE0', value='0')
            ],
            out=dict(name='astore_score1_full_gpu', replace=True))

        res = self.s.fetch(table='astore_score1_full_gpu')
        print(res)

        # remove the astore file
        os.remove(full_astore)

        # case 2: deploy the branch model as astore
        model1.deploy_embedding_model(output_format='astore',
                                      model_type='branch',
                                      path=self.local_dir)

        br_astore = os.path.join(self.local_dir,
                                 model1.model_name + '_branch.astore')
        with open(br_astore, mode='rb') as file:
            file_content = file.read()

        store_ = swat.blob(file_content)
        self.s.astore.upload(rstore=dict(name='test1_br', replace=True),
                             store=store_)

        # run with one gpu
        self.s.score(
            rstore=dict(name='test1_br'),
            table=my_images_test,
            nthreads=1,
            # _debug=dict(ranks=0),
            copyvars=['_path_'],
            options=[
                dict(name='usegpu', value='1'),
                dict(name='NDEVICES', value='1'),
                dict(name='DEVICE0', value='0')
            ],
            out=dict(name='astore_score1_br_gpu', replace=True))

        res = self.s.fetch(table='astore_score1_br_gpu')
        print(res)

        os.remove(br_astore)