{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras import datasets,models,layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-04T17:28:39.484140Z","iopub.execute_input":"2023-09-04T17:28:39.485154Z","iopub.status.idle":"2023-09-04T17:28:50.026828Z","shell.execute_reply.started":"2023-09-04T17:28:39.485111Z","shell.execute_reply":"2023-09-04T17:28:50.025582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = tf.keras.datasets.cifar10","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:50.029399Z","iopub.execute_input":"2023-09-04T17:28:50.030243Z","iopub.status.idle":"2023-09-04T17:28:50.035632Z","shell.execute_reply.started":"2023-09-04T17:28:50.030198Z","shell.execute_reply":"2023-09-04T17:28:50.034354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_images, train_labels), (test_images, test_labels) = data.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:50.037724Z","iopub.execute_input":"2023-09-04T17:28:50.038393Z","iopub.status.idle":"2023-09-04T17:28:57.012749Z","shell.execute_reply.started":"2023-09-04T17:28:50.038362Z","shell.execute_reply":"2023-09-04T17:28:57.011783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.shape, test_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.014084Z","iopub.execute_input":"2023-09-04T17:28:57.014798Z","iopub.status.idle":"2023-09-04T17:28:57.021163Z","shell.execute_reply.started":"2023-09-04T17:28:57.014765Z","shell.execute_reply":"2023-09-04T17:28:57.020426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.023746Z","iopub.execute_input":"2023-09-04T17:28:57.025591Z","iopub.status.idle":"2023-09-04T17:28:57.038476Z","shell.execute_reply.started":"2023-09-04T17:28:57.025556Z","shell.execute_reply":"2023-09-04T17:28:57.036352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = train_labels.reshape(-1,)\ntrain_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.040278Z","iopub.execute_input":"2023-09-04T17:28:57.040654Z","iopub.status.idle":"2023-09-04T17:28:57.050800Z","shell.execute_reply.started":"2023-09-04T17:28:57.040627Z","shell.execute_reply":"2023-09-04T17:28:57.049790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.051849Z","iopub.execute_input":"2023-09-04T17:28:57.052178Z","iopub.status.idle":"2023-09-04T17:28:57.061522Z","shell.execute_reply.started":"2023-09-04T17:28:57.052152Z","shell.execute_reply":"2023-09-04T17:28:57.060544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[6]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.062651Z","iopub.execute_input":"2023-09-04T17:28:57.062971Z","iopub.status.idle":"2023-09-04T17:28:57.075773Z","shell.execute_reply.started":"2023-09-04T17:28:57.062945Z","shell.execute_reply":"2023-09-04T17:28:57.074989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(images, labels, index):\n    plt.figure(figsize=(15,2))\n    plt.imshow(images[index])\n    plt.xlabel(classes[labels[index]])","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.077287Z","iopub.execute_input":"2023-09-04T17:28:57.077580Z","iopub.status.idle":"2023-09-04T17:28:57.085277Z","shell.execute_reply.started":"2023-09-04T17:28:57.077556Z","shell.execute_reply":"2023-09-04T17:28:57.084385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(train_images, train_labels, 3)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.086209Z","iopub.execute_input":"2023-09-04T17:28:57.086521Z","iopub.status.idle":"2023-09-04T17:28:57.311881Z","shell.execute_reply.started":"2023-09-04T17:28:57.086496Z","shell.execute_reply":"2023-09-04T17:28:57.310872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.312991Z","iopub.execute_input":"2023-09-04T17:28:57.313277Z","iopub.status.idle":"2023-09-04T17:28:57.322006Z","shell.execute_reply.started":"2023-09-04T17:28:57.313252Z","shell.execute_reply":"2023-09-04T17:28:57.320772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[0] / 255","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.323569Z","iopub.execute_input":"2023-09-04T17:28:57.324573Z","iopub.status.idle":"2023-09-04T17:28:57.335804Z","shell.execute_reply.started":"2023-09-04T17:28:57.324542Z","shell.execute_reply":"2023-09-04T17:28:57.334604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train  = train_images / 255\ny_train = train_labels\nX_test = test_images / 255\ny_test = test_labels","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:57.337267Z","iopub.execute_input":"2023-09-04T17:28:57.337777Z","iopub.status.idle":"2023-09-04T17:28:58.072472Z","shell.execute_reply.started":"2023-09-04T17:28:57.337749Z","shell.execute_reply":"2023-09-04T17:28:58.071554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **ANN**","metadata":{}},{"cell_type":"code","source":"ann = models.Sequential([\n    layers.Flatten(input_shape=(32, 32, 3)),\n    layers.Dense(3000, activation = 'relu'),\n    layers.Dense(1000, activation = 'relu'),\n    layers.Dense(10, activation = 'sigmoid'),\n    \n])","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:58.076947Z","iopub.execute_input":"2023-09-04T17:28:58.077333Z","iopub.status.idle":"2023-09-04T17:28:58.352199Z","shell.execute_reply.started":"2023-09-04T17:28:58.077300Z","shell.execute_reply":"2023-09-04T17:28:58.351031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.compile(optimizer = 'SGD',\n            loss = 'sparse_categorical_crossentropy',\n            metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:58.353818Z","iopub.execute_input":"2023-09-04T17:28:58.354206Z","iopub.status.idle":"2023-09-04T17:28:58.374936Z","shell.execute_reply.started":"2023-09-04T17:28:58.354174Z","shell.execute_reply":"2023-09-04T17:28:58.373709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.fit(X_train, y_train, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:28:58.376526Z","iopub.execute_input":"2023-09-04T17:28:58.376872Z","iopub.status.idle":"2023-09-04T17:34:30.106726Z","shell.execute_reply.started":"2023-09-04T17:28:58.376843Z","shell.execute_reply":"2023-09-04T17:34:30.105720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:34:30.108393Z","iopub.execute_input":"2023-09-04T17:34:30.108856Z","iopub.status.idle":"2023-09-04T17:34:34.901008Z","shell.execute_reply.started":"2023-09-04T17:34:30.108816Z","shell.execute_reply":"2023-09-04T17:34:34.899912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\n\ny_pred = ann.predict(X_test)\ny_pred_classes = [np.argmax(element) for element in y_pred]\n\nprint(\"Classification report:  \\n\", classification_report(y_test, y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:34:34.902458Z","iopub.execute_input":"2023-09-04T17:34:34.903340Z","iopub.status.idle":"2023-09-04T17:34:40.915274Z","shell.execute_reply.started":"2023-09-04T17:34:34.903306Z","shell.execute_reply":"2023-09-04T17:34:40.913948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CNN**","metadata":{}},{"cell_type":"code","source":"cnn = models.Sequential([\n    #cnn\n    layers.Conv2D(filters=32, kernel_size =(3,3), activation='relu', input_shape=(32,32,3)),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(filters=64, kernel_size =(3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    \n    #Dense\n    layers.Flatten(),\n    layers.Dense(64, activation = 'relu'),\n    layers.Dense(10, activation = 'softmax'),\n    \n])","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:34:40.916805Z","iopub.execute_input":"2023-09-04T17:34:40.917196Z","iopub.status.idle":"2023-09-04T17:34:41.010801Z","shell.execute_reply.started":"2023-09-04T17:34:40.917136Z","shell.execute_reply":"2023-09-04T17:34:41.009661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer = 'adam',\n            loss = 'sparse_categorical_crossentropy',\n            metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:34:41.012271Z","iopub.execute_input":"2023-09-04T17:34:41.012631Z","iopub.status.idle":"2023-09-04T17:34:41.027679Z","shell.execute_reply.started":"2023-09-04T17:34:41.012600Z","shell.execute_reply":"2023-09-04T17:34:41.026670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train, y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:34:41.029037Z","iopub.execute_input":"2023-09-04T17:34:41.030210Z","iopub.status.idle":"2023-09-04T17:40:17.345955Z","shell.execute_reply.started":"2023-09-04T17:34:41.030173Z","shell.execute_reply":"2023-09-04T17:40:17.345134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:17.347164Z","iopub.execute_input":"2023-09-04T17:40:17.347869Z","iopub.status.idle":"2023-09-04T17:40:20.125761Z","shell.execute_reply.started":"2023-09-04T17:40:17.347837Z","shell.execute_reply":"2023-09-04T17:40:20.124539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.reshape(-1,)\n\nplot_sample(X_test, y_test, 1)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:20.129618Z","iopub.execute_input":"2023-09-04T17:40:20.130318Z","iopub.status.idle":"2023-09-04T17:40:20.327139Z","shell.execute_reply.started":"2023-09-04T17:40:20.130281Z","shell.execute_reply":"2023-09-04T17:40:20.325934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(X_test)\ny_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:20.330512Z","iopub.execute_input":"2023-09-04T17:40:20.330845Z","iopub.status.idle":"2023-09-04T17:40:23.012677Z","shell.execute_reply.started":"2023-09-04T17:40:20.330818Z","shell.execute_reply":"2023-09-04T17:40:23.011426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_classes = [np.argmax(element) for element in y_pred]\ny_pred_classes[:5]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.014348Z","iopub.execute_input":"2023-09-04T17:40:23.014713Z","iopub.status.idle":"2023-09-04T17:40:23.052273Z","shell.execute_reply.started":"2023-09-04T17:40:23.014682Z","shell.execute_reply":"2023-09-04T17:40:23.050977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:5]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.053787Z","iopub.execute_input":"2023-09-04T17:40:23.054527Z","iopub.status.idle":"2023-09-04T17:40:23.060417Z","shell.execute_reply.started":"2023-09-04T17:40:23.054495Z","shell.execute_reply":"2023-09-04T17:40:23.059340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_test, y_test, 0)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.063042Z","iopub.execute_input":"2023-09-04T17:40:23.063698Z","iopub.status.idle":"2023-09-04T17:40:23.258688Z","shell.execute_reply.started":"2023-09-04T17:40:23.063657Z","shell.execute_reply":"2023-09-04T17:40:23.257606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_pred_classes[0]]","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.260080Z","iopub.execute_input":"2023-09-04T17:40:23.260435Z","iopub.status.idle":"2023-09-04T17:40:23.267864Z","shell.execute_reply.started":"2023-09-04T17:40:23.260406Z","shell.execute_reply":"2023-09-04T17:40:23.266483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Classification report:  \\n\", classification_report(y_test, y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.269417Z","iopub.execute_input":"2023-09-04T17:40:23.269730Z","iopub.status.idle":"2023-09-04T17:40:23.307636Z","shell.execute_reply.started":"2023-09-04T17:40:23.269705Z","shell.execute_reply":"2023-09-04T17:40:23.306566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model**","metadata":{}},{"cell_type":"code","source":"# model = models.Sequential([\n#     layers.Conv2D(16, (5, 5), activation=\"relu\", input_shape=(48, 48, 3), padding=\"same\"),\n#     layers.AveragePooling2D((2, 2)),\n    \n#     layers.Conv2D(32, (4, 4), activation=\"relu\",  padding=\"same\"),\n#     layers.MaxPool2D((2, 2)),\n    \n#     layers.Conv2D(64, (3, 3), activation=\"relu\", padding=\"valid\"),\n#     layers.AveragePooling2D((2, 2)),\n    \n#     layers.Conv2D(64, (3, 3), activation=\"relu\", padding=\"same\"),\n#     layers.AveragePooling2D((2, 2)),\n    \n#     layers.Conv2D(128, (2, 2), activation=\"relu\", padding=\"same\"),\n#     layers.MaxPool2D((2, 2)),\n    \n#     layers.Conv2D(256, (2, 2), activation=\"relu\", padding=\"same\"),\n    \n#     layers.Flatten(),\n    \n#     layers.BatchNormalization(),\n#     layers.Dense(128, activation=\"sigmoid\"),\n#     layers.Activation(\"relu\"),\n#     layers.Dropout(0.5),\n    \n#     layers.Dense(32, activation=\"relu\"),\n#     layers.Dropout(0.5),\n    \n#     layers.Dense(10, activation=\"softmax\")\n    \n# ])\n\n\n\n# model.compile(optimizer=\"adam\", \n#               loss=\"categorical_crossentropy\",\n#               metrics=[\"accuracy\"])\n\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.309034Z","iopub.execute_input":"2023-09-04T17:40:23.309706Z","iopub.status.idle":"2023-09-04T17:40:23.315271Z","shell.execute_reply.started":"2023-09-04T17:40:23.309664Z","shell.execute_reply":"2023-09-04T17:40:23.314268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fit(X_train, y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.316527Z","iopub.execute_input":"2023-09-04T17:40:23.316838Z","iopub.status.idle":"2023-09-04T17:40:23.327453Z","shell.execute_reply.started":"2023-09-04T17:40:23.316810Z","shell.execute_reply":"2023-09-04T17:40:23.326383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submition = pd.DataFrame({\"id\": range(1, len(y_pred_classes)+1),\n                       \"label\": y_pred_classes})\n\nsubmition.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.328825Z","iopub.execute_input":"2023-09-04T17:40:23.329142Z","iopub.status.idle":"2023-09-04T17:40:23.381075Z","shell.execute_reply.started":"2023-09-04T17:40:23.329117Z","shell.execute_reply":"2023-09-04T17:40:23.379929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# samples = pd.read_csv(\"/kaggle/input/cifar-10/sampleSubmission.csv\", index_col=False)\n\n# samples.label= y_pred_classes\n\n# samples.replace({\"label\":labels_dict}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.382672Z","iopub.execute_input":"2023-09-04T17:40:23.383047Z","iopub.status.idle":"2023-09-04T17:40:23.389721Z","shell.execute_reply.started":"2023-09-04T17:40:23.383017Z","shell.execute_reply":"2023-09-04T17:40:23.388170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# samples.to_csv(\"../kaggle/working/\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.391354Z","iopub.execute_input":"2023-09-04T17:40:23.391947Z","iopub.status.idle":"2023-09-04T17:40:23.405637Z","shell.execute_reply.started":"2023-09-04T17:40:23.391862Z","shell.execute_reply":"2023-09-04T17:40:23.404513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submition.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T17:40:23.407370Z","iopub.execute_input":"2023-09-04T17:40:23.407762Z","iopub.status.idle":"2023-09-04T17:40:23.431401Z","shell.execute_reply.started":"2023-09-04T17:40:23.407727Z","shell.execute_reply":"2023-09-04T17:40:23.430347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}