Python data.base_dataset.normalize() Examples

The following are 9 code examples of data.base_dataset.normalize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module data.base_dataset , or try the search function .
Example #1
Source File: segmentation_dataset.py    From neurips18_hierchical_image_manipulation with MIT License 6 votes vote down vote up
def preprocess_inputs(self, raw_inputs, params):
        outputs = dict()
        # label & inst.
        transform_label = get_transform_fn(self.opt, params, method=Image.NEAREST, normalize=False)
        outputs['label'] = transform_label(raw_inputs['label']) * 255.0
        outputs['inst'] = transform_label(raw_inputs['inst'])
        if self.opt.dataloader == 'sun_rgbd' or self.opt.dataloader == 'ade20k': # NOTE(sh): dirty exception!
            outputs['inst'] *= 255.0
        outputs['label_path'] = raw_inputs['label_path']
        outputs['inst_path'] = raw_inputs['inst_path']
        # image
        if self.load_image:
            transform_image = get_transform_fn(self.opt, params)
            outputs['image'] = transform_image(raw_inputs['image'])
            outputs['image_path'] = raw_inputs['image_path']
        # raw inputs
        if self.load_raw:
            transform_raw = get_raw_transform_fn(normalize=False)
            outputs['label_raw'] = transform_raw(raw_inputs['label']) * 255.0
            outputs['inst_raw'] = transform_raw(raw_inputs['inst'])
            transform_image_raw = get_raw_transform_fn()
            outputs['image_raw'] = transform_image_raw(raw_inputs['image'])
        return outputs 
Example #2
Source File: aligned_dataset.py    From everybody_dance_now_pytorch with GNU Affero General Public License v3.0 5 votes vote down vote up
def __getitem__(self, index):        
        ### input A (label maps)
        A_path = self.A_paths[index]              
        A = Image.open(A_path)        
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain:
            B_path = self.B_paths[index]   
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)      
            B_tensor = transform_B(B)

        ### if using instance maps        
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict 
Example #3
Source File: aligned_dataset.py    From EverybodyDanceNow_reproduce_pytorch with MIT License 5 votes vote down vote up
def __getitem__(self, index):        
        ### input A (label maps)
        A_path = self.A_paths[index]              
        A = Image.open(A_path)        
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain:
            B_path = self.B_paths[index]   
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)      
            B_tensor = transform_B(B)

        ### if using instance maps        
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict 
Example #4
Source File: aligned_dataset.py    From deep-learning-for-document-dewarping with MIT License 5 votes vote down vote up
def __getitem__(self, index):        
        ### input A (label maps)
        A_path = self.A_paths[index]              
        A = Image.open(A_path)        
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain or self.opt.use_encoded_image:
            B_path = self.B_paths[index]   
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)      
            B_tensor = transform_B(B)

        ### if using instance maps        
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict 
Example #5
Source File: segmentation_dataset.py    From neurips18_hierchical_image_manipulation with MIT License 5 votes vote down vote up
def preprocess_cropping(self, raw_inputs, outputs, params):
        transform_obj = get_transform_fn(
            self.opt, params, method=Image.NEAREST, normalize=False, is_context=False)
        label_obj = transform_obj(raw_inputs['label']) * 255.0
        input_bbox = np.array(params['bbox_in_context'])
        bbox_cls = params['bbox_cls']
        bbox_cls = bbox_cls if bbox_cls is not None else self.opt.label_nc-1
        mask_object_inst = (outputs['inst']==params['bbox_inst_id']).float() \
                if not (params['bbox_inst_id'] == None) else torch.zeros(outputs['inst'].size())
        ### generate output bbox
        img_size = outputs['label'].size(1) #shape[1]
        context_ratio = np.random.uniform(
          low=self.config['min_ctx_ratio'], high=self.config['max_ctx_ratio'])
        output_bbox = np.array(get_soft_bbox(input_bbox, img_size, img_size, context_ratio))
        mask_in, mask_object_in, mask_context_in = get_masked_image(
            outputs['label'], input_bbox, bbox_cls)
        mask_out, mask_object_out, _ = get_masked_image(
            outputs['label'], output_bbox)
        # Build dictionary
        outputs['input_bbox'] = torch.from_numpy(input_bbox)
        outputs['output_bbox'] = torch.from_numpy(output_bbox)
        outputs['mask_in'] = mask_in # (1x1xHxW)
        outputs['mask_object_in'] = mask_object_in # (1xCxHxW)
        outputs['mask_context_in'] = mask_context_in # (1xCxHxW)
        outputs['mask_out'] = mask_out # (1x1xHxW)
        outputs['mask_object_out'] = mask_object_out # (1xCxHxW)
        outputs['label_obj'] = label_obj
        outputs['mask_object_inst'] = mask_object_inst
        outputs['cls'] = torch.LongTensor([bbox_cls])
        return outputs 
