crowdcount.transforms

Transforms are common image and density map transformations. They can be chained together using SingleCompose (for single image or density map) ComplexCompose (for both image and density map),

Compose functions

class crowdcount.transforms.SingleCompose(cc_transforms)[source]

Compose several transforms witch only transform single input (image or density map)

Parameters

(list of Transform objects which transform one object (transforms) – [ResizeShrink, LabelEnlarge]): list of transforms to Compose

Example

>>> import crowdcount.transforms as cc_transforms
>>> cc_transforms.SingleCompose([
>>>     ResizeShrink(8),
>>>     LabelEnlarge(10),
>>> ])
class crowdcount.transforms.ComplexCompose(cc_transforms)[source]

Compose several transforms witch transform both of image and density map

Parameters

(list of Transform objects which transform two objects (transforms) – [TransposeFlip, RandomCrop, Scale]): list of transforms to Compose

Example

>>> import crowdcount.transforms as cc_transforms
>>> cc_transforms.ComplexCompose([
>>>     TransposeFlip(),
>>>     RandomCrop([512, 512]),
>>>     Scale([512, 512]),
>>> ])

SingleCompose

class crowdcount.transforms.ResizeShrink(scale_factor)[source]

Reduce the density map scale_factor times (to suit the output be pooled)

Parameters

scale_factor (int) – Desired reduction factor. The output size will be divided by scale_factor. If the scale_factor is 8 and the size of input is (20, 10), the output size will be (20 // 8, 10 // 8) = (2, 1) to match the output image which be pooled.

Example

>>> import crowdcount.transforms as cc_transforms
>>> import numpy as np
>>> resize_shrink = cc_transforms.ResizeShrink(8)
>>> density_map = np.random.rand(20, 10)
>>> density_map.shape
(20, 10)
>>> resize_shrink(density_map)
array([[52.175777], [46.061344]], dtype=float32)
__call__(den)[source]
Parameters

map (density) – density map to be shrunk

Returns

Rescaled image

Return type

numpy.ndarray

class crowdcount.transforms.LabelEnlarge(number=100)[source]

Training trick from the “C^3 Framework…” paper. They find neural network could get faster convergence and lower estimation error when the density map dots a large integer value

Parameters

number (int) – the magnification of density map. default is 100.

Example

>>> import crowdcount.transforms as cc_transforms
>>> import numpy as np
>>> label_enlarge = cc_transforms.LabelEnlarge(100)
>>> density_map = np.random.rand(4, 4)
array([[0.62494003, 0.35120895, 0.21002026, 0.52596833],
       [0.45540917, 0.41721004, 0.45287173, 0.35665398],
       [0.63187118, 0.25588367, 0.44660365, 0.0367272 ],
       [0.86808967, 0.11982928, 0.44544907, 0.81409479]])
>>> label_enlarge(density_map)
array([[62.49400293, 35.12089511, 21.00202647, 52.59683332],
       [45.54091666, 41.72100355, 45.28717272, 35.66539753],
       [63.18711774, 25.58836725, 44.66036518,  3.67272008],
       [86.8089669 , 11.98292805, 44.54490721, 81.40947874]])
__call__(den)[source]
Parameters

map (density) – density map to be enlarged

Returns

enlarged density map

Return type

numpy.ndarray

ComplexCompose

class crowdcount.transforms.TransposeFlip[source]

Randomly flip both of the image and density map left and right

Example

>>> import crowdcount.transforms as cc_transforms
>>> import numpy as np
>>> img = np.randn(4, 4)
>>> density_map = np.randn(4, 4)
>>> transpose_flip = cc_transforms.TransposeFlip()
>>> img, density_map = transpose_flip(img, density_map)
__call__(img, den)[source]
Parameters
  • image (PIL Image or numpy.ndarray) – image to be flipped

  • map (density) – density map to be flipped

Returns

(PIL Image, numpy.ndarray)

class crowdcount.transforms.RandomCrop(size)[source]

In order to use multi-batch training to irregular datasets (like ShanghaiTech Part A where images have different shape), This function random crops both of image and density map with input size.

Parameters
  • size (sequence or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w),

  • square crop (a) –

Example

>>> import crowdcount.transforms as cc_transforms
>>> import numpy as np
>>> img = np.randn(4, 4)
>>> density_map = np.randn(4, 4)
>>> random_crop = cc_transforms.RandomCrop([2, 2])
>>> img, density_map = random_crop(img, density_map)
__call__(img, den)[source]
Parameters
  • image (PIL Image or numpy.ndarray) – image to be cropped

  • map (density) – density map to be cropped

Returns

(PIL Image, numpy.ndarray)

class crowdcount.transforms.Scale(size, interpolation=2)[source]

In order to use multi-batch training to irregular datasets (like ShanghaiTech Part A where images have different shape), This function resize both of image and density map with input size.

Parameters
  • size (sequence or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w),

  • square crop (a) –

Example

>>> import crowdcount.transforms as cc_transforms
>>> import numpy as np
>>> img = np.randn(4, 4)
>>> density_map = np.randn(4, 4)
>>> scale = cc_transforms.Scale([2, 2])
>>> img, density_map = scale(img, density_map)
__call__(img, den)[source]
Parameters
  • image (PIL Image or numpy.ndarray) – image to be cropped

  • map (density) – density map to be cropped

Returns

(PIL Image, numpy.ndarray)