Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<compute_test_metric>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
for param, score in zip(m_r.cv_results_['params'], m_r.cv_results_['mean_test_score']):
print(param, score )<find_best_params>
from fastai.vision import *
rf_bp = m_r.best_params_<choose_model_class>
path = Path('/kaggle/input/dog-breed-identification' )
rf_classifier=RandomForestClassifier(n_estimators=rf_bp["n_estimators"],
min_samples_split=rf_bp['min_samples_split'],
min_samples_leaf=rf_bp['min_samples_leaf'],
max_depth=rf_bp['max_depth'],
bootstrap=rf_bp['bootstrap'] )<train_model>
path.ls()
rf_classifier.fit(new_training_data,target )<predict_on_test>
df = pd.read_csv(path/'labels.csv')
 df.head()
y_pred = rf_classifier.predict(new_test_data )<save_to_csv>
np.random.seed(42)
 src =(ImageList.from_csv(path, 'labels.csv', folder='train', suffix='.jpg')
.split_by_rand_pct(0.2)
.label_from_df(cols='breed')
.add_test_folder(test_folder = 'test'))
sub['surface'] = le.inverse_transform(y_pred)
 sub.to_csv('random_f_rs.csv', index=False )<choose_model_class>
tfms = get_transforms(max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4, p_affine = 1., p_lighting=1.)
n_fold = 4
folds = StratifiedKFold(n_splits=n_fold, shuffle=True )<split>
bs, size = 64, 224
def randomforest_evaluate(**params):
params['n_estimators'] = int(round(params['n_estimators'],0))
params['min_samples_split'] = int(round(params['min_samples_split'],0))
params['min_samples_leaf'] = int(round(params['min_samples_leaf'],0))
params['bootstrap'] = int(round(params['bootstrap'],0))
test_pred_proba = np.zeros(( new_training_data.shape[0],9))
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(new_training_data, target)) :
X_train, X_valid = new_training_data.iloc[train_idx], new_training_data.iloc[valid_idx]
y_train, y_valid = target[train_idx], target[valid_idx]
model = RandomForestClassifier(**params,n_jobs=-1)
 model.fit(X_train, y_train)
 y_pred_valid = model.predict_proba(new_training_data)
 test_pred_proba += y_pred_valid / folds.n_splits
return accuracy_score(target, test_pred_proba.argmax(1))<choose_model_class>
data = src.transform(tfms, size=size ).databunch(bs=bs ).normalize(imagenet_stats )
rf_param_grid = {
'max_depth' :(8,100),
'n_estimators':(50,2000),
'min_samples_split':(2,10),
'min_samples_leaf':(2, 10),
'bootstrap':(True, False),
}
rf_b_o = BayesianOptimization(randomforest_evaluate, rf_param_grid)
 %time rf_b_o.maximize(init_points=5, n_iter=20 )<find_best_params>
arch = models.resnet50
metrics = [error_rate, accuracy]
for i,n in rf_b_o.max["params"].items() :
print(i,int(round(n)) )<save_to_csv>
learn = cnn_learner(data, arch, metrics=metrics )
model=RandomForestClassifier(n_estimators=int(round(rf_b_o.max["params"]["n_estimators"],0)) ,max_depth=int(round(rf_b_o.max["params"]["max_depth"])) ,
min_samples_leaf=int(round(rf_b_o.max["params"]["min_samples_leaf"])) ,min_samples_split=int(round(rf_b_o.max["params"]["min_samples_split"])))
 model.fit(new_training_data,target)
 y_pred = model.predict(new_test_data)
 sub['surface'] = le.inverse_transform(y_pred)
 sub.to_csv('random_f_bo.csv', index=False )<split>
learn.model_dir = '/kaggle/working'
def randomforest_evaluate(params):
print(params)
 params['n_estimators'] = int(round(params['n_estimators'],0))
params['min_samples_split'] = int(round(params['min_samples_split'],0))
params['min_samples_leaf'] = int(round(params['min_samples_leaf'],0))
params['bootstrap'] = int(round(params['bootstrap'],0))
test_pred_proba = np.zeros(( new_training_data.shape[0],9))
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(new_training_data, target)) :
X_train, X_valid = new_training_data.iloc[train_idx], new_training_data.iloc[valid_idx]
y_train, y_valid = target[train_idx], target[valid_idx]
model = RandomForestClassifier(**params,n_jobs=-1)
 model.fit(X_train, y_train)
 y_pred_valid = model.predict_proba(new_training_data)
 test_pred_proba += y_pred_valid / folds.n_splits
return accuracy_score(target, test_pred_proba.argmax(1))
space = {
'max_depth': hp.uniform('max_depth', 8,100),
'n_estimators': hp.uniform('n_estimators', 50, 2000),
'min_samples_split' : hp.choice('min_samples_split',[2,3,6,9]),
'min_samples_leaf' : hp.choice('min_samples_leaf', [2,3,6,9]),
'bootstrap': hp.choice('bootstrap', [False,True]),
}
%time best = fmin(randomforest_evaluate, space, algo=tpe.suggest, max_evals=25)
 if best["min_samples_split"] <=1:
