Kaggle competition: Porto Seguro’s Safe Driver Prediction
predictions = []
for i, batch in enumerate(tqdm(testset)) :
fnames, images = batch
batch_preds = torch.sigmoid(model(images.to(device)))
 batch_preds = batch_preds.detach().cpu().numpy()
for fname, preds in zip(fnames, batch_preds):
for cls, pred in enumerate(preds):
pred, num = post_process(pred, best_threshold, min_size)
 rle = mask2rle(pred)
 name = fname + f"_{cls+1}"
predictions.append([name, rle])
 df = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels'])
 df.to_csv("submission.csv", index=False )<import_modules>
warnings.filterwarnings('ignore' )
import matplotlib.pyplot as plt
import seaborn as sns
import os
import gc
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tqdm.autonotebook import tqdm
import numpy as np
import pandas as pd
from keras import Sequential
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam, SGD
from keras.callbacks import ReduceLROnPlateau
from keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout
from keras.layers import Lambda, Input, GlobalAveragePooling2D,BatchNormalization
from keras.utils import to_categorical
from tensorflow.keras.models import Model
from keras.preprocessing.image import load_img 
<set_options>
input_path = '/kaggle/input/porto-seguro-safe-driver-prediction/'
df = pd.read_csv(input_path+'train.csv')
 df_test = pd.read_csv(input_path+'test.csv' )
print("GPU", "available(YESS!!!!)" if tf.config.list_physical_devices("GPU")else "not available :(")
 tf.config.list_physical_devices("GPU" )<load_from_csv>
id_test = df_test['id']
labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 labels.head()<train_model>
target = df['target']
Image(".. /input/dog-breed-identification/train/43572ba7edf772a95f539e57afd9eb43.jpg" )<sort_values>
target.value_counts()
classes = sorted(list(set(labels['breed'])))
 n_classes = len(classes)
 print('Total unique breed {}'.format(n_classes))
class_to_num = dict(zip(classes, range(n_classes)))
 class_to_num 
<prepare_x_and_y>
df.drop(columns=['target'],inplace=True )
input_shape =(331,331,3)
 def images_to_array(directory, label_dataframe, target_size = input_shape):
image_labels = label_dataframe['breed']
images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8)
 y = np.zeros([len(label_dataframe),1],dtype = np.uint8)
 for ix, image_name in enumerate(tqdm(label_dataframe['id'].values)) :
img_dir = os.path.join(directory, image_name + '.jpg')
 img = load_img(img_dir, target_size = target_size)
 images[ix]=img
del img
dog_breed = image_labels[ix]
y[ix] = class_to_num[dog_breed]
y = to_categorical(y)
 return images,y<prepare_x_and_y>
df_metadata = pickle.load(open('/kaggle/input/pssdpickledfmetedatapickle/df_metedata_pickle','rb'))
t = time.time()
X,y = images_to_array('/kaggle/input/dog-breed-identification/train', labels[:])
 print('runtime in seconds: {}'.format(time.time() - t))<prepare_x_and_y>
def preprocessing(df):
df.replace(to_replace=-1,value=np.nan,inplace=True)
 for col in df.columns:
if df_metadata.loc[col,'Dropped']:
df.drop(columns=[col],inplace=True)
 continue
df[col].fillna(df_metadata.loc[col,'Missing'],inplace=True)
 if(df_metadata.loc[col,'DTypes'] == 'Categorical')or(df_metadata.loc[col,'DTypes'] == 'Ordinal'):
df[col] = df[col].astype('category' )
<choose_model_class>
df.isnull().sum()
lrr= ReduceLROnPlateau(monitor='val_acc', factor=.01, patience=3, min_lr=1e-5,verbose = 1)
 EarlyStop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
<choose_model_class>
df_test.isnull().sum()
batch_size= 128
epochs=50
learn_rate=.001
sgd=SGD(lr=learn_rate,momentum=.9,nesterov=False)
 adam=Adam(lr=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False )<choose_model_class>
def outlier_processing(df,df_test):
for col in df.columns:
if df[col].dtype.name != 'category':
first_quartile, third_quartile = np.percentile(df[col],[25,75])
 first_percetnile, ninetynine_percentile = np.percentile(df[col],[1,99])
 IQR = third_quartile - first_quartile
lower_bound = first_quartile -(1.5*IQR)
 upper_bound = third_quartile +(1.5*IQR)
 df[col].loc[df[col]>upper_bound] = ninetynine_percentile
df_test[col].loc[df_test[col]>upper_bound] = ninetynine_percentile
df[col].loc[df[col]<lower_bound] = first_percetnile
df_test[col].loc[df_test[col]<lower_bound] = first_percetnile
img_size =(331,331,3)
 def get_features(model_name, model_preprocessor, input_size, data):
input_layer = Input(input_size)
 preprocessor = Lambda(model_preprocessor )(input_layer)
 base_model = model_name(weights='imagenet', include_top=False,
input_shape=input_size )(preprocessor)
 avg = GlobalAveragePooling2D()(base_model)
 feature_extractor = Model(inputs = input_layer, outputs = avg)
 feature_maps = feature_extractor.predict(data, verbose=1)
 print('Feature maps shape: ', feature_maps.shape)
 return feature_maps<feature_engineering>
