import torch import torchvision from torchvision import models from torch import nn def create_effnetb2_model( num_classes:int = 3, # Default output classes = 3 (pizza, steak, sushi) seed: int = 42 ): # 1. Setup the pretrained weights of EffNetB2 effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT # 2. Get EffNetB2 transforms to make sure we utilize the same transform as the model that was trained on. effnetb2_transforms = effnetb2_weights.transforms() # 3. Setup pretrained model instance model = torchvision.models.efficientnet_b2(weights=effnetb2_weights) # could also use weights="DEFAULT" # 4. Freeze the base layers in the model (this will stop all layers from training) for param in model.parameters(): param.requires_grad = False # 5. Adjust the classifier head of the pretrained model to suit our use case # Random seed for reproducibility torch.manual_seed(seed) model.classifier = nn.Sequential( nn.Dropout(p=0.3, inplace=True), nn.Linear(in_features=1408, out_features=num_classes, bias=True) ) return model, effnetb2_transforms