本文整理匯總了Python中torchvision.transforms.functional.normalize方法的典型用法代碼示例。如果您正苦於以下問題:Python functional.normalize方法的具體用法?Python functional.normalize怎麽用?Python functional.normalize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torchvision.transforms.functional
的用法示例。
在下文中一共展示了functional.normalize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, rgb_img, label_img=None):
label1 = label_img
label2 = label_img
if self.scale1 != 1:
w, h = label_img.size
label1 = label1.resize((w//self.scale1, h//self.scale1), Image.NEAREST)
if self.scale2 != 1:
w, h = label_img.size
label2 = label2.resize((w//self.scale2, h//self.scale2), Image.NEAREST)
rgb_img = F.to_tensor(rgb_img) # convert to tensor (values between 0 and 1)
rgb_img = F.normalize(rgb_img, self.mean, self.std) # normalize the tensor
label1 = torch.LongTensor(np.array(label1).astype(np.int64))
label2 = torch.LongTensor(np.array(label2).astype(np.int64))
return rgb_img, label1, label2
示例2: prepare_input
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def prepare_input(frame, flip_left_right=False, debug=False):
# BGR to RGB and flip frame
input_image = np.flip(frame, axis=2).copy()
if flip_left_right:
input_image = np.flip(input_image, axis=1).copy()
# Concert to shape batch_size=1, rgb, h, w
input_image = torch.Tensor(input_image.transpose(2, 0, 1))
# To debug what is actually fed to network
if debug:
plt.imshow(input_image.numpy().transpose(1, 2, 0) / 255)
plt.show()
input_image = func_transforms.normalize(
input_image / 255, [0.5, 0.5, 0.5], [1, 1, 1]
).unsqueeze(0)
# Equivalently
# input_image_1 = input_image / 255 - 0.5
input_image = input_image.cuda()
return input_image
示例3: normalize
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def normalize(tensor, mean, std):
"""Normalize a tensor image with mean and standard deviation.
See ``Normalize`` for more details.
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
mean (sequence): Sequence of means for each channel.
std (sequence): Sequence of standard deviations for each channely.
Returns:
Tensor: Normalized Tensor image.
"""
if _is_tensor_image(tensor):
for t, m, s in zip(tensor, mean, std):
t.sub_(m).div_(s)
return tensor
elif _is_numpy_image(tensor):
return (tensor.astype(np.float32) - 255.0 * np.array(mean))/np.array(std)
else:
raise RuntimeError('Undefined type')
示例4: cv_transform
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def cv_transform(img):
# img = resize(img, size=(100, 300))
# img = to_tensor(img)
# img = normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# img = pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant')
# img = pad(img, padding=(100, 100, 100, 100), fill=5, padding_mode='symmetric')
# img = crop(img, -40, -20, 1000, 1000)
# img = center_crop(img, (310, 300))
# img = resized_crop(img, -10.3, -20, 330, 220, (500, 500))
# img = hflip(img)
# img = vflip(img)
# tl, tr, bl, br, center = five_crop(img, 100)
# img = adjust_brightness(img, 2.1)
# img = adjust_contrast(img, 1.5)
# img = adjust_saturation(img, 2.3)
# img = adjust_hue(img, 0.5)
# img = adjust_gamma(img, gamma=3, gain=0.1)
# img = rotate(img, 10, resample='BILINEAR', expand=True, center=None)
# img = to_grayscale(img, 3)
# img = affine(img, 10, (0, 0), 1, 0, resample='BICUBIC', fillcolor=(255,255,0))
# img = gaussion_noise(img)
# img = poisson_noise(img)
img = salt_and_pepper(img)
return to_tensor(img)
示例5: pil_transform
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def pil_transform(img):
# img = functional.resize(img, size=(100, 300))
# img = functional.to_tensor(img)
# img = functional.normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# img = functional.pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant')
# img = functional.pad(img, padding=(100, 100, 100, 100), padding_mode='symmetric')
# img = functional.crop(img, -40, -20, 1000, 1000)
# img = functional.center_crop(img, (310, 300))
# img = functional.resized_crop(img, -10.3, -20, 330, 220, (500, 500))
# img = functional.hflip(img)
# img = functional.vflip(img)
# tl, tr, bl, br, center = functional.five_crop(img, 100)
# img = functional.adjust_brightness(img, 2.1)
# img = functional.adjust_contrast(img, 1.5)
# img = functional.adjust_saturation(img, 2.3)
# img = functional.adjust_hue(img, 0.5)
# img = functional.adjust_gamma(img, gamma=3, gain=0.1)
# img = functional.rotate(img, 10, resample=PIL.Image.BILINEAR, expand=True, center=None)
# img = functional.to_grayscale(img, 3)
# img = functional.affine(img, 10, (0, 0), 1, 0, resample=PIL.Image.BICUBIC, fillcolor=(255,255,0))
return functional.to_tensor(img)
示例6: __getitem__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __getitem__(self, index):
# Apply transforms to the image.
image = torch.FloatTensor(self.nc,self.out_img_size, self.out_img_size).fill_(-1.)
# Get the individual images.
randbox = random.randrange(len(self.metadata['images'][index]))
imglabel = np.zeros(10, dtype=np.int)
boxlabel = np.zeros(10, dtype=np.int)
for i,bb in enumerate(self.metadata['images'][index]):
imid = random.randrange(self.num_data)
bbox = [int(bc*self.out_img_size) for bc in bb]
img, label = self.dataset[imid]
scImg = FN.resize(img,(bbox[3],bbox[2]))
image[:, bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] = FN.normalize(FN.to_tensor(scImg), mean=(0.5,)*self.nc, std=(0.5,)*self.nc)
#imglabel[label] = 1
if i == randbox:
outBox = FN.normalize(FN.to_tensor(FN.resize(scImg, (self.bbox_out_size, self.bbox_out_size))), mean=(0.5,)*self.nc, std=(0.5,)*self.nc)
mask = torch.zeros(1,self.out_img_size,self.out_img_size)
mask[0,bbox[1]:bbox[1]+bbox[3],bbox[0]:bbox[0]+bbox[2]] = 1.
outbbox = bbox
#boxlabel[label]=1
#return image[[0,0,0],::], torch.FloatTensor([1]), outBox[[0,0,0],::], torch.FloatTensor([1]), mask, torch.IntTensor(outbbox)
return image, torch.FloatTensor([1]), outBox, torch.FloatTensor([1]), mask, torch.IntTensor(outbbox)
示例7: base_transform_nimgs
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def base_transform_nimgs(images, size, mean, stds, seq_len=1):
res_imgs = []
# print(images.shape)
for i in range(seq_len):
# img = Image.fromarray(images[i,:, :, :])
# img = img.resize((size, size), Image.BILINEAR)
img = cv2.resize(images[i, :, :, :], (size, size)).astype(np.float32)
#img = images[i, :, :, :].astype(np.float32)
# img = np.asarray(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
res_imgs += [torch.from_numpy(img).permute(2, 0, 1)]
# pdb.set_trace()
# res_imgs = np.asarray(res_imgs)
return [F.normalize(img_tensor, mean, stds) for img_tensor in res_imgs]
# return res_imgs
示例8: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, image, target):
if self.to_bgr255:
image = image[[2, 1, 0]] * 255
image = F.normalize(image, mean=self.mean, std=self.std)
return image, target
示例9: to_tensor_and_normalize
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def to_tensor_and_normalize(image):
return functional.normalize(functional.to_tensor(image), mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
示例10: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, sample):
sample['leftImage'] = F.normalize(
sample['leftImage'], mean=self.mean, std=self.std
)
sample['rightImage'] = F.normalize(
sample['rightImage'], mean=self.mean, std=self.std
)
return sample
示例11: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, image, target):
image = F.normalize(image, mean=self.mean, std=self.std)
return image, target
示例12: _instance_process
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def _instance_process(self, image, params):
image.img = F.normalize(image.img, self.mean[:3], self.std[:3])
if image.x is not None:
image.x = F.normalize(image.x, self.mean[3:4], self.std[3:4])
if image.y is not None:
image.y = F.normalize(image.y, self.mean[3:4], self.std[3:4])
return image
示例13: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, image, target=None):
if self.to_bgr255:
image = image[[2, 1, 0]] * 255
image = F.normalize(image, mean=self.mean, std=self.std)
if target is None:
return image
return image, target
示例14: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, sample):
input_data = sample['input']
input_data = F.normalize(input_data, self.mean, self.std)
rdict = {
'input': input_data,
}
sample.update(rdict)
return sample
示例15: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import normalize [as 別名]
def __call__(self, img, target):
img = F.normalize(img, mean=self.mean, std=self.std)
return img, target