Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<train_on_grid>
from sklearn.preprocessing impbort LabelEncoder
def modelfit(alg,dtrain,predictors,useTrainCV = True, cv_folds = 5, early_stopping_rounds = 50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain[predictors].values, label = dtrain['deal_probability'].values, feature_names = predictors)
 cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round = alg.get_params() ['n_estimators'], nfold = cv_folds, metrics = 'rmse', early_stopping_rounds = early_stopping_rounds)
 alg.set_params(n_estimators = cvresult.shape[0])
 print('Best n_estimator = ' + str(cvresult.shape[0]))
alg.fit(dtrain[predictors], dtrain['deal_probability'], eval_metric = 'rmse')
 dtrain_predictions = alg.predict(dtrain[predictors])
 print('
Model Report:')
 print('RMSE: %f' % math.sqrt(metrics.mean_squared_error(dtrain['deal_probability'].values, dtrain_predictions)) )<categorify>
comp_df = pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 test_df = pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv')
 print('Training set: {}, Test set: {}'.format(comp_df.shape[0],test_df.shape[0]))
train.loc[:,'image_item_n'] = train.groupby(['image_top_1','item_seq_number'] ).user_id.transform('nunique')
 train.loc[:,'image_city_n'] = train.groupby(['city','image_top_1'] ).user_id.transform('nunique')
 train.loc[:,'image_region_n'] = train.groupby(['region','image_top_1'] ).user_id.transform('nunique')
 train.loc[:,'image_categoryname_n'] = train.groupby(['category_name','image_top_1'] ).user_id.transform('nunique')
 train.loc[:,'image_param2_n'] = train.groupby(['image_top_1','param_2'] ).user_id.transform('nunique')
 train.loc[:,'image_parentcategoryname_n'] = train.groupby(['parent_category_name','image_top_1'] ).user_id.transform('nunique')
 train.loc[:,'image_date_n'] = train.groupby(['image_top_1','date'] ).user_id.transform('nunique')
 train.loc[:,'image_usertype_n'] = train.groupby(['image_top_1','user_type'] ).user_id.transform('nunique')
 train.loc[:,'image_param1_n'] = train.groupby(['image_top_1','param_1'] ).user_id.transform('nunique')
 train.loc[:,'image_param3_n'] = train.groupby(['image_top_1','param_3'] ).user_id.transform('nunique' )<categorify>
comp_df.breed.value_counts()
test.loc[:,'image_item_n'] = test.groupby(['image_top_1','item_seq_number'] ).user_id.transform('nunique')
 test.loc[:,'image_city_n'] = test.groupby(['city','image_top_1'] ).user_id.transform('nunique')
 test.loc[:,'image_region_n'] = test.groupby(['region','image_top_1'] ).user_id.transform('nunique')
 test.loc[:,'image_categoryname_n'] = test.groupby(['category_name','image_top_1'] ).user_id.transform('nunique')
 test.loc[:,'image_param2_n'] = test.groupby(['image_top_1','param_2'] ).user_id.transform('nunique')
 test.loc[:,'image_parentcategoryname_n'] = test.groupby(['parent_category_name','image_top_1'] ).user_id.transform('nunique')
 test.loc[:,'image_date_n'] = test.groupby(['image_top_1','date'] ).user_id.transform('nunique')
 test.loc[:,'image_usertype_n'] = test.groupby(['image_top_1','user_type'] ).user_id.transform('nunique')
 test.loc[:,'image_param1_n'] = test.groupby(['image_top_1','param_1'] ).user_id.transform('nunique')
 test.loc[:,'image_param3_n'] = test.groupby(['image_top_1','param_3'] ).user_id.transform('nunique' )<categorify>
comp_df['label'] = LabelEncoder().fit_transform(comp_df.breed)
 dict_df = comp_df[['label','breed']].copy()
dict_df.drop_duplicates(inplace=True)
 dict_df.set_index('label',drop=True,inplace=True)
 index_to_breed = dict_df.to_dict() ['breed']
def clean(string):
string = re.sub(r'
', ' ', string)
 string = re.sub(r'\t', ' ', string)
 string = re.sub('[\W]', ' ', string)
 string = re.sub(r'\s{2,}', ' ', string.lower())
 return string
def find_punc(string):
string = re.sub(r'\s','',string)
 string = re.findall('[\W]',string)
 l = len(string)
 return l<data_type_conversions>
train_dir = '.. /input/dog-breed-identification/train'
comp_df.id = comp_df.id.apply(lambda x: x+'.jpg')
 comp_df.id = comp_df.id.apply(lambda x:train_dir+'/'+x)
 comp_df.pop('breed' )
train_t = train['title'].apply(clean)
 test_t = test['title'].apply(clean)
 train_d = train['description'].astype(str ).apply(clean)
 test_d = test['description'].astype(str ).apply(clean )<string_transform>
class img_dataset(Dataset):
def __init__(self,dataframe,transform=None,test=False):
self.dataframe = dataframe
self.transform = transform
self.test = test
def __getitem__(self,index):
x = Image.open(self.dataframe.iloc[index,0])
 if self.transform:
x = self.transform(x)
 if self.test:
return x
else:
y = self.dataframe.iloc[index,1]
return x,y
def __len__(self):
return self.dataframe.shape[0]
train_t_len = []
for line in train_t:
train_t_len.append(len(line.split()))
train_d_len = []
for line in train_d:
train_d_len.append(len(line.split()))
test_t_len = []
for line in test_t:
test_t_len.append(len(line.split()))
test_d_len = []
for line in test_d:
test_d_len.append(len(line.split()))<data_type_conversions>
train_transformer = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomRotation(15),
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 val_transformer = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])] )
train['t_per'] = np.array(train['title'].apply(find_punc)) /np.array(train_t_len)
 test['t_per'] = np.array(test['title'].apply(find_punc)) /np.array(test_t_len)
 train['d_n'] = train_d_len
