<jupyter_start><jupyter_text>3. Train-LogReg**Tensorboard**- Input at command: tensorboard --logdir=./log- Input at browser: http://127.0.0.1:6006 References:- [ypwhs/dogs_vs_cats](https://github.com/ypwhs/dogs_vs_cats)- [Keras Inception + Xception (0.47), 杨培文(Yang Peiwen)](https://www.kaggle.com/yangpeiwen/keras-inception-xception-0-47)- [use-pretrained-keras-models](https://www.kaggle.com/gaborfodor/use-pretrained-keras-models-lb-0-3)- [LogLoss](https://www.kaggle.com/wiki/LogLoss)- [evaluation](https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-editionevaluation)<jupyter_code>import time
import os
import pandas as pd

project_name = 'Dog_Breed_Identification'
step_name = 'Train'
time_str = time.strftime("%Y%m%d_%H%M%S", time.localtime())
run_name = project_name + '_' + step_name + '_' + time_str
print('run_name: ' + run_name)

cwd = os.getcwd()
model_path = os.path.join(cwd, 'model')
print('model_path: ' + model_path)
import os
import h5py
import numpy as np
from sklearn.utils import shuffle
np.random.seed(2017)

x_data = []
y_data = {}
# x_val = []
# y_val = {}
x_test = []

cwd = os.getcwd()
feature_cgg16 = os.path.join(cwd, 'model', 'feature_VGG16_{}.h5'.format(171023))
feature_cgg19 = os.path.join(cwd, 'model', 'feature_VGG19_{}.h5'.format(171023))
feature_resnet50 = os.path.join(cwd, 'model', 'feature_ResNet50_{}.h5'.format(171023))
feature_xception = os.path.join(cwd, 'model', 'feature_Xception_{}.h5'.format(171023))
feature_inception = os.path.join(cwd, 'model', 'feature_InceptionV3_{}.h5'.format(171023))
for filename in [feature_cgg16, feature_cgg19, feature_resnet50]:
    with h5py.File(filename, 'r') as h:
        x_data.append(np.array(h['train']))
        y_data = np.array(h['train_label'])
#         x_val.append(np.array(h['val']))
#         y_val = np.array(h['val_label'])
        x_test.append(np.array(h['test']))

print(x_data[0].shape)
print(type(y_data[0]))
print(y_data)
x_data = np.concatenate(x_data, axis=1)
# y_train = np.concatenate(y_train, axis=0)
# x_val = np.concatenate(x_val, axis=1)
# y_val = np.concatenate(y_val, axis=0)
x_test = np.concatenate(x_test, axis=1)
print(x_data.shape)
print(len(y_data))
# print(x_val.shape)
# print(len(y_val))
print(x_test.shape)
x_test = x_test[:12500]
print(x_test.shape)
# from keras.utils.np_utils import to_categorical

# y_data = to_categorical(y_data)
# print(y_data.shape)
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

x_train, x_val, y_train, y_val = train_test_split(x_data, y_data, test_size=0.02)
(x_train, y_train) = shuffle(x_train, y_train)

print(x_train.shape)
print(y_train.shape)
print(x_val.shape)
print(y_val.shape)
from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression(multi_class='multinomial', solver='lbfgs', random_state=2017)
logreg.fit(x_train, y_train)
val_proba = logreg.predict_proba(x_val)
val_preds = logreg.predict(x_val)
print(val_proba.shape)
print(val_preds.shape)
print(val_proba[:,1].shape)
print(y_val.shape)
from sklearn.metrics import log_loss, accuracy_score
print('Val log_loss: {}'.format(log_loss(y_val, val_proba[:,1])))
val_proba_limit = np.clip(val_proba[:,1], 0.005, 0.995)
print('Val log_loss: {}'.format(log_loss(y_val, val_proba_limit)))
print('Val accuracy_score: {}'.format(accuracy_score(y_val, val_preds)))<jupyter_output>Val log_loss: 0.14095217182754533
Val log_loss: 0.10630763033081811
Val accuracy_score: 0.974<jupyter_text>Predict<jupyter_code>y_pred = logreg.predict_proba(x_test)
print(y_pred.shape)<jupyter_output>(12500, 2)<jupyter_text>**Handle y_pred prob value, limit them at 0.005 to 0.995 becourse of the infinity of function LogLoss**<jupyter_code>print(y_pred[:10])
y_pred = np.clip(y_pred, 0.005, 0.995)
print(y_pred[:10])
df2 = pd.read_csv('.\\input\\sample_submission.csv')
n_test = len(df2)
print(df2.shape)
df2.head()<jupyter_output>(12500, 2)<jupyter_text>**Need map y_pred to df2, means map [1,10,..., 2, 20,...] to [1, 2,...]**<jupyter_code>print(os.listdir('.\\input\\data_test\\test'))
# Create dictionary
dic = {}
i = 0
for img_name in os.listdir('.\\input\\data_test\\test'):
    key = int(img_name[0:-4]) # Remove .jpg
#     print(key)
    dic[key] = y_pred[i, 1]
    i=i+1
print(len(dic))
print(dic.get(1))
print(dic.get(2))
print(dic.get(3))
print(dic.get(4))
print(dic.get(5))
print(dic.get(6))
# Map y_pred to df2, means map [1,10,..., 2, 20,...] to [1, 2,...]
for i, row in df2.iterrows():
#     print(i)
#     print(row[0])
#     print(row[1])
    df2.iloc[i, 1] = dic.get(df2.iloc[i, 0])
print(df2.head(6))
df2.to_csv('.\\output\\pred.csv', index=None)
print('Done !')<jupyter_output>Done !
StarcoderdataJupyterStructuredCleanDedup