docstr: Linear classifier for benchmarking.
  
  Settings based on SimCLR [0].
  
  - [0]: https://arxiv.org/abs/2002.05709
  
  Args:
      model:
          Model used for feature extraction. Must define a forward(images) method
          that returns a feature tensor.
      batch_size_per_device:
          Batch size per device.
      feature_dim:
          Dimension of features returned by forward method of model.
      num_classes:
          Number of classes in the dataset.
      topk:
          Tuple of integers defining the top-k accuracy metrics to compute.
      freeze_model:
          If True, the model is frozen and only the classification head is
          trained. This corresponds to the linear eval setting. Set to False for
          finetuning.
  
  Examples:
  
      >>> from pytorch_lightning import Trainer
      >>> from torch import nn
      >>> import torchvision
      >>> from lightly.models import LinearClassifier
      >>> from lightly.modles.modules import SimCLRProjectionHead
      >>>
      >>> class SimCLR(nn.Module):
      >>>     def __init__(self):
      >>>         super().__init__()
      >>>         self.backbone = torchvision.models.resnet18()
      >>>         self.backbone.fc = nn.Identity() # Ignore classification layer
      >>>         self.projection_head = SimCLRProjectionHead(512, 512, 128)
      >>>
      >>>     def forward(self, x):
      >>>         # Forward must return image features.
      >>>         features = self.backbone(x).flatten(start_dim=1)
      >>>         return features
      >>>
      >>> # Initialize a model.
      >>> model = SimCLR()
      >>>
      >>> # Wrap it with a LinearClassifier.
      >>> linear_classifier = LinearClassifier(
      >>>     model,
      >>>     batch_size=256,
      >>>     num_classes=10,
      >>>     freeze_model=True, # linear evaluation, set to False for finetune
      >>> )
      >>>
      >>> # Train the linear classifier.
      >>> trainer = Trainer(max_epochs=90)
      >>> trainer.fit(linear_classifier, train_dataloader, val_dataloader)
