コード例 #1
0
    def __init__(self,
                 data_dir,
                 split='train',
                 transform=None,
                 img_size=416,
                 rtn_path=False,
                 keep_difficult=False):
        """Dataset for VOC data.
		Args:
			data_dir: the root of the VOC2007 or VOC2012 dataset, the directory contains the following sub-directories:
				Annotations, ImageSets, JPEGImages, SegmentationClass, SegmentationObject.
		"""
        self.rtn_path = rtn_path
        if split == 'train':
            transform = [
                ConvertFromInts(),
                PhotometricDistort(),
                Expand([123, 117, 104]),
                RandomSampleCrop(),
                RandomMirror(),
                ToPercentCoords(),
                Resize(img_size),
                SubtractMeans([123, 117, 104]),
                ToTensor(),
            ]
        else:
            transform = [
                Resize(img_size),
                #ToPercentCoords(),
                SubtractMeans([123, 117, 104]),
                ToTensor()
            ]
        self.transform = Compose(transform)
        self.data_dir = data_dir
        self.split = split
        if split != 'test':
            image_sets_file = [
                os.path.join(self.data_dir, f'VOC{year}', "ImageSets", "Main",
                             "%s.txt" % self.split) for year in [2007, 2012]
            ]
            self.ids = VOCDataset._read_image_ids(image_sets_file)
        else:
            image_sets_file = [
                os.path.join(self.data_dir, f'VOC{year}', "ImageSets", "Main",
                             "%s.txt" % self.split) for year in [2007]
            ]
            self.ids = VOCDataset._read_image_ids(image_sets_file)
        self.keep_difficult = keep_difficult
        self.batch_count = 0
        self.img_size = 416
        self.min_size = self.img_size - 3 * 32
        self.max_size = self.img_size + 3 * 32
        self.class_dict = {
            class_name: i
            for i, class_name in enumerate(self.class_names)
        }
コード例 #2
0
def build_transforms(cfg):
    transforms = Compose([
        Resize(cfg.TRANSFORMS.RESIZE_SIZE),
        ToTensor(),
        Normalize(mean=cfg.TRANSFORMS.MEAN, std=cfg.TRANSFORMS.STD)
    ])
    return transforms
コード例 #3
0
def test_model(model, X_test, y_test, log_path='./test_data_info.csv'):
    correct = 0
    total = 0
    log_df = pd.DataFrame(columns=['image_path', 'ground_true_label', 'predicted_label'])
    composed = Compose([ToTensor()])

    test_dataloader = DataLoader(
        ParkingLotsDataset(X_test, y_test, composed),
        pin_memory=True,
        batch_size=32,
        shuffle=True,
        num_workers=4
    )

    with torch.no_grad():
        for data in test_dataloader:
            images = data['image'].to(device, dtype=torch.float)
            labels = data['label'].to(device, dtype=torch.float)
            image_paths = data['image_path']

            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += torch.sum(predicted.float() == labels.data)

            for image_path, correct_label, predicted_label in zip(image_paths, labels.cpu().numpy(),
                                                                  predicted.cpu().numpy()):
                log_df = log_df.append({
                    'image_path': image_path,
                    'ground_true_label': int(correct_label),
                    'predicted_label': int(predicted_label)
                }, ignore_index=True)
    log_df.to_csv(log_path)
    accuracy = 100 * correct.double() / total
    print(f'Accuracy {accuracy}')
コード例 #4
0
def abstract_test_single_dataset_raw_only_with_transforms(in_memory):
    # Dataset 1 has 4 raw images and 2 gt images
    dataset_1_raw = conftest.dataset_1_raw_images()
    train_transforms = [RandomCrop(20, 20), ToTensor()]
    eval_transforms = [Crop(0, 0, 20, 20), ToTensor()]
    dataset = TrainingDataset([conftest.dataset_1_raw_dir.name],
                              batch_size=4,
                              val_ratio=0,
                              add_normalization_transform=True,
                              train_transforms=train_transforms,
                              eval_transforms=eval_transforms,
                              keep_in_memory=in_memory)
    assert len(dataset) == 4
    # indices[0] because we only have one dataset
    assert len(dataset.train_indices[0]) == 4
    assert not dataset.val_indices[0]
    # We get back a batch of size 4 with all images in order
    sample = next(iter(dataset))
    raw = sample['raw']
    assert isinstance(raw, torch.Tensor)
    assert raw.shape == (4, 1, 20, 20)
    gt = sample['gt']
    assert isinstance(gt, torch.Tensor)
    assert gt.shape == (4, 1, 20, 20)
