@@ -76,30 +76,47 @@ def forward(self, feats, fiducial_points):
7676 Returns:
7777 Tensor: rectification feature of shape [K x C x output_size]
7878 """
79+ roi_feats = []
80+ scale_factor = 4
81+
7982 # only using 4x feature
80- x = self .relu (self .bn (self .conv (feats [0 ])))
83+ feats = self .relu (self .bn (self .conv (feats [0 ])))
84+ _ , _ , height , width = feats .size ()
8185
82- roi_feats = []
83- for batch_id in range (len (x )):
84- batch_C_prime = fiducial_points [batch_id ]
85- if len (batch_C_prime ) == 0 :
86+ for feat , points in zip (feats , fiducial_points ):
87+ if len (points ) == 0 :
8688 continue
87- # B x point_num x 2
88- batch_C_prime = torch .Tensor (batch_C_prime ).cuda (device = x .device )
89- # B x C x H x W
90- batch_I = x [batch_id ].unsqueeze (0 ).expand (len (batch_C_prime ), - 1 , - 1 , - 1 )
91- # B x N (= output_size[0] x output_size[1]) x 2
92- build_P_prime = self .GridGenerator .build_P_prime (batch_C_prime )
93- # B x output_size x 2
94- build_P_prime_reshape = build_P_prime .reshape ([build_P_prime .size (0 ),
95- self .output_size [0 ],
96- self .output_size [1 ],
97- 2 ])
98- # B x C x output_size
99- batch_I_r = F .grid_sample (batch_I ,
100- build_P_prime_reshape ,
101- padding_mode = 'border' )
102- roi_feats .append (batch_I_r )
89+ points = torch .Tensor (points ).cuda (device = feat .device )
90+ points = points / scale_factor
91+ for point in points :
92+ # Clip points
93+ point [:, 0 ] = torch .clip (point [:, 0 ], 0 , width )
94+ point [:, 1 ] = torch .clip (point [:, 1 ], 0 , height )
95+
96+ # Caculate points boundary
97+ x1 = int (torch .min (point [:, 0 ]))
98+ x2 = int (torch .max (point [:, 0 ])) + 1
99+ y1 = int (torch .min (point [:, 1 ]))
100+ y2 = int (torch .max (point [:, 1 ])) + 1
101+
102+ # Normalize points for tps
103+ point [:, 0 ] = 2 * (point [:, 0 ] - x1 ) / (x2 - x1 ) - 1
104+ point [:, 1 ] = 2 * (point [:, 1 ] - y1 ) / (y2 - y1 ) - 1
105+
106+ # B x N (= output_size[0] x output_size[1]) x 2
107+ build_P_prime = self .GridGenerator .build_P_prime (point .unsqueeze (0 ))
108+ # B x output_size x 2
109+ build_P_prime_reshape = build_P_prime .reshape ([build_P_prime .size (0 ),
110+ self .output_size [0 ],
111+ self .output_size [1 ],
112+ 2 ])
113+ # Crop feature according to points boundary
114+ crop_feat = feat [:, y1 :y2 , x1 :x2 ].unsqueeze (0 )
115+ # B x C x output_size
116+ batch_I_r = F .grid_sample (crop_feat ,
117+ build_P_prime_reshape ,
118+ padding_mode = 'border' )
119+ roi_feats .append (batch_I_r )
103120 roi_feats = torch .cat (roi_feats )
104121 return roi_feats
105122
@@ -151,23 +168,19 @@ def get_fiducial_points(self, imgs, polys):
151168 if len (batch_bboxes ) > 0 :
152169 batch_fiducial_points = np .stack (batch_fiducial_points , axis = 0 )
153170
154- # Normalize fiducial points
155- batch_fiducial_points [:, :, 0 ] = (2 * batch_fiducial_points [:, :, 0 ] - width ) / width
156- batch_fiducial_points [:, :, 1 ] = (2 * batch_fiducial_points [:, :, 1 ] - height ) / height
157-
158171 fiducial_points .append (batch_fiducial_points )
159172 return fiducial_points
160173
161- def normalize_fiducial_points (self , imgs , img_metas , fiducial_points ):
162- """ Normalize the fiducial points coordinates to [0,1] .
174+ def rescale_fiducial_points (self , imgs , img_metas , fiducial_points ):
175+ """ Rescale the fiducial points coordinates.
163176
164177 Args:
165178 imgs (Tensor): input image.
166179 img_metas (dict): image meta-info.
167180 fiducial_points list(np.array): tps fiducial points.
168181
169182 Returns:
170- list(np.array): normalized points
183+ list(np.array): Rescaled points
171184 """
172185 normalized_fiducial_points = []
173186 for img , img_meta , point in zip (imgs , img_metas , fiducial_points ):
@@ -180,10 +193,7 @@ def normalize_fiducial_points(self, imgs, img_metas, fiducial_points):
180193 point [:, :, 0 ] = point [:, :, 0 ] * scale_factor [0 ]
181194 point [:, :, 1 ] = point [:, :, 1 ] * scale_factor [1 ]
182195
183- # Normalize
184- point [:, :, 0 ] = (2 * point [:, :, 0 ] - width ) / width
185- point [:, :, 1 ] = (2 * point [:, :, 1 ] - height ) / height
186-
196+ # Change points order
187197 point_num = int (point .shape [1 ] / 2 )
188198 point [:, point_num :, :] = point [:, point_num :, :][:, ::- 1 , :]
189199 normalized_fiducial_points .append (point )
0 commit comments