Skip to content
Open

test #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions src/subpixel/experimental/cpcv2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import torch
import numpy as np
import torch.nn as nn
import os
import time
import random
import torchvision.transforms as ttf
from torch.utils.data import Dataset, DataLoader, random_split
import cv2
import matplotlib.pylab as plt
import warnings

warnings.filterwarnings("ignore")

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"


def get_grids(image, grid_size, overlap):

try:
h, w, _ = image.shape
except:
h, w = image.shape

try:
h_grid, w_grid = grid_size
except:
h_grid, w_grid = grid_size, grid_size

h_steps = (h - h_grid) // (h_grid * (1 - overlap)) + 1
w_steps = w / w_grid

grids = []

for i in range(int(h_steps)):
for j in range(int(w_steps)):

if j == 0:
w_start = 0
else:
w_start = w_grid * (j - overlap)
w_start = int(np.round(w_start))

grid = image[i * h_grid : (i + 1) * h_grid, w_start : w_start + w_grid, :]
grids.append(grid)

return grids


def read_image(filename, resize=False):

image = cv2.imread(filename)

try:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
except:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

if resize:
image = cv2.resize(image, resize)

return image


def display_images(images, nrows=4, ncols=3, cmap=None, title=None):

fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(10, 10))
if title:
fig.suptitle(title, fontsize=20)
c = 0
for i in range(ncols):
for j in range(nrows):
ax[j][i].imshow(images[c], cmap=cmap)
ax[j][i].axis("off")
c += 1
plt.show()


class CPC_Dataset(Dataset):
def __init__(self, path, grid_size, overlap, transform=None):

self.path = path
self.transform = transform
self.images = os.listdir(path)
self.images.sort()
self.grid_size, self.overlap = grid_size, overlap

def __len__(self):
return len(self.images)

def __getitem__(self, idx):

img_path = os.path.join(self.path, self.images[idx])
img = read_image(img_path)
grids = get_grids(img, self.grid_size, self.overlap)

if self.transform:
for i, grid in enumerate(grids):
grids[i] = self.transform(grid)


class BasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=(2, 2)):
super(BasicBlock, self).__init__()

self.stride = stride

self.conv1 = nn.Conv2d(in_channels, out_channels, (3, 3), stride, (1, 1))
self.bn = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()

self.conv2 = nn.Conv2d(out_channels, out_channels, (3, 3), (1, 1), (1, 1))

self.up = nn.Conv2d(in_channels, out_channels, (1, 1), (2, 2))

def forward(self, x):

x_ = self.relu(self.bn(self.conv1(x)))
x_ = self.bn(self.conv2(x_))

if self.stride == (2, 2):
x = self.bn(self.up(x))

return x_ + x


class Resnet18(nn.Module):
def __init__(self):
super(Resnet18, self).__init__()

self.conv1 = nn.Conv2d(3, 32, (7, 7), (2, 2), (3, 3), bias=False)
self.bn = nn.BatchNorm2d(32)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(3, 2, 1, 1)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))

self.layer1 = nn.ModuleList(
[BasicBlock(32, 32, stride=(1, 1)), BasicBlock(32, 32, stride=(1, 1))]
)
self.layer2 = nn.ModuleList(
[BasicBlock(32, 64), BasicBlock(64, 64, stride=(1, 1))]
)
self.layer3 = nn.ModuleList(
[BasicBlock(64, 128), BasicBlock(128, 128, stride=(1, 1))]
)
self.layer4 = nn.ModuleList(
[BasicBlock(128, 256), BasicBlock(256, 256, stride=(1, 1))]
)

def forward(self, x):

x = self.maxpool(self.relu(self.bn(self.conv1(x))))

for layer in self.layer1:
x = layer(x)

for layer in self.layer2:
x = layer(x)

for layer in self.layer3:
x = layer(x)

for layer in self.layer4:
x = layer(x)

x = self.avgpool(x)
return x


# class CPC_Model(nn.Module):
# def __init__(self):
# super().__init__()

# self.model = Resnet18()

# try:
# self.model = torch.load(r"../input/mri-scan/Encoder_2").to(DEVICE)
# except:
# pass

# self.net = nn.Sequential(
# nn.Conv2d(256, 128, 1, 1),
# nn.Conv2d(128, 128, 1, 1),
# nn.Conv2d(128, 256, 1, 1),
# )

# def forward(self, crops):

