Пример #1
0
    def setup_path(self, flags):

        root_folder = flags.data_root
        train_data = [
            'art_painting_train_features.hdf5', 'cartoon_train_features.hdf5',
            'photo_train_features.hdf5', 'sketch_train_features.hdf5'
        ]

        val_data = [
            'art_painting_val_features.hdf5', 'cartoon_val_features.hdf5',
            'photo_val_features.hdf5', 'sketch_val_features.hdf5'
        ]

        test_data = [
            'art_painting_features.hdf5', 'cartoon_features.hdf5',
            'photo_features.hdf5', 'sketch_features.hdf5'
        ]

        self.train_paths = []
        for data in train_data:
            path = os.path.join(root_folder, data)
            self.train_paths.append(path)

        self.val_paths = []
        for data in val_data:
            path = os.path.join(root_folder, data)
            self.val_paths.append(path)

        unseen_index = flags.unseen_index

        self.unseen_data_path = os.path.join(root_folder,
                                             test_data[unseen_index])
        self.train_paths.remove(self.train_paths[unseen_index])
        self.val_paths.remove(self.val_paths[unseen_index])

        if not os.path.exists(flags.logs):
            os.mkdir(flags.logs)

        flags_log = os.path.join(flags.logs, 'path_log.txt')
        write_log(str(self.train_paths), flags_log)
        write_log(str(self.val_paths), flags_log)
        write_log(str(self.unseen_data_path), flags_log)

        self.batImageGenTrains = []
        for train_path in self.train_paths:
            batImageGenTrain = BatchImageGenerator(flags=flags,
                                                   file_path=train_path,
                                                   stage='train',
                                                   b_unfold_label=False)
            self.batImageGenTrains.append(batImageGenTrain)

        self.batImageGenVals = []
        for val_path in self.val_paths:
            batImageGenVal = BatchImageGenerator(flags=flags,
                                                 file_path=val_path,
                                                 stage='val',
                                                 b_unfold_label=True)
            self.batImageGenVals.append(batImageGenVal)
Пример #2
0
    def heldout_test(self, flags):

        # load the best model in the validation data
        model_path = os.path.join(flags.model_path, 'best_model.tar')
        self.load_state_dict(state_dict=model_path)

        # test
        batImageGenTest = BatchImageGenerator(flags=flags,
                                              file_path=self.unseen_data_path,
                                              stage='test',
                                              b_unfold_label=False)
        test_images = batImageGenTest.images

        threshold = 100
        n_slices_test = len(test_images) / threshold
        indices_test = []
        for per_slice in range(n_slices_test - 1):
            indices_test.append(
                len(test_images) * (per_slice + 1) / n_slices_test)
        test_image_splits = np.split(test_images,
                                     indices_or_sections=indices_test)

        # Verify the splits are correct
        test_image_splits_2_whole = np.concatenate(test_image_splits)
        assert np.all(test_images == test_image_splits_2_whole)

        # split the test data into splits and test them one by one
        predictions = []
        self.network.eval()
        for test_image_split in test_image_splits:
            images_test = Variable(
                torch.from_numpy(np.array(test_image_split,
                                          dtype=np.float32))).cuda()
            outputs, end_points = self.network(images_test)

            pred = end_points['Predictions']
            pred = pred.cpu().data.numpy()
            predictions.append(pred)

        # concatenate the test predictions first
        predictions = np.concatenate(predictions)

        # accuracy
        accuracy = accuracy_score(y_true=batImageGenTest.labels,
                                  y_pred=np.argmax(predictions, -1))

        flags_log = os.path.join(flags.logs, 'heldout_test_log.txt')
        write_log(accuracy, flags_log)
