Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<save_to_csv>
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout , BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
df_submission.to_csv('submission.csv', index=False )<import_modules>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
print(tf.__version__ )<import_modules>
train_Y = np.array(train["label"])
 train_Y = np_utils.to_categorical(train_Y, num_classes = 10 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
import cv2
import PIL
import os
import pathlib
import shutil
from IPython.display import Image, display
import plotly.graph_objs as go
import plotly.graph_objects as go
from sklearn.metrics import cohen_kappa_score
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential, Model,load_model
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications import resnet50
from tensorflow.keras.applications import xception
from tensorflow.keras.applications import inception_v3
from tensorflow.keras.applications import inception_resnet_v2
from tensorflow.keras.applications import resnet_v2
from tensorflow.keras.applications import nasnet
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array
from tensorflow.keras import Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,BatchNormalization,Activation
from tensorflow.keras.layers import GlobalAveragePooling2D, Concatenate
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
from tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping, LearningRateScheduler
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers.experimental import preprocessing
import gc
import skimage.io 
<load_from_csv>
train_X = np.array(train.iloc[:, 1:])
 test_X = np.array(test )
train_dir = '.. /input/dog-breed-dataset-with-subdirectories-by-class/data/train'
test_dir = '.. /input/dog-breed-dataset-with-subdirectories-by-class/data/test'
train_labels = pd.read_csv('.. /input/dog-breed-identification/labels.csv', index_col = 'id')
 submission=pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' )<categorify>
train_X, valid_X, train_Y, valid_Y = train_test_split(train_X, train_Y, shuffle=True, test_size = 0.1, random_state=2, stratify=train_Y )
target, dog_breeds = pd.factorize(train_labels['breed'], sort = True)
 train_labels['target'] = target
print(dog_breeds )<count_values>
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(train_X )
train_labels['breed'].value_counts()<define_variables>
nets = 5
model = [0] *nets
for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[i].add(BatchNormalization())
 model[i].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.4))
model[i].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.4))
model[i].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Flatten())
 model[i].add(Dropout(0.4))
model[i].add(Dense(10, activation='softmax'))
model[i].compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])
N_EPOCHS = 50
BATCH_SIZE = 128
IMG_HEIGHT = 331
IMG_WIDTH = 331<create_dataframe>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 45
for j in range(nets):
print("CNN ",j+1)
 history[j] = model[j].fit_generator(datagen.flow(train_X, train_Y, batch_size=64),
epochs = epochs, steps_per_epoch = train_X.shape[0]//64,
validation_data =(valid_X, valid_Y), callbacks=[annealer], verbose=1)
 print("CNN {0:d} DONE".format(j+1))
train_ds = image_dataset_from_directory(
 directory = train_dir,
labels = 'inferred',
label_mode='int',
batch_size=BATCH_SIZE,
image_size=(IMG_HEIGHT, IMG_WIDTH),
shuffle = True,
seed=1234,
validation_split=0.1,
subset="training",
)<define_variables>
results = np.zeros(( test_X.shape[0],10))
for j in range(nets):
results = results + model[j].predict(test_X)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )