File size: 4,117 Bytes
436b829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import cv2
import numpy as np
from ppd.utils.logger import Log
import torch
import torch.nn.functional as F
from omegaconf.listconfig import ListConfig
EPS = 1e-4

class PrepareForNet(object):
    """Prepare sample for usage as network input.
    """

    def __init__(self):
        pass

    def __str__(self):
        return "PrepareForNet"

    def __repr__(self):
        return "PrepareForNet"

    def __call__(self, sample):
        image = np.transpose(sample["image"], (2, 0, 1))
        sample["image"] = np.ascontiguousarray(image).astype(np.float32)

        if "mask" in sample:
            sample["mask"] = sample["mask"].astype(np.uint8)
            sample["mask"] = np.ascontiguousarray(sample["mask"])[None]

        if "depth" in sample:
            depth = sample["depth"].astype(np.float32)
            sample["depth"] = np.ascontiguousarray(depth)[None]

        return sample


def cv2_resize(image, size, interpolation=cv2.INTER_LINEAR):
    return cv2.resize(image, size, interpolation=interpolation)[None]



class Resize(object):
    """Resize sample to given size (width, height).
    """
    def __init__(
        self,
        width=None,
        height=None,
        # image_interpolation_method=cv2.INTER_AREA,
        image_interpolation_method = cv2.INTER_LINEAR,
    ):
        self.width = width
        self.height = height
        self.__image_interpolation_method = image_interpolation_method

    def __call__(self, sample):
        width, height = self.width, self.height
        if width == sample['image'].shape[1] and height == sample['image'].shape[0]:
            return sample
        Log.debug(
            'Resize: {} -> {}'.format(sample["image"].shape, (height, width)))
        # resize sample
        ori_height, ori_width = sample['image'].shape[:2]
        sample["image"] = cv2.resize(
            sample["image"],
            (width, height),
            interpolation=self.__image_interpolation_method,
        )
        if "depth" in sample:
            sample["depth"] = cv2.resize(
                sample["depth"], 
                (width, height), 
                interpolation=cv2.INTER_NEAREST)

        if "mask" in sample:
            sample["mask"] = cv2.resize(
                sample["mask"].astype(np.float32), 
                (width, height), 
                interpolation=cv2.INTER_NEAREST)
        return sample

class Resize_4K_Crop(object):
    """Resize sample to given size (width, height).
    """

    def __init__(
        self,
        width=None,
        height=None,
        crop_type='random',
        image_interpolation_method=cv2.INTER_AREA,
    ):
        self.width = width
        self.height = height
        self.crop_type = crop_type
        self.__image_interpolation_method = image_interpolation_method

    def __call__(self, sample):
        width, height = 1920, 1024
        sample["image"] = cv2.resize(
            sample["image"],
            (width, height),
            interpolation=self.__image_interpolation_method,
        )

        # crop sample
        crop_h = self.height
        crop_w = self.width
        if self.crop_type == 'random':
            # random crop
            top = np.random.randint(0, height - crop_h + 1)
            left = np.random.randint(0, width - crop_w + 1)
        else:
            # center crop
            top = (height - crop_h) // 2
            left = (width - crop_w) // 2
        sample["image"] = sample["image"][top:top+crop_h, left:left+crop_w]

        if "depth" in sample:
            sample["depth"] = cv2.resize(
                sample["depth"], (width, height), 
                interpolation=cv2.INTER_NEAREST
            )
            # crop sample
            sample["depth"] = sample["depth"][top:top+crop_h, left:left+crop_w]
        if "mask" in sample:
            sample["mask"] = cv2.resize(
                sample["mask"].astype(np.float32),
                (width, height),
                interpolation=cv2.INTER_NEAREST,
            )
            sample["mask"] = sample["mask"][top:top+crop_h, left:left+crop_w]
        return sample