Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<import_modules>
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os
GlobalAveragePooling2D, Conv2D, BatchNormalization, Dropout
INPUT_DIR = '.. /input'
EMB_SIZE = 8
BATCH_SIZE = 1024
N_FOLDS = 2
N_ITER = 50
SEED = 32<compute_test_metric>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
def _all_diffs(a, b):

 return tf.expand_dims(a, axis=1)- tf.expand_dims(b, axis=0)
 def _cdist(a, b, metric='euclidean'):

 with tf.name_scope("_cdist"):
diffs = _all_diffs(a, b)
 if metric == 'sqeuclidean':
return tf.reduce_sum(tf.square(diffs), axis=-1)
 elif metric == 'euclidean':
return tf.sqrt(tf.reduce_sum(tf.square(diffs), axis=-1)+ 1e-12)
 elif metric == 'cityblock':
return tf.reduce_sum(tf.abs(diffs), axis=-1)
 else:
raise NotImplementedError(
 'The following metric is not implemented by `_cdist` yet: {}'.format(metric))
_cdist.supported_metrics = [
'euclidean',
'sqeuclidean',
'cityblock',
]
def _get_at_indices(tensor, indices):

 counter = tf.range(tf.shape(indices, out_type=indices.dtype)[0])
 return tf.gather_nd(tensor, tf.stack(( counter, indices), -1))
def batch_hard_loss(features, pids, metric='euclidean', margin=0.1):

 with tf.name_scope("batch_hard_loss"):
dists = _cdist(features, features, metric=metric)
 pids = tf.argmax(pids, axis=1)
 exp_dims0 = tf.expand_dims(pids, axis=0)
 exp_dims1 = tf.expand_dims(pids, axis=1)
 same_identity_mask = tf.equal(exp_dims1, exp_dims0)
 negative_mask = tf.logical_not(same_identity_mask)
 positive_mask = tf.logical_xor(same_identity_mask,
tf.eye(tf.shape(pids)[0], dtype=tf.bool))
furthest_positive = tf.reduce_max(dists*tf.cast(positive_mask, tf.float32), axis=1)
 closest_negative = tf.reduce_min(dists + 1e5*tf.cast(same_identity_mask, tf.float32), axis=1)
 diff = furthest_positive - closest_negative
if isinstance(margin, numbers.Real):
diff = tf.maximum(diff + margin, 0.0)
 elif margin == 'soft':
diff = tf.nn.softplus(diff)
 elif margin is None:
pass
else:
raise NotImplementedError('The margin {} is not implemented in batch_hard_loss'.format(margin))
return diff
def triplet_loss(labels, features):
return tf.reduce_mean(batch_hard_loss(features, labels, margin=0.2))<compute_test_metric>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
def f1(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 precision = true_positives /(predicted_positives + K.epsilon())
 return 2 *(( precision * recall)/(precision + recall + K.epsilon()))<load_from_csv>
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )
df_train = pd.read_csv(os.path.join(INPUT_DIR, 'train.csv'))
df_test = pd.read_csv(os.path.join(INPUT_DIR, 'test.csv'))
print(df_train.head() )<categorify>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
x_train = df_train.iloc[:,1:].values.astype('float32')/ 255.
 x_test = df_test.values.astype('float32')/ 255.
 xc_train = np.reshape(x_train,(len(x_train), 28, 28, 1))
xc_test = np.reshape(x_test,(len(x_test), 28, 28, 1))
y_train = df_train.label.values
yc_train = to_categorical(y_train)
 input_size = output_size = x_train.shape[1]
input_csize = output_csize = xc_train.shape[1]
print(xc_train[:5], xc_test[:5])
<choose_model_class>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
def base_network(model_type='triplet', input_shape=input_csize):

 if model_type == 'autoencoder':
pass
elif model_type == 'triplet':
model = Sequential([
Conv2D(filters=64, kernel_size=(3, 3), padding='same', input_shape=(input_csize, input_csize, 1,), activation='relu'),
Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu'),
Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu'),
Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu'),
BatchNormalization() ,
MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'),
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.25),
Dense(10, name='embeddings', activation='softmax'),
])
 return model<define_variables>
train_EB1 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB1.npy')
 test_EB1 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB1.npy' )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(xc_train )<train_on_grid>
X_train, X_val, y_train, y_val = train_test_split(train_EB1, target, test_size=0.1, random_state=42 )
yfull_test = []
skf = StratifiedKFold(n_splits=N_FOLDS, random_state=SEED, shuffle=True)
 print(len(xc_train), len(y_train))
for i,(train_index, val_index)in enumerate(skf.split(xc_train, y_train)) :
triplet_model = base_network()
triplet_model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),
loss=['categorical_crossentropy'], metrics=[f1])
 weights_path = os.path.join('.', f'w{i}.h5')
 callbacks=[
ReduceLROnPlateau(monitor='val_f1', patience=3, verbose=1, factor=0.5, min_lr=0.00001),
ModelCheckpoint(weights_path, monitor='val_f1', mode='max', save_best_only=True, verbose=0)
 ]
xb_train = xc_train[train_index]
yb_train = yc_train[train_index]
xb_val = xc_train[val_index]
yb_val = yc_train[val_index]
history = triplet_model.fit_generator(
 datagen.flow(xb_train, yb_train, batch_size=BATCH_SIZE),
validation_data=datagen.flow(xb_val, yb_val, batch_size=BATCH_SIZE),
validation_steps=xb_val.shape[0] // BATCH_SIZE,
verbose=2,
epochs=N_ITER,
steps_per_epoch=xb_train.shape[0] // BATCH_SIZE,
shuffle=True,
callbacks=callbacks)
 if os.path.isfile(weights_path):
triplet_model.load_weights(weights_path)
 print(fbeta_score(yb_val, np.array(triplet_model.predict(xb_val, batch_size=128, verbose=2)) > 0.2, beta=2, average='samples'))
yfull_test.append(triplet_model.predict(xc_test, batch_size=128, verbose=2)) 
<count_unique_values>
%%time
lr = LogisticRegression(C=0.005, max_iter=10000)
 lr.fit(X_train, y_train )
pred = np.array(yfull_test)
 pred = np.argmax(pred, axis=2)
 values, counts = np.unique(pred, axis=0, return_counts=True)
 pred = values[np.argmax(counts)]
print(pred.shape )<train_on_grid>
val_preds = lr.predict_proba(X_val)[:,1]
<save_to_csv>
roc_auc_score(y_val, val_preds )
submission = pd.DataFrame({'ImageId': range(1, pred.shape[0]+1), 'Label': pred})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
log_loss(y_val, val_preds )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
test_preds = lr.predict_proba(test_EB1)[:,1]
<categorify><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()