コード例 #5
0
batch_size = 3

num_classes = 32
input_height = 256
input_width = 256
output_height = 256
output_width = 256

# --- Predict data ---
image_dir = "/Users/Akash_Sengupta/Documents/4th_year_project_datasets/up-s31/trial/images3/train"
label_dir = "/Users/Akash_Sengupta/Documents/4th_year_project_datasets/up-s31/trial/masks3/train"

eval_transforms = transforms.Compose([
    PadToSquare(),
    Resize(input_height, input_width, output_height, output_width),
    ToTensor()
])

eval_dataset = UPS31Dataset(image_dir=image_dir,
                            label_dir=label_dir,
                            transform=eval_transforms)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

print('Eval images found: {}'.format(len(eval_dataset)))
print('Device: {}'.format(device))

# --- Model and Loss---
model = PSPNet(num_classes)
saved_model_path = "./saved_models/pspnet_test.tar"
criterion = nn.CrossEntropyLoss(weight=None)
コード例 #6
0
def main():
    global args, best_prec
    args = parser.parse_args()

    random.seed(seed)
    torch.manual_seed(seed)
    np.random.seed(seed)

    if not args.cpu:
        torch.cuda.manual_seed_all(seed)
        
    data = DatasetReader(root_dir=args.data, data_name=args.data_name)

    assert args.kfold < args.split, "kfold index must be less than the split size!"
    folds = fold(args.split, data)
    for i in range(args.kfold):
        next(folds)

    (train_dataset, val_dataset) = next(folds)

    def transform_factory(transformation):
        
        if(transformation == "RC"):
            train_transform = transforms.Compose([Resize(size=256), RandomCrop(size=224), RandomHorizontalFlip()])
        elif(transformation == "R"):
            train_transform = transforms.Compose([Resize(size=224)])
        elif(transformation == "RF"):
            train_transform = transforms.Compose([Resize(size=224), RandomHorizontalFlip()])    
        
        return train_transform

    train_transformations = transforms.Compose([transform_factory(args.transform), SelectFrames(num_frames=20), FrameDifference(dim=0), Normalize(), ToTensor()])
    val_transformations = transforms.Compose([Resize(size=224), SelectFrames(num_frames=20), FrameDifference(dim=0), Normalize(), ToTensor()])

    train_dataset = DatasetTransform(train_dataset, train_transformations)
    val_dataset = DatasetTransform(val_dataset, val_transformations)

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True)

    val_loader = torch.utils.data.DataLoader(
        val_dataset, batch_size=args.batch_size, shuffle=False,
        num_workers=args.workers, pin_memory=True)

    # create model
    print("=> creating model '{}'".format(args.arch))
    VP = network_factory(args.arch)

    model = VP()

    if not args.cpu:
        model = model.cuda()

    criterion = nn.CrossEntropyLoss()

    optimizer = torch.optim.Adam(model.parameters(), args.lr,
                                 weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    if args.evaluate:
        validate(val_loader, model)
        return

    run_training(train_loader, val_loader, model, criterion, optimizer, best_prec)
コード例 #7
0
from thexp import Trainer
from data import delegate
from trainers import GlobalParams
from thexp.contrib.data import splits
from thexp import DatasetBuilder

from data.transforms import ToTensor
from data.dataxy import datasets
from data.collate_fns import convert_batch_sparse_matrix_collate_fn

toTensor = ToTensor()


class DatasetMixin(Trainer):
    def datasets(self, params: GlobalParams):
        raise NotImplementedError()


class BaseSupDatasetMixin(DatasetMixin):
    """Base Supervised Dataset"""
    @staticmethod
    def _build_datasets(datas, params: GlobalParams):
        region_ids, json_fs, hdf5_fs, mp_ids, mp_fts = datas
        asg_del = delegate.ASGLoadDelegate(region_id=region_ids,
                                           json_fs=json_fs,
                                           hdf5_fs=hdf5_fs,
                                           mp_fts=mp_fts,
                                           mp_ids=mp_ids,
                                           max_attn_len=params.max_attn_len,
                                           pixel_reduce=params.pixel_reduce)
        loader = (DatasetBuilder().add_delegate(asg_del).zip_mode().DataLoader(
コード例 #8
0
def train_model(model,
                criterion,
                optimizer,
                scheduler,
                X_train,
                y_train,
                X_val,
                y_val,
                num_epochs=5,
                save=False,
                log_path=LOG_PATH,
                plot_path=None):
    since = time.time()
    count_train_set = len(X_train)
    count_val_set = len(X_val)

    composed = Compose([ToTensor()])

    train_dataloader = DataLoader(ParkingLotsDataset(X_train, y_train,
                                                     composed),
                                  pin_memory=True,
                                  batch_size=32,
                                  shuffle=True,
                                  num_workers=4)
    validation_dataloader = DataLoader(ParkingLotsDataset(
        X_val, y_val, composed),
                                       pin_memory=True,
                                       batch_size=32,
                                       shuffle=True,
                                       num_workers=4)

    with open(log_path, 'w'):  # truncate existing file
        pass

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0
    phases = {'train': train_dataloader, 'val': validation_dataloader}
    phases_count = {'train': count_train_set, 'val': count_val_set}

    for epoch in tqdm(range(num_epochs)):
        print()
        print(f'Epoch {epoch}/{num_epochs - 1}')
        print('-' * 10)
        log_values = []

        for phase in phases:
            running_loss = 0.0
            running_corrects = 0
            is_train = phase == 'train'

            if is_train:
                model.train()
            else:
                model.eval()

            for batch in phases[phase]:
                inputs = batch['image'].to(device, dtype=torch.float)
                labels = batch['label'].to(device, dtype=torch.long)

                optimizer.zero_grad()
                with torch.set_grad_enabled(is_train):
                    outputs = model(inputs)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, labels)

                    if is_train:
                        loss.backward()
                        optimizer.step()

                    running_loss += loss.item() * inputs.size(0)
                    running_corrects += torch.sum(preds == labels.data)
            if is_train:
                scheduler.step()

            epoch_loss = running_loss / phases_count[phase]
            epoch_acc = running_corrects.double() / phases_count[phase]
            log_values.append(str(epoch_loss))
            log_values.append(str(epoch_acc.cpu().numpy()))
            print(f'{phase} Loss: {epoch_loss} Acc: {epoch_acc}')

            if not is_train and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = copy.deepcopy(model.state_dict())
        with open(log_path, 'a') as log_file:
            log_file.write(' '.join(log_values))
            log_file.write('\n')
    time_elapsed = time.time() - since
    print(f'Training complete in {time_elapsed // 60}m {time_elapsed % 60}s')
    model.load_state_dict(best_model_wts)
    if save:
        torch.save(model, './models/last.pth')
    if plot_path is not None:
        plot_results(plot_path)

    return model
コード例 #9
0
ファイル: cycleGAN.py プロジェクト: carrowsm/ArtifactNet
    def prepare_data(self):
        """ Load the image file names, create dataset objects.
        Called automatically by pytorch lightning.
        """
        # Get train and test data sets
        # Import CSV containing DA labels
        X_img, Y_img = self.hparams.img_domain_x, self.hparams.img_domain_y
        test_csv = "/cluster/home/carrowsm/ArtifactNet/datasets/test_labels.csv"

        x_df_trg, x_df_val, y_df_trg, y_df_val = load_image_data_frame(
            self.hparams.csv_path, X_img, Y_img,
            val_split=0.1)  # Use 10% of data for val
        x_df_test, _, y_df_test, _ = load_image_data_frame(test_csv,
                                                           X_img,
                                                           Y_img,
                                                           val_split=0)
        # Define sequence of transforms
        trg_transform = torchvision.transforms.Compose([
            HorizontalFlip(),
            AffineTransform(max_angle=30.0, max_pixels=[20, 20]),
            Normalize(-1000.0, 1000.0),
            ToTensor()
        ])
        val_transform = torchvision.transforms.Compose(
            [Normalize(-1000.0, 1000.0),
             ToTensor()])
        test_transform = val_transform

        # Train data loader
        trg_dataset = UnpairedDataset(x_df_trg,
                                      y_df_trg,
                                      image_dir=self.hparams.img_dir,
                                      cache_dir=os.path.join(
                                          self.hparams.cache_dir, "unpaired"),
                                      file_type="DICOM",
                                      image_size=self.image_size,
                                      image_spacing=[2.0, 1.0, 1.0],
                                      dim=self.dimension,
                                      transform=trg_transform,
                                      num_workers=self.hparams.n_cpus)
        val_dataset = UnpairedDataset(x_df_val,
                                      y_df_val,
                                      image_dir=self.hparams.img_dir,
                                      cache_dir=os.path.join(
                                          self.hparams.cache_dir, "unpaired"),
                                      file_type="DICOM",
                                      image_size=self.image_size,
                                      image_spacing=[2.0, 1.0, 1.0],
                                      dim=self.dimension,
                                      transform=val_transform,
                                      num_workers=self.hparams.n_cpus)
        test_dataset = UnpairedDataset(x_df_test,
                                       y_df_test,
                                       image_dir=self.hparams.img_dir,
                                       cache_dir=os.path.join(
                                           self.hparams.cache_dir, "unpaired"),
                                       file_type="DICOM",
                                       image_size=self.image_size,
                                       image_spacing=[2.0, 1.0, 1.0],
                                       dim=self.dimension,
                                       transform=test_transform,
                                       num_workers=self.hparams.n_cpus)

        self.trg_dataset = trg_dataset
        self.val_dataset = val_dataset
        self.test_dataset = test_dataset

        # If caching data for the first time, save the image centre coordinates
        if trg_dataset.first_cache and val_dataset.first_cache:
            df = pd.concat([trg_dataset.full_df, val_dataset.full_df])
            df.to_csv(self.hparams.csv_path)
        if test_dataset.first_cache:
            test_dataset.full_df.to_csv(test_csv)
コード例 #10
0
def main(args) :
    # Save a file containing info on the model used to create these results
    info_file_path = os.path.join(args.out_img_dir, "model_info.json")
    info_dict = {"path_to_data_csv" : args.csv_path,
                 "path_to_saved_model" : args.checkpoint,}
    with open(info_file_path, 'w') as outfile:
        json.dump(info_dict, outfile, indent=4)


    # Get list of patient IDs
    x_df, y_df = prepare_data(args.csv_path) # X is DA+, Y is DA-

    # Define transforms to normalize input data
    transform = torchvision.transforms.Compose([
                        Normalize(-1000.0, 1000.0),
                        ToTensor()])

    # Initialize the dataloader
    dataset = UnpairedDataset(x_df, y_df,
                              image_dir=args.in_img_dir,
                              cache_dir=args.cache_dir,
                              file_type="nrrd",
                              image_size=[8, 256,256],
                              dim=3,
                              transform=transform,
                              num_workers=args.n_cpus)
    dataloader = DataLoader(dataset, batch_size=1, shuffle=False,
                            num_workers=args.n_cpus)
    print("Dataloaders created.")

    # Initialize the postprocessor
    postprocess = PostProcessor(input_dir=args.in_img_dir,
                                output_dir=args.out_img_dir,
                                output_spacing='orig',
                                input_file_type="nrrd",
                                output_file_type="nrrd")


    ### COPY CLEAN IMAGES ###
    print(f"Copying {len(y_df.index)} clean images")
    for patient_id in y_df.index :
        from_path = os.path.join(args.in_img_dir, f"{patient_id}.nrrd")
        to_path   = os.path.join(args.out_img_dir, "test/images/" f"{patient_id}.nrrd")

        # Copy the file
        shutil.copy(from_path, to_path)
    ### ----------------- ###


    # Load the model from checkpoint
    model = load_model(GAN, args.checkpoint)
    print("Model loaded")

    if torch.cuda.is_available() :
        n_gpus = torch.cuda.device_count()
        print(f"{n_gpus} GPUs are available")
        device = torch.device('cuda')
    else :
        device = torch.device('cpu')

    # Move the model to GPU
    model.to(torch.device('cpu'))
    generator = model.g_y.to(device)
    del model # Free up memory

    print(f"Generating {len(dataset)} clean images.")

    ### GENERATE CLEAN VERSIONS OF DA+ IMAGES ###
    with torch.no_grad() :
        for i, data in enumerate(dataloader) :
            t0 = time.time()
            x, y = data # Get img X (DA+) and a random unpaired Y (DA-) img
            gen_y = generator(x.to(device)) # Translate DA+ img to DA-

            # Postprocess the resulting torch tensor.
            # This re-inserts the generated image back into the full DICOM and
            # saves it in NRRD format
            patient_id = dataset.x_ids[i]
            img_center_x = float(x_df.at[patient_id, "img_center_x"])
            img_center_y = float(x_df.at[patient_id, "img_center_y"])
            img_center_z = float(x_df.at[patient_id, "img_center_z"])
            postprocess(gen_y.to(torch.device('cpu')),
                        patient_id,
                        [img_center_x, img_center_y, img_center_z])

            print(f"Image {patient_id} processed in {time.time() - t0} s.")