Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<create_dataframe>
Conv2D, SeparableConv1D, Add, BatchNormalization, Activation, GlobalAveragePooling2D, LeakyReLU, Flatten
Lambda, Multiply, LSTM, Bidirectional, PReLU, MaxPooling1D
%matplotlib inline
print(os.listdir(".. /input/"))
datasettrain_final = dataset11.copy()<feature_engineering>
def date_time(x):
if x==1:
return 'Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
 if x==2:
return 'Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now())
 if x==3:
return 'Date now: %s' % datetime.datetime.now()
if x==4:
return 'Date today: %s' % datetime.date.today()
datasettrain_final['Total_SibSp'] = dataset11['SibSp']+dataset11['Parch']<drop_column>
input_directory = r".. /input/dog-breed-identification/"
output_directory = r".. /output/"
training_dir = input_directory + "train"
testing_dir = input_directory + "test"
if not os.path.exists(output_directory):
os.mkdir(output_directory)
 figure_directory = ".. /output/figures"
if not os.path.exists(figure_directory):
os.mkdir(figure_directory)
 file_name_pred_batch = figure_directory+r"/result"
file_name_pred_sample = figure_directory+r"/sample"
datasettrain_final.pop('SibSp')
 datasettrain_final.pop('Parch' )<split>
train_df = pd.read_csv(input_directory + "/labels.csv")
 train_df.rename(columns={"breed": "label"}, inplace=True)
 train_df["id"] = train_df["id"].apply(lambda x: x+"."+"jpg")
 train_df.head()
x_train , x_test , y_train , y_test = train_test_split(datasettrain_final , y , test_size = 0.2 , random_state = 10 )<import_modules>
test_files = os.listdir(testing_dir)
 test_df = pd.DataFrame({"id": test_files, "label": "boston_bull"})
 test_df.head()
from sklearn.ensemble import *
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier<predict_on_test>
train_df2 = train_df.copy()
train_df2["label"] = train_df["label"].apply(lambda x: re.sub("_", " ", x))
train_df2["label"] = train_df2["label"].apply(lambda x: x.title())
 classes2 = train_df2["label"].unique()
classes2.sort()
rows =(len(classes)- 1)/4
obj = XGBClassifier(learning_rate=0.02, n_estimators=750,
max_depth= 3, min_child_weight= 1,
colsample_bytree= 0.6, gamma= 0.0,
reg_alpha= 0.001, subsample= 0.8)
 model = obj.fit(x_train, y_train)
 predicted_val = obj.predict(x_test)
 predicted_test = obj.predict(dataset_final)
 predicted_train = obj.predict(x_train )<compute_test_metric>
