{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":14221615,"sourceType":"datasetVersion","datasetId":9072044},{"sourceId":14221676,"sourceType":"datasetVersion","datasetId":9072081}],"dockerImageVersionId":30066,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# IMPORTING THE LIBRARIES","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport pickle\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.datasets import load_files\nfrom keras.utils import np_utils\nimport matplotlib.pyplot as plt\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.models import Sequential\nfrom keras.utils.vis_utils import plot_model\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\nfrom keras.preprocessing import image                  \nfrom tqdm import tqdm\n\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:50.456725Z","iopub.execute_input":"2025-12-30T23:29:50.456978Z","iopub.status.idle":"2025-12-30T23:29:56.294620Z","shell.execute_reply.started":"2025-12-30T23:29:50.456917Z","shell.execute_reply":"2025-12-30T23:29:56.293986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Pretty display for notebooks\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:56.296404Z","iopub.execute_input":"2025-12-30T23:29:56.296624Z","iopub.status.idle":"2025-12-30T23:29:56.301162Z","shell.execute_reply.started":"2025-12-30T23:29:56.296602Z","shell.execute_reply":"2025-12-30T23:29:56.300431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:56.303137Z","iopub.execute_input":"2025-12-30T23:29:56.303349Z","iopub.status.idle":"2025-12-30T23:29:57.286108Z","shell.execute_reply.started":"2025-12-30T23:29:56.303329Z","shell.execute_reply":"2025-12-30T23:29:57.285109Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Defining the train,test and model directories\n\nWe will create the directories for train,test and model training paths if not present","metadata":{}},{"cell_type":"code","source":"DATA_DIR = \"../input/state-farm-distracted-driver-detection/imgs\"\nTEST_DIR = os.path.join(DATA_DIR,\"test\")\nTRAIN_DIR = os.path.join(DATA_DIR,\"train\")\nMODEL_PATH = os.path.join(os.getcwd(),\"model\",\"self_trained\")\nPICKLE_DIR = os.path.join(os.getcwd(),\"pickle_files\")\nCSV_DIR = os.path.join(os.getcwd(),\"csv_files\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:57.287324Z","iopub.execute_input":"2025-12-30T23:29:57.287623Z","iopub.status.idle":"2025-12-30T23:29:57.292086Z","shell.execute_reply.started":"2025-12-30T23:29:57.287589Z","shell.execute_reply":"2025-12-30T23:29:57.291350Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not os.path.exists(TEST_DIR):\n    print(\"Testing data does not exists\")\nif not os.path.exists(TRAIN_DIR):\n    print(\"Training data does not exists\")\nif not os.path.exists(MODEL_PATH):\n    print(\"Model path does not exists\")\n    os.makedirs(MODEL_PATH)\n    print(\"Model path created\")\nif not os.path.exists(PICKLE_DIR):\n    os.makedirs(PICKLE_DIR)\nif not os.path.exists(CSV_DIR):\n    os.makedirs(CSV_DIR)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:57.293466Z","iopub.execute_input":"2025-12-30T23:29:57.293792Z","iopub.status.idle":"2025-12-30T23:29:57.305924Z","shell.execute_reply.started":"2025-12-30T23:29:57.293760Z","shell.execute_reply":"2025-12-30T23:29:57.305101Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"markdown","source":"We will create a csv file having the location of the files present for training and test images and their associated class if present so that it is easily traceable.","metadata":{}},{"cell_type":"code","source":"def create_csv(DATA_DIR,filename):\n    class_names = os.listdir(DATA_DIR)\n    data = list()\n    if(os.path.isdir(os.path.join(DATA_DIR,class_names[0]))):\n        for class_name in class_names:\n            file_names = os.listdir(os.path.join(DATA_DIR,class_name))\n            for file in file_names:\n                data.append({\n                    \"Filename\":os.path.join(DATA_DIR,class_name,file),\n                    \"ClassName\":class_name\n                })\n    else:\n        class_name = \"test\"\n        file_names = os.listdir(DATA_DIR)\n        for file in file_names:\n            data.append(({\n                \"FileName\":os.path.join(DATA_DIR,file),\n                \"ClassName\":class_name\n            }))\n    data = pd.DataFrame(data)\n    data.to_csv(os.path.join(os.getcwd(),\"csv_files\",filename),index=False)\n\ncreate_csv(TRAIN_DIR,\"train.csv\")\ncreate_csv(TEST_DIR,\"test.csv\")\ndata_train = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"train.csv\"))\ndata_test = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"test.csv\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:57.306868Z","iopub.execute_input":"2025-12-30T23:29:57.307084Z","iopub.status.idle":"2025-12-30T23:29:58.965547Z","shell.execute_reply.started":"2025-12-30T23:29:57.307063Z","shell.execute_reply":"2025-12-30T23:29:58.964760Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:58.966732Z","iopub.execute_input":"2025-12-30T23:29:58.967002Z","iopub.status.idle":"2025-12-30T23:29:58.984804Z","shell.execute_reply.started":"2025-12-30T23:29:58.966977Z","shell.execute_reply":"2025-12-30T23:29:58.984147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train['ClassName'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:58.985969Z","iopub.execute_input":"2025-12-30T23:29:58.986201Z","iopub.status.idle":"2025-12-30T23:29:59.000089Z","shell.execute_reply.started":"2025-12-30T23:29:58.986177Z","shell.execute_reply":"2025-12-30T23:29:58.999305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.describe()","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.001154Z","iopub.execute_input":"2025-12-30T23:29:59.001510Z","iopub.status.idle":"2025-12-30T23:29:59.046272Z","shell.execute_reply.started":"2025-12-30T23:29:59.001478Z","shell.execute_reply":"2025-12-30T23:29:59.045662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nnf = data_train['ClassName'].value_counts(sort=False)\nlabels = data_train['ClassName'].value_counts(sort=False).index.tolist()\ny = np.array(nf)\nwidth = 1/1.5\nN = len(y)\nx = range(N)\n\nfig = plt.figure(figsize=(20,15))\nay = fig.add_subplot(211)\n\nplt.xticks(x, labels, size=15)\nplt.yticks(size=15)\n\nay.bar(x, y, width, color=\"blue\")\n\nplt.title('Bar Chart',size=25)\nplt.xlabel('classname',size=15)\nplt.ylabel('Count',size=15)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.047260Z","iopub.execute_input":"2025-12-30T23:29:59.047503Z","iopub.status.idle":"2025-12-30T23:29:59.227434Z","shell.execute_reply.started":"2025-12-30T23:29:59.047480Z","shell.execute_reply":"2025-12-30T23:29:59.226774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.228355Z","iopub.execute_input":"2025-12-30T23:29:59.228598Z","iopub.status.idle":"2025-12-30T23:29:59.235702Z","shell.execute_reply.started":"2025-12-30T23:29:59.228575Z","shell.execute_reply":"2025-12-30T23:29:59.235062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.236812Z","iopub.execute_input":"2025-12-30T23:29:59.237096Z","iopub.status.idle":"2025-12-30T23:29:59.244715Z","shell.execute_reply.started":"2025-12-30T23:29:59.237072Z","shell.execute_reply":"2025-12-30T23:29:59.244054Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Observation:\n1. There are total 22424 training samples\n2. There are total 79726 training samples\n3. The training dataset is equally balanced to a great extent and hence we need not do any downsampling of the data","metadata":{}},{"cell_type":"markdown","source":"## Converting into numerical values","metadata":{}},{"cell_type":"code","source":"labels_list = list(set(data_train['ClassName'].values.tolist()))\nlabels_id = {label_name:id for id,label_name in enumerate(labels_list)}\nprint(labels_id)\ndata_train['ClassName'].replace(labels_id,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.245841Z","iopub.execute_input":"2025-12-30T23:29:59.246081Z","iopub.status.idle":"2025-12-30T23:29:59.270533Z","shell.execute_reply.started":"2025-12-30T23:29:59.246050Z","shell.execute_reply":"2025-12-30T23:29:59.269833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(os.path.join(os.getcwd(),\"pickle_files\",\"labels_list.pkl\"),\"wb\") as handle:\n    pickle.dump(labels_id,handle)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.272875Z","iopub.execute_input":"2025-12-30T23:29:59.273087Z","iopub.status.idle":"2025-12-30T23:29:59.276517Z","shell.execute_reply.started":"2025-12-30T23:29:59.273067Z","shell.execute_reply":"2025-12-30T23:29:59.275898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = to_categorical(data_train['ClassName'])\nprint(labels.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.277683Z","iopub.execute_input":"2025-12-30T23:29:59.277897Z","iopub.status.idle":"2025-12-30T23:29:59.288862Z","shell.execute_reply.started":"2025-12-30T23:29:59.277876Z","shell.execute_reply":"2025-12-30T23:29:59.288161Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Splitting into Train and Test sets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nxtrain,xtest,ytrain,ytest = train_test_split(data_train.iloc[:,0],labels,test_size = 0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.290033Z","iopub.execute_input":"2025-12-30T23:29:59.290346Z","iopub.status.idle":"2025-12-30T23:29:59.310795Z","shell.execute_reply.started":"2025-12-30T23:29:59.290321Z","shell.execute_reply":"2025-12-30T23:29:59.309924Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Converting into 64*64 images \nYou can substitute 64,64 to 224,224 for better results only if ram is >32gb","metadata":{}},{"cell_type":"code","source":"\ndef path_to_tensor(img_path):\n    # loads RGB image as PIL.Image.Image type\n    img = image.load_img(img_path, target_size=(64, 64))\n    # convert PIL.Image.Image type to 3D tensor with shape (64, 64, 3)\n    x = image.img_to_array(img)\n    # convert 3D tensor to 4D tensor with shape (1, 64,64, 3) and return 4D tensor\n    return np.expand_dims(x, axis=0)\n\ndef paths_to_tensor(img_paths):\n    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]\n    return np.vstack(list_of_tensors)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.311767Z","iopub.execute_input":"2025-12-30T23:29:59.311982Z","iopub.status.idle":"2025-12-30T23:29:59.316025Z","shell.execute_reply.started":"2025-12-30T23:29:59.311960Z","shell.execute_reply":"2025-12-30T23:29:59.315301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfrom PIL import ImageFile                            \nImageFile.LOAD_TRUNCATED_IMAGES = True                 \n\n# pre-process the data for Keras\ntrain_tensors = paths_to_tensor(xtrain).astype('float32')/255 - 0.5\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:29:59.317264Z","iopub.execute_input":"2025-12-30T23:29:59.317600Z","iopub.status.idle":"2025-12-30T23:33:01.606170Z","shell.execute_reply.started":"2025-12-30T23:29:59.317567Z","shell.execute_reply":"2025-12-30T23:33:01.605392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_tensors = paths_to_tensor(xtest).astype('float32')/255 - 0.5\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-30T23:33:01.607547Z","iopub.execute_input":"2025-12-30T23:33:01.607869Z","iopub.status.idle":"2025-12-30T23:33:45.899884Z","shell.execute_reply.started":"2025-12-30T23:33:01.607834Z","shell.execute_reply":"2025-12-30T23:33:45.899234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"##takes too much ram \n## run this if your ram is greater than 16gb \n# test_tensors = paths_to_tensor(data_test.iloc[:,0]).astype('float32')/255 - 0.5 ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Defining the Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(64,64,3), kernel_initializer='glorot_normal'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128, kernel_size=2, padding='same', activation='relu', kernel_initializer='glorot_normal'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=256, kernel_size=2, padding='same', activation='relu', kernel_initializer='glorot_normal'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=512, kernel_size=2, padding='same', activation='relu', kernel_initializer='glorot_normal'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(500, activation='relu', kernel_initializer='glorot_normal'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax', kernel_initializer='glorot_normal'))\n\n\nmodel.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_model(model,to_file=os.path.join(MODEL_PATH,\"model_distracted_driver.png\"),show_shapes=True,show_layer_names=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filepath = os.path.join(MODEL_PATH,\"distracted-{epoch:02d}-{val_accuracy:.2f}.hdf5\")\ncheckpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max',period=1)\ncallbacks_list = [checkpoint]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_history = model.fit(train_tensors,ytrain,validation_data = (valid_tensors, ytest),epochs=25, batch_size=40, shuffle=True,callbacks=callbacks_list)","metadata":{"scrolled":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(model_history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(model_history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, 25, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(model_history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(model_history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, 25, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"scrolled":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Analysis\n\nFinding the Confusion matrix,Precision,Recall and F1 score to analyse the model thus created ","metadata":{}},{"cell_type":"code","source":"\ndef print_confusion_matrix(confusion_matrix, class_names, figsize = (10,7), fontsize=14):\n    df_cm = pd.DataFrame(\n        confusion_matrix, index=class_names, columns=class_names, \n    )\n    fig = plt.figure(figsize=figsize)\n    try:\n        heatmap = sns.heatmap(df_cm, annot=True, fmt=\"d\")\n    except ValueError:\n        raise ValueError(\"Confusion matrix values must be integers.\")\n    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)\n    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    fig.savefig(os.path.join(MODEL_PATH,\"confusion_matrix.png\"))\n    return fig\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def print_heatmap(n_labels, n_predictions, class_names):\n    labels = n_labels #sess.run(tf.argmax(n_labels, 1))\n    predictions = n_predictions #sess.run(tf.argmax(n_predictions, 1))\n\n#     confusion_matrix = sess.run(tf.contrib.metrics.confusion_matrix(labels, predictions))\n    matrix = confusion_matrix(labels.argmax(axis=1),predictions.argmax(axis=1))\n    row_sum = np.sum(matrix, axis = 1)\n    w, h = matrix.shape\n\n    c_m = np.zeros((w, h))\n\n    for i in range(h):\n        c_m[i] = matrix[i] * 100 / row_sum[i]\n\n    c = c_m.astype(dtype = np.uint8)\n\n    \n    heatmap = print_confusion_matrix(c, class_names, figsize=(18,10), fontsize=20)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = list()\nfor name,idx in labels_id.items():\n    class_names.append(name)\n# print(class_names)\nypred = model.predict(valid_tensors)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_heatmap(ytest,ypred,class_names)","metadata":{"scrolled":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Precision Recall F1 Score","metadata":{}},{"cell_type":"code","source":"ypred_class = np.argmax(ypred,axis=1)\n# print(ypred_class[:10])\nytest = np.argmax(ytest,axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = accuracy_score(ytest,ypred_class)\nprint('Accuracy: %f' % accuracy)\n# precision tp / (tp + fp)\nprecision = precision_score(ytest, ypred_class,average='weighted')\nprint('Precision: %f' % precision)\n# recall: tp / (tp + fn)\nrecall = recall_score(ytest,ypred_class,average='weighted')\nprint('Recall: %f' % recall)\n# f1: 2 tp / (2 tp + fp + fn)\nf1 = f1_score(ytest,ypred_class,average='weighted')\nprint('F1 score: %f' % f1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\n# ypred_class zaten var:\n# ypred_class = np.argmax(ypred, axis=1)\n# ytest zaten int'e çevrildi:\n# ytest = np.argmax(ytest, axis=1)\n\n# labels_id: {\"c0\":0, \"c1\":1, ...} gibi\nid_to_label = {v:k for k,v in labels_id.items()}\ntarget_names = [id_to_label[i] for i in range(len(id_to_label))]\n\nprint(classification_report(\n    ytest,\n    ypred_class,\n    target_names=target_names,\n    digits=4\n))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input/testing","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.preprocessing import image\nimport numpy as np\n\nimg_path = \"/kaggle/input/testing/Distracted driver texting and speeding_.jpeg\"\n\ndef preprocess_external_image(img_path):\n    img = image.load_img(img_path, target_size=(64, 64))\n    img_array = image.img_to_array(img)\n    img_array = img_array.astype('float32') / 255 - 0.5\n    img_array = np.expand_dims(img_array, axis=0)\n    return img_array","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_path = \"/kaggle/input/testing/Distracted driver texting and speeding_.jpeg\"\n\nimg_tensor = preprocess_external_image(img_path)\npred = model.predict(img_tensor)\n\npred_class_id = np.argmax(pred)\nconfidence = np.max(pred)\n\n# id -> sınıf adı (c0, c1, ...)\nid_to_label = {v:k for k,v in labels_id.items()}\npred_class_name = id_to_label[pred_class_id]\n\nprint(\"Tahmin edilen sınıf:\", pred_class_name)\nprint(f\"Confidence: %{confidence*100:.2f}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimg = image.load_img(img_path)\nplt.imshow(img)\nplt.axis('off')\nplt.title(f\"Tahmin: {pred_class_name} (%{confidence*100:.2f})\")\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}