best["min_samples_split"] = 2
if best["min_samples_split"] <=1:
best["min_samples_leaf"] = 2
print("Best Params:")
 print(best )<save_to_csv>
min_grad_lr = learn.recorder.min_grad_lr
print(min_grad_lr )
model=RandomForestClassifier(n_estimators=int(round(best["n_estimators"],0)) ,max_depth=int(round(best["max_depth"],0)) ,
min_samples_leaf=int(round(best["min_samples_leaf"])) ,min_samples_split=int(round(best["min_samples_split"])))
 model.fit(new_training_data,target)
 y_pred = model.predict(new_test_data)
 sub['surface'] = le.inverse_transform(y_pred)
 sub.to_csv('random_f_tpe.csv', index=False )<load_from_zip>
learn.fit_one_cycle(3, slice(min_grad_lr))
!git clone https://github.com/thuijskens/scikit-hyperband.git 2>/dev/null 1>/dev/null<install_modules>
learn.save('stage-1-224-rn50' )
!cp -r scikit-hyperband/* .<install_modules>
min_grad_lr = learn.recorder.min_grad_lr
print(min_grad_lr )
!python setup.py install 2>/dev/null 1>/dev/null<train_on_grid>
learn.fit_one_cycle(3, slice(min_grad_lr))
model = RandomForestClassifier()
param_dist = {
'max_depth': sp_randint(8,100),
'n_estimators': sp_randint(50,2000),
'min_samples_split' : [2,3,6,9,10],
'min_samples_leaf' : [2,3,6,9,10],
'bootstrap': [True, False]
}
y = LabelBinarizer().fit_transform(target)
 search = HyperbandSearchCV(model, param_dist,
resource_param='n_estimators',
scoring='accuracy')
 %time search.fit(new_training_data, y)
 print(search.best_params_ )<find_best_params>
learn.save('stage-2-224-rn50' )
best=search.best_params_<save_to_csv>
interp = ClassificationInterpretation.from_learner(learn )
model=RandomForestClassifier(n_estimators=int(round(best["n_estimators"],0)) ,max_depth=int(round(best["max_depth"],0)) ,
min_samples_leaf=best["min_samples_leaf"],min_samples_split=best["min_samples_split"])
 model.fit(new_training_data,target)
 y_pred = model.predict(new_test_data)
 sub['surface'] = le.inverse_transform(y_pred)
 sub.to_csv('random_f_hb.csv', index=False)
<define_search_space>
predictions, targets = learn.get_preds(ds_type=DatasetType.Test )
def initilialize_poplulation(numberOfParents):
max_depth = np.empty([numberOfParents, 1], dtype = np.uint8)
 n_estimators = np.empty([numberOfParents, 1], dtype = np.uint8)
 min_samples_split = np.empty([numberOfParents, 1])
 min_samples_leaf = np.empty([numberOfParents, 1])
 bootstrap = np.empty([numberOfParents, 1])
 for i in range(numberOfParents):
max_depth[i] = int(random.randrange(8, 100,step=1))
n_estimators[i] = int(random.randrange(50, 2000, step = 100))
min_samples_split[i] = int(random.randrange(2, 10, step= 1))
min_samples_leaf[i] = int(random.randrange(2, 10))
bootstrap[i] = int(random.randint(0, 1))
population = np.concatenate(( max_depth, n_estimators, min_samples_split, min_samples_leaf, bootstrap), axis= 1)
 return population<compute_test_metric>
classes = predictions.argmax(1)
 class_dict = dict(enumerate(learn.data.classes))
labels = [class_dict[i] for i in list(classes[:9].tolist())]
test_images = [i.name for i in learn.data.test_ds.items][:9]
def fitness_accscore(y_true, y_pred):
fitness = round(accuracy_score(y_true, y_pred), 4)
 return fitness
def train_population(population, train,y_train, test, y_test):
fScore = []
for i in range(population.shape[0]):
param_dist = {
'max_depth': int(population[i][0]),
'n_estimators': int(population[i][1]),
'min_samples_split' : int(population[i][2]),
'min_samples_leaf' : int(population[i][3]),
'bootstrap': int(population[i][4])
 }
model = RandomForestClassifier(**param_dist)
 model.fit(train,y_train)
 preds = model.predict(test)
 fScore.append(fitness_accscore(y_test, preds))
return fScore<feature_engineering>
preds, y = learn.TTA()
print(accuracy(preds,y))
print(log_loss(y, preds))
<define_search_space><EOS> 
preds_test, y_test = learn.TTA(ds_type=DatasetType.Test)
 df = pd.DataFrame(array(preds_test))
df.columns = data.classes
df.insert(0, "id", [str(pth ).split('/')[5][:-4] for pth in data.test_ds.items])
 df.to_csv("submission.csv", index=False )