Example #6
Source File: aligned_pair_dataset.py    From everybody_dance_now_pytorch with GNU Affero General Public License v3.0 4 votes vote down vote up
def __getitem__(self, index):        
        ### input A (label maps)
        if index > self.dataset_size - self.clip_length:
            index = 0  # it's a rare chance and won't be effecting training dynamics

        A_path = self.A_paths[index: index + self.clip_length]
        A = [Image.open(path) for path in A_path]
        params = get_params(self.opt, A[0].size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = [transform_A(item.convert('RGB')) for item in A]
            A_tensor = torch.stack(A_tensor, dim=0)
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain:
            B_path = self.B_paths[index: index + self.clip_length]
            B = [Image.open(path).convert('RGB') for path in B_path]
            transform_B = get_transform(self.opt, params)      
            B_tensor = [transform_B(item) for item in B]
            B_tensor = torch.stack(B_tensor, dim=0)
        else: # only retain first frame for testing
            B_path = self.B_paths[index]
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)
            B_tensor = transform_B(B)

        ### if using instance maps (which is never supposed to)
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index: index + self.clip_length]
            inst = [Image.open(path) for path in inst_path]
            inst_tensor = [transform_A(item) for item in inst]
            inst_tensor = torch.stack(inst_tensor, dim=0)

            if self.opt.load_features:
                feat_path = self.feat_paths[index: index + self.clip_length]
                feat = [Image.open(path).convert('RGB') for path in feat_path]
                norm = normalize()
                feat_tensor = [norm(transform_A(item)) for item in feat]
                feat_tensor = torch.stack(feat_tensor, dim=0)

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict 
Example #7
Source File: aligned_dataset.py    From EverybodyDanceNow-Temporal-FaceGAN with MIT License 4 votes vote down vote up
def __getitem__(self, index):        
		### input A (label maps)
		A_path = self.A_paths[index]              
		A_tensor = torch.load(A_path).permute((2,0,1))
		# A = Image.open(A_path)        
		# params = get_params(self.opt, A.size)
		# if self.opt.label_nc == 0:
		#     transform_A = get_transform(self.opt, params)
		#     A_tensor = transform_A(A.convert('RGB'))
		# else:
		#     transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
		#     A_tensor = transform_A(A) * 255.0

		B_tensor = inst_tensor = feat_tensor = 0
		### input B (real images)
		if self.opt.isTrain:
			B_path = self.B_paths[index]   
			B = Image.open(B_path).convert('RGB')
			# transform_B = get_transform(self.opt, params)
			# B_tensor = transform_B(B)
			B = np.array(B, dtype = float) / 255.
			B_tensor = torch.tensor(B)[:,:,:3].permute((2,0,1)).float()

			# fig = plt.figure(1)
			# ax = fig.add_subplot(111)
			# ax.imshow(B_tensor[:,:1024,:].permute((1,2,0)))
			# plt.show()

		### if using instance maps        
		if not self.opt.no_instance:
			inst_path = self.inst_paths[index]
			inst = Image.open(inst_path)
			inst_tensor = transform_A(inst)

			if self.opt.load_features:
				feat_path = self.feat_paths[index]            
				feat = Image.open(feat_path).convert('RGB')
				norm = normalize()
				feat_tensor = norm(transform_A(feat))                            

		input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
					  'feat': feat_tensor, 'path': A_path}

		return input_dict 
