Exemplo n.º 1
0
    def __init__(self,
                 n_classes=10,
                 latent_sz=32,
                 ngf=32,
                 init_type='orthogonal',
                 init_gain=0.1,
                 img_sz=32):
        super(CGN, self).__init__()

        # params
        self.batch_size = 1  # default: sample a single image
        self.n_classes = n_classes
        self.latent_sz = latent_sz
        self.label_emb = nn.Embedding(n_classes, n_classes)
        init_sz = img_sz // 4
        inp_dim = self.latent_sz + self.n_classes

        # models
        self.f_shape = nn.Sequential(*shape_layers(inp_dim, 1, ngf, init_sz))
        self.f_text1 = nn.Sequential(*texture_layers(inp_dim, 3, ngf, init_sz),
                                     nn.Tanh())
        self.f_text2 = nn.Sequential(*texture_layers(inp_dim, 3, ngf, init_sz),
                                     nn.Tanh())
        self.shuffler = nn.Sequential(Patch2Image(img_sz, 2),
                                      RandomCrop(img_sz))

        init_net(self, init_type=init_type, init_gain=init_gain)
Exemplo n.º 2
0
def preprocess():
    """Preprocess."""
    print("Transform images")
    client = storage.Client(PROJECT)
    read_bucket = client.get_bucket(RAW_BUCKET)
    write_bucket = client.get_bucket(BUCKET)

    transformations = transforms.Compose([Rescale(256), RandomCrop(224)])

    metadata_df = (
        pd.read_csv(f"gs://{RAW_BUCKET}/{RAW_DATA_DIR}/metadata.csv",
                    usecols=["filename", "view"
                             ]).query("view == 'PA'")  # taking only PA view
    )

    start_time = time.time()
    for filename in metadata_df["filename"].tolist():
        image = gcs_imread(read_bucket,
                           os.path.join(RAW_DATA_DIR, "images", filename))

        proc_image = transformations(image)
        proc_image = (proc_image * 255).astype(np.uint8)

        gcs_imwrite(write_bucket,
                    os.path.join(BASE_DIR, PREPROCESSED_DIR, filename),
                    filename, proc_image)

    print(f"  Time taken = {time.time() - start_time:.2f}s")
    print(f"  Number of images processed = {metadata_df.shape[0]}")
Exemplo n.º 3
0
    def __getitem__(self, index):
        seed = random.random()
        scale = random.choice(self.scale)
        new_image_size = (int(self.image_size[0] * scale),
                          int(self.image_size[1] * scale))

        # Load image and randomly resize and crop
        img = Image.open(self.image_list[index])
        if self.mode == 'train':
            img = transforms.Resize(new_image_size, Image.BILINEAR)(img)
            img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img)
        img = np.array(img)

        # Load label and randomly resize and crop like the image
        label = Image.open(self.label_list[index])
        if self.mode == 'train':
            label = transforms.Resize(new_image_size, Image.NEAREST)(label)
            label = RandomCrop(self.image_size, seed,
                               pad_if_needed=True)(label)
        label = np.array(label)

        # Augmentations
        if self.mode == 'train':
            if random.random() < 0.5:
                img, label = augmentation(img, label)
        if self.mode == 'train':
            if random.random() < 0.5:
                img = augmentation_pixel(img)

        # Convert image to tensor in CHW format
        img = Image.fromarray(img)
        img = self.to_tensor(img).float()

        # Convert label to tensor in CHW format
        if self.loss == 'dice':
            label = label[:, :, 0]
            label_with_camvid_indices = self.func(label)
            label = onehot_initialization_v2(label_with_camvid_indices)
            label = np.transpose(label, [2, 0, 1]).astype(np.float32)
            label = torch.from_numpy(label)
            return img, label

        elif self.loss == 'crossentropy':
            label = one_hot_it_v11(label, self.label_info).astype(np.uint8)
            label = torch.from_numpy(label).long()
            return img, label
Exemplo n.º 4
0
    def __getitem__(self, index):
        #open image and label
        img = Image.open(self.images[index])
        label = Image.open(self.labels[index]).convert("RGB")

        #resize image and label, then crop them
        scale = random.choice(self.scale)
        scale = (int(self.shape[0] * scale), int(self.shape[1] * scale))

        seed = random.random()
        img = transforms.Resize(scale, Image.BILINEAR)(img)
        img = RandomCrop(self.shape, seed, pad_if_needed=True)(img)
        img = np.array(img)

        label = transforms.Resize(scale, Image.NEAREST)(label)
        label = RandomCrop(self.shape, seed, pad_if_needed=True)(label)
        label = np.array(label)

        #translete to CamVid color palette
        label = self.__toCamVid(label)

        #apply augmentation
        img, label = augmentation(img, label)
        if random.randint(0, 1) == 1:
            img = augmentation_pixel(img)

        img = Image.fromarray(img)
        img = self.to_tensor(img).float()

        #computing losses
        if self.loss == 'dice':
            # label -> [num_classes, H, W]
            label = one_hot_it_v11_dice(label,
                                        self.label_info).astype(np.uint8)

            label = np.transpose(label, [2, 0, 1]).astype(np.float32)
            label = torch.from_numpy(label)

            return img, label

        elif self.loss == 'crossentropy':
            label = one_hot_it_v11(label, self.label_info).astype(np.uint8)
            label = torch.from_numpy(label).long()

            return img, label
