text_plain_output_1.png
from time import time

from torch.utils.data import Dataset

import cv2

import numpy as np

import pandas as pd

import torch

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from torch import nn, optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader as DL
from torch.nn.utils import weight_norm as WN
import torch.nn.functional as F
import gc
import os
import cv2
from time import time
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed = 42

def breaker():
    pass
def head(x, no_of_ele=5):
    pass
def getImages(file_path=None, file_names=None, size=None):
    images = []
    for name in file_names:
        try:
            image = cv2.imread(file_path + name + '.jpg', cv2.IMREAD_GRAYSCALE).astype('float64')
        except AttributeError:
        if size:
            image = cv2.resize(image, dsize=(size, size), interpolation=cv2.INTER_LANCZOS4)
        cv2.normalize(src=image, dst=image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
        images.append(image.reshape(1, size, size))
    return np.array(images)

start_time = time()
ss = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
ts_img_names = ss['StudyInstanceUID'].values
ts_images = getImages('../input/ranzcr-clip-catheter-line-classification/test/', ts_img_names, size=144)
breaker()
breaker()

class Dataset(Dataset):
    def __init__(this, X=None, y=None, mode='train'):
        this.mode = mode
        this.X = X
        if mode == 'train':
            this.y = y
    def __len__(this):
        return this.X.shape[0]
    def __getitem__(this, idx):
        if this.mode == 'train':
            return (torch.FloatTensor(this.X[idx]), torch.FloatTensor(this.y[idx]))
        else:
            return torch.FloatTensor(this.X[idx])

class CFG:
    tr_batch_size = 128
    ts_batch_size = 128
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    in_channels = 1
    OL = 11
    def __init__(this, filter_sizes=[64, 128, 256, 512], HL=[4096, 4096], epochs=50, n_folds=5):
        this.filter_sizes = filter_sizes
        this.HL = HL
        this.epochs = epochs
        this.n_folds = n_folds

def predict_(model=None, dataloader=None, device=None, path=None):
    if path:
        model.load_state_dict(torch.load(path))
    else:
        pass
    model.to(device)
    model.eval()
    y_pred = torch.zeros(1, 11).to(device)
    for X in dataloader:
        X = X.to(device)
        with torch.no_grad():
            Pred = torch.sigmoid(model(X))
        y_pred = torch.cat((y_pred, Pred), dim=0)
    return y_pred[1:].detach().cpu().numpy()

cfg = CFG(filter_sizes=[64, 128, 256, 512], HL=[4096, 4096], epochs=50, n_folds=5)
ts_data_setup = Dataset(ts_images, None, 'test')
ts_data = DL(ts_data_setup, batch_size=cfg.ts_batch_size, shuffle=False)
model = CNN(in_channels=cfg.in_channels, filter_sizes=cfg.filter_sizes, HL=cfg.HL, OL=cfg.OL)
y_pred_e19 = predict_(model=model, dataloader=ts_data, device=cfg.device, path='../input/rccl-1x144-f-train/Epoch_19.pt')
y_pred_e20 = predict_(model=model, dataloader=ts_data, device=cfg.device, path='../input/rccl-1x144-f-train/Epoch_20.pt')
y_pred_e23 = predict_(model=model, dataloader=ts_data, device=cfg.device, path='../input/rccl-1x144-f-train/Epoch_23.pt')
y_pred_e25 = predict_(model=model, dataloader=ts_data, device=cfg.device, path='../input/rccl-1x144-f-train/Epoch_25.pt')
y_pred = (y_pred_e19 + y_pred_e20 + y_pred_e23 + y_pred_e25) / 4
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
ss.iloc[:, 1:] = y_pred
ss.to_csv('./submission.csv', index=False)
ss.head(5)