{"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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd \nimport tensorflow as tf \nimport numpy as np \nfrom tensorflow import keras \nfrom tensorflow.keras.applications import VGG16\nfrom keras.callbacks import EarlyStopping , Callback\nimport matplotlib.pyplot as plt \nimport seaborn as sns\nimport cv2\nimport os ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-11T13:03:01.058052Z","iopub.execute_input":"2024-01-11T13:03:01.058486Z","iopub.status.idle":"2024-01-11T13:03:17.223520Z","shell.execute_reply.started":"2024-01-11T13:03:01.058453Z","shell.execute_reply":"2024-01-11T13:03:17.222158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = os.path.join('/kaggle' , 'input' , 'digit-recognizer')\ntrain_path = os.path.join(data_path , 'train.csv')\ntest_path = os.path.join(data_path , 'test.csv')\nbatch = 32\nlr = 1e-3\nepoch =3 ","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:17.225629Z","iopub.execute_input":"2024-01-11T13:03:17.226288Z","iopub.status.idle":"2024-01-11T13:03:17.235322Z","shell.execute_reply.started":"2024-01-11T13:03:17.226254Z","shell.execute_reply":"2024-01-11T13:03:17.233514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(data_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:17.237161Z","iopub.execute_input":"2024-01-11T13:03:17.237948Z","iopub.status.idle":"2024-01-11T13:03:17.255326Z","shell.execute_reply.started":"2024-01-11T13:03:17.237903Z","shell.execute_reply":"2024-01-11T13:03:17.254382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(train_path)\ntest_csv = pd.read_csv(test_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:17.258505Z","iopub.execute_input":"2024-01-11T13:03:17.258883Z","iopub.status.idle":"2024-01-11T13:03:24.542908Z","shell.execute_reply.started":"2024-01-11T13:03:17.258851Z","shell.execute_reply":"2024-01-11T13:03:24.541615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:24.544549Z","iopub.execute_input":"2024-01-11T13:03:24.544979Z","iopub.status.idle":"2024-01-11T13:03:24.578015Z","shell.execute_reply.started":"2024-01-11T13:03:24.544947Z","shell.execute_reply":"2024-01-11T13:03:24.576857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:24.579412Z","iopub.execute_input":"2024-01-11T13:03:24.579789Z","iopub.status.idle":"2024-01-11T13:03:24.587659Z","shell.execute_reply.started":"2024-01-11T13:03:24.579757Z","shell.execute_reply":"2024-01-11T13:03:24.586320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:24.589219Z","iopub.execute_input":"2024-01-11T13:03:24.589604Z","iopub.status.idle":"2024-01-11T13:03:27.026085Z","shell.execute_reply.started":"2024-01-11T13:03:24.589561Z","shell.execute_reply":"2024-01-11T13:03:27.024799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:27.027774Z","iopub.execute_input":"2024-01-11T13:03:27.028145Z","iopub.status.idle":"2024-01-11T13:03:27.063541Z","shell.execute_reply.started":"2024-01-11T13:03:27.028114Z","shell.execute_reply":"2024-01-11T13:03:27.062332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"visulize the data \"\"\"\ntrain_csv['label'].value_counts().sort_index().plot(kind=\"bar\")\nplt.title('the frequency of the label in train dataset')\nplt.ylabel('label')\nplt.ylabel('frequency')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:27.065934Z","iopub.execute_input":"2024-01-11T13:03:27.066330Z","iopub.status.idle":"2024-01-11T13:03:27.457940Z","shell.execute_reply.started":"2024-01-11T13:03:27.066295Z","shell.execute_reply":"2024-01-11T13:03:27.456889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visulize an a image  with it's label \ndef show_ith_image(data,i , labels=True) : \n    if (labels == True):\n        image = np.array(data.drop('label', axis=1).iloc[i])\n        label = data['label'][i]\n    else : \n        image = np.array(data.iloc[i])\n        label = \"none\"\n    image = image.reshape(28,28)\n    plt.imshow(image)\n    plt.title(f'label : {label}')\n    plt.axis('off')\nshow_ith_image(train_csv,6)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:27.463491Z","iopub.execute_input":"2024-01-11T13:03:27.463848Z","iopub.status.idle":"2024-01-11T13:03:27.752894Z","shell.execute_reply.started":"2024-01-11T13:03:27.463819Z","shell.execute_reply":"2024-01-11T13:03:27.751746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_csv.drop('label', axis=1)\ny_train = train_csv['label']\ny_train = pd.get_dummies(y_train ,dtype='float32')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:27.754378Z","iopub.execute_input":"2024-01-11T13:03:27.754720Z","iopub.status.idle":"2024-01-11T13:03:27.852786Z","shell.execute_reply.started":"2024-01-11T13:03:27.754690Z","shell.execute_reply":"2024-01-11T13:03:27.851473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndef split_data(x , y) : \n    x_train ,x_val , y_train , y_val  = train_test_split(x , y  ,test_size=0.15 , shuffle=True , random_state=42)\n    return x_train , x_val , y_train , y_val ","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:27.854076Z","iopub.execute_input":"2024-01-11T13:03:27.854835Z","iopub.status.idle":"2024-01-11T13:03:28.038590Z","shell.execute_reply.started":"2024-01-11T13:03:27.854794Z","shell.execute_reply":"2024-01-11T13:03:28.037516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_val , y_train , y_val = split_data(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:28.040004Z","iopub.execute_input":"2024-01-11T13:03:28.040480Z","iopub.status.idle":"2024-01-11T13:03:28.280468Z","shell.execute_reply.started":"2024-01-11T13:03:28.040448Z","shell.execute_reply":"2024-01-11T13:03:28.279327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:28.281800Z","iopub.execute_input":"2024-01-11T13:03:28.282162Z","iopub.status.idle":"2024-01-11T13:03:28.312766Z","shell.execute_reply.started":"2024-01-11T13:03:28.282132Z","shell.execute_reply":"2024-01-11T13:03:28.311553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tqdm","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:03:28.314292Z","iopub.execute_input":"2024-01-11T13:03:28.315293Z","iopub.status.idle":"2024-01-11T13:04:03.546841Z","shell.execute_reply.started":"2024-01-11T13:03:28.315253Z","shell.execute_reply":"2024-01-11T13:04:03.545665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ndef load_data(x_data ,y_data ) : \n    images = []\n    labels = [] \n    size , j = x_data.shape\n    for i in tqdm(range(0,size)) : \n        img = np.array(x_data.iloc[i])\n        resized_img = img.reshape(28,28)\n        IMG = cv2.resize(np.array(resized_img, dtype='float32'), (32,32))\n        images.append(IMG)\n        labels.append(np.array(y_data.iloc[i]))\n    return images,labels\n\n        ","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:03.549467Z","iopub.execute_input":"2024-01-11T13:04:03.549858Z","iopub.status.idle":"2024-01-11T13:04:03.565838Z","shell.execute_reply.started":"2024-01-11T13:04:03.549821Z","shell.execute_reply":"2024-01-11T13:04:03.564698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"split our data \"\"\"\ntrain_images , train_labels = load_data(x_train,y_train)\nval_images , val_labels = load_data(x_val , y_val)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:03.567058Z","iopub.execute_input":"2024-01-11T13:04:03.567448Z","iopub.status.idle":"2024-01-11T13:04:09.738416Z","shell.execute_reply.started":"2024-01-11T13:04:03.567416Z","shell.execute_reply":"2024-01-11T13:04:09.737188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG = cv2.resize(np.array(train_images[0], dtype='float32'), (32,32))\nIMG.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:09.739738Z","iopub.execute_input":"2024-01-11T13:04:09.740066Z","iopub.status.idle":"2024-01-11T13:04:09.749985Z","shell.execute_reply.started":"2024-01-11T13:04:09.740037Z","shell.execute_reply":"2024-01-11T13:04:09.748840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"train data pipline\"\"\"\ntrain_data= tf.data.Dataset.from_tensor_slices((train_images, train_labels))\ntrain_data = train_data.batch(batch_size=32).prefetch(-1)\n\"\"\"val data pipline\"\"\"\nval_data= tf.data.Dataset.from_tensor_slices((val_images,val_labels))\nval_data = val_data.batch(batch_size=32).prefetch(-1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:09.751166Z","iopub.execute_input":"2024-01-11T13:04:09.751543Z","iopub.status.idle":"2024-01-11T13:04:25.458399Z","shell.execute_reply.started":"2024-01-11T13:04:09.751513Z","shell.execute_reply":"2024-01-11T13:04:25.457299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"check the demension \"\"\"\nfor img , label in train_data : \n    print(img.shape , label.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:25.459725Z","iopub.execute_input":"2024-01-11T13:04:25.460139Z","iopub.status.idle":"2024-01-11T13:04:25.662230Z","shell.execute_reply.started":"2024-01-11T13:04:25.460082Z","shell.execute_reply":"2024-01-11T13:04:25.661060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model \nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout , Conv2D  , MaxPooling2D , Flatten\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import VGG16\n\ndef build_model(input_shape=(32, 32, 1), num_classes=10):\n    model = Sequential()\n\n    # Convolutional layers\n    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))\n    model.add(MaxPooling2D((2, 2)))\n\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(MaxPooling2D((2, 2)))\n\n    model.add(Conv2D(128, (3, 3), activation='relu'))\n    model.add(MaxPooling2D((2, 2)))\n\n    # Flatten layer\n    model.add(Flatten())\n\n    # Fully connected layers\n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n\n    model.add(Dense(num_classes, activation='softmax'))\n    print(model.summary())\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:25.663780Z","iopub.execute_input":"2024-01-11T13:04:25.664984Z","iopub.status.idle":"2024-01-11T13:04:25.678648Z","shell.execute_reply.started":"2024-01-11T13:04:25.664937Z","shell.execute_reply":"2024-01-11T13:04:25.677602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:04:25.679918Z","iopub.execute_input":"2024-01-11T13:04:25.680279Z","iopub.status.idle":"2024-01-11T13:04:25.891213Z","shell.execute_reply.started":"2024-01-11T13:04:25.680249Z","shell.execute_reply":"2024-01-11T13:04:25.890003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor='val_loss' , restore_best_weights=True, patience=1)\ncallbacks = [early_stop]\nmodel.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=1e-3),\n    loss=tf.keras.losses.CategoricalCrossentropy(),\n   metrics =tf.keras.metrics.CategoricalAccuracy(),\n)\nhistory = model.fit(train_data ,validation_data = val_data , epochs = 3  , callbacks =callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:06:39.694745Z","iopub.execute_input":"2024-01-11T13:06:39.695220Z","iopub.status.idle":"2024-01-11T13:08:29.125012Z","shell.execute_reply.started":"2024-01-11T13:06:39.695185Z","shell.execute_reply":"2024-01-11T13:08:29.123236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"plot val and training loss \"\"\"\nplt.plot(np.arange(1,4) , history.history['val_loss'] , label='val_loss')\nplt.plot(np.arange(1,4) , history.history['loss'] , label='training loss')\nplt.legend()\nplt.title('model accuracy')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:05.191807Z","iopub.execute_input":"2024-01-11T13:16:05.192456Z","iopub.status.idle":"2024-01-11T13:16:05.670933Z","shell.execute_reply.started":"2024-01-11T13:16:05.192410Z","shell.execute_reply":"2024-01-11T13:16:05.669436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(test_path)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:12.852393Z","iopub.execute_input":"2024-01-11T13:16:12.852966Z","iopub.status.idle":"2024-01-11T13:16:15.382134Z","shell.execute_reply.started":"2024-01-11T13:16:12.852903Z","shell.execute_reply":"2024-01-11T13:16:15.381159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:15.385222Z","iopub.execute_input":"2024-01-11T13:16:15.386173Z","iopub.status.idle":"2024-01-11T13:16:15.418023Z","shell.execute_reply.started":"2024-01-11T13:16:15.386126Z","shell.execute_reply":"2024-01-11T13:16:15.416699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_ith_image(test_data, 2 , False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:15.419625Z","iopub.execute_input":"2024-01-11T13:16:15.420332Z","iopub.status.idle":"2024-01-11T13:16:15.623862Z","shell.execute_reply.started":"2024-01-11T13:16:15.420287Z","shell.execute_reply":"2024-01-11T13:16:15.622689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = test_data.iloc[2]\ntest_image = np.array(test_image)\ntest_image = test_image.reshape(28,28)\ntest_image = cv2.resize(np.array(test_image , dtype='float32') , (32,32))\ntest_image = np.expand_dims(test_image , axis=0)\npredicted =  model.predict(test_image)\npredicted = np.array(predicted)\npredicted = np.argmax(predicted)\nprint(predicted)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:15.626120Z","iopub.execute_input":"2024-01-11T13:16:15.627167Z","iopub.status.idle":"2024-01-11T13:16:15.850950Z","shell.execute_reply.started":"2024-01-11T13:16:15.627119Z","shell.execute_reply":"2024-01-11T13:16:15.849698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_data_preprocess(data) : \n    size ,_ = data.shape\n    predections = []\n    for i in tqdm(range(size)) : \n        test_image = test_data.iloc[i]\n        test_image = np.array(test_image)\n        test_image = test_image.reshape(28,28)\n        test_image = cv2.resize(np.array(test_image , dtype='float32') , (32,32))\n        test_image = np.expand_dims(test_image , axis=0)\n        predicted =  model.predict(test_image , verbose=0)\n        predicted = np.array(predicted)\n        predicted = np.argmax(predicted)\n        predections.append(predicted)\n    return predections","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:15.855820Z","iopub.execute_input":"2024-01-11T13:16:15.856665Z","iopub.status.idle":"2024-01-11T13:16:15.866538Z","shell.execute_reply.started":"2024-01-11T13:16:15.856613Z","shell.execute_reply":"2024-01-11T13:16:15.864997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predections = test_data_preprocess(test_data)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:16:15.868614Z","iopub.execute_input":"2024-01-11T13:16:15.869197Z","iopub.status.idle":"2024-01-11T13:47:13.959986Z","shell.execute_reply.started":"2024-01-11T13:16:15.869155Z","shell.execute_reply":"2024-01-11T13:47:13.958677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.array(predections)\nnp.count_nonzero(np.isnan(labels))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:51:49.204510Z","iopub.execute_input":"2024-01-11T13:51:49.205026Z","iopub.status.idle":"2024-01-11T13:51:49.217318Z","shell.execute_reply.started":"2024-01-11T13:51:49.204989Z","shell.execute_reply":"2024-01-11T13:51:49.216021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['label'] = labels","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:51:50.628620Z","iopub.execute_input":"2024-01-11T13:51:50.629040Z","iopub.status.idle":"2024-01-11T13:51:50.634738Z","shell.execute_reply.started":"2024-01-11T13:51:50.629007Z","shell.execute_reply":"2024-01-11T13:51:50.633794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:47:13.990268Z","iopub.execute_input":"2024-01-11T13:47:13.990609Z","iopub.status.idle":"2024-01-11T13:47:14.015520Z","shell.execute_reply.started":"2024-01-11T13:47:13.990580Z","shell.execute_reply":"2024-01-11T13:47:14.014392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(3, 4, figsize=(12, 9))\n\nimages = test_data.drop('label', axis=1)\nlabels = test_data['label']\n\nfor i in range(3):\n    for j in range(4):\n        idx = i * 4 + j\n        ax[i, j].imshow(images.iloc[idx].values.reshape(28, 28), cmap='gray')  \n        ax[i, j].set_title(f'label: {labels.iloc[idx]}')\n        ax[i, j].axis('off')  \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T13:51:54.094278Z","iopub.execute_input":"2024-01-11T13:51:54.095042Z","iopub.status.idle":"2024-01-11T13:51:55.308812Z","shell.execute_reply.started":"2024-01-11T13:51:54.094989Z","shell.execute_reply":"2024-01-11T13:51:55.307662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'ImageId': range(1, len(labels) + 1),\n    'Labels': labels\n})\n\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T14:20:40.842416Z","iopub.execute_input":"2024-01-11T14:20:40.843723Z","iopub.status.idle":"2024-01-11T14:20:40.937889Z","shell.execute_reply.started":"2024-01-11T14:20:40.843639Z","shell.execute_reply":"2024-01-11T14:20:40.935668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"/kaggle/working/submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T14:20:50.229739Z","iopub.execute_input":"2024-01-11T14:20:50.230466Z","iopub.status.idle":"2024-01-11T14:20:50.265300Z","shell.execute_reply.started":"2024-01-11T14:20:50.230416Z","shell.execute_reply":"2024-01-11T14:20:50.262873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}