{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n#import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential, Model\nfrom keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization, Input\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau,EarlyStopping\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c892c787c05558ab7ab1ea3945b7a15f8e0579bd"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ef1707503200fb1150b16c789c96f140518c34a"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07d94056d6c45ec1c334a42ebb1a148b7bd6a244"},"cell_type":"code","source":"Y_train=train[\"label\"]\nX_train = train.drop(labels = [\"label\"],axis = 1)\n\nX_train = X_train / 255.0\ntest = test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48a8edc7fc7688ea5183dcff71e46a6c95128719"},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa0ae7ab635a9c916e5be67d67ce2d26a15c782d"},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"313648696c6b5e705c93bb9296c004bbe7816a68"},"cell_type":"code","source":"#ALTERNATIVE METHOD:\n\n# train = train.values\n# np.random.shuffle(train)\n\n# X = train[:, 1:].reshape(-1, 28, 28, 1) / 255.0\n# Y = train[:, 0].astype(np.int32)\n\n#test = test.values\n#test = test.reshape[-1,28,28,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"f9f9d67220e05f44a0286e32c68720a0f42d8ecc"},"cell_type":"code","source":"Y_train.value_counts() #pandas","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"112f90971a952ac91ba02dbed1087c729a51fbfe"},"cell_type":"code","source":"Y_train = to_categorical(Y_train, num_classes = 10)\n# keras.utils.to_categorical(y, num_classes=None, dtype='float32')\n# https://keras.io/utils/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d79f61dc3b9be7560e34ce5cc122c291d252e1d1"},"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17ac9d86108b4d9e56d660a5fa2e65508239de1e"},"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71df2c68769b4a365e4acec73baf576ff0b677c3"},"cell_type":"code","source":"image=Input(shape=[28,28,1])\n\nx= Conv2D(128, (3, 3), padding='same', activation='relu')(image)\nx=MaxPooling2D()(x)\n\nx= Conv2D(128, (3, 3), padding='same')(x)\nx=BatchNormalization()(x)\nx=Activation('relu')(x)\nx=MaxPooling2D(strides = (1,1))(x)\n\nx= Conv2D(128, (3, 3), padding='same')(x)\nx=BatchNormalization()(x)\nx=Activation('relu')(x)\nx=MaxPooling2D()(x)\n\nx=Flatten()(x)\n\nx= Dense(2000, activation = 'relu')(x)\nx= Dropout(0.5, noise_shape=None, seed=None)(x)\nx= Dense(1000, activation = 'relu')(x)\nx= Dropout(0.3, noise_shape=None, seed=None)(x)\nx= Dense(250, activation = 'relu')(x)\nx= Dropout(0.3, noise_shape=None, seed=None)(x)\nout = Dense(10,activation = 'softmax')(x)\n\nmodel = Model(image,out )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ad655ab9cc37ec71b0051ff26b2dde89b394836"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb00164f97a6e4d38e2f9167a11102a32fcdc37d"},"cell_type":"code","source":"model.compile(\n  loss='categorical_crossentropy',\n  optimizer='Adam',\n  metrics=['accuracy']\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"591fb3b0a45af6f0e1f084ebb5a3466952a3891e"},"cell_type":"code","source":"#keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, \n#samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, \n#zca_whitening=False, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0,\n#height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0, \n#channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, \n#rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None)\n# Image_generator = ImageDataGenerator(rotation_range=0, \n#                                      width_shift_range=0.0,\n#                                      height_shift_range=0.0,\n#                                      zoom_range=0.0,\n#                                      horizontal_flip=False, \n#                                      vertical_flip=False,\n#                                      #validation_split=0.15\n#                                     )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3fe95ea13c393cf7002b436e89c870857913b8d"},"cell_type":"code","source":"# Image_generator.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6495f50eb97ca3e6069afa23e9ddf8525ce94e28"},"cell_type":"code","source":"#flow(x, y=None, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, \n#save_prefix='', save_format='png', subset=None)\n#train_generator = \n#valid_generator = ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"347cc98ef79498f7fe0bed8ae99154921e52ea69"},"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1,\n                              patience=3, min_lr=0.0001)\nearly_stop = EarlyStopping(monitor='val_loss', patience=5,restore_best_weights=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"030182583d3f3db932f6d4929ddbc7daa261b620"},"cell_type":"code","source":"# r = model.fit_generator(Image_generator.flow(X_train, Y_train, batch_size=32),\n#                        #validation_data = valid_generator,\n#                         validation_data =(X_val,Y_val),\n#                         steps_per_epoch=X_train.shape[0]// 32,\n#                         epochs=15,\n#                         callbacks = [reduce_lr],\n#                         verbose=1,\n#                         validation_steps=X_val.shape[0]//32,\n#                          )\nr = model.fit(X_train,Y_train,batch_size = 32,\n              epochs= 10,\n             verbose = 1,\n              callbacks = [reduce_lr,early_stop],\n             validation_data = (X_val,Y_val)\n             )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd844297815ed795c80de17ddb710d105cb80712"},"cell_type":"code","source":"plt.plot(r.history['loss'], label='loss')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef1dfc77dbbaf55e13ffb15ce068d6be91dc0802"},"cell_type":"code","source":"plt.plot(r.history['acc'], color='b', label=\"Training accuracy\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"427f0521d880408500ac205393cc3ea8abc467a6"},"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5458c448be81fbf8310393a270635226981d99c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c60b0634e161f5500306e67a41bcbecb61acb05e"},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_result.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a279a7d532b0eb082e9dd16697bc83673586371f"},"cell_type":"code","source":"!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31ad02c04ea31c1eb770fc2af102ad4fc5f702c2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}