상세 컨텐츠

본문 제목

[코드] vgg 전체 학습 코드 정리

SKT fly ai challenger

by jii 2025. 1. 23. 14:10

본문

main

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# from vgg16_full import *
from resnet50_skeleton import *

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu')

# Image Preprocessing
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

# CIFAR-10 Dataset
train_dataset = torchvision.datasets.CIFAR10(root='C:/Users/82102/osproj/data',
                                             train=True,
                                             transform=transform_train,
                                             download=False) # Change Download-flag "True" at the first excution.

test_dataset = torchvision.datasets.CIFAR10(root='C:/Users/82102/osproj/data',
                                            train=False,
                                            transform=transform_test)


# data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=100,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=100,
                                          shuffle=False)
###########################################################
# Choose model
model = ResNet50_layer4().to(device)
PATH = './resnet50_epoch285.ckpt' # test acc would be almost 80

# model = vgg16().to(device)
# PATH = './vgg16_epoch250.ckpt'  # test acc would be almost 85
##############################################################
checkpoint = torch.load(PATH, map_location=torch.device('cpu'))
#model.load_state_dict(checkpoint)

# Train Model
# Hyper-parameters
num_epochs = 1  # students should train 1 epoch because they will use cpu
learning_rate = 0.001

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# For updating learning rate
def update_lr(optimizer, lr):
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

# Train the model
total_step = len(train_loader)
current_lr = learning_rate

for epoch in range(num_epochs):

    model.train()
    train_loss = 0

    for batch_index, (images, labels) in enumerate(train_loader):
        # print(images.shape)
        images = images.to(device)  # "images" = "inputs"
        labels = labels.to(device)  # "labels" = "targets"

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)

        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        train_loss += loss.item()

        if (batch_index + 1) % 100 == 0:
            print("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}"
                  .format(epoch + 1, num_epochs, batch_index + 1, total_step, train_loss / (batch_index + 1)))

    # Decay learning rate
    if (epoch + 1) % 20 == 0:
        current_lr /= 3
        update_lr(optimizer, current_lr)
        torch.save(model.state_dict(), './resnet50_epoch' + str(epoch+1)+'.ckpt')

# Save the model checkpoint
torch.save(model.state_dict(), './resnet50_final.ckpt')

model.eval()
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
import torch.nn as nn

# 1x1 convolution
def conv1x1(in_channels, out_channels, stride, padding):
    model = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, padding=padding),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
    return model


# 3x3 convolution
def conv3x3(in_channels, out_channels, stride, padding):
    model = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=padding),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
    return model

###########################################################################
# Question 1 : Implement the "bottle neck building block" part.
# Hint : Think about difference between downsample True and False. How we make the difference by code?
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, middle_channels, out_channels, downsample=False):
        super(ResidualBlock, self).__init__()
        self.downsample = downsample

        if self.downsample:
            self.layer = nn.Sequential(
                conv1x1(in_channels, middle_channels, 2, 0),  # stride 2
                conv3x3(middle_channels, middle_channels, 1, 1),
                conv1x1(middle_channels, out_channels, 1, 0)
            )
            self.downsize = conv1x1(in_channels, out_channels, 2, 0)
        else:
            self.layer = nn.Sequential(
                conv1x1(in_channels, middle_channels, 1, 0),
                conv3x3(middle_channels, middle_channels, 1, 1),
                conv1x1(middle_channels, out_channels, 1, 0)
            )
            self.make_equal_channel = conv1x1(in_channels, out_channels, 1, 0)

    def forward(self, x):
        if self.downsample:
            out = self.layer(x)
            x = self.downsize(x)
            return out + x
        else:
            out = self.layer(x)
            if x.size() != out.size():
                x = self.make_equal_channel(x)
            return out + x

###########################################################################



