Spaces:
Sleeping
Sleeping
File size: 5,641 Bytes
72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e ef68ae3 72eeb6e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
from torchvision.datasets.cifar import CIFAR10
import torch
from torch.optim.lr_scheduler import OneCycleLR
from torch_lr_finder import LRFinder
import torch.nn as nn
import torch.nn.functional as F
import os
import pytorch_lightning as pl
import albumentations as A
from torchvision.datasets import CIFAR10
from torchvision import transforms
from albumentations.pytorch.transforms import ToTensorV2
import numpy as np
from pytorch_lightning import LightningModule, Trainer
from torch.utils.data import DataLoader, random_split
from torchmetrics import Accuracy
from torchvision import transforms
PATH_DATASETS = os.environ.get("PATH_DATASETS", ".")
class CustomResNet(LightningModule):
def __init__(self, num_classes=10, data_dir=PATH_DATASETS, hidden_size=16, learning_rate=0.05):
super(CustomResNet, self).__init__()
self.save_hyperparameters()
#self.custom_block = CustomBlock(in_channels=64, out_channels=128)
# Set our init args as class attributes
# loading the dataset
self.EPOCHS = 24
self.num_classes=num_classes
self.data_dir = data_dir
self.hidden_size = hidden_size
self.learning_rate = learning_rate
self.prep_layer = nn.Sequential(
nn.Conv2d(
in_channels=3,
out_channels=64,
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
nn.BatchNorm2d(64),
nn.ReLU(),
)
self.layer_1 = nn.Sequential(
nn.Conv2d(
in_channels=64,
out_channels=128,
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
nn.MaxPool2d(kernel_size=2),
nn.BatchNorm2d(128),
nn.ReLU(),
)
self.resblock1 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, stride=1, bias=False, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, stride=1, bias=False, padding=1),
nn.BatchNorm2d(128),
nn.ReLU()
)
self.layer_2 = nn.Sequential(
nn.Conv2d(
in_channels=128,
out_channels=256,
kernel_size=3,
stride=1,
padding=1,
bias=False,
),
nn.MaxPool2d(kernel_size=2),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.layer_3 = nn.Sequential(
nn.Conv2d(256, 512, kernel_size=3, stride=1, bias=False, padding=1),
nn.MaxPool2d(kernel_size = 2, stride = 2),
nn.BatchNorm2d(512),
nn.ReLU()
)
self.resblock2 = nn.Sequential(
nn.Conv2d(512, 512, kernel_size=3, stride=1, bias=False, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, bias=False, padding=1),
nn.BatchNorm2d(512),
nn.ReLU()
)
self.maxpoollayer = nn.Sequential(nn.MaxPool2d(kernel_size=4,stride = 4))
self.fclayer = nn.Linear(512, self.num_classes)
# def loss_function(self, pred, target):
# criterion = torch.nn.CrossEntropyLoss()
# return criterion(pred, target)
def forward(self, x):
x = self.prep_layer(x)
x = self.layer_1(x)
r1 = self.resblock1(x)
x = x + r1
x = self.layer_2(x)
x = self.layer_3(x)
r2 = self.resblock2(x)
x = x + r2
x = self.maxpoollayer(x)
x = x.view((x.shape[0],-1))
x = self.fclayer(x)
return F.log_softmax(x,dim=-1)
# def get_loss_accuracy(self, batch):
# images, labels = batch
# predictions = self(images)
# predicted_classes = torch.argmax(predictions, dim=1)
# accuracy = (predicted_classes == labels).float().mean()
# loss = self.loss_function(predictions, labels)
# return loss, accuracy * 100
# def training_step(self, batch, batch_idx):
# loss, accuracy = self.get_loss_accuracy(batch)
# self.log("loss/train", loss, on_epoch=True, prog_bar=True, logger=True)
# self.log("acc/train", accuracy, on_epoch=True, prog_bar=True, logger=True)
# return loss
# def validation_step(self, batch, batch_idx):
# loss, accuracy = self.get_loss_accuracy(batch)
# self.log("loss/val", loss, on_epoch=True, prog_bar=True, logger=True)
# self.log("acc/val", accuracy, on_epoch=True, prog_bar=True, logger=True)
# return loss
# def test_step(self, batch, batch_idx):
# loss = self.validation_step(batch, batch_idx)
# return loss
# def configure_optimizers(self):
# LEARNING_RATE=0.03
# WEIGHT_DECAY=0
# optimizer = torch.optim.Adam(self.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
# self.trainer.fit_loop.setup_data()
# dataloader = self.trainer.train_dataloader
# lr_scheduler = OneCycleLR(
# optimizer,
# max_lr=4.79E-02,
# steps_per_epoch=len(dataloader),
# epochs=24,
# pct_start=5/24,
# div_factor=100,
# three_phase=False,
# final_div_factor=100,
# anneal_strategy='linear'
# )
# scheduler = {"scheduler": lr_scheduler, "interval" : "step"}
# return [optimizer], [scheduler] |