effnet-pneumonia / model.py
c3n7's picture
First of many
8ee3890
raw
history blame contribute delete
814 Bytes
import torch
import torchvision
from torch import nn
def create_effnetb0_model(num_classes: int = 3, seed: int = 42):
"""Creates an EfficientNetB0 Model and Transforms"""
# 1. Setup Weights
weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
# 2. Get transforms
transforms = weights.transforms()
# 3. Setup pretrained model
model = torchvision.models.efficientnet_b0(weights=weights)
# 4 Freeze all layers
for param in model.parameters():
param.requires_grad = False
# 5. Change classifier head with random seed for reproducability
torch.manual_seed(seed)
model.classifier = nn.Sequential(
nn.Dropout(p=0.2, inplace=True),
nn.Linear(in_features=1280, out_features=num_classes, bias=True),
)
return model, transforms