def get_data(batch_size=32, target_size=(299, 299), class_mode="categorical", training_dir=training_dir, testing_dir=testing_dir, x_col="id", y_col="label"):
print("Preprocessing and Generating Data Batches.......
 ")
 rescale = 1.0/255
train_batch_size = batch_size
validation_batch_size = batch_size*5
test_batch_size = batch_size*5
train_shuffle = True
val_shuffle = True
test_shuffle = False
train_datagen = ImageDataGenerator(
 horizontal_flip=True,
rotation_range=45,
shear_range=15,
rescale=rescale,
validation_split=0.25)
 train_generator = train_datagen.flow_from_dataframe(
 train_df,
training_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
seed=42,
subset='training')
 validation_generator = train_datagen.flow_from_dataframe(
 train_df,
training_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=validation_batch_size,
shuffle=True,
seed=42,
subset='validation')
 test_datagen = ImageDataGenerator(rescale=rescale)
 test_generator = test_datagen.flow_from_dataframe(
 test_df,
testing_dir,
x_col=x_col,
y_col=y_col,
target_size=target_size,
class_mode=class_mode,
batch_size=test_batch_size,
shuffle=False,
seed=42)
 class_weights = get_weight(train_generator.classes, binary=False)
 steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 print("
Preprocessing and Data Batch Generation Completed.
 ")
 return train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps
def get_weight(y, binary=True, n_samples=-1):
if binary==False:
class_weights = cw.compute_class_weight('balanced', np.unique(y), y)
 else:
d = {x:y.count(x)for x in set(y)}
num_class_temp = 2
class_weights = {}
for cls in d:
count = d.get(cls)
 class_weight = n_samples /(num_class_temp * count)
 class_weights[cls] = class_weight
return class_weights
accuracy_train = accuracy_score(y_true=y_train , y_pred= predicted_train)
 accuracy_val = accuracy_score(y_true=y_test, y_pred= predicted_val )<create_dataframe>
def get_model(model_name, input_shape=(96, 96, 3), num_class=2, weights='imagenet', dense_units=1024):
inputs = Input(input_shape)
 if model_name == "Xception":
base_model = Xception(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50":
base_model = ResNet50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101":
base_model = keras.applications.resnet.ResNet101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152":
base_model = keras.applications.resnet.ResNet152(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50V2":
base_model = resnet_v2.ResNet50V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101V2":
base_model = resnet_v2.ResNet101V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152V2":
base_model = resnet_v2.ResNet152V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt50":
base_model = resnext.ResNeXt50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt101":
base_model = resnext.ResNeXt101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionV3":
base_model = InceptionV3(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionResNetV2":
base_model = InceptionResNetV2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "DenseNet201":
base_model = DenseNet201(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetMobile":
base_model = NASNetMobile(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetLarge":
base_model = NASNetLarge(include_top=False, weights=weights, input_shape=input_shape)
 x = base_model(inputs)
 x = Dropout(0.8 )(x)
 x = GlobalAveragePooling2D()(x)
 x = BatchNormalization()(x)
 x = Dropout(0.8 )(x)
 if num_class>1:
outputs = Dense(num_class, activation="softmax" )(x)
 else:
outputs = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inputs, outputs=outputs)
 model.summary()
return model
output = pd.DataFrame({'PassengerId': dataset_test.PassengerId, 'Survived': predicted_test} )<save_to_csv>
def get_conv_model(num_class=2, input_shape=(150,150, 3)) :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
 if num_class>1:
model.add(Dense(num_class, activation='softmax'))
else:
model.add(Dense(num_class, activation='sigmoid'))
return model
output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
print("Settting Callbacks")
 def step_decay(epoch, lr):
lrate = lr
if epoch==2:
lrate = 0.0001
return lrate
checkpoint = ModelCheckpoint(
 model_file,
monitor='val_acc',
save_best_only=True)
 early_stopping = EarlyStopping(
 monitor='val_loss',
patience=3,
verbose=1,
restore_best_weights=True)
 reduce_lr = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.5,
patience=1,
min_lr=0.0000001,
verbose=1)
 learning_rate_scheduler = LearningRateScheduler(step_decay, verbose=1)
 callbacks = [reduce_lr, early_stopping]
print("Set Callbacks at ", date_time(1))
train_data = pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv")
 train_data.columns<feature_engineering>
dim = 299
input_shape =(dim, dim, 3)
 num_class = len(classes)
 weights = 'imagenet'
dense_units = 256
def outlier_detect(feature, data):
outlier_index = []
for each in feature:
Q1 = np.percentile(data[each], 25)
 Q3 = np.percentile(data[each], 75)
 IQR = Q3 - Q1
min_quartile = Q1 - 1.5*IQR
max_quartile = Q3 + 1.5*IQR
outlier_list = data[(data[each] < min_quartile)|(data[each] > max_quartile)].index
outlier_index.extend(outlier_list)
 outlier_index = Counter(outlier_index)
 outlier_data = list(i for i, n in outlier_index.items() if n > 3)
 return outlier_data<filter>
print("Getting Base Model", date_time(1))
model = get_model(model_name="InceptionV3", input_shape=input_shape, num_class=num_class, weights=weights, dense_units=dense_units)
 print("Loaded Base Model", date_time(1))
outlier_data = outlier_detect(["Age","SibSp","Parch","Fare"], train_data)
 train_data.loc[outlier_data] 
<drop_column>
batch_size = 64
class_mode = "categorical"
target_size =(dim, dim)
 y_col = "label"
train_data = train_data.drop(outlier_data, axis=0 ).reset_index(drop=True )<concatenate>
train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps = get_data(batch_size=batch_size,
target_size=target_size,
class_mode=class_mode,
y_col=y_col )
data = pd.concat([train_data, test_data], axis=0 ).reset_index(drop=True )<groupby>
print("Compliling Model...")
 learning_rate = 0.0001
optimizer = Adam(learning_rate)
 loss = 'categorical_crossentropy'
metrics = ['accuracy']
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
 print("Completed Model Compilation.
 " )
data[["Sex", "Survived"]].groupby(["Sex"], as_index = False ).mean()<filter>
steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 verbose = 1
epochs = 30
data.columns[data.isnull().any() ]<count_missing_values>
print("Starting Trainning...
 ")
 start_time = time.time()
print(date_time(1))
print("Trainning Model...
 ")
 history = model.fit_generator(
 train_generator,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_data=validation_generator,
validation_steps=validation_steps,
class_weight=class_weights)
 elapsed_time = time.time() - start_time
elapsed_time = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
print("
Elapsed Time: " + elapsed_time)
 print("Completed Model Trainning", date_time(1))
data.isnull().sum()<count_missing_values>
model.evaluate_generator(generator=validation_generator, steps=len(validation_generator), verbose=1 )
data.isnull().sum()<filter>
label_map =(train_generator.class_indices)
 label_map_inv = {v: k for k, v in label_map.items() }
data[data["Fare"].isnull() ]<feature_engineering>
ypreds = model.predict_generator(generator=test_generator, steps = len(test_generator), verbose=1 )
data["Fare"] = data["Fare"].fillna(np.mean(data[(( data["Pclass"]==3)&(data["Embarked"]==0)) ]["Fare"]))
data[data["Fare"].isnull() ]<filter>
ypred = ypreds.argmax(axis=-1)
data[data["Embarked"].isnull() ]<feature_engineering>
def get_rand_test_img(test_generator=None, labels_test=None, count=5):
filepaths = test_generator.filepaths
file_names = test_generator.filenames
selected_filepaths = []
selected_file_names = []
selected_labels = []
mem = set()
i = count
num_test_sample = test_generator.n
for file in test_generator.labels:
if i<=0:
break
rnd = random.randint(0, test_generator.n)
 while rnd in mem:
rnd = random.randint(0, test_generator.n)
 selected_filepaths.append(filepaths[rnd])
 selected_file_names.append(file_names[rnd])
 lbl = label_map_inv[labels_test[rnd]]
lbl = re.sub("_", " ", lbl)
 lbl = lbl.title()
selected_labels.append(lbl)
 i-=1
return selected_file_names, selected_file_names, selected_labels
data["Embarked"] = data["Embarked"].fillna(1)
 data[data["Embarked"].isnull() ]<filter>
test_img_count = 10
labels_test = ypred
selected_file_names, selected_file_names, selected_labels = get_rand_test_img(test_generator=test_generator, labels_test=labels_test, count=test_img_count )
data[data["Age"].isnull() ]<feature_engineering>
count = test_img_count
ncols = 5
nrows = count//ncols if count%ncols==0 else count//ncols+1
figsize=(20, ncols*nrows)
 ticksize = 14
titlesize = ticksize + 8
labelsize = ticksize + 5
params = {'figure.figsize' : figsize,
'axes.labelsize' : labelsize,
'axes.titlesize' : titlesize,
'xtick.labelsize': ticksize,
'ytick.labelsize': ticksize}
plt.rcParams.update(params)
 i = 0
for i in range(0, count):
if i>test_img_count:
break
plt.subplot(nrows, 5, i+1)
 plot_image(selected_file_names[i], testing_dir, aspect='auto')
 plt.title(selected_labels[i])
 plt.tight_layout()
plt.show()
data_age_nan_index = data[data["Age"].isnull() ].index
for i in data_age_nan_index:
mean_age = data["Age"][(data["Pclass"]==data.iloc[i]["Pclass"])].median()
data["Age"].iloc[i] = mean_age<categorify>
sample_submission = pd.read_csv(input_directory+"sample_submission.csv")
 sample_submission.head()
data["Alone"] = [1 if i == 0 else 0 for i in data["Family"]]
data["Family"].replace([0,1,2,3,4,5,6,7,10], [0,1,1,1,0,2,0,2,2], inplace=True)
 data.head()<feature_engineering>
test_dir_files = os.listdir(testing_dir)
 test_gen_files = test_generator.filenames
sample_submission_files = sample_submission["id"]
len(test_dir_files), len(test_gen_files), len(sample_submission_files )
data['Title']=data.Name.str.extract('([A-Za-z]+)\.' )<categorify>
m = {}
l = len(test_gen_files)
 for i in range(l):
m[test_gen_files[i]] = ypreds[i]
data['Title'].replace(['Mme','Ms','Mlle','Lady','Countess','Dona','Dr','Major','Sir','Capt','Don','Rev','Col', 'Jonkheer'],['Miss','Miss','Miss','Mrs','Mrs','Mrs','Mr','Mr','Mr','Mr','Mr','Other','Other','Other'], inplace=True )<categorify>
ypreds_sync = []
for f in sample_submission_files:
ypreds_sync.append(m[f+".jpg"] )
data['Age_Limit'] = LabelEncoder().fit_transform(data['Age_Limit'])
<categorify>
test_df = pd.DataFrame(data=ypreds_sync, columns=labels)
 test_df.head()
data['Fare_Limit'] = LabelEncoder().fit_transform(data['Fare_Limit'])
<data_type_conversions>
test_df["id"] = sample_submission_files
cols = test_df.columns.tolist()
cols = cols[-1:] + cols[:-1]
test_df = test_df[cols]
test_df.to_csv('submission.csv', index=False)
 test_df.head()