Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
sz = 256
N = 42
def tile(img):
result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
def tile2(img):
result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz +(( sz * 2)// 2),(sz - shape[1]%sz)%sz +(( sz * 2)// 2)
 img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
class ConvNet(tf.keras.Model):
def __init__(self, engine, input_shape, weights):
super(ConvNet, self ).__init__()
self.engine = engine(
 include_top=False, input_shape=input_shape, weights=weights)
 self.avg_pool2d = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(0.5)
 self.dense_1 = tf.keras.layers.Dense(1024)
 self.dense_2 = tf.keras.layers.Dense(1)
 @tf.function
def call(self, inputs, **kwargs):
x = tf.reshape(inputs,(-1, IMG_SIZE, IMG_SIZE, 3))
x = self.engine(x)
 shape = x.shape
x = tf.reshape(x,(-1, N_TILES, shape[1], shape[2], shape[3]))
x = tf.transpose(x, perm=[0, 2, 1, 3, 4])
 x = tf.reshape(x,(-1, shape[1], N_TILES*shape[2], shape[3]))
x = self.avg_pool2d(x)
 x = self.dropout(x, training=False)
 x = self.dense_1(x)
 x = tf.nn.relu(x)
 return self.dense_2(x)
 is_ef = True
backbone_name = 'efficientnet-b0'
N_TILES = 42
IMG_SIZE = 256
if backbone_name.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone_name[-1]}')
 model = ConvNet(engine=model_fn, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights=None)
 TRAIN = '.. /input/prostate-cancer-grade-assessment/train_images/'
MASKS = '.. /input/prostate-cancer-grade-assessment/train_label_masks/'
BASE_PATH = '.. /input/prostate-cancer-grade-assessment/'
train = pd.read_csv(BASE_PATH + "train.csv")
 train.head()
sub = pd.read_csv(".. /input/prostate-cancer-grade-assessment/sample_submission.csv")
 sub.head()
test = pd.read_csv(".. /input/prostate-cancer-grade-assessment/test.csv")
 test.head()
TEST = '.. /input/prostate-cancer-grade-assessment/test_images/'
PRED_PATH = TEST
df = sub
t_df = test
n_TTA = 2
dummy_data = tf.zeros(( n_TTA * N_TILES, IMG_SIZE, IMG_SIZE, 3), dtype=tf.float32)
 _ = model(dummy_data)
<choose_model_class>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 test_ids = test_data['PassengerId']
data = pd.concat([train_data, test_data], axis=0)
 data.head()
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b0-fold0-epochs40.h5')
 if os.path.exists(PRED_PATH):
predictions50 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions50.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions50 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions50.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions52 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions52.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions52 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions52.append(isup)
 del patches, img
gc.collect() 
<load_pretrained>
data = data.drop(['Cabin', 'Ticket'], axis=1 )
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b0-fold4-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions60 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions60.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions60 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions60.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions62 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions62.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions62 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions62.append(isup)
 del patches, img
gc.collect() 
<choose_model_class>
data['Title'] = [value.split(', ')[1].split('.')[0] for value in data['Name'].values]
data.loc[(data['Title'] == 'Lady')|(data['Title'] == 'Mme')|(data['Title'] == 'Ms')|(data['Title'] == 'the Countess')|(data['Title'] == 'Mlle'), 'Title'] = 'Miss'
data.loc[(data['Title'] != 'Mr')&(data['Title'] != 'Mrs')&(data['Title'] != 'Miss')&(data['Title'] != 'Master'), 'Title'] = 'Other'
encoder = LabelEncoder()
data['Surname'] = encoder.fit_transform([value.split(', ')[0] for value in data['Name'].values] )
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b0-fold2-epochs40.h5')
 if os.path.exists(PRED_PATH):
predictions70 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions70.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions70 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions70.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions72 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions72.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions72 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = np.mean(pred)
 predictions72.append(isup)
 del patches, img
gc.collect()
del model, dummy_data, sub, pred, train, isup, image
del patches3,patches4
gc.collect()<define_variables>
data['Alone'] = np.zeros(data.shape[0], dtype=np.int)
 data.loc[(data['SibSp'] == 0)&(data['Parch'] == 0), 'Alone'] = 1