ordinal_columns = [col for col in df.columns if df_metadata.loc[col,'DTypes'] == 'Ordinal' and df[col].nunique() > 2]
inception_preprocessor = preprocess_input
inception_features = get_features(InceptionV3,
inception_preprocessor,
img_size, X )<feature_engineering>
categorical_columns_great_2 = [col for col in df.columns if df_metadata.loc[col,'DTypes'] == 'Categorical' and df[col].nunique() > 2]
xception_preprocessor = preprocess_input
xception_features = get_features(Xception,
xception_preprocessor,
img_size, X )<feature_engineering>
for col in ordinal_columns:
label_encode = LabelEncoder()
df[col+'label'] = label_encode.fit_transform(df[col])
 df_test[col+'label'] = label_encode.transform(df_test[col])
 df.drop(columns=[col],inplace=True)
 df_test.drop(columns=[col],inplace=True )
inc_resnet_preprocessor = preprocess_input
inc_resnet_features = get_features(InceptionResNetV2,
inc_resnet_preprocessor,
img_size, X )<feature_engineering>
df = pd.get_dummies(df,prefix=col,columns=categorical_columns_great_2,drop_first=True)
 df_test = pd.get_dummies(df_test,columns=categorical_columns_great_2,prefix=col,drop_first=True )
nasnet_preprocessor = preprocess_input
nasnet_features = get_features(NASNetLarge,
nasnet_preprocessor,
img_size, X )<set_options>
scaler = MinMaxScaler()
del X
gc.collect()<choose_model_class>
df_train_scale = scaler.fit_transform(df )
model = Sequential()
model.add(Dropout(0.7,input_shape=(final_features.shape[1],)))
 model.add(Dense(n_classes,activation= 'softmax'))
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(final_features, y,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2,
callbacks=[lrr,EarlyStop] )<drop_column>
df_test_scale = scaler.transform(df_test )
del inception_features
del xception_features
del nasnet_features
del inc_resnet_features
del final_features
gc.collect()<load_from_csv>
from sklearn.model_selection import StratifiedKFold
<categorify>
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=17 )
def images_to_array_test(test_path, img_size =(331,331,3)) :
test_filenames = [test_path + fname for fname in os.listdir(test_path)]
data_size = len(test_filenames)
 images = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)
 for ix,img_dir in enumerate(tqdm(test_filenames)) :
img = load_img(img_dir, target_size = img_size)
 images[ix]=img
del img
print('Ouptut Data Size: ', images.shape)
 return images
test_data = images_to_array_test('/kaggle/input/dog-breed-identification/test/', img_size )<feature_engineering>
def gini(actual,pred,cmpcol = 0,sortcol = 1):
assert(len(actual)== len(pred))
All = np.asarray(np.c_[ actual, pred, np.arange(len(actual)) ], dtype=np.float)
 All = All[ np.lexsort(( All[:,2], -1*All[:,1])) ]
totAllosses = All[:,0].sum()
giniSum = All[:,0].cumsum().sum() / totAllosses
giniSum -=(len(actual)+ 1)/ 2.
 return giniSum / len(actual)
 def gini_normalized(a, p):
return gini(a, p)/ gini(a, a)
 def gini_xgb(preds, dtrain):
labels = dtrain.get_label()
gini_score = gini_normalized(labels, preds)
 return 'gini', gini_score
def extact_features(data):
inception_features = get_features(InceptionV3, inception_preprocessor, img_size, data)
 xception_features = get_features(Xception, xception_preprocessor, img_size, data)
 nasnet_features = get_features(NASNetLarge, nasnet_preprocessor, img_size, data)
 inc_resnet_features = get_features(InceptionResNetV2, inc_resnet_preprocessor, img_size, data)
 final_features = np.concatenate([inception_features,
xception_features,
nasnet_features,
inc_resnet_features],axis=-1)
 print('Final feature maps shape', final_features.shape)
 del inception_features
del xception_features
del nasnet_features
del inc_resnet_features
gc.collect()
return final_features
test_features = extact_features(test_data )<set_options>
import xgboost as xgb
del test_data
gc.collect()<predict_on_test>
params = {
'min_child_weight': 10.0,
'objective': 'binary:logistic',
'max_depth': 7,
'max_delta_step': 1.8,
'colsample_bytree': 0.4,
'subsample': 0.8,
'eta': 0.025,
'gamma': 0.65,
'num_boost_round' : 1000
}
pred = model.predict(test_features )<predict_on_test>
model_ensemble = []
for i,(train_index, valid_index)in enumerate(skf.split(df_train_scale,target)) :
print('[FOLD %d/%d]'%(i+1,5))
X_train,X_valid = df_train_scale[train_index],df_train_scale[valid_index]
y_train,y_valid = target.loc[train_index],target.loc[valid_index]
df_train_xgb = xgb.DMatrix(X_train,y_train)
 df_valid_xgb = xgb.DMatrix(X_valid,y_valid)
 valid_list = [(df_train_xgb, 'train'),(df_valid_xgb, 'valid')]
xgb_model = xgb.train(params, df_train_xgb, 3000, valid_list, feval=gini_xgb, maximize=True, early_stopping_rounds=70,verbose_eval=100)
 model_ensemble.append(xgb_model )
print(pred[0])
 print(f"Max value(probability of prediction): {np.max(pred[0])}")
 print(f"Sum: {np.sum(pred[0])}")
 print(f"Max index: {np.argmax(pred[0])}")
 print(f"Predicted label: {classes[np.argmax(pred[0])]}" )<prepare_output>
predict_proba = 0
df_test_xgb = xgb.DMatrix(df_test_scale)
 for i, model in enumerate(model_ensemble):
print('[FOLD %d/%d Prediciton:]'%(i+1,5))
predictions = xgb_model.predict(df_test_xgb)
 predict_proba += predictions
predict_proba = predict_proba/5
preds_df = pd.DataFrame(columns=["id"] + list(classes))
preds_df.head() 
<feature_engineering>
submit = pd.DataFrame({'id':id_test,'target':predict_proba})
 submit.to_csv('xgb_porto.csv',index=False)
 submit.head()