###########################################################################
# Question 2 : Implement the "class, ResNet50_layer4" part.
# Understand ResNet architecture and fill in the blanks below. (25 points)
# (blank : #blank#, 1 points per blank )
# Implement the code.
class ResNet50_layer4(nn.Module):
    def __init__(self, num_classes=10):  # CIFAR-10
        super(ResNet50_layer4, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        )
        self.layer2 = nn.Sequential(
            ResidualBlock(64, 64, 256, downsample=False),
            ResidualBlock(256, 64, 256, downsample=False),
            ResidualBlock(256, 64, 256, downsample=True)
        )
        self.layer3 = nn.Sequential(
            ResidualBlock(256, 128, 512, downsample=False),
            ResidualBlock(512, 128, 512, downsample=False),
            ResidualBlock(512, 128, 512, downsample=False),
            ResidualBlock(512, 128, 512, downsample=True)
        )
        self.layer4 = nn.Sequential(
            ResidualBlock(512, 256, 1024, downsample=False),
            ResidualBlock(1024, 256, 1024, downsample=False),
            ResidualBlock(1024, 256, 1024, downsample=False),
            ResidualBlock(1024, 256, 1024, downsample=False),
            ResidualBlock(1024, 256, 1024, downsample=False),
            ResidualBlock(1024, 256, 1024, downsample=False)
        )

        self.avgpool = nn.AvgPool2d(2, 2)
        self.fc = nn.Linear(1024, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight.data)
            elif isinstance(m, nn.Conv2d):
                nn.init.xavier_uniform_(m.weight.data)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avgpool(out)
        out = out.view(out.size()[0], -1)
        out = self.fc(out)
        return out

###########################################################################
import torch.nn as nn
import math

###### VGG16 #############
class VGG(nn.Module):
    def __init__(self, features):
        super(VGG, self).__init__()
        self.features = features
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(512, 512),
            nn.BatchNorm1d(512),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(512, 10),
        )
         # Initialize weights
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
                m.bias.data.zero_()

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return nn.Sequential(*layers)

def vgg16():
    # cfg shows 'kernel size'
    # 'M' means 'max pooling'
    cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']
    return VGG(make_layers(cfg))

Image Filtering Techniques in Image Processing — Part 1 | by Henrique Vedoveli | Medium

 

Image Filtering Techniques in Image Processing — Part 1

1. Introduction

medium.com

https://www.mathworks.com/help/examples/images/win64/DenoiseColorImageUsingNonLocalMeansFilterExample_01.png

https://www.researchgate.net/publication/317796612/figure/fig1/AS:639916476211200@1529579385477/cameraman-image-see-Fig-61a-corrupted-by-left-first-Gaussian-noise-and-then.png


https://drive.google.com/file/d/1A6QnO7leQX3X59i8zXuViLuOWbPPZjMr/view?usp=sharing

 

OpenCV_8(최종).ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1Oozl8GWrF-Y3w9NIbvRo8oTj2464_mBi/view?usp=sharing

 

OpenCV_9.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1cQAnXExOrNp711ARYIJT08eV2GUzZDY3/view?usp=sharing

 

Gradient.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1Q9nuWhfePdW8cixaliJPuhqhy3y1cE7Y/view?usp=sharing

 

EdgeDetection.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1r2XNoaKIjiNdgkK_Xh8C248zQu90AKyx/view?usp=sharing

 

Pyramids.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1FZ-xfaafkPi6qXxbEYegaJaybnz9pRjg/view?usp=sharing

 

K-Means Clustering.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1e03QSyOSaGbBW8CeJkAvXvb4OHbdBP_O/view?usp=sharing

 

SVM_OCR.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1e03QSyOSaGbBW8CeJkAvXvb4OHbdBP_O/view?usp=sharing

 

SVM_OCR.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1KH5brKbACDocVpEIGGN9_-0ODo9NZNPI/view?usp=sharing

 

OpenCV_10_Tessaract.ipynb

Colab notebook

drive.google.com

https://drive.google.com/file/d/1YZjTqCYlgB6m7j8zwaYWAPOl4hoHjN3N/view?usp=sharing

 

OpenCV_face detection.ipynb

Colab notebook

drive.google.com

 

계속 opencv..

https://drive.google.com/file/d/1fnaIMZjLGQUK7xuqNl9ullS3nWHEIyuE/view?usp=drive_link

 

 

 

'SKT fly ai challenger' 카테고리의 다른 글

Transformer  (0) 2025.01.24
RNN  (0) 2025.01.23
0121  (0) 2025.01.21
0120  (0) 2025.01.20
0118  (0) 2025.01.18

관련글 더보기

댓글 영역