Multi-Label-Image-Classification / classifier.py
classifier.py
Raw
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np

NUM_CLASSES = 21

class SimpleClassifier(nn.Module):
    def __init__(self):
        super(SimpleClassifier, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, 5)
        self.conv2 = nn.Conv2d(64, 32, 3)
        self.conv3 = nn.Conv2d(32, 16, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(16 * 26 * 26, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, NUM_CLASSES)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = x.view(x.size()[0], 16 * 26 * 26)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    
        
        
        
        
        
class Classifier(nn.Module):
    # TODO: implement me
    def __init__(self):
        super(Classifier, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, NUM_CLASSES),
        )
        


    
    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        
        return x
        

class Test_Classifier(nn.Module):
    # TODO: implement me
    def __init__(self):
        super(Test_Classifier, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            #========================================================
            nn.MaxPool2d(kernel_size=3, stride=2),
            #========================================================
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, kernel_size=5, padding=2),
            nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, kernel_size=5, padding=2),
            nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            #========================================================
            nn.MaxPool2d(kernel_size=3, stride=2),
            #========================================================
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, kernel_size=3, padding=1),
            nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            #========================================================
            nn.MaxPool2d(kernel_size=3, stride=2),
            #========================================================
            #nn.Conv2d(256, 256, kernel_size=3, padding=1),
            #nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            #nn.ReLU(inplace=True),
            #nn.Conv2d(256, 256, kernel_size=3, padding=1),
            #nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            #nn.ReLU(inplace=True),
            #nn.Conv2d(256, 256, kernel_size=3, padding=1),
            #nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            #nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),#used kernel size one only
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            nn.ReLU(inplace=True),
            #========================================================
            nn.MaxPool2d(kernel_size=3, stride=2),
            #========================================================
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, NUM_CLASSES),
        )
    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        
        return x


        
        
        


# class Classifier(nn.Module):
    # # TODO: implement me
    # def __init__(self):
        # super(Classifier, self).__init__()
        # self.features = nn.Sequential(
            # nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            # nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # nn.Conv2d(64, 192, kernel_size=5, padding=2),
            # nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(192, 192, kernel_size=5, padding=2),
            # nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(192, 192, kernel_size=5, padding=2),
            # nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # #========================================================
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # #========================================================
            # nn.Conv2d(192, 384, kernel_size=3, padding=1),
            # nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(384, 384, kernel_size=3, padding=1),
            # nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(384, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # #========================================================
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # #========================================================
            # nn.Conv2d(256, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(256, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(256, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(256, 256, kernel_size=3, padding=1),#used kernel size one only
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # #========================================================
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # #========================================================
        # )
        # self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        # self.classifier = nn.Sequential(
            # nn.Dropout(),
            # nn.Linear(256 * 6 * 6, 4096),
            # nn.ReLU(inplace=True),
            # nn.Dropout(),
            # nn.Linear(4096, 4096),
            # nn.ReLU(inplace=True),
            # nn.Linear(4096, NUM_CLASSES),
        # )
        


    
    # def forward(self, x):
        # x = self.features(x)
        # x = self.avgpool(x)
        # x = torch.flatten(x, 1)
        # x = self.classifier(x)
        
        # return x
        
#######################################################################################################################  
# Testing on different classifiers
#######################################################################################################################   
# import torch
# import torch.nn as nn
# from torch.autograd import Variable
# import torch.nn.functional as F
# from torch import optim
# import numpy as np

# NUM_CLASSES = 21

# class SimpleClassifier(nn.Module):
    # def __init__(self):
        # super(SimpleClassifier, self).__init__()
        # self.conv1 = nn.Conv2d(3, 64, 5)
        # self.conv2 = nn.Conv2d(64, 32, 3)
        # self.conv3 = nn.Conv2d(32, 16, 3)
        # self.pool = nn.MaxPool2d(2, 2)
        # self.fc1 = nn.Linear(16 * 26 * 26, 120)
        # self.fc2 = nn.Linear(120, 84)
        # self.fc3 = nn.Linear(84, NUM_CLASSES)

    # def forward(self, x):
        # x = self.pool(F.relu(self.conv1(x)))
        # x = self.pool(F.relu(self.conv2(x)))
        # x = self.pool(F.relu(self.conv3(x)))
        # x = x.view(x.size()[0], 16 * 26 * 26)
        # x = F.relu(self.fc1(x))
        # x = F.relu(self.fc2(x))
        # x = self.fc3(x)
        # return x

    
        
        
        
        
        
# class Classifier(nn.Module):
    # TODO: implement me
    # def __init__(self):
        # super(Classifier, self).__init__()
        # self.features = nn.Sequential(
            # nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            # nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # nn.Conv2d(64, 192, kernel_size=5, padding=2),
            # nn.BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.MaxPool2d(kernel_size=3, stride=2),
            # nn.Conv2d(192, 384, kernel_size=3, padding=1),
            # nn.BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(384, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.Conv2d(256, 256, kernel_size=3, padding=1),
            # nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False),
            # nn.ReLU(inplace=True),
            # nn.MaxPool2d(kernel_size=3, stride=2),
        # )
        # self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        # self.classifier = nn.Sequential(
            # nn.Dropout(),
            # nn.Linear(256 * 6 * 6, 4096),
            # nn.ReLU(inplace=True),
            # nn.Dropout(),
            # nn.Linear(4096, 4096),
            # nn.ReLU(inplace=True),
            # nn.Linear(4096, NUM_CLASSES),
        # )
        


    
    # def forward(self, x):
        # x = self.features(x)
        # x = self.avgpool(x)
        # x = torch.flatten(x, 1)
        # x = self.classifier(x)
        
        # return x