Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
model.fit(train_X, train_Y, batch_size=64, epochs=5, validation_data=(valid_X, valid_Y))<predict_on_test>
labels=['dog','cat']
pred_glove_val_y = model.predict([valid_X], batch_size=64, verbose=1)
 print(f'F1 Score {f1_score(valid_Y,(pred_glove_val_y>0.6 ).astype(int)) }' )<predict_on_test>
create_directory(labels =labels)
pred_glove_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<data_type_conversions>
def move_files(train_path,test_path):
print('Moving Training Files.. ')
 time.sleep(1)
 for i in tqdm(os.listdir(train_path)) :
if 'dog' in i:
shutil.copyfile(train_path+i,'.. /data/train/dog/'+i)
 elif 'cat' in i:
shutil.copyfile(train_path+i,'.. /data/train/cat/'+i)
 else:
print('unkown File', i)
 print('Moving Testing Files.. ')
 time.sleep(1)
 for i in tqdm(os.listdir(test_path)) :
shutil.copyfile(test_path+i,'.. /data/test/test_data/'+i)
sample['target']=(pred_glove_test_y>0.6 ).astype(int )<count_values>
move_files('.. /input/train/','.. /input/test/' )
sample['target'].value_counts()<save_to_csv>
def get_num_files(path):
if not os.path.exists(path):
return 0
return sum([len(files)for r, d, files in os.walk(path)] )
sample.to_csv('sample_submission.csv',index=False )<set_options>
Image_width,Image_height = 299,299
batch_size=50
Number_FC_Neurons=1024
train_dir = '.. /data/train/'
total_samples = get_num_files(train_dir)
 val_split=0.3
n_train=total_samples*(1-val_split)
 n_val=total_samples*val_split
num_classes = len(labels)
 print(n_train,n_val)
 gc.collect()
%matplotlib inline
np.random.seed(17)
 sns.set(style='white', context='notebook', palette='deep')
 for dirname, _, filenames in os.walk('/kaggle/input/digit-recognizer'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
train_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),batch_size=batch_size,seed=42,subset='training',shuffle=True,class_mode='categorical')
 val_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),batch_size=batch_size,seed=42,subset='validation',shuffle=True,class_mode='categorical' )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
InceptionV3_base_model = InceptionV3(weights='imagenet', include_top=False)
 print('Inception v3 base model without last FC loaded' )
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 sns.countplot(Y_train)
 plt.show()<feature_engineering>
x = InceptionV3_base_model.output
x_pool = GlobalAveragePooling2D()(x)
 x_dense = Dense(Number_FC_Neurons,activation='relu' )(x_pool)
 final_pred = Dense(num_classes,activation='softmax' )(x_dense)
 model = Model(inputs=InceptionV3_base_model.input,outputs=final_pred)
 model.summary()
X_train = X_train / 255.0
test = test / 255.0<train_model>
cb_checkpoint = ModelCheckpoint(filepath = '.. /working/best.hd5', monitor = 'val_loss', save_best_only = True, mode = 'auto',)
 cb_stopping = EarlyStopping(monitor='val_loss',patience=10,mode=min,restore_best_weights=True)
 cb_tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True,write_images=False)
 my_callback=[cb_stopping, cb_checkpoint,cb_tensorboard]
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>

 layer_to_Freeze=172
for layer in model.layers[:layer_to_Freeze]:
layer.trainable =False
for layer in model.layers[layer_to_Freeze:]:
layer.trainable=True
sgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)
 model.compile(optimizer=sgd,loss='binary_crossentropy',metrics=['accuracy'])
Y_train = to_categorical(Y_train, num_classes = 10 )<define_variables>
history_transfer_learning = model.fit_generator(train_generator,epochs=30,
steps_per_epoch=n_train//batch_size,
validation_data=val_generator,
validation_steps=n_val//batch_size,
verbose=1,
callbacks=my_callback,
class_weight='auto')
random_seed = 2<split>
print('Loading best model...')
 best_model = load_model('.. /working/best.hd5')
 print('Best model loaded!')
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )<choose_model_class>
gc.collect()
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.summary()<train_model>
score = best_model.evaluate_generator(val_generator,verbose=1)
 print('Test loss: ', score[0])
 print('Test accuracy', score[1])
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image("model.png" )<choose_model_class>
test_dir='.. /data/test/'
model.compile(optimizer = 'nadam' , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
test_image_gen = ImageDataGenerator(rescale=1/255)
 test_generator = test_image_gen.flow_from_directory(test_dir,target_size=(Image_width,Image_height),batch_size=1,seed=42,class_mode=None,shuffle=False )
callbacks_list = [
ReduceLROnPlateau(
 monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=1e-05),
ModelCheckpoint(
 filepath='MNIST_CNN_model.h5',
monitor='val_accuracy',
save_best_only=True 
)]<define_variables>
y_pred = model.predict_generator(generator=test_generator,verbose=1 )
epochs = 50
batch_size = 512<init_hyperparams>
submission = pd.DataFrame({'id':pd.Series(test_generator.filenames),'label':pd.Series(y_pred.clip(min=0.02,max=0.98)[:,1])})
 submission['id'] = submission.id.str.extract('(\d+)')
 submission['id']=pd.to_numeric(submission['id'])
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(X_train )<train_model>
submission.to_csv('DogVsCats_submission.csv',index=False )
<load_pretrained><EOS> 
len(model.layers )