-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenormalization.py
80 lines (61 loc) · 3 KB
/
denormalization.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 11 16:22:15 2019
@author: Amit
"""
def denormalize_coordinates(image_dict,normalized_coords,grid_row_no,grid_col_no,total_grid_rows,total_grid_cols):
'''
b.b => bounding box
The output of feedforward would be of the dimensions:
# of gridrows x # of gridcols x v
v = prob. of obj + (4 b.b params) + # of classes
This function returns the coordinates for the center of a b.b
The inputs for this function are:
1. image_dict => represents the size of the image
2. normalized_coords {x,y} => normalized coordinates between (0,1)
3. grid_row_no => The row to which this particular grid cell belongs to
4. grid_col_no => The col to which this particular grid cell belongs to
5. total_grid_rows => Total grid cells in a row
6. total_grid_cols => Total grid cells in a col
'''
cell_height = image_dict["height"] / total_grid_rows
cell_width = image_dict["width"] / total_grid_cols
grid_cell_topleft = {}
grid_cell_topleft["y"] = grid_row_no*cell_height
grid_cell_topleft["x"] = grid_col_no*cell_width
denormalized_coord = {}
denormalized_coord["x"] = int(grid_cell_topleft["x"] + (cell_width*normalized_coords["x"]))
denormalized_coord["y"] = int(grid_cell_topleft["y"] + (cell_height*normalized_coords["y"]))
return denormalized_coord
def denormalize_box_dimension(image_dict,normalized_dimensions):
'''
This function returns the width and height of the b.b
The inputs for this function are:
1. image_dict => represents the size of the image
2. normalized_box_dimensions {width,height} => normalized box dimensions between (0,1)
'''
denormalized_dimensions = {}
# box_width/image_width
denormalized_dimensions["width"] = int(normalized_dimensions["width"]*image_dict["width"])
denormalized_dimensions["height"] = int(normalized_dimensions["height"]*image_dict["height"])
return denormalized_dimensions
def getDenormalizedBoxParams(image_dict,normalized_dimensions,normalized_coords,grid_row_no,grid_col_no,total_grid_rows,total_grid_cols):
"""
Returns the xmin,ymin,xmax,ymax
"""
box_dimensions = denormalize_box_dimension(image_dict,normalized_dimensions)
coordinates = denormalize_coordinates(image_dict,normalized_coords,grid_row_no,grid_col_no,total_grid_rows,total_grid_cols)
xmin = coordinates["x"] - box_dimensions["width"]/2
xmax = coordinates["x"] + box_dimensions["width"]/2
ymin = coordinates["y"] - box_dimensions["height"]/2
ymax = coordinates["y"] + box_dimensions["height"]/2
box_dict = {"center_x":coordinates["x"],
"center_y":coordinates["y"],
"box_h":box_dimensions["height"],
"box_w":box_dimensions["width"],
"xmin":xmin,
"xmax":xmax,
"ymin":ymin,
"ymax":ymax
}
return box_dict