Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<create_dataframe>
%pylab inline
train_data_set = dataset_(train_data, train_dir,image_transform["train"])
 validation_data_set = dataset_(train_data,val_dir, image_transform["test"] )<create_dataframe>
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
train_data_frame = torch.utils.data.DataLoader(dataset=train_data_set, batch_size=32, shuffle=True)
 valid_data_frame = torch.utils.data.DataLoader(dataset=validation_data_set, batch_size=32 )<choose_model_class>
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
vgg16 = models.vgg16(pretrained=True)
 for param in vgg16.parameters() :
param.requires_grad = False 
<choose_model_class>
rcParams['figure.figsize'] = 10,10
classifier = torch.nn.Sequential(OrderedDict([ 
('fc1',torch.nn.Linear(512*7*7,1024)) , 
('relu',torch.nn.ReLU()), 
('fc2',torch.nn.Linear(1024,512)) , 
('relu', torch.nn.ReLU()), 
('fc3',torch.nn.Linear(512,2)) , 
('output',torch.nn.LogSoftmax(dim=1))
]))
vgg16.classifier = classifier<choose_model_class>
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )
optimizer = Adam(vgg16.classifier.parameters() , lr =0.001)
 criterion = torch.nn.NLLLoss()<set_options>
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )
device =("cuda:0" if torch.cuda.is_available() else "cpu" )<normalization>
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )
total_epochs = 10
vgg16.to(device)
 train_error_array = []
test_error_array = []
for epoch in range(total_epochs):
running_train_loss = 0.0
running_accuracy = 0.0
vgg16.train()
for step,(data, train_label)in enumerate(train_data_frame):
data, train_label = data.to(device), train_label.to(device)
 output_pred = vgg16.forward(data)
 train_ps = torch.exp(output_pred)
 train_prob, train_cls = train_ps.topk(1, dim=1)
 train_equals =(train_cls == train_label.long().view(*train_cls.shape))
running_accuracy += torch.mean(train_equals.type(torch.FloatTensor))
optimizer.zero_grad()
train_loss = criterion(output_pred, train_label.long())
 running_train_loss += train_loss.item()
train_loss.backward()
optimizer.step()
else:
test_accuracy= 0.0
testloss = 0.0
vgg16.eval()
with torch.no_grad() :
for valid_data, valid_label in valid_data_frame:
valid_data, valid_label = valid_data.to(device), valid_label.to(device)
 test_pred = vgg16.forward(valid_data)
 test_loss = criterion(test_pred, valid_label.long())
 testloss += test_loss.item()
out = torch.exp(test_pred)
 prob, cls = out.topk(1, dim=1)
 equals =(cls==valid_label.long().view(*cls.shape))
test_accuracy += torch.mean(equals.type(torch.FloatTensor))
train_error_array.append(running_train_loss/len(train_data_frame))
test_error_array.append(testloss/len(valid_data_frame))
print(f"EPoch: {epoch+1} >> ")
 print(f"training loss >> {running_train_loss/len(train_data_frame)}")
 print(f"test loss >> {testloss/len(valid_data_frame)}")
 print(f"test accuracy >> {test_accuracy/len(valid_data_frame)}")
 print("
")
<predict_on_test>
y_cat = to_categorical(y)
 print(y_cat.shape )
output = []
with torch.no_grad() :
vgg16.eval()
for test_data, _ in test_data_loader:
test_data = test_data.to(device)
 test_output = vgg16.forward(test_data)
 test_prob = torch.exp(test_output)
 output += list(test_prob[:,1].cpu().data.numpy())
<prepare_output>
model = Sequential()
model.add(Dense(1024,input_dim=192, init='uniform', activation='relu'))
model.add(Dense(99, activation='softmax'))
sample_output = {"id": sample["id"], "has_cactus":output}
sample_output_dataframe = pd.DataFrame(sample_output)
 sample_output_dataframe.head(10 )<save_to_csv>
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics = ["accuracy"] )
sample_output_dataframe.to_csv("submission.csv",index=False )<load_pretrained>
history = model.fit(X,y_cat,batch_size=32,
nb_epoch=400,verbose=0 )
kf = np.load(".. /input/split-dataset/dataset_indexs.npy", allow_pickle=True )<load_from_csv>
test = pd.read_csv('.. /input/test.csv' )
train_df = pd.read_csv('.. /input/google-quest-challenge/train.csv')
 sub_df = pd.read_csv('.. /input/google-quest-challenge/sample_submission.csv' )<define_variables>
index = test.pop('id' )
model_names = ["albert", "bert_base", "tf_bert_large", "feat_multi", "feat_nn",
"tf_bert_base_epoch1", "tf_bert_base_epoch2", "tf_bert_base_epoch3"]<compute_test_metric>
test = StandardScaler().fit(test ).transform(test )
def compute_spearmanr(trues, preds):
rhos = []
for col_true, col_pred in zip(trues.T, preds.T):
rhos.append(spearmanr(col_true, col_pred + np.random.normal(0, 1e-7, col_pred.shape[0])).correlation)
 return np.mean(rhos)
<load_from_csv>
yPred = model.predict_proba(test )
target_columns = list(sub_df.columns)
 results = []
for fold_i in range(1, 6):
result_df = pd.DataFrame(index=model_names, columns=target_columns[-30:] + ["ave"])
 for model_name in model_names:
for target in target_columns[-30:]:
true = train_df[target_columns].iloc[kf[fold_i-1][1]].reset_index(drop=True)
 oof = pd.read_csv(model_name + f"/oof{fold_i}.csv")
 r = spearmanr(true[target].values, oof[target].values + np.random.normal(0, 1e-7, oof.shape[0])).correlation
result_df.loc[model_name, target] = r
result_df.loc[model_name, "ave"] = np.mean(result_df.loc[model_name, target_columns[-30:]].values)
 results.append(result_df)
<load_from_csv>
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))
<load_from_csv><EOS> 
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )