Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<count_unique_values>
from fastai.vision import *
import fastai
import pandas as pd
len(target.series_id.unique() )<count_values>
fastai.__version__
len(train['series_id'].value_counts() )<count_unique_values>
path = Path('.. /input/dog-breed-identification')
 path_train = path / 'train'
path_test = path / 'test/test'
path_model = Path('/tmp/model/' )
len(test.series_id.unique() )<define_variables>
path_train, path_test
def create_valid_set(label):
ldict = {
'concrete': 0.16,
'soft_pvc': 0.18,
'wood': 0.06,
'tiled': 0.03,
'fine_concrete': 0.10,
'hard_tiles_large_space': 0.12,
'soft_tiles': 0.23,
'carpet': 0.05,
'hard_tiles': 0.07,
}
score = 0
print("Required count of target classes for the Valid Set :: ")
 for key, value in ldict.items() :
score += value
print(key,':',value )<normalization>
path.ls()
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z<feature_engineering>
list(Path('.. /input/dog-breed-identification' ).glob('**'))
def fe_step0(actual):
actual['norm_quat'] =(actual['orientation_X']**2 + actual['orientation_Y']**2 + actual['orientation_Z']**2 + actual['orientation_W']**2)
 actual['mod_quat'] =(actual['norm_quat'])**0.5
actual['norm_X'] = actual['orientation_X'] / actual['mod_quat']
actual['norm_Y'] = actual['orientation_Y'] / actual['mod_quat']
actual['norm_Z'] = actual['orientation_Z'] / actual['mod_quat']
actual['norm_W'] = actual['orientation_W'] / actual['mod_quat']
return actual<feature_engineering>
bs, size = 64, 224
%%time
train = fe_step0(train)
 test = fe_step0(test )<concatenate>
data = ImageDataBunch.from_csv(path=path,
folder='train',
csv_labels='labels.csv',
ds_tfms=get_transforms() ,
suffix='.jpg',
test='test/test',
size=size,
bs=bs,
num_workers=0 ).normalize(imagenet_stats )
def fe_step1(actual):

 x, y, z, w = actual['norm_X'].tolist() , actual['norm_Y'].tolist() , actual['norm_Z'].tolist() , actual['norm_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 actual['euler_x'] = nx
actual['euler_y'] = ny
actual['euler_z'] = nz
return actual<count_unique_values>
8178 + 2044
len(test['series_id'].unique() )<feature_engineering>
learn = cnn_learner(data,
base_arch=models.resnet50,
metrics=accuracy,
model_dir=path_model )
def total_values_fe(data):
data['totl_anglr_vel'] =(data['angular_velocity_X']**2 + data['angular_velocity_Y']**2 +
data['angular_velocity_Z'])** 0.5
data['totl_linr_acc'] =(data['linear_acceleration_X']**2 + data['linear_acceleration_Y']**2 +
data['linear_acceleration_Z'])**0.5
data['totl_xyz'] =(data['orientation_X']**2 + data['orientation_Y']**2 +
data['orientation_Z'])**0.5
data['acc_vs_vel'] = data['totl_linr_acc'] / data['totl_anglr_vel']
return data<count_missing_values>
learn.lr_find()
data = total_values_fe(train)
 test = total_values_fe(test)
 print(data.shape)
 data.head()<feature_engineering>
learn.fit_one_cycle(8 )
def mean_change_of_abs_change(x):
return np.mean(np.diff(np.abs(np.diff(x))))<count_unique_values>
learn.save('stage-1-50-224' )
len(test['series_id'].unique() )<create_dataframe>
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))
train_data = pd.DataFrame()
test_data = pd.DataFrame()<feature_engineering>
learn.load('stage-1-50-224');
%%time
columns = data.columns
for i in columns[1:]:
if i in ['row_id','series_id','measurement_number']:
continue
train_data[i+'_max'] = data.groupby(by='series_id')[i].max()
test_data[i+'_max'] = test.groupby(by='series_id')[i].max()
print(i)
 train_data[i+'_min'] = data.groupby(by='series_id')[i].min()
test_data[i+'_min'] = test.groupby(by='series_id')[i].min()
train_data[i+'_mean'] = data.groupby(by='series_id')[i].mean()
test_data[i+'_mean'] = test.groupby(by='series_id')[i].mean()
train_data[i+'_median'] = data.groupby(by='series_id')[i].median()
test_data[i+'_median'] = test.groupby(by='series_id')[i].median()
train_data[i+'_quantile_25'] = data.groupby(by='series_id')[i].quantile(0.25)
 test_data[i+'_quantile_25'] = test.groupby(by='series_id')[i].quantile(0.25)
 train_data[i+'_quantile_50'] = data.groupby(by='series_id')[i].quantile(0.5)
 test_data[i+'_quantile_50'] = test.groupby(by='series_id')[i].quantile(0.5)
 train_data[i+'_quantile_75'] = data.groupby(by='series_id')[i].quantile(0.75)
 test_data[i+'_quantile_75'] = test.groupby(by='series_id')[i].quantile(0.75)
 train_data[i+'_abs_max'] = data.groupby(by='series_id')[i].apply(lambda x: np.max(np.abs(x)))
 test_data[i+'_abs_max'] = test.groupby(by='series_id')[i].apply(lambda x: np.max(np.abs(x)))
 train_data[i + '_mean_abs_chg'] = data.groupby(['series_id'])[i].apply(lambda x: np.mean(np.abs(np.diff(x))))
test_data[i + '_mean_abs_chg'] = test.groupby(['series_id'])[i].apply(lambda x: np.mean(np.abs(np.diff(x))))
train_data[i + '_mean_change_of_abs_change'] = data.groupby('series_id')[i].apply(mean_change_of_abs_change)
 test_data[i + '_mean_change_of_abs_change'] = test.groupby('series_id')[i].apply(mean_change_of_abs_change)
 train_data[i + '_abs_min'] = data.groupby(['series_id'])[i].apply(lambda x: np.min(np.abs(x)))
 test_data[i + '_abs_min'] = test.groupby(['series_id'])[i].apply(lambda x: np.min(np.abs(x)))
 train_data[i + '_abs_avg'] =(train_data[i + '_abs_min'] + train_data[i + '_abs_max'])/2
test_data[i + '_abs_avg'] =(test_data[i + '_abs_min'] + test_data[i + '_abs_max'])/2
train_data[i+'_std'] = data.groupby(by='series_id')[i].std()
test_data[i+'_std'] = test.groupby(by='series_id')[i].std()
train_data[i + '_range'] = train_data[i + '_max'] - train_data[i + '_min']
test_data[i + '_range'] = test_data[i + '_max'] - test_data[i + '_min']
train_data[i + '_maxtoMin'] = train_data[i + '_max'] / train_data[i + '_min']
test_data[i + '_maxtoMin'] = test_data[i + '_max'] / test_data[i + '_min'] 
<count_missing_values>
preds, _ = learn.get_preds(DatasetType.Test )
train_data.isnull().values.any()<categorify>
def get_numeric_part(file_path, path_test):
low = len(str(path_test)) + 1
return str(file_path)[low:-4]
train_data.fillna(0,inplace=True)
 train_data.replace(-np.inf,0,inplace=True)
 train_data.replace(np.inf,0,inplace=True)
 test_data.fillna(0,inplace=True)
 test_data.replace(-np.inf,0,inplace=True)
 test_data.replace(np.inf,0,inplace=True )<count_missing_values>
get_numeric_part(data.test_ds.items[0], path_test )
train_data.isnull().values.any()<categorify>
df = pd.DataFrame(preds.numpy() , columns=data.classes)
 filenames = [get_numeric_part(fp, path_test)for fp in data.test_ds.items]
df.insert(0, "id", filenames)
 df_sorted = df.sort_values(by='id' )
<count_values><EOS> 
df_sorted.to_csv('submission.csv', index=False )