docstr_tok: ["Linear","classifier","for","benchmarking",".","Settings","based","on","SimCLR","[","0","]",".","-","[","0","]",":","https",":","\/\/arxiv.org\/abs\/2002.05709","Args",":","model",":","Model","used","for","feature","extraction",".","Must","define","a","forward","(","images",")","method","that","returns","a","feature","tensor",".","batch_size_per_device",":","Batch","size","per","device",".","feature_dim",":","Dimension","of","features","returned","by","forward","method","of","model",".","num_classes",":","Number","of","classes","in","the","dataset",".","topk",":","Tuple","of","integers","defining","the","top-k","accuracy","metrics","to","compute",".","freeze_model",":","If","True",",","the","model","is","frozen","and","only","the","classification","head","is","trained",".","This","corresponds","to","the","linear","eval","setting",".","Set","to","False","for","finetuning",".","Examples",":",">",">",">","from","pytorch_lightning","import","Trainer",">",">",">","from","torch","import","nn",">",">",">","import","torchvision",">",">",">","from","lightly.models","import","LinearClassifier",">",">",">","from","lightly.modles.modules","import","SimCLRProjectionHead",">",">",">",">",">",">","class","SimCLR","(","nn.Module",")",":",">",">",">","def","__init__","(","self",")",":",">",">",">","super","(",")",".__init__","(",")",">",">",">","self.backbone","=","torchvision.models.resnet18","(",")",">",">",">","self.backbone.fc","=","nn.Identity","(",")","#","Ignore","classification","layer",">",">",">","self.projection_head","=","SimCLRProjectionHead","(","512",",","512",",","128",")",">",">",">",">",">",">","def","forward","(","self",",","x",")",":",">",">",">","#","Forward","must","return","image","features",".",">",">",">","features","=","self.backbone","(","x",")",".flatten","(","start_dim=1",")",">",">",">","return","features",">",">",">",">",">",">","#","Initialize","a","model",".",">",">",">","model","=","SimCLR","(",")",">",">",">",">",">",">","#","Wrap","it","with","a","LinearClassifier",".",">",">",">","linear_classifier","=","LinearClassifier","(",">",">",">","model",",",">",">",">","batch_size=256",",",">",">",">","num_classes=10",",",">",">",">","freeze_model=True",",","#","linear","evaluation",",","set","to","False","for","finetune",">",">",">",")",">",">",">",">",">",">","#","Train","the","linear","classifier",".",">",">",">","trainer","=","Trainer","(","max_epochs=90",")",">",">",">","trainer.fit","(","linear_classifier",",","train_dataloader",",","val_dataloader",")"]
code: def __init__(
      self,
      model: Module,
      batch_size_per_device: int,
      feature_dim: int = 2048,
      num_classes: int = 1000,
      topk: Tuple[int, ...] = (1, 5),
      freeze_model: bool = False,
  ) -> None:
      """Linear classifier for benchmarking.
  
      Settings based on SimCLR [0].
  
      - [0]: https://arxiv.org/abs/2002.05709
  
      Args:
          model:
              Model used for feature extraction. Must define a forward(images) method
              that returns a feature tensor.
          batch_size_per_device:
              Batch size per device.
          feature_dim:
              Dimension of features returned by forward method of model.
          num_classes:
              Number of classes in the dataset.
          topk:
              Tuple of integers defining the top-k accuracy metrics to compute.
          freeze_model:
              If True, the model is frozen and only the classification head is
              trained. This corresponds to the linear eval setting. Set to False for
              finetuning.
  
      Examples:
  
          >>> from pytorch_lightning import Trainer
          >>> from torch import nn
          >>> import torchvision
          >>> from lightly.models import LinearClassifier
          >>> from lightly.modles.modules import SimCLRProjectionHead
          >>>
          >>> class SimCLR(nn.Module):
          >>>     def __init__(self):
          >>>         super().__init__()
          >>>         self.backbone = torchvision.models.resnet18()
          >>>         self.backbone.fc = nn.Identity() # Ignore classification layer
          >>>         self.projection_head = SimCLRProjectionHead(512, 512, 128)
          >>>
          >>>     def forward(self, x):
          >>>         # Forward must return image features.
          >>>         features = self.backbone(x).flatten(start_dim=1)
          >>>         return features
          >>>
          >>> # Initialize a model.
          >>> model = SimCLR()
          >>>
          >>> # Wrap it with a LinearClassifier.
          >>> linear_classifier = LinearClassifier(
          >>>     model,
          >>>     batch_size=256,
          >>>     num_classes=10,
          >>>     freeze_model=True, # linear evaluation, set to False for finetune
          >>> )
          >>>
          >>> # Train the linear classifier.
          >>> trainer = Trainer(max_epochs=90)
          >>> trainer.fit(linear_classifier, train_dataloader, val_dataloader)
  
      """
      super().__init__()
      self.save_hyperparameters(ignore="model")
  
      self.model = model
      self.batch_size_per_device = batch_size_per_device
      self.feature_dim = feature_dim
      self.num_classes = num_classes
      self.topk = topk
      self.freeze_model = freeze_model
  
      self.classification_head = Linear(feature_dim, num_classes)
      self.criterion = CrossEntropyLoss()
  
file_path: lightly/lightly/utils/benchmarking/linear_classifier.py
imports: from typing import Any, Dict, List, Tuple, Union
  import torch
  from pytorch_lightning import LightningModule
  from torch import Tensor
  from torch.nn import CrossEntropyLoss, Linear, Module
  from torch.optim import SGD, Optimizer
  from lightly.utils.benchmarking.topk import mean_topk_accuracy
  from lightly.utils.scheduler import CosineWarmupScheduler
label_desc: Use image node_id 1 to create a new LinearClassifier object from inherited base classes: LightningModule with example: obj = LinearClassifier(model, batch_size_per_device, feature_dim, num_classes, topk, freeze_model)