Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
%%time
x_models = []
labels = []
for idx in glob('*.h5'):
model = BLSTM()
model.load_weights(idx)
 x_models.append(model)
 for idx in x_models:
preds = idx.predict_classes(test_sentences)
 labels.append(preds)
 labels = scipy.stats.mode(labels)[0]
labels = np.squeeze(labels )<save_to_csv>
import numpy as np
import pandas as pd
import cudf
import cupy as cp
from cuml.svm import SVC
from sklearn.model_selection import KFold, train_test_split
from sklearn.metrics import roc_auc_score, log_loss
submission.target = labels
submission.to_csv("submission.csv", index=False)
 submission.target.value_counts().plot.bar() ;<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
import tensorflow as tf
import numpy as np
import pandas as pd
import random
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D<load_from_csv>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
train_df = pd.read_csv('.. /input/train.csv' )<drop_column>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
label_df = train_df.label
train_df = train_df.drop('label', axis=1 )<split>
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' )
DS_SIZE = len(train_df)
 BATCH_SIZE = 64
x_train, x_val, y_train, y_val = train_test_split(
 np.reshape(train_df.values,(DS_SIZE, 28, 28, 1)) ,
label_df.values,
test_size=0.01,
random_state=11)
 train_gen = ImageDataGenerator(
 rescale=1./255,
shear_range=10,
zoom_range=0.1,
width_shift_range=0.05,
height_shift_range=0.05,
rotation_range=15,
brightness_range=(0.9,1.1),
fill_mode='constant' 
)
 train_gen_flow = train_gen.flow(
 x_train,
y_train,
batch_size=BATCH_SIZE
)<define_variables>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
val_gen = ImageDataGenerator(rescale=1./255)
 val_gen_flow = val_gen.flow(
 x_val,
y_val,
batch_size=BATCH_SIZE
)<choose_model_class>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
def get_model() :
model = Sequential()
model.add(Conv2D(64,(3, 3), input_shape=(28,28,1), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(rate=0.4))
model.add(Conv2D(96,(4, 4), activation='relu'))
model.add(Conv2D(128,(6, 6), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(64))
model.add(Dropout(0.3))
model.add(Dense(10, activation="softmax"))
return model
model = get_model()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc'] )<train_model>
train_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')
 test_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy' )
history = model.fit_generator(
 train_gen_flow,
steps_per_epoch=len(x_train)/BATCH_SIZE,
epochs=50,
validation_data = val_gen_flow,
validation_steps = len(x_val)/BATCH_SIZE 
)
<load_from_csv>
X_train, X_val, y_train, y_val = train_test_split(train_EB7_ns, target, test_size=0.1, random_state=42 )
train_df = pd.read_csv('.. /input/train.csv')
 raw_train_image_ds = np.reshape(
 train_df.drop('label', axis=1 ).values/255.0, 
(len(train_df), 28, 28, 1)
)
 model.evaluate(raw_train_image_ds,train_df.label.values )<load_from_csv>
%%time
svc = SVC(C= 0.07, probability=True)
 svc.fit(X_train, y_train )
test_df = pd.read_csv('.. /input/test.csv')
 test_image_ds = np.reshape(
 test_df.values/255.0, 
(len(test_df), 28, 28, 1)
)
 preds = np.round(model.predict(test_image_ds))<load_from_csv>
val_preds = svc.predict_proba(X_val)[:,1]
sample_submission_df = pd.read_csv('.. /input/sample_submission.csv')
 sample_submission_df.Label = np.argmax(preds, axis=1)
 sample_submission_df.head()<save_to_csv>
roc_auc_score(y_val, val_preds )
sample_submission_df.to_csv('submission.csv', index=False )<import_modules>
log_loss(y_val, val_preds )
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau<import_modules>
test_preds = svc.predict_proba(test_EB7_ns)[:,1]
<import_modules><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()