data['FamilyCount'] = data['SibSp'] + data['Parch']
data.head()
PREDS =(1/5)*PREDS +(1/5)*PREDS1 +(1/5)*PREDS2 +(1/5)*PREDS3 +(1/5)*PREDS4
FINAL = np.round(( 6/10)*PREDS + 
(2/60)*np.array(predictions10)+(2/60)*np.array(predictions12)+ 
(2/60)*np.array(predictions20)+(2/60)*np.array(predictions22)+ 
(2/60)*np.array(predictions30)+(2/60)*np.array(predictions32)+ 
(0.5/10)*np.array(predictions40)+(0.5/10)*np.array(predictions42)+ 
(1/60)*np.array(predictions50)+(1/60)*np.array(predictions52)+ 
(1/60)*np.array(predictions60)+(1/60)*np.array(predictions62)+ 
(1/60)*np.array(predictions70)+(1/60)*np.array(predictions72))
df['isup_grade'] = FINAL.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<define_variables>
np.random.seed(seed=157)
 mean_ages_title = data[['Title', 'Age']].loc[data['Age'].notna() ].groupby('Title' ).mean()
age_nan_titles = data['Title'].loc[data['Age'].isnull() ].unique()
for i in range(len(age_nan_titles)) :
if age_nan_titles[i] == 'Mr' or age_nan_titles[i] == 'Mrs' or age_nan_titles[i] == 'Miss':
data.loc[(data['Age'].isnull())&(data['Title'] == age_nan_titles[i]), 'Age'] = [np.ceil(mean_ages_title.loc[age_nan_titles[i]].values[0])+ np.random.randint(-6, 6)
 for _ in data['Age'].loc[(data['Age'].isnull())&(data['Title'] == age_nan_titles[i])]]
else:
data.loc[(data['Age'].isnull())&(data['Title'] == age_nan_titles[i]), 'Age'] = [np.ceil(mean_ages_title.loc[age_nan_titles[i]].values[0])
 for _ in data['Age'].loc[(data['Age'].isnull())&(data['Title'] == age_nan_titles[i])]]
DEBUG = False<define_variables>
_, age_categories = pd.cut(data['Age'], 5, retbins=True)
 age_categories = [np.floor(value)for value in age_categories]
data['AgeRange'] = np.zeros(len(data), dtype=np.int)
 data.loc[data['Age'] <= age_categories[1], 'AgeRange'] = 0
data.loc[(data['Age'] > age_categories[1])&(data['Age'] <= age_categories[2]), 'AgeRange'] = 1
data.loc[(data['Age'] > age_categories[2])&(data['Age'] <= age_categories[3]), 'AgeRange'] = 2
data.loc[(data['Age'] > age_categories[3])&(data['Age'] <= age_categories[4]), 'AgeRange'] = 3
data.loc[data['Age'] > age_categories[4], 'AgeRange'] = 4
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
_, fare_categories = pd.qcut(data['Fare'], 4, retbins=True)
 data['FareRange'] = np.zeros(len(data), dtype=np.int)
 data.loc[data['Fare'] <= fare_categories[1], 'FareRange'] = 0
data.loc[(data['Fare'] > fare_categories[1])&(data['Fare'] <= fare_categories[2]), 'FareRange'] = 1
data.loc[(data['Fare'] > fare_categories[2])&(data['Fare'] <= fare_categories[3]), 'FareRange'] = 2
data.loc[data['Fare'] > fare_categories[3], 'FareRange'] = 3
import skimage.io
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from efficientnet_pytorch import model as enet
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm 
<load_from_csv>
data = pd.get_dummies(data=data, columns=['Sex', 'Embarked', 'Pclass', 'AgeRange', 'FareRange', 'Title'], dtype=np.int )
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/panda-enet-b1-model/'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:100]
tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 8
num_workers = 4
device = torch.device('cuda')
 print(image_folder )<define_search_model>
