• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zcemycl / TF2DeepFloorplan / 5149989027

pending completion
5149989027

push

github

web-flow
Merge branch 'main' into staging

684 of 1187 relevant lines covered (57.62%)

0.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.12
/src/dfp/utils/util.py
1
import cv2
1✔
2
import numpy as np
1✔
3
import tensorflow as tf
1✔
4
from scipy import ndimage
1✔
5

6

7
def fast_hist(im: np.ndarray, gt: np.ndarray, n: int = 9) -> np.ndarray:
1✔
8
    """
9
    n is num_of_classes
10
    """
11
    k = (gt >= 0) & (gt < n)
1✔
12
    return np.bincount(
1✔
13
        n * gt[k].astype(int) + im[k], minlength=n**2
14
    ).reshape(n, n)
15

16

17
def flood_fill(test_array: np.ndarray, h_max: int = 255) -> np.ndarray:
1✔
18
    """
19
    fill in the hole
20
    """
21
    test_array = test_array.squeeze()
1✔
22
    input_array = np.copy(test_array)
1✔
23
    el = ndimage.generate_binary_structure(2, 2).astype(int)
1✔
24
    inside_mask = ndimage.binary_erosion(~np.isnan(input_array), structure=el)
1✔
25
    output_array = np.copy(input_array)
1✔
26
    output_array[inside_mask] = h_max
1✔
27
    output_old_array = np.copy(input_array)
1✔
28
    output_old_array.fill(0)
1✔
29
    el = ndimage.generate_binary_structure(2, 1).astype(int)
1✔
30
    while not np.array_equal(output_old_array, output_array):
1✔
31
        output_old_array = np.copy(output_array)
1✔
32
        output_array = np.maximum(
1✔
33
            input_array,
34
            ndimage.grey_erosion(output_array, size=(3, 3), footprint=el),
35
        )
36
    return output_array
1✔
37

38

39
def fill_break_line(cw_mask: np.ndarray) -> np.ndarray:
1✔
40
    broken_line_h = np.array(
1✔
41
        [
42
            [0, 0, 0, 0, 0],
43
            [0, 0, 0, 0, 0],
44
            [1, 0, 0, 0, 1],
45
            [0, 0, 0, 0, 0],
46
            [0, 0, 0, 0, 0],
47
        ],
48
        dtype=np.uint8,
49
    )
50
    broken_line_h2 = np.array(
1✔
51
        [
52
            [0, 0, 0, 0, 0],
53
            [0, 0, 0, 0, 0],
54
            [1, 1, 0, 1, 1],
55
            [0, 0, 0, 0, 0],
56
            [0, 0, 0, 0, 0],
57
        ],
58
        dtype=np.uint8,
59
    )
60
    broken_line_v = np.transpose(broken_line_h)
1✔
61
    broken_line_v2 = np.transpose(broken_line_h2)
1✔
62
    cw_mask = cv2.morphologyEx(cw_mask, cv2.MORPH_CLOSE, broken_line_h)
1✔
63
    cw_mask = cv2.morphologyEx(cw_mask, cv2.MORPH_CLOSE, broken_line_v)
1✔
64
    cw_mask = cv2.morphologyEx(cw_mask, cv2.MORPH_CLOSE, broken_line_h2)
1✔
65
    cw_mask = cv2.morphologyEx(cw_mask, cv2.MORPH_CLOSE, broken_line_v2)
1✔
66

67
    return cw_mask
1✔
68

69

70
def refine_room_region(cw_mask: np.ndarray, rm_ind: np.ndarray) -> np.ndarray:
1✔
71
    label_rm, num_label = ndimage.label((1 - cw_mask))
1✔
72
    new_rm_ind = np.zeros(rm_ind.shape)
1✔
73
    for j in range(1, num_label + 1):
1✔
74
        mask = (label_rm == j).astype(np.uint8)
1✔
75
        ys, xs, _ = np.where(mask != 0)
1✔
76
        area = (np.amax(xs) - np.amin(xs)) * (np.amax(ys) - np.amin(ys))
1✔
77
        if area < 100:
1✔
78
            continue
×
79
        else:
80
            room_types, type_counts = np.unique(
1✔
81
                mask * rm_ind, return_counts=True
82
            )
83
            if len(room_types) > 1:
1✔
84
                room_types = room_types[1:]
1✔
85
                # ignore background type which is zero
86
                type_counts = type_counts[1:]
1✔
87
                # ignore background count
88
            new_rm_ind += mask * room_types[np.argmax(type_counts)]
1✔
89

90
    return new_rm_ind
1✔
91

92

93
def print_model_weights_sparsity(model):
1✔
94
    for layer in model.layers:
×
95
        if isinstance(layer, tf.keras.layers.Wrapper):
×
96
            weights = layer.trainable_weights
×
97
        else:
98
            weights = layer.weights
×
99
        for weight in weights:
×
100
            if "kernel" not in weight.name or "centroid" in weight.name:
×
101
                continue
×
102
            weight_size = weight.numpy().size
×
103
            zero_num = np.count_nonzero(weight == 0)
×
104
            print(
×
105
                f"{weight.name}: {zero_num/weight_size:.2%} sparsity ",
106
                f"({zero_num}/{weight_size})",
107
            )
108

109

110
def print_model_weight_clusters(model):
1✔
111
    for layer in model.layers:
×
112
        if isinstance(layer, tf.keras.layers.Wrapper):
×
113
            weights = layer.trainable_weights
×
114
        else:
115
            weights = layer.weights
×
116
        for weight in weights:
×
117
            # ignore auxiliary quantization weights
118
            if "quantize_layer" in weight.name:
×
119
                continue
×
120
            if "kernel" in weight.name:
×
121
                unique_count = len(np.unique(weight))
×
122
                print(f"{layer.name}/{weight.name}: {unique_count} clusters ")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc