{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-19T05:42:48.759099Z","iopub.execute_input":"2023-05-19T05:42:48.759453Z","iopub.status.idle":"2023-05-19T05:42:48.780120Z","shell.execute_reply.started":"2023-05-19T05:42:48.759418Z","shell.execute_reply":"2023-05-19T05:42:48.779242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:42:48.781921Z","iopub.execute_input":"2023-05-19T05:42:48.782316Z","iopub.status.idle":"2023-05-19T05:43:05.156092Z","shell.execute_reply.started":"2023-05-19T05:42:48.782285Z","shell.execute_reply":"2023-05-19T05:43:05.154925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import libraries\n\n# Loading and processing\nimport glob\nimport cv2\nimport py7zr\n\n# Data manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:43:05.160494Z","iopub.execute_input":"2023-05-19T05:43:05.160843Z","iopub.status.idle":"2023-05-19T05:43:12.118203Z","shell.execute_reply.started":"2023-05-19T05:43:05.160806Z","shell.execute_reply":"2023-05-19T05:43:12.117241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Class labels\nCLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n               'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:43:12.120890Z","iopub.execute_input":"2023-05-19T05:43:12.121647Z","iopub.status.idle":"2023-05-19T05:43:12.127357Z","shell.execute_reply.started":"2023-05-19T05:43:12.121612Z","shell.execute_reply":"2023-05-19T05:43:12.126430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Open training archive file\n\nif not len(glob.glob('/kaggle/working/train/*.png')):\n    with py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as z:\n        z.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:43:12.132233Z","iopub.execute_input":"2023-05-19T05:43:12.133136Z","iopub.status.idle":"2023-05-19T05:44:09.026068Z","shell.execute_reply.started":"2023-05-19T05:43:12.133102Z","shell.execute_reply":"2023-05-19T05:44:09.025076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the filepath to an integer image ID\ndef filename_to_id(filename):\n    return int(filename.split('/')[-1].split('.')[0])\n\n# Load all of the images into an array, also return the image IDs\ndef load_imgs_and_ids(img_directory_path):\n    image_filenames = glob.glob(img_directory_path + '/*.png')\n    list_of_images = []\n    image_ids = []\n    for f in image_filenames:\n        image_id = filename_to_id(f) # E.G. '/kaggle/working/train/44240.png' -> 44240\n        image_ids.append(image_id)\n        data_array = cv2.imread(f)\n        list_of_images.append(data_array)\n\n    X = np.array(list_of_images)\n    \n    return X, image_ids\n    \nX_train, train_img_ids = load_imgs_and_ids('/kaggle/working/train')\n\n# Read the labels from the CSV files and convert the array of IDs into a 0-9 code\ntrain_labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nimg_codes = [CLASS_NAMES.index(train_labels_df.loc[img_id-1]['label']) for img_id in train_img_ids]\ny_train = np.array(img_codes)\n\nX_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:44:09.027331Z","iopub.execute_input":"2023-05-19T05:44:09.027684Z","iopub.status.idle":"2023-05-19T05:44:18.074327Z","shell.execute_reply.started":"2023-05-19T05:44:09.027650Z","shell.execute_reply":"2023-05-19T05:44:18.073449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizations**","metadata":{}},{"cell_type":"code","source":"# To get a sense of the data we work with\n\nplt.rcParams[\"figure.figsize\"] = (10,10)\nfig,axes = plt.subplots(10,10)\nfor i,ax in enumerate(axes.flatten()):\n  ax.imshow(X_train[i])\n  ax.set_title(f'{CLASS_NAMES[y_train[i]]}')\n  ax.set_xticks([])\n  ax.set_yticks([])\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:44:18.075974Z","iopub.execute_input":"2023-05-19T05:44:18.076682Z","iopub.status.idle":"2023-05-19T05:44:22.889156Z","shell.execute_reply.started":"2023-05-19T05:44:18.076648Z","shell.execute_reply":"2023-05-19T05:44:22.888169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn=models.Sequential([\n    layers.Conv2D(filters=32, kernel_size=(3, 3),  \n                                 activation='relu', input_shape=(32,32,3)),\n    layers.MaxPooling2D(2,2),\n    \n    layers.Conv2D(filters=64, kernel_size=(3, 3),  \n                                 activation='relu'),\n    layers.MaxPooling2D(2,2),\n    \n    layers.Conv2D(filters=128, kernel_size=(3, 3),  \n                                 activation='relu'),\n    layers.MaxPooling2D(2,2),\n    \n    layers.Flatten(),\n    \n    layers.Dense(128,activation='relu'),\n    layers.Dense(64,activation='relu'),\n    layers.Dense(10,activation='softmax')\n])\n\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:44:22.892127Z","iopub.execute_input":"2023-05-19T05:44:22.892544Z","iopub.status.idle":"2023-05-19T05:44:25.381222Z","shell.execute_reply.started":"2023-05-19T05:44:22.892495Z","shell.execute_reply":"2023-05-19T05:44:25.380537Z"},"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-05-19T05:44:25.382221Z","iopub.execute_input":"2023-05-19T05:44:25.382804Z","iopub.status.idle":"2023-05-19T05:44:25.410192Z","shell.execute_reply.started":"2023-05-19T05:44:25.382776Z","shell.execute_reply":"2023-05-19T05:44:25.409312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train,y_train,epochs=25)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:44:25.411549Z","iopub.execute_input":"2023-05-19T05:44:25.412096Z","iopub.status.idle":"2023-05-19T05:47:23.533543Z","shell.execute_reply.started":"2023-05-19T05:44:25.412064Z","shell.execute_reply":"2023-05-19T05:47:23.532687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test set\nif not len(glob.glob('/kaggle/working/test/*.png')):\n    with py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n        z.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T05:47:29.516209Z","iopub.execute_input":"2023-05-19T05:47:29.516610Z","iopub.status.idle":"2023-05-19T06:06:26.396061Z","shell.execute_reply.started":"2023-05-19T05:47:29.516575Z","shell.execute_reply":"2023-05-19T06:06:26.395083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Feed in the files from the directory. We do not attempt to load all images to memory at once\ntest_datagen = ImageDataGenerator(rescale=1/255.0)\n\ntest_flow = test_datagen.flow_from_directory('/kaggle/working/test',\n                                            classes=['.'],\n                                            target_size=(32,32),\n                                            shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:06:26.397862Z","iopub.execute_input":"2023-05-19T06:06:26.398241Z","iopub.status.idle":"2023-05-19T06:06:33.258908Z","shell.execute_reply.started":"2023-05-19T06:06:26.398207Z","shell.execute_reply":"2023-05-19T06:06:33.257947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Code -> classnames, and the helper function we made earlier\ncode_to_classname = {i:CLASS_NAMES[i] for i in range(len(CLASS_NAMES))}\ndef filename_to_id(filename):\n    return int(filename.split('/')[-1].split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:14:47.762658Z","iopub.execute_input":"2023-05-19T06:14:47.763086Z","iopub.status.idle":"2023-05-19T06:14:47.774810Z","shell.execute_reply.started":"2023-05-19T06:14:47.763053Z","shell.execute_reply":"2023-05-19T06:14:47.772841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Form predictions\ntest_flow.reset()\n\ntest_output = cnn.predict(test_flow)\ntest_prediction_codes = pd.Series(np.argmax(test_output, axis=1))\ntest_prediction_labels = test_prediction_codes.map(code_to_classname)\n\ntest_filenames = pd.Series(test_flow.filenames)\ntest_ids = test_filenames.apply(filename_to_id)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:15:31.681661Z","iopub.execute_input":"2023-05-19T06:15:31.682017Z","iopub.status.idle":"2023-05-19T06:17:35.165676Z","shell.execute_reply.started":"2023-05-19T06:15:31.681987Z","shell.execute_reply":"2023-05-19T06:17:35.164664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Final cleaning up and output\nout_df = pd.DataFrame({'id':test_ids,'label':test_prediction_labels})\nout_df.sort_values(by=['id'], inplace=True)\nout_df = out_df.reset_index(drop=True)\nout_df = out_df.dropna()\nout_df['id'] = out_df['id'].astype(int)\n\nout_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:17:35.167457Z","iopub.execute_input":"2023-05-19T06:17:35.167839Z","iopub.status.idle":"2023-05-19T06:17:35.879412Z","shell.execute_reply.started":"2023-05-19T06:17:35.167801Z","shell.execute_reply":"2023-05-19T06:17:35.878487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_in=pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')\ndata_in","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:19:37.452088Z","iopub.execute_input":"2023-05-19T06:19:37.452657Z","iopub.status.idle":"2023-05-19T06:19:37.612629Z","shell.execute_reply.started":"2023-05-19T06:19:37.452614Z","shell.execute_reply":"2023-05-19T06:19:37.611616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_out=pd.read_csv('/kaggle/working/submission.csv')\ndata_out","metadata":{"execution":{"iopub.status.busy":"2023-05-19T06:19:59.014265Z","iopub.execute_input":"2023-05-19T06:19:59.014753Z","iopub.status.idle":"2023-05-19T06:19:59.093234Z","shell.execute_reply.started":"2023-05-19T06:19:59.014712Z","shell.execute_reply":"2023-05-19T06:19:59.092346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}