-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtransforms.py
More file actions
77 lines (60 loc) · 1.62 KB
/
transforms.py
File metadata and controls
77 lines (60 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu May 4 15:22:58 2017
@author: loop
"""
from __future__ import division
import torch
import math
import random
from PIL import Image, ImageOps
import numpy as np
import numbers
import types
class Compose(object):
"""Composes several transforms together.
Args:
transforms (List[Transform]): list of transforms to compose.
Example:
>>> transforms.Compose([
>>> transforms.CenterCrop(10),
>>> transforms.ToTensor(),
>>> ])
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img
class ToTensor(object):
"""
Converts numpy.ndarray (N x H x W x C x 1) in the range
[0, 255] to a torch.FloatTensor of shape (N x H x W x C x 1).
"""
def __call__(self, pic):
# handle numpy array
img = torch.from_numpy(pic)
# backard compability
return img
class Normalize(object):
"""
will normalize each channel of the torch.*Tensor, i.e.
channel = channel/127.5 - 1
"""
def __call__(self, tensor):
# TODO: make efficient
for t in tensor:
t.div_(127.5).sub_(1)
return tensor
class RandomHorizontalFlip(object):
"""
Randomly horizontally flips the given numpy.ndarray
(N x H x W x C x 1) with a probability of 0.5
"""
def __call__(self, img):
for n in xrange(img.shape[0]):
if random.random() < 0.5:
img[n] = img[n,:,::-1]
return img