Example #8
Source File: aligned_dataset_GAN.py    From EverybodyDanceNow-Temporal-FaceGAN with MIT License 4 votes vote down vote up
def __getitem__(self, index):        
		### input A (label maps)
		lpath = self.label_paths[index]
		A_tensor_0 = torch.load(lpath).permute((2,0,1)).float()

		idx_ = lpath.split('/')[-1][:12]
		spath = self.opt.input_image_root + '%s_synthesized_image.jpg'%idx_
		A = Image.open(spath).convert('RGB')
		A = np.array(A, dtype = float) / 255.
		A = A[:,:,:3]
		idx = lpath.split('/')[-1].split('.')[0]

		minx, maxx, miny, maxy = list(self.crop_coor[int(idx), :])
		A = A[minx: maxx + 1, miny: maxy + 1, :]
		A  = cv2.resize(A, (128, 128))
		A_tensor_1 = torch.tensor(A).permute((2,0,1)).float()
		A_tensor = torch.cat((A_tensor_0, A_tensor_1), dim = 0)

		B_tensor = inst_tensor = feat_tensor = 0


		lidx = lpath.split('/')[-1][:12]
		sidx = spath.split('/')[-1][:12]
		### input B (real images)
		if self.opt.isTrain:
			B_path = self.rimage_paths[index]   
			B = Image.open(B_path).convert('RGB')
			B = np.array(B, dtype = float) / 255.
			B_tensor = torch.tensor(B)[:,:,:3].permute((2,0,1)).float()

			# fig = plt.figure(1)
			# ax = fig.add_subplot(111)
			# ax.imshow(B_tensor[:,:1024,:].permute((1,2,0)))
			# plt.show()
			ridx = B_path.split('/')[-1][:12]
			assert lidx == ridx , "Wrong match"

		### if using instance maps        
		if not self.opt.no_instance:
			inst_path = self.inst_paths[index]
			inst = Image.open(inst_path)
			inst_tensor = transform_A(inst)

			if self.opt.load_features:
				feat_path = self.feat_paths[index]            
				feat = Image.open(feat_path).convert('RGB')
				norm = normalize()
				feat_tensor = norm(transform_A(feat))                            

		# print(lpath, spath, B_path)
		# print(lidx, sidx )
		assert lidx == sidx , "Wrong match"
		# fig = plt.figure(1)
		# ax = fig.add_subplot(111)
		# ax.imshow(A)
		# plt.show()

		input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
					  'feat': feat_tensor, 'path': lpath}

		return input_dict 
Example #9
Source File: aligned_dataset_temporal.py    From EverybodyDanceNow-Temporal-FaceGAN with MIT License 4 votes vote down vote up
def work(self, index):        
		### input A (label maps)
		A_path = self.A_paths[index]              
		A_tensor = torch.load(A_path).permute((2,0,1))
		# A = Image.open(A_path)        
		# params = get_params(self.opt, A.size)
		# if self.opt.label_nc == 0:
		#     transform_A = get_transform(self.opt, params)
		#     A_tensor = transform_A(A.convert('RGB'))
		# else:
		#     transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
		#     A_tensor = transform_A(A) * 255.0

		B_tensor = inst_tensor = feat_tensor = 0
		### input B (real images)
		if self.opt.isTrain:
			B_path = self.B_paths[index]   
			B = Image.open(B_path).convert('RGB')
			# transform_B = get_transform(self.opt, params)
			# B_tensor = transform_B(B)
			B = np.array(B, dtype = float) / 255.
			B_tensor = torch.tensor(B)[:,:,:3].permute((2,0,1)).float()

			# fig = plt.figure(1)
			# ax = fig.add_subplot(111)
			# ax.imshow(B_tensor[:,:1024,:].permute((1,2,0)))
			# plt.show()

		### if using instance maps        
		if not self.opt.no_instance:
			inst_path = self.inst_paths[index]
			inst = Image.open(inst_path)
			inst_tensor = transform_A(inst)

			if self.opt.load_features:
				feat_path = self.feat_paths[index]            
				feat = Image.open(feat_path).convert('RGB')
				norm = normalize()
				feat_tensor = norm(transform_A(feat))                            

		input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
					  'feat': feat_tensor, 'path': A_path}

		return input_dict