data = data.drop(['Name', 'Age', 'Fare', 'SibSp', 'Parch'], axis=1 )
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b1'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'enet_8ep_fold0.pth'
]
models = load_models(model_files )<categorify>
X_train = data.loc[data['PassengerId'].isin(test_ids)== False]
y_train = X_train['Survived']
X_test = data.loc[data['PassengerId'].isin(test_ids)]
X_train = X_train.drop(['PassengerId', 'Survived'], axis=1)
 X_test = X_test.drop(['PassengerId', 'Survived'], axis=1)
 X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=157, stratify=y_train)
 X_train.head()
def get_tiles(img, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size +(( tile_size * mode)// 2)
 pad_w =(tile_size - w % tile_size)% tile_size +(( tile_size * mode)// 2)
 img2 = np.pad(img,[[pad_h // 2, pad_h - pad_h // 2], [pad_w // 2,pad_w - pad_w//2], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img)< n_tiles:
img3 = np.pad(img3,[[0,N-len(img3)],[0,0],[0,0],[0,0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rand=False,
sub_imgs=False 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rand = rand
self.sub_imgs = sub_imgs
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
images[h1:h1+image_size, w1:w1+image_size] = this_img
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images)
<load_pretrained>
scaler = StandardScaler()
column_names = X_train.columns
X_train = pd.DataFrame(scaler.fit_transform(X_train), columns=column_names)
 column_names = X_val.columns
X_val = pd.DataFrame(scaler.fit_transform(X_val), columns=column_names)
 column_names = X_test.columns
X_test = pd.DataFrame(scaler.fit_transform(X_test), columns=column_names )
dataset = PANDADataset(df, image_size, n_tiles,0)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False )<save_to_csv>
init_tanh = glorot_uniform(seed=157)
 model = Sequential()
model.add(Dense(units=X_train.shape[1], kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)))
 model.add(BatchNormalization())
 model.add(Activation(tanh))
model.add(Dense(units=X_train.shape[1] * 2, kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)))
 model.add(BatchNormalization())
 model.add(Activation(tanh))
model.add(Dense(units=X_train.shape[1] * 4, kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)))
 model.add(BatchNormalization())
 model.add(Activation(tanh))
model.add(Dense(units=X_train.shape[1], kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)))
 model.add(BatchNormalization())
 model.add(Activation(tanh))
model.add(Dense(units=X_train.shape[1] * 4, kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)))
 model.add(BatchNormalization())
 model.add(Activation(tanh))
model.add(Dense(units=1, activation='sigmoid', kernel_initializer=init_tanh, kernel_constraint=maxnorm(3)) )
LOGITS = []
LOGITS2 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() + torch.cat(LOGITS2 ).sigmoid().cpu())/ 2
PREDS = LOGITS.sum(1 ).round().numpy()
df['isup_grade'] = PREDS.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts())
<define_variables>
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ['PYTHONHASHSEED'] = str(157)
 random.seed(157)
 np.random.seed(157)
 tf.compat.v1.set_random_seed(157)
 session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
 sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph() , config=session_conf)
 k.set_session(sess )
DEBUG = False<define_variables>
opt = Adam(learning_rate=0.0009, amsgrad=True)
 model.compile(optimizer=opt,
loss='binary_crossentropy',
metrics=['accuracy'])
 checkpoint = ModelCheckpoint('neural_network_checkpoint_training.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
 tensorboard = TensorBoard(log_dir='./logs',
histogram_freq=0,
write_graph=True,
write_images=True)
 model.fit(x=X_train, y=y_train,
epochs=40,
validation_data=(X_val, y_val),
batch_size=50,
shuffle=True,
callbacks=[tensorboard, checkpoint],
verbose=1)
 save_model(checkpoint.model, 'neural_network_latest_saved.h5' )
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
clf = load_model('neural_network_checkpoint_training.h5')
 threshold = 0.5
test_prediction = clf.predict(X_test.values ).flatten()
test_prediction[test_prediction <= threshold] = 0
test_prediction[test_prediction > threshold] = 1
test_prediction = pd.DataFrame(test_prediction, columns=['Survived'], dtype=np.int)
 prediction = pd.concat([test_ids, test_prediction], axis=1)
 prediction.to_csv('submission_nn.csv', index=False )