{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:24:25.367423Z","iopub.execute_input":"2024-04-13T18:24:25.367843Z","iopub.status.idle":"2024-04-13T18:24:37.113781Z","shell.execute_reply.started":"2024-04-13T18:24:25.367813Z","shell.execute_reply":"2024-04-13T18:24:37.112624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import required dependencies\nimport py7zr\nimport pandas as pd\nimport numpy as np\nimport os\nfrom sklearn.preprocessing import LabelEncoder\nimport cv2\nimport matplotlib.pyplot as plt\nimport random\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:19:02.053214Z","iopub.execute_input":"2024-04-13T17:19:02.05365Z","iopub.status.idle":"2024-04-13T17:19:03.500411Z","shell.execute_reply.started":"2024-04-13T17:19:02.053617Z","shell.execute_reply":"2024-04-13T17:19:03.499084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unzip the train data file\n\narchive = py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z','r')\narchive.extractall()\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:19:05.1032Z","iopub.execute_input":"2024-04-13T17:19:05.103775Z","iopub.status.idle":"2024-04-13T17:19:34.290975Z","shell.execute_reply.started":"2024-04-13T17:19:05.103734Z","shell.execute_reply":"2024-04-13T17:19:34.289894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the label data \nlabel_data = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:24:39.357665Z","iopub.execute_input":"2024-04-13T17:24:39.358706Z","iopub.status.idle":"2024-04-13T17:24:39.381396Z","shell.execute_reply.started":"2024-04-13T17:24:39.358654Z","shell.execute_reply":"2024-04-13T17:24:39.380003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the first five rows in the label data and print its size\nprint(label_data.head())\nprint(len(label_data))\nunique_values = label_data['label'].unique()\nprint(unique_values)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:25:09.358279Z","iopub.execute_input":"2024-04-13T17:25:09.358734Z","iopub.status.idle":"2024-04-13T17:25:09.372299Z","shell.execute_reply.started":"2024-04-13T17:25:09.358702Z","shell.execute_reply":"2024-04-13T17:25:09.371155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\nlabel_data['label'] = encoder.fit_transform(label_data['label'])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:25:29.251949Z","iopub.execute_input":"2024-04-13T17:25:29.252358Z","iopub.status.idle":"2024-04-13T17:25:29.272483Z","shell.execute_reply.started":"2024-04-13T17:25:29.252326Z","shell.execute_reply":"2024-04-13T17:25:29.270908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_values_indices = label_data['label'].unique()\nprint(unique_values_indices)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:25:47.036884Z","iopub.execute_input":"2024-04-13T17:25:47.037289Z","iopub.status.idle":"2024-04-13T17:25:47.047008Z","shell.execute_reply.started":"2024-04-13T17:25:47.037258Z","shell.execute_reply":"2024-04-13T17:25:47.045575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create dictionary for the label and its encoded label that created by label encoder\nclasses = {encoded_label:label for encoded_label,label in zip(np.unique(label_data['label']),encoder.classes_)}\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:26:19.595643Z","iopub.execute_input":"2024-04-13T17:26:19.596022Z","iopub.status.idle":"2024-04-13T17:26:19.606761Z","shell.execute_reply.started":"2024-04-13T17:26:19.595993Z","shell.execute_reply":"2024-04-13T17:26:19.605452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:26:34.77267Z","iopub.execute_input":"2024-04-13T17:26:34.773049Z","iopub.status.idle":"2024-04-13T17:26:34.785928Z","shell.execute_reply.started":"2024-04-13T17:26:34.77302Z","shell.execute_reply":"2024-04-13T17:26:34.78454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_names = os.listdir('/kaggle/working/train')","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:26:59.427018Z","iopub.execute_input":"2024-04-13T17:26:59.427434Z","iopub.status.idle":"2024-04-13T17:26:59.458751Z","shell.execute_reply.started":"2024-04-13T17:26:59.427396Z","shell.execute_reply":"2024-04-13T17:26:59.457607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if the images have the same size or not\ni = 0\nfor image_name in images_names:\n    image_path = os.path.join('/kaggle/working/train',image_name)\n    image = cv2.imread(image_path)\n    print(image.shape)\n    if i==10:\n        break\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:28:48.179335Z","iopub.execute_input":"2024-04-13T17:28:48.179757Z","iopub.status.idle":"2024-04-13T17:28:48.188882Z","shell.execute_reply.started":"2024-04-13T17:28:48.179725Z","shell.execute_reply":"2024-04-13T17:28:48.187328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = []\nfor image_name,label in zip(images_names,label_data['label']):\n    image_path = os.path.join('/kaggle/working/train',image_name)\n    image = cv2.imread(image_path)\n    if image is not None:\n        data.append([image,label])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:29:02.119454Z","iopub.execute_input":"2024-04-13T17:29:02.119844Z","iopub.status.idle":"2024-04-13T17:29:07.079043Z","shell.execute_reply.started":"2024-04-13T17:29:02.119815Z","shell.execute_reply":"2024-04-13T17:29:07.077879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:32:27.75623Z","iopub.execute_input":"2024-04-13T17:32:27.756638Z","iopub.status.idle":"2024-04-13T17:32:27.762504Z","shell.execute_reply.started":"2024-04-13T17:32:27.756606Z","shell.execute_reply":"2024-04-13T17:32:27.761221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature, label = data[6]\nplt.imshow(feature)\nprint(label)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:36:01.885334Z","iopub.execute_input":"2024-04-13T17:36:01.886601Z","iopub.status.idle":"2024-04-13T17:36:02.141182Z","shell.execute_reply.started":"2024-04-13T17:36:01.886526Z","shell.execute_reply":"2024-04-13T17:36:02.139765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature, label = data[6]\nmpimg.imsave(\"my_image_2.png\", feature)\npath = \"/kaggle/working/my_image_2.png\"\nimage = plt.imread(path)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:36:26.360144Z","iopub.execute_input":"2024-04-13T17:36:26.360617Z","iopub.status.idle":"2024-04-13T17:36:26.614252Z","shell.execute_reply.started":"2024-04-13T17:36:26.360584Z","shell.execute_reply":"2024-04-13T17:36:26.612799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(data)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:37:44.69693Z","iopub.execute_input":"2024-04-13T17:37:44.697346Z","iopub.status.idle":"2024-04-13T17:37:44.741665Z","shell.execute_reply.started":"2024-04-13T17:37:44.697315Z","shell.execute_reply":"2024-04-13T17:37:44.740796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data into input and label data\nX = []\nY = []\nfor feature,label in data:\n    X.append(feature)\n    Y.append(label)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:37:46.764208Z","iopub.execute_input":"2024-04-13T17:37:46.764687Z","iopub.status.idle":"2024-04-13T17:37:46.808009Z","shell.execute_reply.started":"2024-04-13T17:37:46.764652Z","shell.execute_reply":"2024-04-13T17:37:46.806683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert input and label data into numpy array\nX = np.array(X)\nY = np.array(Y)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:37:49.660655Z","iopub.execute_input":"2024-04-13T17:37:49.661139Z","iopub.status.idle":"2024-04-13T17:37:49.847957Z","shell.execute_reply.started":"2024-04-13T17:37:49.661103Z","shell.execute_reply":"2024-04-13T17:37:49.846481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-25T07:15:53.725403Z","iopub.execute_input":"2023-10-25T07:15:53.725754Z","iopub.status.idle":"2023-10-25T07:15:53.73224Z","shell.execute_reply.started":"2023-10-25T07:15:53.725729Z","shell.execute_reply":"2023-10-25T07:15:53.731119Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scaling input data\nX = X/255","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:37:52.910798Z","iopub.execute_input":"2024-04-13T17:37:52.912432Z","iopub.status.idle":"2024-04-13T17:37:53.6352Z","shell.execute_reply.started":"2024-04-13T17:37:52.912373Z","shell.execute_reply":"2024-04-13T17:37:53.632961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(Y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T07:18:52.37787Z","iopub.execute_input":"2023-10-25T07:18:52.378247Z","iopub.status.idle":"2023-10-25T07:18:52.383385Z","shell.execute_reply.started":"2023-10-25T07:18:52.378219Z","shell.execute_reply":"2023-10-25T07:18:52.382315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split input and label data into train and test data\nx_train,x_test,y_train,y_test = train_test_split(X,Y,train_size=0.92,random_state=2)\nprint(X.shape,x_train.shape,x_test.shape)\nprint(Y.shape,y_train.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:38:21.18108Z","iopub.execute_input":"2024-04-13T17:38:21.181574Z","iopub.status.idle":"2024-04-13T17:38:22.048305Z","shell.execute_reply.started":"2024-04-13T17:38:21.181523Z","shell.execute_reply":"2024-04-13T17:38:22.047006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import required dependencies for the convolutional neural network to create it from scratch\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Input,Conv2D,MaxPooling2D,Flatten,Dropout,Dense","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:38:28.250795Z","iopub.execute_input":"2024-04-13T17:38:28.251223Z","iopub.status.idle":"2024-04-13T17:38:38.747873Z","shell.execute_reply.started":"2024-04-13T17:38:28.251192Z","shell.execute_reply":"2024-04-13T17:38:38.746266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the model\n\n# Determine input size\ninput_size = (32,32,3)\n# Determine number of classes\nnum_classes = 10\n\n# Create the model\nmodel = Sequential([\n    Input(shape=input_size),\n    Conv2D(32, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    \n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(num_classes, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:38:50.565448Z","iopub.execute_input":"2024-04-13T17:38:50.568003Z","iopub.status.idle":"2024-04-13T17:38:50.680489Z","shell.execute_reply.started":"2024-04-13T17:38:50.567945Z","shell.execute_reply":"2024-04-13T17:38:50.678873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:39:00.443165Z","iopub.execute_input":"2024-04-13T17:39:00.44365Z","iopub.status.idle":"2024-04-13T17:39:00.470104Z","shell.execute_reply.started":"2024-04-13T17:39:00.443613Z","shell.execute_reply":"2024-04-13T17:39:00.468873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.fit(x_train,y_train,epochs=30,validation_split=0.03)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:24:56.787003Z","iopub.execute_input":"2024-04-13T18:24:56.787416Z","iopub.status.idle":"2024-04-13T18:43:51.910396Z","shell.execute_reply.started":"2024-04-13T18:24:56.787383Z","shell.execute_reply":"2024-04-13T18:43:51.908367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate the model\nevaluation = model.evaluate(x_test,y_test)\nprint(\"the loss value is: \",evaluation[0])\nprint(\"the accuracy value is: \",evaluation[1])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:52:04.021095Z","iopub.execute_input":"2024-04-13T18:52:04.021522Z","iopub.status.idle":"2024-04-13T18:52:05.574073Z","shell.execute_reply.started":"2024-04-13T18:52:04.02149Z","shell.execute_reply":"2024-04-13T18:52:05.572835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model, save_model","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:52:27.226387Z","iopub.execute_input":"2024-04-13T18:52:27.226893Z","iopub.status.idle":"2024-04-13T18:52:27.236123Z","shell.execute_reply.started":"2024-04-13T18:52:27.226859Z","shell.execute_reply":"2024-04-13T18:52:27.234786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\nsave_model(model, 'Model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:53:05.570227Z","iopub.execute_input":"2024-04-13T18:53:05.571537Z","iopub.status.idle":"2024-04-13T18:53:05.649254Z","shell.execute_reply.started":"2024-04-13T18:53:05.571485Z","shell.execute_reply":"2024-04-13T18:53:05.647896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize the accuracy with the validation accuracy\nplt.figure(figsize=(7,7))\nplt.plot(result.history['accuracy'],color='red')\nplt.plot(result.history['val_accuracy'],color='blue')\nplt.title('model accuracy')\nplt.xlabel('epochs')\nplt.ylabel('accuracy')\nplt.legend(['accuracy','val_accuracy'],loc='lower right')\n# Visualize the loss with the validation loss\nplt.figure(figsize=(7,7))\nplt.plot(result.history['loss'],color='red')\nplt.plot(result.history['val_loss'],color='blue')\nplt.title('model loss')\nplt.xlabel('epochs')\nplt.ylabel('accuracy')\nplt.legend(['loss','val_loss'],loc='upper right')","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:22:42.268569Z","iopub.execute_input":"2023-10-25T08:22:42.268999Z","iopub.status.idle":"2023-10-25T08:22:43.567483Z","shell.execute_reply.started":"2023-10-25T08:22:42.268967Z","shell.execute_reply":"2023-10-25T08:22:43.565696Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.math import confusion_matrix\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:24:15.261734Z","iopub.execute_input":"2023-10-25T08:24:15.262119Z","iopub.status.idle":"2023-10-25T08:24:15.618831Z","shell.execute_reply.started":"2023-10-25T08:24:15.262091Z","shell.execute_reply":"2023-10-25T08:24:15.61717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make the model predict on test input data\npredicted_y = Model.predict(x_test)\ny_predicted_values = []\nfor value in predicted_y:\n    y_predicted_values.append(np.argmax(value))\ncomparison = []\nfor predicted_value,true_value in zip(y_predicted_values,y_test):\n    comparison.append([predicted_value,true_value])\nprint(comparison)\n# create the confusion matrix and visualize it\nplt.figure(figsize=(5,5))\nconf_matrix = confusion_matrix(y_test,y_predicted_values)\nsns.heatmap(conf_matrix,square=True,cbar=True,annot=True,annot_kws={'size':8},cmap='Blues')","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:24:17.099825Z","iopub.execute_input":"2023-10-25T08:24:17.100205Z","iopub.status.idle":"2023-10-25T08:24:18.932734Z","shell.execute_reply.started":"2023-10-25T08:24:17.100175Z","shell.execute_reply":"2023-10-25T08:24:18.930972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle as pk","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:26:11.296808Z","iopub.execute_input":"2023-10-25T08:26:11.297174Z","iopub.status.idle":"2023-10-25T08:26:11.302077Z","shell.execute_reply.started":"2023-10-25T08:26:11.297143Z","shell.execute_reply":"2023-10-25T08:26:11.300826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\npk.dump(Model,open('trained_model.sav','wb'))","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:26:12.743896Z","iopub.execute_input":"2023-10-25T08:26:12.744253Z","iopub.status.idle":"2023-10-25T08:26:12.819592Z","shell.execute_reply.started":"2023-10-25T08:26:12.744223Z","shell.execute_reply":"2023-10-25T08:26:12.817606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make a predictive system \nimage_path = input(\"Enter the image path: \")\nimage = cv2.imread(image_path)\nplt.imshow(image)\n# Ensure the image has 3 color channels (e.g., convert from grayscale to RGB)\nif image.shape[-1] == 1:\n    image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)\n# Normalize the image by scaling it\nimage = image / 255\n\n# Make the model predict what is in the image if it's dog will print 1 otherwise will print 0\nprediction = Model.predict(np.expand_dims(image, axis=0))\npredicted_class = np.argmax(prediction)\nprint(classes[predicted_class])","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:33:36.67688Z","iopub.execute_input":"2023-10-25T08:33:36.677239Z","iopub.status.idle":"2023-10-25T08:33:44.53747Z","shell.execute_reply.started":"2023-10-25T08:33:36.67721Z","shell.execute_reply":"2023-10-25T08:33:44.536012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor image_name in images_names:\n    image_path = os.path.join('/kaggle/working/train',image_name)\n    print(image_path)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-25T08:32:10.288034Z","iopub.execute_input":"2023-10-25T08:32:10.288928Z","iopub.status.idle":"2023-10-25T08:32:10.294052Z","shell.execute_reply.started":"2023-10-25T08:32:10.288895Z","shell.execute_reply":"2023-10-25T08:32:10.293163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}