Spaces:
Runtime error
Runtime error
File size: 201,551 Bytes
d6d3a5b |
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 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 |
import numpy as np
import torch
import torch.nn as nn
# import torch.nn.functional as F
import clip
# from model.rotation2xyz import Rotation2xyz
import utils.model_utils as model_utils
# from model.PointNet2 import PointnetPP
# from model.DGCNN import PrimitiveNet
# from utils.anchor_utils import masking_load_driver, anchor_load_driver ### load driver; masking load driver #
# import os
### MDM 10 ###
class MDMV10(nn.Module):
def __init__(self, modeltype, njoints, nfeats, num_actions, translation, pose_rep, glob, glob_rot,
latent_dim=256, ff_size=1024, num_layers=8, num_heads=4, dropout=0.1,
ablation=None, activation="gelu", legacy=False, data_rep='rot6d', dataset='amass', clip_dim=512,
arch='trans_enc', emb_trans_dec=False, clip_version=None, **kargs):
super().__init__()
self.legacy = legacy
self.modeltype = modeltype
self.njoints = njoints
self.nfeats = nfeats
self.num_actions = num_actions
self.data_rep = data_rep
self.dataset = dataset
self.pose_rep = pose_rep
self.glob = glob
self.glob_rot = glob_rot
self.translation = translation
self.latent_dim = latent_dim
self.ff_size = ff_size
self.num_layers = num_layers
self.num_heads = num_heads
self.dropout = dropout
self.ablation = ablation
self.activation = activation
self.clip_dim = clip_dim
self.action_emb = kargs.get('action_emb', None)
self.input_feats = self.njoints * self.nfeats
self.normalize_output = kargs.get('normalize_encoder_output', False)
self.cond_mode = kargs.get('cond_mode', 'no_cond')
self.cond_mask_prob = kargs.get('cond_mask_prob', 0.) #
### GET args ###
self.args = kargs.get('args', None)
### GET the diff. suit ###
self.diff_jts = self.args.diff_jts
self.diff_basejtsrel = self.args.diff_basejtsrel
self.diff_basejtse = self.args.diff_basejtse
self.diff_realbasejtsrel = self.args.diff_realbasejtsrel
self.diff_realbasejtsrel_to_joints = self.args.diff_realbasejtsrel_to_joints
### GET the diff. suit ###
self.arch = arch
## ==== gru_emb_dim ==== ## # gru emb dim #
self.gru_emb_dim = self.latent_dim if self.arch == 'gru' else 0
# joint_sequence_input_process; joint_sequence_pos_encoder; joint_sequence_seqTransEncoder; joint_sequence_seqTransDecoder; joint_sequence_embed_timestep; joint_sequence_output_process #
###### ======= Construct joint sequence encoder, communicator, and decoder ======== #######
self.use_anchors = self.args.use_anchors
# if self.use_anchors: # use anchors # anchor_load_driver, masking_load_driver #
# # anchor_load_driver, masking_load_driver #
# inpath = "/home/xueyi/sim/CPF/assets" # contact potential field; assets # ##
# fvi, aw, _, _ = anchor_load_driver(inpath)
# self.face_vertex_index = torch.from_numpy(fvi).long()
# self.anchor_weight = torch.from_numpy(aw).float()
# anchor_path = os.path.join("/home/xueyi/sim/CPF/assets", "anchor")
# palm_path = os.path.join("/home/xueyi/sim/CPF/assets", "hand_palm_full.txt")
# hand_region_assignment, hand_palm_vertex_mask = masking_load_driver(anchor_path, palm_path)
# # self.hand_palm_vertex_mask for hand palm mask #
# self.hand_palm_vertex_mask = torch.from_numpy(hand_palm_vertex_mask).bool() ## the mask for hand palm to get hand anchors #
# self.nn_anchors = int(self.hand_palm_vertex_mask.float().sum()) #### number of anchors here ###
# self.joints_feats_in_dim = 21 * 3
# joints feats in dim #
self.nn_keypoints = 21
# if self.args.use_anchors:
# # self.nn_keypoints = self.nn_anchors # nn_anchors #
# self.nn_keypoints = 32 # nn_anchors #
self.joints_feats_in_dim = self.nn_keypoints * 3
self.data_rep = "xyz"
if self.diff_jts:
## Input process for joints ##
self.joint_sequence_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
# # InputProcessObjBase(self, data_rep, input_feats, latent_dim)
# self.joint_sequence_input_process = InputProcessObjBase(self.data_rep, 3, self.latent_dim)
# self.input_process = InputProcess(self.data_rep, self.input_feats+self.gru_emb_dim, self.latent_dim)
self.joint_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
# if self.arch == 'trans_enc':
print("TRANS_ENC init") ## transformer encoder layer ## UNet
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
## Joint sequence transformer encoder layer ## # sequence encoder #
self.joint_sequence_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
### logvar for the encoding laeyer and
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
seqTransEncoderLayer_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
## Joint sequence transformer encoder layer ## # sequence encoder #
self.joint_sequence_logvar_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer_logvar,
num_layers=self.num_layers)
# elif self.arch == 'trans_dec':
# print("TRANS_DEC init")
# seqTransDecoderLayer = nn.TransformerDecoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads, # num_heads
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=activation)
# self.joint_sequence_seqTransDecoder = nn.TransformerDecoder(seqTransDecoderLayer,
# num_layers=self.num_layers)
# elif self.arch == 'gru':
# print("GRU init")
# self.joint_sequence_gru = nn.GRU(self.latent_dim, self.latent_dim, num_layers=self.num_layers, batch_first=True)
# else:
# raise ValueError('Please choose correct architecture [trans_enc, trans_dec, gru]')
### joint sequence embed timestep ## ## timestep
self.joint_sequence_embed_timestep = TimestepEmbedder(self.latent_dim, self.joint_sequence_pos_encoder)
# self.joint_sequence_output_process = OutputProcess(self.data_rep, self.latent_dim)
# (self, data_rep, input_feats, latent_dim, njoints, nfeats):
#### ====== joint sequence denoising block ====== ####
## seqTransEncoder ##
self.joint_sequence_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.joint_sequence_pos_encoder)
self.joint_sequence_denoising_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc:
self.joint_sequence_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.joint_sequence_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
# seq_len x bsz x dim
if self.args.const_noise:
# 1) max pool latents over the sequence
# 2) transform the pooled latnets via the linear layer
self.glb_denoising_latents_trans_layer = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
nn.Linear(self.latent_dim * 2, self.latent_dim)
)
# refinement for predicted joints # --> not in the paradigm of generation #
# seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=self.activation)
# self.joint_sequence_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
# num_layers=self.num_layers)
#### ====== joint sequence denoiisng block ====== ####
### Output process ### output proces for joint sequence ### # output proces --> datarep, joints feats in dim, latent dim ##
###### joints_feats_in_dim ######
self.joint_sequence_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
# OutputProcessCond
# self.joint_sequence_output_process = OutputProcessCond(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
###### ======= Construct joint sequence encoder, communicator, and decoder ======== #######
# real_basejtsrel_to_joints_embed_timestep, real_basejtsrel_to_joints_sequence_pos_denoising_encoder, real_basejtsrel_to_joints_denoising_seqTransEncoder, real_basejtsrel_to_joints_output_process
if self.diff_realbasejtsrel_to_joints: # feature for each joint point? --> for the denoising purpose #
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
layernorm = True
self.rel_input_feats = 21 * (3 + 3 + 3) # base pts, normals, the relative positions
if self.args.use_abs_jts_for_encoding_obj_base:
self.rel_input_feats = 21 * (3)
# layernorm = False
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
# self.real_basejtsrel_to_joints_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
# elif self.args.use
else:
if self.args.use_objbase_v2:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v3:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV3(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
else:
self.real_basejtsrel_to_joints_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
if self.args.use_abs_jts_for_encoding: # use_abs_jts_for_encoding, real_basejtsrel_to_joints_input_process
self.real_basejtsrel_to_joints_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
self.real_basejtsrel_to_joints_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
### Encoding layer ### # InputProcessObjBaseV2
real_basejtsrel_to_joints_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim, # latent dim # nn_heads # ff_size # dropout #
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_to_joints_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_to_joints_seqTransEncoderLayer, # basejtsrel_seqTrans
num_layers=self.num_layers)
### timesteps embedding layer ###
self.real_basejtsrel_to_joints_embed_timestep = TimestepEmbedder(self.latent_dim, self.real_basejtsrel_to_joints_sequence_pos_encoder)
self.real_basejtsrel_to_joints_sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.real_basejtsrel_to_joints_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
real_basejtsrel_to_joints_denoising_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_to_joints_denoising_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_to_joints_denoising_seqTransEncoderLayer,
num_layers=self.num_layers)
# self.real_basejtsrel_output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
self.real_basejtsrel_to_joints_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
# OutputProcessCond
if self.diff_realbasejtsrel:
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
# self.rel_input_feats = 21 * (3 + 3 + 3) # base pts, normals, the relative positions
# self.real_basejtsrel_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim)
self.rel_input_feats = self.nn_keypoints * (3 + 3 + 3)
layernorm = True
if self.args.use_objbase_v2:
self.rel_input_feats = 3 + 3 + (self.nn_keypoints * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm, glb_feats_trans=True)
elif self.args.use_objbase_v4: # use_objbase_out_v4
self.rel_input_feats = (self.args.nn_base_pts * (3 + 3 + 3)) # current joint positions # how to keep the dimension
self.real_basejtsrel_input_process = InputProcessObjBaseV4(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v5: # use_objbase_v5,
if self.args.v5_in_not_base:
self.rel_input_feats = (self.nn_keypoints * 3)
elif self.args.v5_in_not_base_pos:
self.rel_input_feats = 3 + (self.nn_keypoints * 3)
else:
self.rel_input_feats = 3 + 3 + (self.nn_keypoints * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV5(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm, without_glb=self.args.v5_in_without_glb)
elif self.args.use_objbase_v6: # real_basejtsrel_input_process
self.rel_input_feats = 3 + 3 + (self.nn_keypoints * 3) + 3
self.real_basejtsrel_input_process = InputProcessObjBaseV6(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v7:
# InputProcessObjBaseV7
self.rel_input_feats = 3 + 3 + (self.nn_keypoints * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV7(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
else:
self.real_basejtsrel_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
self.real_basejtsrel_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
### Encoding layer ###
real_basejtsrel_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim, # latent dim # nn_heads # ff_size # dropout # # dropout # # dropout
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_seqTransEncoderLayer, # basejtsrel_seqTrans
num_layers=self.num_layers)
### timesteps embedding layer ###
self.real_basejtsrel_embed_timestep = TimestepEmbedder(self.latent_dim, self.real_basejtsrel_sequence_pos_encoder)
self.real_basejtsrel_sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.real_basejtsrel_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
real_basejtsrel_denoising_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_denoising_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_denoising_seqTransEncoderLayer,
num_layers=self.num_layers)
print(f"not_cond_base: {self.args.not_cond_base}, latent_dim: {self.latent_dim}")
if self.args.use_jts_pert_realbasejtsrel:
print(f"use_jts_pert_realbasejtsrel!!!!!!")
self.real_basejtsrel_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, self.nn_keypoints, 3)
else:
if self.args.use_objbase_out_v3:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV3(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
elif self.args.use_objbase_out_v4:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV4(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
elif self.args.use_objbase_out_v5: # use_objbase_v5, use_objbase_out_v5
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV5(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base, out_objbase_v5_bundle_out=self.args.out_objbase_v5_bundle_out, v5_out_not_cond_base=self.args.v5_out_not_cond_base, nn_keypoints=self.nn_keypoints)
else:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
# OutputProcessCond
if self.diff_basejtsrel:
# treate them as textures of signals to model # # base pts -> dec on base pts features -->
# latent space denoising and feature decoding --> a little bit concern about the feature decoding process #
# TODO: add base_pts and base_normals to the base points -rel-to- rhand joints encoding process #
self.rel_input_feats = self.nn_keypoints * (3 + 3 + 3) # relative positions from base pts to rhand joints ##
# self.avg_joints_sequence_input_process, self.avg_joint_sequence_output_process #
self.avg_joints_sequence_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
if self.args.with_glb_info:
# InputProcessWithGlbInfo
self.joints_offset_input_process = InputProcessWithGlbInfo(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
else:
self.joints_offset_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
if self.args.not_cond_base:
self.rel_input_feats = self.nn_keypoints * ( 3)
# self.input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats+self.gru_emb_dim, self.latent_dim)
self.sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
### Encoding layer ###
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
### Encoding layer ###
# logvar_seqTransEncoder_e, logvar_seqTransEncoder # logvar_seqTranEncoder
seqTransEncoderLayer_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.logvar_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer_logvar,
num_layers=self.num_layers)
### timesteps embedding layer ###
self.embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder)
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process # # baseptsrel #
self.basejtsrel_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder)
self.sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.basejtsrel_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.basejtsrel_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
# seq_len x bsz x dim
if self.args.const_noise: # add to attention network #
# 1) max pool latents over the sequence
# 2) transform the pooled latnets via the linear layer
self.basejtsrel_glb_denoising_latents_trans_layer = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
nn.Linear(self.latent_dim * 2, self.latent_dim)
)
###### joints_feats_in_dim ###### # a linear transformation net with weights and bias set to zero #
self.avg_joint_sequence_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, self.nn_keypoints, 3) # output avgjts sequence
# OutputProcessCond
self.joint_offset_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, self.nn_keypoints, 3)
if self.args.use_dec_rel_v2:
self.output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
else:
# OutputProcessObjBaseRaw ## output process for basejtsrel #
self.output_process = OutputProcessObjBaseRaw(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
##### ==== input process, communications, output process for rel, dists ==== #####
if self.diff_basejtse:
### input process obj base ###
# construct input_process_e #
# self.input_feats_e = 21 * (3 + 3 + 3 + 1 + 1)
self.input_feats_e = self.nn_keypoints * (3 + 3 + 1 + 1)
self.input_process_e = InputProcessObjBase(self.data_rep, self.input_feats_e+self.gru_emb_dim, self.latent_dim)
self.sequence_pos_encoder_e = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
# # single layer transformers # ## predict relative position for each base point? # existing model
# if self.arch == 'trans_enc':
print("TRANS_ENC init")
seqTransEncoderLayer_e = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.seqTransEncoder_e = nn.TransformerEncoder(seqTransEncoderLayer_e,
num_layers=self.num_layers)
print("TRANS_ENC init")
# logvar_seqTransEncoder_e,
seqTransEncoderLayer_e_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.logvar_seqTransEncoder_e = nn.TransformerEncoder(seqTransEncoderLayer_e_logvar,
num_layers=self.num_layers)
#
# elif self.arch == 'trans_dec':
# print("TRANS_DEC init")
# seqTransDecoderLayer_e = nn.TransformerDecoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=activation)
# self.seqTransDecoder_e = nn.TransformerDecoder(seqTransDecoderLayer_e,
# num_layers=self.num_layers)
# elif self.arch == 'gru': ## arch ##
# print("GRU init")
# self.gru_e = nn.GRU(self.latent_dim, self.latent_dim, num_layers=self.num_layers, batch_first=True)
# else:
# raise ValueError('Please choose correct architecture [trans_enc, trans_dec, gru]')
# tiemstep # # timestep embedding e # Embed timestep e #
self.embed_timestep_e = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder_e)
self.sequence_pos_denoising_encoder_e = PositionalEncoding(self.latent_dim, self.dropout)
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process #
self.basejtse_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder_e)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.basejtse_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
) ### basejtse_denoising_seqTransEncoder ###
else:
basejtse_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.basejtse_denoising_seqTransEncoder = nn.TransformerEncoder(basejtse_seqTransEncoderLayer,
num_layers=self.num_layers)
# basejtse_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=self.activation)
# self.basejtse_denoising_seqTransEncoder = nn.TransformerEncoder(basejtse_seqTransEncoderLayer,
# num_layers=self.num_layers)
# seq_len x bsz x dim
if self.args.const_noise:
# 1) max pool latents over the sequence
# 2) transform the pooled latnets via the linear layer
self.basejtse_denoising_seqTransEncoder = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
nn.Linear(self.latent_dim * 2, self.latent_dim)
)
# self.output_process_e = OutputProcessObjBaseV3(self.data_rep, self.latent_dim)
self.output_process_e = OutputProcessObjBaseERaw(self.data_rep, self.latent_dim)
# self.rot2xyz = Rotation2xyz(device='cpu', dataset=self.dataset)
def set_enc_to_eval(self):
# jts: joint_sequence_input_process, joint_sequence_pos_encoder, joint_sequence_seqTransEncoder, joint_sequence_logvar_seqTransEncoder
# basejtsrel: input_process, sequence_pos_encoder, seqTransEncoder, logvar_seqTransEncoder,
# basejtse: input_process_e, sequence_pos_encoder_e, seqTransEncoder_e, logvar_seqTransEncoder_e
if self.diff_jts:
self.joint_sequence_input_process.eval()
self.joint_sequence_pos_encoder.eval()
self.joint_sequence_seqTransEncoder.eval()
self.joint_sequence_logvar_seqTransEncoder.eval()
if self.diff_basejtse:
self.input_process_e.eval()
self.sequence_pos_encoder_e.eval()
self.seqTransEncoder_e.eval()
self.logvar_seqTransEncoder_e.eval()
if self.diff_basejtsrel:
self.input_process.eval()
self.sequence_pos_encoder.eval()
self.seqTransEncoder.eval() # seqTransEncoder, logvar_seqTransEncoder
self.logvar_seqTransEncoder.eval()
def set_bn_to_eval(self):
if self.args.use_objbase_v6: # real_basejtsrel_input_process
try:
self.real_basejtsrel_input_process.pnpp_conv_net.set_bn_no_training()
except:
pass
def parameters_wo_clip(self):
return [p for name, p in self.named_parameters() if not name.startswith('clip_model.')]
def load_and_freeze_clip(self, clip_version):
clip_model, clip_preprocess = clip.load(clip_version, device='cpu',
jit=False) # Must set jit=False for training
clip.model.convert_weights( # encode # ours float
clip_model) # Actually this line is unnecessary since clip by default already on float16 ### ours
# Freeze CLIP weights
clip_model.eval()
for p in clip_model.parameters():
p.requires_grad = False
return clip_model
def mask_cond(self, cond, force_mask=False):
bs, d = cond.shape
if force_mask:
return torch.zeros_like(cond)
elif self.training and self.cond_mask_prob > 0.:
mask = torch.bernoulli(torch.ones(bs, device=cond.device) * self.cond_mask_prob).view(bs, 1) # 1-> use null_cond, 0-> use real cond
return cond * (1. - mask)
else:
return cond
def encode_text(self, raw_text):
# raw_text - list (batch_size length) of strings with input text prompts #
device = next(self.parameters()).device
max_text_len = 20 if self.dataset in ['humanml', 'kit', 'motion_ours'] else None # Specific hardcoding for humanml dataset
if max_text_len is not None:
default_context_length = 77
context_length = max_text_len + 2 # start_token + 20 + end_token
assert context_length < default_context_length
texts = clip.tokenize(raw_text, context_length=context_length, truncate=True).to(device) # [bs, context_length] # if n_tokens > context_length -> will truncate
# print('texts', texts.shape)
zero_pad = torch.zeros([texts.shape[0], default_context_length-context_length], dtype=texts.dtype, device=texts.device)
texts = torch.cat([texts, zero_pad], dim=1)
# print('texts after pad', texts.shape, texts)
else: ##
texts = clip.tokenize(raw_text, truncate=True).to(device) # [bs, context_length] # if n_tokens > 77 -> will truncate
return self.clip_model.encode_text(texts).float()
def dec_jts_only_fr_latents(self, latents_feats):
joint_seq_output = self.joint_sequence_output_process(latents_feats) # [bs, njoints, nfeats, nframes]
# bsz x ws x nnj x nnfeats # --> joints_seq_outputs #
joint_seq_output = joint_seq_output.permute(0, 3, 1, 2).contiguous()
## joints seq outputs ##
diff_jts_dict = {
"joint_seq_output": joint_seq_output,
"joints_seq_latents": latents_feats,
}
return diff_jts_dict
def dec_basejtsrel_only_fr_latents(self, latent_feats, x):
# basejtsrel_seq_latents_pred_feats
avg_jts_seq_latents = latent_feats[0:1, ...]
other_basejtsrel_seq_latents = latent_feats[1:, ...]
avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
basejtsrel_dec_out = {
'avg_jts_outputs': avg_jts_outputs,
'basejtsrel_output': basejtsrel_output['dec_rel'],
}
return basejtsrel_dec_out
# def dec_latents_to_joints_with_t(self, joints_seq_latents, timesteps):
def dec_latents_to_joints_with_t(self, input_latent_feats, x, timesteps):
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
# joints_seq_latents: seq x bs x d --> perturbed joitns_seq_latents \in [-1, 1] ##
# def dec_latents_to_joints_with_t(self, joints_seq_latents, timesteps):
## positional encoding for denoising ##
# rt_dict = {
# 'joint_seq_output': joint_seq_output,
# 'rel_base_pts_outputs': rel_base_pts_outputs,
# }
rt_dict = {}
if self.diff_jts:
####### input latent feats #######
joints_seq_latents = input_latent_feats["joints_seq_latents"]
if not self.args.without_dec_pos_emb:
joints_seq_latents = self.joint_sequence_denoising_pos_encoder(joints_seq_latents)
# ### GET joints seq time embeddings ### ### embed time stamps ###
# joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
# joints_seq_latents = torch.cat(
# [joints_seq_time_emb, joints_seq_latents], dim=0
# )
# joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)[1:] # seq x bs x d
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
joints_seq_time_emb = joints_seq_time_emb.repeat(joints_seq_latents.size(0), 1, 1).contiguous()
joints_seq_latents = joints_seq_latents + joints_seq_time_emb
if self.args.use_ours_transformer_enc:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
joints_seq_latents = joints_seq_latents.permute(1, 0, 2)
else:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
joints_seq_latents = torch.cat(
[joints_seq_time_emb, joints_seq_latents], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
joints_seq_latents = joints_seq_latents.permute(1, 0, 2)[1:]
else:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)[1:]
# joints_seq_latents: seq_len x bsz x latent_dim #
if self.args.const_noise:
seq_len = joints_seq_latents.size(0)
# if self.args.const_noise:
joints_seq_latents, _ = torch.max(joints_seq_latents, dim=0, keepdim=True)
joints_seq_latents = self.glb_denoising_latents_trans_layer(joints_seq_latents) # seq_len x bsz x latent_dim
joints_seq_latents = joints_seq_latents.repeat(seq_len, 1, 1).contiguous()
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
joints_seq_latents = input_latent_feats["joints_seq_latents_enc"]
# bsz x ws x nnj x 3 #
joint_seq_output = self.joint_sequence_output_process(joints_seq_latents) # [bs, njoints, nfeats, nframes]
# bsz x ws x nnj x nnfeats # --> joints_seq_outputs #
joint_seq_output = joint_seq_output.permute(0, 3, 1, 2).contiguous()
diff_jts_dict = {
"joint_seq_output": joint_seq_output,
"joints_seq_latents": joints_seq_latents,
}
else:
diff_jts_dict = {}
if self.diff_basejtsrel:
rel_base_pts_outputs = input_latent_feats["rel_base_pts_outputs"]
if rel_base_pts_outputs.size(0) == 1 and self.args.single_frame_noise:
rel_base_pts_outputs = rel_base_pts_outputs.repeat(self.args.window_size + 1, 1, 1)
if not self.args.without_dec_pos_emb: # without
avg_jts_inputs = rel_base_pts_outputs[0:1, ...]
other_rel_base_pts_outputs = rel_base_pts_outputs[1: , ...]
other_rel_base_pts_outputs = self.sequence_pos_denoising_encoder(other_rel_base_pts_outputs)
rel_base_pts_outputs = torch.cat(
[avg_jts_inputs, other_rel_base_pts_outputs], dim=0
)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_time_emb = basejtsrel_time_emb.repeat(rel_base_pts_outputs.size(0), 1, 1).contiguous()
basejtsrel_seq_latents = rel_base_pts_outputs + basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_seq_latents = torch.cat(
[basejtsrel_time_emb, rel_base_pts_outputs], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
basejtsrel_seq_latents = input_latent_feats["rel_base_pts_outputs_enc"]
if basejtsrel_seq_latents.size(0) == 1 and self.args.single_frame_noise:
basejtsrel_seq_latents = basejtsrel_seq_latents.repeat(self.args.window_size + 1, 1, 1)
basejtsrel_seq_latents_pred_feats = basejtsrel_seq_latents
elif self.args.pred_diff_noise:
basejtsrel_seq_latents_pred_feats = input_latent_feats["rel_base_pts_outputs"] - basejtsrel_seq_latents
else:
basejtsrel_seq_latents_pred_feats = basejtsrel_seq_latents
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
### GET joints seq output ###
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process #
# basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
# basejtsrel_seq_latents = torch.cat(
# [basejtsrel_time_emb, rel_base_pts_outputs], dim=0
# )
# basejtsrel_seq_latents_pred_feats
avg_jts_seq_latents = basejtsrel_seq_latents_pred_feats[0:1, ...]
other_basejtsrel_seq_latents = basejtsrel_seq_latents_pred_feats[1:, ...]
avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
####
# avg_jts_seq_latents = basejtsrel_seq_latents[0:1, ...]
# other_basejtsrel_seq_latents = basejtsrel_seq_latents[1:, ...]
# avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
# avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# # basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
diff_basejtsrel_dict = {
"basejtsrel_output": basejtsrel_output['dec_rel'],
"basejtsrel_seq_latents": basejtsrel_seq_latents,
"avg_jts_outputs": avg_jts_outputs,
}
else:
diff_basejtsrel_dict = {}
if self.diff_basejtse:
# e_disp_rel_to_base_along_normals = input_latent_feats['e_disp_rel_to_base_along_normals']
# e_disp_rel_to_baes_vt_normals = input_latent_feats['e_disp_rel_to_baes_vt_normals']
base_jts_e_feats = input_latent_feats['base_jts_e_feats'] # seq x bs x d --> e feats
if not self.args.without_dec_pos_emb:
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_time_emb = base_jts_e_time_emb.repeat(base_jts_e_feats.size(0), 1, 1).contiguous()
base_jts_e_feats = base_jts_e_feats + base_jts_e_time_emb
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)
else:
#### Decode e_along_normals and e_vt_normals ####
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_feats = torch.cat(
[base_jts_e_time_emb, base_jts_e_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)[1:]
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
base_jts_e_feats = input_latent_feats["base_jts_e_feats_enc"]
# base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
#### Decode e_along_normals and e_vt_normals ####
### bae jts e embeddings feats ###
# base_jts_e_time_emb = self.embed_timestep_e(timesteps)
# base_jts_e_feats = torch.cat(
# [base_jts_e_time_emb, base_jts_e_feats], dim=0
# )
# base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
base_jts_e_output = self.output_process_e(base_jts_e_feats, x)
# dec_e_along_normals: bsz x (ws - 1) x nnb x nnj #
dec_e_along_normals = base_jts_e_output['dec_e_along_normals']
dec_e_vt_normals = base_jts_e_output['dec_e_vt_normals']
dec_e_along_normals = dec_e_along_normals.contiguous().permute(0, 1, 3, 2).contiguous()
dec_e_vt_normals = dec_e_vt_normals.contiguous().permute(0, 1, 3, 2).contiguous()
#### Decode e_along_normals and e_vt_normals ####
diff_basejtse_dict = {
'dec_e_along_normals': dec_e_along_normals,
'dec_e_vt_normals': dec_e_vt_normals,
'base_jts_e_feats': base_jts_e_feats,
}
else:
diff_basejtse_dict = {}
rt_dict = {}
rt_dict.update(diff_jts_dict)
rt_dict.update(diff_basejtsrel_dict)
rt_dict.update(diff_basejtse_dict)
### rt_dict --> rt_dict of joints, rel ###
return rt_dict
# return joint_seq_output, joints_seq_latents
def reparameterization(self, val_mean, val_var):
val_noise = torch.randn_like(val_mean)
val_sampled = val_mean + val_noise * val_var ### sample the value
if self.args.rnd_noise:
val_sampled = val_noise
return val_sampled
def decode_realbasejtsrel_from_objbasefeats(self, objbasefeats, input_data):
real_dec_basejtsrel = self.real_basejtsrel_output_process(
objbasefeats, input_data
)
# real_dec_basejtsrel -> decoded realtive positions #
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous()
return real_dec_basejtsrel
def denoising_realbasejtsrel_objbasefeats(self, pert_obj_base_pts_feats, timesteps):
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(pert_obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = pert_obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq des
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, pert_obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# bsz, nframes, nnb, nnj, 3 -->
#
# real_dec_basejtsrel = self.real_basejtsrel_output_process(
# real_basejtsrel_seq_latents, x
# )
return real_basejtsrel_seq_latents
def forward(self, x, timesteps):
"""
x: [batch_size, njoints, nfeats, max_frames], denoted x_t in the paper
timesteps: [batch_size] (int)
"""
# joint_sequence_input_process; joint_sequence_pos_encoder; joint_sequence_seqTransEncoder; joint_sequence_seqTransDecoder; joint_sequence_embed_timestep; joint_sequence_output_process #
# bsz, nframes, nnj = x['pert_rhand_joints'].shape[:3]
# pert_rhand_joints = x['pert_rhand_joints'] # bsz x ws x nnj x 3 # bsz x nnj x 3 x ws
bsz, nframes, nnj = x['rhand_joints'].shape[:3]
pert_rhand_joints = x['rhand_joints'] # bsz x ws x nnj x 3 # bsz x nnj x 3 x ws
base_pts = x['base_pts'] ### bsz x nnb x 3 ###
base_normals = x['base_normals'] ### bsz x nnb x 3 ### --> base normals ###
# base_normals # ##
rt_dict = {}
## # # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
if self.diff_basejtse:
### Embed physicss quantities ###
# e_disp_rel_to_base_along_normals: bsz x (ws - 1) x nnj x nnb #
# e_disp_rel_to_baes_vt_normals: bsz x (ws - 1) x nnj x nnb #
# e_disp_rel_to_base_along_normals = x['e_disp_rel_to_base_along_normals']
# e_disp_rel_to_baes_vt_normals = x['e_disp_rel_to_baes_vt_normals']
e_disp_rel_to_base_along_normals = x['pert_e_disp_rel_to_base_along_normals']
e_disp_rel_to_baes_vt_normals = x['pert_e_disp_rel_to_base_vt_normals']
nnb = base_pts.size(1)
disp_ws = e_disp_rel_to_base_along_normals.size(1) ### --> base normals ###
base_pts_disp_exp = base_pts.unsqueeze(1).unsqueeze(1).repeat(1, disp_ws, nnj, 1, 1).contiguous()
base_normals_disp_exp = base_normals.unsqueeze(1).unsqueeze(1).repeat(1, disp_ws, nnj, 1, 1).contiguous()
# bsz x (ws - 1) x nnj x nnb x (3 + 3 + 1 + 1)
base_pts_normals_e_in_feats = torch.cat( # along normals; # vt normals #
[base_pts_disp_exp, base_normals_disp_exp, e_disp_rel_to_base_along_normals.unsqueeze(-1), e_disp_rel_to_baes_vt_normals.unsqueeze(-1)], dim=-1
)
base_pts_normals_e_in_feats = base_pts_normals_e_in_feats.permute(0, 1, 3, 2, 4).contiguous()
# bsz x (ws - 1) x nnb x (nnj x (xxx feats_dim))
base_pts_normals_e_in_feats = base_pts_normals_e_in_feats.view(bsz, disp_ws, nnb, -1).contiguous()
## input process ##
base_jts_e_feats = self.input_process_e(base_pts_normals_e_in_feats)
base_jts_e_feats = self.sequence_pos_encoder_e(base_jts_e_feats)
## seq transformation for e ## #
base_jts_e_feats_mean = self.seqTransEncoder_e(base_jts_e_feats) ## mean, mdm_ours ##
# print(f"base_jts_e_feats: {base_jts_e_feats.size()}")
### Embed physicss quantities ###
#### base_jts_e_feats, base_jts_e_feats_mean ####
# ## us basejtsefeats for denoising directly ##
base_jts_e_feats = base_jts_e_feats_mean
if not self.args.without_dec_pos_emb: ## use positional encoding ##
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_time_emb = base_jts_e_time_emb.repeat(base_jts_e_feats.size(0), 1, 1).contiguous()
base_jts_e_feats = base_jts_e_feats + base_jts_e_time_emb
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)
else:
#### Decode e_along_normals and e_vt_normals ####
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_feats = torch.cat(
[base_jts_e_time_emb, base_jts_e_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)[1:]
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
# ### sequence latents ###
# if self.args.train_enc: # trian enc for seq latents ###
# base_jts_e_feats = input_latent_feats["base_jts_e_feats_enc"]
# base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
#### Decode e_along_normals and e_vt_normals ####
### bae jts e embeddings feats ###
# base_jts_e_time_emb = self.embed_timestep_e(timesteps)
# base_jts_e_feats = torch.cat(
# [base_jts_e_time_emb, base_jts_e_feats], dim=0
# )
# base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
##### output_process_e -> output energies #####
base_jts_e_output = self.output_process_e(base_jts_e_feats, x)
# dec_e_along_normals: bsz x (ws - 1) x nnb x nnj #
dec_e_along_normals = base_jts_e_output['dec_e_along_normals']
dec_e_vt_normals = base_jts_e_output['dec_e_vt_normals']
dec_e_along_normals = dec_e_along_normals.contiguous().permute(0, 1, 3, 2).contiguous() # bsz x (ws - 1) x nnj x nnb
dec_e_vt_normals = dec_e_vt_normals.contiguous().permute(0, 1, 3, 2).contiguous() # bsz x (ws - 1) x nnj x nnb
#### Decode e_along_normals and e_vt_normals ####
diff_basejtse_dict = {
'dec_e_along_normals': dec_e_along_normals,
'dec_e_vt_normals': dec_e_vt_normals,
'base_jts_e_feats': base_jts_e_feats,
}
# rt_dict['base_jts_e_feats'] = base_jts_e_feats
# rt_dict['base_jts_e_feats_mean'] = base_jts_e_feats_mean
# rt_dict['base_jts_e_feats_logvar'] = base_jts_e_feats_logvar # log_var #
else:
diff_basejtse_dict = {}
if self.diff_jts:
# base_pts_normal
### InputProcess ###
pert_rhand_joints_trans = pert_rhand_joints.permute(0, 2, 3, 1).contiguous() # bsz x nnj x 3 x ws #
rhand_joints_feats = self.joint_sequence_input_process(pert_rhand_joints_trans) # [seqlen, bs, d]
### InputProcessObjBase ###
# rhand_joints_feats = self.joint_sequence_input_process(pert_rhand_joints)
### === Encode input joint sequences === ###
# bs, njoints, nfeats, nframes = x.shape
# rhand_joints_emb = self.joint_sequence_embed_timestep(timesteps) # [1, bs, d]
# if self.arch == 'trans_enc':
xseq = rhand_joints_feats # [seqlen+1, bs, d]
xseq = self.joint_sequence_pos_encoder(xseq) # [seqlen+1, bs, d]
joint_seq_output_mean = self.joint_sequence_seqTransEncoder(xseq) # [1:] # , src_key_padding_mask=~maskseq) # [seqlen, bs, d]
### calculate logvar, mean, and feats ###
joint_seq_output_logvar = self.joint_sequence_logvar_seqTransEncoder(xseq)
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
joint_seq_output_var = torch.exp(joint_seq_output_logvar) # seq x bs x d --> encodeing and decoding
## base_jts_e_feats: seqlen x bs x d --> val latents ##
joint_seq_output = self.reparameterization(joint_seq_output_mean, joint_seq_output_var)
rt_dict['joint_seq_output'] = joint_seq_output
# rt_dict['joint_seq_output'] = joint_seq_output_mean
rt_dict['joint_seq_output_mean'] = joint_seq_output_mean
rt_dict['joint_seq_output_logvar'] = joint_seq_output_logvar
if self.args.diff_realbasejtsrel_to_joints: # nframes x nnbase x nnjts x (base pts + base normals + 3) 2) point feature for each point; point feature for; condition on the noisy input for the denoised information
# real_basejtsrel_to_joints_input_process, real_basejtsrel_to_joints_sequence_pos_encoder, real_basejtsrel_to_joints_seqTransEncoder
# real_basejtsrel_to_joints_embed_timestep, real_basejtsrel_to_joints_sequence_pos_denoising_encoder, real_basejtsrel_to_joints_denoising_seqTransEncoder, real_basejtsrel_to_joints_output_process
bsz, nf, nnj, nnb = x['pert_rel_base_pts_to_joints_for_jts_pred'].size()[:4]
normed_base_pts = x['normed_base_pts']
base_normals = x['base_normals']
pert_rel_base_pts_to_rhand_joints = x['pert_rel_base_pts_to_joints_for_jts_pred'] # bsz x nf x nnj x nnb x 3
## use_abs_jts_pos --> obj jts pos for the encodingj
if self.args.use_abs_jts_pos: ## bsz x nf x nnj x nnb x 3 ## ---> abs jts pos ##
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1)
# use_abs_jts_for_encoding, real_basejtsrel_to_joints_input_process
if self.args.use_abs_jts_for_encoding:
if not self.args.use_abs_jts_pos:
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1) # pert_rel_base_pts_to_rhand_joints: bsz x nf x nnj x nnb x 3
abs_jts = pert_rel_base_pts_to_rhand_joints[..., 0, :]
abs_jts = abs_jts.permute(0, 2, 3, 1).contiguous()
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(abs_jts)
elif self.args.use_abs_jts_for_encoding_obj_base:
if not self.args.use_abs_jts_pos:
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1) # pert_rel_base_pts_to_rhand_joints: bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints[:, :, :, 0:1, :]
# obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
# [normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1)[:, :, :, 0:1, :], base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1)[:, :, :, 0:1, :], pert_rel_base_pts_to_rhand_joints], dim=-1
# )
obj_base_in_feats = pert_rel_base_pts_to_rhand_joints
# --> tnrasform the input feature dim to 21 * 3 here for encoding #
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, 1, -1).contiguous() #
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(obj_base_in_feats) # nf x bsz x feat_dim #
else:
if self.args.use_objbase_v2:
# bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
[normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
)
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(obj_base_in_feats) # nf x bsz x feat_dim #
# ### real_basejtsrel_to_joints_input_process --> real_basejtsrel_to_joints_input_process --> for the joints and input process ### # obj_base_encoded_feats
# obj_base_encoded_feats
obj_base_pts_feats_pos_embedding = self.real_basejtsrel_to_joints_sequence_pos_encoder(obj_base_encoded_feats)
obj_base_pts_feats = self.real_basejtsrel_to_joints_seqTransEncoder(obj_base_pts_feats_pos_embedding)
if self.args.use_sigmoid:
obj_base_pts_feats = (torch.sigmoid(obj_base_pts_feats) - 0.5) * 2.
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_to_joints_embed_timestep(timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq des
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_to_joints_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
joints_offset_output = self.real_basejtsrel_to_joints_output_process(real_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2)
if self.args.diff_basejtsrel:
diff_basejtsrel_to_joints_dict = {
'joints_offset_output_from_rel': joints_offset_output
}
else:
diff_basejtsrel_to_joints_dict = {
'joints_offset_output': joints_offset_output
}
else:
diff_basejtsrel_to_joints_dict = {}
if self.diff_realbasejtsrel: # real_dec_basejtsrel
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
bsz, nf, nnj, nnb = x['pert_rel_base_pts_to_rhand_joints'].size()[:4]
normed_base_pts = x['normed_base_pts']
base_normals = x['base_normals']
pert_rel_base_pts_to_rhand_joints = x['pert_rel_base_pts_to_rhand_joints'] # bsz x nf x nnj x nnb x 3
if self.args.use_objbase_v2:
# bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.use_objbase_v4:
# use_objbase_v4: # use_objbase_out_v4
exp_normed_base_pts = normed_base_pts.unsqueeze(1).unsqueeze(2).repeat(1, nf, nnj, 1, 1).contiguous()
exp_base_normals = base_normals.unsqueeze(1).unsqueeze(2).repeat(1, nf, nnj, 1, 1).contiguous()
obj_base_in_feats = torch.cat(
[pert_rel_base_pts_to_rhand_joints, exp_normed_base_pts, exp_base_normals], dim=-1 # bsz x nf x nnj x nnb x (3 + 3 + 3) # -> exp_base_normals
)
obj_base_in_feats = obj_base_in_feats.view(bsz, nf, nnj, -1).contiguous()
elif self.args.use_objbase_v5: # use_objbase_v5, use_objbase_out_v5
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
if self.args.v5_in_not_base:
obj_base_in_feats = torch.cat(
[ pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.v5_in_not_base_pos:
obj_base_in_feats = torch.cat(
[base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.use_objbase_v6 or self.args.use_objbase_v7:
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
[normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
)
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
# obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
# [normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
# )
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
# obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
if self.args.use_objbase_v6:
normed_base_pts_exp = normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1) # and repeat for the base pts #
obj_base_encoded_feats = self.real_basejtsrel_input_process(obj_base_in_feats, normed_base_pts_exp)
else:
obj_base_encoded_feats = self.real_basejtsrel_input_process(obj_base_in_feats) # nf x bsz x feat_dim # nf x bsz x nnbasepts x feats_dim #
# obj_base_encoded_feats
obj_base_pts_feats_pos_embedding = self.real_basejtsrel_sequence_pos_encoder(obj_base_encoded_feats)
obj_base_pts_feats = self.real_basejtsrel_seqTransEncoder(obj_base_pts_feats_pos_embedding)
if self.args.use_sigmoid:
obj_base_pts_feats = (torch.sigmoid(obj_base_pts_feats) - 0.5) * 2.
if self.args.train_enc:
# basejtsrel_seq
# bsz, nframes, nnb, nnj, 3 -->
#
real_dec_basejtsrel = self.real_basejtsrel_output_process(
obj_base_pts_feats, x
)
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
if self.args.use_objbase_out_v3:
real_dec_basejtsrel = real_dec_basejtsrel
else:
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous() # bsz x nf x nnj x nnb x 3
else:
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
# print(f"timesteps: {timesteps.size()}, obj_base_pts_feats: {obj_base_pts_feats.size()}")
if self.args.use_objbase_v5:
cur_timesteps = timesteps.unsqueeze(1).repeat(1, nnb).view(-1)
else:
cur_timesteps = timesteps
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(cur_timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq des
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_seq
# bsz, nframes, nnb, nnj, 3 -->
#
if self.args.use_jts_pert_realbasejtsrel:
joints_offset_output = self.real_basejtsrel_output_process(real_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2) # bsz x nf x nnj x 3
real_dec_basejtsrel = joints_offset_output.unsqueeze(-2).repeat(1, 1, 1, nnb, 1)
# real_dec_basejtsrel = joints_offset_output
else:
real_dec_basejtsrel = self.real_basejtsrel_output_process(
real_basejtsrel_seq_latents, x
)
# real_dec_basejtsrel -> decoded realtive positions #
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
if self.args.use_objbase_out_v3 or self.args.use_objbase_out_v4 or self.args.use_objbase_out_v5:
real_dec_basejtsrel = real_dec_basejtsrel
else:
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous() # bsz x nf x nnj x nnb x 3
diff_realbasejtsrel_out_dict = {
'real_dec_basejtsrel': real_dec_basejtsrel,
'obj_base_pts_feats': obj_base_pts_feats,
}
else:
diff_realbasejtsrel_out_dict = {}
# relative joints encoder; obj pos encoder; obj pos encoder; # penetrations, depth, --- how to use depth for guidance -> and also penetrations # object penetrations #
if self.diff_basejtsrel:
# joints_offset_sequence --> x['pert_joints_offset_sequence']
joints_offset_sequence = x['pert_joints_offset_sequence'] # bsz x nf x nnj x 3
joints_offset_sequence = joints_offset_sequence.permute(0, 2, 3, 1).contiguous()
joints_offset_feats = self.joints_offset_input_process(joints_offset_sequence) # nf x bsz x dim
# rel_base_pts_feats = self.input_process(basejtsrel_enc_in_feats)
# sequence_pos_encoder
rel_base_pts_feats_pos_embedding = self.sequence_pos_encoder(joints_offset_feats)
# print(f"joints_offset_feats: {joints_offset_feats.size()}, rel_base_pts_feats_pos_embedding: {rel_base_pts_feats_pos_embedding.size()}")
# outputs rel base jts encoded latents ##
# seqTransEncoder, logvar_seqTransEncoder
# rel_base_pts_outputs_mean = self.basejtsrel_denoising_seqTransEncoder(rel_base_pts_feats_pos_embedding)
# ### calculate logvar, mean, and feats ###
# rel_base_pts_outputs_logvar = self.joint_sequence_logvar_seqTransEncoder(rel_base_pts_outputs)
if self.args.not_diff_avgjts: # not use diff avgjts ##
rel_base_pts_feats = rel_base_pts_feats_pos_embedding
# seqTransEncoder, logvar_seqTransEncoder #
rel_base_pts_outputs_mean = self.seqTransEncoder(rel_base_pts_feats)
# print(f"rel_base_pts_outputs_mean 1: {rel_base_pts_outputs_mean.size()}")
if not self.args.without_dec_pos_emb: # without dec pos embedding
# avg_jts_inputs = rel_base_pts_outputs_mean[0:1, ...]
other_rel_base_pts_outputs = rel_base_pts_outputs_mean #
other_rel_base_pts_outputs = self.sequence_pos_denoising_encoder(other_rel_base_pts_outputs)
rel_base_pts_outputs_mean = other_rel_base_pts_outputs
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_time_emb = basejtsrel_time_emb.repeat(rel_base_pts_outputs_mean.size(0), 1, 1).contiguous()
basejtsrel_seq_latents = rel_base_pts_outputs_mean + basejtsrel_time_emb ### time embeddings and relbaseptsoutputs
# print(f"basejtsrel_seq_latents: {basejtsrel_seq_latents.size()}, rel_base_pts_outputs_mean: {rel_base_pts_outputs_mean.size()}, basejtsrel_time_emb: {basejtsrel_time_emb.size()}")
if self.args.use_ours_transformer_enc:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_seq_latents = torch.cat(
[basejtsrel_time_emb, rel_base_pts_outputs_mean], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_seq_latents_pred_feats
# avg_jts_seq_latents = basejtsrel_seq_latents[0:1, ...]
other_basejtsrel_seq_latents = basejtsrel_seq_latents # [1:, ...]
joints_offset_output = self.joint_offset_output_process(other_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2)
# print(f"joints_offset_output in MDM: {joints_offset_output.size()}, joints_offset_sequence: {joints_offset_sequence.size()}, other_basejtsrel_seq_latents: {other_basejtsrel_seq_latents.size()}")
diff_basejtsrel_dict = {
'joints_offset_output': joints_offset_output
}
# rt_dict['joints_offset_output'] = joints_offset_output
else:
avg_joints_sequence = x['pert_avg_joints_sequence']
avg_joints_sequence_trans = avg_joints_sequence.unsqueeze(-1)
avg_joints_feats = self.avg_joints_sequence_input_process(avg_joints_sequence_trans) ## 1 x bsz x dim ###
rel_base_pts_feats = torch.cat( # (seq_len + 1) x bsz x dim #
[avg_joints_feats, rel_base_pts_feats_pos_embedding], dim=0 ## jrel_base_pts_pos_embedding #
)
## joints embedding for mean statistics and logvar statistics ##
# seqTransEncoder, logvar_seqTransEncoder #
rel_base_pts_outputs_mean = self.seqTransEncoder(rel_base_pts_feats)
if not self.args.without_dec_pos_emb: # without dec pos embedding
avg_jts_inputs = rel_base_pts_outputs_mean[0:1, ...]
other_rel_base_pts_outputs = rel_base_pts_outputs_mean[1: , ...]
other_rel_base_pts_outputs = self.sequence_pos_denoising_encoder(other_rel_base_pts_outputs)
rel_base_pts_outputs_mean = torch.cat(
[avg_jts_inputs, other_rel_base_pts_outputs], dim=0
)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_time_emb = basejtsrel_time_emb.repeat(rel_base_pts_outputs_mean.size(0), 1, 1).contiguous()
basejtsrel_seq_latents = rel_base_pts_outputs_mean + basejtsrel_time_emb ### time embeddings and relbaseptsoutputs
if self.args.use_ours_transformer_enc:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_seq_latents = torch.cat(
[basejtsrel_time_emb, rel_base_pts_outputs_mean], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_seq_latents_pred_feats
avg_jts_seq_latents = basejtsrel_seq_latents[0:1, ...]
other_basejtsrel_seq_latents = basejtsrel_seq_latents[1:, ...]
avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
joints_offset_output = self.joint_offset_output_process(other_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2)
rt_dict['joints_offset_output'] = joints_offset_output
rt_dict['avg_jts_outputs'] = avg_jts_outputs
else:
diff_basejtsrel_dict = {}
rt_dict = {}
rt_dict.update(diff_basejtsrel_dict)
rt_dict.update(diff_basejtse_dict) ### rt_dict and diff_basejtse
rt_dict.update(diff_basejtsrel_to_joints_dict)
rt_dict.update(diff_realbasejtsrel_out_dict) ### diff
return rt_dict
def _apply(self, fn):
super()._apply(fn)
# self.rot2xyz.smpl_model._apply(fn)
def train(self, *args, **kwargs):
super().train(*args, **kwargs)
# self.rot2xyz.smpl_model.train(*args, **kwargs)
### MDM 10 ###
class MDMV12(nn.Module):
def __init__(self, modeltype, njoints, nfeats, num_actions, translation, pose_rep, glob, glob_rot,
latent_dim=256, ff_size=1024, num_layers=8, num_heads=4, dropout=0.1,
ablation=None, activation="gelu", legacy=False, data_rep='rot6d', dataset='amass', clip_dim=512,
arch='trans_enc', emb_trans_dec=False, clip_version=None, **kargs):
super().__init__()
self.legacy = legacy
self.modeltype = modeltype
self.njoints = njoints
self.nfeats = nfeats
self.num_actions = num_actions
self.data_rep = data_rep
self.dataset = dataset
self.pose_rep = pose_rep
self.glob = glob
self.glob_rot = glob_rot
self.translation = translation
self.latent_dim = latent_dim
self.ff_size = ff_size
self.num_layers = num_layers
self.num_heads = num_heads
self.dropout = dropout
self.ablation = ablation
self.activation = activation
self.clip_dim = clip_dim
self.action_emb = kargs.get('action_emb', None)
self.input_feats = self.njoints * self.nfeats
self.normalize_output = kargs.get('normalize_encoder_output', False)
self.cond_mode = kargs.get('cond_mode', 'no_cond')
self.cond_mask_prob = kargs.get('cond_mask_prob', 0.) #
### GET args ###
self.args = kargs.get('args', None)
### GET the diff. suit ###
self.diff_jts = self.args.diff_jts
self.diff_basejtsrel = self.args.diff_basejtsrel
self.diff_basejtse = self.args.diff_basejtse
self.diff_realbasejtsrel = self.args.diff_realbasejtsrel
self.diff_realbasejtsrel_to_joints = self.args.diff_realbasejtsrel_to_joints
### GET the diff. suit ###
self.arch = arch
## ==== gru_emb_dim ==== ## # gru emb dim #
self.gru_emb_dim = self.latent_dim if self.arch == 'gru' else 0
#
# joint_sequence_input_process; joint_sequence_pos_encoder; joint_sequence_seqTransEncoder; joint_sequence_seqTransDecoder; joint_sequence_embed_timestep; joint_sequence_output_process #
###### ======= Construct joint sequence encoder, communicator, and decoder ======== #######
self.joints_feats_in_dim = 21 * 3
self.data_rep = "xyz"
if self.diff_jts:
## Input process for joints ##
self.joint_sequence_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
# # InputProcessObjBase(self, data_rep, input_feats, latent_dim)
# self.joint_sequence_input_process = InputProcessObjBase(self.data_rep, 3, self.latent_dim)
# self.input_process = InputProcess(self.data_rep, self.input_feats+self.gru_emb_dim, self.latent_dim)
self.joint_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
# if self.arch == 'trans_enc':
print("TRANS_ENC init") ## transformer encoder layer ## UNet
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
## Joint sequence transformer encoder layer ## # sequence encoder #
self.joint_sequence_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
### logvar for the encoding laeyer and
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
seqTransEncoderLayer_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
## Joint sequence transformer encoder layer ## # sequence encoder #
self.joint_sequence_logvar_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer_logvar,
num_layers=self.num_layers)
# elif self.arch == 'trans_dec':
# print("TRANS_DEC init")
# seqTransDecoderLayer = nn.TransformerDecoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads, # num_heads
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=activation)
# self.joint_sequence_seqTransDecoder = nn.TransformerDecoder(seqTransDecoderLayer,
# num_layers=self.num_layers)
# elif self.arch == 'gru':
# print("GRU init")
# self.joint_sequence_gru = nn.GRU(self.latent_dim, self.latent_dim, num_layers=self.num_layers, batch_first=True)
# else:
# raise ValueError('Please choose correct architecture [trans_enc, trans_dec, gru]')
### joint sequence embed timestep ## ## timestep
self.joint_sequence_embed_timestep = TimestepEmbedder(self.latent_dim, self.joint_sequence_pos_encoder)
# self.joint_sequence_output_process = OutputProcess(self.data_rep, self.latent_dim)
# (self, data_rep, input_feats, latent_dim, njoints, nfeats):
#### ====== joint sequence denoising block ====== ####
## seqTransEncoder ##
self.joint_sequence_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.joint_sequence_pos_encoder)
self.joint_sequence_denoising_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc:
self.joint_sequence_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.joint_sequence_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
# seq_len x bsz x dim
if self.args.const_noise:
# 1) max pool latents over the sequence
# 2) transform the pooled latnets via the linear layer
self.glb_denoising_latents_trans_layer = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
nn.Linear(self.latent_dim * 2, self.latent_dim)
)
# refinement for predicted joints # --> not in the paradigm of generation #
# seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=self.activation)
# self.joint_sequence_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
# num_layers=self.num_layers)
#### ====== joint sequence denoiisng block ====== ####
### Output process ### output proces for joint sequence ### # output proces --> datarep, joints feats in dim, latent dim ##
###### joints_feats_in_dim ######
self.joint_sequence_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
# OutputProcessCond
# self.joint_sequence_output_process = OutputProcessCond(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
###### ======= Construct joint sequence encoder, communicator, and decoder ======== #######
# real_basejtsrel_to_joints_embed_timestep, real_basejtsrel_to_joints_sequence_pos_denoising_encoder, real_basejtsrel_to_joints_denoising_seqTransEncoder, real_basejtsrel_to_joints_output_process
if self.diff_realbasejtsrel_to_joints: # feature for each joint point? --> for the denoising purpose #
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
layernorm = True
self.rel_input_feats = 21 * (3 + 3 + 3) # base pts, normals, the relative positions
if self.args.use_abs_jts_for_encoding_obj_base:
self.rel_input_feats = 21 * (3)
# layernorm = False
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
# self.real_basejtsrel_to_joints_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
# elif self.args.use
else:
if self.args.use_objbase_v2:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v3:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_to_joints_input_process = InputProcessObjBaseV3(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
else:
self.real_basejtsrel_to_joints_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
if self.args.use_abs_jts_for_encoding: # use_abs_jts_for_encoding, real_basejtsrel_to_joints_input_process
self.real_basejtsrel_to_joints_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
self.real_basejtsrel_to_joints_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
### Encoding layer ### # InputProcessObjBaseV2
real_basejtsrel_to_joints_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim, # latent dim # nn_heads # ff_size # dropout #
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_to_joints_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_to_joints_seqTransEncoderLayer, # basejtsrel_seqTrans
num_layers=self.num_layers)
### timesteps embedding layer ###
self.real_basejtsrel_to_joints_embed_timestep = TimestepEmbedder(self.latent_dim, self.real_basejtsrel_to_joints_sequence_pos_encoder)
self.real_basejtsrel_to_joints_sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.real_basejtsrel_to_joints_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
real_basejtsrel_to_joints_denoising_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_to_joints_denoising_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_to_joints_denoising_seqTransEncoderLayer,
num_layers=self.num_layers)
# self.real_basejtsrel_output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
self.real_basejtsrel_to_joints_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
# OutputProcessCond
if self.diff_realbasejtsrel:
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
self.rel_input_feats = 21 * (3 + 3 + 3) # base pts, normals, the relative positions
# self.real_basejtsrel_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim)
layernorm = True
if self.args.use_objbase_v2:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV2(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm, glb_feats_trans=True)
elif self.args.use_objbase_v4: # use_objbase_out_v4
self.rel_input_feats = (self.args.nn_base_pts * (3 + 3 + 3)) # current joint positions # how to keep the dimension
self.real_basejtsrel_input_process = InputProcessObjBaseV4(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v5: # use_objbase_v5,
if self.args.v5_in_not_base:
self.rel_input_feats = (21 * 3)
elif self.args.v5_in_not_base_pos:
self.rel_input_feats = 3 + (21 * 3)
else:
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV5(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm, without_glb=self.args.v5_in_without_glb)
elif self.args.use_objbase_v6: # real_basejtsrel_input_process
self.rel_input_feats = 3 + 3 + (21 * 3) + 3
self.real_basejtsrel_input_process = InputProcessObjBaseV6(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
elif self.args.use_objbase_v7:
# InputProcessObjBaseV7
self.rel_input_feats = 3 + 3 + (21 * 3)
self.real_basejtsrel_input_process = InputProcessObjBaseV7(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
else:
self.real_basejtsrel_input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm)
self.real_basejtsrel_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
### Encoding layer ###
real_basejtsrel_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim, # latent dim # nn_heads # ff_size # dropout # # dropout # # dropout
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_seqTransEncoderLayer, # basejtsrel_seqTrans
num_layers=self.num_layers)
### timesteps embedding layer ###
self.real_basejtsrel_embed_timestep = TimestepEmbedder(self.latent_dim, self.real_basejtsrel_sequence_pos_encoder)
self.real_basejtsrel_sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.real_basejtsrel_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
real_basejtsrel_denoising_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.real_basejtsrel_denoising_seqTransEncoder = nn.TransformerEncoder(real_basejtsrel_denoising_seqTransEncoderLayer,
num_layers=self.num_layers)
print(f"not_cond_base: {self.args.not_cond_base}, latent_dim: {self.latent_dim}")
if self.args.use_jts_pert_realbasejtsrel:
print(f"use_jts_pert_realbasejtsrel!!!!!!")
self.real_basejtsrel_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
else:
if self.args.use_objbase_out_v3:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV3(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
elif self.args.use_objbase_out_v4:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV4(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
elif self.args.use_objbase_out_v5: # use_objbase_v5, use_objbase_out_v5
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV5(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base, out_objbase_v5_bundle_out=self.args.out_objbase_v5_bundle_out, v5_out_not_cond_base=self.args.v5_out_not_cond_base)
else:
self.real_basejtsrel_output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
# OutputProcessCond
if self.diff_basejtsrel: ## basejtsrel ##
# treate them as textures of signals to model # # base pts -> dec on base pts features -->
# latent space denoising and feature decoding --> a little bit concern about the feature decoding process #
# TODO: add base_pts and base_normals to the base points -rel-to- rhand joints encoding process #
self.rel_input_feats = 21 * (3 + 3 + 3) # relative positions from base pts to rhand joints ##
# cond_real_basejtsrel_input_process, cond_real_basejtsrel_pos_encoder, cond_real_basejtsrel_seqTransEncoderLayer, cond_trans_linear_layer #
# Conditional strategy 1: use the relative position embeddings for guidance (cannot use the origianl weights for finetuning) #
layernorm = True
# [finetune_with_cond_rel, finetune_with_cond_jtsobj]
if self.args.finetune_with_cond_rel:
if self.args.use_objbase_v5: # use_objbase_v5 #
if self.args.v5_in_not_base:
self.rel_input_feats = (21 * 3)
elif self.args.v5_in_not_base_pos:
self.rel_input_feats = 3 + (21 * 3)
else: # as an additional conditions for the input and denoising #
self.rel_input_feats = 3 + 3 + (21 * 3)
self.cond_real_basejtsrel_input_process = InputProcessObjBaseV5(self.data_rep, self.rel_input_feats, self.latent_dim, layernorm=layernorm, without_glb=self.args.v5_in_without_glb, only_with_glb=True)
else:
raise ValueError(f"Must use objbase_v5 currently, others have not been implemented yet.")
self.cond_real_basejtsrel_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
cond_real_basejtsrel_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.cond_real_basejtsrel_seqTransEncoderLayer = nn.TransformerEncoder(cond_real_basejtsrel_seqTransEncoderLayer,
num_layers=self.num_layers)
elif self.args.finetune_with_cond_jtsobj: # cond_obj_trans_layer
# InputProcessObjV6(self, data_rep, input_feats, latent_dim, layernorm=True)
# cond_obj_input_layer, cond_obj_trans_layer, cond_joints_offset_input_process, cond_sequence_pos_encoder, cond_jtsobj_seqTransEncoder #
# TODO: cond_obj_trans_layer : cond_joints_offset_input_process <- joints_offset_input_process; cond_sequence_pos_encoder <- sequence_pos_encoder; cond_seqTransEncoder <- seqTransEncoder
self.cond_obj_input_feats = 3
# if self.args.finetune_cond_obj_feats_dim == 3:
# self.cond_obj_input_feats = 3
self.cond_obj_input_feats = self.args.finetune_cond_obj_feats_dim # finetune cond with obj feats dim #
self.cond_obj_input_layer = InputProcessObjV6(self.data_rep, self.cond_obj_input_feats, self.latent_dim, layernorm=layernorm)
# TODO: remember to set this layer to zero! #
self.cond_obj_trans_layer = nn.Linear(self.latent_dim, self.latent_dim)
# hand_embedding + zero_trans_layer(obj_cond_embedding)
self.cond_joints_offset_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
self.cond_sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
cond_jtsobj_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.cond_seqTransEncoder = nn.TransformerEncoder(cond_jtsobj_seqTransEncoderLayer,
num_layers=self.num_layers)
else:
raise ValueError(f"Either ``finetune_with_cond_rel'' or ``finetune_with_cond_jtsobj'' should be activated!")
# TODO: remember to initialize it to zero! #
self.cond_trans_linear_layer = nn.Linear(self.latent_dim, self.latent_dim)
# self.avg_joints_sequence_input_process, self.avg_joint_sequence_output_process #
self.avg_joints_sequence_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
# TODO: should set those weights to frozen --> joints offset input process...
self.joints_offset_input_process = InputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim)
if self.args.not_cond_base:
self.rel_input_feats = 21 * ( 3)
# self.input_process = InputProcessObjBase(self.data_rep, self.rel_input_feats+self.gru_emb_dim, self.latent_dim)
self.sequence_pos_encoder = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
### Encoding layer ###
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
### Encoding layer ###
# logvar_seqTransEncoder_e, logvar_seqTransEncoder # logvar_seqTranEncoder
seqTransEncoderLayer_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.logvar_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer_logvar,
num_layers=self.num_layers)
### timesteps embedding layer ### # TimestepEmbedder -> embedding times #
self.embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder)
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process # # baseptsrel #
self.basejtsrel_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder)
self.sequence_pos_denoising_encoder = PositionalEncoding(self.latent_dim, self.dropout)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.basejtsrel_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
)
else:
seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.basejtsrel_denoising_seqTransEncoder = nn.TransformerEncoder(seqTransEncoderLayer,
num_layers=self.num_layers)
# # seq_len x bsz x dim
# if self.args.const_noise: # add to attention network # add j
# # 1) max pool latents over the sequence
# # 2) transform the pooled latnets via the linear layer
# self.basejtsrel_glb_denoising_latents_trans_layer = nn.Sequential(
# nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
# nn.Linear(self.latent_dim * 2, self.latent_dim)
# )
###### joints_feats_in_dim ###### # a linear transformation net with weights and bias set to zero #
self.avg_joint_sequence_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3) # output avgjts sequence
# OutputProcessCond
self.joint_offset_output_process = OutputProcess(self.data_rep, self.joints_feats_in_dim, self.latent_dim, 21, 3)
if self.args.use_dec_rel_v2:
self.output_process = OutputProcessObjBaseRawV2(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base, finetune_with_cond=self.args.finetune_with_cond)
else:
# OutputProcessObjBaseRaw ## output process for basejtsrel #
self.output_process = OutputProcessObjBaseRaw(self.data_rep, self.latent_dim, not_cond_base=self.args.not_cond_base)
##### ==== input process, communications, output process for rel, dists ==== #####
if self.diff_basejtse:
### input process obj base ###
# construct input_process_e #
# self.input_feats_e = 21 * (3 + 3 + 3 + 1 + 1)
# self.input_feats_e = 21 * (3 + 3 + 1 + 1)
self.input_feats_e = 21 * (3 + 3 + 1 + 1 + 3 + 3 + 1)
self.input_process_e = InputProcessObjBase(self.data_rep, self.input_feats_e+self.gru_emb_dim, self.latent_dim)
self.sequence_pos_encoder_e = PositionalEncoding(self.latent_dim, self.dropout)
self.emb_trans_dec = emb_trans_dec
# # single layer transformers # ## predict relative position for each base point? # existing model
# if self.arch == 'trans_enc':
print("TRANS_ENC init")
seqTransEncoderLayer_e = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.seqTransEncoder_e = nn.TransformerEncoder(seqTransEncoderLayer_e,
num_layers=self.num_layers)
print("TRANS_ENC init")
# logvar_seqTransEncoder_e,
seqTransEncoderLayer_e_logvar = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.logvar_seqTransEncoder_e = nn.TransformerEncoder(seqTransEncoderLayer_e_logvar,
num_layers=self.num_layers)
#
# elif self.arch == 'trans_dec':
# print("TRANS_DEC init")
# seqTransDecoderLayer_e = nn.TransformerDecoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=activation)
# self.seqTransDecoder_e = nn.TransformerDecoder(seqTransDecoderLayer_e,
# num_layers=self.num_layers)
# elif self.arch == 'gru': ## arch ##
# print("GRU init")
# self.gru_e = nn.GRU(self.latent_dim, self.latent_dim, num_layers=self.num_layers, batch_first=True)
# else:
# raise ValueError('Please choose correct architecture [trans_enc, trans_dec, gru]')
# tiemstep # # timestep embedding e # Embed timestep e #
self.embed_timestep_e = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder_e)
self.sequence_pos_denoising_encoder_e = PositionalEncoding(self.latent_dim, self.dropout)
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process #
self.basejtse_denoising_embed_timestep = TimestepEmbedder(self.latent_dim, self.sequence_pos_encoder_e)
if self.args.use_ours_transformer_enc: # our transformer encoder #
self.basejtse_denoising_seqTransEncoder = model_utils.TransformerEncoder(
hidden_size=self.latent_dim,
fc_size=self.ff_size,
num_heads=self.num_heads,
layer_norm=True,
num_layers=self.num_layers,
dropout_rate=0.2,
re_zero=True,
memory_efficient=False,
) ### basejtse_denoising_seqTransEncoder ###
else:
basejtse_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
nhead=self.num_heads,
dim_feedforward=self.ff_size,
dropout=self.dropout,
activation=self.activation)
self.basejtse_denoising_seqTransEncoder = nn.TransformerEncoder(basejtse_seqTransEncoderLayer,
num_layers=self.num_layers)
# basejtse_seqTransEncoderLayer = nn.TransformerEncoderLayer(d_model=self.latent_dim,
# nhead=self.num_heads,
# dim_feedforward=self.ff_size,
# dropout=self.dropout,
# activation=self.activation)
# self.basejtse_denoising_seqTransEncoder = nn.TransformerEncoder(basejtse_seqTransEncoderLayer,
# num_layers=self.num_layers)
# seq_len x bsz x dim
# if self.args.const_noise:
# # 1) max pool latents over the sequence
# # 2) transform the pooled latnets via the linear layer
# self.basejtse_denoising_seqTransEncoder = nn.Sequential(
# nn.Linear(self.latent_dim, self.latent_dim * 2), nn.ReLU(),
# nn.Linear(self.latent_dim * 2, self.latent_dim)
# )
# self.output_process_e = OutputProcessObjBaseV3(self.data_rep, self.latent_dim)
self.output_process_e = OutputProcessObjBaseERaw(self.data_rep, self.latent_dim)
# self.rot2xyz = Rotation2xyz(device='cpu', dataset=self.dataset)
def set_enc_to_eval(self):
# jts: joint_sequence_input_process, joint_sequence_pos_encoder, joint_sequence_seqTransEncoder, joint_sequence_logvar_seqTransEncoder
# basejtsrel: input_process, sequence_pos_encoder, seqTransEncoder, logvar_seqTransEncoder,
# basejtse: input_process_e, sequence_pos_encoder_e, seqTransEncoder_e, logvar_seqTransEncoder_e,
if self.diff_jts:
self.joint_sequence_input_process.eval()
self.joint_sequence_pos_encoder.eval()
self.joint_sequence_seqTransEncoder.eval()
self.joint_sequence_logvar_seqTransEncoder.eval()
if self.diff_basejtse:
self.input_process_e.eval()
self.sequence_pos_encoder_e.eval()
self.seqTransEncoder_e.eval()
self.logvar_seqTransEncoder_e.eval()
if self.diff_basejtsrel:
self.input_process.eval()
self.sequence_pos_encoder.eval()
self.seqTransEncoder.eval() # seqTransEncoder, logvar_seqTransEncoder
self.logvar_seqTransEncoder.eval()
def set_bn_to_eval(self):
if self.args.use_objbase_v6: # real_basejtsrel_input_process
try:
self.real_basejtsrel_input_process.pnpp_conv_net.set_bn_no_training()
except:
pass
def parameters_wo_clip(self):
return [p for name, p in self.named_parameters() if not name.startswith('clip_model.')]
def load_and_freeze_clip(self, clip_version):
clip_model, clip_preprocess = clip.load(clip_version, device='cpu',
jit=False) # Must set jit=False for training
clip.model.convert_weights( # encode
clip_model) # Actually this line is unnecessary since clip by default already on float16
# Freeze CLIP weights
clip_model.eval()
for p in clip_model.parameters():
p.requires_grad = False
return clip_model
def mask_cond(self, cond, force_mask=False):
bs, d = cond.shape
if force_mask:
return torch.zeros_like(cond)
elif self.training and self.cond_mask_prob > 0.:
mask = torch.bernoulli(torch.ones(bs, device=cond.device) * self.cond_mask_prob).view(bs, 1) # 1-> use null_cond, 0-> use real cond
return cond * (1. - mask)
else:
return cond
def encode_text(self, raw_text):
# raw_text - list (batch_size length) of strings with input text prompts #
device = next(self.parameters()).device
max_text_len = 20 if self.dataset in ['humanml', 'kit', 'motion_ours'] else None # Specific hardcoding for humanml dataset
if max_text_len is not None:
default_context_length = 77
context_length = max_text_len + 2 # start_token + 20 + end_token
assert context_length < default_context_length
texts = clip.tokenize(raw_text, context_length=context_length, truncate=True).to(device) # [bs, context_length] # if n_tokens > context_length -> will truncate
# print('texts', texts.shape)
zero_pad = torch.zeros([texts.shape[0], default_context_length-context_length], dtype=texts.dtype, device=texts.device)
texts = torch.cat([texts, zero_pad], dim=1)
# print('texts after pad', texts.shape, texts)
else: ##
texts = clip.tokenize(raw_text, truncate=True).to(device) # [bs, context_length] # if n_tokens > 77 -> will truncate
return self.clip_model.encode_text(texts).float()
def dec_jts_only_fr_latents(self, latents_feats):
joint_seq_output = self.joint_sequence_output_process(latents_feats) # [bs, njoints, nfeats, nframes]
# bsz x ws x nnj x nnfeats # --> joints_seq_outputs #
joint_seq_output = joint_seq_output.permute(0, 3, 1, 2).contiguous()
## joints seq outputs ##
diff_jts_dict = {
"joint_seq_output": joint_seq_output,
"joints_seq_latents": latents_feats,
}
return diff_jts_dict
def dec_basejtsrel_only_fr_latents(self, latent_feats, x):
# basejtsrel_seq_latents_pred_feats
avg_jts_seq_latents = latent_feats[0:1, ...]
other_basejtsrel_seq_latents = latent_feats[1:, ...]
avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
basejtsrel_dec_out = {
'avg_jts_outputs': avg_jts_outputs,
'basejtsrel_output': basejtsrel_output['dec_rel'],
}
return basejtsrel_dec_out
# def dec_latents_to_joints_with_t(self, joints_seq_latents, timesteps):
def dec_latents_to_joints_with_t(self, input_latent_feats, x, timesteps):
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
# joints_seq_latents: seq x bs x d --> perturbed joitns_seq_latents \in [-1, 1] ##
# def dec_latents_to_joints_with_t(self, joints_seq_latents, timesteps):
## positional encoding for denoising ##
# rt_dict = {
# 'joint_seq_output': joint_seq_output,
# 'rel_base_pts_outputs': rel_base_pts_outputs,
# }
rt_dict = {}
if self.diff_jts:
####### input latent feats #######
joints_seq_latents = input_latent_feats["joints_seq_latents"]
if not self.args.without_dec_pos_emb:
joints_seq_latents = self.joint_sequence_denoising_pos_encoder(joints_seq_latents)
# ### GET joints seq time embeddings ### ### embed time stamps ###
# joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
# joints_seq_latents = torch.cat(
# [joints_seq_time_emb, joints_seq_latents], dim=0
# )
# joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)[1:] # seq x bs x d
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
joints_seq_time_emb = joints_seq_time_emb.repeat(joints_seq_latents.size(0), 1, 1).contiguous()
joints_seq_latents = joints_seq_latents + joints_seq_time_emb
if self.args.use_ours_transformer_enc:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
joints_seq_latents = joints_seq_latents.permute(1, 0, 2)
else:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
joints_seq_time_emb = self.joint_sequence_embed_timestep(timesteps)
joints_seq_latents = torch.cat(
[joints_seq_time_emb, joints_seq_latents], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
joints_seq_latents = joints_seq_latents.permute(1, 0, 2)[1:]
else:
joints_seq_latents = self.joint_sequence_denoising_seqTransEncoder(joints_seq_latents)[1:]
# joints_seq_latents: seq_len x bsz x latent_dim #
if self.args.const_noise:
seq_len = joints_seq_latents.size(0)
# if self.args.const_noise:
joints_seq_latents, _ = torch.max(joints_seq_latents, dim=0, keepdim=True)
joints_seq_latents = self.glb_denoising_latents_trans_layer(joints_seq_latents) # seq_len x bsz x latent_dim
joints_seq_latents = joints_seq_latents.repeat(seq_len, 1, 1).contiguous()
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
joints_seq_latents = input_latent_feats["joints_seq_latents_enc"]
# bsz x ws x nnj x 3 #
joint_seq_output = self.joint_sequence_output_process(joints_seq_latents) # [bs, njoints, nfeats, nframes]
# bsz x ws x nnj x nnfeats # --> joints_seq_outputs #
joint_seq_output = joint_seq_output.permute(0, 3, 1, 2).contiguous()
diff_jts_dict = {
"joint_seq_output": joint_seq_output,
"joints_seq_latents": joints_seq_latents,
}
else:
diff_jts_dict = {}
if self.diff_basejtsrel:
rel_base_pts_outputs = input_latent_feats["rel_base_pts_outputs"]
if rel_base_pts_outputs.size(0) == 1 and self.args.single_frame_noise:
rel_base_pts_outputs = rel_base_pts_outputs.repeat(self.args.window_size + 1, 1, 1)
if not self.args.without_dec_pos_emb: # without
avg_jts_inputs = rel_base_pts_outputs[0:1, ...]
other_rel_base_pts_outputs = rel_base_pts_outputs[1: , ...]
other_rel_base_pts_outputs = self.sequence_pos_denoising_encoder(other_rel_base_pts_outputs)
rel_base_pts_outputs = torch.cat(
[avg_jts_inputs, other_rel_base_pts_outputs], dim=0
)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_time_emb = basejtsrel_time_emb.repeat(rel_base_pts_outputs.size(0), 1, 1).contiguous()
basejtsrel_seq_latents = rel_base_pts_outputs + basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_seq_latents = torch.cat(
[basejtsrel_time_emb, rel_base_pts_outputs], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
basejtsrel_seq_latents = input_latent_feats["rel_base_pts_outputs_enc"]
if basejtsrel_seq_latents.size(0) == 1 and self.args.single_frame_noise:
basejtsrel_seq_latents = basejtsrel_seq_latents.repeat(self.args.window_size + 1, 1, 1)
basejtsrel_seq_latents_pred_feats = basejtsrel_seq_latents
elif self.args.pred_diff_noise:
basejtsrel_seq_latents_pred_feats = input_latent_feats["rel_base_pts_outputs"] - basejtsrel_seq_latents
else:
basejtsrel_seq_latents_pred_feats = basejtsrel_seq_latents
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
### GET joints seq output ###
# basejtsrel_denoising_embed_timestep, basejtsrel_denoising_seqTransEncoder, output_process #
# basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
# basejtsrel_seq_latents = torch.cat(
# [basejtsrel_time_emb, rel_base_pts_outputs], dim=0
# )
# basejtsrel_seq_latents_pred_feats
avg_jts_seq_latents = basejtsrel_seq_latents_pred_feats[0:1, ...]
other_basejtsrel_seq_latents = basejtsrel_seq_latents_pred_feats[1:, ...]
avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
####
# avg_jts_seq_latents = basejtsrel_seq_latents[0:1, ...]
# other_basejtsrel_seq_latents = basejtsrel_seq_latents[1:, ...]
# avg_jts_outputs = self.avg_joint_sequence_output_process(avg_jts_seq_latents) # bsz x njoints x nfeats x 1
# avg_jts_outputs = avg_jts_outputs.squeeze(-1) # bsz x nnjoints x 1 --> avg joints here #
# # basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_output = self.output_process(other_basejtsrel_seq_latents, x)
diff_basejtsrel_dict = {
"basejtsrel_output": basejtsrel_output['dec_rel'],
"basejtsrel_seq_latents": basejtsrel_seq_latents,
"avg_jts_outputs": avg_jts_outputs,
}
else:
diff_basejtsrel_dict = {}
if self.diff_basejtse:
# e_disp_rel_to_base_along_normals = input_latent_feats['e_disp_rel_to_base_along_normals']
# e_disp_rel_to_baes_vt_normals = input_latent_feats['e_disp_rel_to_baes_vt_normals']
base_jts_e_feats = input_latent_feats['base_jts_e_feats'] # seq x bs x d --> e feats
if not self.args.without_dec_pos_emb:
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_time_emb = base_jts_e_time_emb.repeat(base_jts_e_feats.size(0), 1, 1).contiguous()
base_jts_e_feats = base_jts_e_feats + base_jts_e_time_emb
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)
else:
#### Decode e_along_normals and e_vt_normals ####
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_feats = torch.cat(
[base_jts_e_time_emb, base_jts_e_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)[1:]
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
### sequence latents ###
if self.args.train_enc: # trian enc for seq latents ###
base_jts_e_feats = input_latent_feats["base_jts_e_feats_enc"]
# base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
#### Decode e_along_normals and e_vt_normals ####
### bae jts e embeddings feats ###
# base_jts_e_time_emb = self.embed_timestep_e(timesteps)
# base_jts_e_feats = torch.cat(
# [base_jts_e_time_emb, base_jts_e_feats], dim=0
# )
# base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
base_jts_e_output = self.output_process_e(base_jts_e_feats, x)
# dec_e_along_normals: bsz x (ws - 1) x nnb x nnj #
dec_e_along_normals = base_jts_e_output['dec_e_along_normals']
dec_e_vt_normals = base_jts_e_output['dec_e_vt_normals']
dec_d = basejtsrel_output['dec_d']
rel_vel_dec = basejtsrel_output['rel_vel_dec']
dec_e_along_normals = dec_e_along_normals.contiguous().permute(0, 1, 3, 2).contiguous()
dec_e_vt_normals = dec_e_vt_normals.contiguous().permute(0, 1, 3, 2).contiguous()
dec_d = dec_d.contiguous().permute(0, 1, 3, 2).contiguous()
rel_vel_dec = rel_vel_dec.contiguous().permute(0, 1, 3, 2).contiguous()
#### Decode e_along_normals and e_vt_normals ####
diff_basejtse_dict = {
'dec_e_along_normals': dec_e_along_normals,
'dec_e_vt_normals': dec_e_vt_normals,
'base_jts_e_feats': base_jts_e_feats,
'dec_d': dec_d,
'rel_vel_dec': rel_vel_dec,
}
else:
diff_basejtse_dict = {}
rt_dict = {}
rt_dict.update(diff_jts_dict)
rt_dict.update(diff_basejtsrel_dict)
rt_dict.update(diff_basejtse_dict)
### rt_dict --> rt_dict of joints, rel ###
return rt_dict
# return joint_seq_output, joints_seq_latents
def reparameterization(self, val_mean, val_var):
val_noise = torch.randn_like(val_mean)
val_sampled = val_mean + val_noise * val_var ### sample the value
if self.args.rnd_noise:
val_sampled = val_noise
return val_sampled
def decode_realbasejtsrel_from_objbasefeats(self, objbasefeats, input_data):
real_dec_basejtsrel = self.real_basejtsrel_output_process(
objbasefeats, input_data
)
# real_dec_basejtsrel -> decoded realtive positions #
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous()
return real_dec_basejtsrel
def denoising_realbasejtsrel_objbasefeats(self, pert_obj_base_pts_feats, timesteps):
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(pert_obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = pert_obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, pert_obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# bsz, nframes, nnb, nnj, 3 -->
# real_dec_basejtsrel = self.real_basejtsrel_output_process(
# real_basejtsrel_seq_latents, x
# )
return real_basejtsrel_seq_latents
def get_cond_parameters(self):
# [finetune_with_cond_rel, finetune_with_cond_jtsobj]
# cond_real_basejtsrel_input_process, cond_real_basejtsrel_pos_encoder, cond_real_basejtsrel_seqTransEncoderLayer, cond_trans_linear_layer #
# finetune_with_cond_jtsobj : cond_joints_offset_input_process <- joints_offset_input_process; cond_sequence_pos_encoder <- sequence_pos_encoder; cond_seqTransEncoder <- seqTransEncoder
# cond_obj_input_layer, cond_obj_trans_layer, cond_joints_offset_input_process, cond_sequence_pos_encoder, cond_seqTransEncoder #
if self.args.diff_basejtsrel:
# TODO: finetune_with_cond_jtsobj : cond_joints_offset_input_process <- joints_offset_input_process; cond_sequence_pos_encoder <- sequence_pos_encoder; cond_seqTransEncoder <- seqTransEncoder
if self.args.finetune_with_cond_rel:
params = list(self.cond_real_basejtsrel_input_process.parameters()) + list(self.cond_real_basejtsrel_pos_encoder.parameters()) + list(self.cond_real_basejtsrel_seqTransEncoderLayer.parameters()) + list(self.cond_trans_linear_layer.parameters())
elif self.args.finetune_with_cond_jtsobj:
params = list(self.cond_joints_offset_input_process.parameters()) + list(self.cond_sequence_pos_encoder.parameters()) + list(self.cond_seqTransEncoder.parameters()) + list(self.cond_obj_trans_layer.parameters()) + list(self.cond_obj_input_layer.parameters())
else:
raise ValueError(f"Either ``finetune_with_cond_rel'' or ``finetune_with_cond_jtsobj'' should be activated !")
else:
raise ValueError(f"Must use diff_basejtsrel currently, others have not been implemented yet.")
return params
def set_trans_linear_layer_to_zero(self):
torch.nn.init.zeros_(self.cond_trans_linear_layer.weight)
torch.nn.init.zeros_(self.cond_trans_linear_layer.bias)
# finetune_with_cond_jtsobj: # cond_obj_trans_layer
if self.args.finetune_with_cond_jtsobj:
torch.nn.init.zeros_(self.cond_obj_trans_layer.weight)
torch.nn.init.zeros_(self.cond_obj_trans_layer.bias)
def forward(self, x, timesteps):
"""
x: [batch_size, njoints, nfeats, max_frames], denoted x_t in the paper
timesteps: [batch_size] (int)
"""
# joint_sequence_input_process; joint_sequence_pos_encoder; joint_sequence_seqTransEncoder; joint_sequence_seqTransDecoder; joint_sequence_embed_timestep; joint_sequence_output_process #
# bsz, nframes, nnj = x['pert_rhand_joints'].shape[:3]
# pert_rhand_joints = x['pert_rhand_joints'] # bsz x ws x nnj x 3 # bsz x nnj x 3 x ws
bsz, nframes, nnj = x['rhand_joints'].shape[:3]
pert_rhand_joints = x['rhand_joints'] # bsz x ws x nnj x 3 # bsz x nnj x 3 x ws
base_pts = x['base_pts'] ### bsz x nnb x 3 ###
base_normals = x['base_normals'] ### bsz x nnb x 3 ### --> base normals ###
# base_normals # ##
rt_dict = {}
## # # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
if self.diff_basejtse:
### Embed physicss quantities ###
# e_disp_rel_to_base_along_normals: bsz x (ws - 1) x nnj x nnb #
# e_disp_rel_to_baes_vt_normals: bsz x (ws - 1) x nnj x nnb #
# e_disp_rel_to_base_along_normals = x['e_disp_rel_to_base_along_normals']
# e_disp_rel_to_baes_vt_normals = x['e_disp_rel_to_baes_vt_normals']
e_disp_rel_to_base_along_normals = x['pert_e_disp_rel_to_base_along_normals']
e_disp_rel_to_baes_vt_normals = x['pert_e_disp_rel_to_base_vt_normals']
obj_pts_disp = x['obj_pts_disp']
vel_obj_pts_to_hand_pts = x['vel_obj_pts_to_hand_pts']
disp_dist = x['disp_dist']
nnb = base_pts.size(1)
disp_ws = e_disp_rel_to_base_along_normals.size(1) ### --> base normals ###
base_pts_disp_exp = base_pts.unsqueeze(1).unsqueeze(1).repeat(1, disp_ws, nnj, 1, 1).contiguous()
base_normals_disp_exp = base_normals.unsqueeze(1).unsqueeze(1).repeat(1, disp_ws, nnj, 1, 1).contiguous()
obj_pts_disp_exp = obj_pts_disp.unsqueeze(1).unsqueeze(1).repeat(1, disp_ws, nnj, 1, 1).contiguous()
# bsz x (ws - 1) x nnj x nnb x (3 + 3 + 1 + 1)
base_pts_normals_e_in_feats = torch.cat( # along normals; # vt normals #
[base_pts_disp_exp, base_normals_disp_exp, obj_pts_disp_exp, vel_obj_pts_to_hand_pts, disp_dist, e_disp_rel_to_base_along_normals.unsqueeze(-1), e_disp_rel_to_baes_vt_normals.unsqueeze(-1)], dim=-1
)
base_pts_normals_e_in_feats = base_pts_normals_e_in_feats.permute(0, 1, 3, 2, 4).contiguous()
# bsz x (ws - 1) x nnb x (nnj x (xxx feats_dim))
base_pts_normals_e_in_feats = base_pts_normals_e_in_feats.view(bsz, disp_ws, nnb, -1).contiguous()
## input process ##
base_jts_e_feats = self.input_process_e(base_pts_normals_e_in_feats)
base_jts_e_feats = self.sequence_pos_encoder_e(base_jts_e_feats)
## seq transformation for e ## #
base_jts_e_feats_mean = self.seqTransEncoder_e(base_jts_e_feats) ## mean, mdm_ours ##
# print(f"base_jts_e_feats: {base_jts_e_feats.size()}")
### Embed physicss quantities ###
#### base_jts_e_feats, base_jts_e_feats_mean ####
# ## us basejtsefeats for denoising directly ##
base_jts_e_feats = base_jts_e_feats_mean
if not self.args.without_dec_pos_emb: ## use positional encoding ##
# rel_base_pts_outputs = self.sequence_pos_denoising_encoder(rel_base_pts_outputs)
base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_time_emb = base_jts_e_time_emb.repeat(base_jts_e_feats.size(0), 1, 1).contiguous()
base_jts_e_feats = base_jts_e_feats + base_jts_e_time_emb
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)
else:
#### Decode e_along_normals and e_vt_normals ####
base_jts_e_time_emb = self.embed_timestep_e(timesteps)
base_jts_e_feats = torch.cat(
[base_jts_e_time_emb, base_jts_e_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## transformer encoder ##
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
base_jts_e_feats = base_jts_e_feats.permute(1, 0, 2)[1:]
else:
base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
# ### sequence latents ###
# if self.args.train_enc: # trian enc for seq latents ###
# base_jts_e_feats = input_latent_feats["base_jts_e_feats_enc"]
# base_jts_e_feats = self.sequence_pos_denoising_encoder_e(base_jts_e_feats)
#### Decode e_along_normals and e_vt_normals ####
### bae jts e embeddings feats ###
# base_jts_e_time_emb = self.embed_timestep_e(timesteps)
# base_jts_e_feats = torch.cat(
# [base_jts_e_time_emb, base_jts_e_feats], dim=0
# )
# base_jts_e_feats = self.basejtse_denoising_seqTransEncoder(base_jts_e_feats)[1:]
##### output_process_e -> output energies #####
base_jts_e_output = self.output_process_e(base_jts_e_feats, x)
# dec_e_along_normals: bsz x (ws - 1) x nnb x nnj #
dec_e_along_normals = base_jts_e_output['dec_e_along_normals']
dec_e_vt_normals = base_jts_e_output['dec_e_vt_normals']
dec_d = base_jts_e_output['dec_d']
rel_vel_dec = base_jts_e_output['rel_vel_dec']
dec_e_along_normals = dec_e_along_normals.contiguous().permute(0, 1, 3, 2).contiguous() # bsz x (ws - 1) x nnj x nnb
dec_e_vt_normals = dec_e_vt_normals.contiguous().permute(0, 1, 3, 2).contiguous() # bsz x (ws - 1) x nnj x nnb
#### Decode e_along_normals and e_vt_normals ####
diff_basejtse_dict = {
'dec_e_along_normals': dec_e_along_normals,
'dec_e_vt_normals': dec_e_vt_normals,
'base_jts_e_feats': base_jts_e_feats,
'dec_d': dec_d,
'rel_vel_dec': rel_vel_dec,
}
# rt_dict['base_jts_e_feats'] = base_jts_e_feats
# rt_dict['base_jts_e_feats_mean'] = base_jts_e_feats_mean
# rt_dict['base_jts_e_feats_logvar'] = base_jts_e_feats_logvar # log_var #
else:
diff_basejtse_dict = {}
if self.diff_jts:
# base_pts_normal
### InputProcess ###
pert_rhand_joints_trans = pert_rhand_joints.permute(0, 2, 3, 1).contiguous() # bsz x nnj x 3 x ws #
rhand_joints_feats = self.joint_sequence_input_process(pert_rhand_joints_trans) # [seqlen, bs, d]
### InputProcessObjBase ###
# rhand_joints_feats = self.joint_sequence_input_process(pert_rhand_joints)
### === Encode input joint sequences === ###
# bs, njoints, nfeats, nframes = x.shape
# rhand_joints_emb = self.joint_sequence_embed_timestep(timesteps) # [1, bs, d]
# if self.arch == 'trans_enc':
xseq = rhand_joints_feats # [seqlen+1, bs, d]
xseq = self.joint_sequence_pos_encoder(xseq) # [seqlen+1, bs, d]
joint_seq_output_mean = self.joint_sequence_seqTransEncoder(xseq) # [1:] # , src_key_padding_mask=~maskseq) # [seqlen, bs, d]
### calculate logvar, mean, and feats ###
joint_seq_output_logvar = self.joint_sequence_logvar_seqTransEncoder(xseq)
# # logvar_seqTransEncoder_e, logvar_seqTransEncoder, joint_sequence_logvar_seqTransEncoder # #
joint_seq_output_var = torch.exp(joint_seq_output_logvar) # seq x bs x d --> encodeing and decoding
## base_jts_e_feats: seqlen x bs x d --> val latents ##
joint_seq_output = self.reparameterization(joint_seq_output_mean, joint_seq_output_var)
rt_dict['joint_seq_output'] = joint_seq_output
# rt_dict['joint_seq_output'] = joint_seq_output_mean
rt_dict['joint_seq_output_mean'] = joint_seq_output_mean
rt_dict['joint_seq_output_logvar'] = joint_seq_output_logvar
if self.args.diff_realbasejtsrel_to_joints: # nframes x nnbase x nnjts x (base pts + base normals + 3) 2) point feature for each point; point feature for; condition on the noisy input for the denoised information
# real_basejtsrel_to_joints_input_process, real_basejtsrel_to_joints_sequence_pos_encoder, real_basejtsrel_to_joints_seqTransEncoder
# real_basejtsrel_to_joints_embed_timestep, real_basejtsrel_to_joints_sequence_pos_denoising_encoder, real_basejtsrel_to_joints_denoising_seqTransEncoder, real_basejtsrel_to_joints_output_process
bsz, nf, nnj, nnb = x['pert_rel_base_pts_to_joints_for_jts_pred'].size()[:4]
normed_base_pts = x['normed_base_pts']
base_normals = x['base_normals']
pert_rel_base_pts_to_rhand_joints = x['pert_rel_base_pts_to_joints_for_jts_pred'] # bsz x nf x nnj x nnb x 3
## use_abs_jts_pos --> obj jts pos for the encodingj
if self.args.use_abs_jts_pos: ## bsz x nf x nnj x nnb x 3 ## ---> abs jts pos ##
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1)
# use_abs_jts_for_encoding, real_basejtsrel_to_joints_input_process
if self.args.use_abs_jts_for_encoding:
if not self.args.use_abs_jts_pos:
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1) # pert_rel_base_pts_to_rhand_joints: bsz x nf x nnj x nnb x 3
abs_jts = pert_rel_base_pts_to_rhand_joints[..., 0, :]
abs_jts = abs_jts.permute(0, 2, 3, 1).contiguous()
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(abs_jts)
elif self.args.use_abs_jts_for_encoding_obj_base:
if not self.args.use_abs_jts_pos:
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints + normed_base_pts.unsqueeze(1).unsqueeze(1) # pert_rel_base_pts_to_rhand_joints: bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints = pert_rel_base_pts_to_rhand_joints[:, :, :, 0:1, :]
# obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
# [normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1)[:, :, :, 0:1, :], base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1)[:, :, :, 0:1, :], pert_rel_base_pts_to_rhand_joints], dim=-1
# )
obj_base_in_feats = pert_rel_base_pts_to_rhand_joints
# --> tnrasform the input feature dim to 21 * 3 here for encoding #
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, 1, -1).contiguous() #
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(obj_base_in_feats) # nf x bsz x feat_dim #
else:
if self.args.use_objbase_v2:
# bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
[normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
)
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
obj_base_encoded_feats = self.real_basejtsrel_to_joints_input_process(obj_base_in_feats) # nf x bsz x feat_dim #
# ### real_basejtsrel_to_joints_input_process --> real_basejtsrel_to_joints_input_process --> for the joints and input process ### # obj_base_encoded_feats
# obj_base_encoded_feats
obj_base_pts_feats_pos_embedding = self.real_basejtsrel_to_joints_sequence_pos_encoder(obj_base_encoded_feats)
obj_base_pts_feats = self.real_basejtsrel_to_joints_seqTransEncoder(obj_base_pts_feats_pos_embedding)
if self.args.use_sigmoid:
obj_base_pts_feats = (torch.sigmoid(obj_base_pts_feats) - 0.5) * 2.
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_to_joints_embed_timestep(timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq des
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_to_joints_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_to_joints_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
joints_offset_output = self.real_basejtsrel_to_joints_output_process(real_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2)
if self.args.diff_basejtsrel:
diff_basejtsrel_to_joints_dict = {
'joints_offset_output_from_rel': joints_offset_output
}
else:
diff_basejtsrel_to_joints_dict = {
'joints_offset_output': joints_offset_output
}
else:
diff_basejtsrel_to_joints_dict = {}
if self.diff_realbasejtsrel: # real_dec_basejtsrel
# real_basejtsrel_input_process, real_basejtsrel_sequence_pos_encoder, real_basejtsrel_seqTransEncoder, real_basejtsrel_embed_timestep, real_basejtsrel_sequence_pos_denoising_encoder, real_basejtsrel_denoising_seqTransEncoder
bsz, nf, nnj, nnb = x['pert_rel_base_pts_to_rhand_joints'].size()[:4]
normed_base_pts = x['normed_base_pts']
base_normals = x['base_normals']
pert_rel_base_pts_to_rhand_joints = x['pert_rel_base_pts_to_rhand_joints'] # bsz x nf x nnj x nnb x 3
if self.args.use_objbase_v2:
# bsz x nf x nnj x nnb x 3
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.use_objbase_v4:
# use_objbase_v4: # use_objbase_out_v4
exp_normed_base_pts = normed_base_pts.unsqueeze(1).unsqueeze(2).repeat(1, nf, nnj, 1, 1).contiguous()
exp_base_normals = base_normals.unsqueeze(1).unsqueeze(2).repeat(1, nf, nnj, 1, 1).contiguous()
obj_base_in_feats = torch.cat(
[pert_rel_base_pts_to_rhand_joints, exp_normed_base_pts, exp_base_normals], dim=-1 # bsz x nf x nnj x nnb x (3 + 3 + 3) # -> exp_base_normals
)
obj_base_in_feats = obj_base_in_feats.view(bsz, nf, nnj, -1).contiguous()
elif self.args.use_objbase_v5: # use_objbase_v5, use_objbase_out_v5
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
if self.args.v5_in_not_base:
obj_base_in_feats = torch.cat(
[ pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.v5_in_not_base_pos:
obj_base_in_feats = torch.cat(
[base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.use_objbase_v6 or self.args.use_objbase_v7:
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
[normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
)
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
# obj_base_in_feats = torch.cat( # bsz x nf x nnj x nnb x (3 + 3 + 3)
# [normed_base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nf, nnj, 1, 1), pert_rel_base_pts_to_rhand_joints], dim=-1
# )
# print(f"obj_base_in_feats: {obj_base_in_feats.size()}, bsz: {bsz}, nf: {nf}, nnj: {nnj}, nnb: {nnb}")
# obj_base_in_feats = obj_base_in_feats.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous() #
if self.args.use_objbase_v6:
normed_base_pts_exp = normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1) # and repeat for the base pts #
obj_base_encoded_feats = self.real_basejtsrel_input_process(obj_base_in_feats, normed_base_pts_exp)
else:
obj_base_encoded_feats = self.real_basejtsrel_input_process(obj_base_in_feats) # nf x bsz x feat_dim # nf x bsz x nnbasepts x feats_dim #
# obj_base_encoded_feats
obj_base_pts_feats_pos_embedding = self.real_basejtsrel_sequence_pos_encoder(obj_base_encoded_feats)
obj_base_pts_feats = self.real_basejtsrel_seqTransEncoder(obj_base_pts_feats_pos_embedding)
if self.args.use_sigmoid:
obj_base_pts_feats = (torch.sigmoid(obj_base_pts_feats) - 0.5) * 2.
if self.args.train_enc:
# basejtsrel_seq
# bsz, nframes, nnb, nnj, 3 -->
#
real_dec_basejtsrel = self.real_basejtsrel_output_process(
obj_base_pts_feats, x
)
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
if self.args.use_objbase_out_v3:
real_dec_basejtsrel = real_dec_basejtsrel
else:
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous() # bsz x nf x nnj x nnb x 3
else:
if self.args.deep_fuse_timeemb:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
# print(f"timesteps: {timesteps.size()}, obj_base_pts_feats: {obj_base_pts_feats.size()}")
if self.args.use_objbase_v5:
cur_timesteps = timesteps.unsqueeze(1).repeat(1, nnb).view(-1)
else:
cur_timesteps = timesteps
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(cur_timesteps)
real_basejtsrel_time_emb = real_basejtsrel_time_emb.repeat(obj_base_pts_feats.size(0), 1, 1).contiguous()
real_basejtsrel_seq_latents = obj_base_pts_feats + real_basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)
else: # seq des
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
real_basejtsrel_time_emb = self.real_basejtsrel_embed_timestep(timesteps)
real_basejtsrel_seq_latents = torch.cat(
[real_basejtsrel_time_emb, obj_base_pts_feats], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(real_basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
real_basejtsrel_seq_latents = real_basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
real_basejtsrel_seq_latents = self.real_basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
# basejtsrel_seq
# bsz, nframes, nnb, nnj, 3 -->
#
if self.args.use_jts_pert_realbasejtsrel:
joints_offset_output = self.real_basejtsrel_output_process(real_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2) # bsz x nf x nnj x 3
real_dec_basejtsrel = joints_offset_output.unsqueeze(-2).repeat(1, 1, 1, nnb, 1)
# real_dec_basejtsrel = joints_offset_output
else:
real_dec_basejtsrel = self.real_basejtsrel_output_process(
real_basejtsrel_seq_latents, x
)
# real_dec_basejtsrel -> decoded realtive positions #
real_dec_basejtsrel = real_dec_basejtsrel['dec_rel']
if self.args.use_objbase_out_v3 or self.args.use_objbase_out_v4 or self.args.use_objbase_out_v5:
real_dec_basejtsrel = real_dec_basejtsrel
else:
real_dec_basejtsrel = real_dec_basejtsrel.permute(0, 1, 3, 2, 4).contiguous() # bsz x nf x nnj x nnb x 3
diff_realbasejtsrel_out_dict = {
'real_dec_basejtsrel': real_dec_basejtsrel,
'obj_base_pts_feats': obj_base_pts_feats,
}
else:
diff_realbasejtsrel_out_dict = {}
if self.diff_basejtsrel:
bsz, nf, nnj, nnb = x['pert_rel_base_pts_to_rhand_joints'].size()[:4]
# cond_real_basejtsrel_input_process, cond_real_basejtsrel_pos_encoder, cond_real_basejtsrel_seqTransEncoderLayer, cond_trans_linear_layer #
normed_base_pts = x['normed_base_pts']
base_normals = x['base_normals']
pert_rel_base_pts_to_rhand_joints = x['pert_rel_base_pts_to_rhand_joints'] # bsz x nf x nnj x nnb x 3
# conditional strategies and representations for denoising #
# conditional strategies
# [finetune_with_cond_rel, finetune_with_cond_jtsobj] #
# cond_real_basejtsrel_input_process, cond_real_basejtsrel_pos_encoder, cond_real_basejtsrel_seqTransEncoderLayer, cond_trans_linear_layer #
# finetune_with_cond_jtsobj : cond_joints_offset_input_process <- joints_offset_input_process; cond_sequence_pos_encoder <- sequence_pos_encoder; cond_seqTransEncoder <- seqTransEncoder
if self.args.finetune_with_cond_rel: # finetune with cond rel # finetune with cond # finetune with
if self.args.use_objbase_v5: # use_objbase_v5, use_objbase_out_v5
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
if self.args.v5_in_not_base:
obj_base_in_feats = torch.cat(
[ pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
elif self.args.v5_in_not_base_pos:
obj_base_in_feats = torch.cat(
[base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
obj_base_in_feats = torch.cat(
[normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1), base_normals.unsqueeze(1).repeat(1, nf, 1, 1), pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else:
raise ValueError(f"Must use objbase_v5 currently, others have not been implemented yet.")
# print(f'obj_base_in_feats: {obj_base_in_feats.size()}')
obj_base_encoded_feats = self.cond_real_basejtsrel_input_process(obj_base_in_feats)
obj_base_pts_feats_pos_embedding = self.cond_real_basejtsrel_pos_encoder(obj_base_encoded_feats)
# print(f"obj_base_pts_feats_pos_embedding: {obj_base_pts_feats_pos_embedding.size()}")
obj_base_pts_feats = self.cond_real_basejtsrel_seqTransEncoderLayer(obj_base_pts_feats_pos_embedding) # seq x bsz x latent_dim
elif self.args.finetune_with_cond_jtsobj:
# cond_obj_input_layer, cond_obj_trans_layer, cond_joints_offset_input_process, cond_sequence_pos_encoder, cond_seqTransEncoder #
cond_joints_offset_sequence = x['pert_joints_offset_sequence'] # bsz x nf x nnj x 3
cond_joints_offset_sequence = cond_joints_offset_sequence.permute(0, 2, 3, 1).contiguous()
cond_joints_offset_feats = self.cond_joints_offset_input_process(cond_joints_offset_sequence) # nf x bsz x dim
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
#
if self.cond_obj_input_feats == 3:
normed_base_pts_exp = normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1).contiguous()
elif self.cond_obj_input_feats == 6:
normed_base_pts_exp = normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1).contiguous()
normed_base_normals_exp = base_normals.unsqueeze(1).repeat(1, nf, 1, 1).contiguous()
normed_base_pts_exp = torch.cat(
[normed_base_pts_exp, normed_base_normals_exp], dim=-1 ## bsz x nf x nn_base_pts x (3 + 3)
)
elif self.cond_obj_input_feats == (6 + 21 * 3): # 63 + 6 -> 69
normed_base_pts_exp = normed_base_pts.unsqueeze(1).repeat(1, nf, 1, 1).contiguous()
normed_base_normals_exp = base_normals.unsqueeze(1).repeat(1, nf, 1, 1).contiguous()
pert_rel_base_pts_to_rhand_joints_exp = pert_rel_base_pts_to_rhand_joints.transpose(-2, -3).contiguous().view(bsz, nf, nnb, -1).contiguous()
normed_base_pts_exp = torch.cat(
[normed_base_pts_exp, normed_base_normals_exp, pert_rel_base_pts_to_rhand_joints_exp], dim=-1
)
else: # cond_obj_input_feats --> cond_obj_input_feats # finetune_cond_obj_feats_dim
raise ValueError(f"Unrecognized cond_obj_input_feats: {self.cond_obj_input_feats}")
cond_obj_feats = self.cond_obj_input_layer(normed_base_pts_exp) # nf x bsz x feats_dim #
cond_obj_feats = self.cond_obj_trans_layer(cond_obj_feats)
cond_jtsobj_feats = cond_joints_offset_feats + cond_obj_feats
cond_jtsobj_feats = self.cond_sequence_pos_encoder(cond_jtsobj_feats)
obj_base_pts_feats = self.cond_seqTransEncoder(cond_jtsobj_feats)
else:
raise ValueError(f"Either ``finetune_with_cond_rel'' or ``finetune_with_cond_jtsobj'' should be activated! ")
obj_base_pts_feats = self.cond_trans_linear_layer(obj_base_pts_feats)
# print(f"obj_base_pts_feats: {obj_base_pts_feats.size()}")
### ==== Encoding joints ==== ###
joints_offset_sequence = x['pert_joints_offset_sequence'] # bsz x nf x nnj x 3
joints_offset_sequence = joints_offset_sequence.permute(0, 2, 3, 1).contiguous()
joints_offset_feats = self.joints_offset_input_process(joints_offset_sequence) # nf x bsz x dim
rel_base_pts_feats_pos_embedding = self.sequence_pos_encoder(joints_offset_feats)
rel_base_pts_feats = rel_base_pts_feats_pos_embedding
# seqTransEncoder, logvar_seqTransEncoder #
rel_base_pts_outputs_mean = self.seqTransEncoder(rel_base_pts_feats)
### ==== Encoding joints ==== ###
### ==== fuse conditional embeddings with joints embeddigns ==== ###
rel_base_pts_outputs_mean = rel_base_pts_outputs_mean + obj_base_pts_feats
### ==== fuse conditional embeddings with joints embeddigns ==== ###
# print(f"rel_base_pts_outputs_mean: {rel_base_pts_outputs_mean.size()}")
### ==== denoise latent features ==== ###
if not self.args.without_dec_pos_emb: # without dec pos embedding
# avg_jts_inputs = rel_base_pts_outputs_mean[0:1, ...]
other_rel_base_pts_outputs = rel_base_pts_outputs_mean #
other_rel_base_pts_outputs = self.sequence_pos_denoising_encoder(other_rel_base_pts_outputs)
rel_base_pts_outputs_mean = other_rel_base_pts_outputs
if self.args.deep_fuse_timeemb:
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_time_emb = basejtsrel_time_emb.repeat(rel_base_pts_outputs_mean.size(0), 1, 1).contiguous()
basejtsrel_seq_latents = rel_base_pts_outputs_mean + basejtsrel_time_emb
if self.args.use_ours_transformer_enc:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)
else:
## denoising process ###
## GET joints seq time embeddings ### ### embed time stamps ###
basejtsrel_time_emb = self.basejtsrel_denoising_embed_timestep(timesteps)
basejtsrel_seq_latents = torch.cat(
[basejtsrel_time_emb, rel_base_pts_outputs_mean], dim=0
)
if self.args.use_ours_transformer_enc: ## mdm ours ##
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents.permute(1, 0, 2), set_attn_to_none=self.args.set_attn_to_none)
basejtsrel_seq_latents = basejtsrel_seq_latents.permute(1, 0, 2)[1:]
else:
basejtsrel_seq_latents = self.basejtsrel_denoising_seqTransEncoder(basejtsrel_seq_latents)[1:]
### ==== denoise latent features ==== ###
other_basejtsrel_seq_latents = basejtsrel_seq_latents # [1:, ...]
joints_offset_output = self.joint_offset_output_process(other_basejtsrel_seq_latents)
joints_offset_output = joints_offset_output.permute(0, 3, 1, 2)
diff_basejtsrel_dict = {
'joints_offset_output': joints_offset_output
}
else:
diff_basejtsrel_dict = {}
rt_dict = {}
rt_dict.update(diff_basejtsrel_dict)
rt_dict.update(diff_basejtse_dict) ### rt_dict and diff_basejtse
rt_dict.update(diff_basejtsrel_to_joints_dict)
rt_dict.update(diff_realbasejtsrel_out_dict) ### diff
return rt_dict
def _apply(self, fn):
super()._apply(fn)
# self.rot2xyz.smpl_model._apply(fn)
def train(self, *args, **kwargs):
super().train(*args, **kwargs)
# self.rot2xyz.smpl_model.train(*args, **kwargs)
### LayerNorm layer ###
class LayerNorm(nn.Module):
def __init__(self, dim, eps=1e-5):
super().__init__()
self.dim = dim
self.eps = eps
def forward(self, x):
x = (x - x.mean(dim=self.dim, keepdim=True)) / torch.sqrt(x.var(dim=self.dim, keepdim=True)+self.eps)
return x
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-np.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
# not used in the final model
x = x + self.pe[:x.shape[0], :]
return self.dropout(x)
class TimestepEmbedder(nn.Module):
def __init__(self, latent_dim, sequence_pos_encoder):
super().__init__()
self.latent_dim = latent_dim
self.sequence_pos_encoder = sequence_pos_encoder
time_embed_dim = self.latent_dim
self.time_embed = nn.Sequential(
nn.Linear(self.latent_dim, time_embed_dim),
nn.SiLU(),
nn.Linear(time_embed_dim, time_embed_dim),
)
def forward(self, timesteps):
return self.time_embed(self.sequence_pos_encoder.pe[timesteps]).permute(1, 0, 2)
## hand sequence; object shape
# InputProcessObjBase(self, data_rep, input_feats, latent_dim)
class InputProcessObjBaseV5(nn.Module): # inputobjbase
def __init__(self, data_rep, input_feats, latent_dim, layernorm=True, without_glb=False, only_with_glb=False):
super().__init__()
self.data_rep = data_rep
self.input_feats = input_feats # 21 * 3 + 3 + 3 --> for each joint + 3 pos + 3 normals #
self.latent_dim = latent_dim
self.pts_feats_encoding_net = nn.Sequential( # nnb --> 21
nn.Linear(input_feats, self.latent_dim), nn.ReLU(),
nn.Linear(self.latent_dim, self.latent_dim),
)
self.glb_feats_encoding_net = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim), nn.ReLU(),
nn.Linear(self.latent_dim, self.latent_dim),
)
self.without_glb = without_glb
self.only_with_glb = only_with_glb
if self.without_glb:
self.pts_glb_feats_encoding_net = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim), nn.ReLU(),
nn.Linear(self.latent_dim, self.latent_dim),
)
else:
self.pts_glb_feats_encoding_net = nn.Sequential(
nn.Linear(self.latent_dim * 2, self.latent_dim), nn.ReLU(),
nn.Linear(self.latent_dim, self.latent_dim),
)
# self.embedding_pn_blk = nn.Sequential( # nnb --> 21
# nn.Linear(input_feats, self.latent_dim), nn.ReLU(),
# nn.Linear(self.latent_dim, self.latent_dim),
# )
def forward(self, x): # decode relative positions and others ##
# bs, nframes, njoints, nfeats = x.shape #
# bsz x nf x nnj x (3 + nnb x (3 + 3)) # bsz x nf x nnb x (latent_dim)
# x: bsz x nf x nnb x (3 + 3 + 21 * 3) # x.size()
bsz, nf, nnb = x.size()[:3]
if self.only_with_glb:
x_pts_emb = self.pts_feats_encoding_net( # input # noisy --- too noisy #
x
)
x_glb_emb, _ = torch.max(x_pts_emb, dim=2, keepdim=True)
x_glb_emb = self.glb_feats_encoding_net(x_glb_emb) # bsz x nf x latent_dim
x_glb_emb = x_glb_emb.squeeze(-2)
x_pts_emb = x_glb_emb.permute(1, 0, 2).contiguous()
else:
x_pts_emb = self.pts_feats_encoding_net( # input # noisy --- too noisy #
x
)
if not self.without_glb:
x_glb_emb, _ = torch.max(x_pts_emb, dim=2, keepdim=True)
x_glb_emb = self.glb_feats_encoding_net(x_glb_emb) # bsz x nf x 1 x latnet_dim
x_pts_emb = torch.cat( # 1
[x_pts_emb, x_glb_emb.repeat(1, 1, nnb, 1)], dim=-1
)
x_pts_emb = self.pts_glb_feats_encoding_net(x_pts_emb) # bsz x nf x nn_base_pts x latent_dim #
x_pts_emb = x_pts_emb.permute(1, 0, 2, 3) # nf x bsz x nn_base_pts x latent_dim #
x_pts_emb = x_pts_emb.contiguous().view(x_pts_emb.size(0), bsz * nnb, -1).contiguous() # nf x (bsz x nn_base_pts) x latent_dim #
return x_pts_emb
class OutputProcessObjBaseRawV5(nn.Module):
def __init__(self, data_rep, latent_dim, not_cond_base=False, out_objbase_v5_bundle_out=False, v5_out_not_cond_base=False, nn_keypoints=21):
super().__init__()
self.data_rep = data_rep
# self.input_feats = input_feats
self.latent_dim = latent_dim
# self.njoints = njoints
self.not_cond_base = not_cond_base ## not cond base ##
# self.nfeats = nfeats # dec cond on latent code and base pts, base normals #
self.v5_out_not_cond_base = v5_out_not_cond_base
if self.not_cond_base:
self.rel_dec_cond_dim = self.latent_dim
self.dist_dec_cond_dim = self.latent_dim
else:
self.rel_dec_cond_dim = self.latent_dim + 3 + 3 + 3
self.dist_dec_cond_dim = self.latent_dim + 3 + 3
# self.use_anchors = use_anchors
self.nn_keypoints = nn_keypoints
# if self.use_anchors:
# self.nn_keypoints =
# self.rel_dec_blk = nn.Sequential(
# nn.Linear(self.rel_dec_cond_dim, 3,),
# )
self.out_objbase_v5_bundle_out = out_objbase_v5_bundle_out
if self.out_objbase_v5_bundle_out:
if self.v5_out_not_cond_base:
self.rel_dec_blk = nn.Sequential(
nn.Linear(self.latent_dim, self.latent_dim // 2), nn.ReLU(),
nn.Linear(self.latent_dim // 2, self.nn_keypoints * 3),
)
else:
self.rel_dec_blk = nn.Sequential(
nn.Linear(self.latent_dim + 3 + 3, self.latent_dim // 2), nn.ReLU(),
nn.Linear(self.latent_dim // 2, self.nn_keypoints * 3),
)
else:
self.rel_dec_blk = nn.Sequential(
nn.Linear(self.rel_dec_cond_dim, 3,),
)
# self.rel_dec_blk = nn.Linear( # rel_dec_blk -> output relative positions #
# self.rel_dec_cond_dim, 3 * 21
# )
self.dist_dec_cond_dim = self.latent_dim + 3 + 3
self.dist_dec_blk = nn.Linear( # dist_dec_blk -> output relative distances #
self.dist_dec_cond_dim, 1 * self.nn_keypoints
)
# self.poseFinal = nn.Linear(self.latent_dim, self.input_feats)
# if self.data_rep == 'rot_vel':
# self.velFinal = nn.Linear(self.latent_dim, self.input_feats)
def forward(self, output, x): # output
# nframes, bs, d = output.shape
# bsz, nframes, nnj, nnb = x['rel_base_pts_to_rhand_joints'].shape[:4] # pert_rel_base_pts_to_rhand_joints
bsz, nframes, nnj, nnb = x['pert_rel_base_pts_to_rhand_joints'].shape[:4] # bsz x nf x nnj x nnb x 3 # nf x nnb x 3 --> noisy input for denoised values #
# forward the samole # base_pts, base_normals, #
# base_pts = x['base_pts'] # bsz x nnb x 3
base_pts = x['normed_base_pts'] # bsz x nnb x 3
base_normals = x['base_normals'] # bsz x nnb x 3
# rel_base_pts_to_rhand_joints = x['rel_base_pts_to_rhand_joints'] # bsz x ws x nnj x nnb x 3
# dist_base_pts_to_rhand_joints = x['dist_base_pts_to_rhand_joints'] # bsz x ws x nnj x nnb
##
# output: bsz x nf x nnj x latent_dim
output = output.view(nframes, bsz, nnb, -1) # nframes x bsz x nnb x latent_dim
output = output.permute(1, 0, 2, 3) # bsz x nnf x nnb x latent_dim ### for the output_dim #
if self.out_objbase_v5_bundle_out:
if self.v5_out_not_cond_base:
output_exp = output
else: # otuptu_exp for rel_dec_blk
base_pts_exp = base_pts.unsqueeze(1).repeat(1, nframes, 1, 1)
base_normals_exp = base_normals.unsqueeze(1).repeat(1, nframes, 1, 1)
output_exp = torch.cat( # with input noisy data # ############### denoised latents for each base pts ###
[output, base_pts_exp, base_normals_exp], dim=-1
)
dec_rel = self.rel_dec_blk(output_exp)
dec_rel = dec_rel.view(bsz, nframes, nnb, nnj, 3).permute(0, 1, 3, 2, 4).contiguous()
else:
# output = output.permute(1, 0, 2)
# output = output.view(bsz, nframes, nnj, -1).contiguous() # bsz x nf x nnj x (decoded_latent_dim) #
output = output.unsqueeze(2).repeat(1, 1, nnj, 1, 1).contiguous()
# bsz x nnframes x d # #
# output = output.permute(1, 0, 2).contiguous().unsqueeze(2).unsqueeze(2).repeat(1, 1, nnj, nnb, 1).contiguous()
base_pts_exp = base_pts.unsqueeze(1).unsqueeze(1).repeat(1, nframes, nnj, 1, 1)
base_normals_exp = base_normals.unsqueeze(1).unsqueeze(1).repeat(1, nframes, nnj, 1, 1)
# bsz x nnframes x nnb x (d + 3 + 3) # --> base normals ##
# if self.not_cond_base:
# output_exp = output
# else:
output_exp = torch.cat( # with input noisy data
[output, base_pts_exp, base_normals_exp, x['pert_rel_base_pts_to_rhand_joints']], dim=-1
)
dec_rel = self.rel_dec_blk(output_exp) # bsz x nnframes x nnb x (21 * 3) --> decoded relative positions #
dec_rel = dec_rel.contiguous().view(bsz, nframes, nnj, nnb, 3).contiguous() # bsz x nnframes x nnb x nnj x 3 #
# decoded rel, decoded distances #
out = {
'dec_rel': dec_rel,
# 'dec_dist': dec_dist.squeeze(-1),
}
return out ## output
|