<jupyter_start><jupyter_text>Library Imports<jupyter_code>from time import time
notebook_start_time = time()
import re
import torch
from torch import nn
from torchvision import models<jupyter_output><empty_output><jupyter_text>Constants and Utilities<jupyter_code>DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def breaker(num=50, char="*") -> None:
    print("\n" + num*char + "\n")


def head(x, no_of_ele=5) -> None:
    print(x[:no_of_ele])<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>def build_model(model_name: str, pretrained=True):
    class ImageModel(nn.Module):
        def __init__(self, model_name=None, pretrained=False):
            super(ImageModel, self).__init__()

            # Resnets
            if re.match(r"^resnet18$", model_name, re.IGNORECASE):
                self.features = models.resnet18(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^resnet34$", model_name, re.IGNORECASE):
                self.features = models.resnet34(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^resnet50$", model_name, re.IGNORECASE):
                self.features = models.resnet50(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^resnet101$", model_name, re.IGNORECASE):
                self.features = models.resnet101(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^resnet152$", model_name, re.IGNORECASE):
                self.features = models.resnet152(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            
            # ResNext
            if re.match(r"^resnext50$", model_name, re.IGNORECASE):
                self.features = models.resnext50_32x4d(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^resnext101$", model_name, re.IGNORECASE):
                self.features = models.resnext101_32x8d(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            
            # Wide Resnet
            if re.match(r"^wresnet50$", model_name, re.IGNORECASE):
                self.features = models.wide_resnet50_2(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^wresnet101$", model_name, re.IGNORECASE):
                self.features = models.wide_resnet101_2(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Flatten", nn.Flatten())

                
            # VGG
            if re.match(r"^vgg11$", model_name, re.IGNORECASE):
                self.features = models.vgg11_bn(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-2])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(2, 2)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^vgg13$", model_name, re.IGNORECASE):
                self.features = models.vgg13_bn(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-2])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(2, 2)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^vgg16$", model_name, re.IGNORECASE):
                self.features = models.vgg16_bn(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-2])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(2, 2)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^vgg19$", model_name, re.IGNORECASE):
                self.features = models.vgg19_bn(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-2])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(2, 2)))
                self.features.add_module("Flatten", nn.Flatten())

            
            # Densenet
            if re.match(r"^densenet121$", model_name, re.IGNORECASE):
                self.features = models.densenet121(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(1, 1)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^densenet161$", model_name, re.IGNORECASE):
                self.features = models.densenet161(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(1, 1)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^densenet169$", model_name, re.IGNORECASE):
                self.features = models.densenet169(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(1, 1)))
                self.features.add_module("Flatten", nn.Flatten())
            
            if re.match(r"^densenet201$", model_name, re.IGNORECASE):
                self.features = models.densenet201(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(1, 1)))
                self.features.add_module("Flatten", nn.Flatten())
            
            
            # Mobilenet
            if re.match(r"^mobilenet$", model_name, re.IGNORECASE):
                self.features = models.mobilenet_v2(pretrained=pretrained, progress=True)
                if pretrained:
                    self.freeze()
                self.features = nn.Sequential(*[*self.features.children()][:-1])
                self.features.add_module("Adaptive Average Pool", nn.AdaptiveAvgPool2d(output_size=(1, 1)))
                self.features.add_module("Flatten", nn.Flatten())
        
        def freeze(self):
            for params in self.parameters():
                params.requires_grad = False

        def forward(self, x):
            return self.features(x)

    
    breaker()
    print("Building Model ...")
    
    model = ImageModel(model_name=model_name, pretrained=pretrained)
    
    return model<jupyter_output><empty_output><jupyter_text>Save Pretrained Weights<jupyter_code>model_names  = ["resnet18", "resnet34", "resnet50", "resnet101", "resnet152",
                "resnext50", "resnext101",
                "wresnet50", "wresnet101",
                "vgg11", "vgg13", "vgg16", "vgg19",
                "densenet121", "densenet161", "densenet169", "densenet201",
                "mobilenet"]

for model_name in model_names:
    model = build_model(model_name=model_name, pretrained=True).to(DEVICE)
    torch.save(model.state_dict(), "./{}_state.pt".format(model_name))
breaker()
print("Notebook Run Time : {:.2f} minutes".format((time()-notebook_start_time)/60))
breaker()<jupyter_output>**************************************************

Notebook Run Time : 3.08 minutes

**************************************************
{'fraction_non_alphanumeric': 0.1387878787878788, 'fraction_numerical': 0.019090909090909092, 'mean_word_length': 2.098904538341158, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '470973', 'license': 'MIT', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2840, 'n_words': 415, 'path': 'Notebooks - 1/Pretrained Model Weights (All).ipynb', 'repo_name': 'pchandrasekaran1595/PetFinder.my---Pawpularity-Contest', 'size': '9900'}