test['d_n'] = test_d_len 
<drop_column>
def print_epoch_result(train_loss,train_acc,val_loss,val_acc):
print('loss: {:.3f}, acc: {:.3f}, val_loss: {:.3f}, val_acc: {:.3f}'.format(train_loss,
train_acc,
val_loss,
val_acc))
def train_model(model, cost_function, optimizer,num_epochs=5):
train_losses = []
val_losses = []
train_acc = []
val_acc = []
train_acc_object = metrics.Accuracy(compute_on_step=False)
 val_acc_object = metrics.Accuracy(compute_on_step=False)
 for epoch in range(num_epochs):

 print('-'*15)
 print('Start training {}/{}'.format(epoch+1, num_epochs))
print('-'*15)
 train_sub_losses = []
model.train()
for x,y in train_loader:
optimizer.zero_grad()
x,y = x.to(device),y.to(device)
 y_hat = model(x)
 loss = cost_function(y_hat,y)
 loss.backward()
optimizer.step()
train_sub_losses.append(loss.item())
 train_acc_object(y_hat.cpu() ,y.cpu())
 val_sub_losses = []
model.eval()
for x,y in val_loader:
x,y = x.to(device),y.to(device)
 y_hat = model(x)
 loss = cost_function(y_hat,y)
 val_sub_losses.append(loss.item())
 val_acc_object(y_hat.cpu() ,y.cpu())

train_losses.append(np.mean(train_sub_losses))
val_losses.append(np.mean(val_sub_losses))
train_epoch_acc = train_acc_object.compute()
val_epoch_acc = val_acc_object.compute()
train_acc.append(train_epoch_acc)
 val_acc.append(val_epoch_acc)
 train_acc_object.reset()
val_acc_object.reset()
print_epoch_result(np.mean(train_sub_losses),train_epoch_acc,np.mean(val_sub_losses),val_epoch_acc)
 print('Finish Training.')
 return train_losses, train_acc, val_losses, val_acc
