Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
X_train,X_test,y_train,y_test=train_test_split(X_train,y_train,test_size=0.10,random_state=42 )<define_variables>
from keras.layers import Dense, Flatten, Dropout, Lambda, Input, Concatenate, concatenate
from keras.models import Model
from keras.applications import *
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint
from keras import regularizers
batch_size=128
num_classes=10
epochs=20
inputshape=(28,28,1 )<choose_model_class>
filenames = os.listdir(".. /input/train/train")
 labels = []
for file in filenames:
category = file.split('.')[0]
if category == 'cat':
labels.append('cat')
 else:
labels.append('dog' )
model=Sequential()
model.add(Conv2D(32,kernel_size=(5,5),activation="relu",input_shape=inputshape))
model.add(Conv2D(64,(3,3),activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(128,kernel_size=(5,5),activation="relu"))
model.add(Conv2D(128,(3,3),activation="relu"))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256,activation="relu"))
model.add(Dense(128,activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))<choose_model_class>
df = pd.DataFrame({
'filename': filenames,
'label': labels
})
 train_df, validation_df = train_test_split(df, test_size=0.1, random_state = 42)
 train_df = train_df.reset_index(drop=True)
 validation_df = validation_df.reset_index(drop=True)
reduce_learning_rate = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 3, verbose = 1, factor = 0.3, min_lr = 0.00001)
 checkpoint = ModelCheckpoint('save_weights.h5', monitor = 'val_accuracy', verbose = 1, save_best_only = True, mode = 'max')
 early_stopping = EarlyStopping(monitor = 'val_loss', min_delta = 1e-10, patience = 10, verbose = 1, restore_best_weights = True)
 callbacks = [reduce_learning_rate, checkpoint, early_stopping]<train_model>
batch_size = 64
train_num = len(train_df)
 validation_num = len(validation_df )
model.fit(X_train,y_train,batch_size=batch_size,epochs=epochs,validation_data=(X_test,y_test),callbacks=callbacks)
 accuracy=model.evaluate(X_test,y_test )<save_to_csv>
def two_image_generator(generator, df, directory, batch_size,
x_col = 'filename', y_col = None, model = None, shuffle = False,
img_size1 =(224, 224), img_size2 =(299,299)) :
gen1 = generator.flow_from_dataframe(
 df,
directory,
x_col = x_col,
y_col = y_col,
target_size = img_size1,
class_mode = model,
batch_size = batch_size,
shuffle = shuffle,
seed = 1)
 gen2 = generator.flow_from_dataframe(
 df,
directory,
x_col = x_col,
y_col = y_col,
target_size = img_size2,
class_mode = model,
batch_size = batch_size,
shuffle = shuffle,
seed = 1)
 while True:
X1i = gen1.next()
X2i = gen2.next()
if y_col:
yield [X1i[0], X2i[0]], X1i[1]
else:
yield [X1i, X2i]
pred = model.predict_classes(test)
 res = pd.DataFrame({"ImageId":list(range(1,28001)) ,"Label":pred})
 res.to_csv("output.csv", index = False )<set_options>
%matplotlib inline
tqdm.pandas() 
<load_from_csv>
train_aug_datagen = ImageDataGenerator(
 rotation_range = 20,
shear_range = 0.1,
zoom_range = 0.2,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = True 
)
 train_generator = two_image_generator(train_aug_datagen, train_df, '.. /input/train/train/',
batch_size = batch_size, y_col = 'label',
model = 'binary', shuffle = True )
train=pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test=pd.read_csv(".. /input/nlp-getting-started/test.csv")
 sample=pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv" )<categorify>
validation_datagen = ImageDataGenerator()
validation_generator = two_image_generator(validation_datagen, validation_df,
'.. /input/train/train/', batch_size = batch_size,
y_col = 'label',model = 'binary', shuffle = True )
def load_embedding(file):
def get_coefs(word,*arr):
return word,np.array(arr,dtype='float32')
 embeddings_index=dict(get_coefs(*o.split(" ")) for o in open(file)if len(o)>100)
 return embeddings_index 
