Kaggle competition: Santander Value Prediction Challenge
<SOS> metric: RMSLE Kaggle data source: santander-value-prediction-challenge<data_type_conversions>
import random
import numpy as np
import pandas as pd
from sklearn.base import clone
from keras.models import Sequential, load_model
from keras.layers import Dense, BatchNormalization, Dropout
from keras.callbacks import EarlyStopping, TensorBoard
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score, check_cv, KFold, train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from subprocess import check_output
from IPython.display import display
def preprocess_images(input_files, output_file, size):
x_tot, x2_tot = [], []
with zipfile.ZipFile(output_file, "w")as img_out:
for fname in input_files:
df = pd.read_parquet(fname)
 data = 255 - df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 for idx in range(len(df)) :
name = df.iloc[idx, 0]
img =(data[idx]*(255.0/data[idx].max())).astype(np.uint8)
 img = crop_resize(img, size)
 x_tot.append(( img/255.0 ).mean())
 x2_tot.append(((img/255.0)**2 ).mean())
 img = cv2.imencode(".png", img)[1]
img_out.writestr(name + ".png", img )<set_options>
warnings.filterwarnings('ignore')
 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
random_seed = 42
random.seed = random_seed
np.random.seed = random_seed
tf.set_random_seed(random_seed )
class MyDataset(Dataset):
def __init__(self, img_dir, img_files):
self.img_dir = img_dir
self.img_files = img_files
def __len__(self):
return len(self.img_files)
 def __getitem__(self, idx):
img = cv2.imread(self.img_files[idx])
 img = img.transpose(( 2, 0, 1)) [0]
img = img / 255
return int(self.img_files[idx].replace(self.img_dir, "" ).replace(".png", "" ).replace("Test_", "")) , np.array([img] )<define_variables>
train_df = pd.read_csv('.. /input/train.csv' )
preds = {}<load_pretrained>
cols = ['f190486d6', '58e2e02e6', 'eeb9cd3aa', '9fd594eec', '6eef030c1',
'15ace8c9f', 'fb0f5dbfe', '58e056e12', '20aa07010', '024c577b9',
'd6bb78916', 'b43a7cfd5', '58232a6fb', '1702b5bf0', '324921c7b',
'62e59a501', '2ec5b290f', '241f0f867', 'fb49e4212', '66ace2992',
'f74e8f13d', '5c6487af1', '963a49cdc', '26fc93eb7', '1931ccfdd',
'703885424', '70feb1494', '491b9ee45', '23310aa6f', 'e176a204a',
'6619d81fc', '1db387535', 'fc99f9426', '91f701ba2', '0572565c2',
'190db8488', 'adb64ff71', 'c47340d97', 'c5a231d81', '0ff32eb98']
if not os.path.isfile("test_images_128.zip"):
preprocess_images(INPUT_FILES, "test_images_128.zip", 128)
 with zipfile.ZipFile("test_images_128.zip")as img_zip:
img_zip.extractall("test_images_128")
 test_set = MyDataset("test_images_128/", glob.glob("test_images_128/*.png"))
test_loader = DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=False, num_workers=4 )<train_model>
y_train = np.log1p(train_df["target"].values)
 X_train = prepare_X(np.log1p(train_df.drop(["ID", "target"], axis=1)))
 print(X_train.shape, X_train.columns.tolist())
 X_train.head()
model = EfficientNet.from_name_2("efficientnet-b5", num_classes=186, in_channels=1)
 model.load_state_dict(torch.load("/kaggle/input/bengaliai-trained-models/model_b5_28.pth"))
model = model.to(device=DEVICE, dtype=DTYPE)
 model = model.eval()
with torch.no_grad() :
for test_id, X in test_loader:
test_id = test_id.numpy()
X = X.to(device=DEVICE, dtype=DTYPE)
 y_pred = model(X)
 y_pred = torch.sigmoid(y_pred ).cpu().detach().numpy() * 0.9638
for i, pred in list(zip(test_id, y_pred)) :
if i in preds:
preds[i] += pred
else:
preds[i] = pred<train_model>
def get_model(size1=16, size2=32, size3=0):
model = Sequential()
model.add(Dense(size1, input_dim=X_train.shape[1], activation='relu'))
model.add(BatchNormalization())
 if size2 > 2:
model.add(Dense(size2, activation='relu'))
model.add(BatchNormalization())
 if size3 > 2:
model.add(Dense(size3, activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(1, activation='relu'))
model.compile(loss='mse', optimizer='adam')
 return model
def fit(X, y, folds=5):
cv = KFold(folds, random_state=random_seed)
 estimators = []
for train, val in cv.split(X):
estimators.append(
 clone(model ).fit(X[train], y[train], callbacks=[EarlyStopping(verbose=2, patience=10)], epochs=999, validation_data=[X[val], y[val]])
)
 return estimators
def mean_score() :
best_scores = []
for e in estimators:
best_scores.append(
 e.history['val_loss'][-1] 
)
 return np.mean(best_scores)
 def predict(X):
y_pred = []
for e in estimators:
y_pred.append(e.model.predict(X))
return np.mean(y_pred, axis=0 )
model = EfficientNet.from_name_2("efficientnet-b2", num_classes=186, in_channels=1)
 model.load_state_dict(torch.load("/kaggle/input/bengaliai-trained-models/model_b2_20.pth"))
model = model.to(device=DEVICE, dtype=DTYPE)
 model = model.eval()
with torch.no_grad() :
for test_id, X in test_loader:
test_id = test_id.numpy()
X = X.to(device=DEVICE, dtype=DTYPE)
 y_pred = model(X)
 y_pred = torch.sigmoid(y_pred ).cpu().detach().numpy() * 0.9629
for i, pred in list(zip(test_id, y_pred)) :
if i in preds:
preds[i] += pred
else:
preds[i] = pred<load_from_zip>
model = KerasRegressor(build_fn=get_model, size1=20, size2=0, size3=0, batch_size=16, verbose=2)
 estimators = fit(X_train.values, y_train, folds=10)
 print('RMSE:', np.sqrt(mean_score()))
!rm -rf test_images_128 test_images_128.zip<load_pretrained>
test_df = pd.read_csv('.. /input/test.csv')
 X_test = prepare_X(np.log1p(test_df.drop(['ID'], axis=1)))
 print(X_test.shape )
<train_on_grid><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub["target"] = np.expm1(predict(X_test.values))
print(sub.head())
 sub.to_csv('sub_keras_{:.2f}.csv'.format(np.sqrt(mean_score())) , index=False )