train = train.drop(columns = ['title','description'])
 test = test.drop(columns = ['title','description'] )<define_variables>
device = torch.device('cuda:0' if torch.cuda.is_available else 'cpu' )
ready_train = train[['price','image_top_1','param_1','item_seq_number','city','region','parent_category_name','category_name','user_type','date','param_2','deal_probability','param_3','mix'] + list(train.columns[15:])]
ready_test = test[['price','image_top_1','param_1','item_seq_number','city','region','parent_category_name','category_name','user_type','date','param_2','param_3','mix'] + list(test.columns[14:])]<filter>
training_samples = comp_df.shape[0]
test_size=0.05
batch_size = 64
sample_df = comp_df.sample(training_samples)
 x_train,x_val,_,_ = train_test_split(sample_df,sample_df,test_size=test_size)
 train_set = img_dataset(x_train, transform=train_transformer)
 val_set = img_dataset(x_val, transform=val_transformer)
 train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
 val_loader = DataLoader(val_set , batch_size=batch_size, shuffle=True)
 print('Training set: {}, Validation set: {}'.format(x_train.shape[0],x_val.shape[0]))
predictors = ready_train.columns[ready_train.columns != 'deal_probability']<choose_model_class>
class net(torch.nn.Module):
def __init__(self, base_model, base_out_features, num_classes):
super(net,self ).__init__()
self.base_model=base_model
self.linear1 = torch.nn.Linear(base_out_features, 512)
 self.output = torch.nn.Linear(512,num_classes)
 def forward(self,x):
x = F.relu(self.base_model(x))
x = F.relu(self.linear1(x))
x = self.output(x)
 return x
res = torchvision.models.resnet50(pretrained=True)
 for param in res.parameters() :
param.requires_grad=False
model_final = net(base_model=res, base_out_features=res.fc.out_features, num_classes=120)
 model_final = model_final.to(device )
xgb1 = XGBRegressor(objective = 'reg:logistic', learning_rate = 0.1, n_estimators = 1000, max_depth = 5, min_child_weight = 6, gamma = 0, subsample = 0.9, colsample_bytree = 0.7, reg_alpha = 1.4, seed = 2018 )<compute_test_metric>
cost_function = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam([param for param in model_final.parameters() if param.requires_grad], lr=0.0003)
 EPOCHS = 30
modelfit(xgb1, ready_train, predictors, useTrainCV = False )<predict_on_test>
train_losses, train_acc, val_losses, val_acc = train_model(model=model_final,
cost_function=cost_function,
optimizer=optimizer,
num_epochs=EPOCHS )
pred = xgb1.predict(ready_test[predictors] )<save_to_csv>
test_dir = '.. /input/dog-breed-identification/test'
test_df = test_df[['id']]
test_df.id = test_df.id.apply(lambda x: x+'.jpg')
 test_df.id = test_df.id.apply(lambda x : test_dir+'/'+x)
 test_set = img_dataset(test_df,transform=val_transformer, test=True)
 test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False )
submission['deal_probability'] = pred
submission.to_csv('submission.csv',index=False )<set_options>
model_final.eval()
predictions = torch.tensor([])
 print('Start predicting.... ')
 for x in test_loader:
x = x.to(device)
 y_hat = model_final(x)
 predictions = torch.cat([predictions, y_hat.cpu() ])
 print('Finish prediction.' )
%matplotlib inline<load_from_csv>
predictions = F.softmax(predictions,dim=1 ).detach().numpy()
<feature_engineering><EOS> 
answer_id = pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' ).id.tolist()
predictions_df = pd.DataFrame(predictions, index=answer_id)
 predictions_df.columns = predictions_df.columns.map(index_to_breed)
 predictions_df.rename_axis('id', inplace=True)
 predictions_df.to_csv('submission.csv' )