|
import os
|
|
import numpy as np
|
|
from PIL import Image
|
|
from collections import Counter
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.optim as optim
|
|
from torch.utils.data import Dataset, DataLoader
|
|
|
|
import torchvision.transforms as transforms
|
|
|
|
|
|
import timm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transform_small = transforms.Compose([
|
|
transforms.Resize((50, 50)),
|
|
transforms.ToTensor(),
|
|
transforms.Normalize(mean=[0.5]*3, std=[0.5]*3)
|
|
])
|
|
|
|
|
|
transform_large = transforms.Compose([
|
|
transforms.Resize((224, 224)),
|
|
transforms.ToTensor(),
|
|
transforms.Normalize(mean=[0.5]*3, std=[0.5]*3)
|
|
])
|
|
|
|
|
|
|
|
class ModelA(nn.Module):
|
|
def __init__(self, num_classes=2):
|
|
super(ModelA, self).__init__()
|
|
|
|
self.block1 = nn.Sequential(
|
|
nn.Conv2d(3, 32, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.BatchNorm2d(32),
|
|
nn.Conv2d(32, 32, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(kernel_size=2),
|
|
nn.Dropout(0.3)
|
|
)
|
|
self.block2 = nn.Sequential(
|
|
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.BatchNorm2d(64),
|
|
nn.Conv2d(64, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(kernel_size=2),
|
|
nn.Dropout(0.3)
|
|
)
|
|
self.block3 = nn.Sequential(
|
|
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.BatchNorm2d(128),
|
|
nn.Conv2d(128, 128, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(kernel_size=2),
|
|
nn.Dropout(0.3)
|
|
)
|
|
|
|
self.classifier = nn.Sequential(
|
|
nn.Flatten(),
|
|
nn.Linear(128 * 6 * 6, 512),
|
|
nn.ReLU(),
|
|
nn.Dropout(0.3),
|
|
nn.Linear(512, num_classes)
|
|
)
|
|
|
|
def forward(self, x):
|
|
x = self.block1(x)
|
|
x = self.block2(x)
|
|
x = self.block3(x)
|
|
x = self.classifier(x)
|
|
return x
|
|
|
|
|
|
|
|
|
|
class ModelB(nn.Module):
|
|
def __init__(self, num_classes=2):
|
|
super(ModelB, self).__init__()
|
|
|
|
self.features = nn.Sequential(
|
|
nn.Conv2d(3, 32, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2),
|
|
nn.Dropout(0.3),
|
|
|
|
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2),
|
|
nn.Dropout(0.3),
|
|
|
|
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2),
|
|
nn.Dropout(0.3)
|
|
)
|
|
self.classifier = nn.Sequential(
|
|
nn.Flatten(),
|
|
nn.Linear(128 * 6 * 6, 512),
|
|
nn.ReLU(),
|
|
nn.Dropout(0.3),
|
|
nn.Linear(512, num_classes)
|
|
)
|
|
|
|
def forward(self, x):
|
|
x = self.features(x)
|
|
x = self.classifier(x)
|
|
return x
|
|
|
|
|
|
|
|
class ModelC(nn.Module):
|
|
def __init__(self, num_classes=2):
|
|
super(ModelC, self).__init__()
|
|
|
|
|
|
self.cnn_feature_extractor = nn.Sequential(
|
|
nn.Conv2d(3, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2),
|
|
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2),
|
|
nn.Conv2d(128, 256, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.MaxPool2d(2)
|
|
)
|
|
|
|
|
|
|
|
self.vit = timm.create_model('vit_base_patch16_224', pretrained=True)
|
|
|
|
in_features = self.vit.head.in_features
|
|
self.vit.head = nn.Linear(in_features, num_classes)
|
|
|
|
def forward(self, x):
|
|
|
|
features = self.cnn_feature_extractor(x)
|
|
|
|
|
|
out = self.vit(x)
|
|
return out
|
|
|