@@ -648,37 +648,18 @@ fn image_tokens(
648
648
let max_longest_edge_for_image_resize = config. get_max_longest_edge_for_image_resize ( ) ;
649
649
let max_image_size = config. get_max_image_size ( ) ;
650
650
651
- // resize image to max_longest_edge_for_image_resize and keep aspect ratio
652
651
let ( height, width) = {
653
- let aspect_ratio = height as f32 / width as f32 ;
654
- if height > width {
655
- (
656
- max_longest_edge_for_image_resize,
657
- ( max_longest_edge_for_image_resize as f32 / aspect_ratio) as usize ,
658
- )
659
- } else {
660
- (
661
- ( max_longest_edge_for_image_resize as f32 * aspect_ratio) as usize ,
662
- max_longest_edge_for_image_resize,
663
- )
664
- }
665
- } ;
652
+ let h = height as f32 ;
653
+ let w = width as f32 ;
666
654
667
- let ( height, width) = {
668
- let aspect_ratio = height as f32 / width as f32 ;
669
- if height >= width && height > max_image_size {
670
- (
671
- max_image_size,
672
- ( max_image_size as f32 / aspect_ratio) as usize ,
673
- )
674
- } else if width > height && width > max_image_size {
675
- (
676
- ( max_image_size as f32 * aspect_ratio) as usize ,
677
- max_image_size,
678
- )
679
- } else {
680
- ( height, width)
681
- }
655
+ // First resize to max_longest_edge (always scale to this size)
656
+ let scale1 = max_longest_edge_for_image_resize as f32 / h. max ( w) ;
657
+ let ( h, w) = ( h * scale1, w * scale1) ;
658
+
659
+ // Ensure we dont exceed max_size (only scale down)
660
+ let scale2 = ( max_image_size as f32 / h. max ( w) ) . min ( 1.0 ) ;
661
+
662
+ ( ( h * scale2) as usize , ( w * scale2) as usize )
682
663
} ;
683
664
684
665
let image_seq_len = config. get_number_of_features ( ) ;
0 commit comments