Spaces:
Sleeping
Sleeping
File size: 111,116 Bytes
7bfbdc3 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 |
# coding=utf-8
# Copyright 2025 MMaDA Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import bisect
import csv
import logging
import itertools
import json
import math
import os
import hashlib
import contextlib
from pathlib import Path
from accelerate import Accelerator
from itertools import chain
# Video real-time?
import os.path as osp
import time
import requests
import random
import re
import datasets
import pandas as pd
from functools import partial
from typing import List, Optional, Union, Dict, Any, Sequence
from glob import glob
from tqdm import tqdm
import numpy as np
import cv2
from PIL import Image
import torch
from dataclasses import dataclass
from datasets import Dataset as HFDataset
from datasets import load_dataset, get_dataset_config_names
from io import BytesIO
Image.warnings.simplefilter('error', Image.DecompressionBombWarning)
import webdataset as wds
import yaml
from braceexpand import braceexpand
from torch.utils.data import default_collate, Dataset
from torchvision import transforms
from transformers import PreTrainedTokenizer
from datasets import (
load_dataset,
load_from_disk,
DatasetDict,
DownloadConfig,
get_dataset_config_names,
concatenate_datasets,
)
import warnings
from training.utils import image_transform as utils_image_transform, image_transform_squash as utils_image_transform_squash
from webdataset.tariterators import (
base_plus_ext,
tar_file_expander,
url_opener,
valid_sample,
)
logger = logging.getLogger(__name__)
S2T_INSTRUCTION = ["Transcribe the given audio.",
"Write down what you hear in the audio.",
"Provide a transcript for the given speech.",
"What does the speaker in the audio say?",
"Convert the speech in the audio to text.",
"Listen to the audio and write out the text."]
T2S_INSTRUCTION = ["Generate speech for the given text.",
"Read the given sentence aloud.",
"Say the given words.",
"Convert the given text into spoken audio.",
"Speak the given text.",
"Synthesize the text into speech."]
V2T_INSTRUCTION = ["Describe the video in detail.",
"Please provide a detailed description of the video.",
"What is happening in the video?",
"Describe the content of the video in detail.",]
V2S_INSTRUCTION = [
"Generate speech that describes the given video.",
"Narrate the events happening in the video.",
"Produce spoken audio describing the video content.",
"Convert the video into a detailed spoken narration.",
"Speak a description of what is shown in the video.",
"Synthesize speech that explains the content of the video.",
]
person_token = ["a person", "someone", "somebody"]
def replace_person_token(t):
"Used for CC12M - handles all case variations of <person> tag"
t = re.sub(r"<person>([,\s]*(and)*[,\s]*<person>)+", " people ", t, flags=re.IGNORECASE)
person_pattern = re.compile(r"<person>", re.IGNORECASE)
while person_pattern.search(t):
match = person_pattern.search(t)
t = t[:match.start()] + f" {random.choice(person_token)} " + t[match.end():]
return t
def filter_keys(key_set):
def _f(dictionary):
return {k: v for k, v in dictionary.items() if k in key_set}
return _f
def group_by_keys_nothrow(data, keys=base_plus_ext, lcase=True, suffixes=None, handler=None, src=None):
"""Return function over iterator that groups key, value pairs into samples.
:param keys: function that splits the key into key and extension (base_plus_ext)
:param lcase: convert suffixes to lower case (Default value = True)
"""
current_sample = None
for filesample in data:
assert isinstance(filesample, dict)
if "fname" not in filesample.keys():
print(f"fname not in filesample.keys(): {filesample}")
print(f"src: {src}")
continue
fname, value = filesample["fname"], filesample["data"]
prefix, suffix = keys(fname)
if prefix is None:
continue
if lcase:
suffix = suffix.lower()
if current_sample is None or prefix != current_sample["__key__"] or suffix in current_sample:
if valid_sample(current_sample):
yield current_sample
current_sample = dict(__key__=prefix, __url__=filesample["__url__"])
if suffixes is None or suffix in suffixes:
current_sample[suffix] = value
if valid_sample(current_sample):
yield current_sample
def tarfile_to_samples_nothrow(src, handler=wds.warn_and_continue):
# NOTE this is a re-impl of the webdataset impl with group_by_keys that doesn't throw
streams = url_opener(src, handler=handler)
files = tar_file_expander(streams, handler=handler) # [{fname,data,__url__}, ...] __url__ 字段标识当前读取的文件来自哪个 tar 包
samples = group_by_keys_nothrow(files, handler=handler, src=src)
return samples
def image_transform(sample, resolution=256):
image = sample["images"]
image = transforms.Resize(resolution, interpolation=transforms.InterpolationMode.BICUBIC)(image)
image = transforms.CenterCrop((resolution, resolution))(image)
image = transforms.ToTensor()(image)
image = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True)(image)
sample["images"] = image
return sample
def image_transform_squash(sample, resolution=256):
image = sample["images"]
image = transforms.Resize((resolution, resolution), interpolation=transforms.InterpolationMode.BICUBIC)(image)
image = transforms.ToTensor()(image)
image = transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])(image)
sample["images"] = image
return sample
def conditional_image_transform(sample, resolution=256):
url = sample.get("__url__", "")
special_datasets = ['ai2d', 'clevr', 'docvqa', 'geo']
use_squash = False
for keyword in special_datasets:
if keyword in url:
use_squash = True
break
if use_squash:
return image_transform_squash(sample, resolution)
else:
return image_transform(sample, resolution)
def remove_prefix(caption):
caption = caption.replace('The image features ', '').replace('The image presents ', '').replace(
"The image you've sent is, ", '').replace("In the center of the image, ", '').replace(
"The image showcases ", '').replace("The image is ", '').replace(
"The image captures ", '').replace("In the given image ", '').replace(
"The image portrays ", '').replace("In the image, ", '').replace("In this image, we see ", '').replace(
"The image depicts ", '').replace("This is ", '').replace("In this image, ", '').replace(
"This image captures ", '')
return caption
def filter_long_samples(sample):
return sample.get('input_ids') is not None
class Text2ImageDataset:
def __init__(
self,
train_shards_path_or_url: Union[str, List[str]],
tokenizer: PreTrainedTokenizer,
max_seq_length: int,
num_train_examples: int,
per_gpu_batch_size: int,
global_batch_size: int,
num_workers: int,
resolution: int = 256,
shuffle_buffer_size: int = 1000,
pin_memory: bool = False,
persistent_workers: bool = False,
external_caption_path: Optional[str] = '',
external_journeydb_caption_path: Optional[str] = '',
external_laion12m_caption_path: Optional[str] = '',
external_cc12m_caption_path: Optional[str] = '',
external_text_to_image_2M_512_caption_path: Optional[str] = '',
external_ai2d_caption_path: Optional[str] = '',
external_clevr_caption_path: Optional[str] = '',
external_docvqa_caption_path: Optional[str] = '',
external_geo_caption_path: Optional[str] = '',
is_captioning: bool = False,
add_caption_prompt: bool = False,
long_caption: bool = True,
shuffle: bool = True,
):
if f"{train_shards_path_or_url}.yaml" in os.listdir('./configs'):
with open(f"./configs/{train_shards_path_or_url}.yaml") as f:
train_shards_path_or_url = yaml.safe_load(f)
self.long_caption = long_caption
self.external_caption_path = external_caption_path
self.external_journeydb_caption_path = external_journeydb_caption_path
self.external_laion12m_caption_path = external_laion12m_caption_path
self.external_cc12m_caption_path = external_cc12m_caption_path
self.external_text_to_image_2M_512_caption_path = external_text_to_image_2M_512_caption_path
self.is_captioning = is_captioning
self.add_caption_prompt = add_caption_prompt
if self.add_caption_prompt:
with open("./training/questions.json") as f:
self.caption_prompt = json.load(f)
# self.caption_prompt = ['USER: \n' + prompt + ' ASSISTANT:' for prompt in self.caption_prompt]
self.caption_prompt = ['<|start_header_id|>user<|end_header_id|>\n' + prompt + '<eot_id><|start_header_id|>assistant<|end_header_id|>\n' for prompt in self.caption_prompt]
else:
self.caption_prompt = None
if external_journeydb_caption_path != '':
with open(external_journeydb_caption_path) as file:
self.journeydb_caption = json.load(file)
else:
self.journeydb_caption = None
if external_ai2d_caption_path!= '':
self.ai2d_caption = pd.read_csv(external_ai2d_caption_path)
if external_clevr_caption_path!= '':
self.clevr_caption = pd.read_csv(external_clevr_caption_path)
if external_docvqa_caption_path!= '':
self.docvqa_caption = pd.read_csv(external_docvqa_caption_path)
if external_geo_caption_path!= '':
self.geo_caption = pd.read_csv(external_geo_caption_path)
def tokenize(text):
if tokenizer is not None:
text = replace_person_token(text)
encoding = tokenizer(
text,
truncation=True,
max_length=2 * max_seq_length,
padding=False,
return_tensors="pt"
)
full_input_ids = encoding.input_ids[0]
if len(full_input_ids) > max_seq_length:
return None
else:
return text
else:
return text
if not isinstance(train_shards_path_or_url, str):
train_shards_path_or_url = [list(braceexpand(urls)) for urls in train_shards_path_or_url]
# flatten list using itertools
train_shards_path_or_url = list(itertools.chain.from_iterable(train_shards_path_or_url))
if external_caption_path != '':
processing_pipeline = [
wds.decode("pil", handler=wds.ignore_and_continue),
wds.map(self.load_external_caption, handler=wds.ignore_and_continue),
wds.rename(
images="jpg;png;jpeg;webp",
input_ids="text;txt;caption",
handler=wds.warn_and_continue,
),
wds.map(partial(conditional_image_transform, resolution=resolution), handler=wds.warn_and_continue),
wds.map(filter_keys(set(["images", "input_ids"]))),
wds.map_dict(
input_ids=tokenize,
handler=wds.warn_and_continue,
),
wds.select(filter_long_samples),
]
else:
processing_pipeline = [
wds.decode("pil", handler=wds.ignore_and_continue),
wds.rename(
images="jpg;png;jpeg;webp",
input_ids="text;txt;caption",
handler=wds.warn_and_continue,
),
wds.map(partial(conditional_image_transform, resolution=resolution), handler=wds.warn_and_continue),
wds.map(filter_keys(set(["images", "input_ids"]))),
wds.map_dict(
input_ids=tokenize,
handler=wds.warn_and_continue,
),
wds.select(filter_long_samples),
]
pipeline = [
wds.ResampledShards(train_shards_path_or_url),
tarfile_to_samples_nothrow,
wds.shuffle(shuffle_buffer_size),
*processing_pipeline,
wds.batched(per_gpu_batch_size, partial=False, collation_fn=default_collate),
]
num_batches = math.ceil(num_train_examples / global_batch_size)
num_worker_batches = math.ceil(num_train_examples / (global_batch_size * num_workers)) # per dataloader worker
num_batches = num_worker_batches * num_workers
num_samples = num_batches * global_batch_size
self._train_dataset = wds.DataPipeline(*pipeline).with_epoch(num_worker_batches)
self._train_dataloader = wds.WebLoader(
self._train_dataset,
batch_size=None,
shuffle=False,
num_workers=num_workers,
pin_memory=pin_memory,
persistent_workers=persistent_workers,
)
# add meta-data to dataloader instance for convenience
self._train_dataloader.num_batches = num_batches
self._train_dataloader.num_samples = num_samples
def load_external_caption(self, sample):
if 'SA1B' in sample['__key__'] or 'sa' in sample['__key__']:
captionf = f"{self.external_caption_path}/{sample['__key__'].split('/')[-1]}.txt"
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.readlines()[0].replace('\n', '')
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif 'laion' in sample['__url__']:
url_part = sample['__url__'].split('/')[-1].split('.')[0]
key = sample['__key__'].split('/')[-1]
captionf = os.path.join(self.external_laion12m_caption_path, url_part, f"{key}.caption")
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.read().strip()
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif 'cc12m' in sample['__url__']:
url_part = sample['__url__'].split('/')[-1].split('.')[0]
key = sample['__key__'].split('/')[-1]
captionf = os.path.join(self.external_cc12m_caption_path, url_part, f"{key}.caption")
if os.path.exists(captionf):
with open(captionf, "r") as reader:
captions = reader.read().strip()
else:
captions = ""
# for captioning
if self.is_captioning:
if self.add_caption_prompt is not None:
prompt = random.sample(self.caption_prompt, 1)[0]
sample['txt'] = prompt + captions
else:
sample['txt'] = captions
# for generation
else:
# randomly choose short and long captions
if random.random() < 0.5:
sample['txt'] = captions.split('.')[0]
else:
sample['txt'] = captions
sample['txt'] = remove_prefix(sample['txt'])
return sample
elif "text-to-image-2M" in sample['__url__']:
if "json" in sample and "prompt" in sample["json"]:
captions = sample["json"]["prompt"]
else:
print(f"sample has no json or prompt: {sample}")
captions = ""
sample['txt'] = captions
return sample
elif 'ai2d' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.ai2d_caption[self.ai2d_caption['image'].astype(str) == key + '.png']
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'clevr' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.clevr_caption[self.clevr_caption['image'].astype(str) == key + ".jpg"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'docvqa' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.docvqa_caption[self.docvqa_caption['image'].astype(str) == key + ".png"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif 'geo' in sample['__url__']:
key = sample['__key__'].split('/')[-1]
df_row = self.geo_caption[self.geo_caption['image'].astype(str) == key + ".jpg"]
if len(df_row) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
elif len(df_row) > 1:
# print(f"Multiple captions available for key {sample['__key__']}")
df_row = df_row.sample(1)
question = df_row['question'].values[0]
solution = df_row['solution'].values[0]
caption = (
'<|start_header_id|>user<|end_header_id|>\n'
"You should first think about the reasoning process in the mind and then provide the user with the answer. The reasoning process is enclosed within <think> </think> tags, i.e. <think> reasoning process here </think> answer here\n"
f"{question}\n"
'<eot_id><|start_header_id|>assistant<|end_header_id|>\n'
f"{solution}"
)
sample['txt'] = caption
return sample
elif self.journeydb_caption is not None and sample['__key__'] in self.journeydb_caption:
captions_list = self.journeydb_caption[sample['__key__']]
if len(captions_list) == 0:
print(f"No captions available for key {sample['__key__']}")
return sample
sample['txt'] = random.sample(captions_list, 1)[0]
return sample
else:
print(f"none exist sample: {sample}")
return sample
@property
def train_dataset(self):
return self._train_dataset
@property
def train_dataloader(self):
return self._train_dataloader
# +++++ S2T/T2S Dataset Definition +++++
class SpeechTextDataset(Dataset):
def __init__(self, dataset : str, subset : str, split : Optional[str] = None):
self.dataset_name = dataset
if self.dataset_name == "gigaspeech": # subset is either "xs" or "xl"
self.hgf_dataset : datasets.Dataset = load_dataset("speechcolab/gigaspeech", subset, split=split)
elif self.dataset_name == "librispeech":
root_path = "/home/work/AIDAS/data/audio/LibriSpeech"
self.dataset_path = root_path + "/" + subset # subset is like "train-clean-100", etc
if split is not None:
warnings.warn(f"Split parameter '{split}' is provided but will not be used for LibriSpeech dataset.")
# librispeech path processing
self.subdirs_path = sorted(list(glob(self.dataset_path + "/*/*")))
self.subdirs_len = [len(glob(subdir + "/*.flac")) for subdir in self.subdirs_path]
self.subdirs_len_accum = list(itertools.accumulate(self.subdirs_len))
# handle wrong subset name
if len(self.subdirs_path) == 0:
raise ValueError(f"Invalid subset name '{subset}' for LibriSpeech dataset. Available subsets are: train-clean-100, train-clean-360")
elif self.dataset_name == "commonvoice":
self.commonvoice_path = "/home/work/AIDAS/data/audio/commonvoice/cv-corpus-22.0-2025-06-20/en"
if split is not None:
warnings.warn(f"Split parameter '{split}' is provided but will not be used for commonvoice dataset.")
self.tsv = pd.read_csv(self.commonvoice_path + f"/{subset}.tsv", sep="\t", usecols=["path", "sentence"])
else:
raise ValueError(f"Unsupported dataset: {dataset}. Supported datasets are: gigaspeech, librispeech, commonvoice.")
def __len__(self):
if self.dataset_name == "gigaspeech":
return len(self.hgf_dataset)
elif self.dataset_name == "librispeech":
return self.subdirs_len_accum[-1]
else: # commonvoice
return len(self.tsv)
def __getitem__(self, idx):
audio_path : str; text : str
if self.dataset_name == "gigaspeech":
sample = self.hgf_dataset[idx]
audio_path = sample["audio"]["path"]
text = sample["text"]
elif self.dataset_name == "librispeech":
# idx overflow
if idx >= self.subdirs_len_accum[-1]:
raise IndexError(f"Index {idx} is out of bounds for the dataset with length {len(self)}.")
# audio_path (flac)
subdir_idx = bisect.bisect_right(self.subdirs_len_accum, idx)
flac_idx = idx - self.subdirs_len_accum[subdir_idx - 1] if subdir_idx > 0 else idx
audio_path = sorted(list(glob(self.subdirs_path[subdir_idx]+"/*.flac")))[flac_idx]
# text
txt_path = glob(self.subdirs_path[subdir_idx]+"/*.txt")
assert len(txt_path) == 1, f"Expected one txt file in {self.subdirs_path[subdir_idx]}, found {len(txt_path)}"
with open(txt_path[0], "r") as f:
txt = f.readlines()
text = " ".join(txt[flac_idx].split(" ")[1:]) # rip off the header, e.g., "103-1240-0007 [TEXT]"
else: # commonvoice
audio_path = self.commonvoice_path + "/clips/" + self.tsv.iloc[idx]["path"]
text = self.tsv.iloc[idx]["sentence"]
return {"audio_path": audio_path, "text": text}
class MixedSpeechTextDataset(Dataset):
def __init__(self, dataset_configs: list):
"""
Initializes and combines multiple speech datasets.
Args:
dataset_configs (list): A list of configuration dictionaries,
where each dict defines a dataset to load.
"""
self.dataset_metadata = []
self.dataset_lengths = []
self._sha1 = hashlib.sha1
# Iterate through the list of dataset configurations from the YAML file
for config in dataset_configs:
name = config['name']
subset = config.get('subset')
split = config.get('split')
use_tokens = bool(config.get("use_precomputed_tokens", False))
token_root = config.get("precomputed_tokens_root")
token_root_path = Path(token_root).expanduser() if token_root else None
print(f"Initializing dataset: {name} (Subset: {subset}, Split: {split})")
# --- Gigaspeech ---
if name == "gigaspeech":
hgf_dataset = datasets.load_dataset("speechcolab/gigaspeech", subset, split=split)
self.dataset_metadata.append({
"name": name,
"data": hgf_dataset,
"use_precomputed_tokens": use_tokens and token_root_path is not None,
"precomputed_tokens_root": token_root_path,
})
self.dataset_lengths.append(len(hgf_dataset))
# --- LibriSpeech ---
elif name == "librispeech":
root_path = "/home/work/AIDAS/data/audio/LibriSpeech"
dataset_path = f"{root_path}/{subset}"
if split is not None:
warnings.warn(f"Split parameter '{split}' is provided but will not be used for LibriSpeech.")
subdirs_path = sorted(glob(f"{dataset_path}/*/*"))
if not subdirs_path:
raise ValueError(f"Invalid subset for LibriSpeech or path not found: {dataset_path}")
subdirs_len = [len(glob(f"{subdir}/*.flac")) for subdir in subdirs_path]
subdirs_len_accum = list(itertools.accumulate(subdirs_len))
metadata = {
"name": name,
"subdirs_path": subdirs_path,
"subdirs_len_accum": subdirs_len_accum,
"use_precomputed_tokens": use_tokens and token_root_path is not None,
"precomputed_tokens_root": token_root_path,
}
self.dataset_metadata.append(metadata)
self.dataset_lengths.append(subdirs_len_accum[-1])
# --- Common Voice ---
elif name == "commonvoice":
commonvoice_path = "/home/work/AIDAS/data/audio/commonvoice/cv-corpus-22.0-2025-06-20/en"
if split is not None:
warnings.warn(f"Split parameter '{split}' is provided but will not be used for Common Voice.")
tsv_path = f"{commonvoice_path}/{subset}.tsv"
tsv = pd.read_csv(tsv_path, sep="\t", usecols=["path", "sentence"])
metadata = {
"name": name,
"data_root": f"{commonvoice_path}/clips/",
"tsv": tsv,
"use_precomputed_tokens": use_tokens and token_root_path is not None,
"precomputed_tokens_root": token_root_path,
}
self.dataset_metadata.append(metadata)
self.dataset_lengths.append(len(tsv))
else:
raise ValueError(f"Unsupported dataset: {name}.")
# Calculate cumulative lengths to map a global index to a specific dataset
self.cumulative_lengths = list(itertools.accumulate(self.dataset_lengths))
# print(f"✅ All datasets loaded for the SPEECH!. Total length: {self.__len__()} samples.")
def __len__(self):
"""Returns the total number of samples across all datasets."""
return self.cumulative_lengths[-1] if self.cumulative_lengths else 0
def __getitem__(self, idx):
"""
Fetches a sample from the combined dataset.
It first determines which dataset the global index `idx` belongs to,
calculates the local index within that dataset, and then retrieves the item.
"""
if idx >= self.__len__():
raise IndexError(f"Index {idx} is out of bounds for the combined dataset with length {self.__len__()}.")
# Find which dataset the index belongs to
dataset_idx = bisect.bisect_right(self.cumulative_lengths, idx)
# Calculate the local index within that dataset
local_idx = idx - self.cumulative_lengths[dataset_idx - 1] if dataset_idx > 0 else idx
metadata = self.dataset_metadata[dataset_idx]
dataset_name = metadata["name"]
dataset_length = self.dataset_lengths[dataset_idx]
audio_path: str
text: str
audio_tokens: Optional[torch.Tensor]
max_retry = 5
retry = 0
while retry < max_retry:
try:
audio_tokens = None
if dataset_name == "gigaspeech":
sample = metadata["data"][local_idx]
audio_path = sample["audio"]["path"]
text = sample["text"]
# Preprocess special words to punctuation
text = (
text.replace(" <COMMA>", ",")
.replace(" <PERIOD>", ".")
.replace(" <QUESTIONMARK>", "?")
.replace(" <EXCLAMATIONMARK>", "!")
)
elif dataset_name == "librispeech":
# Find the specific subdirectory and file using the local index
subdir_idx = bisect.bisect_right(metadata["subdirs_len_accum"], local_idx)
flac_idx = local_idx - metadata["subdirs_len_accum"][subdir_idx - 1] if subdir_idx > 0 else local_idx
subdir_path = metadata["subdirs_path"][subdir_idx]
audio_path = sorted(glob(f"{subdir_path}/*.flac"))[flac_idx]
# Read the corresponding transcript
txt_path = glob(f"{subdir_path}/*.txt")[0]
with open(txt_path, "r") as f:
line = f.readlines()[flac_idx]
text = " ".join(line.strip().split(" ")[1:])
else: # commonvoice
row = metadata["tsv"].iloc[local_idx]
audio_path = metadata["data_root"] + row["path"]
text = row["sentence"]
# Preprocess lower case to upper case
text = text.upper()
audio_tokens = self._maybe_load_precomputed_tokens(audio_path, metadata)
return {
"audio_path": audio_path,
"text": text,
"audio_tokens": audio_tokens,
}
except Exception as exc:
print(f"[MixedSpeechTextDataset] Failed to load sample from '{dataset_name}' at local index {local_idx}: {exc!r}")
retry += 1
if retry >= max_retry:
break
local_idx = random.randint(0, dataset_length - 1)
continue
raise RuntimeError(f"Unable to fetch a valid sample from dataset '{dataset_name}' after {max_retry} retries.")
def _maybe_load_precomputed_tokens(self, audio_path: str, metadata: dict) -> Optional[torch.Tensor]:
if not metadata.get("use_precomputed_tokens"):
return None
root: Optional[Path] = metadata.get("precomputed_tokens_root")
if root is None:
return None
if not root.exists():
logger.warning("Precomputed token root missing: %s", root)
return None
key = os.path.abspath(audio_path)
digest = self._sha1(key.encode("utf-8")).hexdigest()
token_path = root / digest[:2] / digest[2:4] / f"{digest}.pt"
if not token_path.exists():
logger.warning("Precomputed audio tokens not found: %s", token_path)
return None
try:
tokens = torch.load(token_path, map_location="cpu")
if isinstance(tokens, torch.Tensor):
return tokens.clone()
if isinstance(tokens, (list, tuple)):
return torch.tensor(tokens, dtype=torch.long)
logger.warning("Unexpected token format in %s (type=%s)", token_path, type(tokens))
except Exception as exc:
logger.warning("Failed to load precomputed tokens %s: %s", token_path, exc)
return None
class Speech2SpeechDataset(Dataset):
"""
Mixed dataset of emova-sft and InstructS2S-200K.
Return value of __getitem__ indicates a pair of (user, assistant) message (single-turn).
Critically, the return type for emova_sft and instructs2s are different:
emova_sft: tuple[list[int], list[int], Any]
instructs2s: tuple[str, str, Optional[Any]]
So in the main training code, we need to handle both types of return values.
Notes:
- Use `s2s_collate_fn` within DataLoader.
- For emova_sft, Tensor cannot be returned because padding is done later in the main training code.
For reference:
Total samples: 496514
emova-sft (speech-text): 73.6k
emova-sft (speech-image): 71.5k
InstructS2S-200K: 422856
"""
def __init__(self, dataset_configs: list):
self.dataset_configs = dataset_configs # currently this arg is not used
## emova-sft (text + image splits)
emova_sft_text = load_dataset("Emova-ollm/emova-sft-4m", "emova-speech-text-en", split='train')
emova_sft_image = load_dataset("Emova-ollm/emova-sft-4m", "emova-speech-image-en", split='train')
def _maybe_cast_image_columns(ds):
for column in ("image", "images"):
if column in ds.column_names:
try:
ds = ds.cast_column(column, datasets.Image(decode=False))
except Exception:
# Column may already be raw bytes/str; ignore and keep as-is
pass
return ds
emova_sft_text = _maybe_cast_image_columns(emova_sft_text)
emova_sft_image = _maybe_cast_image_columns(emova_sft_image)
def emova_sft_preprocess_batch(batch):
# Extract conversations from the batch
conversations_list = batch['conversations']
usr_ids_list = []
asst_ids_list = []
images_list = []
def normalize_emova_ids(ids: str) -> list[int]:
unit_numbers = ids.replace('<|speech_', '').replace('|>', ' ').strip()
unit_ids = [int(unit) for unit in unit_numbers.split(" ")]
return unit_ids
# Process each conversation in the batch
for conversations in conversations_list:
usr_raw = conversations[0]['value']
asst_raw = conversations[1]['value']
usr_ids: str = usr_raw.split("\n\nuser question speech:")[-1].strip()
asst_ids: str = json.loads(asst_raw)['assistant response speech'].strip()
usr_ids_list.append(normalize_emova_ids(usr_ids))
asst_ids_list.append(normalize_emova_ids(asst_ids))
raw_images = (
batch.get('image')
or batch.get('images')
or batch.get('image_base64')
or [None] * len(conversations_list)
)
if not isinstance(raw_images, (list, tuple)):
raw_images = [raw_images] * len(conversations_list)
else:
raw_images = list(raw_images)
if len(raw_images) != len(conversations_list):
# Align lengths by padding/truncating with None without decoding payloads
adjusted = raw_images[:len(conversations_list)]
if len(adjusted) < len(conversations_list):
adjusted.extend([None] * (len(conversations_list) - len(adjusted)))
raw_images = adjusted
images_list.extend(raw_images)
# Return a dictionary with lists of processed data
return {
"usr_ids": usr_ids_list,
"asst_ids": asst_ids_list,
"image": images_list,
}
self.emova_sft_text = emova_sft_text.map(
emova_sft_preprocess_batch,
batched=True,
batch_size=1024,
remove_columns=['conversations'],
desc="Processing emova-sft (text)",
num_proc=16
)
self.emova_sft_image = emova_sft_image.map(
emova_sft_preprocess_batch,
batched=True,
batch_size=1024,
remove_columns=['conversations'],
desc="Processing emova-sft (image)",
num_proc=16
)
self._emova_text_len = len(self.emova_sft_text)
self._emova_image_len = len(self.emova_sft_image)
self._emova_total_len = self._emova_text_len + self._emova_image_len
## InstructS2S-200K (with caching)
instructs2s_rootdir = "/home/work/AIDAS/data/InstructS2S-200K/en/wav"
self.instructs2s_wav_pair_paths = []
pairs_txt = os.path.join(instructs2s_rootdir, "pairs.txt")
if os.path.isfile(pairs_txt):
with open(pairs_txt, "r") as f:
for line in tqdm(f, desc="Loading InstructS2S-200K paths from cached file"):
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) >= 2:
self.instructs2s_wav_pair_paths.append((parts[0], parts[1]))
else:
instructs2s_wav_dirs = [p for p in glob(os.path.join(instructs2s_rootdir, "*")) if os.path.isdir(p)]
# Walk each directory and collect (user, assistant) wav pairs
for dir_path in tqdm(instructs2s_wav_dirs, desc="Processing instructs2s-200k"):
dir_name = os.path.basename(dir_path)
k = 1
while True:
user_wav = os.path.join(dir_path, f"{dir_name}-{k}-user.wav")
assistant_wav = os.path.join(dir_path, f"{dir_name}-{k}-assistant.wav")
if os.path.isfile(user_wav) and os.path.isfile(assistant_wav):
self.instructs2s_wav_pair_paths.append((user_wav, assistant_wav))
k += 1
continue
break
with open(pairs_txt, "w") as f:
for u, a in self.instructs2s_wav_pair_paths:
f.write(f"{u} {a}\n")
## Mixed dataset (ordered)
self.mixed_dataset = [self.emova_sft_text, self.emova_sft_image, self.instructs2s_wav_pair_paths]
def __len__(self):
return sum([len(dataset) for dataset in self.mixed_dataset])
def __getitem__(self, idx) -> Union[tuple[list[int], list[int], Any], tuple[str, str, Optional[Any]]]:
if idx < self._emova_text_len: # emova_sft text split
sample = self.emova_sft_text[idx]
elif idx < self._emova_total_len: # emova_sft image split
sample = self.emova_sft_image[idx - self._emova_text_len]
else: # instructs2s
local_idx = idx - self._emova_total_len
usr_wav, asst_wav = self.instructs2s_wav_pair_paths[local_idx]
return usr_wav, asst_wav, None # tuple[str, str, Optional[image]]; wav file paths
usr_ids = sample['usr_ids']
asst_ids = sample['asst_ids']
image = sample.get('image')
return usr_ids, asst_ids, image # tuple[list[int], list[int], image]
def s2s_collate_fn(batch):
"""
Collate function for Speech2SpeechDataset.
"""
emova_data = []
instructs2s_data = []
for item in batch:
if isinstance(item[0], list): # emova_sft: tuple[list[int], list[int]]
emova_data.append(item)
else: # instructs2s: tuple[str, str]
instructs2s_data.append(item)
return {
'emova_sft': emova_data,
'instructs2s': instructs2s_data,
}
class VideoCaptionDataset(Dataset):
def __init__(
self,
transform,
tokenizer,
max_seq_length: int,
resolution: int = 256,
panda70m_path = "/home/work/AIDAS/data/video/panda70m/panda70m_training_2m",
openvid1m_path = "/home/work/AIDAS/data/video/openvid1m/video",
webvid10m_path = "/home/work/AIDAS/data/video/webvid10m",
llavavid_path = "/home/work/AIDAS/data/video/LLaVA-Video-178K",
dataset_name = "openvid1m",
llavavid_local_files_only: bool = False,
llavavid_skip_configs: Optional[Sequence[str]] = None,
llavavid_skip_video_patterns: Optional[Sequence[str]] = None,
sample_method='uniform',
num_frames: int = 8,
vq_model=None,
):
available_datasets = ['panda70m', 'openvid1m', 'webvid10m', 'llavavid']
if dataset_name not in available_datasets:
raise ValueError(f"Invalid dataset name: {dataset_name}. Available datasets: {available_datasets}")
self.max_seq_length = max_seq_length
self.transform = transform
self.vq_model = vq_model
self.tokenizer = tokenizer
self.resolution = resolution
self.sample_method = sample_method
self.dataset_name = dataset_name
self.num_frames = num_frames
self.llavavid_local_files_only = llavavid_local_files_only
self.llavavid_skip_configs = set(llavavid_skip_configs or [])
self.llavavid_skip_video_patterns = tuple(llavavid_skip_video_patterns or [])
self.caption_prompt = V2T_INSTRUCTION
self.caption_prompt = ['<|start_header_id|>user<|end_header_id|>\n' + prompt + '<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n' for prompt in self.caption_prompt]
self.webvid10m_path = webvid10m_path
if dataset_name == 'panda70m':
self.vid_data = self._collect_panda70m(panda70m_path)
self.dataset_root = panda70m_path
elif dataset_name == 'webvid10m':
self.vid_data = self._collect_webvid10m(webvid10m_path)
self.dataset_root = webvid10m_path
elif dataset_name == 'openvid1m':
self.vid_data = self._collect_openvid1m(openvid1m_path)
self.dataset_root = openvid1m_path
elif dataset_name == 'llavavid':
self.vid_data = self._collect_llavavid(llavavid_path)
self.dataset_root = Path(llavavid_path)
self.llavavid_video_root = Path(llavavid_path)
else:
raise ValueError(f"Invalid dataset name: {dataset_name}. Available datasets: panda70m, webvid10m")
def _get_caption_prompt(self):
"""
Get a random caption prompt from the list of caption prompts.
"""
return np.random.choice(self.caption_prompt)
def _tokenize(self, text):
if self.tokenizer is not None:
input_ids = self.tokenizer(
text,
truncation=True,
max_length=2 * self.max_seq_length,
padding=False,
return_tensors="pt"
)[0]
if len(input_ids) > self.max_seq_length:
return None
else:
return input_ids
else:
raise ValueError("Tokenizer is not provided.")
def _collect_webvid10m(self, root_path):
print("Loading videos from WebVid10m dataset...")
csv_path = osp.join(root_path, "webvid-10M-train.csv")
webvid_pd = pd.read_csv(csv_path)
self.dataset_length = len(webvid_pd)
print(f"{len(webvid_pd)} videos has been loaded.")
return webvid_pd
def _collect_panda70m(self, root_path):
video_caption_pairs = []
subdirs = sorted(os.listdir(root_path))
print("Loading videos from panda70m dataset...")
for subdir in subdirs:
full_subdir = os.path.join(root_path, subdir)
if not os.path.isdir(full_subdir):
continue
video_paths = glob(os.path.join(full_subdir, "*.mp4"))
for video_path in video_paths:
caption_path = video_path.replace(".mp4", ".txt")
if os.path.exists(caption_path):
with open(caption_path, 'r') as f:
caption = f.read().strip()
prompt = self._get_caption_prompt()
video_caption_pairs.append({
"video": video_path,
"caption": prompt + caption
})
print(f"{len(video_caption_pairs)} videos has been loaded.")
return video_caption_pairs
def _collect_openvid1m(self, root_path):
csv_path = osp.join(root_path, "OpenVid-1M.csv")
openvid_pd = pd.read_csv(csv_path)
self.dataset_length = len(openvid_pd)
print(f"{len(openvid_pd)} videos has been loaded.")
return openvid_pd
def _collect_llavavid(
self,
root_path="lmms-lab/LLaVA-Video-178K",
cache_dir="/home/work/AIDAS/huggingface/datasets"
):
"""
Collect all available (and locally cached) subsets of the LLaVA-Video-178K dataset.
Handles both on-disk exports (each config stored as subfolders of splits) and remote configs.
Returns a single flattened HuggingFace Dataset that concatenates every successfully loaded config.
"""
DATASET_NAME = root_path
local_root = Path(DATASET_NAME)
configs: list[str]
using_local_dirs = local_root.exists()
configs = []
if using_local_dirs:
for p in sorted(local_root.iterdir()):
if not p.is_dir():
continue
if p.name.startswith("."):
continue
split_exists = any((p / split_name).exists() for split_name in ("open_ended", "caption", "multi_choice"))
if not split_exists:
continue
configs.append(p.name)
if not configs:
using_local_dirs = False
if not configs:
try:
configs = get_dataset_config_names(DATASET_NAME)
using_local_dirs = False
except Exception as e:
raise RuntimeError(f"Failed to fetch configs for {DATASET_NAME}: {e}")
skip_configs = getattr(self, "llavavid_skip_configs", set())
if skip_configs:
existing = [cfg for cfg in configs if cfg in skip_configs]
if existing:
print(f"LLaVA-Vid: skipping configs {existing}")
configs = [cfg for cfg in configs if cfg not in skip_configs]
if not configs:
raise RuntimeError("All LLaVA-Video configs were skipped; nothing left to load.")
def _add_config_column(dataset: HFDataset, cfg_name: str, row_count: int):
"""Attach the originating config name so downstream can locate videos."""
if dataset is None or not cfg_name:
return dataset
if "llavavid_config" in dataset.column_names:
return dataset
return dataset.add_column("llavavid_config", [cfg_name] * row_count)
def _flatten_dataset(ds_obj, label: str, cfg_name: str):
"""Convert DatasetDicts into a single Dataset and report the row count."""
if ds_obj is None:
return None, 0
if isinstance(ds_obj, DatasetDict):
splits = [split for split in ds_obj.values()]
if not splits:
print(f"Skipping {label}: dataset dict has no splits.")
return None, 0
total_rows = sum(len(split) for split in splits)
if len(splits) == 1:
return splits[0], total_rows
try:
merged = concatenate_datasets(splits)
except Exception as merge_err:
print(f"Skipping {label}: failed to concatenate splits: {merge_err}")
return None, 0
dataset = merged
else:
dataset = ds_obj
try:
total_rows = len(dataset)
except Exception as len_err:
print(f"Skipping {label}: unable to compute dataset length ({len_err}).")
return None, 0
dataset = _add_config_column(dataset, cfg_name, total_rows)
return dataset, total_rows
def _load_local_config(cfg_name: str):
"""Attempt to read a single config from disk, handling split sub-directories if needed."""
cfg_root = local_root / cfg_name
if not cfg_root.exists():
return None, 0
# First try loading the directory directly (Dataset or DatasetDict exports).
try:
ds_direct = load_from_disk(str(cfg_root))
except Exception as direct_err:
print(f"Failed to load config {cfg_name} via load_from_disk: {direct_err}.")
else:
ds_flat, ds_count = _flatten_dataset(ds_direct, cfg_name, cfg_name)
if ds_flat is not None and ds_count > 0:
return ds_flat, ds_count
# Fallback: iterate over split sub-directories (caption/open_ended/multi_choice, etc.).
split_dirs = [p for p in sorted(cfg_root.iterdir()) if p.is_dir()]
if not split_dirs:
return None, 0
split_datasets = []
for split_dir in split_dirs:
try:
split_ds = load_from_disk(str(split_dir))
except Exception as split_err:
print(f"Skipping {cfg_name}/{split_dir.name}: {split_err}")
continue
split_datasets.append(split_ds)
if not split_datasets:
return None, 0
split_total = sum(len(split_ds) for split_ds in split_datasets)
if len(split_datasets) == 1:
dataset = split_datasets[0]
else:
try:
dataset = concatenate_datasets(split_datasets)
except Exception as merge_err:
print(f"Skipping {cfg_name}: failed to concatenate split datasets: {merge_err}")
return None, 0
dataset = _add_config_column(dataset, cfg_name, split_total)
return dataset, split_total
datasets_loaded = []
total_count = 0
for cfg in configs:
ds = None
cfg_count = 0
if using_local_dirs:
ds, cfg_count = _load_local_config(cfg)
if ds is None or cfg_count == 0:
download_cfg = None
if self.llavavid_local_files_only:
download_cfg = DownloadConfig(local_files_only=True)
try:
remote_ds = load_dataset(
DATASET_NAME,
name=cfg,
cache_dir=cache_dir,
verification_mode="no_checks",
download_config=download_cfg,
)
except Exception as remote_err:
print(f"Skipping {cfg}: {remote_err}")
continue
ds, cfg_count = _flatten_dataset(remote_ds, cfg, cfg)
if ds is None or cfg_count == 0:
print(f"Skipping {cfg}: dataset empty after flattening.")
continue
datasets_loaded.append(ds)
total_count += cfg_count
if not datasets_loaded:
raise RuntimeError("No valid configs could be loaded!")
if len(datasets_loaded) == 1:
global_dataset = datasets_loaded[0]
else:
try:
global_dataset = concatenate_datasets(datasets_loaded)
except Exception as merge_err:
print(f"Failed to concatenate configs in one step: {merge_err}. Trying pairwise concatenation.")
try:
combined = datasets_loaded[0]
for ds_next in datasets_loaded[1:]:
combined = concatenate_datasets([combined, ds_next])
global_dataset = combined
except Exception as pair_err:
raise RuntimeError(f"Unable to merge LLaVA-Video configs: {pair_err}") from pair_err
# Filter out samples whose video path matches known-bad patterns (e.g., missing shareVideoGPTV frames)
skip_patterns = getattr(self, "llavavid_skip_video_patterns", tuple())
if skip_patterns:
def _matches_skip(entry: dict[str, Any]) -> bool:
video_entry = entry.get("video")
if not isinstance(video_entry, str):
return False
return any(pattern in video_entry for pattern in skip_patterns)
def _filter_dataset(ds_obj):
if isinstance(ds_obj, list):
filtered_list = []
removed_total = 0
for item in ds_obj:
filtered_item, removed_item = _filter_dataset(item)
removed_total += removed_item
if filtered_item is None:
continue
filtered_list.append(filtered_item)
return filtered_list, removed_total
elif isinstance(ds_obj, HFDataset):
before = len(ds_obj)
filtered = ds_obj.filter(lambda ex: not _matches_skip(ex))
removed = before - len(filtered)
return filtered, removed
elif isinstance(ds_obj, dict):
return (None, 1) if _matches_skip(ds_obj) else (ds_obj, 0)
else:
return ds_obj, 0
global_dataset, removed_samples = _filter_dataset(global_dataset)
if removed_samples > 0:
total_count -= removed_samples
print(f"LLaVA-Vid: skipped {removed_samples} samples matching patterns {skip_patterns}.")
print(f"LLaVA-Vid: {len(datasets_loaded)} configs loaded.")
print(f"LLaVA-Vid: {total_count:,} total samples loaded.")
self.dataset_length = total_count
return global_dataset
def __len__(self):
return len(self.vid_data)
def __getitem__(self, idx):
max_try_count = 50
for try_count in range(max_try_count):
try:
data = self._sample_data(idx)
except Exception as exc:
logger.warning(
"VideoCaptionDataset failed to fetch index %s on attempt %s/%s: %s",
idx,
try_count + 1,
max_try_count,
exc,
)
idx = random.randint(0, self.dataset_length - 1)
continue
if data is not None:
return {
"video": data["video"],
"caption": data["caption"],
}
idx = random.randint(0, self.dataset_length - 1)
logger.warning(
"VideoCaptionDataset exhausted %s attempts without a valid sample; returning None.",
max_try_count,
)
return None
def _sample_data_webvid10m(self):
store_path = osp.join(self.webvid10m_path, "video_store")
row = self.video_caption_pairs['webvid10m'].sample(1).iloc[0]
video_id = str(row["videoid"])
url = row["contentUrl"]
caption = row["name"]
video_path = osp.join(store_path, f"{video_id}.mp4")
if not osp.exists(video_path): # not downloaded yet
download_video_url(url, video_path)
# print(video_id)
# print(_whoami_str())
return video_path, caption
def _sample_data(self, idx):
if self.dataset_name == 'webvid10m':
# currently randomly sample from the dataset
video_path, caption = self._sample_data_webvid10m()
elif self.dataset_name == 'panda70m':
raise NotImplementedError("Panda70m is not implemented yet.")
# video_path, caption = self._sample_data_panda70m()
elif self.dataset_name == 'openvid1m':
data_row = self.vid_data.iloc[idx]
video_path = osp.join(self.dataset_root, "video", data_row["video"])
caption = data_row["caption"]
elif self.dataset_name == 'llavavid':
data_row = self.vid_data[idx]
video_entry = data_row['video']
cfg_name = data_row.get('llavavid_config') if isinstance(data_row, dict) else None
caption = data_row['conversations'] # this is a list of turns in llavavid
resolved_video_path = None
if isinstance(video_entry, str):
candidate_paths = []
video_path_obj = Path(video_entry)
if video_path_obj.is_absolute() and video_path_obj.exists():
resolved_video_path = video_path_obj
else:
if hasattr(self, "llavavid_video_root"):
base_root = Path(self.llavavid_video_root)
if cfg_name:
candidate_paths.append(base_root / cfg_name / video_entry)
candidate_paths.append(base_root / video_entry)
# Also allow treating the stored value as relative to current dir.
candidate_paths.append(Path(video_entry))
for candidate in candidate_paths:
if candidate.exists():
resolved_video_path = candidate
break
if resolved_video_path is None:
logger.warning(
"LLaVA-Video sample missing video file: %s (config=%s)",
video_entry,
cfg_name,
)
return None
if resolved_video_path.suffix.lower() == ".mkv":
logger.warning(
"LLaVA-Video skipping MKV file: %s (config=%s)",
resolved_video_path,
cfg_name,
)
return None
video_path = str(resolved_video_path)
else:
raise ValueError(f"Invalid dataset name: {self.dataset_name}. Available datasets: panda70m, webvid10m, openvid1m")
try:
frames = load_video_mp4(
video_path=video_path,
sample_method=self.sample_method,
num_frames=self.num_frames,
resolution=self.resolution,
transform=self.transform,
strict=False,
)
except Exception as exc:
logger.warning(
"LLaVA-Video sample failed to load (%s): %s",
video_path,
exc,
)
return None
if frames is None:
logger.warning(
"LLaVA-Video sample timed out while reading frames (%s); skipping sample.",
video_path,
)
return None
return {
"video": frames, # torch tensor (T, C, H, W)
"caption": caption # input_ids (seq_len); str
}
def download_video_url(url: str, save_path, timeout=10, max_retries=3) -> bool:
for attempt in range(1, max_retries + 1):
try:
with requests.get(url, stream=True, timeout=timeout) as r:
r.raise_for_status()
with open(save_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return True # Success
except Exception as e:
print(f"[Attempt {attempt}/{max_retries}] Download failed: {e}")
if attempt < max_retries:
sleep_time = 2 ** (attempt - 1) # exponential backoff: 1,2,4,8,...
time.sleep(sleep_time)
else:
return False # all attempts failed
return False
def load_video_mp4(
video_path,
sample_method: str = 'uniform',
num_frames: int = 8,
resolution: int = 256,
transform=None,
*,
per_frame_timeout: float = 1.5,
read_retry_interval: float = 0.05,
strict: bool = True,
):
"""
Load video frames and return them as a list of PIL images.
Args:
video_path: Path to the video file.
sample_method: Sampling method, 'uniform' or 'random'.
num_frames: Number of frames to sample from the video.
per_frame_timeout: Max seconds to block while seeking/reading a frame.
read_retry_interval: Delay between read retries while waiting for a frame.
strict: When False, return None on timeout/seek failure instead of raising.
Returns:
List[Image.Image] | None (if strict=False and a timeout/seek failure occurs)
"""
with open(os.devnull, "w") as devnull, contextlib.redirect_stderr(devnull):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise IOError(f"Could not open video file {video_path}")
if per_frame_timeout <= 0:
per_frame_timeout = 0.1
if read_retry_interval <= 0:
read_retry_interval = 0.01
def _read_frame_with_timeout(frame_index: Optional[int] = None):
deadline = time.monotonic() + per_frame_timeout
attempts = 0
while True:
if frame_index is not None:
cap.set(cv2.CAP_PROP_POS_FRAMES, int(frame_index))
ret, frame = cap.read()
if ret and frame is not None:
return frame
attempts += 1
if time.monotonic() >= deadline:
return None
time.sleep(min(read_retry_interval, max(deadline - time.monotonic(), 0.0)))
try:
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
except Exception:
frame_count = -1
if frame_count is None or frame_count <= 0:
# Fallback: attempt to read sequentially but stop early on failure
frames = []
try:
while len(frames) < num_frames:
frame = _read_frame_with_timeout()
if frame is None:
break
frames.append(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
finally:
cap.release()
if len(frames) < num_frames:
msg = f"Video {video_path} has insufficient frames ({len(frames)})."
if strict:
raise ValueError(msg)
logger.warning("%s Skipping sample.", msg)
return None
selected = frames
else:
if frame_count < num_frames:
cap.release()
msg = f"Video {video_path} has insufficient frames ({frame_count})."
if strict:
raise ValueError(msg)
logger.warning("%s Skipping sample.", msg)
return None
if sample_method == 'uniform':
indices = np.linspace(0, frame_count - 1, num_frames).astype(int)
elif sample_method == 'random':
indices = np.sort(np.random.choice(frame_count, num_frames, replace=False))
else:
cap.release()
raise ValueError(f"Sampling method {sample_method} not supported.")
selected = []
try:
for idx in indices:
frame = _read_frame_with_timeout(idx)
if frame is None:
msg = (
f"Timed out ({per_frame_timeout:.2f}s) seeking frame {idx} in {video_path}"
)
if strict:
raise TimeoutError(msg)
logger.warning("%s. Skipping sample.", msg)
return None
selected.append(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
finally:
cap.release()
sampled_frames = []
for frame in selected:
if transform:
frame = transform(frame, resolution=resolution)
sampled_frames.append(frame)
return sampled_frames
class VideoSpeechDataset(Dataset):
"""Loads paired video clips and speech audio paths or pre-tokenized speech."""
def __init__(
self,
*,
transform=None,
resolution: int = 256,
num_frames: int = 8,
video_root: str = "/home/work/AIDAS/data/video/openvid1m/video/video",
audio_root: str = "/home/work/AIDAS/data/video-speech",
speech_dir_name: str = "openvid-speech-trunc",
index_path: str = "/home/work/AIDAS/data/video-speech/openvid-speech.csv",
sample_method: str = "uniform",
precomputed_tokens_root: Optional[str] = None,
) -> None:
self.transform = transform
self.resolution = resolution
self.num_frames = num_frames
self.sample_method = sample_method or "uniform"
if self.sample_method not in {"uniform", "random"}:
logger.warning("Unknown sample_method '%s', defaulting to 'uniform'", self.sample_method)
self.sample_method = "uniform"
self.video_root = Path(video_root).expanduser().resolve()
audio_base = Path(audio_root).expanduser()
if speech_dir_name:
audio_base = audio_base / speech_dir_name
self.audio_root = audio_base.resolve()
self.index_path = Path(index_path).expanduser().resolve()
if not self.index_path.exists():
raise FileNotFoundError(f"VideoSpeechDataset index not found: {self.index_path}")
self.precomputed_tokens_root = (
Path(precomputed_tokens_root).expanduser().resolve()
if precomputed_tokens_root
else None
)
if self.precomputed_tokens_root is not None and not self.precomputed_tokens_root.exists():
logger.warning(
"Precomputed speech token root %s missing; falling back to raw audio paths.",
self.precomputed_tokens_root,
)
self.precomputed_tokens_root = None
self._samples: list[tuple[Path, Path]] = []
self._token_cache: Dict[str, torch.Tensor] = {}
self._token_cache_limit = 4096
self._load_index()
if not self._samples:
raise RuntimeError(f"VideoSpeechDataset found no valid samples in {self.index_path}")
def _load_index(self) -> None:
missing = 0
with self.index_path.open("r", newline="") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if not row:
continue
base = row[0].strip()
if not base:
continue
if base.lower().endswith(".wav"):
base = base[:-4]
video_path = self.video_root / f"{base}.mp4"
audio_path = self.audio_root / f"{base}.wav"
if not video_path.is_file() or not audio_path.is_file():
missing += 1
continue
self._samples.append((video_path, audio_path))
if missing:
logger.info(
"VideoSpeechDataset skipped %d entries missing media (index=%s)",
missing,
self.index_path,
)
def __len__(self) -> int:
return len(self._samples)
def _transform_frame(self, image: Image.Image, resolution: int) -> torch.Tensor:
if self.transform is None:
return utils_image_transform(image, resolution)
try:
return self.transform(image, resolution=resolution)
except TypeError:
return self.transform(image)
def _resolve_token_path(self, audio_path: Path) -> Optional[Path]:
if self.precomputed_tokens_root is None:
return None
digest = hashlib.sha1(os.path.abspath(str(audio_path)).encode("utf-8")).hexdigest()
return self.precomputed_tokens_root / digest[:2] / digest[2:4] / f"{digest}.pt"
def _get_precomputed_tokens(self, audio_path: Path) -> Optional[torch.Tensor]:
cache_key = os.path.abspath(str(audio_path))
cached = self._token_cache.get(cache_key)
if cached is not None:
return cached.clone()
token_path = self._resolve_token_path(audio_path)
if token_path is None or not token_path.exists():
return None
try:
tokens = torch.load(token_path, map_location="cpu")
except Exception as exc:
logger.warning("Failed to load precomputed speech tokens %s: %s", token_path, exc)
return None
if not isinstance(tokens, torch.Tensor):
return None
tokens = tokens.to(dtype=torch.long, copy=False)
if len(self._token_cache) < self._token_cache_limit:
self._token_cache[cache_key] = tokens
return tokens.clone()
def _prepare_speech_entry(self, audio_path: Path):
tokens = self._get_precomputed_tokens(audio_path)
if tokens is not None:
return tokens
return str(audio_path)
def __getitem__(self, idx: int) -> Dict[str, Any]:
video_path, audio_path = self._samples[idx]
frames = load_video_mp4(
str(video_path),
sample_method=self.sample_method,
num_frames=self.num_frames,
resolution=self.resolution,
transform=self._transform_frame,
)
speech_entry = self._prepare_speech_entry(audio_path)
return {
"video": frames,
"speech": speech_entry,
}
class TextImageInterleavedDataset:
"""
HF-backed dataset that yields rows of:
{
"image_paths": [str, ...], # absolute paths (no decoding)
"user_text": str,
"assistant_text": str,
}
"""
def __init__(
self,
*,
configs: Union[str, Sequence[str], None] = None, # default: all configs
split: str = "train",
data_root: str = "/home/work/AIDAS/data/TIGER-Lab/Mantis-Instruct",
max_images: Optional[int] = None,
filter_empty: bool = True,
resolution: int = 256,
# sampling controls
per_config_fraction: float = 1/7, # ← sample 1/7 PER CONFIG
sample_seed: int = 42,
# kept for compatibility, not used in this 1/7-per-config version
max_samples: Optional[int] = 1_000_000,
local_data_root: Optional[str] = None,
local_data_files: Optional[Dict[str, Any]] = None,
local_files_only: bool = False,
):
self.data_root = data_root
self.split = self._normalize_split(split)
self.max_images = max_images
self.filter_empty = filter_empty
self.resolution = resolution
self.local_data_root = local_data_root
self.local_data_files = local_data_files or {}
self._download_config = DownloadConfig(local_files_only=True) if local_files_only else None
# cache transforms
self._tfm_crop = transforms.Compose([
transforms.Resize(resolution, interpolation=transforms.InterpolationMode.BICUBIC),
transforms.CenterCrop((resolution, resolution)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]),
])
self._tfm_squash = transforms.Compose([
transforms.Resize((resolution, resolution), interpolation=transforms.InterpolationMode.BICUBIC),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]),
])
# ---- resolve configs ----
if configs is None or configs == "all":
cfgs = self._resolve_configs_from_local()
if not cfgs:
cfgs = sorted(get_dataset_config_names("TIGER-Lab/Mantis-Instruct"))
elif isinstance(configs, str):
cfgs = [configs]
else:
cfgs = list(configs)
self.configs = cfgs
rng = np.random.default_rng(sample_seed)
per_cfg_ds: List[HFDataset] = []
acc = Accelerator()
for cfg in cfgs:
base_ds = self._load_base_dataset(cfg, acc)
if base_ds is None:
continue
# --- SAMPLE 1/7 OF BASE ROWS *PER CONFIG* (before any map/expansion) ---
n = len(base_ds)
if n == 0:
continue
k = max(1, int(np.floor(n * per_config_fraction)))
# reproducible uniform sample without replacement
sel_idx = rng.choice(n, size=k, replace=False)
base_ds = base_ds.select(list(sel_idx))
# locate image dir for this (cfg, split)
img_dir = self._resolve_img_dir(cfg, self.split)
if img_dir is None:
raise FileNotFoundError(f"No image dir for config='{cfg}', split='{self.split}'")
# (1) attach constants
def add_const_cols(batch):
m = len(next(iter(batch.values()))) if batch else 0
return {"config": [cfg]*m, "img_dir": [img_dir]*m}
ds = base_ds.map(add_const_cols, batched=True)
# (2) normalize image column → absolute string paths
image_key = self._guess_image_key(ds.column_names)
def make_abs_paths(batch):
bases = batch["img_dir"] # list[str] per row
rels = batch[image_key] # per-row: list[dict]|dict|list[str]|str|None
def dict_to_rel(d: Dict[str, Any]) -> Optional[str]:
# typical HF Image: {"path": "...", "bytes": ...}
for k in ("path", "file_name", "filepath", "image_path", "name"):
v = d.get(k)
if isinstance(v, str) and v:
return v
# nested
img = d.get("image")
if isinstance(img, dict):
v = img.get("path")
if isinstance(v, str) and v:
return v
return None
out_paths = []
for base, r in zip(bases, rels):
# normalize r → list[str]
if r is None:
row = []
elif isinstance(r, str):
row = [r]
elif isinstance(r, dict):
s = dict_to_rel(r)
row = [s] if s else []
elif isinstance(r, list):
tmp = []
for x in r:
if isinstance(x, str):
tmp.append(x)
elif isinstance(x, dict):
s = dict_to_rel(x)
if s:
tmp.append(s)
row = tmp
else:
row = []
# join to absolute (keep absolute if already)
abs_paths = [p if os.path.isabs(p) else os.path.join(base, p) for p in row if isinstance(p, str)]
# cap if requested
if self.max_images is not None and len(abs_paths) > self.max_images:
abs_paths = abs_paths[: self.max_images]
out_paths.append(abs_paths)
return {"image_paths": out_paths}
ds = ds.map(make_abs_paths, batched=True)
# (3) expand conversation: one row per (user → assistant) turn
conv_key = "conversation"
def expand_turns(batch):
image_paths_list = batch["image_paths"]
conversations = batch.get(conv_key, [[]] * len(image_paths_list))
out_img_paths, out_user, out_assistant = [], [], []
for img_paths, conv in zip(image_paths_list, conversations):
conv = conv or []
# walk adjacent pairs
i = 0
while i < len(conv) - 1:
a, b = conv[i], conv[i + 1]
if (isinstance(a, dict) and isinstance(b, dict)
and a.get("role") == "user" and b.get("role") == "assistant"):
user_text = (a.get("content") or "").strip()
assistant_text = (b.get("content") or "").strip()
if (not self.filter_empty) or assistant_text:
out_img_paths.append(img_paths)
out_user.append(user_text)
out_assistant.append(assistant_text)
i += 2
else:
i += 1
return {
"image_paths": out_img_paths,
"user_text": out_user,
"assistant_text": out_assistant,
}
ds = ds.map(expand_turns, batched=True, remove_columns=ds.column_names)
if self.filter_empty:
ds = ds.filter(lambda e: bool(e["assistant_text"]))
per_cfg_ds.append(ds)
if not per_cfg_ds:
raise ValueError("Empty dataset after per-config sampling and preprocessing.")
self.dataset = concatenate_datasets(per_cfg_ds) if len(per_cfg_ds) > 1 else per_cfg_ds[0]
self.dataset = self.dataset.with_format("python")
print(f"[HF Dataset] per-config 1/7 sampled; configs={self.configs}, split='{self.split}', rows={len(self.dataset)}")
# ---- public API ----
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
start_idx = idx
attempts = 0
max_attempts = 10
while attempts < max_attempts:
ex = self.dataset[idx]
text = (
"<|start_header_id|>user<|end_header_id|>\n"
f"{ex['user_text']}\n"
"<eot_id><|start_header_id|>assistant<|end_header_id|>\n"
f"{ex['assistant_text']}"
)
paths = ex["image_paths"]
imgs: list[torch.Tensor] = []
for path in paths:
img = self._load_and_transform_one(path)
if img is not None:
imgs.append(img)
if imgs:
return {
"images": imgs,
"text": text,
}
attempts += 1
idx = (idx + 1) % len(self.dataset)
if idx == start_idx:
break
raise RuntimeError("TextImageInterleavedDataset: no valid images found after retries.")
# ---- helpers ----
@staticmethod
def _normalize_split(split: str) -> str:
s = split.lower()
return {"val": "validation", "dev": "validation"}.get(s, s)
def _resolve_img_dir(self, cfg: str, split: str) -> Optional[str]:
# Typical local layout:
# {data_root}/{cfg}/{split}_images
# {data_root}/{cfg}/images
cand1 = os.path.join(self.data_root, cfg, f"{split}_images")
cand2 = os.path.join(self.data_root, cfg, "images")
for c in (cand1, cand2):
if os.path.isdir(c):
return c
return None
def _load_and_transform_one(self, path: str):
try:
with Image.open(path) as im:
im = im.convert("RGB")
except FileNotFoundError:
return None
except Exception:
return None
return self._tfm_crop(im)
@staticmethod
def _guess_image_key(cols: List[str]) -> str:
for k in ("images", "image_paths", "imgs", "paths", "image"):
if k in cols:
return k
raise KeyError(f"Cannot find image column among {cols}")
def _resolve_configs_from_local(self) -> List[str]:
cfgs: List[str] = []
if self.local_data_root:
root = Path(self.local_data_root)
if root.is_dir():
for entry in sorted(root.iterdir()):
if not entry.is_dir():
continue
if self._has_split_data(entry):
cfgs.append(entry.name)
if not cfgs and self.local_data_files:
cfgs = [k for k in sorted(self.local_data_files.keys()) if k != "default"]
return cfgs
def _has_split_data(self, cfg_path: Path) -> bool:
split_dir = cfg_path / self.split
if split_dir.is_dir():
return True
alt_dirs = [
cfg_path / f"{self.split}.dataset",
cfg_path / f"{self.split}.arrow",
]
for candidate in alt_dirs:
if candidate.is_dir():
return True
patterns = [
cfg_path / self.split / "*.arrow",
cfg_path / self.split / "*.parquet",
cfg_path / f"{self.split}/*.arrow",
cfg_path / f"{self.split}/*.parquet",
cfg_path / f"{self.split}*.arrow",
cfg_path / f"{self.split}*.parquet",
]
for pattern in patterns:
if glob(str(pattern)):
return True
return False
def _load_base_dataset(self, cfg: str, acc: Accelerator) -> Optional[HFDataset]:
base_ds: Optional[HFDataset] = None
if self.local_data_root is not None:
# print(self.local_data_root)
base_ds = self._load_from_local_root(cfg)
if base_ds is None and self.local_data_files:
base_ds = self._load_from_local_data_files(cfg)
if base_ds is not None:
return base_ds
kwargs = {}
if self._download_config is not None:
kwargs["download_config"] = self._download_config
if acc.num_processes > 1:
acc.wait_for_everyone()
try:
base_ds = load_dataset(
"TIGER-Lab/Mantis-Instruct",
cfg,
split=self.split,
**kwargs,
)
except Exception as exc:
if self._download_config is not None:
raise RuntimeError(
f"Failed to load local dataset for config='{cfg}'. "
"Ensure that the dataset is cached or provide 'local_data_root'."
) from exc
raise
finally:
if acc.num_processes > 1:
acc.wait_for_everyone()
return base_ds
def _load_from_local_root(self, cfg: str) -> Optional[HFDataset]:
cfg_root = os.path.join(self.local_data_root, cfg)
if not os.path.exists(cfg_root):
return None
candidates = [
cfg_root,
os.path.join(cfg_root, self.split),
os.path.join(cfg_root, f"{self.split}.dataset"),
]
for path in candidates:
if not os.path.isdir(path):
continue
try:
loaded = load_from_disk(path)
if isinstance(loaded, DatasetDict):
if self.split in loaded:
return loaded[self.split]
continue
return loaded
except Exception:
continue
patterns = [
os.path.join(cfg_root, f"{self.split}.parquet"),
os.path.join(cfg_root, f"{self.split}/*.parquet"),
os.path.join(cfg_root, f"{self.split}_*.parquet"),
os.path.join(cfg_root, f"{self.split}.json"),
os.path.join(cfg_root, f"{self.split}.jsonl"),
os.path.join(cfg_root, f"{self.split}/*.jsonl"),
os.path.join(cfg_root, f"{self.split}.arrow"),
os.path.join(cfg_root, f"{self.split}/*.arrow"),
]
for pattern in patterns:
files = sorted(glob(pattern))
if files:
return self._load_from_files(files)
return None
def _load_from_local_data_files(self, cfg: str) -> Optional[HFDataset]:
spec = self.local_data_files.get(cfg) or self.local_data_files.get("default")
if spec is None:
return None
if isinstance(spec, str):
entries = [spec]
loader = None
elif isinstance(spec, dict):
loader = spec.get("type") or spec.get("loader") or spec.get("format")
files = spec.get(self.split) or spec.get("files")
if files is None:
return None
entries = files if isinstance(files, list) else [files]
else:
entries = list(spec)
loader = None
resolved_files: list[str] = []
for entry in entries:
if not entry:
continue
matched = sorted(glob(entry))
if matched:
resolved_files.extend(matched)
elif os.path.exists(entry):
resolved_files.append(entry)
if not resolved_files:
return None
return self._load_from_files(resolved_files, loader_hint=loader)
def _load_from_files(self, files: list[str], loader_hint: Optional[str] = None) -> Optional[HFDataset]:
if not files:
return None
ext = Path(files[0]).suffix.lower()
loader = loader_hint
if loader is None:
if ext in (".parquet",):
loader = "parquet"
elif ext in (".json", ".jsonl"):
loader = "json"
elif ext in (".arrow", ".feather"):
loader = "arrow"
if loader == "parquet":
return load_dataset("parquet", data_files={self.split: files}, split=self.split)
if loader in {"json", "jsonl"}:
return load_dataset("json", data_files={self.split: files}, split=self.split)
if loader == "arrow":
datasets = [HFDataset.from_file(path) for path in files]
return concatenate_datasets(datasets) if len(datasets) > 1 else datasets[0]
return None
class HFInstructionTextDataset(Dataset):
"""Mixed instruction-following text dataset sourced from multiple HF corpora."""
HF_SOURCES = (
{
"name": "openai/gsm8k",
"config": "main",
"split": "train",
"user_key": "question",
"assistant_key": "answer",
},
{
"name": "qwedsacf/grade-school-math-instructions",
"config": None,
"split": "train",
"user_key": "INSTRUCTION",
"assistant_key": "RESPONSE",
},
{
"name": "alespalla/chatbot_instruction_prompts",
"config": None,
"split": "train",
"user_key": "prompt",
"assistant_key": "response",
},
{
"name": "TIGER-Lab/MathInstruct",
"config": None,
"split": "train",
"user_key": "instruction",
"assistant_key": "output",
},
)
def __init__(
self,
*,
split: str = "train",
max_samples_per_source: Optional[int] = None,
max_total_samples: Optional[int] = None,
seed: int = 42,
) -> None:
self.split = split
self.seed = seed
self.samples: List[str] = []
rng = random.Random(seed)
for source in self.HF_SOURCES:
desired_split = source.get("split", split)
try:
dataset_name = source["name"]
dataset_config = source.get("config")
if dataset_config is not None:
hf_ds = load_dataset(dataset_name, dataset_config, split=desired_split)
else:
hf_ds = load_dataset(dataset_name, split=desired_split)
except Exception as exc:
print(f"[HFInstructionTextDataset] Failed to load {source['name']}: {exc}")
continue
if max_samples_per_source is not None and len(hf_ds) > max_samples_per_source:
hf_ds = hf_ds.shuffle(seed=seed).select(range(max_samples_per_source))
user_key = source["user_key"]
assistant_key = source["assistant_key"]
for example in hf_ds:
user_raw = str(example.get(user_key, "")).strip()
assistant_raw = str(example.get(assistant_key, "")).strip()
if not user_raw or not assistant_raw:
continue
formatted = self._format_dialogue(user_raw, assistant_raw)
if formatted:
self.samples.append(formatted)
if not self.samples:
raise ValueError("HFInstructionTextDataset loaded zero valid samples.")
rng.shuffle(self.samples)
if max_total_samples is not None:
self.samples = self.samples[: max_total_samples]
@staticmethod
def _format_dialogue(user_text: str, assistant_text: str) -> str:
return (
"<|start_header_id|>user<|end_header_id|>\n"
f"{user_text}\n"
"<|eot_id><|start_header_id|>assistant<|end_header_id|>\n"
f"{assistant_text}"
)
def __len__(self) -> int:
return len(self.samples)
def __getitem__(self, index: int) -> Dict[str, str]:
return {"input_ids": self.samples[index]}
@staticmethod
def collate_fn(batch: List[Dict[str, str]]) -> Dict[str, List[str]]:
return {"input_ids": [example["input_ids"] for example in batch]}
class TextToImage2MDataset(Dataset):
"""Loads jackyhate/text-to-image-2M for text-to-image fine-tuning."""
def __init__(
self,
split: str = "train",
resolution: int = 256,
dataset_name: str = "jackyhate/text-to-image-2M",
cache_dir: str | None = None,
local_files_only: bool = False,
) -> None:
self.resolution = resolution
self.dataset_name = dataset_name
self.cache_dir = cache_dir
self.local_files_only = local_files_only
download_cfg = None
if local_files_only:
download_cfg = DownloadConfig(local_files_only=True)
self._dataset = load_dataset(
dataset_name,
split=split,
cache_dir=cache_dir,
download_config=download_cfg,
)
def __len__(self) -> int:
return len(self._dataset)
def __getitem__(self, idx: int) -> Dict[str, Any]:
sample = self._dataset[idx]
prompt = None
json_meta = sample.get("json")
if isinstance(json_meta, dict):
prompt = json_meta.get("prompt")
if prompt is None:
prompt = sample.get("prompt", "")
image_field = sample.get("jpg") or sample.get("image")
if image_field is None:
raise KeyError("Expected image field 'jpg' in text-to-image-2M sample")
if isinstance(image_field, Image.Image):
image = image_field.convert("RGB")
elif isinstance(image_field, bytes):
image = Image.open(BytesIO(image_field)).convert("RGB")
else:
image = Image.fromarray(np.array(image_field)).convert("RGB")
image_tensor = utils_image_transform(image, self.resolution)
return {
"input_prompt": prompt,
"output_prompt": None,
"edit_prompt": None,
"inverse_prompt": None,
"input_image": image_tensor,
"output_image": image_tensor,
}
class HQEditX2IDataset(Dataset):
def __init__(
self,
split: str = "train",
resolution: int = 256,
dataset_name: str = "UCSC-VLAA/HQ-Edit",
cache_dir: str = "/home/work/AIDAS/huggingface/datasets",
):
self.resolution = resolution
self.cache_dir = cache_dir # retained for API compatibility
self._dataset = load_dataset(dataset_name, split=split)
def __len__(self) -> int:
return len(self._dataset)
def __getitem__(self, idx: int) -> Dict[str, Any]:
sample = self._dataset[idx]
input_tensor = utils_image_transform(sample['input_image'].convert("RGB"), self.resolution)
output_tensor = utils_image_transform(sample['output_image'].convert("RGB"), self.resolution)
return {
"input_prompt": sample["input"],
"output_prompt": sample["output"],
"edit_prompt": sample["edit"],
"inverse_prompt": sample["inverse_edit"],
"input_image": input_tensor,
"output_image": output_tensor,
}
class CombinedX2IDataset(Dataset):
"""Round-robin combination of multiple x2i-style datasets."""
def __init__(self, datasets: Sequence[Dataset]):
if not datasets:
raise ValueError("CombinedX2IDataset requires at least one dataset.")
self.datasets = list(datasets)
self.lengths = [len(ds) for ds in self.datasets]
if any(length == 0 for length in self.lengths):
raise ValueError("Underlying x2i dataset has zero length.")
self.cumulative = list(itertools.accumulate(self.lengths))
self.total_length = self.cumulative[-1]
def __len__(self) -> int:
return self.total_length
def __getitem__(self, idx: int) -> Dict[str, Any]:
if idx < 0 or idx >= self.total_length:
raise IndexError(f"Index {idx} out of bounds for CombinedX2IDataset of length {self.total_length}")
dataset_idx = bisect.bisect_right(self.cumulative, idx)
prev = self.cumulative[dataset_idx - 1] if dataset_idx > 0 else 0
local_idx = idx - prev
return self.datasets[dataset_idx][local_idx]
class OpenImageI2IDataset(Dataset):
"""
Image-to-image dataset built from local Open Images edit JSONL files.
Supports three JSONL schemas:
* SFT-style single turn edits (text + output_image + local_input_image)
* Preference data; only positive edits (output_image) are used by default
* Multi-turn edits which are flattened into single-turn pairs
"""
def __init__(
self,
resolution: int = 256,
image_root: str | None = None,
sft_jsonl: Union[str, Sequence[str], None] = None,
pref_jsonl: Union[str, Sequence[str], None] = None,
multi_turn_jsonl: Union[str, Sequence[str], None] = None,
prefer_summarized_text: bool = True,
pref_positive_only: bool = True,
skip_missing: bool = True,
max_samples_per_source: int | None = None,
max_total_samples: int | None = None,
seed: int | None = None,
) -> None:
self.resolution = resolution
self.image_root = image_root
self.prefer_summarized_text = prefer_summarized_text
self.pref_positive_only = pref_positive_only
self.skip_missing = skip_missing
self._rng = random.Random(seed if seed is not None else 0)
self._per_source_limit = self._coerce_positive_int(max_samples_per_source)
self._total_limit = self._coerce_positive_int(max_total_samples)
self._samples: list[dict[str, str]] = []
self._stats: dict[str, int] = {
"sft": 0,
"pref": 0,
"multi_turn": 0,
"missing_paths": 0,
"invalid_records": 0,
}
sft_paths = self._coerce_paths(sft_jsonl)
pref_paths = self._coerce_paths(pref_jsonl)
multi_turn_paths = self._coerce_paths(multi_turn_jsonl)
for path in sft_paths:
self._samples.extend(self._load_single_turn_file(path, source_key="sft"))
for path in pref_paths:
if not self.pref_positive_only:
logger.warning("OpenImageI2IDataset currently only supports positive preference pairs.")
self._samples.extend(self._load_single_turn_file(path, source_key="pref"))
for path in multi_turn_paths:
self._samples.extend(self._load_multi_turn_file(path))
if self._total_limit is not None and len(self._samples) > self._total_limit:
self._rng.shuffle(self._samples)
self._samples = self._samples[: self._total_limit]
if not self._samples:
raise ValueError("OpenImageI2IDataset could not load any valid examples.")
logger.info(
"Loaded %d OpenImage i2i samples (sft=%d, pref=%d, multi_turn=%d, missing_paths=%d, invalid=%d).",
len(self._samples),
self._stats["sft"],
self._stats["pref"],
self._stats["multi_turn"],
self._stats["missing_paths"],
self._stats["invalid_records"],
)
def __len__(self) -> int:
return len(self._samples)
def __getitem__(self, idx: int) -> Dict[str, Any]:
record = self._samples[idx]
input_image = Image.open(record["input_path"]).convert("RGB")
target_image = Image.open(record["target_path"]).convert("RGB")
input_tensor = utils_image_transform(input_image, self.resolution)
target_tensor = utils_image_transform(target_image, self.resolution)
prompt = record["prompt"]
return {
"input_prompt": prompt,
"output_prompt": None,
"edit_prompt": prompt,
"inverse_prompt": None,
"input_image": input_tensor,
"output_image": target_tensor,
}
def _load_single_turn_file(self, path: str, *, source_key: str) -> list[dict[str, str]]:
file_path = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(file_path):
logger.warning("OpenImageI2IDataset: JSONL file not found: %s", file_path)
return []
base_dir = os.path.dirname(file_path)
samples: list[dict[str, str]] = []
with open(file_path, "r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError:
self._stats["invalid_records"] += 1
continue
prompt = self._select_prompt(record)
input_path = self._resolve_path(record.get("local_input_image"), base_dir=base_dir)
output_path = self._resolve_path(record.get("output_image"), base_dir=base_dir)
sample = self._build_sample(prompt, input_path, output_path)
if sample:
samples.append(sample)
if self._per_source_limit is not None and len(samples) > self._per_source_limit:
self._rng.shuffle(samples)
samples = samples[: self._per_source_limit]
self._stats[source_key] += len(samples)
return samples
def _load_multi_turn_file(self, path: str) -> list[dict[str, str]]:
file_path = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(file_path):
logger.warning("OpenImageI2IDataset: JSONL file not found: %s", file_path)
return []
base_dir = os.path.dirname(file_path)
samples: list[dict[str, str]] = []
with open(file_path, "r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError:
self._stats["invalid_records"] += 1
continue
multi_samples = self._expand_multi_turn(record, base_dir=base_dir)
if multi_samples:
samples.extend(multi_samples)
if self._per_source_limit is not None and len(samples) > self._per_source_limit:
self._rng.shuffle(samples)
samples = samples[: self._per_source_limit]
self._stats["multi_turn"] += len(samples)
return samples
def _expand_multi_turn(self, record: dict, *, base_dir: str) -> list[dict[str, str]]:
prompts = record.get("metadata_edit_turn_prompts") or []
files = record.get("files") or []
if not prompts or not files:
self._stats["invalid_records"] += 1
return []
outputs: dict[int, str] = {}
final_image: str | None = None
for entry in files:
file_id = entry.get("id")
url = entry.get("url")
if not file_id or not url:
continue
if file_id.startswith("edit_turn"):
try:
idx = int(file_id.replace("edit_turn", "").strip())
except ValueError:
continue
outputs[idx] = url
elif file_id == "final_image":
final_image = url
current_input = self._resolve_path(record.get("local_input_image"), base_dir=base_dir)
if not current_input:
return []
samples: list[dict[str, str]] = []
for turn_idx, prompt in enumerate(prompts, start=1):
target_rel = outputs.get(turn_idx)
if target_rel is None:
if turn_idx == len(prompts):
target_rel = final_image
else:
break
target_path = self._resolve_path(target_rel, base_dir=base_dir)
if not target_path:
break
sample = self._build_sample(prompt, current_input, target_path)
if not sample:
break
samples.append(sample)
current_input = target_path
return samples
def _select_prompt(self, record: dict) -> str | None:
if self.prefer_summarized_text and record.get("summarized_text"):
return record.get("summarized_text")
if record.get("text"):
return record.get("text")
return record.get("metadata_edit_turn_prompt")
def _build_sample(self, prompt: str | None, input_path: str | None, target_path: str | None) -> dict[str, str] | None:
if not prompt:
self._stats["invalid_records"] += 1
return None
if not input_path or not target_path:
return None
return {
"prompt": str(prompt).strip(),
"input_path": input_path,
"target_path": target_path,
}
def _resolve_path(self, path: str | None, *, base_dir: str | None = None) -> str | None:
if not path or path.startswith("http://") or path.startswith("https://"):
return None
candidates: list[str] = []
normalized = path.replace("\\", "/")
if os.path.isabs(normalized):
candidates.append(os.path.normpath(normalized))
else:
if self.image_root:
candidates.append(os.path.normpath(os.path.join(self.image_root, normalized)))
if base_dir:
candidates.append(os.path.normpath(os.path.join(base_dir, normalized)))
for candidate in candidates:
if not self.skip_missing or os.path.exists(candidate):
return candidate
self._stats["missing_paths"] += 1
return None
def _coerce_paths(self, value: Union[str, Sequence[str], None]) -> list[str]:
if value is None:
return []
if isinstance(value, str):
values = [value]
else:
values = [item for item in value if item]
return [os.path.abspath(os.path.expanduser(path)) for path in values]
@staticmethod
def _coerce_positive_int(value: Any) -> int | None:
if value is None:
return None
try:
int_value = int(value)
except (TypeError, ValueError):
return None
return int_value if int_value > 0 else None
# import os, socket
# from typing import Optional
# def _dist_identity():
# """Return a dict with rank info from env/torch if available."""
# info = {}
# # Env fallbacks for different launchers (torchrun/SLURM/MPI)
# def _get(*keys) -> Optional[int]:
# for k in keys:
# v = os.environ.get(k)
# if v is not None:
# try:
# return int(v)
# except ValueError:
# return None
# return None
# info["rank"] = _get("RANK", "SLURM_PROCID", "OMPI_COMM_WORLD_RANK")
# info["local_rank"] = _get("LOCAL_RANK", "SLURM_LOCALID", "MPI_LOCALRANKID")
# info["node_rank"] = _get("NODE_RANK", "SLURM_NODEID")
# info["world_size"] = _get("WORLD_SIZE", "SLURM_NTASKS", "OMPI_COMM_WORLD_SIZE")
# info["hostname"] = socket.gethostname()
# info["pid"] = os.getpid()
# # Optional: torch.distributed status
# try:
# import torch.distributed as dist
# info["dist_initialized"] = dist.is_available() and dist.is_initialized()
# if info["dist_initialized"]:
# info["rank"] = dist.get_rank()
# info["world_size"] = dist.get_world_size()
# info["backend"] = dist.get_backend()
# except Exception:
# info["dist_initialized"] = False
# # Optional: DataLoader worker ID
# try:
# from torch.utils.data import get_worker_info
# wi = get_worker_info()
# info["worker_id"] = wi.id if wi is not None else None
# except Exception:
# info["worker_id"] = None
# return info
# def _whoami_str():
# i = _dist_identity()
# return (
# f"[PROC] rank={i['rank']} local_rank={i['local_rank']} node_rank={i['node_rank']} "
# f"world={i['world_size']} worker={i['worker_id']} "
# f"host={i['hostname']} pid={i['pid']} "
# f"{'(backend='+i['backend']+')' if i.get('backend') else ''}"
# )
if __name__ == '__main__':
pass
|