@rishit_dagli (Rishit Dagli) posted a new topic, subject "Converting channel_last data to channel_first". It read:

   I was writing a tutorial for one of my paper implementations quite similar to the Image classification tutorial and the model I use expects data in the `channels_first` format. Of course, while using `tf.data.Dataset` we can simply use `tf.transpose`.
    
    I was wondering if there was any _built-in way_ to load data with `tf.keras.utils.image_dataset_from_directory` in the `channels_first` format or a layer to do so?
    
    Right now I do so in my model adding a lambda layer to do so:
    [code] 
        model = tf.keras.Sequential([tf.keras.layers.Lambda(lambda x: tf.einsum("...ijk->...kij", x)),
                                                           ...
                                                           ...])
        
    [/code]
    
    Here is my complete Colab notebook if required (created in a similar fashion to the classification guide on TensorFlow website):
    
    github.com
    
    #### Rishit-dagli/Transformer-in-Transformer/blob/main/training.ipynb
    [code] 
        {
         "cells": [
          {
           "cell_type": "markdown",
           "metadata": {
            "colab_type": "text",
            "id": "view-in-github"
           },
           "source": [
            "<a href=\"https://colab.research.google.com/github/Rishit-dagli/Transformer-in-Transformer/blob/main/training.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
           ]
          },
          {
           "cell_type": "markdown",
           "metadata": {
            "id": "FE7KNzPPVrVV"
           },
           "source": [
            "# Image classification with Transformer iN Transformer\n",
            "\n",
        
    [/code]
    
    This file has been truncated. show original

So @ariG23498 (Aritra Roy Gosthipaty) replied

    Another option is to use the `tf.keras.layers.Permute`
    
    Link: tf.keras.layers.Permute | TensorFlow Core v2.8.0

Then @Bhack (Stefano Fabri) said

    In the end this API is just going to dataset map over `load_image` that it is a multiple call over `tf.image.decode_image` so you don't have any parameter to do it internally.
    
    github.com
    
    #### keras-team/keras/blob/41c992105fdc0f0d00e7618752f36e0884f8e5c9/keras/preprocessing/image_dataset.py#L251
    [code] 
              
                
        
        
                    
          241.                                 labels,
        
                    
          242.                                 label_mode,
        
                    
          243.                                 num_classes,
        
                    
          244.                                 interpolation,
        
                    
          245.                                 crop_to_aspect_ratio=False):
        
                    
          246.   """Constructs a dataset of images and labels."""
        
                    
          247.   # TODO(fchollet): consider making num_parallel_calls settable
        
                    
          248.   path_ds = tf.data.Dataset.from_tensor_slices(image_paths)
        
                    
          249.   args = (image_size, num_channels, interpolation, crop_to_aspect_ratio)
        
                    
          250.   img_ds = path_ds.map(
        
                    
          251.       lambda x: load_image(x, *args), num_parallel_calls=tf.data.AUTOTUNE)
        
                    
          252.   if label_mode:
        
                    
          253.     label_ds = dataset_utils.labels_to_dataset(labels, label_mode, num_classes)
        
                    
          254.     img_ds = tf.data.Dataset.zip((img_ds, label_ds))
        
                    
          255.   return img_ds
        
                    
          256. 
                    
          257. 
                    
          258. def load_image(path, image_size, num_channels, interpolation,
        
                    
          259.                crop_to_aspect_ratio=False):
        
                    
          260.   """Load an image from a path and resize it."""
        
                    
          261.   img = tf.io.read_file(path)
        
                
        
              
            
    [/code]

So @rishit_dagli (Rishit Dagli) replied

    Got it, thanks so much @ariG23498 and @Bhack ! Also surprisingly enough (I haven't checked the source code for `Permute` layer yet and I will get to doing that) while my experiments I noticed the exact same operation with `tf.keras.layers.Permute` is faster than `tf.einsum`.

To which @Bhack (Stefano Fabri) replied

    permute is a `tf.transpose`


{'source': 'discourse', 'url': 'https://discuss.tensorflow.org/t/converting-channel-last-data-to-channel-first/7143', 'title': 'Converting channel_last data to channel_first', 'views': 498, 'has_accepted_answer': False, 'word_count': 264, 'like_count': 6, 'reply_count': 1, 'participant_count': 3, 'posts': 5, 'tags': 'models keras', 'author': '@rishit_dagli (Rishit Dagli)', 'participants': '@rishit_dagli (Rishit Dagli), @ariG23498 (Aritra Roy Gosthipaty), @Bhack (Stefano Fabri)', 'image_url': [None], 'created_at': '2022-01-19T09:44:18.427Z', 'last_posted_at': '2022-01-20T06:51:32.667Z'}