def __init__(self):
        self.best_accuracy = 0.0

        # all data
        self.data_train = CUBDataset.get_data_all(Config.data_root)
        self.task_train = CUBDataset(self.data_train, Config.num_way,
                                     Config.num_shot)
        self.task_train_loader = DataLoader(self.task_train,
                                            Config.batch_size,
                                            True,
                                            num_workers=Config.num_workers)

        # model
        self.matching_net = RunnerTool.to_cuda(Config.matching_net)
        RunnerTool.to_cuda(self.matching_net.apply(RunnerTool.weights_init))
        self.norm = Normalize(2)

        # loss
        self.loss = RunnerTool.to_cuda(nn.MSELoss())

        # optim
        self.matching_net_optim = torch.optim.Adam(
            self.matching_net.parameters(), lr=Config.learning_rate)
        self.matching_net_scheduler = MultiStepLR(self.matching_net_optim,
                                                  Config.train_epoch_lr,
                                                  gamma=0.5)

        self.test_tool = FSLTestTool(self.matching_test,
                                     data_root=Config.data_root,
                                     num_way=Config.num_way,
                                     num_shot=Config.num_shot,
                                     episode_size=Config.episode_size,
                                     test_episode=Config.test_episode,
                                     transform=self.task_train.transform_test)
        pass
    def __init__(self):
        # all data
        self.data_train = OmniglotDataset.get_data_all(Config.data_root)
        self.task_train = OmniglotDataset(self.data_train, Config.num_way,
                                          Config.num_shot)
        self.task_train_loader = DataLoader(self.task_train,
                                            Config.batch_size,
                                            True,
                                            num_workers=Config.num_workers)

        # model
        self.matching_net = RunnerTool.to_cuda(
            MatchingNet(hid_dim=64, z_dim=64))
        RunnerTool.to_cuda(self.matching_net.apply(RunnerTool.weights_init))
        self.norm = Normalize(2)

        # loss
        self.loss = RunnerTool.to_cuda(nn.MSELoss())

        # optim
        self.matching_net_optim = torch.optim.SGD(
            self.matching_net.parameters(),
            lr=Config.learning_rate,
            momentum=0.9,
            weight_decay=5e-4)

        self.test_tool = FSLTestTool(self.matching_test,
                                     data_root=Config.data_root,
                                     num_way=Config.num_way_test,
                                     num_shot=Config.num_shot,
                                     episode_size=Config.episode_size,
                                     test_episode=Config.test_episode,
                                     transform=self.task_train.transform_test)
        pass
Beispiel #3
0
 def __init__(self, resnet, low_dim=512, modify_head=False):
     super().__init__()
     self.resnet = resnet(num_classes=low_dim)
     self.l2norm = Normalize(2)
     if modify_head:
         self.resnet.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
         pass
     pass
    def __init__(self):
        self.adjust_learning_rate = Config.adjust_learning_rate

        # all data
        self.data_train = OmniglotDataset.get_data_all(Config.data_root)
        self.task_train = OmniglotDataset(self.data_train, Config.num_way, Config.num_shot)
        self.task_train_loader = DataLoader(self.task_train, Config.batch_size, True, num_workers=Config.num_workers)

        # IC
        self.produce_class = ProduceClass(len(self.data_train), Config.ic_out_dim, Config.ic_ratio)
        self.produce_class.init()
        self.task_train.set_samples_class(self.produce_class.classes)

        # model
        self.matching_net = RunnerTool.to_cuda(Config.matching_net)
        self.ic_model = RunnerTool.to_cuda(ICResNet(low_dim=Config.ic_out_dim, encoder=Config.ic_net))
        self.norm = Normalize(2)
        if Config.multi_gpu:
            self.matching_net = RunnerTool.to_cuda(nn.DataParallel(self.matching_net))
            cudnn.benchmark = True
            pass
        # RunnerTool.to_cuda(self.matching_net.apply(RunnerTool.weights_init))
        # RunnerTool.to_cuda(self.ic_model.apply(RunnerTool.weights_init))

        # optim
        self.matching_net_optim = torch.optim.SGD(
            self.matching_net.parameters(), lr=Config.learning_rate, momentum=0.9, weight_decay=5e-4)
        self.ic_model_optim = torch.optim.SGD(
            self.ic_model.parameters(), lr=Config.learning_rate, momentum=0.9, weight_decay=5e-4)

        # loss
        self.ic_loss = RunnerTool.to_cuda(nn.CrossEntropyLoss())
        self.fsl_loss = RunnerTool.to_cuda(nn.MSELoss())

        # Eval
        self.test_tool_fsl = FSLTestTool(self.matching_test, data_root=Config.data_root,
                                         num_way=Config.num_way_test, num_shot=Config.num_shot,
                                         episode_size=Config.episode_size, test_episode=Config.test_episode,
                                         transform=self.task_train.transform_fsl_test)
        self.test_tool_ic = ICTestTool(feature_encoder=None, ic_model=self.ic_model,
                                       data_root=Config.data_root, batch_size=Config.batch_size,
                                       num_workers=Config.num_workers, ic_out_dim=Config.ic_out_dim,
                                       transform=self.task_train_loader.dataset.transform_ic_test, k=20)
        pass
