{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":81127,"databundleVersionId":8844785,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from tensorflow import keras\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras import backend as K\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nimport pandas as pd\nfrom tensorflow.keras.layers import BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:29.094725Z","iopub.execute_input":"2024-06-18T16:46:29.095164Z","iopub.status.idle":"2024-06-18T16:46:29.100907Z","shell.execute_reply.started":"2024-06-18T16:46:29.095134Z","shell.execute_reply":"2024-06-18T16:46:29.099771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.load('/kaggle/input/fds-summer-2024-data-challenger/train-imgs.npz')['arr_0']\ntarget = np.load('/kaggle/input/fds-summer-2024-data-challenger/train-labels.npz')['arr_0']\nY = target\nX = data.reshape(data.shape[0],28,28,1)\nX_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.05, random_state=42)\nprint(data.shape)\n\ntrain_images = X_train\ntrain_labels = Y_train\ntest_images = X_val\ntest_labels = Y_val","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:34.081249Z","iopub.execute_input":"2024-06-18T16:47:34.082082Z","iopub.status.idle":"2024-06-18T16:47:35.186550Z","shell.execute_reply.started":"2024-06-18T16:47:34.082048Z","shell.execute_reply":"2024-06-18T16:47:35.185640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, Y_train.shape)\nprint(X_val.shape, Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:35.541345Z","iopub.execute_input":"2024-06-18T16:47:35.541860Z","iopub.status.idle":"2024-06-18T16:47:35.546342Z","shell.execute_reply.started":"2024-06-18T16:47:35.541834Z","shell.execute_reply":"2024-06-18T16:47:35.545509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if K.image_data_format() == \"channels_first\":\n#   train_images = train_images.reshape(train_images.shape[0], 1,28,28)\n#   test_images = test_images.reshape(test_images.shape[0], 1,28,28)\n#   shape = (1,28,28)\n# else:\n#   train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)\n#   test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)\n#   shape = (28,28,1)\n\n# train_images = train_images/255.0\n\n# test_images = test_images / 255.0\n\n# datagen = ImageDataGenerator()\n# datagen.fit(train_images)","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:35.965225Z","iopub.execute_input":"2024-06-18T16:47:35.965534Z","iopub.status.idle":"2024-06-18T16:47:35.970090Z","shell.execute_reply.started":"2024-06-18T16:47:35.965507Z","shell.execute_reply":"2024-06-18T16:47:35.969203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n\tkeras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28,28,1)),\n# \tkeras.layers.Conv2D(64, (3,3), activation='relu'),\n    keras.layers.Conv2D(64, (3,3),padding ='same', activation='relu'),\n\tkeras.layers.MaxPooling2D(2,2),\n\tkeras.layers.Conv2D(128, (3,3), activation='relu'),\n    keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'),\n\tkeras.layers.MaxPooling2D(2,2),\n  keras.layers.Flatten(),\n#   keras.layers.Dropout(0.5),\n  keras.layers.Dense(512, activation='relu'),\n  keras.layers.Dense(49, activation=\"softmax\")\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:36.332419Z","iopub.execute_input":"2024-06-18T16:47:36.333115Z","iopub.status.idle":"2024-06-18T16:47:36.404584Z","shell.execute_reply.started":"2024-06-18T16:47:36.333089Z","shell.execute_reply":"2024-06-18T16:47:36.403806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss=\"sparse_categorical_crossentropy\", metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:36.581262Z","iopub.execute_input":"2024-06-18T16:47:36.581554Z","iopub.status.idle":"2024-06-18T16:47:36.590550Z","shell.execute_reply.started":"2024-06-18T16:47:36.581529Z","shell.execute_reply":"2024-06-18T16:47:36.589744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fit(datagen.flow(train_images,train_labels,shuffle=True),epochs=1,validation_data=(test_images,test_labels),callbacks = [keras.callbacks.EarlyStopping(patience=8,verbose=1,restore_best_weights=True),keras.callbacks.ReduceLROnPlateau(factor=0.5,patience=3,verbose=1)])\n\nmodel.fit(X, Y, batch_size=32, epochs=10)\n#test_loss, test_acc = model.evaluate(test_images2, test_labels)\n#print(\"Accuracy: \", test_acc)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:47:37.374324Z","iopub.execute_input":"2024-06-18T16:47:37.375228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"kuzushiji30.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:31.309404Z","iopub.status.idle":"2024-06-18T16:46:31.309710Z","shell.execute_reply.started":"2024-06-18T16:46:31.309557Z","shell.execute_reply":"2024-06-18T16:46:31.309570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_X = np.load('/kaggle/input/fds-summer-2024-data-challenger/test-imgs.npz')['arr_0']\nval_X = val_X.reshape(val_X.shape[0], 28,28, 1)\nprint(val_X.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:31.311021Z","iopub.status.idle":"2024-06-18T16:46:31.311440Z","shell.execute_reply.started":"2024-06-18T16:46:31.311224Z","shell.execute_reply":"2024-06-18T16:46:31.311242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(val_X)\npredict = np.argmax(predict, axis=1)\npredict = pd.DataFrame(predict)\npredict = predict.reset_index()\npredict.columns = ['id', 'xyz']\npredict\npredict.to_csv('result.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:31.312675Z","iopub.status.idle":"2024-06-18T16:46:31.313140Z","shell.execute_reply.started":"2024-06-18T16:46:31.312894Z","shell.execute_reply":"2024-06-18T16:46:31.312919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:31.314359Z","iopub.status.idle":"2024-06-18T16:46:31.314805Z","shell.execute_reply.started":"2024-06-18T16:46:31.314570Z","shell.execute_reply":"2024-06-18T16:46:31.314590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict['xyz'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-18T16:46:31.315975Z","iopub.status.idle":"2024-06-18T16:46:31.316328Z","shell.execute_reply.started":"2024-06-18T16:46:31.316167Z","shell.execute_reply":"2024-06-18T16:46:31.316180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}