Exemplo n.º 5
0
def process_image(image):
    """Process image."""
    seed_torch(seed=42)
    proc_image = transforms.Compose([
        Rescale(256),
        RandomCrop(224),
        ToTensor(),
    ])(image)
    proc_image = proc_image.unsqueeze(0).to(DEVICE)
    return proc_image
    str(args.M), 'dropout',
    str(args.dropout_rate), 'noise',
    str(args.noise)
])
if args.comment != '':
    writer_comment = '_'.join([writer_comment, args.comment])
print(writer_comment)
writer = SummaryWriter(comment=writer_comment)
iteration = 0

# Prepare the CIFAR dataset
if args.dataset == 'CIFAR10':
    normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],
                                     std=[0.2023, 0.1994, 0.2010])
    train_transform = transforms.Compose([
        RandomCrop(32, padding=4, mode='reflect'),
        #DownPadding(scale=0.7),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize
    ])
    test_transform = transforms.Compose([transforms.ToTensor(), normalize])
    train_dataset = datasets.CIFAR10(root=args.dataset_dir,
                                     train=True,
                                     download=True,
                                     transform=train_transform)
    if args.noise == 0:
        test_dataset = datasets.CIFAR10(root=args.dataset_dir,
                                        train=False,
                                        download=True,
                                        transform=test_transform)
Exemplo n.º 7
0
        acc = model.evaluate(test_loader=dtest)
        print("Iteration: {}, len(dl): {}, len(du): {},"
              " len(dh) {}, acc: {} ".format(iteration,
                                             len(dl.sampler.indices),
                                             len(du.sampler.indices),
                                             len(hcs_idx), acc))


if __name__ == "__main__":

    dataset_train = Caltech256Dataset(
        root_dir="../caltech256/256_ObjectCategories_train",
        transform=transforms.Compose(
            [SquarifyImage(),
             RandomCrop(224),
             Normalize(),
             ToTensor()]))

    dataset_test = Caltech256Dataset(
        root_dir="../caltech256/256_ObjectCategories_test",
        transform=transforms.Compose(
            [SquarifyImage(),
             RandomCrop(224),
             Normalize(),
             ToTensor()]))

    # Creating data indices for training and validation splits:
    random_seed = 123
    validation_split = 0.1  # 10%
    shuffling_dataset = True
Exemplo n.º 8
0
    def __getitem__(self, index):
        # load image and crop
        seed = random.random()
        img = Image.open(self.image_list[index])
        # random crop image
        # =====================================
        # w,h = img.size
        # th, tw = self.scale
        # i = random.randint(0, h - th)
        # j = random.randint(0, w - tw)
        # img = F.crop(img, i, j, th, tw)
        # =====================================

        scale = random.choice(self.scale)
        scale = (int(self.image_size[0] * scale),
                 int(self.image_size[1] * scale))

        # randomly resize image and random crop
        # =====================================
        if self.mode == 'train' or self.mode == 'adversarial_train':
            img = transforms.Resize(scale, Image.BILINEAR)(img)
            img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img)
        # =====================================

        img = np.array(img)
        if self.mode != 'adversarial_train':
            label = Image.open(self.label_list[index])
        else:
            imarray = np.zeros(shape=(2, 2, 4)) * 255
            label = Image.fromarray(imarray.astype('uint8')).convert('RGBA')

        # crop the corresponding label
        # =====================================
        # label = F.crop(label, i, j, th, tw)
        # =====================================

        # randomly resize label and random crop
        # =====================================
        if self.mode == 'train' or self.mode == 'adversarial_train':
            label = transforms.Resize(scale, Image.NEAREST)(label)
            label = RandomCrop(self.image_size, seed,
                               pad_if_needed=True)(label)

        label = np.array(label)

        # augment image and label
        if self.mode == 'train' or self.mode == 'adversarial_train':
            if random.random() < 0.5:
                img, label = augmentation(img, label)

        # augment pixel image
        if self.mode == 'train' or self.mode == 'adversarial_train':
            # set a probability of 0.5
            if random.random() < 0.5:
                img = augmentation_pixel(img)

        # image -> [C, H, W]

        img = Image.fromarray(img)
        img = self.to_tensor(img).float()

        if self.loss == 'dice':
            # label -> [num_classes, H, W]
            if self.mode != 'adversarial_train':
                label = one_hot_it_v11_dice(label,
                                            self.label_info).astype(np.uint8)

            label = np.transpose(label, [2, 0, 1]).astype(np.float32)
            label = torch.from_numpy(label)

            return img, label

        elif self.loss == 'crossentropy':
            label = one_hot_it_v11(label, self.label_info).astype(np.uint8)
            # label = label.astype(np.float32)
            label = torch.from_numpy(label).long()

            return img, label