Beispiel #5
0
    def __init__(self):
        self.best_accuracy = 0.0
        self.adjust_learning_rate = Config.adjust_learning_rate

        # all data
        self.data_train = TieredImageNetDataset.get_data_all(Config.data_root)
        self.task_train = TieredImageNetDataset(self.data_train, Config.num_way,
                                                Config.num_shot, load_data=Config.load_data)
        self.task_train_loader = DataLoader(self.task_train, Config.batch_size, True, num_workers=Config.num_workers)

        # IC
        self.produce_class = ProduceClass(len(self.data_train), Config.ic_out_dim, Config.ic_ratio)
        self.produce_class.init()
        self.task_train.set_samples_class(self.produce_class.classes)
        self.task_train.set_samples_feature(self.produce_class.features)

        # model
        self.matching_net = RunnerTool.to_cuda(Config.matching_net)
        self.ic_model = RunnerTool.to_cuda(ICResNet(low_dim=Config.ic_out_dim,
                                                    resnet=Config.resnet, modify_head=Config.modify_head))
        self.norm = Normalize(2)
        RunnerTool.to_cuda(self.matching_net.apply(RunnerTool.weights_init))
        RunnerTool.to_cuda(self.ic_model.apply(RunnerTool.weights_init))

        # optim
        self.matching_net_optim = torch.optim.SGD(
            self.matching_net.parameters(), lr=Config.learning_rate, momentum=0.9, weight_decay=5e-4)
        self.ic_model_optim = torch.optim.SGD(
            self.ic_model.parameters(), lr=Config.learning_rate, momentum=0.9, weight_decay=5e-4)

        # loss
        self.ic_loss = RunnerTool.to_cuda(nn.CrossEntropyLoss())
        self.fsl_loss = RunnerTool.to_cuda(nn.MSELoss())

        # Eval
        self.test_tool_fsl = FSLTestTool(self.matching_test, data_root=Config.data_root,
                                         num_way=Config.num_way, num_shot=Config.num_shot,
                                         episode_size=Config.episode_size, test_episode=Config.test_episode,
                                         transform=self.task_train.transform_test)
        self.test_tool_ic = ICTestTool(feature_encoder=None, ic_model=self.ic_model, data_root=Config.data_root,
                                       transform=self.task_train.transform_test, batch_size=Config.batch_size,
                                       num_workers=Config.num_workers, ic_out_dim=Config.ic_out_dim, k=Config.knn,
                                       load_data=Config.load_data)
        pass
    def __init__(self, data_train, classes):
        # all data
        self.data_train = data_train
        self.task_train = TieredImageNetFSLDataset(self.data_train, classes,
                                                   Config.fsl_num_way,
                                                   Config.fsl_num_shot)
        self.task_train_loader = DataLoader(self.task_train,
                                            Config.fsl_batch_size,
                                            True,
                                            num_workers=Config.num_workers)

        # model
        self.matching_net = RunnerTool.to_cuda(Config.fsl_matching_net)
        self.matching_net = RunnerTool.to_cuda(
            nn.DataParallel(self.matching_net))
        cudnn.benchmark = True
        RunnerTool.to_cuda(self.matching_net.apply(RunnerTool.weights_init))
        self.norm = Normalize(2)

        # optim
        self.matching_net_optim = torch.optim.Adam(
            self.matching_net.parameters(), lr=Config.fsl_learning_rate)
        self.matching_net_scheduler = MultiStepLR(self.matching_net_optim,
                                                  Config.fsl_lr_schedule,
                                                  gamma=1 / 3)

        # loss
        self.fsl_loss = RunnerTool.to_cuda(nn.MSELoss())

        # Eval
        self.test_tool_fsl = FSLTestTool(
            self.matching_test,
            data_root=Config.data_root,
            num_way=Config.fsl_num_way,
            num_shot=Config.fsl_num_shot,
            episode_size=Config.fsl_episode_size,
            test_episode=Config.fsl_test_episode,
            transform=self.task_train.transform_test)
        pass
 def __init__(self, encoder, low_dim=512):
     super().__init__()
     self.encoder = encoder
     self.fc = nn.Linear(self.encoder.out_dim, low_dim)
     self.l2norm = Normalize(2)
     pass