Skip to content

Commit

Permalink
style: format code with black, yapf, autopep8 and isort
Browse files Browse the repository at this point in the history
Format code with black, yapf, autopep8 and isort

This commit fixes the style issues introduced in ad48ab7 according to the output
from Black, Yapf, Autopep8 and isort.

Details: None
  • Loading branch information
deepsource-autofix[bot] authored Aug 11, 2023
1 parent ad48ab7 commit 714169c
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 81 deletions.
16 changes: 8 additions & 8 deletions ML/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os
import random
import threading
from typing import *
import time
from typing import *

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -14,13 +15,17 @@
from PIL import Image
from sklearn.model_selection import train_test_split
from torch import nn, optim
from torch.nn import *
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from torchvision.models import *
from tqdm import tqdm
from wandb import AlertLevel
from torch.nn import *
from torchvision.models import *

from ML.data_loader import *
from ML.helper import *
from ML.helper.metrics import *
from ML.modelling import *

os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
# os.environ["WANDB_SILENT"] = "true"
Expand All @@ -33,8 +38,3 @@
torch.manual_seed(42)
np.random.seed(42)
torch.cuda.manual_seed(42)

from ML.data_loader import *
from ML.helper import *
from ML.helper.metrics import *
from ML.modelling import *
2 changes: 1 addition & 1 deletion ML/data_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from ML.data_loader.train import *
from ML.data_loader.test import *
from ML.data_loader.train import *
1 change: 1 addition & 0 deletions ML/data_loader/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class TestDataset(Dataset):

def __init__(self, path):
self.data = pd.read_csv(path)

Expand Down
12 changes: 8 additions & 4 deletions ML/data_loader/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class TrainDataset(Dataset):