Пример #3
0
    def test(self,
             flags,
             ite,
             log_prefix,
             log_dir='logs/',
             batImageGenTest=None):

        # switch on the network test mode
        self.network.eval()

        if batImageGenTest is None:
            batImageGenTest = BatchImageGenerator(flags=flags,
                                                  file_path='',
                                                  stage='test',
                                                  b_unfold_label=True)

        images_test = batImageGenTest.images
        labels_test = batImageGenTest.labels

        threshold = 50
        if len(images_test) > threshold:

            n_slices_test = len(images_test) / threshold
            indices_test = []
            for per_slice in range(n_slices_test - 1):
                indices_test.append(
                    len(images_test) * (per_slice + 1) / n_slices_test)
            test_image_splits = np.split(images_test,
                                         indices_or_sections=indices_test)

            # Verify the splits are correct
            test_image_splits_2_whole = np.concatenate(test_image_splits)
            assert np.all(images_test == test_image_splits_2_whole)

            # split the test data into splits and test them one by one
            test_image_preds = []
            for test_image_split in test_image_splits:
                images_test = Variable(
                    torch.from_numpy(
                        np.array(test_image_split, dtype=np.float32))).cuda()
                outputs, end_points = self.network(images_test)

                predictions = end_points['Predictions']
                predictions = predictions.cpu().data.numpy()
                test_image_preds.append(predictions)

            # concatenate the test predictions first
            predictions = np.concatenate(test_image_preds)
        else:
            images_test = Variable(
                torch.from_numpy(np.array(images_test,
                                          dtype=np.float32))).cuda()
            outputs, end_points = self.network(images_test)

            predictions = end_points['Predictions']
            predictions = predictions.cpu().data.numpy()

        accuracy = compute_accuracy(predictions=predictions,
                                    labels=labels_test)
        print('----------accuracy test----------:', accuracy)

        if not os.path.exists(log_dir):
            os.mkdir(log_dir)

        log_path = os.path.join(log_dir, '{}.txt'.format(log_prefix))
        write_log(str('ite:{}, accuracy:{}'.format(ite, accuracy)),
                  log_path=log_path)

        # switch on the network train mode after test
        self.network.train()

        return accuracy
Пример #4
0
    def setup_path(self, flags):

        root_folder = flags.data_root #provide this as input: root_folder = '/Users/pulkit/Desktop/PACS/'

        data_text_files = ['/Users/pulkit/Desktop/data_paths/art_painting.txt',
                            '/Users/pulkit/Desktop/data_paths/cartoon.txt',
                              '/Users/pulkit/Desktop/data_paths/photo.txt',
                              '/Users/pulkit/Desktop/data_paths/sketch.txt']


        self.train_paths = ['/Users/pulkit/Desktop/data_paths/art_painting.txt',
                            '/Users/pulkit/Desktop/data_paths/cartoon.txt',
                              '/Users/pulkit/Desktop/data_paths/photo.txt',
                              '/Users/pulkit/Desktop/data_paths/sketch.txt']

        self.val_paths = ['/Users/pulkit/Desktop/data_paths/art_painting.txt',
                            '/Users/pulkit/Desktop/data_paths/cartoon.txt',
                              '/Users/pulkit/Desktop/data_paths/photo.txt',
                              '/Users/pulkit/Desktop/data_paths/sketch.txt']

        unseen_index = flags.unseen_index

        self.train_paths.remove(self.train_paths[unseen_index])
        self.val_paths.remove(self.val_paths[unseen_index])
        self.unseen_data_path = data_text_files[unseen_index]

        if not os.path.exists(flags.logs):
            os.makedirs(flags.logs)

        flags_log = os.path.join(flags.logs, 'path_log.txt')
        write_log(str(self.train_paths), flags_log)
        write_log(str(self.val_paths), flags_log)
        write_log(str(self.unseen_data_path), flags_log)

        # self.batImageGenTrains = []
        # self.batImageGenVals = []
        # self.batImageGenTest = []

        # Get all the data at once in the dataset from the DataSet class
        # and then sample data at every iteration of training

        for path in data_text_files:

            dataset = BatchImageGenerator(root=root_folder, flist=path,
                                        transform=transforms.Compose([transforms.Resize(227),
                                                                      #transforms.RandomResizedCrop(224),
                                                                  transforms.ToTensor(),
                                                                  transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                                                       std=[0.229, 0.224, 0.225])]))

            dataset_size = len(dataset)
            indices = list(range(dataset_size))

            until_train = int(np.floor(0.8 * dataset_size))
            until_val = int(np.floor(0.9 * dataset_size))

            np.random.shuffle(indices)

            train_indices, val_indices, test_indices = indices[:until_train], indices[until_train:until_val],\
                                                       indices[until_val:]

            self.TrainMetaData.append([dataset, train_indices])
            self.TestMetaData.append([dataset, val_indices])
            self.ValidMetaData.append([dataset, test_indices])