readme: ---
  tags:
  - image-classification
  - timm
  library_tag: timm
  license: apache-2.0
  datasets:
  - imagenet-1k
  - imagenet-21k
  ---
  # Model card for maxvit_xlarge_tf_384.in21k_ft_in1k
  
  An official MaxViT image classification model. Pretrained in tensorflow on ImageNet-21k (21843 Google specific instance of ImageNet-22k) and fine-tuned on ImageNet-1k by paper authors.
  
  Ported from official Tensorflow implementation (https://github.com/google-research/maxvit) to PyTorch by Ross Wightman.
  
  ### Model Variants in [maxxvit.py](https://github.com/rwightman/pytorch-image-models/blob/main/timm/models/maxxvit.py)
  
  MaxxViT covers a number of related model architectures that share a common structure including:
  - CoAtNet - Combining MBConv (depthwise-separable) convolutional blocks in early stages with self-attention transformer blocks in later stages.
  - MaxViT - Uniform blocks across all stages, each containing a MBConv (depthwise-separable) convolution block followed by two self-attention blocks with different partitioning schemes (window followed by grid).
  - CoAtNeXt - A timm specific arch that uses ConvNeXt blocks in place of MBConv blocks in CoAtNet. All normalization layers are LayerNorm (no BatchNorm).
  - MaxxViT - A timm specific arch that uses ConvNeXt blocks in place of MBConv blocks in MaxViT. All normalization layers are LayerNorm (no BatchNorm).
  - MaxxViT-V2 - A MaxxViT variation that removes the window block attention leaving only ConvNeXt blocks and grid attention w/ more width to compensate.
  
  Aside from the major variants listed above, there are more subtle changes from model to model. Any model name with the string `rw` are `timm` specific configs w/ modelling adjustments made to favour PyTorch eager use. These were created while training initial reproductions of the models so there are variations.
  All models with the string `tf` are models exactly matching Tensorflow based models by the original paper authors with weights ported to PyTorch. This covers a number of MaxViT models. The official CoAtNet models were never released.
  
  ## Model Details
  - **Model Type:** Image classification / feature backbone
  - **Model Stats:**
    - Params (M): 475.3
    - GMACs: 292.8
    - Activations (M): 668.8
    - Image size: 384 x 384
  - **Papers:**
    - MaxViT: Multi-Axis Vision Transformer: https://arxiv.org/abs/2204.01697
  - **Dataset:** ImageNet-1k
  - **Pretrain Dataset:** ImageNet-21k
  
  ## Model Usage
  ### Image Classification
  ```python
  from urllib.request import urlopen
  from PIL import Image
  import timm
  
  img = Image.open(
      urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
  
  model = timm.create_model('maxvit_xlarge_tf_384.in21k_ft_in1k', pretrained=True)
  model = model.eval()
  
  # get model specific transforms (normalization, resize)
  data_config = timm.data.resolve_model_data_config(model)
  transforms = timm.data.create_transform(**data_config, is_training=False)
  
  output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1
  
  top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)
  ```
  
  ### Feature Map Extraction
  ```python
  from urllib.request import urlopen
  from PIL import Image
  import timm
  
  img = Image.open(
      urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
  
  model = timm.create_model(
      'maxvit_xlarge_tf_384.in21k_ft_in1k',
      pretrained=True,
      features_only=True,
  )
  model = model.eval()
  
  # get model specific transforms (normalization, resize)
  data_config = timm.data.resolve_model_data_config(model)
  transforms = timm.data.create_transform(**data_config, is_training=False)
  
  output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1
  
  for o in output:
      # print shape of each feature map in output
      # e.g.: 
      #  torch.Size([1, 128, 192, 192])
      #  torch.Size([1, 128, 96, 96])
      #  torch.Size([1, 256, 48, 48])
      #  torch.Size([1, 512, 24, 24])
      #  torch.Size([1, 1024, 12, 12])
      print(o.shape)
  ```
  
  ### Image Embeddings
  ```python
  from urllib.request import urlopen
  from PIL import Image
  import timm
  
  img = Image.open(
      urlopen('https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'))
  
  model = timm.create_model(
      'maxvit_xlarge_tf_384.in21k_ft_in1k',
      pretrained=True,
      num_classes=0,  # remove classifier nn.Linear
  )
  model = model.eval()
  
  # get model specific transforms (normalization, resize)
  data_config = timm.data.resolve_model_data_config(model)
  transforms = timm.data.create_transform(**data_config, is_training=False)
  
  output = model(transforms(img).unsqueeze(0))  # output is (batch_size, num_features) shaped tensor
  
  # or equivalently (without needing to set num_classes=0)
  
  output = model.forward_features(transforms(img).unsqueeze(0))
  # output is unpooled (ie.e a (batch_size, num_features, H, W) tensor
  
  output = model.forward_head(output, pre_logits=True)