Spaces:
Running
on
Zero
Running
on
Zero
File size: 114,782 Bytes
11e6f7b |
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 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 |
import os
import kiui
from kiui.op import recenter
import collections
import math
import time
import itertools
import pickle
from typing import Any
import lmdb
import cv2
import imageio
import numpy as np
from PIL import Image
import Imath
import OpenEXR
from pdb import set_trace as st
from pathlib import Path
import torchvision
from nsr.camera_utils import generate_input_camera
from einops import rearrange, repeat
from functools import partial
import io
import gzip
import random
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from torch.utils.data.distributed import DistributedSampler
from pathlib import Path
import lz4.frame
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
from utils.general_utils import PILtoTorch, matrix_to_quaternion
from guided_diffusion import logger
import json
import webdataset as wds
from .shapenet import LMDBDataset, LMDBDataset_MV_Compressed, decompress_and_open_image_gzip, decompress_array
from kiui.op import safe_normalize
from utils.gs_utils.graphics_utils import getWorld2View2, getProjectionMatrix, getView2World
def fov2focal(fov, pixels):
return pixels / (2 * math.tan(fov / 2))
def focal2fov(focal, pixels):
return 2 * math.atan(pixels / (2 * focal))
def resize_depth_mask(depth_to_resize, resolution):
depth_resized = cv2.resize(depth_to_resize, (resolution, resolution),
interpolation=cv2.INTER_LANCZOS4)
# interpolation=cv2.INTER_AREA)
return depth_resized, depth_resized > 0 # type: ignore
def resize_depth_mask_Tensor(depth_to_resize, resolution):
if depth_to_resize.shape[-1] != resolution:
depth_resized = torch.nn.functional.interpolate(
input=depth_to_resize.unsqueeze(1),
size=(resolution, resolution),
# mode='bilinear',
mode='nearest',
align_corners=False,
).squeeze(1)
else:
depth_resized = depth_to_resize
return depth_resized, depth_resized > 0 # type: ignore
def load_dataset(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
# shuffle=True,
num_workers=6,
load_depth=False,
preprocess=None,
imgnet_normalize=True,
dataset_size=-1,
trainer_name='input_rec',
use_lmdb=False,
use_wds=False,
use_lmdb_compressed=False,
infi_sampler=True):
# st()
# dataset_cls = {
# 'input_rec': MultiViewDataset,
# 'nv': NovelViewDataset,
# }[trainer_name]
# st()
if use_wds:
return load_wds_data(file_path, reso, reso_encoder, batch_size,
num_workers)
if use_lmdb:
logger.log('using LMDB dataset')
# dataset_cls = LMDBDataset_MV # 2.5-3iter/s, but unstable, drops to 1 later.
if use_lmdb_compressed:
if 'nv' in trainer_name:
dataset_cls = Objv_LMDBDataset_NV_Compressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
dataset_cls = Objv_LMDBDataset_MV_Compressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
if 'nv' in trainer_name:
dataset_cls = Objv_LMDBDataset_NV_NoCompressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
dataset_cls = Objv_LMDBDataset_MV_NoCompressed # 2.5-3iter/s, but unstable, drops to 1 later.
# dataset = dataset_cls(file_path)
else:
if 'nv' in trainer_name:
dataset_cls = NovelViewObjverseDataset
else:
dataset_cls = MultiViewObjverseDataset # 1.5-2iter/s
dataset = dataset_cls(file_path,
reso,
reso_encoder,
test=False,
preprocess=preprocess,
load_depth=load_depth,
imgnet_normalize=imgnet_normalize,
dataset_size=dataset_size)
logger.log('dataset_cls: {}, dataset size: {}'.format(
trainer_name, len(dataset)))
loader = DataLoader(dataset,
batch_size=batch_size,
num_workers=num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=num_workers > 0,
shuffle=False)
return loader
def load_data(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
# shuffle=True,
num_workers=6,
load_depth=False,
preprocess=None,
imgnet_normalize=True,
dataset_size=-1,
trainer_name='input_rec',
use_lmdb=False,
use_wds=False,
use_lmdb_compressed=False,
plucker_embedding=False,
infi_sampler=True):
if use_lmdb:
logger.log('using LMDB dataset')
# dataset_cls = LMDBDataset_MV # 2.5-3iter/s, but unstable, drops to 1 later.
if use_lmdb_compressed:
if 'nv' in trainer_name:
dataset_cls = Objv_LMDBDataset_NV_Compressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
dataset_cls = Objv_LMDBDataset_MV_Compressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
if 'nv' in trainer_name:
dataset_cls = Objv_LMDBDataset_NV_NoCompressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
dataset_cls = Objv_LMDBDataset_MV_NoCompressed # 2.5-3iter/s, but unstable, drops to 1 later.
else:
if 'nv' in trainer_name:
dataset_cls = NovelViewObjverseDataset # 1.5-2iter/s
else:
dataset_cls = MultiViewObjverseDataset
dataset = dataset_cls(file_path,
reso,
reso_encoder,
test=False,
preprocess=preprocess,
load_depth=load_depth,
imgnet_normalize=imgnet_normalize,
dataset_size=dataset_size,
plucker_embedding=plucker_embedding)
logger.log('dataset_cls: {}, dataset size: {}'.format(
trainer_name, len(dataset)))
# st()
if infi_sampler:
train_sampler = DistributedSampler(dataset=dataset,
shuffle=True,
drop_last=True)
loader = DataLoader(dataset,
batch_size=batch_size,
num_workers=num_workers,
drop_last=True,
pin_memory=True,
persistent_workers=num_workers > 0,
sampler=train_sampler)
while True:
yield from loader
# else:
# # loader = DataLoader(dataset,
# # batch_size=batch_size,
# # num_workers=num_workers,
# # drop_last=False,
# # pin_memory=True,
# # persistent_workers=num_workers > 0,
# # shuffle=False)
# st()
# return dataset
def load_eval_data(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
num_workers=1,
load_depth=False,
preprocess=None,
imgnet_normalize=True,
interval=1,
use_lmdb=False,
plucker_embedding=False,
load_real=False,
four_view_for_latent=False,
shuffle_across_cls=False,
load_extra_36_view=False,
gs_cam_format=False,
single_view_for_i23d=False,
**kwargs,
):
if use_lmdb:
logger.log('using LMDB dataset')
dataset_cls = Objv_LMDBDataset_MV_Compressed # 2.5-3iter/s, but unstable, drops to 1 later.
dataset = dataset_cls(file_path,
reso,
reso_encoder,
test=True,
preprocess=preprocess,
load_depth=load_depth,
imgnet_normalize=imgnet_normalize,
interval=interval)
elif load_real:
dataset = RealDataset(file_path,
# dataset = RealMVDataset(file_path,
reso,
reso_encoder,
preprocess=preprocess,
load_depth=load_depth,
test=True,
imgnet_normalize=imgnet_normalize,
interval=interval,
plucker_embedding=plucker_embedding)
else:
dataset = MultiViewObjverseDataset(
file_path,
reso,
reso_encoder,
preprocess=preprocess,
load_depth=load_depth,
test=True,
imgnet_normalize=imgnet_normalize,
interval=interval,
plucker_embedding=plucker_embedding,
four_view_for_latent=four_view_for_latent,
load_extra_36_view=load_extra_36_view,
shuffle_across_cls=shuffle_across_cls,
gs_cam_format=gs_cam_format,
single_view_for_i23d=single_view_for_i23d,
)
print('eval dataset size: {}'.format(len(dataset)))
# train_sampler = DistributedSampler(dataset=dataset)
loader = DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
drop_last=False,
shuffle=False,
)
# sampler=train_sampler)
return loader
def load_data_for_lmdb(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
# shuffle=True,
num_workers=6,
load_depth=False,
preprocess=None,
imgnet_normalize=True,
dataset_size=-1,
trainer_name='input_rec',
shuffle_across_cls=False,
four_view_for_latent=False,
wds_split=1):
# st()
# dataset_cls = {
# 'input_rec': MultiViewDataset,
# 'nv': NovelViewDataset,
# }[trainer_name]
# if 'nv' in trainer_name:
# dataset_cls = NovelViewDataset
# else:
# dataset_cls = MultiViewDataset
# st()
dataset_cls = MultiViewObjverseDatasetforLMDB
dataset = dataset_cls(file_path,
reso,
reso_encoder,
test=False,
preprocess=preprocess,
load_depth=load_depth,
imgnet_normalize=imgnet_normalize,
dataset_size=dataset_size,
shuffle_across_cls=shuffle_across_cls,
wds_split=wds_split,
four_view_for_latent=four_view_for_latent)
logger.log('dataset_cls: {}, dataset size: {}'.format(
trainer_name, len(dataset)))
# train_sampler = DistributedSampler(dataset=dataset, shuffle=True, drop_last=True)
loader = DataLoader(
dataset,
shuffle=False,
batch_size=batch_size,
num_workers=num_workers,
drop_last=False,
# prefetch_factor=2,
# prefetch_factor=3,
pin_memory=True,
persistent_workers=num_workers > 0,
)
# sampler=train_sampler)
# while True:
# yield from loader
return loader, dataset.dataset_name, len(dataset)
def load_lmdb_for_lmdb(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
# shuffle=True,
num_workers=6,
load_depth=False,
preprocess=None,
imgnet_normalize=True,
dataset_size=-1,
trainer_name='input_rec'):
# st()
# dataset_cls = {
# 'input_rec': MultiViewDataset,
# 'nv': NovelViewDataset,
# }[trainer_name]
# if 'nv' in trainer_name:
# dataset_cls = NovelViewDataset
# else:
# dataset_cls = MultiViewDataset
# st()
dataset_cls = Objv_LMDBDataset_MV_Compressed_for_lmdb
dataset = dataset_cls(file_path,
reso,
reso_encoder,
test=False,
preprocess=preprocess,
load_depth=load_depth,
imgnet_normalize=imgnet_normalize,
dataset_size=dataset_size)
logger.log('dataset_cls: {}, dataset size: {}'.format(
trainer_name, len(dataset)))
# train_sampler = DistributedSampler(dataset=dataset, shuffle=True, drop_last=True)
loader = DataLoader(
dataset,
shuffle=False,
batch_size=batch_size,
num_workers=num_workers,
drop_last=False,
prefetch_factor=2,
# prefetch_factor=3,
pin_memory=True,
persistent_workers=True,
)
# sampler=train_sampler)
# while True:
# yield from loader
return loader, len(dataset)
def load_memory_data(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
num_workers=1,
# load_depth=True,
preprocess=None,
imgnet_normalize=True,
**kwargs):
# load a single-instance into the memory to speed up training IO
# dataset = MultiViewObjverseDataset(file_path,
dataset = NovelViewObjverseDataset(file_path,
reso,
reso_encoder,
preprocess=preprocess,
load_depth=True,
test=False,
overfitting=True,
imgnet_normalize=imgnet_normalize,
overfitting_bs=batch_size,
**kwargs)
logger.log('!!!!!!! memory dataset size: {} !!!!!!'.format(len(dataset)))
# train_sampler = DistributedSampler(dataset=dataset)
loader = DataLoader(
dataset,
batch_size=len(dataset),
num_workers=num_workers,
drop_last=False,
shuffle=False,
)
all_data: dict = next(
iter(loader)
) # torchvision.utils.save_image(all_data['img'], 'gt.jpg', normalize=True, value_range=(-1,1))
if kwargs.get('gs_cam_format', False): # gs rendering pipeline
# ! load V=4 images for training in a batch.
while True:
# indices = torch.randperm(len(dataset))[:4]
indices = torch.randperm(
len(dataset))[:batch_size] # all instances
# indices2 = torch.randperm(len(dataset))[:] # all instances
batch_c = collections.defaultdict(dict)
for k in ['c', 'nv_c']:
for k_c, v_c in all_data[k].items():
batch_c[k][k_c] = torch.index_select(
v_c, dim=0, index=indices).reshape(
batch_size //
4, 4, *v_c.shape[1:]).float() if isinstance(
v_c, torch.Tensor) else v_c.float() # float
batch_c['c']['tanfov'] = batch_c['c']['tanfov'][0][0].item()
batch_c['nv_c']['tanfov'] = batch_c['nv_c']['tanfov'][0][0].item()
batch_data = {}
for k, v in all_data.items():
if k not in ['c', 'nv_c']:
batch_data[k] = torch.index_select(
v, dim=0, index=indices).float() if isinstance(
v, torch.Tensor) else v # float
yield {
**batch_data,
**batch_c,
}
else:
while True:
start_idx = np.random.randint(0, len(dataset) - batch_size + 1)
yield {
k: v[start_idx:start_idx + batch_size]
for k, v in all_data.items()
}
def read_dnormal(normald_path, cond_pos, h=None, w=None):
cond_cam_dis = np.linalg.norm(cond_pos, 2)
near = 0.867 #sqrt(3) * 0.5
near_distance = cond_cam_dis - near
normald = cv2.imread(normald_path, cv2.IMREAD_UNCHANGED).astype(np.float32)
depth = normald[..., 3:]
depth[depth < near_distance] = 0
if h is not None:
assert w is not None
# depth = cv2.resize(depth, (h, w)) # 512,512, 1 -> self.reso, self.reso
depth = cv2.resize(depth, (h, w), interpolation=cv2.INTER_NEAREST
) # 512,512, 1 -> self.reso, self.reso
else:
depth = depth[..., 0]
return torch.from_numpy(depth).float()
def get_intri(target_im=None, h=None, w=None, normalize=False):
if target_im is None:
assert (h is not None and w is not None)
else:
h, w = target_im.shape[:2]
fx = fy = 1422.222
res_raw = 1024
f_x = f_y = fx * h / res_raw
K = np.array([f_x, 0, w / 2, 0, f_y, h / 2, 0, 0, 1]).reshape(3, 3)
if normalize: # center is [0.5, 0.5], eg3d renderer tradition
K[:6] /= h
# print("intr: ", K)
return K
def convert_pose(C2W):
# https://github.com/modelscope/richdreamer/blob/c3d9a77fa15fc42dbae12c2d41d64aaec14efd37/dataset/gobjaverse/depth_warp_example.py#L402
flip_yz = np.eye(4)
flip_yz[1, 1] = -1
flip_yz[2, 2] = -1
C2W = np.matmul(C2W, flip_yz)
return torch.from_numpy(C2W)
def read_camera_matrix_single(json_file):
with open(json_file, 'r', encoding='utf8') as reader:
json_content = json.load(reader)
'''
# NOTE that different from unity2blender experiments.
camera_matrix = np.eye(4)
camera_matrix[:3, 0] = np.array(json_content['x'])
camera_matrix[:3, 1] = -np.array(json_content['y'])
camera_matrix[:3, 2] = -np.array(json_content['z'])
camera_matrix[:3, 3] = np.array(json_content['origin'])
'''
camera_matrix = np.eye(4) # blender-based
camera_matrix[:3, 0] = np.array(json_content['x'])
camera_matrix[:3, 1] = np.array(json_content['y'])
camera_matrix[:3, 2] = np.array(json_content['z'])
camera_matrix[:3, 3] = np.array(json_content['origin'])
# print(camera_matrix)
# '''
# return convert_pose(camera_matrix)
return camera_matrix
def unity2blender(normal):
normal_clone = normal.copy()
normal_clone[..., 0] = -normal[..., -1]
normal_clone[..., 1] = -normal[..., 0]
normal_clone[..., 2] = normal[..., 1]
return normal_clone
def blender2midas(img):
'''Blender: rub
midas: lub
'''
img[..., 0] = -img[..., 0]
img[..., 1] = -img[..., 1]
img[..., -1] = -img[..., -1]
return img
def current_milli_time():
return round(time.time() * 1000)
# modified from ShapeNet class
class MultiViewObjverseDataset(Dataset):
def __init__(
self,
file_path,
reso,
reso_encoder,
preprocess=None,
classes=False,
load_depth=False,
test=False,
scene_scale=1,
overfitting=False,
imgnet_normalize=True,
dataset_size=-1,
overfitting_bs=-1,
interval=1,
plucker_embedding=False,
shuffle_across_cls=False,
wds_split=1, # 4 splits to accelerate preprocessing
four_view_for_latent=False,
single_view_for_i23d=False,
load_extra_36_view=False,
gs_cam_format=False,
**kwargs):
self.load_extra_36_view = load_extra_36_view
# st()
self.gs_cam_format = gs_cam_format
self.four_view_for_latent = four_view_for_latent # export 0 12 30 36, 4 views for reconstruction
self.single_view_for_i23d = single_view_for_i23d
self.file_path = file_path
self.overfitting = overfitting
self.scene_scale = scene_scale
self.reso = reso
self.reso_encoder = reso_encoder
self.classes = False
self.load_depth = load_depth
self.preprocess = preprocess
self.plucker_embedding = plucker_embedding
self.intrinsics = get_intri(h=self.reso, w=self.reso,
normalize=True).reshape(9)
assert not self.classes, "Not support class condition now."
dataset_name = Path(self.file_path).stem.split('_')[0]
self.dataset_name = dataset_name
self.zfar = 100.0
self.znear = 0.01
# if test:
# self.ins_list = sorted(os.listdir(self.file_path))[0:1] # the first 1 instance for evaluation reference.
# else:
# ! TODO, read from list?
def load_single_cls_instances(file_path):
ins_list = [] # the first 1 instance for evaluation reference.
for dict_dir in os.listdir(file_path)[:]:
for ins_dir in os.listdir(os.path.join(file_path, dict_dir)):
# self.ins_list.append(os.path.join(self.file_path, dict_dir, ins_dir,))
ins_list.append(
os.path.join(file_path, dict_dir, ins_dir,
'campos_512_v4'))
return ins_list
if shuffle_across_cls:
self.ins_list = []
# for subset in ['Animals', 'Transportations_tar', 'Furnitures']:
# for subset in ['Furnitures']:
# selected subset for training
for subset in [ # ! around 17W instances in total. MVImageNet is the next thing to deal with? Later.
# 'daily-used',
# 'Food',
# 'Plants',
# 'Electronics',
# 'BuildingsOutdoor',
# 'Human-Shape',
'Animals',
# 'Transportations_tar',
# 'Furnitures',
]: # selected subset for training
self.ins_list += load_single_cls_instances(
os.path.join(self.file_path, subset))
# st()
current_time = int(current_milli_time()
) # randomly shuffle given current time
random.seed(current_time)
random.shuffle(self.ins_list)
else: # preprocess single class
self.ins_list = load_single_cls_instances(self.file_path)
self.ins_list = sorted(self.ins_list)
# if test:
# self.ins_list = self.ins_list[0:1]
if overfitting:
self.ins_list = self.ins_list[:1]
self.rgb_list = []
self.pose_list = []
self.depth_list = []
self.data_ins_list = []
self.instance_data_length = -1
with open(
# '/mnt/yslan/objaverse/richdreamer/dataset/text_captions_cap3d.json',
'./datasets/text_captions_cap3d.json',
) as f:
self.caption_data = json.load(f)
self.shuffle_across_cls = shuffle_across_cls
# for ins in self.ins_list[47000:]:
if four_view_for_latent:
self.wds_split_all = 1 # ! when dumping latent
ins_list_to_process = self.ins_list
else:
self.wds_split_all = 4
# self.wds_split_all = 8 # ! 8 cls in total
all_ins_size = len(self.ins_list)
ratio_size = all_ins_size // self.wds_split_all + 1
ins_list_to_process = self.ins_list[ratio_size *
(wds_split - 1):ratio_size *
wds_split]
# st()
for ins in ins_list_to_process:
# ins = os.path.join(
# # self.file_path, ins , 'campos_512_v4'
# self.file_path, ins ,
# # 'compos_512_v4'
# )
# cur_rgb_path = os.path.join(self.file_path, ins, 'compos_512_v4')
# cur_pose_path = os.path.join(self.file_path, ins, 'pose')
# st()
# ][:27])
if self.four_view_for_latent:
# ! v=6 version infer latent
cur_all_fname = [f'{idx:05d}' for idx in [25,0,9,18,27,33]]
elif self.single_view_for_i23d:
cur_all_fname = [f'{idx:05d}'
for idx in [2]] # ! furniture side view
else:
cur_all_fname = [t.split('.')[0] for t in os.listdir(ins)
] # use full set for training
if shuffle_across_cls:
random.seed(current_time)
random.shuffle(cur_all_fname)
else:
cur_all_fname = sorted(cur_all_fname)
if self.instance_data_length == -1:
self.instance_data_length = len(cur_all_fname)
else:
try: # data missing?
assert len(cur_all_fname) == self.instance_data_length
except:
with open('missing_ins.txt', 'a') as f:
f.write(str(Path(ins.parent)) +
'\n') # remove the "campos_512_v4"
continue
self.pose_list += ([
os.path.join(ins, fname, fname + '.json')
for fname in cur_all_fname
])
self.rgb_list += ([
os.path.join(ins, fname, fname + '.png')
for fname in cur_all_fname
])
self.depth_list += ([
os.path.join(ins, fname, fname + '_nd.exr')
for fname in cur_all_fname
])
self.data_ins_list += ([ins] * len(cur_all_fname))
# check
# ! setup normalizataion
transformations = [
transforms.ToTensor(), # [0,1] range
]
if imgnet_normalize:
transformations.append(
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)) # type: ignore
)
else:
transformations.append(
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))) # type: ignore
self.normalize = transforms.Compose(transformations)
def get_source_cw2wT(self, source_cameras_view_to_world):
return matrix_to_quaternion(
source_cameras_view_to_world[:3, :3].transpose(0, 1))
def c_to_3dgs_format(self, pose):
# TODO, switch to torch version (batched later)
c2w = pose[:16].reshape(4, 4) # 3x4
# ! load cam
w2c = np.linalg.inv(c2w)
R = np.transpose(
w2c[:3, :3]) # R is stored transposed due to 'glm' in CUDA code
T = w2c[:3, 3]
fx = pose[16]
FovX = focal2fov(fx, 1)
FovY = focal2fov(fx, 1)
tanfovx = math.tan(FovX * 0.5)
tanfovy = math.tan(FovY * 0.5)
assert tanfovx == tanfovy
trans = np.array([0.0, 0.0, 0.0])
scale = 1.0
world_view_transform = torch.tensor(getWorld2View2(R, T, trans,
scale)).transpose(
0, 1)
projection_matrix = getProjectionMatrix(znear=self.znear,
zfar=self.zfar,
fovX=FovX,
fovY=FovY).transpose(0, 1)
full_proj_transform = (world_view_transform.unsqueeze(0).bmm(
projection_matrix.unsqueeze(0))).squeeze(0)
camera_center = world_view_transform.inverse()[3, :3]
view_world_transform = torch.tensor(getView2World(R, T, trans,
scale)).transpose(
0, 1)
# item.update(viewpoint_cam=[viewpoint_cam])
c = {}
c["source_cv2wT_quat"] = self.get_source_cw2wT(view_world_transform)
c.update(
# projection_matrix=projection_matrix, # K
cam_view=world_view_transform, # world_view_transform
cam_view_proj=full_proj_transform, # full_proj_transform
cam_pos=camera_center,
tanfov=tanfovx, # TODO, fix in the renderer
# orig_c2w=c2w,
# orig_w2c=w2c,
orig_pose=torch.from_numpy(pose),
orig_c2w=torch.from_numpy(c2w),
orig_w2c=torch.from_numpy(w2c),
# tanfovy=tanfovy,
)
return c # dict for gs rendering
def __len__(self):
return len(self.rgb_list)
def load_bbox(self, mask):
# st()
nonzero_value = torch.nonzero(mask)
height, width = nonzero_value.max(dim=0)[0]
top, left = nonzero_value.min(dim=0)[0]
bbox = torch.tensor([top, left, height, width], dtype=torch.float32)
return bbox
def __getitem__(self, idx):
# try:
data = self._read_data(idx)
return data
# except Exception as e:
# with open('error_log.txt', 'a') as f:
# f.write(str(e) + '\n')
# with open('error_idx.txt', 'a') as f:
# f.write(str(self.data_ins_list[idx]) + '\n')
# print(e, flush=True)
# return {}
def gen_rays(self, c2w):
# Generate rays
self.h = self.reso_encoder
self.w = self.reso_encoder
yy, xx = torch.meshgrid(
torch.arange(self.h, dtype=torch.float32) + 0.5,
torch.arange(self.w, dtype=torch.float32) + 0.5,
indexing='ij')
# normalize to 0-1 pixel range
yy = yy / self.h
xx = xx / self.w
# K = np.array([f_x, 0, w / 2, 0, f_y, h / 2, 0, 0, 1]).reshape(3, 3)
cx, cy, fx, fy = self.intrinsics[2], self.intrinsics[
5], self.intrinsics[0], self.intrinsics[4]
# cx *= self.w
# cy *= self.h
# f_x = f_y = fx * h / res_raw
c2w = torch.from_numpy(c2w).float()
xx = (xx - cx) / fx
yy = (yy - cy) / fy
zz = torch.ones_like(xx)
dirs = torch.stack((xx, yy, zz), dim=-1) # OpenCV convention
dirs /= torch.norm(dirs, dim=-1, keepdim=True)
dirs = dirs.reshape(-1, 3, 1)
del xx, yy, zz
# st()
dirs = (c2w[None, :3, :3] @ dirs)[..., 0]
origins = c2w[None, :3, 3].expand(self.h * self.w, -1).contiguous()
origins = origins.view(self.h, self.w, 3)
dirs = dirs.view(self.h, self.w, 3)
return origins, dirs
def _read_data(self, idx):
rgb_fname = self.rgb_list[idx]
pose_fname = self.pose_list[idx]
raw_img = imageio.imread(rgb_fname)
# ! RGBD
alpha_mask = raw_img[..., -1:] / 255
raw_img = alpha_mask * raw_img[..., :3] + (
1 - alpha_mask) * np.ones_like(raw_img[..., :3]) * 255
raw_img = raw_img.astype(
np.uint8) # otherwise, float64 won't call ToTensor()
# return raw_img
# st()
if self.preprocess is None:
img_to_encoder = cv2.resize(raw_img,
(self.reso_encoder, self.reso_encoder),
interpolation=cv2.INTER_LANCZOS4)
# interpolation=cv2.INTER_AREA)
img_to_encoder = img_to_encoder[
..., :3] #[3, reso_encoder, reso_encoder]
img_to_encoder = self.normalize(img_to_encoder)
else:
img_to_encoder = self.preprocess(Image.open(rgb_fname)) # clip
# return img_to_encoder
img = cv2.resize(raw_img, (self.reso, self.reso),
interpolation=cv2.INTER_LANCZOS4)
# interpolation=cv2.INTER_AREA)
# img_sr = cv2.resize(raw_img, (512, 512), interpolation=cv2.INTER_AREA)
# img_sr = cv2.resize(raw_img, (256, 256), interpolation=cv2.INTER_AREA) # just as refinement, since eg3d uses 64->128 final resolution
# img_sr = cv2.resize(raw_img, (128, 128), interpolation=cv2.INTER_AREA) # just as refinement, since eg3d uses 64->128 final resolution
# img_sr = cv2.resize(
# raw_img, (128, 128), interpolation=cv2.INTER_LANCZOS4
# ) # just as refinement, since eg3d uses 64->128 final resolution
# img = torch.from_numpy(img)[..., :3].permute(
# 2, 0, 1) / 255.0 #[3, reso, reso]
img = torch.from_numpy(img)[..., :3].permute(
2, 0, 1
) / 127.5 - 1 #[3, reso, reso], normalize to [-1,1], follow triplane range
# img_sr = torch.from_numpy(img_sr)[..., :3].permute(
# 2, 0, 1
# ) / 127.5 - 1 #[3, reso, reso], normalize to [-1,1], follow triplane range
c2w = read_camera_matrix_single(pose_fname) #[1, 4, 4] -> [1, 16]
# c = np.concatenate([c2w, self.intrinsics], axis=0).reshape(25) # 25, no '1' dim needed.
# return c2w
# if self.load_depth:
# depth, depth_mask, depth_mask_sr = read_dnormal(self.depth_list[idx],
# try:
depth = read_dnormal(self.depth_list[idx], c2w[:3, 3:], self.reso,
self.reso)
# return depth
# except:
# # print(self.depth_list[idx])
# raise NotImplementedError(self.depth_list[idx])
# if depth
# try:
bbox = self.load_bbox(depth > 0)
# except:
# print(rgb_fname)
# return {}
# st()
# plucker
rays_o, rays_d = self.gen_rays(c2w)
rays_plucker = torch.cat([torch.cross(rays_o, rays_d, dim=-1), rays_d],
dim=-1) # [h, w, 6]
img_to_encoder = torch.cat(
[img_to_encoder, rays_plucker.permute(2, 0, 1)],
0).float() # concat in C dim
# ! add depth as input
normalized_depth = read_dnormal(self.depth_list[idx], c2w[:3, 3:],
self.reso_encoder,
self.reso_encoder).unsqueeze(0)
# normalized_depth = depth.unsqueeze(0) # min=0
img_to_encoder = torch.cat([img_to_encoder, normalized_depth],
0) # concat in C dim
c = np.concatenate([c2w.reshape(16), self.intrinsics],
axis=0).reshape(25).astype(
np.float32) # 25, no '1' dim needed.
if self.gs_cam_format:
c = self.c_to_3dgs_format(c)
else:
c = torch.from_numpy(c)
ret_dict = {
# 'rgb_fname': rgb_fname,
'img_to_encoder': img_to_encoder,
'img': img,
'c': c,
# 'img_sr': img_sr,
# 'ins_name': self.data_ins_list[idx]
}
ins = str(
(Path(self.data_ins_list[idx]).relative_to(self.file_path)).parent)
if self.shuffle_across_cls:
caption = self.caption_data['/'.join(ins.split('/')[1:])]
else:
caption = self.caption_data[ins]
ret_dict.update({
'depth': depth,
'depth_mask': depth > 0,
# 'depth_mask_sr': depth_mask_sr,
'bbox': bbox,
'caption': caption,
'rays_plucker': rays_plucker, # cam embedding used in lgm
'ins': ins, # placeholder
})
return ret_dict
class RealDataset(Dataset):
def __init__(
self,
file_path,
reso,
reso_encoder,
preprocess=None,
classes=False,
load_depth=False,
test=False,
scene_scale=1,
overfitting=False,
imgnet_normalize=True,
dataset_size=-1,
overfitting_bs=-1,
interval=1,
plucker_embedding=False,
shuffle_across_cls=False,
wds_split=1, # 4 splits to accelerate preprocessing
) -> None:
super().__init__()
self.file_path = file_path
self.overfitting = overfitting
self.scene_scale = scene_scale
self.reso = reso
self.reso_encoder = reso_encoder
self.classes = False
self.load_depth = load_depth
self.preprocess = preprocess
self.plucker_embedding = plucker_embedding
self.rgb_list = []
all_fname = [
t for t in os.listdir(self.file_path)
if t.split('.')[1] in ['png', 'jpg']
]
all_fname = [name for name in all_fname if '-input' in name ]
self.rgb_list += ([
os.path.join(self.file_path, fname) for fname in all_fname
])
# if len(self.rgb_list) == 1:
# # placeholder
# self.rgb_list = self.rgb_list * 40
# ! setup normalizataion
transformations = [
transforms.ToTensor(), # [0,1] range
]
assert imgnet_normalize
if imgnet_normalize:
transformations.append(
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)) # type: ignore
)
else:
transformations.append(
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))) # type: ignore
self.normalize = transforms.Compose(transformations)
# camera = torch.load('eval_pose.pt', map_location='cpu')
# self.eval_camera = camera
# pre-cache
# self.calc_rays_plucker()
def __len__(self):
return len(self.rgb_list)
def __getitem__(self, index) -> Any:
# return super().__getitem__(index)
rgb_fname = self.rgb_list[index]
# ! preprocess, normalize
raw_img = imageio.imread(rgb_fname)
# interpolation=cv2.INTER_AREA)
if raw_img.shape[-1] == 4:
alpha_mask = raw_img[..., 3:4] / 255.0
bg_white = np.ones_like(alpha_mask) * 255.0
raw_img = raw_img[..., :3] * alpha_mask + (
1 - alpha_mask) * bg_white #[3, reso_encoder, reso_encoder]
raw_img = raw_img.astype(np.uint8)
# raw_img = recenter(raw_img, np.ones_like(raw_img), border_ratio=0.2)
# log gt
img = cv2.resize(raw_img, (self.reso, self.reso),
interpolation=cv2.INTER_LANCZOS4)
img = torch.from_numpy(img)[..., :3].permute(
2, 0, 1
) / 127.5 - 1 #[3, reso, reso], normalize to [-1,1], follow triplane range
ret_dict = {
# 'rgb_fname': rgb_fname,
# 'img_to_encoder':
# img_to_encoder.unsqueeze(0).repeat_interleave(40, 0),
'img': img,
# 'c': self.eval_camera, # TODO, get pre-calculated samples
# 'ins': 'placeholder',
# 'bbox': 'placeholder',
# 'caption': 'placeholder',
}
# ! repeat as a intance
return ret_dict
class RealMVDataset(Dataset):
def __init__(
self,
file_path,
reso,
reso_encoder,
preprocess=None,
classes=False,
load_depth=False,
test=False,
scene_scale=1,
overfitting=False,
imgnet_normalize=True,
dataset_size=-1,
overfitting_bs=-1,
interval=1,
plucker_embedding=False,
shuffle_across_cls=False,
wds_split=1, # 4 splits to accelerate preprocessing
) -> None:
super().__init__()
self.file_path = file_path
self.overfitting = overfitting
self.scene_scale = scene_scale
self.reso = reso
self.reso_encoder = reso_encoder
self.classes = False
self.load_depth = load_depth
self.preprocess = preprocess
self.plucker_embedding = plucker_embedding
self.rgb_list = []
all_fname = [
t for t in os.listdir(self.file_path)
if t.split('.')[1] in ['png', 'jpg']
]
# all_fname = [name for name in all_fname if '-input' in name ]
# all_fname = [name for name in all_fname if 'sorting_board-input' in name ]
all_fname = [name for name in all_fname if 'teasure_chest-input' in name ]
# all_fname = [name for name in all_fname if 'bubble_mart_blue-input' in name ]
# all_fname = [name for name in all_fname if 'chair_comfort-input' in name ]
self.rgb_list += ([
os.path.join(self.file_path, fname) for fname in all_fname
])
# if len(self.rgb_list) == 1:
# # placeholder
# self.rgb_list = self.rgb_list * 40
# ! setup normalizataion
transformations = [
transforms.ToTensor(), # [0,1] range
]
# load zero123pp pose
# '''
camera = torch.load('assets/objv_eval_pose.pt', map_location='cpu') # 40, 25
# v=6 render
# zero123pp_pose = torch.load('assets/input_cameras.pt', map_location='cpu')[0]
# zero123pp_pose = torch.load('assets/input_cameras_1.5.pt', map_location='cpu')[0]
# V = zero123pp_pose.shape[0]
# zero123pp_pose = torch.cat([zero123pp_pose[:, :12], # change radius to 1.5 here
# torch.Tensor([0,0,0,1]).reshape(1,4).repeat_interleave(V, 0).to(camera),
# camera[:V, 16:]],
# 1)
azimuths = np.array([30, 90, 150, 210, 270, 330]).astype(float)
elevations = np.array([20, -10, 20, -10, 20, -10]).astype(float)
# zero123pp_pose, _ = generate_input_camera(1.6, [[elevations[i], azimuths[i]] for i in range(6)], fov=30)
zero123pp_pose, _ = generate_input_camera(1.8, [[elevations[i], azimuths[i]] for i in range(6)], fov=30)
K = torch.Tensor([1.3889, 0.0000, 0.5000, 0.0000, 1.3889, 0.5000, 0.0000, 0.0000, 0.0039]).to(zero123pp_pose) # keeps the same
# st()
zero123pp_pose = torch.cat([zero123pp_pose.reshape(6,-1), K.unsqueeze(0).repeat(6,1)], dim=-1)
# ! directly adopt gt input
# self.indices = np.array([0,2,4,5])
# eval_camera = zero123pp_pose[self.indices]
# self.eval_camera = torch.cat([torch.zeros_like(eval_camera[0:1]),eval_camera], 0) # first c not used as condition here, just placeholder
# ! adopt mv-diffusion output as input.
# self.indices = np.array([1,0,2,4,5])
self.indices = np.array([0,1,2,3,4,5])
eval_camera = zero123pp_pose[self.indices].float().cpu().numpy() # for normalization
# eval_camera = zero123pp_pose[self.indices]
# self.eval_camera = eval_camera
# self.eval_camera = torch.cat([torch.zeros_like(eval_camera[0:1]),eval_camera], 0) # first c not used as condition here, just placeholder
# # * normalize here
# self.eval_camera = self.normalize_camera(eval_camera, eval_camera[0:1]) # the first img is not used.
# ! no normalization required
self.eval_camera = eval_camera
# st()
# self.eval_camera = self.eval_camera + np.random.randn(*self.eval_camera.shape) * 0.04 - 0.02
# '''
# self.eval_camera = torch.load('/nas/shared/V2V/yslan/logs/nips24/LSGM/t23d/FM/9cls/i23d/dit-XL2-MV-ampFast/gpu1-batch10-lr0-bf16-qknorm-fixinpRange-debug/2590000_c.pt')[1:6].float().cpu().numpy()
# self.eval_camera = self.normalize_camera(self.eval_camera, self.eval_camera[0:1])
# self.eval_camera = self.eval_camera + np.random.randn(*self.eval_camera.shape) * 0.04 - 0.02
# pass
# FIXME check how sensitive it is: requries training augmentation.
# self.eval_camera = torch.load('/nas/shared/V2V/yslan/logs/nips24/LSGM/t23d/FM/9cls/i23d/dit-XL2-MV-ampFast/gpu1-batch10-lr0-bf16-qknorm-fixinpRange-debug/2590000_c.pt')[1:]
def normalize_camera(self, c, c_frame0):
# assert c.shape[0] == self.chunk_size # 8 o r10
B = c.shape[0]
camera_poses = c[:, :16].reshape(B, 4, 4) # 3x4
canonical_camera_poses = c_frame0[:, :16].reshape(1, 4, 4)
inverse_canonical_pose = np.linalg.inv(canonical_camera_poses)
inverse_canonical_pose = np.repeat(inverse_canonical_pose, B, 0)
cam_radius = np.linalg.norm(
c_frame0[:, :16].reshape(1, 4, 4)[:, :3, 3],
axis=-1,
keepdims=False) # since g-buffer adopts dynamic radius here.
frame1_fixed_pos = np.repeat(np.eye(4)[None], 1, axis=0)
frame1_fixed_pos[:, 2, -1] = -cam_radius
transform = frame1_fixed_pos @ inverse_canonical_pose
new_camera_poses = np.repeat(
transform, 1, axis=0
) @ camera_poses # [V, 4, 4]. np.repeat() is th.repeat_interleave()
c = np.concatenate([new_camera_poses.reshape(B, 16), c[:, 16:]],
axis=-1)
return c
def __len__(self):
return len(self.rgb_list)
def __getitem__(self, index) -> Any:
# return super().__getitem__(index)
rgb_fname = self.rgb_list[index]
# ! if loading training imgs
# raw_img = imageio.imread(rgb_fname)
# if raw_img.shape[-1] == 4:
# alpha_mask = raw_img[..., 3:4] / 255.0
# bg_white = np.ones_like(alpha_mask) * 255.0
# raw_img = raw_img[..., :3] * alpha_mask + (
# 1 - alpha_mask) * bg_white #[3, reso_encoder, reso_encoder]
# raw_img = raw_img.astype(np.uint8)
# img = rearrange(raw_img, 'h (n w) c -> n c h w', n=6)[self.indices]
# img = torch.from_numpy(img) / 127.5 - 1 #
# '''
# img = cv2.resize(raw_img, (320,320),
# interpolation=cv2.INTER_LANCZOS4) # for easy concat
# img = torch.from_numpy(img)[..., :3].permute(
# 2, 0, 1
# ) / 127.5 - 1 #[3, reso, reso], normalize to [-1,1], follow triplane range
# img = rearrange(raw_img, 'h (n w) c -> n c h w', n=6)[1:6] # ! if loading gt inp views
# '''
# ! if loading mv-diff output views
mv_img = imageio.imread(rgb_fname.replace('-input', ''))
# st()
# mv_img = rearrange(mv_img, '(n h) (m w) c -> (n m) c h w', n=3, m=2) # (6, 3, 320, 320)
mv_img = rearrange(mv_img, '(n h) (m w) c -> (n m) h w c', n=3, m=2)[self.indices] # (6, 3, 320, 320)
mv_img = np.stack([recenter(img, np.ones_like(img), border_ratio=0.2) for img in mv_img], axis=0)
# mv_img = np.stack([recenter(img, np.ones_like(img), border_ratio=0.3) for img in mv_img], axis=0)
# mv_img = np.stack([recenter(img, np.ones_like(img), border_ratio=0.1) for img in mv_img], axis=0)
# mv_img = np.stack([recenter(img, np.ones_like(img), border_ratio=0.4) for img in mv_img], axis=0)
mv_img = rearrange(mv_img, 'b h w c -> b c h w') # to torch tradition
mv_img = torch.from_numpy(mv_img) / 127.5 - 1
# img = torch.cat([img[None], mv_img], 0)
img = mv_img # ! directly adopt output views for reconstruction/generation
# '''
ret_dict = {
'img': img,
'c': self.eval_camera
}
return ret_dict
class NovelViewObjverseDataset(MultiViewObjverseDataset):
"""novel view prediction version.
"""
def __init__(self,
file_path,
reso,
reso_encoder,
preprocess=None,
classes=False,
load_depth=False,
test=False,
scene_scale=1,
overfitting=False,
imgnet_normalize=True,
dataset_size=-1,
overfitting_bs=-1,
**kwargs):
super().__init__(file_path, reso, reso_encoder, preprocess, classes,
load_depth, test, scene_scale, overfitting,
imgnet_normalize, dataset_size, overfitting_bs,
**kwargs)
def __getitem__(self, idx):
input_view = super().__getitem__(
idx) # get previous input view results
# get novel view of the same instance
novel_view = super().__getitem__(
(idx // self.instance_data_length) * self.instance_data_length +
random.randint(0, self.instance_data_length - 1))
# assert input_view['ins_name'] == novel_view['ins_name'], 'should sample novel view from the same instance'
input_view.update({f'nv_{k}': v for k, v in novel_view.items()})
return input_view
class MultiViewObjverseDatasetforLMDB(MultiViewObjverseDataset):
def __init__(
self,
file_path,
reso,
reso_encoder,
preprocess=None,
classes=False,
load_depth=False,
test=False,
scene_scale=1,
overfitting=False,
imgnet_normalize=True,
dataset_size=-1,
overfitting_bs=-1,
shuffle_across_cls=False,
wds_split=1,
four_view_for_latent=False,
):
super().__init__(file_path,
reso,
reso_encoder,
preprocess,
classes,
load_depth,
test,
scene_scale,
overfitting,
imgnet_normalize,
dataset_size,
overfitting_bs,
shuffle_across_cls=shuffle_across_cls,
wds_split=wds_split,
four_view_for_latent=four_view_for_latent)
assert self.reso == 256
with open(
'/cpfs01/shared/V2V/V2V_hdd/yslan/aigc3d/text_captions_cap3d.json'
) as f:
self.caption_data = json.load(f)
lmdb_path = '/cpfs01/user/yangpeiqing.p/yslan/data/Furnitures_uncompressed/'
# with open(os.path.join(lmdb_path, 'idx_to_ins_mapping.json')) as f:
# self.idx_to_ins_mapping = json.load(f)
def __len__(self):
return super().__len__()
# return 100 # for speed debug
def __getitem__(self, idx):
# ret_dict = super().__getitem__(idx)
rgb_fname = self.rgb_list[idx]
pose_fname = self.pose_list[idx]
raw_img = imageio.imread(rgb_fname) # [..., :3]
# assert raw_img.shape[-1] == 4
if raw_img.shape[-1] == 4: # ! set bg to white
alpha_mask = raw_img[..., -1:] / 255 # [0,1]
raw_img = alpha_mask * raw_img[..., :3] + (
1 - alpha_mask) * np.ones_like(raw_img[..., :3]) * 255
raw_img = raw_img.astype(np.uint8)
raw_img = cv2.resize(raw_img, (self.reso, self.reso),
interpolation=cv2.INTER_LANCZOS4)
c2w = read_camera_matrix_single(pose_fname) #[1, 4, 4] -> [1, 16]
c = np.concatenate([c2w.reshape(16), self.intrinsics],
axis=0).reshape(25).astype(
np.float32) # 25, no '1' dim needed.
c = torch.from_numpy(c)
# c = np.concatenate([c2w, self.intrinsics], axis=0).reshape(25) # 25, no '1' dim needed.
# if self.load_depth:
# depth, depth_mask, depth_mask_sr = read_dnormal(self.depth_list[idx],
# try:
depth = read_dnormal(self.depth_list[idx], c2w[:3, 3:], self.reso,
self.reso)
# except:
# # print(self.depth_list[idx])
# raise NotImplementedError(self.depth_list[idx])
# if depth
# try:
bbox = self.load_bbox(depth > 0)
ins = str(
(Path(self.data_ins_list[idx]).relative_to(self.file_path)).parent)
if self.shuffle_across_cls:
caption = self.caption_data['/'.join(ins.split('/')[1:])]
else:
caption = self.caption_data[ins]
ret_dict = {
'raw_img': raw_img,
'c': c,
'depth': depth,
# 'depth_mask': depth_mask, # 64x64 here?
'bbox': bbox,
'ins': ins,
'caption': caption,
# 'fname': rgb_fname,
}
return ret_dict
class Objv_LMDBDataset_MV_Compressed(LMDBDataset_MV_Compressed):
def __init__(self,
lmdb_path,
reso,
reso_encoder,
imgnet_normalize=True,
dataset_size=-1,
test=False,
**kwargs):
super().__init__(lmdb_path,
reso,
reso_encoder,
imgnet_normalize,
dataset_size=dataset_size,
**kwargs)
self.instance_data_length = 40 # ! could save some key attributes in LMDB
if test:
self.length = self.instance_data_length
elif dataset_size > 0:
self.length = dataset_size * self.instance_data_length
# load caption data, and idx-to-ins mapping
with open(
'/cpfs01/shared/V2V/V2V_hdd/yslan/aigc3d/text_captions_cap3d.json'
) as f:
self.caption_data = json.load(f)
with open(os.path.join(lmdb_path, 'idx_to_ins_mapping.json')) as f:
self.idx_to_ins_mapping = json.load(f)
def _load_data(self, idx):
# '''
raw_img, depth, c, bbox = self._load_lmdb_data(idx)
# raw_img, depth, c, bbox = self._load_lmdb_data_no_decompress(idx)
# resize depth and bbox
caption = self.caption_data[self.idx_to_ins_mapping[str(idx)]]
return {
**self._post_process_sample(raw_img, depth),
'c': c,
'bbox': (bbox * (self.reso / 512.0)).astype(np.uint8),
# 'bbox': (bbox*(self.reso/256.0)).astype(np.uint8), # TODO, double check 512 in wds?
'caption': caption
}
# '''
# raw_img, depth, c, bbox = self._load_lmdb_data_no_decompress(idx)
# st()
# return {}
def __getitem__(self, idx):
return self._load_data(idx)
class Objv_LMDBDataset_MV_NoCompressed(Objv_LMDBDataset_MV_Compressed):
def __init__(self,
lmdb_path,
reso,
reso_encoder,
imgnet_normalize=True,
dataset_size=-1,
test=False,
**kwargs):
super().__init__(lmdb_path, reso, reso_encoder, imgnet_normalize,
dataset_size, test, **kwargs)
def _load_data(self, idx):
# '''
raw_img, depth, c, bbox = self._load_lmdb_data_no_decompress(idx)
# resize depth and bbox
caption = self.caption_data[self.idx_to_ins_mapping[str(idx)]]
return {
**self._post_process_sample(raw_img, depth), 'c': c,
'bbox': (bbox * (self.reso / 512.0)).astype(np.uint8),
'caption': caption
}
return {}
class Objv_LMDBDataset_NV_NoCompressed(Objv_LMDBDataset_MV_NoCompressed):
def __init__(self,
lmdb_path,
reso,
reso_encoder,
imgnet_normalize=True,
dataset_size=-1,
test=False,
**kwargs):
super().__init__(lmdb_path, reso, reso_encoder, imgnet_normalize,
dataset_size, test, **kwargs)
def __getitem__(self, idx):
input_view = self._load_data(idx) # get previous input view results
# get novel view of the same instance
try:
novel_view = self._load_data(
(idx // self.instance_data_length) *
self.instance_data_length +
random.randint(0, self.instance_data_length - 1))
except Exception as e:
raise NotImplementedError(idx)
# assert input_view['ins_name'] == novel_view['ins_name'], 'should sample novel view from the same instance'
input_view.update({f'nv_{k}': v for k, v in novel_view.items()})
return input_view
class Objv_LMDBDataset_MV_Compressed_for_lmdb(LMDBDataset_MV_Compressed):
def __init__(self,
lmdb_path,
reso,
reso_encoder,
imgnet_normalize=True,
dataset_size=-1,
test=False,
**kwargs):
super().__init__(lmdb_path,
reso,
reso_encoder,
imgnet_normalize,
dataset_size=dataset_size,
**kwargs)
self.instance_data_length = 40 # ! could save some key attributes in LMDB
if test:
self.length = self.instance_data_length
elif dataset_size > 0:
self.length = dataset_size * self.instance_data_length
# load caption data, and idx-to-ins mapping
with open(
'/cpfs01/shared/V2V/V2V_hdd/yslan/aigc3d/text_captions_cap3d.json'
) as f:
self.caption_data = json.load(f)
with open(os.path.join(lmdb_path, 'idx_to_ins_mapping.json')) as f:
self.idx_to_ins_mapping = json.load(f)
# def _load_data(self, idx):
# # '''
# raw_img, depth, c, bbox = self._load_lmdb_data(idx)
# # resize depth and bbox
# caption = self.caption_data[self.idx_to_ins_mapping[str(idx)]]
# # st()
# return {
# **self._post_process_sample(raw_img, depth), 'c': c,
# 'bbox': (bbox*(self.reso/512.0)).astype(np.uint8),
# 'caption': caption
# }
# # '''
# # raw_img, depth, c, bbox = self._load_lmdb_data_no_decompress(idx)
# # st()
# # return {}
def load_bbox(self, mask):
# st()
nonzero_value = torch.nonzero(mask)
height, width = nonzero_value.max(dim=0)[0]
top, left = nonzero_value.min(dim=0)[0]
bbox = torch.tensor([top, left, height, width], dtype=torch.float32)
return bbox
def __getitem__(self, idx):
raw_img, depth, c, bbox = self._load_lmdb_data(idx)
return {'raw_img': raw_img, 'depth': depth, 'c': c, 'bbox': bbox}
class Objv_LMDBDataset_NV_Compressed(Objv_LMDBDataset_MV_Compressed):
def __init__(self,
lmdb_path,
reso,
reso_encoder,
imgnet_normalize=True,
dataset_size=-1,
**kwargs):
super().__init__(lmdb_path, reso, reso_encoder, imgnet_normalize,
dataset_size, **kwargs)
def __getitem__(self, idx):
input_view = self._load_data(idx) # get previous input view results
# get novel view of the same instance
try:
novel_view = self._load_data(
(idx // self.instance_data_length) *
self.instance_data_length +
random.randint(0, self.instance_data_length - 1))
except Exception as e:
raise NotImplementedError(idx)
# assert input_view['ins_name'] == novel_view['ins_name'], 'should sample novel view from the same instance'
input_view.update({f'nv_{k}': v for k, v in novel_view.items()})
return input_view
#
# test tar loading
def load_wds_ResampledShard(file_path,
batch_size,
num_workers,
reso,
reso_encoder,
test=False,
preprocess=None,
imgnet_normalize=True,
plucker_embedding=False,
decode_encode_img_only=False,
load_instance=False,
mv_input=False,
split_chunk_input=False,
duplicate_sample=True,
append_depth=False,
gs_cam_format=False,
orthog_duplicate=False,
**kwargs):
# return raw_img, depth, c, bbox, sample_pyd['ins.pyd'], sample_pyd['fname.pyd']
class PostProcess:
def __init__(
self,
reso,
reso_encoder,
imgnet_normalize,
plucker_embedding,
decode_encode_img_only,
mv_input,
split_chunk_input,
duplicate_sample,
append_depth,
gs_cam_format,
orthog_duplicate,
) -> None:
self.gs_cam_format = gs_cam_format
self.append_depth = append_depth
self.plucker_embedding = plucker_embedding
self.decode_encode_img_only = decode_encode_img_only
self.duplicate_sample = duplicate_sample
self.orthog_duplicate = orthog_duplicate
self.zfar = 100.0
self.znear = 0.01
transformations = []
if not split_chunk_input:
transformations.append(transforms.ToTensor())
if imgnet_normalize:
transformations.append(
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)) # type: ignore
)
else:
transformations.append(
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))) # type: ignore
self.normalize = transforms.Compose(transformations)
self.reso_encoder = reso_encoder
self.reso = reso
self.instance_data_length = 40
# self.pair_per_instance = 1 # compat
self.mv_input = mv_input
self.split_chunk_input = split_chunk_input # 8
self.chunk_size = 8 if split_chunk_input else 40
# st()
if split_chunk_input:
self.pair_per_instance = 1
else:
self.pair_per_instance = 4 if mv_input else 2 # check whether improves IO
def gen_rays(self, c):
# Generate rays
intrinsics, c2w = c[16:], c[:16].reshape(4, 4)
self.h = self.reso_encoder
self.w = self.reso_encoder
yy, xx = torch.meshgrid(
torch.arange(self.h, dtype=torch.float32) + 0.5,
torch.arange(self.w, dtype=torch.float32) + 0.5,
indexing='ij')
# normalize to 0-1 pixel range
yy = yy / self.h
xx = xx / self.w
# K = np.array([f_x, 0, w / 2, 0, f_y, h / 2, 0, 0, 1]).reshape(3, 3)
cx, cy, fx, fy = intrinsics[2], intrinsics[5], intrinsics[
0], intrinsics[4]
# cx *= self.w
# cy *= self.h
# f_x = f_y = fx * h / res_raw
c2w = torch.from_numpy(c2w).float()
xx = (xx - cx) / fx
yy = (yy - cy) / fy
zz = torch.ones_like(xx)
dirs = torch.stack((xx, yy, zz), dim=-1) # OpenCV convention
dirs /= torch.norm(dirs, dim=-1, keepdim=True)
dirs = dirs.reshape(-1, 3, 1)
del xx, yy, zz
# st()
dirs = (c2w[None, :3, :3] @ dirs)[..., 0]
origins = c2w[None, :3, 3].expand(self.h * self.w, -1).contiguous()
origins = origins.view(self.h, self.w, 3)
dirs = dirs.view(self.h, self.w, 3)
return origins, dirs
def _post_process_batch_sample(
self, sample): # sample is an instance batch here
caption, ins = sample[-2:]
instance_samples = []
for instance_idx in range(sample[0].shape[0]):
instance_samples.append(
self._post_process_sample(item[instance_idx]
for item in sample[:-2]))
return (*instance_samples, caption, ins)
def _post_process_sample(self, data_sample):
# raw_img, depth, c, bbox, caption, ins = data_sample
raw_img, depth, c, bbox = data_sample
bbox = (bbox * (self.reso / 256)).astype(
np.uint8) # normalize bbox to the reso range
if raw_img.shape[-2] != self.reso_encoder:
img_to_encoder = cv2.resize(
raw_img, (self.reso_encoder, self.reso_encoder),
interpolation=cv2.INTER_LANCZOS4)
else:
img_to_encoder = raw_img
img_to_encoder = self.normalize(img_to_encoder)
if self.plucker_embedding:
rays_o, rays_d = self.gen_rays(c)
rays_plucker = torch.cat(
[torch.cross(rays_o, rays_d, dim=-1), rays_d],
dim=-1).permute(2, 0, 1) # [h, w, 6] -> 6,h,w
img_to_encoder = torch.cat([img_to_encoder, rays_plucker], 0)
img = cv2.resize(raw_img, (self.reso, self.reso),
interpolation=cv2.INTER_LANCZOS4)
img = torch.from_numpy(img).permute(2, 0, 1) / 127.5 - 1
if self.decode_encode_img_only:
depth_reso, fg_mask_reso = depth, depth
else:
depth_reso, fg_mask_reso = resize_depth_mask(depth, self.reso)
# return {
# # **sample,
# 'img_to_encoder': img_to_encoder,
# 'img': img,
# 'depth_mask': fg_mask_reso,
# # 'img_sr': img_sr,
# 'depth': depth_reso,
# 'c': c,
# 'bbox': bbox,
# 'caption': caption,
# 'ins': ins
# # ! no need to load img_sr for now
# }
# if len(data_sample) == 4:
return (img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox)
# else:
# return (img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox, data_sample[-2], data_sample[-1])
def _post_process_sample_batch(self, data_sample):
# raw_img, depth, c, bbox, caption, ins = data_sample
raw_img, depth, c, bbox = data_sample
bbox = (bbox * (self.reso / 256)).astype(
np.uint8) # normalize bbox to the reso range
assert raw_img.shape[-2] == self.reso_encoder
# img_to_encoder = cv2.resize(
# raw_img, (self.reso_encoder, self.reso_encoder),
# interpolation=cv2.INTER_LANCZOS4)
# else:
# img_to_encoder = raw_img
raw_img = torch.from_numpy(raw_img).permute(0, 3, 1,
2) / 255.0 # [0,1]
img_to_encoder = self.normalize(raw_img)
if self.plucker_embedding:
rays_plucker = []
for idx in range(c.shape[0]):
rays_o, rays_d = self.gen_rays(c[idx])
rays_plucker.append(
torch.cat(
[torch.cross(rays_o, rays_d, dim=-1), rays_d],
dim=-1).permute(2, 0, 1)) # [h, w, 6] -> 6,h,w
rays_plucker = torch.stack(rays_plucker, 0)
img_to_encoder = torch.cat([img_to_encoder, rays_plucker],
1) # concat in C dim
if self.append_depth:
normalized_depth = torch.from_numpy(depth).clone().unsqueeze(
1) # min=0
# normalized_depth -= torch.min(normalized_depth) # always 0 here
# normalized_depth /= torch.max(normalized_depth)
# normalized_depth = normalized_depth.unsqueeze(1) * 2 - 1 # normalize to [-1,1]
img_to_encoder = torch.cat([img_to_encoder, normalized_depth],
1) # concat in C dim
# img = cv2.resize(raw_img, (self.reso, self.reso),
# interpolation=cv2.INTER_LANCZOS4)
# img = torch.from_numpy(raw_img).permute(2, 0, 1) / 127.5 - 1
# st()
if raw_img.shape[-1] != self.reso:
img = torch.nn.functional.interpolate(
input=raw_img,
size=(self.reso, self.reso),
mode='bilinear',
align_corners=False,
) * 2 - 1 # [-1,1] range
else:
img = raw_img * 2 - 1
if self.decode_encode_img_only:
depth_reso, fg_mask_reso = depth, depth
else:
depth_reso, fg_mask_reso = resize_depth_mask_Tensor(
torch.from_numpy(depth), self.reso)
# if not self.gs_cam_format: # otherwise still playing with np format later
c = torch.from_numpy(c)
return (img_to_encoder, img, fg_mask_reso, depth_reso, c,
torch.from_numpy(bbox))
def rand_sample_idx(self):
return random.randint(0, self.instance_data_length - 1)
def rand_pair(self):
return (self.rand_sample_idx() for _ in range(2))
def paired_post_process(self, sample):
# repeat n times?
all_inp_list = []
all_nv_list = []
caption, ins = sample[-2:]
# expanded_return = []
for _ in range(self.pair_per_instance):
cano_idx, nv_idx = self.rand_pair()
cano_sample = self._post_process_sample(
item[cano_idx] for item in sample[:-2])
nv_sample = self._post_process_sample(item[nv_idx]
for item in sample[:-2])
all_inp_list.extend(cano_sample)
all_nv_list.extend(nv_sample)
return (*all_inp_list, *all_nv_list, caption, ins)
# return [cano_sample, nv_sample, caption, ins]
# return (*cano_sample, *nv_sample, caption, ins)
def get_source_cw2wT(self, source_cameras_view_to_world):
return matrix_to_quaternion(
source_cameras_view_to_world[:3, :3].transpose(0, 1))
def c_to_3dgs_format(self, pose):
# TODO, switch to torch version (batched later)
c2w = pose[:16].reshape(4, 4) # 3x4
# ! load cam
w2c = np.linalg.inv(c2w)
R = np.transpose(
w2c[:3, :3]
) # R is stored transposed due to 'glm' in CUDA code
T = w2c[:3, 3]
fx = pose[16]
FovX = focal2fov(fx, 1)
FovY = focal2fov(fx, 1)
tanfovx = math.tan(FovX * 0.5)
tanfovy = math.tan(FovY * 0.5)
assert tanfovx == tanfovy
trans = np.array([0.0, 0.0, 0.0])
scale = 1.0
view_world_transform = torch.tensor(
getView2World(R, T, trans, scale)).transpose(0, 1)
world_view_transform = torch.tensor(
getWorld2View2(R, T, trans, scale)).transpose(0, 1)
projection_matrix = getProjectionMatrix(znear=self.znear,
zfar=self.zfar,
fovX=FovX,
fovY=FovY).transpose(0, 1)
full_proj_transform = (world_view_transform.unsqueeze(0).bmm(
projection_matrix.unsqueeze(0))).squeeze(0)
camera_center = world_view_transform.inverse()[3, :3]
# item.update(viewpoint_cam=[viewpoint_cam])
c = {}
#
c["source_cv2wT_quat"] = self.get_source_cw2wT(
view_world_transform)
c.update(
# projection_matrix=projection_matrix, # K
cam_view=world_view_transform, # world_view_transform
cam_view_proj=full_proj_transform, # full_proj_transform
cam_pos=camera_center,
tanfov=tanfovx, # TODO, fix in the renderer
orig_pose=torch.from_numpy(pose),
orig_c2w=torch.from_numpy(c2w),
orig_w2c=torch.from_numpy(w2c),
# tanfovy=tanfovy,
)
return c # dict for gs rendering
def paired_post_process_chunk(self, sample):
# repeat n times?
all_inp_list = []
all_nv_list = []
caption, ins = sample[-2:]
assert sample[0].shape[0] == 8 # random chunks
# expanded_return = []
if self.duplicate_sample:
processed_sample = self._post_process_sample_batch(
item for item in sample[:-2])
if self.orthog_duplicate:
indices = torch.cat([torch.randperm(8),
torch.randperm(8)]) # for now
else:
indices = torch.randperm(8)
shuffle_processed_sample = []
for _, item in enumerate(processed_sample):
shuffle_processed_sample.append(
torch.index_select(item, dim=0, index=indices))
processed_sample = shuffle_processed_sample
if not self.orthog_duplicate:
all_inp_list.extend(item[:4] for item in processed_sample)
all_nv_list.extend(item[4:] for item in processed_sample)
else:
all_inp_list.extend(item[:8] for item in processed_sample)
all_nv_list.extend(item[8:] for item in processed_sample)
return (*all_inp_list, *all_nv_list, caption, ins)
else:
processed_sample = self._post_process_sample_batch( # avoid shuffle shorten processing time
item[:4] for item in sample[:-2])
all_inp_list.extend(item for item in processed_sample)
all_nv_list.extend(
item for item in processed_sample) # ! placeholder
return (*all_inp_list, *all_nv_list, caption, ins)
# randomly shuffle 8 views, avoid overfitting
def single_sample_create_dict(self, sample, prefix=''):
# if len(sample) == 1:
# sample = sample[0]
# assert len(sample) == 6
img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox = sample
if self.gs_cam_format:
# TODO, can optimize later after model converges
B, V, _ = c.shape # B 4 25
c = rearrange(c, 'B V C -> (B V) C').cpu().numpy()
all_gs_c = [self.c_to_3dgs_format(pose) for pose in c]
c = {
k:
rearrange(torch.stack([gs_c[k] for gs_c in all_gs_c]),
'(B V) ... -> B V ...',
B=B,
V=V) if isinstance(all_gs_c[0][k], torch.Tensor)
else all_gs_c[0][k]
for k in all_gs_c[0].keys()
}
# c = collate_gs_c
return {
# **sample,
f'{prefix}img_to_encoder': img_to_encoder,
f'{prefix}img': img,
f'{prefix}depth_mask': fg_mask_reso,
f'{prefix}depth': depth_reso,
f'{prefix}c': c,
f'{prefix}bbox': bbox,
}
def single_instance_sample_create_dict(self, sample, prfix=''):
assert len(sample) == 42
inp_sample_list = [[] for _ in range(6)]
for item in sample[:40]:
for item_idx in range(6):
inp_sample_list[item_idx].append(item[0][item_idx])
inp_sample = self.single_sample_create_dict(
(torch.stack(item_list) for item_list in inp_sample_list),
prefix='')
return {
**inp_sample, #
'caption': sample[-2],
'ins': sample[-1]
}
def decode_zip(self, sample_pyd, shape=(256, 256)):
if isinstance(sample_pyd, tuple):
sample_pyd = sample_pyd[0]
assert isinstance(sample_pyd, dict)
raw_img = decompress_and_open_image_gzip(
sample_pyd['raw_img'],
is_img=True,
decompress=True,
decompress_fn=lz4.frame.decompress)
caption = sample_pyd['caption'].decode('utf-8')
ins = sample_pyd['ins'].decode('utf-8')
c = decompress_array(sample_pyd['c'], (
self.chunk_size,
25,
),
np.float32,
decompress=True,
decompress_fn=lz4.frame.decompress)
bbox = decompress_array(
sample_pyd['bbox'],
(
self.chunk_size,
4,
),
np.float32,
# decompress=False)
decompress=True,
decompress_fn=lz4.frame.decompress)
if self.decode_encode_img_only:
depth = np.zeros(shape=(self.chunk_size,
*shape)) # save loading time
else:
depth = decompress_array(sample_pyd['depth'],
(self.chunk_size, *shape),
np.float32,
decompress=True,
decompress_fn=lz4.frame.decompress)
# return {'raw_img': raw_img, 'depth': depth, 'bbox': bbox, 'caption': caption, 'ins': ins, 'c': c}
# return raw_img, depth, c, bbox, caption, ins
# return raw_img, bbox, caption, ins
# return bbox, caption, ins
return raw_img, depth, c, bbox, caption, ins
# ! run single-instance pipeline first
# return raw_img[0], depth[0], c[0], bbox[0], caption, ins
def create_dict(self, sample):
# sample = [item[0] for item in sample] # wds wrap items in []
# st()
cano_sample_list = [[] for _ in range(6)]
nv_sample_list = [[] for _ in range(6)]
# st()
# bs = (len(sample)-2) // 6
for idx in range(0, self.pair_per_instance):
cano_sample = sample[6 * idx:6 * (idx + 1)]
nv_sample = sample[6 * self.pair_per_instance +
6 * idx:6 * self.pair_per_instance + 6 *
(idx + 1)]
for item_idx in range(6):
cano_sample_list[item_idx].append(cano_sample[item_idx])
nv_sample_list[item_idx].append(nv_sample[item_idx])
# ! cycle input/output view for more pairs
cano_sample_list[item_idx].append(nv_sample[item_idx])
nv_sample_list[item_idx].append(cano_sample[item_idx])
# if self.split_chunk_input:
# cano_sample = self.single_sample_create_dict(
# (torch.cat(item_list, 0) for item_list in cano_sample_list),
# prefix='')
# nv_sample = self.single_sample_create_dict(
# (torch.cat(item_list, 0) for item_list in nv_sample_list),
# prefix='nv_')
# else:
cano_sample = self.single_sample_create_dict(
(torch.cat(item_list, 0) for item_list in cano_sample_list),
prefix='')
nv_sample = self.single_sample_create_dict(
(torch.cat(item_list, 0) for item_list in nv_sample_list),
prefix='nv_')
return {
**cano_sample,
**nv_sample, 'caption': sample[-2],
'ins': sample[-1]
}
def prepare_mv_input(self, sample):
# sample = [item[0] for item in sample] # wds wrap items in []
bs = len(sample['caption']) # number of instances
chunk_size = sample['img'].shape[0] // bs
if self.split_chunk_input:
for k, v in sample.items():
if isinstance(v, torch.Tensor):
sample[k] = rearrange(v,
"b f c ... -> (b f) c ...",
f=4 if not self.orthog_duplicate
else 8).contiguous()
# img = rearrange(sample['img'], "(b f) c h w -> b f c h w", f=4).contiguous()
# gt = rearrange(sample['nv_img'], "(b f) c h w -> b c (f h) w", f=4).contiguous()
# img = rearrange(sample['img'], "b f c h w -> b c (f h) w", f=4).contiguous()
# gt = rearrange(sample['nv_img'], "b f c h w -> b c (f h) w", f=4).contiguous()
# torchvision.utils.save_image(img, 'inp.jpg', normalize=True)
# torchvision.utils.save_image(gt, 'nv.jpg', normalize=True)
# ! shift nv
else:
for k, v in sample.items():
if k not in ['ins', 'caption']:
rolled_idx = torch.LongTensor(
list(
itertools.chain.from_iterable(
list(range(i, sample['img'].shape[0], bs))
for i in range(bs))))
v = torch.index_select(v, dim=0, index=rolled_idx)
sample[k] = v
# img = sample['img']
# gt = sample['nv_img']
# torchvision.utils.save_image(img[0], 'inp.jpg', normalize=True)
# torchvision.utils.save_image(gt[0], 'nv.jpg', normalize=True)
for k, v in sample.items():
if 'nv' in k:
rolled_idx = torch.LongTensor(
list(
itertools.chain.from_iterable(
list(
np.roll(
np.arange(i * chunk_size, (i + 1) *
chunk_size), 4)
for i in range(bs)))))
v = torch.index_select(v, dim=0, index=rolled_idx)
sample[k] = v
# torchvision.utils.save_image(sample['nv_img'], 'nv.png', normalize=True)
# torchvision.utils.save_image(sample['img'], 'inp.png', normalize=True)
return sample
post_process_cls = PostProcess(
reso,
reso_encoder,
imgnet_normalize=imgnet_normalize,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
mv_input=mv_input,
split_chunk_input=split_chunk_input,
duplicate_sample=duplicate_sample,
append_depth=append_depth,
gs_cam_format=gs_cam_format,
orthog_duplicate=orthog_duplicate,
)
# ! add shuffling
if isinstance(file_path, list): # lst of shard urls
all_shards = []
for url_path in file_path:
all_shards.extend(wds.shardlists.expand_source(url_path))
logger.log('all_shards', all_shards)
else:
all_shards = file_path # to be expanded
if not load_instance: # during reconstruction training, load pair
if not split_chunk_input:
dataset = wds.DataPipeline(
wds.ResampledShards(all_shards), # url_shard
# at this point we have an iterator over all the shards
wds.shuffle(50),
wds.split_by_worker, # if multi-node
wds.tarfile_to_samples(),
# add wds.split_by_node here if you are using multiple nodes
wds.shuffle(
1000
), # shuffles in the memory, leverage large RAM for more efficient loading
wds.decode(wds.autodecode.basichandlers), # TODO
wds.to_tuple(
"sample.pyd"), # extract the pyd from top level dict
wds.map(post_process_cls.decode_zip),
wds.map(post_process_cls.paired_post_process
), # create input-novelview paired samples
# wds.map(post_process_cls._post_process_sample),
# wds.detshuffle(1000), # shuffles in the memory, leverage large RAM for more efficient loading
wds.batched(
16,
partial=True,
# collation_fn=collate
) # streaming more data at once, and rebatch later
)
else:
dataset = wds.DataPipeline(
wds.ResampledShards(all_shards), # url_shard
# at this point we have an iterator over all the shards
wds.shuffle(100),
wds.split_by_worker, # if multi-node
wds.tarfile_to_samples(),
# add wds.split_by_node here if you are using multiple nodes
wds.shuffle(
# 7500 if not duplicate_sample else 2500
# 7500 if not duplicate_sample else 5000
# 1000,
250,
), # shuffles in the memory, leverage large RAM for more efficient loading
wds.decode(wds.autodecode.basichandlers), # TODO
wds.to_tuple(
"sample.pyd"), # extract the pyd from top level dict
wds.map(post_process_cls.decode_zip),
wds.map(post_process_cls.paired_post_process_chunk
), # create input-novelview paired samples
# wds.map(post_process_cls._post_process_sample),
# wds.detshuffle(1000), # shuffles in the memory, leverage large RAM for more efficient loading
wds.batched(
20,
partial=True,
# collation_fn=collate
) # streaming more data at once, and rebatch later
)
loader_shard = wds.WebLoader(
dataset,
num_workers=num_workers,
drop_last=False,
batch_size=None,
shuffle=False,
persistent_workers=num_workers
# > 0).unbatched().shuffle(1000).batched(batch_size).map(
> 0).unbatched().shuffle(250).batched(batch_size).map(
post_process_cls.create_dict)
if mv_input:
loader_shard = loader_shard.map(post_process_cls.prepare_mv_input)
else: # load single instance during test/eval
assert batch_size == 1
dataset = wds.DataPipeline(
wds.ResampledShards(all_shards), # url_shard
# at this point we have an iterator over all the shards
wds.shuffle(50),
wds.split_by_worker, # if multi-node
wds.tarfile_to_samples(),
# add wds.split_by_node here if you are using multiple nodes
wds.detshuffle(
100
), # shuffles in the memory, leverage large RAM for more efficient loading
wds.decode(wds.autodecode.basichandlers), # TODO
wds.to_tuple("sample.pyd"), # extract the pyd from top level dict
wds.map(post_process_cls.decode_zip),
# wds.map(post_process_cls.paired_post_process), # create input-novelview paired samples
wds.map(post_process_cls._post_process_batch_sample),
# wds.detshuffle(1000), # shuffles in the memory, leverage large RAM for more efficient loading
wds.batched(
2,
partial=True,
# collation_fn=collate
) # streaming more data at once, and rebatch later
)
loader_shard = wds.WebLoader(
dataset,
num_workers=num_workers,
drop_last=False,
batch_size=None,
shuffle=False,
persistent_workers=num_workers
> 0).unbatched().shuffle(200).batched(batch_size).map(
post_process_cls.single_instance_sample_create_dict)
# persistent_workers=num_workers > 0).unbatched().batched(batch_size).map(post_process_cls.create_dict)
# 1000).batched(batch_size).map(post_process_cls.create_dict)
# .map(collate)
# .map(collate)
# .batched(batch_size)
#
# .unbatched().shuffle(1000).batched(batch_size).map(post_process)
# # https://github.com/webdataset/webdataset/issues/187
# return next(iter(loader_shard))
#return dataset
return loader_shard
# test tar loading
def load_wds_diff_ResampledShard(file_path,
batch_size,
num_workers,
reso,
reso_encoder,
test=False,
preprocess=None,
imgnet_normalize=True,
plucker_embedding=False,
decode_encode_img_only=False,
mv_latent_dir='',
**kwargs):
# return raw_img, depth, c, bbox, sample_pyd['ins.pyd'], sample_pyd['fname.pyd']
class PostProcess:
def __init__(
self,
reso,
reso_encoder,
imgnet_normalize,
plucker_embedding,
decode_encode_img_only,
mv_latent_dir,
) -> None:
self.plucker_embedding = plucker_embedding
self.mv_latent_dir = mv_latent_dir
self.decode_encode_img_only = decode_encode_img_only
transformations = [
transforms.ToTensor(), # [0,1] range
]
if imgnet_normalize:
transformations.append(
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)) # type: ignore
)
else:
transformations.append(
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))) # type: ignore
self.normalize = transforms.Compose(transformations)
self.reso_encoder = reso_encoder
self.reso = reso
self.instance_data_length = 40
# self.pair_per_instance = 1 # compat
self.pair_per_instance = 2 # check whether improves IO
# self.pair_per_instance = 3 # check whether improves IO
# self.pair_per_instance = 4 # check whether improves IO
def get_rays_kiui(self, c, opengl=True):
h, w = self.reso_encoder, self.reso_encoder
intrinsics, pose = c[16:], c[:16].reshape(4, 4)
# cx, cy, fx, fy = intrinsics[2], intrinsics[5]
fx = fy = 525 # pixel space
cx = cy = 256 # rendering default K
factor = self.reso / (cx * 2) # 128 / 512
fx = fx * factor
fy = fy * factor
x, y = torch.meshgrid(
torch.arange(w, device=pose.device),
torch.arange(h, device=pose.device),
indexing="xy",
)
x = x.flatten()
y = y.flatten()
cx = w * 0.5
cy = h * 0.5
# focal = h * 0.5 / np.tan(0.5 * np.deg2rad(fovy))
camera_dirs = F.pad(
torch.stack(
[
(x - cx + 0.5) / fx,
(y - cy + 0.5) / fy * (-1.0 if opengl else 1.0),
],
dim=-1,
),
(0, 1),
value=(-1.0 if opengl else 1.0),
) # [hw, 3]
rays_d = camera_dirs @ pose[:3, :3].transpose(0, 1) # [hw, 3]
rays_o = pose[:3, 3].unsqueeze(0).expand_as(rays_d) # [hw, 3]
rays_o = rays_o.view(h, w, 3)
rays_d = safe_normalize(rays_d).view(h, w, 3)
return rays_o, rays_d
def gen_rays(self, c):
# Generate rays
intrinsics, c2w = c[16:], c[:16].reshape(4, 4)
self.h = self.reso_encoder
self.w = self.reso_encoder
yy, xx = torch.meshgrid(
torch.arange(self.h, dtype=torch.float32) + 0.5,
torch.arange(self.w, dtype=torch.float32) + 0.5,
indexing='ij')
# normalize to 0-1 pixel range
yy = yy / self.h
xx = xx / self.w
# K = np.array([f_x, 0, w / 2, 0, f_y, h / 2, 0, 0, 1]).reshape(3, 3)
cx, cy, fx, fy = intrinsics[2], intrinsics[5], intrinsics[
0], intrinsics[4]
# cx *= self.w
# cy *= self.h
# f_x = f_y = fx * h / res_raw
c2w = torch.from_numpy(c2w).float()
xx = (xx - cx) / fx
yy = (yy - cy) / fy
zz = torch.ones_like(xx)
dirs = torch.stack((xx, yy, zz), dim=-1) # OpenCV convention
dirs /= torch.norm(dirs, dim=-1, keepdim=True)
dirs = dirs.reshape(-1, 3, 1)
del xx, yy, zz
# st()
dirs = (c2w[None, :3, :3] @ dirs)[..., 0]
origins = c2w[None, :3, 3].expand(self.h * self.w, -1).contiguous()
origins = origins.view(self.h, self.w, 3)
dirs = dirs.view(self.h, self.w, 3)
return origins, dirs
def _post_process_sample(self, data_sample):
# raw_img, depth, c, bbox, caption, ins = data_sample
raw_img, c, caption, ins = data_sample
# bbox = (bbox*(self.reso/256)).astype(np.uint8) # normalize bbox to the reso range
# if raw_img.shape[-2] != self.reso_encoder:
# img_to_encoder = cv2.resize(
# raw_img, (self.reso_encoder, self.reso_encoder),
# interpolation=cv2.INTER_LANCZOS4)
# else:
# img_to_encoder = raw_img
# img_to_encoder = self.normalize(img_to_encoder)
# if self.plucker_embedding:
# rays_o, rays_d = self.gen_rays(c)
# rays_plucker = torch.cat(
# [torch.cross(rays_o, rays_d, dim=-1), rays_d],
# dim=-1).permute(2, 0, 1) # [h, w, 6] -> 6,h,w
# img_to_encoder = torch.cat([img_to_encoder, rays_plucker], 0)
# img = cv2.resize(raw_img, (self.reso, self.reso),
# interpolation=cv2.INTER_LANCZOS4)
img = raw_img # 256x256
img = torch.from_numpy(img).permute(2, 0, 1) / 127.5 - 1
# load latent
latent_path = Path(self.mv_latent_dir, ins, 'latent.npy')
latent = np.load(latent_path)
# return (img_to_encoder, img, c, caption, ins)
return (latent, img, c, caption, ins)
def rand_sample_idx(self):
return random.randint(0, self.instance_data_length - 1)
def rand_pair(self):
return (self.rand_sample_idx() for _ in range(2))
def paired_post_process(self, sample):
# repeat n times?
all_inp_list = []
all_nv_list = []
caption, ins = sample[-2:]
# expanded_return = []
for _ in range(self.pair_per_instance):
cano_idx, nv_idx = self.rand_pair()
cano_sample = self._post_process_sample(
item[cano_idx] for item in sample[:-2])
nv_sample = self._post_process_sample(item[nv_idx]
for item in sample[:-2])
all_inp_list.extend(cano_sample)
all_nv_list.extend(nv_sample)
return (*all_inp_list, *all_nv_list, caption, ins)
# return [cano_sample, nv_sample, caption, ins]
# return (*cano_sample, *nv_sample, caption, ins)
# def single_sample_create_dict(self, sample, prefix=''):
# # if len(sample) == 1:
# # sample = sample[0]
# # assert len(sample) == 6
# img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox = sample
# return {
# # **sample,
# f'{prefix}img_to_encoder': img_to_encoder,
# f'{prefix}img': img,
# f'{prefix}depth_mask': fg_mask_reso,
# f'{prefix}depth': depth_reso,
# f'{prefix}c': c,
# f'{prefix}bbox': bbox,
# }
def single_sample_create_dict(self, sample, prefix=''):
# if len(sample) == 1:
# sample = sample[0]
# assert len(sample) == 6
# img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox = sample
# img_to_encoder, img, c, caption, ins = sample
# img, c, caption, ins = sample
latent, img, c, caption, ins = sample
# load latent
return {
# **sample,
# 'img_to_encoder': img_to_encoder,
'latent': latent,
'img': img,
'c': c,
'caption': caption,
'ins': ins
}
def decode_zip(self, sample_pyd, shape=(256, 256)):
if isinstance(sample_pyd, tuple):
sample_pyd = sample_pyd[0]
assert isinstance(sample_pyd, dict)
raw_img = decompress_and_open_image_gzip(
sample_pyd['raw_img'],
is_img=True,
decompress=True,
decompress_fn=lz4.frame.decompress)
caption = sample_pyd['caption'].decode('utf-8')
ins = sample_pyd['ins'].decode('utf-8')
c = decompress_array(sample_pyd['c'], (25, ),
np.float32,
decompress=True,
decompress_fn=lz4.frame.decompress)
# bbox = decompress_array(
# sample_pyd['bbox'],
# (
# 40,
# 4,
# ),
# np.float32,
# # decompress=False)
# decompress=True,
# decompress_fn=lz4.frame.decompress)
# if self.decode_encode_img_only:
# depth = np.zeros(shape=(40, *shape)) # save loading time
# else:
# depth = decompress_array(sample_pyd['depth'], (40, *shape),
# np.float32,
# decompress=True,
# decompress_fn=lz4.frame.decompress)
# return {'raw_img': raw_img, 'depth': depth, 'bbox': bbox, 'caption': caption, 'ins': ins, 'c': c}
# return raw_img, depth, c, bbox, caption, ins
# return raw_img, bbox, caption, ins
# return bbox, caption, ins
return raw_img, c, caption, ins
# ! run single-instance pipeline first
# return raw_img[0], depth[0], c[0], bbox[0], caption, ins
def create_dict(self, sample):
# sample = [item[0] for item in sample] # wds wrap items in []
# cano_sample_list = [[] for _ in range(6)]
# nv_sample_list = [[] for _ in range(6)]
# for idx in range(0, self.pair_per_instance):
# cano_sample = sample[6*idx:6*(idx+1)]
# nv_sample = sample[6*self.pair_per_instance+6*idx:6*self.pair_per_instance+6*(idx+1)]
# for item_idx in range(6):
# cano_sample_list[item_idx].append(cano_sample[item_idx])
# nv_sample_list[item_idx].append(nv_sample[item_idx])
# # ! cycle input/output view for more pairs
# cano_sample_list[item_idx].append(nv_sample[item_idx])
# nv_sample_list[item_idx].append(cano_sample[item_idx])
cano_sample = self.single_sample_create_dict(sample, prefix='')
# nv_sample = self.single_sample_create_dict((torch.cat(item_list) for item_list in nv_sample_list) , prefix='nv_')
return cano_sample
# return {
# **cano_sample,
# # **nv_sample,
# 'caption': sample[-2],
# 'ins': sample[-1]
# }
post_process_cls = PostProcess(
reso,
reso_encoder,
imgnet_normalize=imgnet_normalize,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
mv_latent_dir=mv_latent_dir,
)
if isinstance(file_path, list): # lst of shard urls
all_shards = []
for url_path in file_path:
all_shards.extend(wds.shardlists.expand_source(url_path))
logger.log('all_shards', all_shards)
else:
all_shards = file_path # to be expanded
dataset = wds.DataPipeline(
wds.ResampledShards(all_shards), # url_shard
# at this point we have an iterator over all the shards
wds.shuffle(50),
wds.split_by_worker, # if multi-node
wds.tarfile_to_samples(),
# add wds.split_by_node here if you are using multiple nodes
wds.detshuffle(
15000
), # shuffles in the memory, leverage large RAM for more efficient loading
wds.decode(wds.autodecode.basichandlers), # TODO
wds.to_tuple("sample.pyd"), # extract the pyd from top level dict
wds.map(post_process_cls.decode_zip),
# wds.map(post_process_cls.paired_post_process), # create input-novelview paired samples
wds.map(post_process_cls._post_process_sample),
# wds.detshuffle(1000), # shuffles in the memory, leverage large RAM for more efficient loading
wds.batched(
80,
partial=True,
# collation_fn=collate
) # streaming more data at once, and rebatch later
)
loader_shard = wds.WebLoader(
dataset,
num_workers=num_workers,
drop_last=False,
batch_size=None,
shuffle=False,
persistent_workers=num_workers
> 0).unbatched().shuffle(2500).batched(batch_size).map(
post_process_cls.create_dict)
# persistent_workers=num_workers > 0).unbatched().batched(batch_size).map(post_process_cls.create_dict)
# 1000).batched(batch_size).map(post_process_cls.create_dict)
# .map(collate)
# .map(collate)
# .batched(batch_size)
#
# .unbatched().shuffle(1000).batched(batch_size).map(post_process)
# # https://github.com/webdataset/webdataset/issues/187
# return next(iter(loader_shard))
#return dataset
return loader_shard
def load_wds_data(
file_path="",
reso=64,
reso_encoder=224,
batch_size=1,
num_workers=6,
plucker_embedding=False,
decode_encode_img_only=False,
load_wds_diff=False,
load_wds_latent=False,
load_instance=False, # for evaluation
mv_input=False,
split_chunk_input=False,
duplicate_sample=True,
mv_latent_dir='',
append_depth=False,
gs_cam_format=False,
orthog_duplicate=False,
**args):
if load_wds_diff:
assert num_workers == 0 # on aliyun, worker=0 performs much much faster
wds_loader = load_wds_diff_ResampledShard(
file_path,
batch_size=batch_size,
num_workers=num_workers,
reso=reso,
reso_encoder=reso_encoder,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
mv_input=mv_input,
split_chunk_input=split_chunk_input,
append_depth=append_depth,
mv_latent_dir=mv_latent_dir,
gs_cam_format=gs_cam_format,
orthog_duplicate=orthog_duplicate,
)
elif load_wds_latent:
# for diffusion training, cache latent
wds_loader = load_wds_latent_ResampledShard(
file_path,
batch_size=batch_size,
num_workers=num_workers,
reso=reso,
reso_encoder=reso_encoder,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
mv_input=mv_input,
split_chunk_input=split_chunk_input,
)
# elif load_instance:
# wds_loader = load_wds_instance_ResampledShard(
# file_path,
# batch_size=batch_size,
# num_workers=num_workers,
# reso=reso,
# reso_encoder=reso_encoder,
# plucker_embedding=plucker_embedding,
# decode_encode_img_only=decode_encode_img_only
# )
else:
wds_loader = load_wds_ResampledShard(
file_path,
batch_size=batch_size,
num_workers=num_workers,
reso=reso,
reso_encoder=reso_encoder,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
load_instance=load_instance,
mv_input=mv_input,
split_chunk_input=split_chunk_input,
duplicate_sample=duplicate_sample,
append_depth=append_depth,
gs_cam_format=gs_cam_format,
orthog_duplicate=orthog_duplicate,
)
while True:
yield from wds_loader
# yield from wds_loader
class PostProcess_forlatent:
def __init__(
self,
reso,
reso_encoder,
imgnet_normalize,
plucker_embedding,
decode_encode_img_only,
) -> None:
self.plucker_embedding = plucker_embedding
self.decode_encode_img_only = decode_encode_img_only
transformations = [
transforms.ToTensor(), # [0,1] range
]
if imgnet_normalize:
transformations.append(
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)) # type: ignore
)
else:
transformations.append(
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))) # type: ignore
self.normalize = transforms.Compose(transformations)
self.reso_encoder = reso_encoder
self.reso = reso
self.instance_data_length = 40
# self.pair_per_instance = 1 # compat
self.pair_per_instance = 2 # check whether improves IO
# self.pair_per_instance = 3 # check whether improves IO
# self.pair_per_instance = 4 # check whether improves IO
def _post_process_sample(self, data_sample):
# raw_img, depth, c, bbox, caption, ins = data_sample
raw_img, c, caption, ins = data_sample
# bbox = (bbox*(self.reso/256)).astype(np.uint8) # normalize bbox to the reso range
if raw_img.shape[-2] != self.reso_encoder:
img_to_encoder = cv2.resize(
raw_img, (self.reso_encoder, self.reso_encoder),
interpolation=cv2.INTER_LANCZOS4)
else:
img_to_encoder = raw_img
img_to_encoder = self.normalize(img_to_encoder)
if self.plucker_embedding:
rays_o, rays_d = self.gen_rays(c)
rays_plucker = torch.cat(
[torch.cross(rays_o, rays_d, dim=-1), rays_d],
dim=-1).permute(2, 0, 1) # [h, w, 6] -> 6,h,w
img_to_encoder = torch.cat([img_to_encoder, rays_plucker], 0)
img = cv2.resize(raw_img, (self.reso, self.reso),
interpolation=cv2.INTER_LANCZOS4)
img = torch.from_numpy(img).permute(2, 0, 1) / 127.5 - 1
return (img_to_encoder, img, c, caption, ins)
def rand_sample_idx(self):
return random.randint(0, self.instance_data_length - 1)
def rand_pair(self):
return (self.rand_sample_idx() for _ in range(2))
def paired_post_process(self, sample):
# repeat n times?
all_inp_list = []
all_nv_list = []
caption, ins = sample[-2:]
# expanded_return = []
for _ in range(self.pair_per_instance):
cano_idx, nv_idx = self.rand_pair()
cano_sample = self._post_process_sample(
item[cano_idx] for item in sample[:-2])
nv_sample = self._post_process_sample(item[nv_idx]
for item in sample[:-2])
all_inp_list.extend(cano_sample)
all_nv_list.extend(nv_sample)
return (*all_inp_list, *all_nv_list, caption, ins)
# return [cano_sample, nv_sample, caption, ins]
# return (*cano_sample, *nv_sample, caption, ins)
# def single_sample_create_dict(self, sample, prefix=''):
# # if len(sample) == 1:
# # sample = sample[0]
# # assert len(sample) == 6
# img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox = sample
# return {
# # **sample,
# f'{prefix}img_to_encoder': img_to_encoder,
# f'{prefix}img': img,
# f'{prefix}depth_mask': fg_mask_reso,
# f'{prefix}depth': depth_reso,
# f'{prefix}c': c,
# f'{prefix}bbox': bbox,
# }
def single_sample_create_dict(self, sample, prefix=''):
# if len(sample) == 1:
# sample = sample[0]
# assert len(sample) == 6
# img_to_encoder, img, fg_mask_reso, depth_reso, c, bbox = sample
img_to_encoder, img, c, caption, ins = sample
return {
# **sample,
'img_to_encoder': img_to_encoder,
'img': img,
'c': c,
'caption': caption,
'ins': ins
}
def decode_zip(self, sample_pyd, shape=(256, 256)):
if isinstance(sample_pyd, tuple):
sample_pyd = sample_pyd[0]
assert isinstance(sample_pyd, dict)
latent = sample_pyd['latent']
caption = sample_pyd['caption'].decode('utf-8')
c = sample_pyd['c']
# img = sample_pyd['img']
# st()
return latent, caption, c
def create_dict(self, sample):
return {
# **sample,
'latent': sample[0],
'caption': sample[1],
'c': sample[2],
}
# test tar loading
def load_wds_latent_ResampledShard(file_path,
batch_size,
num_workers,
reso,
reso_encoder,
test=False,
preprocess=None,
imgnet_normalize=True,
plucker_embedding=False,
decode_encode_img_only=False,
**kwargs):
post_process_cls = PostProcess_forlatent(
reso,
reso_encoder,
imgnet_normalize=imgnet_normalize,
plucker_embedding=plucker_embedding,
decode_encode_img_only=decode_encode_img_only,
)
if isinstance(file_path, list): # lst of shard urls
all_shards = []
for url_path in file_path:
all_shards.extend(wds.shardlists.expand_source(url_path))
logger.log('all_shards', all_shards)
else:
all_shards = file_path # to be expanded
dataset = wds.DataPipeline(
wds.ResampledShards(all_shards), # url_shard
# at this point we have an iterator over all the shards
wds.shuffle(50),
wds.split_by_worker, # if multi-node
wds.tarfile_to_samples(),
# add wds.split_by_node here if you are using multiple nodes
wds.detshuffle(
2500
), # shuffles in the memory, leverage large RAM for more efficient loading
wds.decode(wds.autodecode.basichandlers), # TODO
wds.to_tuple("sample.pyd"), # extract the pyd from top level dict
wds.map(post_process_cls.decode_zip),
# wds.map(post_process_cls._post_process_sample),
# wds.detshuffle(1000), # shuffles in the memory, leverage large RAM for more efficient loading
wds.batched(
150,
partial=True,
# collation_fn=collate
) # streaming more data at once, and rebatch later
)
loader_shard = wds.WebLoader(
dataset,
num_workers=num_workers,
drop_last=False,
batch_size=None,
shuffle=False,
persistent_workers=num_workers
> 0).unbatched().shuffle(1000).batched(batch_size).map(
post_process_cls.create_dict)
# persistent_workers=num_workers > 0).unbatched().batched(batch_size).map(post_process_cls.create_dict)
# 1000).batched(batch_size).map(post_process_cls.create_dict)
# .map(collate)
# .map(collate)
# .batched(batch_size)
#
# .unbatched().shuffle(1000).batched(batch_size).map(post_process)
# # https://github.com/webdataset/webdataset/issues/187
# return next(iter(loader_shard))
#return dataset
return loader_shard
|