def __init__(
self,
path: str,
Expand All @@ -18,7 +19,11 @@ def __init__(
self.labels = np.array(self.data[label_col].tolist())
self.images = self.data.drop(label_col, inplace=False, axis=1)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
self.images, self.labels, shuffle=True, random_state=seed, train_size=(1 - test_split)
self.images,
self.labels,
shuffle=True,
random_state=seed,
train_size=(1 - test_split),
)
if train:
self.images = self.X_train
Expand All @@ -29,9 +34,8 @@ def __init__(
self.transforms = transforms

def __getitem__(self, index) -> Tuple[torch.tensor, int]:
self.image = (
np.array(self.images.iloc[index].values.tolist()).astype(np.uint8).reshape(28, 28, 1)
)
self.image = (np.array(self.images.iloc[index].values.tolist()).astype(
np.uint8).reshape(28, 28, 1))
self.image = self.transforms(self.image)
return (
self.image,
Expand Down
4 changes: 2 additions & 2 deletions ML/helper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ML.helper.alert import *
from ML.helper.load_data import *
from ML.helper.metrics import *
from ML.helper.normalizer import *
from ML.helper.trainer import *
from ML.helper.alert import *
from ML.helper.load_data import *
1 change: 1 addition & 0 deletions ML/helper/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class Alert:

def __init__(self, results, past_results):
self.results = results
self.past_results = past_results
Expand Down
3 changes: 2 additions & 1 deletion ML/helper/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ def load_data(train: list, test: list, valid: list):
num_workers=round(os.cpu_count() / 2),
pin_memory=True,
)
return (train_dataset, test_dataset, val_dataset, train_dl, valid_dl, test_dl)
return (train_dataset, test_dataset, val_dataset, train_dl, valid_dl,
test_dl)
20 changes: 10 additions & 10 deletions ML/helper/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class Metrics:
def __init__(self, criterion: nn.Module, classes: list, dataloader, model, device):

def __init__(self, criterion: nn.Module, classes: list, dataloader, model,
device):
self.device = device
self.criterion = criterion
self.classes = torch.tensor(classes).to(device=self.device)
Expand All @@ -20,25 +22,23 @@ def accuracy(self):
tot = 0
cor = 0
for X, y in self.dataloader:
preds = torch.argmax(
torch.softmax(self.model(X.to(self.device)), dim=1), dim=1
)
preds = torch.argmax(torch.softmax(self.model(X.to(self.device)),
dim=1),
dim=1)

for pred, tr in zip(preds, y):
if pred == tr:
cor += 1
tot += 1
return (cor / tot) * 100

def precision(
self,
):
def precision(self, ):
tp = 0
fp = 0
for X, y in self.dataloader:
preds = torch.argmax(
torch.softmax(self.model(X.to(self.device)), dim=1), dim=1
)
preds = torch.argmax(torch.softmax(self.model(X.to(self.device)),
dim=1),
dim=1)
for clz in self.classes:
for pred, tr in zip(preds.to(self.device), y.to(self.device)):
if pred == clz and tr == pred:
Expand Down
6 changes: 3 additions & 3 deletions ML/helper/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class Normalizer:

def __init__(self, path, label_col):
self.data = pd.read_csv(path).drop(label_col, axis=1)
self.no = len(self.data)
Expand All @@ -17,9 +18,8 @@ def create_long_list(self):
self.tot_imgs = []
for i in range(self.no):
self.tot_imgs.append(np.array(self.data.iloc[i].tolist()) / 255)
self.tot_imgs = (
torch.tensor(self.tot_imgs).squeeze().view(self.no * 784).float()
)
self.tot_imgs = (torch.tensor(self.tot_imgs).squeeze().view(
self.no * 784).float())
return self.tot_imgs

def mean(self) -> float:
Expand Down
23 changes: 14 additions & 9 deletions ML/helper/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class Training:

def __init__(
self,
model: nn.Module,
Expand Down Expand Up @@ -33,7 +34,8 @@ def __init__(
self.project_name = project_name
self.device = device
self.num_classes = len(classes)
self.train_metrics = Metrics(criterion, classes, train_dl, model, device)
self.train_metrics = Metrics(criterion, classes, train_dl, model,
device)
self.test_metrics = Metrics(criterion, classes, test_dl, model, device)
self.valid_ds = valid_ds
self.config = config
Expand All @@ -43,7 +45,9 @@ def __init__(
def train(self, run_name):
torch.cuda.empty_cache()
torchinfo.summary(self.model)
wandb.init(project=self.project_name, name=run_name, config=self.config)
wandb.init(project=self.project_name,
name=run_name,
config=self.config)
wandb.watch(self.model, log="all")
iterater = tqdm(range(self.epochs))
for _ in iterater:
Expand Down Expand Up @@ -84,9 +88,7 @@ def train_step(self, X_batch, y_batch) -> Tuple[int, torch.Tensor]:
self.optimizer.step()
return self.loss.item(), logits

def test(
self,
):
def test(self, ):
self.model.eval()
with torch.no_grad():
results = {}
Expand All @@ -106,7 +108,9 @@ def make_predictions(self, run_name=None):
i = i + 1
pred = torch.argmax(
torch.softmax(
self.model(self.resize(image.view(1, 1, 28, 28).to(device).float())),
self.model(
self.resize(
image.view(1, 1, 28, 28).to(device).float())),
dim=1,
),
dim=1,
Expand All @@ -115,9 +119,10 @@ def make_predictions(self, run_name=None):
threading.Thread(target=ids.append, args=[i]).start()
print(pd.DataFrame({"ImageId": ids, "Label": preds}))
if run_name:
pd.DataFrame({"ImageId": ids, "Label": preds}).to_csv(
f"ML/predictions/{run_name}.csv", index=False
)
pd.DataFrame({
"ImageId": ids,
"Label": preds
}).to_csv(f"ML/predictions/{run_name}.csv", index=False)
return ids, preds

def plot_predictions(self, run_name):
Expand Down
25 changes: 11 additions & 14 deletions ML/modelling/cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class CNNModel(nn.Module):

def __init__(
self,
in_channels: int,
Expand All @@ -18,39 +19,35 @@ def __init__(
):
super().__init__()
self.convblo1 = nn.Sequential(
nn.Conv2d(in_channels, hidden_units_cnn,
kernel_size, stride, padding),
nn.Conv2d(in_channels, hidden_units_cnn, kernel_size, stride,
padding),
pool_type(pool),
nn.BatchNorm2d(hidden_units_cnn, affine=affine),
activation(),
)
self.convblo2 = nn.Sequential(
nn.Conv2d(
hidden_units_cnn, hidden_units_cnn * 2, kernel_size, stride, padding
),
nn.Conv2d(hidden_units_cnn, hidden_units_cnn * 2, kernel_size,
stride, padding),
pool_type(pool),
nn.BatchNorm2d(hidden_units_cnn * 2, affine=affine),
activation(),
)
self.convblo3 = nn.Sequential(
nn.Conv2d(
hidden_units_cnn * 2, hidden_units_cnn * 3, kernel_size, stride, padding
),
nn.Conv2d(hidden_units_cnn * 2, hidden_units_cnn * 3, kernel_size,
stride, padding),
pool_type(pool),
activation(),
)
self.convblo4 = nn.Sequential(
nn.Conv2d(
hidden_units_cnn * 3, hidden_units_cnn * 4, kernel_size, stride, padding
),
nn.Conv2d(hidden_units_cnn * 3, hidden_units_cnn * 4, kernel_size,
stride, padding),
pool_type(pool),
nn.BatchNorm2d(hidden_units_cnn * 4, affine=affine),
activation(),
)
self.convblo5 = nn.Sequential(
nn.Conv2d(
hidden_units_cnn * 4, hidden_units_cnn * 5, kernel_size, stride, padding
),
nn.Conv2d(hidden_units_cnn * 4, hidden_units_cnn * 5, kernel_size,
stride, padding),
pool_type(pool),
nn.BatchNorm2d(hidden_units_cnn * 5, affine=affine),
activation(),
Expand Down
7 changes: 6 additions & 1 deletion ML/modelling/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@


class LinearModel(nn.Module):

def __init__(
self, in_size: int = 784, hidden_unis: int = 64, out_size: int = 10, affine: bool = False
self,
in_size: int = 784,
hidden_unis: int = 64,
out_size: int = 10,
affine: bool = False,
) -> None:
super().__init__()
self.linblo1 = nn.Sequential(
Expand Down
57 changes: 29 additions & 28 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

# Loading Data
n = Normalizer(path="ML/data/train.csv", label_col="label")
train_transform = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
# transforms.ColorJitter(0.125, 0.125, 0.125, 0.125),
# transforms.Grayscale(1),
# transforms.RandomHorizontalFlip(),
# transforms.RandomVerticalFlip(),
# transforms.GaussianBlur(5, (0.1, 1)),
transforms.ToTensor(),
transforms.Normalize(mean=(n.mean()), std=(n.std())),
# transforms.Normalize(
# mean=[np.mean([0.485, 0.456, 0.406])], std=[np.mean([0.229, 0.224, 0.225])]
# ),
]
)
test_transform = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
]
)
train_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
# transforms.ColorJitter(0.125, 0.125, 0.125, 0.125),
# transforms.Grayscale(1),
# transforms.RandomHorizontalFlip(),
# transforms.RandomVerticalFlip(),
# transforms.GaussianBlur(5, (0.1, 1)),
transforms.ToTensor(),
transforms.Normalize(mean=(n.mean()), std=(n.std())),
# transforms.Normalize(
# mean=[np.mean([0.485, 0.456, 0.406])], std=[np.mean([0.229, 0.224, 0.225])]
# ),
])
test_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
])
# train_transform = torchvision.models.ViT_B_16_Weights.DEFAULT.transforms()
# test_transform = torchvision.models.ViT_B_16_Weights.DEFAULT.transforms()
train_path = "ML/data/train.csv"
Expand All @@ -43,13 +39,18 @@
BATCH_SIZE,
]
val = [valid_path, 1]
train_dataset, test_dataset, val_dataset, train_dl, valid_dl, test_dl = load_data(train, test, val)
train_dataset, test_dataset, val_dataset, train_dl, valid_dl, test_dl = load_data(
train, test, val)
class_names = train_dataset.classes()
# Creating Model
model = efficientnet_v2_s(torchvision.models.EfficientNet_V2_S_Weights.DEFAULT).to(device)
model.features[0][0] = Conv2d(
1, 24, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False
)
model = efficientnet_v2_s(
torchvision.models.EfficientNet_V2_S_Weights.DEFAULT).to(device)
model.features[0][0] = Conv2d(1,
24,
kernel_size=(3, 3),
stride=(2, 2),
padding=(1, 1),
bias=False)
model.classifier[1] = Linear(1280, len(class_names), bias=True)
model = torch.compile(model, dynamic=True, mode="max-autotune", disable=True)
criterion = nn.CrossEntropyLoss().to(device)
Expand Down

0 comments on commit 714169c

Please sign in to comment.