Title: Update torch_formatter.py
State: closed

Ensure PyTorch images are converted to (C, H, W) instead of (H, W, C). See #6394 for motivation. 

Comments: The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6402). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.<details>
<summary>Show benchmarks</summary>

PyArrow==8.0.0

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005391 / 0.011353 (-0.005962) | 0.003908 / 0.011008 (-0.007100) | 0.064342 / 0.038508 (0.025834) | 0.031385 / 0.023109 (0.008275) | 0.251869 / 0.275898 (-0.024030) | 0.294875 / 0.323480 (-0.028605) | 0.003053 / 0.007986 (-0.004933) | 0.002881 / 0.004328 (-0.001448) | 0.050072 / 0.004250 (0.045822) | 0.044463 / 0.037052 (0.007411) | 0.264646 / 0.258489 (0.006157) | 0.296024 / 0.293841 (0.002183) | 0.027832 / 0.128546 (-0.100714) | 0.010937 / 0.075646 (-0.064710) | 0.207799 / 0.419271 (-0.211472) | 0.036423 / 0.043533 (-0.007110) | 0.251022 / 0.255139 (-0.004117) | 0.271366 / 0.283200 (-0.011833) | 0.019780 / 0.141683 (-0.121903) | 1.149634 / 1.452155 (-0.302521) | 1.196476 / 1.492716 (-0.296240) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.094857 / 0.018006 (0.076850) | 0.312159 / 0.000490 (0.311669) | 0.000215 / 0.000200 (0.000015) | 0.000042 / 0.000054 (-0.000012) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018858 / 0.037411 (-0.018553) | 0.062022 / 0.014526 (0.047496) | 0.078302 / 0.176557 (-0.098255) | 0.122199 / 0.737135 (-0.614936) | 0.076044 / 0.296338 (-0.220294) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.286414 / 0.215209 (0.071205) | 2.785446 / 2.077655 (0.707791) | 1.438248 / 1.504120 (-0.065872) | 1.307558 / 1.541195 (-0.233636) | 1.337172 / 1.468490 (-0.131318) | 0.582228 / 4.584777 (-4.002549) | 2.457207 / 3.745712 (-1.288505) | 2.906692 / 5.269862 (-2.363169) | 1.833020 / 4.565676 (-2.732656) | 0.063549 / 0.424275 (-0.360726) | 0.005080 / 0.007607 (-0.002527) | 0.333178 / 0.226044 (0.107133) | 3.332463 / 2.268929 (1.063534) | 1.797209 / 55.444624 (-53.647415) | 1.514446 / 6.876477 (-5.362031) | 1.601252 / 2.142072 (-0.540820) | 0.664933 / 4.805227 (-4.140294) | 0.120140 / 6.500664 (-6.380524) | 0.042769 / 0.075469 (-0.032700) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.983738 / 1.841788 (-0.858050) | 11.996685 / 8.074308 (3.922376) | 9.594757 / 10.191392 (-0.596635) | 0.146680 / 0.680424 (-0.533744) | 0.014455 / 0.534201 (-0.519746) | 0.292546 / 0.579283 (-0.286737) | 0.270381 / 0.434364 (-0.163983) | 0.326759 / 0.540337 (-0.213579) | 0.423387 / 1.386936 (-0.963549) |

</details>
PyArrow==latest

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005659 / 0.011353 (-0.005694) | 0.004841 / 0.011008 (-0.006167) | 0.049598 / 0.038508 (0.011090) | 0.031796 / 0.023109 (0.008687) | 0.265967 / 0.275898 (-0.009931) | 0.287648 / 0.323480 (-0.035832) | 0.004264 / 0.007986 (-0.003721) | 0.002759 / 0.004328 (-0.001569) | 0.049249 / 0.004250 (0.044999) | 0.049526 / 0.037052 (0.012474) | 0.357765 / 0.258489 (0.099276) | 0.307563 / 0.293841 (0.013722) | 0.030329 / 0.128546 (-0.098217) | 0.010536 / 0.075646 (-0.065111) | 0.057547 / 0.419271 (-0.361724) | 0.059874 / 0.043533 (0.016341) | 0.264946 / 0.255139 (0.009807) | 0.283666 / 0.283200 (0.000466) | 0.021005 / 0.141683 (-0.120677) | 1.170959 / 1.452155 (-0.281195) | 1.206232 / 1.492716 (-0.286484) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.097567 / 0.018006 (0.079561) | 0.308585 / 0.000490 (0.308095) | 0.000220 / 0.000200 (0.000020) | 0.000052 / 0.000054 (-0.000003) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.023267 / 0.037411 (-0.014144) | 0.075896 / 0.014526 (0.061370) | 0.087341 / 0.176557 (-0.089216) | 0.130270 / 0.737135 (-0.606866) | 0.091086 / 0.296338 (-0.205252) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.298728 / 0.215209 (0.083519) | 2.876498 / 2.077655 (0.798843) | 1.591584 / 1.504120 (0.087464) | 1.461251 / 1.541195 (-0.079944) | 1.483276 / 1.468490 (0.014786) | 0.572344 / 4.584777 (-4.012433) | 2.474050 / 3.745712 (-1.271662) | 2.707051 / 5.269862 (-2.562811) | 1.759210 / 4.565676 (-2.806466) | 0.063420 / 0.424275 (-0.360855) | 0.005014 / 0.007607 (-0.002593) | 0.344490 / 0.226044 (0.118446) | 3.411065 / 2.268929 (1.142137) | 1.937232 / 55.444624 (-53.507392) | 1.665826 / 6.876477 (-5.210650) | 1.824377 / 2.142072 (-0.317696) | 0.631630 / 4.805227 (-4.173597) | 0.115791 / 6.500664 (-6.384873) | 0.040846 / 0.075469 (-0.034623) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 1.015248 / 1.841788 (-0.826540) | 12.738696 / 8.074308 (4.664388) | 10.324303 / 10.191392 (0.132911) | 0.153597 / 0.680424 (-0.526827) | 0.015396 / 0.534201 (-0.518805) | 0.287160 / 0.579283 (-0.292123) | 0.279886 / 0.434364 (-0.154478) | 0.324128 / 0.540337 (-0.216210) | 0.456089 / 1.386936 (-0.930847) |

</details>
</details>

![](https://cml.dev/watermark.png#a02997de6e990eb78dccd905f05e12404a919156 "CML watermark")