{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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"}],"dockerImageVersionId":31236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip -q install -U fastai\n\nfrom fastai.vision.all import *\nimport fastai\nprint(\"fastai:\", fastai.__version__)\nprint(\"ImageDataLoaders exists?\", \"ImageDataLoaders\" in dir())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:29:34.487912Z","iopub.execute_input":"2025-12-21T17:29:34.488790Z","iopub.status.idle":"2025-12-21T17:29:51.622059Z","shell.execute_reply.started":"2025-12-21T17:29:34.488747Z","shell.execute_reply":"2025-12-21T17:29:51.621371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pathlib import Path\npath = Path(\"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\")\n\ndls = ImageDataLoaders.from_folder(\n    path,\n    valid_pct=0.2,\n    seed=42,\n    item_tfms=Resize(224),\n    batch_tfms=[*aug_transforms(), Normalize.from_stats(*imagenet_stats)],\n    bs=32\n)\n\nprint(dls.vocab)\ndls.show_batch(max_n=9, figsize=(7,8))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:29:51.623528Z","iopub.execute_input":"2025-12-21T17:29:51.623785Z","iopub.status.idle":"2025-12-21T17:30:24.670931Z","shell.execute_reply.started":"2025-12-21T17:29:51.623753Z","shell.execute_reply":"2025-12-21T17:30:24.670196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.metrics import error_rate # 1 - accuracy\nlearn = cnn_learner(\n    dls,\n    resnet34,\n    metrics=error_rate\n)\nlearn.path = Path(\"/kaggle/working\")\nlearn.model_dir = \"models\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:30:24.671975Z","iopub.execute_input":"2025-12-21T17:30:24.672239Z","iopub.status.idle":"2025-12-21T17:30:25.656861Z","shell.execute_reply.started":"2025-12-21T17:30:24.672211Z","shell.execute_reply":"2025-12-21T17:30:25.656228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:30:25.657751Z","iopub.execute_input":"2025-12-21T17:30:25.658070Z","iopub.status.idle":"2025-12-21T17:30:25.662903Z","shell.execute_reply.started":"2025-12-21T17:30:25.658017Z","shell.execute_reply":"2025-12-21T17:30:25.662254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.callback.tracker import EarlyStoppingCallback, SaveModelCallback\n\ncbs = [\n    EarlyStoppingCallback(monitor='valid_loss', patience=5),\n    SaveModelCallback(monitor='valid_loss', fname='best_resnet34')\n]\n\nlearn.fine_tune(10, cbs=cbs)\nlearn.recorder.plot_loss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:30:25.664771Z","iopub.execute_input":"2025-12-21T17:30:25.665012Z","iopub.status.idle":"2025-12-21T17:44:12.506131Z","shell.execute_reply.started":"2025-12-21T17:30:25.664976Z","shell.execute_reply":"2025-12-21T17:44:12.505407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls {learn.path/'models'}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:44:12.507356Z","iopub.execute_input":"2025-12-21T17:44:12.507638Z","iopub.status.idle":"2025-12-21T17:44:12.659626Z","shell.execute_reply.started":"2025-12-21T17:44:12.507603Z","shell.execute_reply":"2025-12-21T17:44:12.658738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.load('best_resnet34')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:44:12.661075Z","iopub.execute_input":"2025-12-21T17:44:12.661585Z","iopub.status.idle":"2025-12-21T17:44:12.778870Z","shell.execute_reply.started":"2025-12-21T17:44:12.661551Z","shell.execute_reply":"2025-12-21T17:44:12.778297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def find_appropriate_lr(model:Learner, lr_diff:int = 15, loss_threshold:float = .05, adjust_value:float = 1, plot:bool = False) -> float:\n    #Run the Learning Rate Finder\n    model.lr_find()\n    \n    #Get loss values and their corresponding gradients, and get lr values\n    losses = np.array(model.recorder.losses)\n    min_loss_index = np.argmin(losses)\n    \n    \n    #loss_grad = np.gradient(losses)\n    lrs = model.recorder.lrs\n    \n    #return the learning rate that produces the minimum loss divide by 10   \n    return lrs[min_loss_index] / 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:44:12.779808Z","iopub.execute_input":"2025-12-21T17:44:12.780125Z","iopub.status.idle":"2025-12-21T17:44:12.784677Z","shell.execute_reply.started":"2025-12-21T17:44:12.780085Z","shell.execute_reply":"2025-12-21T17:44:12.784139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nimport numpy as np\n\n# Validation set tahminleri\npreds, targs = learn.get_preds(dl=dls.valid)   # data = ImageDataLoaders\ny_pred = preds.argmax(dim=1).cpu().numpy()\ny_true = targs.cpu().numpy()\n\n# Sınıf isimleri\nclass_names = dls.vocab  # örn: ['Cam Atık', 'Kağıt Atık', ...]\n\n# Classification report\nreport = classification_report(\n    y_true, y_pred,\n    target_names=class_names,\n    digits=4\n)\n\nprint(report)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:44:12.785639Z","iopub.execute_input":"2025-12-21T17:44:12.786240Z","iopub.status.idle":"2025-12-21T17:44:24.454411Z","shell.execute_reply.started":"2025-12-21T17:44:12.786212Z","shell.execute_reply":"2025-12-21T17:44:24.453588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.recorder.plot_metrics()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:44:24.455699Z","iopub.execute_input":"2025-12-21T17:44:24.456036Z","iopub.status.idle":"2025-12-21T17:44:24.466931Z","shell.execute_reply.started":"2025-12-21T17:44:24.455998Z","shell.execute_reply":"2025-12-21T17:44:24.466023Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(figsize=(8,8))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:45:33.760531Z","iopub.execute_input":"2025-12-21T17:45:33.760863Z","iopub.status.idle":"2025-12-21T17:45:57.390434Z","shell.execute_reply.started":"2025-12-21T17:45:33.760830Z","shell.execute_reply":"2025-12-21T17:45:57.389552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"interp.print_classification_report()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:46:13.179756Z","iopub.execute_input":"2025-12-21T17:46:13.180146Z","iopub.status.idle":"2025-12-21T17:46:24.748691Z","shell.execute_reply.started":"2025-12-21T17:46:13.180103Z","shell.execute_reply":"2025-12-21T17:46:24.748005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(10,10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-21T17:46:27.916226Z","iopub.execute_input":"2025-12-21T17:46:27.916568Z","iopub.status.idle":"2025-12-21T17:46:28.854165Z","shell.execute_reply.started":"2025-12-21T17:46:27.916530Z","shell.execute_reply":"2025-12-21T17:46:28.853138Z"}},"outputs":[],"execution_count":null}]}