@@ -114,54 +114,40 @@ def section_reward(label: dict, pred: dict, alpha_p=1.0, alpha_f=1.0, alpha_t=1.
114
114
115
115
return reward
116
116
117
- def sort_predictions (image_data : List [dict ], predictions : List [dict ], draw = False ) -> List [dict ]:
117
+ def sort_predictions (labels : List [dict ], predictions : List [dict ], draw = False ) -> List [dict ]:
118
118
"""
119
119
Sort the predictions to match the order of the ground truth data using the Hungarian algorithm.
120
120
121
121
Args:
122
- - image_data (list): The ground truth data for the image.
122
+ - labels (list): The ground truth data for the image.
123
123
- predictions (list): The predicted data for the image.
124
124
125
125
Returns:
126
126
- list: The sorted predictions.
127
127
"""
128
128
129
129
# First, make sure that the predictions is at least as long as the image data
130
- predictions += [{}] * (len (image_data ) - len (predictions ))
131
- r = torch .zeros ((len (image_data ), len (predictions )))
130
+ predictions += [{}] * (len (labels ) - len (predictions ))
131
+ r = torch .zeros ((len (labels ), len (predictions )))
132
132
for i in range (r .shape [0 ]):
133
133
for j in range (r .shape [1 ]):
134
- r [i ,j ] = section_reward (image_data [i ], predictions [j ])['total' ]
134
+ r [i ,j ] = section_reward (labels [i ], predictions [j ])['total' ]
135
135
136
136
# Use the Hungarian algorithm to find the best assignment
137
137
row_indices , col_indices = linear_sum_assignment (r , maximize = True )
138
138
139
- if draw :
140
- fig = px .imshow (r .detach ().numpy (),
141
- color_continuous_scale = 'Blues' ,
142
- title = f'Optimal Assignment (Avg. Reward: { r [row_indices , col_indices ].mean ():.3f} )' ,
143
- width = 600 , height = 600
144
- )
145
- fig .update_layout (coloraxis_showscale = False )
146
- fig .update_yaxes (title_text = 'Ground Truth' )
147
- fig .update_xaxes (title_text = 'Predictions' )
148
-
149
- for i , j in zip (row_indices , col_indices ):
150
- fig .add_annotation (x = j , y = i , text = '+' , showarrow = False , font = dict (color = 'red' , size = 16 ))
151
- fig .show ()
152
-
153
139
sorted_predictions = [predictions [i ] for i in col_indices ]
154
140
155
141
return sorted_predictions
156
142
157
143
158
- def reward (self , image_data : List [dict ], response : OCRSynapse ) -> float :
144
+ def reward (self , labels : List [dict ], response : OCRSynapse ) -> float :
159
145
"""
160
146
Reward the miner response to the OCR request. This method returns a reward
161
147
value for the miner, which is used to update the miner's score.
162
148
163
149
Args:
164
- - image (List[dict]): The true data underlying the image sent to the miner.
150
+ - labels (List[dict]): The true data underlying the image sent to the miner.
165
151
- response (OCRSynapse): Response from the miner.
166
152
167
153
The expected fields in each section of the response are:
@@ -177,8 +163,8 @@ def reward(self, image_data: List[dict], response: OCRSynapse) -> float:
177
163
return 0.0
178
164
179
165
# Sort the predictions to match the order of the ground truth data as best as possible
180
- predictions = sort_predictions (image_data , predictions )
181
-
166
+ predictions = sort_predictions (labels , predictions )
167
+
182
168
alpha_p = self .config .neuron .alpha_position
183
169
alpha_t = self .config .neuron .alpha_text
184
170
alpha_f = self .config .neuron .alpha_font
@@ -187,8 +173,8 @@ def reward(self, image_data: List[dict], response: OCRSynapse) -> float:
187
173
188
174
# Take mean score over all sections in document (note that we don't penalize extra sections)
189
175
section_rewards = [
190
- section_reward (label , pred , verbose = True , alpha_f = alpha_f , alpha_p = alpha_p , alpha_t = alpha_t )
191
- for label , pred in zip (image_data , predictions )
176
+ section_reward (label , pred , verbose = True , alpha_f = alpha_f , alpha_p = alpha_p , alpha_t = alpha_t )
177
+ for label , pred in zip (labels , predictions )
192
178
]
193
179
prediction_reward = torch .mean (torch .FloatTensor ([reward ['total' ] for reward in section_rewards ]))
194
180
@@ -200,7 +186,7 @@ def reward(self, image_data: List[dict], response: OCRSynapse) -> float:
200
186
201
187
def get_rewards (
202
188
self ,
203
- image_data : List [dict ],
189
+ labels : List [dict ],
204
190
responses : List [OCRSynapse ],
205
191
) -> torch .FloatTensor :
206
192
"""
@@ -215,5 +201,5 @@ def get_rewards(
215
201
"""
216
202
# Get all the reward results by iteratively calling your reward() function.
217
203
return torch .FloatTensor (
218
- [reward (self , image_data , response ) for response in responses ]
204
+ [reward (self , labels , response ) for response in responses ]
219
205
).to (self .device )
0 commit comments