<define_variables>
def create_base_model(MODEL, img_size, lambda_fun = None):
inp = Input(shape =(img_size[0], img_size[1], 3))
x = inp
if lambda_fun:
x = Lambda(lambda_fun )(x)
 base_model = MODEL(input_tensor = x, weights = 'imagenet', include_top = False, pooling = 'avg')
 model = Model(inp, base_model.output)
 return model
glove='.. /input/glovetwitter27b100dtxt/glove.twitter.27B.200d.txt'<load_pretrained>
model1 = create_base_model(vgg16.VGG16,(224, 224), vgg16.preprocess_input)
 model2 = create_base_model(resnet50.ResNet50,(224, 224), resnet50.preprocess_input)
 model3 = create_base_model(inception_v3.InceptionV3,(299, 299), inception_v3.preprocess_input)
 model1.trainable = False
model2.trainable = False
model3.trainable = False
inpA = Input(shape =(224, 224, 3))
inpB = Input(shape =(299, 299, 3))
out1 = model1(inpA)
 out2 = model2(inpA)
 out3 = model3(inpB)
 x = Concatenate()([out1, out2, out3])
 x = Dropout(0.6 )(x)
 x = Dense(1, activation='sigmoid' )(x)
 multiple_pretained_model = Model([inpA, inpB], x)
 multiple_pretained_model.compile(loss = 'binary_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
 multiple_pretained_model.summary()
embed_glove = load_embedding(glove )<categorify>
checkpointer = ModelCheckpoint(filepath='dogcat.weights.best.hdf5', verbose=1,
save_best_only=True, save_weights_only=True )
def build_vocab(text):
vocab={}
for s in tqdm(text):
for word in s:
try:
vocab[word]+=1
except KeyError:
vocab[word]=1
return vocab 
<categorify>
multiple_pretained_model.fit_generator(
 train_generator,
epochs = 5,
steps_per_epoch = train_num // batch_size,
validation_data = validation_generator,
validation_steps = validation_num // batch_size,
verbose = 1,
callbacks = [checkpointer]
)
def check_coverage(vocab,embedding_index):
known_words={}
unknown_words={}
nb_known_words=0
nb_unknown_words=0
for word in tqdm(vocab.keys()):
try:
known_words[word]=embedding_index[word]
nb_known_words+=vocab[word]
except:
unknown_words[word]=vocab[word]
nb_unknown_words+=vocab[word]
print('Found embeddings for {:.3%} of vocab'.format(len(known_words)/ len(vocab)))
 print('Found embeddings for {:.3%} of all text'.format(nb_known_words /(nb_known_words + nb_unknown_words)))
 unknown_words = sorted(unknown_words.items() , key=operator.itemgetter(1)) [::-1]
return unknown_words<feature_engineering>
multiple_pretained_model.load_weights('dogcat.weights.best.hdf5' )
sentences = train["text"].progress_apply(lambda x: x.split() ).values
vocab = build_vocab(sentences)
 print({k: vocab[k] for k in list(vocab)[:5]})
<compute_test_metric>
test_filenames = os.listdir(".. /input/test/test")
 test_df = pd.DataFrame({
'filename': test_filenames
})
 num_test = len(test_df)
 test_datagen = ImageDataGenerator()
test_generator = two_image_generator(test_datagen, test_df, '.. /input/test/test/', batch_size = batch_size )
oov=check_coverage(vocab,embed_glove )<define_variables>
prediction = multiple_pretained_model.predict_generator(test_generator,
steps=np.ceil(num_test/batch_size))
prediction = prediction.clip(min = 0.005, max = 0.995 )
<define_variables><EOS> 
submission_df = pd.read_csv('.. /input/sample_submission.csv')
 for i, fname in enumerate(test_filenames):
index = int(fname[fname.rfind('/')+1:fname.rfind('.')])
 submission_df.at[index-1, 'label'] = prediction[i]
submission_df.to_csv('submission.csv', index=False )