khang119966 commited on
Commit
23191fb
·
verified ·
1 Parent(s): c3d33a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -316,31 +316,50 @@ def visualize_attention_hiddenstate(attention_tensor, head=None, start_img_token
316
 
317
  return heat_maps, top_5_tokens
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  def adjust_overlay(overlay, text_img):
320
  h_o, w_o = overlay.shape[:2]
321
  h_t, w_t = text_img.shape[:2]
322
 
323
- if h_o > w_o: # Overlay ảnh đứng
324
- # Resize overlay sao cho h = h_t, giữ nguyên tỷ lệ
325
- new_h = h_t
326
- new_w = int(w_o * (new_h / h_o))
327
- overlay_resized = cv2.resize(overlay, (new_w, new_h))
328
- else: # Overlay là ảnh ngang
329
- # Giữ nguyên overlay, nhưng nếu h < h_t thì thêm padding trắng
330
- overlay_resized = overlay.copy()
331
 
332
- # Thêm padding trắng nếu overlay h < h_t
333
  if overlay_resized.shape[0] < h_t:
334
  pad_h = h_t - overlay_resized.shape[0]
335
  padding = np.ones((pad_h, overlay_resized.shape[1], 3), dtype=np.uint8) * 255
336
- overlay_resized = np.vstack((overlay_resized, padding)) # Padding vào dưới
337
-
338
- # Đảm bảo overlay có cùng chiều cao với text_img
339
- if overlay_resized.shape[0] != h_t:
340
- overlay_resized = cv2.resize(overlay_resized, (overlay_resized.shape[1], h_t))
341
 
342
  return overlay_resized
343
 
 
344
  def extract_next_token_table_data(model, tokenizer, response, index_focus):
345
  next_token_table = []
346
  for layer_index in range(len(response.hidden_states[index_focus])):
 
316
 
317
  return heat_maps, top_5_tokens
318
 
319
+ # def adjust_overlay(overlay, text_img):
320
+ # h_o, w_o = overlay.shape[:2]
321
+ # h_t, w_t = text_img.shape[:2]
322
+
323
+ # if h_o > w_o: # Overlay là ảnh đứng
324
+ # # Resize overlay sao cho h = h_t, giữ nguyên tỷ lệ
325
+ # new_h = h_t
326
+ # new_w = int(w_o * (new_h / h_o))
327
+ # overlay_resized = cv2.resize(overlay, (new_w, new_h))
328
+ # else: # Overlay là ảnh ngang
329
+ # # Giữ nguyên overlay, nhưng nếu h < h_t thì thêm padding trắng
330
+ # overlay_resized = overlay.copy()
331
+
332
+ # # Thêm padding trắng nếu overlay có h < h_t
333
+ # if overlay_resized.shape[0] < h_t:
334
+ # pad_h = h_t - overlay_resized.shape[0]
335
+ # padding = np.ones((pad_h, overlay_resized.shape[1], 3), dtype=np.uint8) * 255
336
+ # overlay_resized = np.vstack((overlay_resized, padding)) # Padding vào dưới
337
+
338
+ # # Đảm bảo overlay có cùng chiều cao với text_img
339
+ # if overlay_resized.shape[0] != h_t:
340
+ # overlay_resized = cv2.resize(overlay_resized, (overlay_resized.shape[1], h_t))
341
+
342
+ # return overlay_resized
343
+
344
  def adjust_overlay(overlay, text_img):
345
  h_o, w_o = overlay.shape[:2]
346
  h_t, w_t = text_img.shape[:2]
347
 
348
+ # Resize overlay sao cho chiều ngang <= 500, chiều dọc <= 1000 (giữ nguyên tỉ lệ)
349
+ scale = min(500 / w_o, 1000 / h_o, 1.0) # không phóng to quá kích thước gốc
350
+ new_w = int(w_o * scale)
351
+ new_h = int(h_o * scale)
352
+ overlay_resized = cv2.resize(overlay, (new_w, new_h))
 
 
 
353
 
354
+ # Nếu overlay nhỏ hơn chiều cao của text_img thì thêm padding trắng bên dưới
355
  if overlay_resized.shape[0] < h_t:
356
  pad_h = h_t - overlay_resized.shape[0]
357
  padding = np.ones((pad_h, overlay_resized.shape[1], 3), dtype=np.uint8) * 255
358
+ overlay_resized = np.vstack((overlay_resized, padding))
 
 
 
 
359
 
360
  return overlay_resized
361
 
362
+
363
  def extract_next_token_table_data(model, tokenizer, response, index_focus):
364
  next_token_table = []
365
  for layer_index in range(len(response.hidden_states[index_focus])):