You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

transforms.py 39 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import tensorlayer as tl
  4. from . import load_vision_backend as F
  5. import numbers
  6. import numpy as np
  7. __all__ = [
  8. 'Crop',
  9. 'CentralCrop',
  10. 'HsvToRgb',
  11. 'AdjustBrightness',
  12. 'AdjustContrast',
  13. 'AdjustHue',
  14. 'AdjustSaturation',
  15. 'FlipHorizontal',
  16. 'FlipVertical',
  17. 'RgbToGray',
  18. 'PadToBoundingbox',
  19. 'Pad',
  20. 'Normalize',
  21. 'StandardizePerImage',
  22. 'RandomBrightness',
  23. 'RandomContrast',
  24. 'RandomHue',
  25. 'RandomSaturation',
  26. 'RandomCrop',
  27. 'Resize',
  28. 'RgbToHsv',
  29. 'Transpose',
  30. 'RandomRotation',
  31. 'RandomShift',
  32. 'RandomShear',
  33. 'RandomZoom',
  34. 'RandomFlipVertical',
  35. 'RandomFlipHorizontal',
  36. 'HWC2CHW',
  37. 'CHW2HWC',
  38. 'ToTensor',
  39. 'Compose',
  40. 'RandomResizedCrop',
  41. 'RandomAffine',
  42. 'ColorJitter',
  43. ]
  44. class ToTensor(object):
  45. """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
  46. Parameters
  47. ----------
  48. data_format : str
  49. Data format of output tensor, should be 'HWC' or 'CHW'. Default: 'HWC'.
  50. Examples
  51. ----------
  52. With TensorLayer
  53. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  54. >>> transform = tl.vision.transforms.ToTensor(data_format='HWC')
  55. >>> image = transform(image)
  56. >>> print(image)
  57. """
  58. def __init__(self, data_format='HWC'):
  59. if not data_format in ['CHW', 'HWC']:
  60. raise ValueError('data_format should be CHW or HWC. Got {}'.format(data_format))
  61. self.data_format = data_format
  62. def __call__(self, image):
  63. F.to_tensor(image, self.data_format)
  64. class CentralCrop(object):
  65. """Crops the given image at the center.If the size is given, image will be cropped as size.
  66. If the central_fraction is given, image will cropped as (H * central_fraction, W * fraction).
  67. Size has a higher priority.
  68. Parameters
  69. ----------
  70. size : int or sequence of int
  71. The output size of the cropped image.
  72. If size is an integer, a square crop of size (size, size) is returned.
  73. If size is a sequence of length 2, it should be (height, width).
  74. central_fraction : float
  75. float (0, 1], fraction of size to crop
  76. Examples
  77. ----------
  78. With TensorLayer
  79. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  80. >>> transform = tl.vision.transforms.CentralCrop(size = (50, 50))
  81. >>> image = transform(image)
  82. >>> print(image)
  83. >>> image shape : (50, 50, 3)
  84. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  85. >>> transform = tl.vision.transforms.CentralCrop(central_fraction=0.5)
  86. >>> image = transform(image)
  87. >>> print(image)
  88. >>> image shape : (112, 112, 3)
  89. """
  90. def __init__(self, size=None, central_fraction=None):
  91. self.central_fraction = central_fraction
  92. self.size = size
  93. def __call__(self, image):
  94. F.central_crop(image, self.size, self.central_fraction)
  95. class Compose(object):
  96. """Composes several transforms together.
  97. Parameters
  98. ----------
  99. transforms : list of 'transform' objects
  100. list of transforms to compose.
  101. Examples
  102. ----------
  103. With TensorLayer
  104. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  105. >>> transform = tl.vision.transforms.Compose([tl.vision.transforms.ToTensor(data_format='HWC'),tl.vision.transforms.CentralCrop(size = 100)])
  106. >>> image = transform(image)
  107. >>> print(image)
  108. >>> image shape : (100, 100, 3)
  109. """
  110. def __init__(self, transforms):
  111. self.transforms = transforms
  112. def __call__(self, data):
  113. for t in self.transforms:
  114. data = t(data)
  115. return data
  116. class Crop(object):
  117. """Crops an image to a specified bounding box.
  118. Parameters
  119. ----------
  120. offset_height : int
  121. Vertical coordinate of the top-left corner of the bounding box in image.
  122. offset_width: int
  123. Horizontal coordinate of the top-left corner of the bounding box in image.
  124. target_height: int
  125. Height of the bounding box.
  126. target_width: int
  127. Width of the bounding box.
  128. Examples
  129. ----------
  130. With TensorLayer
  131. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  132. >>> transform = tl.vision.transforms.Crop(offset_height=10, offset_width=10, target_height=100, target_width=100)
  133. >>> image = transform(image)
  134. >>> print(image)
  135. >>> image shape : (100, 100, 3)
  136. """
  137. def __init__(self, offset_height, offset_width, target_height, target_width):
  138. self.offset_height = offset_height
  139. self.offset_width = offset_width
  140. self.target_height = target_height
  141. self.target_width = target_width
  142. def __call__(self, image):
  143. return F.crop(image, self.offset_height, self.offset_width, self.target_height, self.target_width)
  144. class Pad(object):
  145. """Pad the given image on all sides with the given "pad" value.
  146. Parameters
  147. ----------
  148. padding : int or sequenece
  149. Padding on each border.
  150. If a single int is provided, this is used to pad all borders.
  151. If sequence of length 2 is provided, this is the padding on left/right and top/bottom respectively.
  152. If a sequence of length 4 is provided, this is the padding for the left, top, right and bottom borders respectively.
  153. padding_value : number or sequenece
  154. Pixel fill value for constant fill. Default is 0.
  155. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  156. This value is only used when the mode is constant.
  157. mode : str
  158. Type of padding. Default is constant.
  159. Examples
  160. ----------
  161. With TensorLayer
  162. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  163. >>> transform = tl.vision.transforms.Pad(padding=10, padding_value=0, mode='constant')
  164. >>> image = transform(image)
  165. >>> print(image)
  166. >>> image shape : (244, 244, 3)
  167. """
  168. def __init__(self, padding, padding_value=0, mode='constant'):
  169. self.padding = padding
  170. self.padding_value = padding_value
  171. self.mode = mode
  172. def __call__(self, image):
  173. return F.pad(image, self.padding, self.padding_value, self.mode)
  174. class Resize(object):
  175. """Resize the input image to the given size.
  176. Parameters
  177. ----------
  178. size : int or sequenece
  179. Desired output size.
  180. If size is a sequence like (h, w), output size will be matched to this.
  181. If size is an int, smaller edge of the image will be matched to this number.
  182. i.e, if height > width, then image will be rescaled to (size * height / width, size).
  183. interpolation : str
  184. Interpolation method. Default: 'bilinear'.
  185. Examples
  186. ----------
  187. With TensorLayer
  188. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  189. >>> transform = tl.vision.transforms.Resize(size = (100,100), interpolation='bilinear')
  190. >>> image = transform(image)
  191. >>> print(image)
  192. >>> image shape : (100, 100, 3)
  193. """
  194. def __init__(self, size, interpolation='bilinear'):
  195. self.size = size
  196. self.interpolation = interpolation
  197. def __call__(self, image):
  198. return F.resize(image, self.size, self.interpolation)
  199. class Transpose(object):
  200. """Transpose image(s) by swapping dimension.
  201. Parameters
  202. ----------
  203. order : sequenece of int
  204. Desired output dimension order.
  205. Examples
  206. ----------
  207. With TensorLayer
  208. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  209. >>> transform = tl.vision.transforms.Transpose(order=(2, 0 ,1))
  210. >>> image = transform(image)
  211. >>> print(image)
  212. >>> image shape : (3, 224, 224)
  213. """
  214. def __init__(self, order):
  215. self.order = order
  216. def __call__(self, image):
  217. return F.transpose(image, self.order)
  218. class HWC2CHW(object):
  219. """Transpose a image shape (H, W, C) to shape (C, H, W).
  220. Examples
  221. ----------
  222. With TensorLayer
  223. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  224. >>> transform = tl.vision.transforms.HWC2CHW()
  225. >>> image = transform(image)
  226. >>> print(image)
  227. >>> image shape : (3, 224, 224)
  228. """
  229. def __call__(self, image):
  230. F.hwc_to_chw(image)
  231. class CHW2HWC(object):
  232. """Transpose a image shape (C, H, W) to shape (H, W, C).
  233. Examples
  234. ----------
  235. With TensorLayer
  236. >>> image = (np.random.rand(3, 224, 224) * 255.).astype(np.uint8)
  237. >>> transform = tl.vision.transforms.CHW2HWC()
  238. >>> image = transform(image)
  239. >>> print(image)
  240. >>> image shape : (224, 224, 3)
  241. """
  242. def __call__(self, image):
  243. F.chw_to_hwc(image)
  244. class RgbToHsv(object):
  245. """Converts a image from RGB to HSV.
  246. Examples
  247. ----------
  248. With TensorLayer
  249. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  250. >>> transform = tl.vision.transforms.RgbToHsv()
  251. >>> image = transform(image)
  252. >>> print(image)
  253. >>> image shape : (224, 224, 3)
  254. """
  255. def __call__(self, image):
  256. F.rgb_to_hsv(image)
  257. class HsvToRgb(object):
  258. """Converts a image from HSV to RGB.
  259. Examples
  260. ----------
  261. With TensorLayer
  262. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  263. >>> transform = tl.vision.transforms.HsvToRgb()
  264. >>> image = transform(image)
  265. >>> print(image)
  266. >>> image shape : (224, 224, 3)
  267. """
  268. def __call__(self, image):
  269. F.hsv_to_rgb(image)
  270. class RgbToGray(object):
  271. """Converts a image from RGB to grayscale.
  272. Parameters
  273. ----------
  274. num_output_channels: int
  275. (1 or 3) number of channels desired for output image. Default is 1.
  276. Examples
  277. ----------
  278. With TensorLayer
  279. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  280. >>> transform = tl.vision.transforms.RgbToGray(num_output_channels=1)
  281. >>> image = transform(image)
  282. >>> print(image)
  283. >>> image shape : (224, 224, 1)
  284. """
  285. def __init__(self, num_output_channels=1):
  286. self.num_output_channels = num_output_channels
  287. def __call__(self, image):
  288. F.rgb_to_gray(image, self.num_output_channels)
  289. class AdjustBrightness(object):
  290. """Adjust brightness of the image.
  291. Parameters
  292. ----------
  293. brightness_factor: float
  294. How much to adjust the brightness. Can be any non negative number. 1 gives the original image.
  295. Default is 1.
  296. Examples
  297. ----------
  298. With TensorLayer
  299. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  300. >>> transform = tl.vision.transforms.AdjustBrightness(brightness_factor=1)
  301. >>> image = transform(image)
  302. >>> print(image)
  303. """
  304. def __init__(self, brightness_factor=1):
  305. self.brightness_factor = brightness_factor
  306. def __call__(self, image):
  307. return F.adjust_brightness(image, self.brightness_factor)
  308. class AdjustContrast(object):
  309. """Adjust contrast of the image.
  310. Parameters
  311. ----------
  312. contrast_factor: float
  313. How much to adjust the contrast. Can be any non negative number. 1 gives the original image.
  314. Default is 1.
  315. Examples
  316. ----------
  317. With TensorLayer
  318. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  319. >>> transform = tl.vision.transforms.AdjustContrast(contrast_factor=1)
  320. >>> image = transform(image)
  321. >>> print(image)
  322. """
  323. def __init__(self, contrast_factor=1):
  324. self.contrast_factor = contrast_factor
  325. def __call__(self, image):
  326. return F.adjust_contrast(image, self.contrast_factor)
  327. class AdjustHue(object):
  328. """Adjust hue of the image.
  329. Parameters
  330. ----------
  331. hue_factor: float
  332. How much to shift the hue channel. Should be in [-0.5, 0.5].
  333. 0.5 and -0.5 give complete reversal of hue channel in HSV space in positive and negative direction respectively.
  334. 0 means no shift. Therefore, both -0.5 and 0.5 will give an image with complementary colors while 0 gives the original image.
  335. Default is 0.
  336. Examples
  337. ----------
  338. With TensorLayer
  339. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  340. >>> transform = tl.vision.transforms.AdjustHue(hue_factor=0)
  341. >>> image = transform(image)
  342. >>> print(image)
  343. """
  344. def __init__(self, hue_factor=0):
  345. self.hue_factor = hue_factor
  346. def __call__(self, image):
  347. return F.adjust_hue(image, self.hue_factor)
  348. class AdjustSaturation(object):
  349. """Adjust saturation of the image.
  350. Parameters
  351. ----------
  352. saturation_factor: float
  353. How much to adjust the saturation. Can be any non negative number. 1 gives the original image.
  354. Default is 1.
  355. Examples
  356. ----------
  357. With TensorLayer
  358. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  359. >>> transform = tl.vision.transforms.AdjustSaturation(saturation_factor=1)
  360. >>> image = transform(image)
  361. >>> print(image)
  362. """
  363. def __init__(self, saturation_factor=1):
  364. self.saturation_factor = saturation_factor
  365. def __call__(self, image):
  366. return F.adjust_saturation(image, self.saturation_factor)
  367. class FlipHorizontal(object):
  368. """Flip an image horizontally.
  369. Examples
  370. ----------
  371. With TensorLayer
  372. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  373. >>> transform = tl.vision.transforms.FlipHorizontal()
  374. >>> image = transform(image)
  375. >>> print(image)
  376. """
  377. def __call__(self, image):
  378. return F.hflip(image)
  379. class FlipVertical(object):
  380. """Flip an image vertically.
  381. Examples
  382. ----------
  383. With TensorLayer
  384. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  385. >>> transform = tl.vision.transforms.FlipVertical()
  386. >>> image = transform(image)
  387. >>> print(image)
  388. """
  389. def __call__(self, image):
  390. return F.vflip(image)
  391. class PadToBoundingbox(object):
  392. """Pad image with the specified height and width to target size.
  393. Parameters
  394. ----------
  395. offset_height: int
  396. Number of rows to add on top.
  397. offset_width: int
  398. Number of columns to add on the left.
  399. target_height: int
  400. Height of output image.
  401. target_width: int
  402. Width of output image.
  403. padding_value: int or sequence
  404. value to pad.
  405. Examples
  406. ----------
  407. With TensorLayer
  408. >>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
  409. >>> transform = tl.vision.transforms.PadToBoundingbox(offset_height=10, offset_width=10, target_height=300, target_width=300, padding_value=0)
  410. >>> image = transform(image)
  411. >>> print(image)
  412. >>> image shape : (300, 300, 3)
  413. """
  414. def __init__(self, offset_height, offset_width, target_height, target_width, padding_value=0):
  415. self.offset_height = offset_height
  416. self.offset_width = offset_width
  417. self.target_height = target_height
  418. self.target_width = target_width
  419. self.padding_value = padding_value
  420. def __call__(self, image):
  421. return F.padtoboundingbox(
  422. image, self.offset_height, self.offset_width, self.target_height, self.target_width, self.padding_value
  423. )
  424. class Normalize(object):
  425. """Normalize a tensor image with mean and standard deviation.
  426. Parameters
  427. ----------
  428. mean: number or sequence
  429. If mean is a number, mean will be applied for all channels. Sequence of means for each channel.
  430. std: number or sequnece
  431. If std is a number, std will be applied for all channels.Sequence of standard deviations for each channel.
  432. data_format: str
  433. Data format of input image, should be 'HWC' or 'CHW'. Default: 'HWC'.
  434. Examples
  435. ----------
  436. With TensorLayer
  437. >>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
  438. >>> transform = tl.vision.transforms.Normalize(mean = (155.0, 155.0, 155.0), std = (75.0, 75.0, 75.0),data_format='HWC')
  439. >>> image = transform(image)
  440. >>> print(image)
  441. """
  442. def __init__(self, mean, std, data_format='HWC'):
  443. self.mean = mean
  444. self.std = std
  445. self.data_format = data_format
  446. def __call__(self, image):
  447. return F.normalize(image, self.mean, self.std, self.data_format)
  448. class StandardizePerImage(object):
  449. """For each 3-D image x in image, computes (x - mean) / adjusted_stddev, where mean is the average of all values in x.
  450. adjusted_stddev = max(stddev, 1.0/sqrt(N)) is capped away from 0 to protect against division by 0 when handling uniform images.
  451. N is the number of elements in x. stddev is the standard deviation of all values in x
  452. Examples
  453. ----------
  454. With TensorLayer
  455. >>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
  456. >>> transform = tl.vision.transforms.StandardizePerImage()
  457. >>> image = transform(image)
  458. >>> print(image)
  459. """
  460. def __call__(self, image):
  461. return F.standardize(image)
  462. class RandomBrightness(object):
  463. """Random adjust brightness of the image.
  464. Parameters
  465. ----------
  466. brightness_factor: float or sequence
  467. Brightness adjustment factor (default=(1, 1)).
  468. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness_factor), 1+brightness_factor].
  469. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  470. Examples
  471. ----------
  472. With TensorLayer
  473. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  474. >>> transform = tl.vision.transforms.RandomBrightness(brightness_factor=(0.5, 2))
  475. >>> image = transform(image)
  476. >>> print(image)
  477. """
  478. def __init__(self, brightness_factor=(1, 1)):
  479. self.brighthness_factor = brightness_factor
  480. def __call__(self, image):
  481. return F.random_brightness(image, self.brighthness_factor)
  482. class RandomContrast(object):
  483. """Random adjust contrast of the image.
  484. Parameters
  485. ----------
  486. contrast_factor: float or sequence
  487. Contrast adjustment factor (default=(1, 1)).
  488. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast_factor), 1+contrast_factor].
  489. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  490. Examples
  491. ----------
  492. With TensorLayer
  493. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  494. >>> transform = tl.vision.transforms.RandomContrast(contrast_factor=(0.5, 2))
  495. >>> image = transform(image)
  496. >>> print(image)
  497. """
  498. def __init__(self, contrast_factor=(1, 1)):
  499. self.contrast_factor = contrast_factor
  500. def __call__(self, image):
  501. return F.random_contrast(image, self.contrast_factor)
  502. class RandomSaturation(object):
  503. """Random adjust saturation of the image.
  504. Parameters
  505. ----------
  506. saturation_factor: float or sequence
  507. Saturation adjustment factor (default=(1, 1)).
  508. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation_factor), 1+saturation_factor].
  509. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  510. Examples
  511. ----------
  512. With TensorLayer
  513. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  514. >>> transform = tl.vision.transforms.RandomSaturation(saturation_factor=(0.5, 2))
  515. >>> image = transform(image)
  516. >>> print(image)
  517. """
  518. def __init__(self, saturation_factor=(1, 1)):
  519. self.saturation_factor = saturation_factor
  520. def __call__(self, image):
  521. return F.random_saturation(image, self.saturation_factor)
  522. class RandomHue(object):
  523. """Random adjust hue of the image.
  524. Parameters
  525. ----------
  526. hue_factor: float or sequence
  527. Hue adjustment factor (default=(0, 0)).
  528. If it is a float, the factor is uniformly chosen from the range [-hue_factor, hue_factor].
  529. If it is a sequence, it should be [min, max] for the range.Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.
  530. Examples
  531. ----------
  532. With TensorLayer
  533. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  534. >>> transform = tl.vision.transforms.RandomHue(hue_factor=(-0.5, 0.5))
  535. >>> image = transform(image)
  536. >>> print(image)
  537. """
  538. def __init__(self, hue_factor=(0, 0)):
  539. self.hue_factor = hue_factor
  540. def __call__(self, image):
  541. return F.random_hue(image, self.hue_factor)
  542. class RandomCrop(object):
  543. """Crop the given image at a random location.
  544. Parameters
  545. ----------
  546. size: int or sequence
  547. Desired output size of the crop.
  548. If size is an int instead of sequence like (h, w), a square crop (size, size) is made.
  549. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  550. padding: int or sequence, optional
  551. Optional padding on each border of the image.
  552. If a single int is provided this is used to pad all borders.
  553. If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively.
  554. If a sequence of length 4 is provided, it is used to pad left, top, right, bottom borders respectively.
  555. Default: 0.
  556. pad_if_needed: boolean
  557. It will pad the image if smaller than the desired size to avoid raising an exception.
  558. Since cropping is done after padding, the padding seems to be done at a random offset.
  559. fill: number or sequence
  560. Pixel fill value for constant fill. Default is 0.
  561. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  562. padding_mode: str
  563. Type of padding. Default is constant.
  564. Examples
  565. ----------
  566. With TensorLayer
  567. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  568. >>> transform = tl.vision.transforms.RandomCrop(size=50, padding=10, pad_if_needed=False, fill=0, padding_mode='constant')
  569. >>> image = transform(image)
  570. >>> print(image)
  571. >>> image shape : (70,70,3)
  572. """
  573. def __init__(self, size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant'):
  574. self.size = size
  575. self.padding = padding
  576. self.pad_if_needed = pad_if_needed
  577. self.fill = fill
  578. self.padding_mode = padding_mode
  579. def __call__(self, image):
  580. return F.random_crop(
  581. image,
  582. size=self.size,
  583. padding=self.padding,
  584. pad_if_needed=self.pad_if_needed,
  585. fill=self.fill,
  586. padding_mode=self.padding_mode,
  587. )
  588. class RandomResizedCrop(object):
  589. """Crop the given image to random size and aspect ratio.
  590. Parameters
  591. ----------
  592. size: int or sequence
  593. Desired output size of the crop.
  594. If size is an int instead of sequence like (h, w), a square crop (size, size) is made.
  595. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  596. scale: tuple of float
  597. scale range of the cropped image before resizing, relatively to the origin image.
  598. ratio: tuple of float
  599. aspect ratio range of the cropped image before resizing.
  600. interpolation: str
  601. Type of interpolation. Default is bilinear.
  602. Examples
  603. ----------
  604. With TensorLayer
  605. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  606. >>> transform = tl.vision.transforms.RandomResizedCrop(size = (100, 100), scale = (0.08, 1.0), ratio = (3./4.,4./3.), interpolation = 'bilinear')
  607. >>> image = transform(image)
  608. >>> print(image)
  609. >>> image shape : (100,100,3)
  610. """
  611. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation='bilinear'):
  612. self.size = size
  613. self.scale = scale
  614. self.ratio = ratio
  615. self.interpolation = interpolation
  616. def __call__(self, image):
  617. return F.random_resized_crop(image, self.size, self.scale, self.ratio, self.interpolation)
  618. class RandomFlipVertical(object):
  619. """Vertically flip the given image randomly with a given probability.
  620. Parameters
  621. ----------
  622. prob: float
  623. probability of the image being flipped. Default value is 0.5
  624. Examples
  625. ----------
  626. With TensorLayer
  627. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  628. >>> transform = tl.vision.transforms.RandomFlipVertical(prob = 0.5)
  629. >>> image = transform(image)
  630. >>> print(image)
  631. """
  632. def __init__(self, prob=0.5):
  633. self.prob = prob
  634. def __call__(self, image):
  635. return F.random_vflip(image, self.prob)
  636. class RandomFlipHorizontal(object):
  637. """Horizontally flip the given image randomly with a given probability.
  638. Parameters
  639. ----------
  640. prob: float
  641. probability of the image being flipped. Default value is 0.5
  642. Examples
  643. ----------
  644. With TensorLayer
  645. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  646. >>> transform = tl.vision.transforms.RandomFlipHorizontal(prob = 0.5)
  647. >>> image = transform(image)
  648. >>> print(image)
  649. """
  650. def __init__(self, prob=0.5):
  651. self.prob = prob
  652. def __call__(self, image):
  653. return F.random_hflip(image, self.prob)
  654. class RandomRotation(object):
  655. """Rotate the image by random angle.
  656. Parameters
  657. ----------
  658. degrees: number or sequnence
  659. Range of degrees to select from.
  660. If degrees is a number, the range of degrees will be (-degrees, +degrees).
  661. If degrees is a sequence, the range of degrees will (degrees[0], degrees[1]).
  662. interpolation: str
  663. Interpolation method. Default is 'bilinear'.
  664. expand: boolean
  665. If true, expands the output to make it large enough to hold the entire rotated image.
  666. If false or omitted, make the output image the same size as the input image.
  667. Note that the expand flag assumes rotation around the center and no translation.
  668. center: sequence or None
  669. Optional center of rotation, (x, y). Origin is the upper left corner.
  670. Default is the center of the image.
  671. fill: number or sequence
  672. Pixel fill value for the area outside the rotated image. Default is 0.
  673. Examples
  674. ----------
  675. With TensorLayer
  676. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  677. >>> transform = tl.vision.transforms.RandomRotation(degrees=30, interpolation='bilinear', expand=False, center=None, fill=0)
  678. >>> image = transform(image)
  679. >>> print(image)
  680. """
  681. def __init__(self, degrees, interpolation='bilinear', expand=False, center=None, fill=0):
  682. self.degrees = degrees
  683. self.interpolation = interpolation
  684. self.expand = expand
  685. self.center = center
  686. self.fill = fill
  687. def __call__(self, image):
  688. return F.random_rotation(image, self.degrees, self.interpolation, self.expand, self.center, self.fill)
  689. class RandomShear(object):
  690. """Shear the image by random angle.
  691. Parameters
  692. ----------
  693. degrees: number or sequnence
  694. Range of degrees to select from.
  695. If degrees is a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied.
  696. If shear is a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied.
  697. If shear is a sequence of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.
  698. interpolation: str
  699. Interpolation method. Default is 'bilinear'.
  700. fill: number or sequence
  701. Pixel fill value for the area outside the sheared image. Default is 0.
  702. Examples
  703. ----------
  704. With TensorLayer
  705. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  706. >>> transform = tl.vision.transforms.RandomShear(degrees=30, interpolation='bilinear', fill=0)
  707. >>> image = transform(image)
  708. >>> print(image)
  709. """
  710. def __init__(self, degrees, interpolation='bilinear', fill=0):
  711. self.degrees = degrees
  712. self.interpolation = interpolation
  713. self.fill = fill
  714. def __call__(self, image):
  715. return F.random_shear(image, self.degrees, self.interpolation, self.fill)
  716. class RandomShift(object):
  717. """Shift the image by random translations.
  718. Parameters
  719. ----------
  720. shift: list or tuple
  721. Maximum absolute fraction for horizontal and vertical translations.
  722. shift=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a.
  723. vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b.
  724. interpolation: str
  725. Interpolation method. Default is 'bilinear'.
  726. fill: number or sequence
  727. Pixel fill value for the area outside the sheared image. Default is 0.
  728. Examples
  729. ----------
  730. With TensorLayer
  731. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  732. >>> transform = tl.vision.transforms.RandomShift(shift=(0.2, 0.2), interpolation='bilinear', fill=0)
  733. >>> image = transform(image)
  734. >>> print(image)
  735. """
  736. def __init__(self, shift, interpolation='bilinear', fill=0):
  737. self.shift = shift
  738. self.interpolation = interpolation
  739. self.fill = fill
  740. def __call__(self, image):
  741. return F.random_shift(image, self.shift, self.interpolation, self.fill)
  742. class RandomZoom(object):
  743. """Zoom the image by random scale.
  744. Parameters
  745. ----------
  746. zoom: list or tuple
  747. Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b.
  748. interpolation: str
  749. Interpolation method. Default is 'bilinear'.
  750. fill: number or sequence
  751. Pixel fill value for the area outside the sheared image. Default is 0.
  752. Examples
  753. ----------
  754. With TensorLayer
  755. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  756. >>> transform = tl.vision.transforms.RandomZoom(zoom=(0.2, 0.5), interpolation='bilinear', fill=0)
  757. >>> image = transform(image)
  758. >>> print(image)
  759. """
  760. def __init__(self, zoom, interpolation='bilinear', fill=0):
  761. self.zoom = zoom
  762. self.interpolation = interpolation
  763. self.fill = fill
  764. def __call__(self, image):
  765. return F.random_zoom(image, self.zoom, self.interpolation, self.fill)
  766. class RandomAffine(object):
  767. """Random affine transformation of the image keeping center invariant.
  768. Parameters
  769. ----------
  770. degrees: number or sequnence
  771. Range of degrees to select from.
  772. If degrees is a number, the range of degrees will be (-degrees, +degrees).
  773. If degrees is a sequence, the range of degrees will (degrees[0], degrees[1]).
  774. Set to 0 to deactivate rotations.
  775. shift: sequence or None
  776. Maximum absolute fraction for horizontal and vertical translations.
  777. shift=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a.
  778. vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b.
  779. Will not shift by default.
  780. shear: number or sequnence or None
  781. Range of degrees to select from.
  782. If degrees is a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied.
  783. If shear is a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied.
  784. If shear is a sequence of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.
  785. Will not apply shear by default.
  786. zoom: sequence or None
  787. Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b.
  788. Will not zoom by default.
  789. interpolation: str
  790. Interpolation method. Default is 'bilinear'.
  791. fill: number or sequence
  792. Pixel fill value for the area outside the sheared image. Default is 0.
  793. Examples
  794. ----------
  795. With TensorLayer
  796. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  797. >>> transform = tl.vision.transforms.RandomAffine(degrees=30, shift=(0.2,0.2), zoom=(0.2, 0.5), shear=30, interpolation='bilinear', fill=0)
  798. >>> image = transform(image)
  799. >>> print(image)
  800. """
  801. def __init__(self, degrees, shift=None, zoom=None, shear=None, interpolation='bilinear', fill=0):
  802. if isinstance(degrees, numbers.Number):
  803. if degrees < 0:
  804. raise ValueError('If degrees is a single number, it must be positive.' 'But got {}.'.format(degrees))
  805. degrees = [-degrees, degrees]
  806. elif not (isinstance(degrees, (list, tuple)) and len(degrees) == 2):
  807. raise TypeError('If degrees is a list or tuple, it should be length of 2.' 'But got {}'.format(degrees))
  808. self.degrees = (float(x) for x in degrees)
  809. if shift is not None:
  810. if not (isinstance(shift, (list, tuple)) and len(shift) == 2):
  811. raise TypeError("shift should be a list or tuple of length 2." "But got {}.".format(shift))
  812. for s in shift:
  813. if not (0.0 <= s <= 1.0):
  814. raise ValueError('shift values should be between 0 and 1.' 'But got {}.'.format(shift))
  815. self.shift = shift
  816. if zoom is not None:
  817. if not (isinstance(zoom, (list, tuple)) and len(zoom) == 2):
  818. raise TypeError("zoom should be a list or tuple of length 2." "But got {}.".format(zoom))
  819. if not (0 <= zoom[0] <= zoom[1]):
  820. raise ValueError("zoom valuse should be positive, and zoom[1] should be less than zoom[0].")
  821. self.zoom = zoom
  822. if shear is not None:
  823. if isinstance(shear, numbers.Number):
  824. if shear < 0:
  825. raise ValueError("If shear is a single number, it must be positive.")
  826. shear = [-shear, shear]
  827. elif not (isinstance(shear, (list, tuple)) and len(shear) in (2, 4)):
  828. raise TypeError('shear should be a list or tuple of length (2, 4).')
  829. self.shear = (float(x) for x in shear)
  830. self.interpolation = interpolation
  831. if fill is None:
  832. fill = 0
  833. elif not isinstance(fill, (list, tuple, numbers.Number)):
  834. raise TypeError("Fill should be either a sequence or a number.")
  835. self.fill = fill
  836. def __call__(self, image):
  837. return F.random_affine(image, self.degrees, self.shift, self.zoom, self.shear, self.interpolation, self.fill)
  838. class ColorJitter(object):
  839. """Randomly change the brightness, contrast, saturation and hue of an image.
  840. Parameters
  841. ----------
  842. brightness: float or sequence
  843. Brightness adjustment factor (default=(1, 1)).
  844. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness_factor), 1+brightness_factor].
  845. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  846. contrast: float or sequence
  847. Contrast adjustment factor (default=(1, 1)).
  848. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast_factor), 1+contrast_factor].
  849. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  850. saturation: float or sequence
  851. Saturation adjustment factor (default=(1, 1)).
  852. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation_factor), 1+saturation_factor].
  853. If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.
  854. hue: float or sequence
  855. Hue adjustment factor (default=(0, 0)).
  856. If it is a float, the factor is uniformly chosen from the range [-hue_factor, hue_factor].
  857. If it is a sequence, it should be [min, max] for the range.Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.
  858. Examples
  859. ----------
  860. With TensorLayer
  861. >>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
  862. >>> transform = tl.vision.transforms.ColorJitter(brightness=(1,5), contrast=(1,5), saturation=(1,5), hue=(-0.2,0.2))
  863. >>> image = transform(image)
  864. >>> print(image)
  865. """
  866. def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
  867. self.brightness = self._check_input(brightness, 'brightness')
  868. self.contrast = self._check_input(contrast, 'contrast')
  869. self.saturation = self._check_input(saturation, 'saturation')
  870. self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5), clip_first_on_zero=False)
  871. def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
  872. if isinstance(value, numbers.Number):
  873. if value < 0:
  874. raise ValueError("If {} is a single number, it must be non negative.".format(name))
  875. value = [center - float(value), center + float(value)]
  876. if clip_first_on_zero:
  877. value[0] = max(value[0], 0.0)
  878. elif isinstance(value, (tuple, list)) and len(value) == 2:
  879. if not bound[0] <= value[0] <= value[1] <= bound[1]:
  880. raise ValueError("{} values should be between {}".format(name, bound))
  881. else:
  882. raise TypeError("{} should be a single number or a list/tuple with lenght 2.".format(name))
  883. if value[0] == value[1] == center:
  884. value = None
  885. return value
  886. @staticmethod
  887. def get_params(brightness, contrast, saturation, hue):
  888. fn_idx = np.random.permutation(np.arange(4))
  889. b = None if brightness is None else float(np.random.uniform(brightness[0], brightness[1]))
  890. c = None if contrast is None else float(np.random.uniform(contrast[0], contrast[1]))
  891. s = None if saturation is None else float(np.random.uniform(saturation[0], saturation[1]))
  892. h = None if hue is None else float(np.random.uniform(hue[0], hue[1]))
  893. return fn_idx, b, c, s, h
  894. def __call__(self, image):
  895. fn_idx, brightness_factor, contrast_factor, saturation_factor, hue_factor = \
  896. self.get_params(self.brightness, self.contrast, self.saturation, self.hue)
  897. for fn_id in fn_idx:
  898. if fn_id == 0 and brightness_factor is not None:
  899. image = F.adjust_brightness(image, brightness_factor)
  900. elif fn_id == 1 and contrast_factor is not None:
  901. image = F.adjust_contrast(image, contrast_factor)
  902. elif fn_id == 2 and saturation_factor is not None:
  903. image = F.adjust_saturation(image, saturation_factor)
  904. elif fn_id == 3 and hue_factor is not None:
  905. image = F.adjust_hue(image, hue_factor)
  906. return image

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.