{"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\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":"from keras.models import Sequential\nfrom keras.layers import Dense , Dropout , Flatten,Conv2D,MaxPool2D\nfrom keras.optimizers import Adam \nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nimport pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"529c94bfd07a8a02962eb261d476c92f54fd6f19","scrolled":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/train.csv\")\nprint(df.shape)\nx_train=df.iloc[:,1:].values.astype('float32')\ny_train=df.iloc[:,0].values.astype('int32')\nprint(y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7b4c364a9d050806e75f22289b350a1373c27ec"},"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nsc=MinMaxScaler()\ndf = pd.read_csv(\"../input/test.csv\")\nprint(df.shape)\nx_train=x_train/255.0\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e69c2f6bcbe6aa5c29c6d69c86289379aba6bc30","scrolled":false},"cell_type":"code","source":"x_train = x_train.reshape(-1,28,28,1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67863d0cc97a28a562c33a79ae4f10ef48068ce7","scrolled":true},"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ny_train= to_categorical(y_train)\nnum_classes = y_train.shape[1]\nnum_classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"468ed55a92fb820c9a6cbb7c7499b583d78402af"},"cell_type":"code","source":"print(x_train.shape) # 2\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40f7b37da7e8d75cdcc61cbd531d5aaee559e2c8"},"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6c4af497949d139e8fe0c3e56d8f17bb23e0a04"},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        )  # randomly flip images\n\n\ndatagen.fit(x_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fddfba9a46f9314e4284fdb710e15b5c86a73697","scrolled":false},"cell_type":"code","source":"from keras.layers.advanced_activations import LeakyReLU\nfrom keras.layers import BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32,kernel_size=3,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\n\nmodel.add(Conv2D(64,kernel_size=3,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,kernel_size=3,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n\n\n\n\nmodel.fit_generator(datagen.flow(x_train,y_train, batch_size=2048),\n                              epochs = 10, validation_data = (x_val,y_val),\n                               steps_per_epoch=100\n                              )\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13aaccf82db7ad7502c01eebd539dccc8e90fd64"},"cell_type":"code","source":"x_test=df.iloc[:,:].values.astype('float32')\n\nx_test=x_test/255.0\nx_test = x_test.reshape(-1,28,28,1)\n\ny_pred=model.predict(x_test)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7dcbe1be23ab82bf9808a73c55f38f0a8f975268"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"940b100387caf253a53bce43102dab0bde981bab"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0af5321533a6a4034ea7fab0863b99d9b696f64"},"cell_type":"code","source":"predicted_classes = model.predict_classes(x_test)\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(predicted_classes)+1)),\n                         \"Label\": predicted_classes})\nsubmissions.to_csv(\"asd2.csv\", index=False, header=True)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8faf574efa1e8cc226618261f3d67bb3a65b12fc"},"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}