# embedding = self.model(crops[0].to(DEVICE))
# for crop in crops[1:]:
# emb = self.model(crop.to(DEVICE))
# embedding = torch.cat([embedding, emb], dim=0)

# context = embedding.reshape((1, 256, 6, 6))

# if np.random.rand(1)[0] > 0.5:
# if np.random.rand(1)[0] > 0.5:
# top_half = context[:, :, :3, :]
# bottom_half = context[:, :, 3:, :]

# return self.net(top_half)

# else:
# bottom_half = context[:, :, 3:, :]
# top_half = context[:, :, :3, :]

# return self.net(bottom_half)
# else:
# if np.random.rand(1)[0] > 0.5:
# right_half = context[:, :, :, 3:]
# left_half = context[:, :, :, :3]

# return self.net(right_half)

# else:
# left_half = context[:, :, :, :3]
# right_half = context[:, :, :, 3:]

# return self.net(left_half)
33 changes: 33 additions & 0 deletions src/subpixel/experimental/gradcam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from torchcam.methods import SmoothGradCAMpp
import cv2
import torch
from torchvision.transforms.functional import normalize
import numpy as np


def get_activationMap(model, image, device='cpu'):

cam_extractor = SmoothGradCAMpp(model)

if isinstance(image, str):
image = cv2.cvtColor(cv2.imread(image), cv2.COLOR_BGR2RGB)
image = torch.tensor(image).permute(2, 0, 1).float()
image = normalize(image / 255., [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

out = model(image.unsqueeze(0).to(device))

return cam_extractor(out.squeeze(0).argmax().item(), out)


def get_bbox(activation_maps, image_shape= None, threshold=0.5):

if image_shape:
activation_maps = cv2.resize(activation_maps, image_shape)

activation_map = activation_maps[0]
activation_map = activation_map > threshold
contours, _ = cv2.findContours(activation_map.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

bbox = cv2.boundingRect(contours[0])

return bbox
51 changes: 51 additions & 0 deletions src/subpixel/ml/EDA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from utils import *


class EDA:
def __init__(self, df, target_col=None):
self.df = df
self.target_col = target_col

def show_corrMatrix(self):
return correlation_matrix(self.df)

def get_importantFeatures(self):

if self.target_col:
imp_features = feature_importance(self.df, self.target_col)
return imp_features
else:
raise Exception("Target column not specified.")

def deal_withNaN(self, method="mean"):
if method == "mean":
return fill_nan_with_mean(self.df)
elif method == "delete":
return delete_row_with_nan(self.df)
else:
raise Exception("Method not supported.")

def check_and_deal_wtihOutliers(self):

cols_with_outliers = check_for_outliers(self.df)
if cols_with_outliers:
_, df = find_outliers(self.df, cols=cols_with_outliers, remove=True)
return df
else:
return None

def data_stats(self):
return get_statistics(self.df)

def show_chart(self, df, col, chart="pie"):

if chart == "pie":
pie_chart(self.df, col)
elif chart == "count":
count_plot(self.df, col)
elif chart == "hist":
histogram(self.df, col)
elif chart == "box":
boxplot(self.df, col)
else:
raise Exception("Chart not supported.")
Empty file added src/subpixel/ml/display.py
Empty file.
28 changes: 28 additions & 0 deletions src/subpixel/ml/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import torch
import torch.nn as nn
from tab_transformer_pytorch import TabTransformer

# https://github.com/lucidrains/tab-transformer-pytorch


cont_mean_std = torch.randn(10, 2)

## EXPERMIENTAL ##
# If normal ML models don't give good results and if the dataset is big enogh to use TabTransformer.
# Use exisiting train function or make a new train function to train TabTransformer on the custom dataset.
# Try to maybe find methods to find the best parameters for TabTransformer for the given task.


model = TabTransformer(
categories=(10, 5, 6, 5, 8), # tuple containing the number of unique values within each category
num_continuous=10, # number of continuous values
dim=32, # dimension, paper set at 32
dim_out=1, # binary prediction, but could be anything
depth=6, # depth, paper recommended 6
heads=8, # heads, paper recommends 8
attn_dropout=0.1, # post-attention dropout
ff_dropout=0.1, # feed forward dropout
mlp_hidden_mults=(4, 2), # relative multiples of each hidden dimension of the last mlp to logits
mlp_act=nn.ReLU(), # activation for final mlp, defaults to relu, but could be anything else (selu etc)
continuous_mean_std=cont_mean_std, # (optional) - normalize the continuous values before layer norm
)
Loading