Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<define_variables>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
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 )<categorify>
labels = pd.read_csv(".. /input/labels.csv" )
m = {}
l = len(test_gen_files)
 for i in range(l):
m[test_gen_files[i]] = ypreds[i]<define_variables>
PATH = ".. /input/"
TMP_PATH = "/tmp/tmp"
MODEL_PATH = "/tmp/model/"
ypreds_sync = []
for f in sample_submission_files:
ypreds_sync.append(m[f+".jpg"] )<prepare_output>
arch=resnet101
sz=224
bs=48
test_df = pd.DataFrame(data=ypreds_sync, columns=labels)
 test_df.head()<save_to_csv>
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1 )
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()<set_options>
data = ImageClassifierData.from_csv(path=PATH, folder="train", csv_fname=f"{PATH}labels.csv", tfms=tfms, suffix=".jpg", test_name="test",bs=bs,num_workers=4 )
warnings.filterwarnings('ignore' )<load_from_csv>
learn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH, ps=0.4 )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv' )<drop_column>
lrf = learn.lr_find()
learn.sched.plot()
train_data.drop(['Cabin'], axis=1, inplace=True)
 test_data.drop(['Cabin'], axis=1, inplace=True )<count_values>
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', f'{PATH}labels.csv', test_name='test', suffix='.jpg',tfms=tfms, bs=bs, num_workers=4)
 return data if sz > 300 else data.resize(sz, '/tmp' )
whole_data = train_data.append(test_data)
 whole_data['Title'] = whole_data.Name.str.extract(r'([A-Za-z]+)\.', expand=False)
 whole_data.Title.value_counts()<feature_engineering>
learn.fit(1e-1, 5)
Common_Title = ['Mr', 'Miss', 'Mrs', 'Master']
whole_data['Title'].replace(['Ms', 'Mlle', 'Mme'], 'Miss', inplace=True)
 whole_data['Title'].replace(['Lady'], 'Mrs', inplace=True)
 whole_data['Title'].replace(['Sir', 'Rev'], 'Mr', inplace=True)
 whole_data['Title'][~whole_data.Title.isin(Common_Title)] = 'Others'<groupby>
learn.precompute=False
learn.fit(1e-1, 5, cycle_len=1 )
AgeMedian_by_titles = train_data.groupby('Title')['Age'].median()
AgeMedian_by_titles<feature_engineering>
learn.set_data(get_data(299,bs))
for title in AgeMedian_by_titles.index:
train_data['Age'][(train_data.Age.isnull())&(train_data.Title == title)] = AgeMedian_by_titles[title]
test_data['Age'][(test_data.Age.isnull())&(test_data.Title == title)] = AgeMedian_by_titles[title]<data_type_conversions>
learn.fit(1e-1, 3, cycle_len=1 )
train_data['Embarked'].fillna(train_data.Embarked.mode() [0], inplace=True )<data_type_conversions>
learn.fit(1e-1,3,cycle_len=1,cycle_mult=2 )
test_data['Fare'].fillna(test_data['Fare'].median() , inplace=True )<feature_engineering>
learn.fit(1e-1, 1, cycle_len=2 )
train_data.loc[train_data.Fare>512, 'Fare'] = 263
train_data.Fare.sort_values(ascending=False ).head(5 )<data_type_conversions>
log_preds, y = learn.TTA()
probs = np.mean(np.exp(log_preds), 0)
 accuracy_np(probs, y), log_loss(y, probs )
<data_type_conversions><EOS> 
log_preds_test, y_test = learn.TTA(is_test=True)
 probs_test = np.mean(np.exp(log_preds_test), 0)
 df = pd.DataFrame(probs_test)
 df.columns = data.classes
df.insert(0, "id", [e[5:-4] for e in data.test_ds.fnames])
 df.to_csv("submission.csv", index=False )