Kaggle competition: Titanic - Machine Learning from Disaster
sub1 = pd.read_csv('.. /input/resultsdriven-tabular-playground-series-201/submission - 2021-01-15T023916.124.csv')
 predictions1 = sub1['target'].tolist()
sub2 = pd.read_csv('.. /input/resultsdriventabularplaygroundseries2011/submission - 2021-01-16T012125.132.csv')
 predictions2 = sub2['target'].tolist()<define_variables>
gender_data = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
results = [y*1.0002 for x, y in zip(predictions1, predictions2)]<save_to_csv>
train_data = train_data.drop(['Ticket', 'Cabin'], axis = 1)
 test_data = test_data.drop(['Ticket', 'Cabin'], axis = 1 )
output = pd.DataFrame({"id":test_data.id, "target":results})
 output.to_csv('submission.csv', index=False )<install_modules>
df = [train_data, test_data]
for row in df:
row['Title'] = row.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_data['Title'], train_data['Sex'] )
!pip install -q /kaggle/input/timm-0-1-30/timm-0.1.30-py3-none-any.whl
!pip install -q /kaggle/input/omegaconf/omegaconf-2.0.2-py3-none-any.whl
!cp /kaggle/input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2<feature_engineering>
for row in df:
row['Title'] = row['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 row['Title'] = row['Title'].replace('Mlle', 'Miss')
 row['Title'] = row['Title'].replace('Ms', 'Miss')
 row['Title'] = row['Title'].replace('Mme', 'Mrs')
 train_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
def check_consistency(sub, test):

 for i in test['StudyInstanceUID'].unique() :
df_tmp = sub.loc[sub.id.str.contains(i, regex = False)].reset_index(drop = True)
 df_tmp['StudyInstanceUID'] = df_tmp['id'].str.split('_' ).str[0]
df_tmp['label_type'] = df_tmp['id'].str.split('_' ).str[1:].apply(lambda x: '_'.join(x))
del df_tmp['id']
if i == test['StudyInstanceUID'].unique() [0]:
df = df_tmp.copy()
else:
df = pd.concat([df, df_tmp], axis = 0)
 df_exam = df.pivot(index = 'StudyInstanceUID', columns = 'label_type', values = 'label')
 df_image = sub.loc[sub.id.isin(test.SOPInstanceUID)].reset_index(drop = True)
 df_image = df_image.merge(test, how = 'left', left_on = 'id', right_on = 'SOPInstanceUID')
 df_image.rename(columns = {"label": "pe_present_on_image"}, inplace = True)
 del df_image['id']
df = df_exam.merge(df_image, how = 'left', on = 'StudyInstanceUID')
 ids = ['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID']
labels = [c for c in df.columns if c not in ids]
df = df[ids + labels]
df['positive_images_in_exam'] = df['StudyInstanceUID'].map(df.groupby(['StudyInstanceUID'] ).pe_present_on_image.max())
 df_pos = df.loc[df.positive_images_in_exam > 0.5]
df_neg = df.loc[df.positive_images_in_exam <= 0.5]
rule1a = df_pos.loc[(( df_pos.rv_lv_ratio_lt_1 > 0.5)& 
(df_pos.rv_lv_ratio_gte_1 > 0.5)) | 
(( df_pos.rv_lv_ratio_lt_1 <= 0.5)& 
(df_pos.rv_lv_ratio_gte_1 <= 0.5)) ].reset_index(drop = True)
 rule1a['broken_rule'] = '1a'
rule1b = df_pos.loc[(df_pos.central_pe <= 0.5)& 
(df_pos.rightsided_pe <= 0.5)& 
(df_pos.leftsided_pe <= 0.5)].reset_index(drop = True)
 rule1b['broken_rule'] = '1b'
rule1c = df_pos.loc[(df_pos.acute_and_chronic_pe > 0.5)& 
(df_pos.chronic_pe > 0.5)].reset_index(drop = True)
 rule1c['broken_rule'] = '1c'
rule1d = df_pos.loc[(df_pos.indeterminate > 0.5)| 
(df_pos.negative_exam_for_pe > 0.5)].reset_index(drop = True)
 rule1d['broken_rule'] = '1d'
rule2a = df_neg.loc[(( df_neg.indeterminate > 0.5)& 
(df_neg.negative_exam_for_pe > 0.5)) | 
(( df_neg.indeterminate <= 0.5)& 
(df_neg.negative_exam_for_pe <= 0.5)) ].reset_index(drop = True)
 rule2a['broken_rule'] = '2a'
rule2b = df_neg.loc[(df_neg.rv_lv_ratio_lt_1 > 0.5)| 
(df_neg.rv_lv_ratio_gte_1 > 0.5)| 
(df_neg.central_pe > 0.5)| 
(df_neg.rightsided_pe > 0.5)| 
(df_neg.leftsided_pe > 0.5)| 
(df_neg.acute_and_chronic_pe > 0.5)| 
(df_neg.chronic_pe > 0.5)].reset_index(drop = True)
 rule2b['broken_rule'] = '2b'
errors = pd.concat([rule1a, rule1b, rule1c, rule1d, rule2a, rule2b], axis = 0)
 print('Found', len(errors), 'inconsistent predictions')
 return errors<define_variables>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for row in df:
row['Title'] = row['Title'].map(title_mapping)
 row['Title'] = row['Title'].fillna(0 )
DO_FULL = False
FAST_COMMIT = path.exists('/kaggle/input/rsna-str-pulmonary-embolism-detection/train')and not DO_FULL<load_from_csv>
train_data = train_data.drop(['Name', 'PassengerId'], axis=1)
 test_data = test_data.drop(['Name'], axis=1 )
cfg = OmegaConf.load('/kaggle/input/rsna-str-pe-src/configs/predict/predict107.yaml')
 cfg.local_rank = 0
cfg.data.num_workers = 0
cfg.submission = '/kaggle/working/submission.csv'
predict_full(cfg, runtime=True, bsize=128, final_sub_only=True, fast_commit=FAST_COMMIT, use_vtk=True, debug=False )<load_from_csv>
df = [train_data, test_data]
sub = pd.read_csv('/kaggle/working/submission.csv')
 test = pd.read_csv('/kaggle/input/rsna-str-pulmonary-embolism-detection/test.csv')
 errors = check_consistency(sub, test)
 assert len(errors)== 0<load_from_zip>
encoder = LabelEncoder()
train_data['Sex'] = encoder.fit_transform(train_data['Sex'])
 test_data['Sex'] = encoder.fit_transform(test_data['Sex'] )
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2<install_modules>
guess_ages = np.zeros(( 2,3))
df = [train_data, test_data]
guess_ages
!pip install.. /input/efficientnet-pytorch063/efficientnet_pytorch-0.6.3-py3-none-any.whl<install_modules>
for row in df:
for i in range(0, 2):
for j in range(0, 3):
guess_df = row[(row['Sex'] == i)& \ 
(row['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
row.loc[(row.Age.isnull())&(row.Sex == i)&(row.Pclass == j+1),\
'Age'] = guess_ages[i,j]
row['Age'] = row['Age'].astype(int)
 train_data.head()
!pip install -q.. /input/noamior/monai-0.3.0-202010042353-py3-none-any.whl<import_modules>
train_data['AgeBand'] = pd.cut(train_data['Age'], 5)
 train_data.head()
efficientnet_pytorch.__version__<choose_model_class>
train_data[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
def load_cnn(filepath,name):
cnn5 = EfficientNet.from_name(name ).cuda()
if name == 'efficientnet-b5':
cnn5._fc = torch.nn.Linear(in_features=2048, out_features=1, bias=True)
 elif name == 'efficientnet-b4':
cnn5._fc = torch.nn.Linear(in_features=1536+256, out_features=1, bias=True)
 elif name == 'efficientnet-b3':
cnn5._fc = torch.nn.Linear(in_features=1536, out_features=1, bias=True)
 elif name == 'efficientnet-b2':
cnn5._fc = torch.nn.Linear(in_features=1408, out_features=1, bias=True)
 cnn5 = torch.nn.DataParallel(cnn5)
 cnn5.load_state_dict(torch.load(filepath))
cnn5.eval()
return cnn5<load_pretrained>
train_data['AgeBand'] = encoder.fit_transform(train_data['AgeBand'])
 train_data.head()
cnnb3 = [load_cnn(f'.. /input/rsnalstm2/efficientnet-b3_cnn_{row}_best.pth','efficientnet-b3')for row in [1,2]]
cnnb4 = [load_cnn(f'.. /input/rsnalstm2/efficientnet-b4_cnn_{row}_best.pth','efficientnet-b4')for row in [3,4]]
cnnb5 = [load_cnn(f'.. /input/rsnalstm2/efficientnet-b5_cnn_{row}_best.pth','efficientnet-b5')for row in [2,0]]<import_modules>
test_data['AgeBand'] = pd.cut(test_data['Age'], 5)
 test_data['AgeBand'] = encoder.fit_transform(test_data['AgeBand'])
 test_data.head()
import numpy as np
import pandas as pd
import glob
import cv2
import os
from matplotlib import pyplot as plt
import os
import cv2
import numpy as np
import pandas as pd
from torch.utils.data import TensorDataset, DataLoader,Dataset
import albumentations as albu
import functools
import torch
from tqdm.auto import tqdm
from joblib import Parallel, delayed
from joblib import parallel_backend<define_variables>
df = [train_data, test_data]
for row in df:
row['FamilySize'] = row['SibSp'] + row['Parch'] + 1
train_data[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
test_csv_path = '.. /input/rsna-str-pulmonary-embolism-detection/test.csv'<load_from_csv>
for row in df:
row['IsAlone'] = 0
row.loc[row['FamilySize'] == 1, 'IsAlone'] = 1
train_data[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
df = pd.read_csv(test_csv_path)
 df.head()<normalization>
train_data = train_data.drop(['Parch', 'SibSp'], axis = 1)
 test_data = test_data.drop(['Parch', 'SibSp'], axis = 1 )
def pixel_array(d):
return d.pixel_array
def load_dicom_array(f):
dicom_files = glob.glob(osp.join(f, '*.dcm'))
dicoms = [pydicom.dcmread(d)for d in dicom_files]
M = float(dicoms[0].RescaleSlope)
 B = float(dicoms[0].RescaleIntercept)
 z_pos = [float(d.ImagePositionPatient[-1])for d in dicoms]
dicoms = np.asarray([d.pixel_array for d in dicoms])
 dicoms = dicoms[np.argsort(z_pos)]
dicoms = dicoms * M
dicoms = dicoms + B
return dicoms, np.asarray(dicom_files)[np.argsort(z_pos)]
def window(img, WL=50, WW=350):
upper, lower = WL+WW//2, WL-WW//2
X = np.clip(img.copy() , lower, upper)
 X = X - np.min(X)
 X = X / np.max(X)
 X =(X*255.0 ).astype('uint8')
 return X
def save_array(X, save_dir, file_names):
for ind, img in enumerate(X):
savefile = osp.join(save_dir, file_names[ind])
 if not osp.exists(osp.dirname(savefile)) :
os.makedirs(osp.dirname(savefile))
_ = cv2.imwrite(osp.join(save_dir, file_names[ind]), img)
 def edit_filenames(files):
dicoms = [f"{ind:04d}_{f.split('/')[-1].replace('dcm','jpg')}" for ind,f in enumerate(files)]
series = ['/'.join(f.split('/')[-3:-1])for f in files]
return [osp.join(s,d)for s,d in zip(series, dicoms)]<normalization>
train_data['Age*Class'] = train_data['AgeBand'] * train_data['Pclass']
train_data.head()
def get_training_augmentation(y=256,x=256):
train_transform = [albu.RandomBrightnessContrast(p=0.3),
albu.VerticalFlip(p=0.5),
albu.HorizontalFlip(p=0.5),
albu.Downscale(p=1.0,scale_min=0.35,scale_max=0.75,),
albu.Resize(y, x)]
return albu.Compose(train_transform)
 formatted_settings = {
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],}
def preprocess_input(
 x, mean=None, std=None, input_space="RGB", input_range=None, **kwargs 
):
if input_space == "BGR":
x = x[..., ::-1].copy()
if input_range is not None:
if x.max() > 1 and input_range[1] == 1:
x = x / 255.0
return x
def get_preprocessing(preprocessing_fn):
_transform = [
albu.Lambda(image=preprocessing_fn),
albu.Lambda(image=to_tensor, mask=to_tensor),
]
return albu.Compose(_transform)
 def get_validation_augmentation(y=224,x=224):

 test_transform = [albu.Resize(256, 256),albu.CenterCrop(y,x)]
return albu.Compose(test_transform)
 def to_tensor(x, **kwargs):

 return x.transpose(2, 0, 1 ).astype('float32')
 def norm(img):
img-=img.min()
return img/img.max()
class Lungs(Dataset, Randomizable):
def __init__(self, dicom_folders):
self.dicom_folders = dicom_folders
self.transforms = get_validation_augmentation()
self.preprocessing = get_preprocessing(functools.partial(preprocess_input, **formatted_settings))
self.transform3d = Compose([ScaleIntensity() , Resize(( 160, 160, 160)) , ToTensor() ])
 def __len__(self):
return len(self.dicom_folders)
 def randomize(self)-> None:
MAX_SEED = np.iinfo(np.uint32 ).max + 1
self._seed = self.R.randint(MAX_SEED, dtype="uint32")
 def get(self, i):
s = time.time()
data = load_dicom_array(self.dicom_folders[i])
 image, files = data
image_lung = np.expand_dims(window(image, WL=-600, WW=1500), axis=3)
 image_mediastinal = np.expand_dims(window(image, WL=40, WW=400), axis=3)
 image_pe_specific = np.expand_dims(window(image, WL=100, WW=700), axis=3)
 image = np.concatenate([image_mediastinal, image_pe_specific, image_lung], axis=3)
 rat = MAX_LENGTH / np.max(image.shape[1:])
 names = [row.split(".dcm")[0].split("/")[-3:] for row in files]
images = []
for img in image:
if self.transforms:
img = self.transforms(image=img)['image']
if self.preprocessing:
img = self.preprocessing(image=img)['image']
images.append(img)
 images = np.array(images)
 img = images[:,::-1].transpose(1,2,3,0)
 if self.transform3d is not None:
if isinstance(self.transform3d, Randomizable):
self.transform3d.set_random_state(seed=self._seed)
 img = apply_transform(self.transform3d, img)
 return torch.from_numpy(images),names,img
def __getitem__(self, i):
self.randomize()
try:
return self.get(i)
 except Exception as e:
print(e)
 return None,None
MAX_LENGTH = 256.
 dicom_folders = list(( '.. /input/rsna-str-pulmonary-embolism-detection/test/' + df.StudyInstanceUID + '/'+ df.SeriesInstanceUID ).unique())
 dset = Lungs(dicom_folders )<load_pretrained>
test_data['Age*Class'] = test_data['AgeBand'] * test_data['Pclass']
train_data.head()
test = DataLoader(dset, batch_size=config.batch_size, shuffle=False, num_workers=config.WORKERS )<prepare_x_and_y>
freq_port = train_data.Embarked.dropna().mode() [0]
freq_port
x,n,x1 = dset[0]<load_from_csv>
train_data['Embarked'] = train_data['Embarked'].fillna(freq_port)
 test_data['Embarked'] = test_data['Embarked'].fillna(freq_port )
classes = list(pd.read_csv(".. /input/rsna-str-pulmonary-embolism-detection/train.csv" ).columns[3:] )<load_from_csv>
train_data['Embarked'] = encoder.fit_transform(train_data['Embarked'])
 train_data.head()
sub = pd.read_csv(".. /input/rsna-str-pulmonary-embolism-detection/sample_submission.csv")
 sub.id = sub.id.astype(str )<normalization>
test_data['Embarked'] = encoder.fit_transform(test_data['Embarked'])
 test_data.head()
df = [train_data, test_data]
sigmoid = nn.Sigmoid()
class Swish(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * sigmoid(i)
 ctx.save_for_backward(i)
 return result
@staticmethod
def backward(ctx, grad_output):
i = ctx.saved_variables[0]
sigmoid_i = sigmoid(i)
 return grad_output *(sigmoid_i *(1 + i *(1 - sigmoid_i)))
 class Swish_Module(nn.Module):
def forward(self, x):
return Swish.apply(x)
 class GaussianNoise(nn.Module):
def __init__(self, sigma=0.1, is_relative_detach=True):
super().__init__()
self.sigma = sigma
self.is_relative_detach = is_relative_detach
self.noise = torch.tensor(0 ).cuda().float()
def forward(self, x):
if self.training and self.sigma != 0:
scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
sampled_noise = self.noise.repeat(*x.size() ).normal_() * scale
x = x + sampled_noise
return x
class NeuralNet2(nn.Module):
def __init__(self, embed_size=5379, LSTM_UNITS=512, DO = 0.2,g=0.05):
super(NeuralNet2, self ).__init__()
self.lstm1 = nn.LSTM(embed_size, LSTM_UNITS, bidirectional=True, batch_first=True,dropout=0.0)
 self.lstm2 = nn.LSTM(LSTM_UNITS * 2, LSTM_UNITS, bidirectional=True, batch_first=True,dropout=0.0)
 self.Noise = GaussianNoise(g)
 self.linear1 = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear2 = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.avd3d = nn.AdaptiveAvgPool3d(1)
 self.avd1d = nn.AdaptiveAvgPool1d(1)
 self.linear_rv = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear_rlc = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear_pe = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear_pe = nn.Linear(LSTM_UNITS*2, 1)
 self.linear_global_fc0 = nn.Linear(3073, 3)
 self.linear_global_fc1 = nn.Linear(LSTM_UNITS*2*2, 3)
 self.linear_global_fc2 = nn.Linear(3073, 4)
 self.linear_global_fc3 = nn.Linear(LSTM_UNITS*2*2, 3)
 self.dropuot3d = nn.Dropout3d(DO)
 self.dropuot1d = nn.Dropout(DO)
 self.s3d = Swish_Module()
self.s1d = Swish_Module()
self.s2d = Swish_Module()
def forward(self, x, x_rv,x_rlc,x_pe):
x_rv = self.dropuot3d(x_rv)
 x_rv = self.avd3d(x_rv ).reshape(1,-1)
 x_rv1 = self.s3d(self.linear_rv(x_rv))
x_rv = x_rv.reshape(1,-1)
 x_rv1 = x_rv1.reshape(1,-1)
 x_rlc = self.dropuot3d(x_rlc)
 x_rlc = self.avd3d(x_rlc ).reshape(1,-1)
 x_rlc1 = self.s3d(self.linear_rlc(x_rlc))
x_rlc = x_rlc.reshape(1,-1)
 x_rlc1 = x_rlc1.reshape(1,-1)
 x_pe = self.dropuot3d(x_pe)
 x_pe = self.avd3d(x_pe ).reshape(1,-1)
 x_pe1 = self.s3d(self.linear_pe(x_pe))
x_pe = x_pe.reshape(1,-1)
 x_pe1 = x_pe1.reshape(1,-1)
 b,f = x.shape
embedding = x.reshape(1,b,f)
 self.lstm1.flatten_parameters()
h_lstm1, _ = self.lstm1(embedding)
 self.lstm2.flatten_parameters()
h_lstm2, _ = self.lstm2(h_lstm1)
 h_conc_linear1 = self.s1d(self.linear1(h_lstm1))
h_conc_linear2 = self.s2d(self.linear2(h_lstm2))
hidden = h_lstm1 + h_lstm2 + h_conc_linear1 + h_conc_linear2
output = self.linear_pe(hidden)
 hidden2 = self.avd1d(hidden.transpose(2,1))
hidden_rv = torch.cat([hidden2[:,:,0],x_rv+x_rv1],-1)
 hidden_rlc = torch.cat([hidden2[:,:,0],x_rlc+x_rlc1],-1)
 hidden_global = torch.cat([hidden2[:,:,0],x_pe1,x_pe,x_rlc+x_rlc1],-1)
 output_global0 = self.linear_global_fc0(hidden_global)
 output_global1 = self.linear_global_fc1(hidden_rv)
 output_global2 = self.linear_global_fc2(hidden_global)
 output_global3 = self.linear_global_fc3(hidden_rlc)
 return output,output_global0,output_global1,output_global2,output_global3<normalization>
freq_fare = test_data.Fare.dropna().mode() [0]
freq_fare.astype('int')
sigmoid = nn.Sigmoid()
class Swish(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * sigmoid(i)
 ctx.save_for_backward(i)
 return result
@staticmethod
def backward(ctx, grad_output):
i = ctx.saved_variables[0]
sigmoid_i = sigmoid(i)
 return grad_output *(sigmoid_i *(1 + i *(1 - sigmoid_i)))
 class Swish_Module(nn.Module):
def forward(self, x):
return Swish.apply(x)
 class GaussianNoise(nn.Module):
def __init__(self, sigma=0.1, is_relative_detach=True):
super().__init__()
self.sigma = sigma
self.is_relative_detach = is_relative_detach
self.noise = torch.tensor(0 ).cuda().float()
def forward(self, x):
if self.training and self.sigma != 0:
scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
sampled_noise = self.noise.repeat(*x.size() ).normal_() * scale
x = x + sampled_noise
return x
class NeuralNet(nn.Module):
def __init__(self, embed_size=5379, LSTM_UNITS=512, DO = 0.3,g=0.05):
super(NeuralNet, self ).__init__()
self.lstm1 = nn.LSTM(embed_size, LSTM_UNITS, bidirectional=True, batch_first=True,dropout=0.0)
 self.lstm2 = nn.LSTM(LSTM_UNITS * 2, LSTM_UNITS, bidirectional=True, batch_first=True,dropout=0.0)
 self.Noise = GaussianNoise(g)
 self.linear1 = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear2 = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.avd3d = nn.AdaptiveAvgPool3d(1)
 self.avd1d = nn.AdaptiveAvgPool1d(1)
 self.linear_rv = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear_rlc = nn.Linear(LSTM_UNITS*2, LSTM_UNITS*2)
 self.linear_pe = nn.Linear(LSTM_UNITS*2, 1)
 self.linear_global_fc0 = nn.Linear(LSTM_UNITS*2*4, 3)
 self.linear_global_fc1 = nn.Linear(LSTM_UNITS*2*2, 3)
 self.linear_global_fc2 = nn.Linear(LSTM_UNITS*2*4, 4)
 self.linear_global_fc3 = nn.Linear(LSTM_UNITS*2*2, 3)
 self.dropuot3d = nn.Dropout3d(DO)
 self.dropuot1d = nn.Dropout(DO)
 self.s3d = Swish_Module()
self.s1d = Swish_Module()
self.s2d = Swish_Module()
def forward(self, x, x_rv,x_rlc):
x_rv = self.dropuot3d(x_rv)
 x_rv = self.Noise(self.avd3d(x_rv ).reshape(1,-1))
x_rv1 = self.s3d(self.linear_rv(x_rv))
x_rv = x_rv.reshape(1,-1)
 x_rv1 = x_rv1.reshape(1,-1)
 x_rlc = self.dropuot3d(x_rlc)
 x_rlc = self.Noise(self.avd3d(x_rlc ).reshape(1,-1))
x_rlc1 = self.s3d(self.linear_rlc(x_rlc))
x_rlc = x_rlc.reshape(1,-1)
 x_rlc1 = x_rlc1.reshape(1,-1)
 b,f = x.shape
embedding = x.reshape(1,b,f)
 self.lstm1.flatten_parameters()
h_lstm1, _ = self.lstm1(embedding)
 self.lstm2.flatten_parameters()
h_lstm2, _ = self.lstm2(h_lstm1)
 h_conc_linear1 = self.s1d(self.linear1(h_lstm1))
h_conc_linear2 = self.s2d(self.linear2(h_lstm2))
hidden = h_lstm1 + h_lstm2 + h_conc_linear1 + h_conc_linear2
output = self.linear_pe(hidden)
 hidden2 = self.avd1d(hidden.transpose(2,1))
hidden_rv = torch.cat([hidden2[:,:,0],x_rv+x_rv1],-1)
 hidden_rlc = torch.cat([hidden2[:,:,0],x_rlc+x_rlc1],-1)
 hidden_global = torch.cat([hidden_rv,hidden_rlc],-1)
 output_global0 = self.linear_global_fc0(hidden_global)
 output_global1 = self.linear_global_fc1(hidden_rv)
 output_global2 = self.linear_global_fc2(hidden_global)
 output_global3 = self.linear_global_fc3(hidden_rlc)
 return output,output_global0,output_global1,output_global2,output_global3<choose_model_class>
test_data['Fare'] = test_data['Fare'].fillna(freq_fare)
 test_data.info()
models =[]
for file in [".. /input/rsnape3d/liniar_pe_neg_fold_0_fold_0_best.pth"]:
model = NeuralNet2().cuda()
model.load_state_dict(torch.load(file))
model.eval()
models.append(model)
 print(len(models))<choose_model_class>
for row in df:
row.loc[ row['Fare'] <= 7.91, 'Fare'] = 0
row.loc[(row['Fare'] > 7.91)&(row['Fare'] <= 14.454), 'Fare'] = 1
row.loc[(row['Fare'] > 14.454)&(row['Fare'] <= 31), 'Fare'] = 2
row.loc[ row['Fare'] > 31, 'Fare'] = 3
row['Fare'] = row['Fare'].astype(int)
 train_data = train_data.drop(['FareBand'], axis=1)
 combine = [train_data, test_data]
train_data.head(10 )
models_old =[]
for file in [".. /input/pth-lstm-543/best0.pth"]:
model = NeuralNet().cuda()
model.load_state_dict(torch.load(file))
model.eval()
models_old.append(model)
 print(len(models_old))<define_variables>
train_data = train_data.drop(['Age', 'FamilySize'], axis = 1)
 test_data = test_data.drop(['Age', 'FamilySize'], axis = 1 )
image_classes = '{}_pe_present_on_image'
global_class = ['{}_negative_exam_for_pe', '{}_rv_lv_ratio_gte_1', '{}_rv_lv_ratio_lt_1',
'{}_leftsided_pe', '{}_chronic_pe','{}_rightsided_pe', '{}_acute_and_chronic_pe', '{}_central_pe', '{}_indeterminate']
target_cols3d = [
'negative_exam_for_pe',
'rv_lv_ratio_gte_1',
'rv_lv_ratio_lt_1',
'leftsided_pe',
'chronic_pe',
'rightsided_pe',
'acute_and_chronic_pe',
'central_pe',
'indeterminate'
]<compute_test_metric>
X_train = train_data.drop("Survived", axis=1)
 Y_train = train_data["Survived"]
X_test = test_data.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape
def lstm_results(y,y1,y2):
predicted0,predicted1,predicted2,predicted3,predicted4 = 0,0,0,0,0
for i,model in enumerate(models):
predicted = model(y,y1,y2)
 if i>0:
predicted0_,predicted1_,predicted2_,predicted3_,predicted4_ = predicted[0],predicted[1],predicted[2],predicted[3],predicted[4]
predicted0+=predicted0_
predicted1+=predicted1_
predicted2+=predicted2_
predicted3+=predicted3_
predicted4+=predicted4_
else:
predicted0,predicted1,predicted2,predicted3,predicted4 = predicted[0],predicted[1],predicted[2],predicted[3],predicted[4]
return [predicted0/len(models),predicted1/len(models),predicted2/len(models),predicted3/len(models),predicted4/len(models)]<define_variables>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest
model_files_rlc = ['.. /input/lstm3d/densenet121_rlc_best_fold2.pth','.. /input/lstm3d/densenet121_rlc_model_fold1.pth','.. /input/lstm3d/densenet121_rlc_model_fold4.pth']
model_fils_rv = [f'.. /input/lstm3d/densenet121_best_fold2.pth',f'.. /input/lstm3d/densenet121_best_fold0.pth',f'.. /input/lstm3d/densenet121_best_fold3.pth']
model_fils_pe = [f'.. /input/rsnape3d/densenet121_pe_best_fold3.pth',f'.. /input/rsnape3d/densenet121_pe_best_fold1.pth',f'.. /input/rsnape3d/densenet121_pe_best_fold0.pth']<load_pretrained>
submission = pd.DataFrame({
"PassengerId": test_data["PassengerId"],
"Survived": Y_pred
} )
def load_model(model_file,c=1):
print(model_file)
 model = monai.networks.nets.densenet.densenet121(spatial_dims=3, in_channels=3, out_channels=c ).cuda()
try:
model.load_state_dict(torch.load(model_file), strict=True)
 except:
state_dict = torch.load(model_file)
 state_dict = {k[7:] if k.startswith('module.')else k: state_dict[k] for k in state_dict.keys() }
model.load_state_dict(state_dict, strict=True)
 model.eval()
return model
models_3d_rlc = [load_model(model3d,3)for i,model3d in enumerate(model_files_rlc)]
models_3d_rv = [load_model(model3d,1)for i,model3d in enumerate(model_fils_rv)]
models_3d_pe = [load_model(model3d,1)for i,model3d in enumerate(model_fils_pe)]
len(models_3d_rv),len(models_3d_rlc )<define_variables>
submission.to_csv('submission.csv', index=False )
do_all=True<find_best_params>
submission.to_csv('submission.csv', index=False )