Kaggle competition: Digit Recognizer
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator as Imgen
from keras.models import Sequential,load_model
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,GlobalAveragePooling2D,Dropout
from keras.preprocessing import image
import cv2
import pickle<load_from_csv>
import os
import numpy as np
import glob
import shutil
import pandas as pd;
import matplotlib.pyplot as plt
labels = pd.read_csv(".. /input/dog-breed-identification/labels.csv")
 sample_sub = pd.read_csv(".. /input/dog-breed-identification/sample_submission.csv")
 labels.head()<feature_engineering>
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras import regularizers
from keras.layers.normalization import BatchNormalization
def addjpg(id):
return id+".jpg"<feature_engineering>
train_full = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test= pd.read_csv(".. /input/digit-recognizer/test.csv")
 train_full.head()
labels['id'] = labels['id'].apply(addjpg)
 sample_sub['id'] = sample_sub['id'].apply(addjpg )<choose_model_class>
train = train_full.sample(frac=0.8, random_state=0)
 val = train_full.drop(train.index )
data = Imgen(preprocessing_function=keras.applications.nasnet.preprocess_input,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.1
)<prepare_x_and_y>
X_full =(train_full.iloc[:,1:].values ).astype('float32')
 y_full = train_full.iloc[:,0].values.astype('int32')
 X_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 X_val =(val.iloc[:,1:].values ).astype('float32')
 y_val = val.iloc[:,0].values.astype('int32')
 X_test = test.values.astype('float32' )
train_ds = data.flow_from_dataframe(
 labels,
directory = '.. /input/dog-breed-identification/train',
x_col = 'id',
y_col = 'breed',
subset="training",
color_mode="rgb",
target_size =(331,331),
class_mode="categorical",
batch_size=32,
shuffle=True,
seed=123, 
)
 val_ds = data.flow_from_dataframe(
 labels,
directory = '.. /input/dog-breed-identification/train',
x_col = 'id',
y_col = 'breed',
subset="validation",
color_mode="rgb",
target_size =(331,331),
class_mode="categorical",
batch_size=32,
shuffle=True,
seed=123,
)<prepare_x_and_y>
BATCH_SIZE = 100
x,y = next(train_ds)
 x.shape<import_modules>
image_gen_train = ImageDataGenerator(
 rescale=1./255,
rotation_range=20,
horizontal_flip=False,
fill_mode='nearest')
 train_data_gen = image_gen_train.flow(X_train, y_train, batch_size=BATCH_SIZE,
shuffle=True)
 full_data_gen = image_gen_train.flow(X_full, y_full, batch_size=BATCH_SIZE,
shuffle=True )
from keras.applications.nasnet import NASNetLarge
from keras.applications.inception_resnet_v2 import InceptionResNetV2<choose_model_class>
image_gen_val = ImageDataGenerator(rescale=1./255)
 val_data_gen = image_gen_val.flow(X_val, y_val, batch_size=BATCH_SIZE )
base_model = InceptionResNetV2(include_top=False,
weights='imagenet',
input_shape=(331,331,3)
)
 base_model.trainable = False<choose_model_class>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3,3), activation='relu', input_shape=(28, 28, 1)) ,
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.Conv2D(32,(3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64,(3,3), activation='relu'),
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.Conv2D(64,(3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
 ] )
model = Sequential([
base_model,
GlobalAveragePooling2D() ,
Dense(256,activation = 'relu'),
Dropout(0.5),
Dense(120,activation='softmax')
 ])
 model.summary()<choose_model_class>
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy() ,
metrics=['accuracy'] )
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'] )<choose_model_class>
epochs=20
history = model.fit_generator(
 train_data_gen,
steps_per_epoch=int(np.ceil(total_train / float(BATCH_SIZE))),
epochs=epochs,
validation_data=val_data_gen,
validation_steps=int(np.ceil(total_val / float(BATCH_SIZE)))
 )
my_calls = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=2),
keras.callbacks.ModelCheckpoint("Model.h5",verbose=1,save_best_only=True)]<train_model>
epochs=17
history = model.fit_generator(
 full_data_gen,
steps_per_epoch=int(np.ceil(total_full / float(BATCH_SIZE))),
epochs=epochs
)
<load_pretrained><EOS> 
predictions = model.predict_classes(X_test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("DigitsClassif.csv", index=False, header=True )