size_categories:10K<n<100K format:parquet modality:image modality:timeseries library:datasets library:pandas library:mlcroissant library:polars region:us
---
dataset_info:
  features:
  - name: img
    dtype: image
  - name: label
    dtype:
      class_label:
        names:
          '0': apple
          '1': aquarium_fish
          '2': baby
          '3': bear
          '4': beaver
          '5': bed
          '6': bee
          '7': beetle
          '8': bicycle
          '9': bottle
          '10': bowl
          '11': boy
          '12': bridge
          '13': bus
          '14': butterfly
          '15': camel
          '16': can
          '17': castle
          '18': caterpillar
          '19': cattle
          '20': chair
          '21': chimpanzee
          '22': clock
          '23': cloud
          '24': cockroach
          '25': couch
          '26': cra
          '27': crocodile
          '28': cup
          '29': dinosaur
          '30': dolphin
          '31': elephant
          '32': flatfish
          '33': forest
          '34': fox
          '35': girl
          '36': hamster
          '37': house
          '38': kangaroo
          '39': keyboard
          '40': lamp
          '41': lawn_mower
          '42': leopard
          '43': lion
          '44': lizard
          '45': lobster
          '46': man
          '47': maple_tree
          '48': motorcycle
          '49': mountain
          '50': mouse
          '51': mushroom
          '52': oak_tree
          '53': orange
          '54': orchid
          '55': otter
          '56': palm_tree
          '57': pear
          '58': pickup_truck
          '59': pine_tree
          '60': plain
          '61': plate
          '62': poppy
          '63': porcupine
          '64': possum
          '65': rabbit
          '66': raccoon
          '67': ray
          '68': road
          '69': rocket
          '70': rose
          '71': sea
          '72': seal
          '73': shark
          '74': shrew
          '75': skunk
          '76': skyscraper
          '77': snail
          '78': snake
          '79': spider
          '80': squirrel
          '81': streetcar
          '82': sunflower
          '83': sweet_pepper
          '84': table
          '85': tank
          '86': telephone
          '87': television
          '88': tiger
          '89': tractor
          '90': train
          '91': trout
          '92': tulip
          '93': turtle
          '94': wardrobe
          '95': whale
          '96': willow_tree
          '97': wolf
          '98': woman
          '99': worm
  - name: features
    sequence: float32
  - name: index
    dtype: int64
  splits:
  - name: train
    num_bytes: 317545106.0
    num_examples: 50000
  - name: test
    num_bytes: 63564261.0
    num_examples: 10000
  download_size: 518939586
  dataset_size: 381109367.0
configs:
- config_name: default
  data_files:
  - split: train
    path: data/train-*
  - split: test
    path: data/test-*
---

This dataset is original cifar10 dataset but also contains features from vit_large.

# Import packages
  from transformers import AutoImageProcessor, AutoModelForImageClassification
  import torch
  from torch.utils.data import DataLoader
  from datasets import load_dataset


#  model
  processor = AutoImageProcessor.from_pretrained("google/vit-large-patch32-384",use_fast=True)
  model = AutoModelForImageClassification.from_pretrained("google/vit-large-patch32-384")

# dataset
  ds = load_dataset("aaaakash001/cifar100_Vit_large")

# Set model to evaluation mode
  model.eval()
  
  transform = transforms.Compose([
          transforms.Resize((384, 384)),  # Resize images to a consistent size
          transforms.ToTensor(),           # Convert PIL images to tensors
          transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
      ])

# Define a function to process and extract features from each image
  def extract_features(batch):
      imgs = batch['img']  # This will now be a list of images in a batch
  
      # Preprocess each image in the batch
      img_tensors = [transform(img).unsqueeze(0).to(device) for img in imgs]  # Add batch dimension and move to device
      img_tensors = torch.cat(img_tensors, dim=0)  # Concatenate to create a batch tensor
  
      inputs = {'pixel_values': img_tensors}  # Adjust for model input (assuming using Hugging Face models)
  
      # Forward pass through the model to get features
      with torch.no_grad():
          outputs = model(**inputs, output_hidden_states=True)
  
      hidden_states = outputs.hidden_states  # List of all hidden states
  
      # Extract the final hidden state
      final_hidden_state = hidden_states[-1]  # Shape: [batch_size, num_patches, hidden_size]
      
      # Optionally, pool the features
      pooled_features = final_hidden_state.mean(dim=1)  # Shape: [batch_size, hidden_size]
      
      # Return the features as a new column for the batch
      return {"features": pooled_features.cpu().numpy()}  # 


  ds_feature = ds.map(extract_features, batched=True, batch_size=64)



  def custom_collate(batch):
      # Define the transformations you want to apply to the images
      transform = transforms.Compose([
          transforms.Resize((224, 224)),  # Resize images to a consistent size
          transforms.ToTensor(),           # Convert PIL images to tensors
      ])
  
      # Extracting each field from the batch
      index = [item['index'] for item in batch]
      images = [transform(item['img']) for item in batch]  # Load and transform images
      labels = torch.tensor([item['label'] for item in batch])          # Convert labels to a tensor
      features = [torch.tensor(item['features']) for item in batch]     # Convert features to tensors
      
      # Return a dictionary with batched data
      return {
          'index': index,
          'img': torch.stack(images),    # Stack image tensors into a single tensor
          'label': labels,
          'features': torch.stack(features),  # Stack feature tensors into a single tensor
      }


# Using dataloader
  train_dataloader = DataLoader(ds_feature['train'], batch_size=64, shuffle=True,collate_fn=custom_collate)
  test_dataloader = DataLoader(ds_feature['test'], batch_size=64, shuffle=True,collate_fn=custom_collate)
