File size: 3,104 Bytes
48fa639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import numpy as np
import torch


def blend_image_segmentation(img, seg, mode, image_size=224):


    if mode in {'blur_highlight', 'blur3_highlight', 'blur3_highlight01', 'blur_highlight_random', 'crop'}:
        if isinstance(img, np.ndarray):
            img = torch.from_numpy(img)

        if isinstance(seg, np.ndarray):
            seg = torch.from_numpy(seg)            

    if mode == 'overlay':
        out = img * seg
        out = [out.astype('float32')]
    elif mode == 'highlight':
        out = img * seg[None, :, :] * 0.85 + 0.15 * img
        out = [out.astype('float32')]
    elif mode == 'highlight2':
        img = img / 2
        out = (img+0.1) * seg[None, :, :] + 0.3 * img
        out = [out.astype('float32')]
    elif mode == 'blur_highlight':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=1, bg_fac=0.5).numpy()[0] - 0.01]
    elif mode == 'blur3_highlight':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=3, bg_fac=0.5).numpy()[0] - 0.01]
    elif mode == 'blur3_highlight01':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=3, bg_fac=0.1).numpy()[0] - 0.01]                
    elif mode == 'blur_highlight_random':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=0 + torch.randint(0, 3, (1,)).item(), bg_fac=0.1 + 0.8*torch.rand(1).item()).numpy()[0] - 0.01]               
    elif mode == 'crop':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=1, center_context=0.1, image_size=image_size)[0].numpy()]  
    elif mode == 'crop_blur_highlight':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=3, center_context=0.1, bg_fac=0.1, image_size=image_size)[0].numpy()]  
    elif mode == 'crop_blur_highlight352':
        from evaluation_utils import img_preprocess
        out  = [img_preprocess((None, [img], [seg]), blur=3, center_context=0.1, bg_fac=0.1, image_size=352)[0].numpy()]          
    elif mode == 'shape':
        out = [np.stack([seg[:, :]]*3).astype('float32')]
    elif mode == 'concat':
        out = [np.concatenate([img, seg[None, :, :]]).astype('float32')]
    elif mode == 'image_only':
        out = [img.astype('float32')]
    elif mode == 'image_black':
        out = [img.astype('float32')*0]        
    elif mode is None:
        out = [img.astype('float32')]
    elif mode == 'separate':
        out = [img.astype('float32'), seg.astype('int64')]
    elif mode == 'separate_img_black':
        out = [img.astype('float32')*0, seg.astype('int64')]        
    elif mode == 'separate_seg_ones':
        out = [img.astype('float32'), np.ones_like(seg).astype('int64')]                
    elif mode == 'separate_both_black':
        out = [img.astype('float32')*0, seg.astype('int64')*0]        
    else:
        raise ValueError(f'invalid mode: {mode}')

    return out