Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<define_variables>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
csv_names = ['PassengerId',	'Survived',	'Pclass',	'Name',	'Sex',	'Age',	'SibSp',	'Parch',	'Ticket',	'Fare',	'Cabin', 'Embarked']<load_from_csv>
import numpy as np
import pandas as pd
import os
from fastai.imports import *
from fastai.torch_imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
dataset = pd.read_csv('/kaggle/input/titanic/train.csv', names= csv_names , header = 0 ,skipinitialspace=True )<count_missing_values>
PATH = '.. /input/'
sz = 224
bs = 58
arch = resnet50
print(os.listdir(PATH))
dataset.isnull().sum()<correct_missing_values>
labels_dir = f'{PATH}labels.csv'
n = len(list(open(labels_dir)))-1
val_idxs = get_cv_idxs(n)
 len(val_idxs)
 n
dataset1 = dataset.dropna(axis= 1 )<count_missing_values>
labels = pd.read_csv(labels_dir)
 labels.head()
labels['breed'].value_counts()
dataset1.isnull().sum()<feature_engineering>
tfms = tfms_from_model(arch, sz, aug_tfms = transforms_side_on, max_zoom = 1.1)
 data = ImageClassifierData.from_csv(PATH, 'train', csv_fname = labels_dir,test_name = 'test', suffix = '.jpg',val_idxs = val_idxs,tfms=tfms, bs =bs)
dataset1['Age'] = dataset['Age'].fillna(round(np.mean(dataset['Age'])) )<count_missing_values>
file = PATH + data.trn_ds.fnames[1000]
file
dataset1.isnull().sum()<count_unique_values>
img = PIL.Image.open(file)
 img
for i in dataset1.keys() :
print('the unique values for {} is {}'.format(i , len(dataset1[i].unique())) )<groupby>
def get_data(sz, bs):
tfms = tfms_from_model(arch, sz, aug_tfms = transforms_side_on, max_zoom = 1.1)
 data = ImageClassifierData.from_csv(PATH, 'train', csv_fname = labels_dir,test_name = 'test', suffix = '.jpg',val_idxs = val_idxs,tfms=tfms, bs =bs)
 if sz > 300:
return data
else:
data.resize(340, '/tmp')
 return data
dataset1.groupby('Survived')['Survived'].agg('count' )<groupby>
TMP_PATH = "/tmp/tmp"
MODEL_PATH = "/tmp/model/"
dataset1[dataset1['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count' )<groupby>
data = get_data(sz, bs)
 learn = ConvLearner.pretrained(arch, data,tmp_name = TMP_PATH, models_name = MODEL_PATH, precompute = True)
print('Total female survivors: {}'.format(dataset1[dataset1['Sex'] == 'female'].groupby('Survived')['Survived'].agg('count')[1]))
print('Total male survivors: {}'.format(dataset1[dataset1['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count')[1]))
print('Total survivors: {}'.format(dataset1.groupby('Survived')['Survived'].agg('count')[1]))<drop_column>
%time learn.fit(1e-1,5,cycle_len=1 )
dataset2 = dataset1.copy()
data_pop = dataset2.pop('Ticket' )<feature_engineering>
learn.precompute = False
%time learn.fit(1e-2,5,cycle_len=1 )
dataset2['Name'] = dataset2['Name'].apply(lambda x: x.split(',')[0] )<feature_engineering>
learn.save("224_pre")
 learn.load("224_pre" )
dataset2['Embarked'] = dataset['Embarked']<data_type_conversions>
sz = 299
learn.set_data(get_data(sz,bs))
learn.freeze()
learn.fit(1e-2,3,cycle_len=1 )
dataset2['Embarked'] = dataset2['Embarked'].fillna('S' )<count_missing_values>
learn.fit(1e-2, 3, cycle_len = 1, cycle_mult = 2 )
dataset2.isnull().sum()<groupby>
log_preds, y = learn.TTA()
probs = np.mean(np.exp(log_preds), 0)
 accuracy_np(probs, y), metrics.log_loss(y, probs )
dataset2.groupby('Embarked')['Embarked'].agg('count' )<groupby>
learn.save("229_pre")
 learn.load("229_pre" )
female_survival_ratio = dataset2[dataset2['Sex'] == 'female'].groupby('Survived')['Survived'].agg('count')[1]/dataset2.groupby('Sex')['Sex'].agg('count')['female']<groupby>
log_preds, y = learn.TTA(is_test= True)
 probs = np.mean(np.exp(log_preds), 0 )
male_survival_ratio = dataset2[dataset2['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count')[1]/dataset2.groupby('Sex')['Sex'].agg('count')['male']<compute_test_metric>
df = pd.DataFrame(probs)
 df.columns = data.classes
print('the male survival ratio is :{}'.format(male_survival_ratio))
print('the female survival ratio is :{}'.format(female_survival_ratio))<filter>
df.insert(0, 'id', [o[5:-4] for o in data.test_ds.fnames] )
passenger_survived =(dataset2['PassengerId'][dataset2['Survived'] == 1])-1
passenger_not_survived =(dataset2['PassengerId'][dataset2['Survived'] == 0])-1<create_dataframe>
df.to_csv("Submit.csv",index = False)