Exemplo n.º 9
0

opt = CycleGanTrainOptions().parse()
invTransform = Denormalize(opt.normalizeMean, opt.normalizeNorm)

# set model

model = createModel(opt)
model.setup(opt)

# set dataloader

if opt.augment:
    transformList = [
        Resize(opt.loadSize),
        RandomCrop(opt.fineSize),
        RandomRotation(opt.rotate),
        ToTensor(),
        Normalize(opt.normalizeMean, opt.normalizeNorm),
        RandomHorizontalFlip(),
    ]
else:
    transformList = [
        Resize(opt.loadSize),
        ToTensor(),
        Normalize(opt.normalizeMean, opt.normalizeNorm)
    ]

transform = Compose(transformList)

datasetA = createDataset([opt.datasetA], transform=transform, outputFile=False)
Exemplo n.º 10
0
from focal_loss import FocalLoss
from model import get_model

transform = {
    'train':
    transforms.Compose([
        LeftToRightFlip(0.5),
        RandomRotation(angle=3, p=0.5),
        Resize(224),
        ColorJitter(p=0.5,
                    color=0.1,
                    contrast=0.1,
                    brightness=0.1,
                    sharpness=0.1),
        RandomCrop(scale=210, p=0.5),
        Resize(224),
        ToTensor()
    ]),
    'test':
    transforms.Compose([ToTensor()])
}

datasets = {
    x: ChestXrayDataset(csv_file=os.path.join('dataset', x, x + '.csv'),
                        root_dir=os.path.join('dataset', x),
                        transform=transform[x])
    for x in ['train', 'test']
}
dataloaders = {
    x: DataLoader(datasets[x], batch_size=32, shuffle=True)
Exemplo n.º 11
0
# ## GPU ID
gpu_id = args.gpu
# ## batch size could be bigger if you have enough GPU memory.
batch_size = args.batch_size

# ## folder
root_folder = args.case_folder
gt_folder = glob(join(root_folder, 'GT*'))[0]
recon_folder = glob(join(root_folder, 'Recon*'))[0]
refine_folder = glob(join(root_folder, 'Refine*'))[0]

gt_paths = glob(join(gt_folder, '*.tif'))
recon_paths = glob(join(recon_folder, '*.tif'))
refine_paths = glob(join(refine_folder, '*.tif'))

rc = RandomCrop(256, 0)

fid = FID(gpu_id=gpu_id, batch_size=batch_size)

gt_imgs = [io.imread(_) for _ in gt_paths]
gt_imgs = [_ / 255 for _ in gt_imgs]
gt_imgs = np.array(gt_imgs)
gt_imgs = gt_imgs[:, :, :, np.newaxis]

recon_imgs = [io.imread(_) for _ in recon_paths]
recon_imgs = [_ / 255 for _ in recon_imgs]
recon_imgs = np.array(recon_imgs)
recon_imgs = recon_imgs[:, :, :, np.newaxis]

refine_imgs = [io.imread(_) for _ in refine_paths]
refine_imgs = [_ / 255 for _ in refine_imgs]
Exemplo n.º 12
0
    def __getitem__(self, index):
        # load image and crop
        seed = random.random()
        img = Image.open(self.image_list[index])
        # random crop image
        # =====================================
        # w,h = img.size
        # th, tw = self.scale
        # i = random.randint(0, h - th)
        # j = random.randint(0, w - tw)
        # img = F.crop(img, i, j, th, tw)
        # =====================================

        scale = random.choice(self.scale)
        scale = (int(self.image_size[0] * scale), int(self.image_size[1] * scale))

        # randomly resize image and random crop
        # =====================================
        if self.mode == 'train':
            img = transforms.Resize(scale, Image.BILINEAR)(img)
            img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img)
        # =====================================

        img = np.array(img)
        # load label
        label = Image.open(self.label_list[index])


        # crop the corresponding label
        # =====================================
        # label = F.crop(label, i, j, th, tw)
        # =====================================

        # randomly resize label and random crop
        # =====================================
        if self.mode == 'train':
            label = transforms.Resize(scale, Image.NEAREST)(label)
            label = RandomCrop(self.image_size, seed, pad_if_needed=True)(label)
        # =====================================

        label = np.array(label)


        # augment image and label
        if self.mode == 'train':
            seq_det = self.fliplr.to_deterministic()
            img = seq_det.augment_image(img)
            label = seq_det.augment_image(label)


        # image -> [C, H, W]
        img = Image.fromarray(img)
        img = self.to_tensor(img).float()

        if self.loss == 'dice':
            # label -> [num_classes, H, W]
            label = one_hot_it_v11_dice(label, self.label_info).astype(np.uint8)

            label = np.transpose(label, [2, 0, 1]).astype(np.float32)
            # label = label.astype(np.float32)
            label = torch.from_numpy(label)

            return img, label

        elif self.loss == 'crossentropy':
            label = one_hot_it_v11(label, self.label_info).astype(np.uint8)
            # label = label.astype(np.float32)
            label = torch.from_numpy(label).long()

            return img, label