{"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":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os \n\nimport tensorflow as tf \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator \nfrom tensorflow.keras import Model \nfrom tensorflow.keras.layers import Input , GlobalAveragePooling2D , BatchNormalization , Dropout , Dense \nfrom tensorflow.keras.applications import EfficientNetB3 \nfrom tensorflow.keras.callbacks import ModelCheckpoint , EarlyStopping ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-05T07:40:59.925945Z","iopub.execute_input":"2022-10-05T07:40:59.926623Z","iopub.status.idle":"2022-10-05T07:41:03.271352Z","shell.execute_reply.started":"2022-10-05T07:40:59.926531Z","shell.execute_reply":"2022-10-05T07:41:03.270208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:03.273054Z","iopub.execute_input":"2022-10-05T07:41:03.274005Z","iopub.status.idle":"2022-10-05T07:41:04.423781Z","shell.execute_reply.started":"2022-10-05T07:41:03.273961Z","shell.execute_reply":"2022-10-05T07:41:04.422624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir = \"../input/state-farm-distracted-driver-detection\"\ntrain_dir = os.path.join(dir , 'imgs/train/')\ntest_dir = os.path.join(dir , 'imgs/test/')\ndata = pd.read_csv(os.path.join(dir , 'driver_imgs_list.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.425345Z","iopub.execute_input":"2022-10-05T07:41:04.425657Z","iopub.status.idle":"2022-10-05T07:41:04.451664Z","shell.execute_reply.started":"2022-10-05T07:41:04.425628Z","shell.execute_reply":"2022-10-05T07:41:04.450823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.454952Z","iopub.execute_input":"2022-10-05T07:41:04.455245Z","iopub.status.idle":"2022-10-05T07:41:04.471991Z","shell.execute_reply.started":"2022-10-05T07:41:04.455219Z","shell.execute_reply":"2022-10-05T07:41:04.470988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts = data.classname.value_counts()\n\nclass_counts.plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.473440Z","iopub.execute_input":"2022-10-05T07:41:04.473879Z","iopub.status.idle":"2022-10-05T07:41:04.696371Z","shell.execute_reply.started":"2022-10-05T07:41:04.473841Z","shell.execute_reply":"2022-10-05T07:41:04.695354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = (224 ,224)\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.697899Z","iopub.execute_input":"2022-10-05T07:41:04.698238Z","iopub.status.idle":"2022-10-05T07:41:04.703034Z","shell.execute_reply.started":"2022-10-05T07:41:04.698202Z","shell.execute_reply":"2022-10-05T07:41:04.702024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = ImageDataGenerator(width_shift_range = 0.3,\n                               height_shift_range = 0.3,\n                               shear_range = 0.3,\n                               zoom_range = 0.4,\n                               validation_split = 0.2)\n\ntest_gen = ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.704549Z","iopub.execute_input":"2022-10-05T07:41:04.705193Z","iopub.status.idle":"2022-10-05T07:41:04.713711Z","shell.execute_reply.started":"2022-10-05T07:41:04.705155Z","shell.execute_reply":"2022-10-05T07:41:04.712746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_gen.flow_from_directory(train_dir,\n                                         target_size=IMG_SIZE,\n                                         batch_size=BATCH_SIZE,\n                                         subset='training',\n                                         seed = 42)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:04.714926Z","iopub.execute_input":"2022-10-05T07:41:04.715863Z","iopub.status.idle":"2022-10-05T07:41:10.128416Z","shell.execute_reply.started":"2022-10-05T07:41:04.715824Z","shell.execute_reply":"2022-10-05T07:41:10.126542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data = train_gen.flow_from_directory(train_dir,\n                                         target_size=IMG_SIZE,\n                                         batch_size=BATCH_SIZE,\n                                         subset='validation',\n                                         seed = 42)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:10.129881Z","iopub.execute_input":"2022-10-05T07:41:10.130191Z","iopub.status.idle":"2022-10-05T07:41:10.566526Z","shell.execute_reply.started":"2022-10-05T07:41:10.130163Z","shell.execute_reply":"2022-10-05T07:41:10.565420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model(num_class):\n    inputs = Input(shape=(224,224,3))\n    base = EfficientNetB3(include_top = False,\n                         weights='imagenet')(inputs)\n    x = GlobalAveragePooling2D()(base)\n    x = BatchNormalization()(x)\n    x = Dropout(0.2)(x)\n    outputs = Dense(num_class , activation='softmax')(x)\n    \n    model = Model(inputs = inputs , outputs = outputs)\n    model.compile(loss = 'categorical_crossentropy',\n                 optimizer = tf.optimizers.Adam(1e-4),\n                 metrics = ['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:10.569686Z","iopub.execute_input":"2022-10-05T07:41:10.569982Z","iopub.status.idle":"2022-10-05T07:41:10.578770Z","shell.execute_reply.started":"2022-10-05T07:41:10.569953Z","shell.execute_reply":"2022-10-05T07:41:10.577813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_class = len(class_counts )\nnum_class","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:10.580622Z","iopub.execute_input":"2022-10-05T07:41:10.581350Z","iopub.status.idle":"2022-10-05T07:41:10.591897Z","shell.execute_reply.started":"2022-10-05T07:41:10.581313Z","shell.execute_reply":"2022-10-05T07:41:10.590770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model(num_class)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:10.593845Z","iopub.execute_input":"2022-10-05T07:41:10.594434Z","iopub.status.idle":"2022-10-05T07:41:15.300275Z","shell.execute_reply.started":"2022-10-05T07:41:10.594387Z","shell.execute_reply":"2022-10-05T07:41:15.299292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ck = ModelCheckpoint('best_model.hdf5' , save_best_only=True,monitor='val_loss',mode='min')\nes = EarlyStopping(monitor = 'val_loss', patience = 5)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:15.304180Z","iopub.execute_input":"2022-10-05T07:41:15.306162Z","iopub.status.idle":"2022-10-05T07:41:15.310927Z","shell.execute_reply.started":"2022-10-05T07:41:15.306131Z","shell.execute_reply":"2022-10-05T07:41:15.309608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib \nprint(device_lib.list_local_devices())","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:15.312135Z","iopub.execute_input":"2022-10-05T07:41:15.312858Z","iopub.status.idle":"2022-10-05T07:41:15.326620Z","shell.execute_reply.started":"2022-10-05T07:41:15.312817Z","shell.execute_reply":"2022-10-05T07:41:15.325664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" tf.config.list_physical_devices('GPU')","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:15.328037Z","iopub.execute_input":"2022-10-05T07:41:15.328637Z","iopub.status.idle":"2022-10-05T07:41:15.338998Z","shell.execute_reply.started":"2022-10-05T07:41:15.328600Z","shell.execute_reply":"2022-10-05T07:41:15.337853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:15.340793Z","iopub.execute_input":"2022-10-05T07:41:15.341276Z","iopub.status.idle":"2022-10-05T07:41:15.349362Z","shell.execute_reply.started":"2022-10-05T07:41:15.341239Z","shell.execute_reply":"2022-10-05T07:41:15.348339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data , \n                   epochs = 10 ,\n                   validation_data = val_data ,\n                   callbacks=[es,ck],\n                   verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T07:41:15.350852Z","iopub.execute_input":"2022-10-05T07:41:15.351408Z","iopub.status.idle":"2022-10-05T08:54:53.963275Z","shell.execute_reply.started":"2022-10-05T07:41:15.351362Z","shell.execute_reply":"2022-10-05T08:54:53.962337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = os.path.join(dir , 'imgs')\ntest_data = test_gen.flow_from_directory(test_dir ,\n                                        target_size=IMG_SIZE,\n                                        batch_size=BATCH_SIZE,\n                                        classes = ['test'],\n                                        shuffle = False)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-05T08:59:12.147913Z","iopub.execute_input":"2022-10-05T08:59:12.148314Z","iopub.status.idle":"2022-10-05T09:01:08.113379Z","shell.execute_reply.started":"2022-10-05T08:59:12.148281Z","shell.execute_reply":"2022-10-05T09:01:08.112193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel = load_model('./best_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-10-05T09:20:48.998848Z","iopub.execute_input":"2022-10-05T09:20:48.999413Z","iopub.status.idle":"2022-10-05T09:20:54.263293Z","shell.execute_reply.started":"2022-10-05T09:20:48.999368Z","shell.execute_reply":"2022-10-05T09:20:54.262292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_data)\npreds","metadata":{"execution":{"iopub.status.busy":"2022-10-05T09:20:57.425449Z","iopub.execute_input":"2022-10-05T09:20:57.426618Z","iopub.status.idle":"2022-10-05T10:01:32.442481Z","shell.execute_reply.started":"2022-10-05T09:20:57.426571Z","shell.execute_reply":"2022-10-05T10:01:32.441425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T10:07:56.902952Z","iopub.execute_input":"2022-10-05T10:07:56.903314Z","iopub.status.idle":"2022-10-05T10:07:56.909369Z","shell.execute_reply.started":"2022-10-05T10:07:56.903284Z","shell.execute_reply":"2022-10-05T10:07:56.908441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = os.path.join(dir , 'imgs/test')\ntest_ids = sorted(os.listdir(test_imgs))\npred_df = pd.DataFrame(columns=['img','c0','c1','c2','c3','c4','c5','c6','c7','c8','c9'])\nfor i in range(len(preds)):\n    pred_df.loc[i,'img'] = test_ids[i]\n    pred_df.loc[i , 'c0':'c9'] = preds[i]\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-05T10:08:10.807136Z","iopub.execute_input":"2022-10-05T10:08:10.807535Z","iopub.status.idle":"2022-10-05T10:18:29.099909Z","shell.execute_reply.started":"2022-10-05T10:08:10.807498Z","shell.execute_reply":"2022-10-05T10:18:29.098878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df","metadata":{"execution":{"iopub.status.busy":"2022-10-05T10:26:48.802976Z","iopub.execute_input":"2022-10-05T10:26:48.803886Z","iopub.status.idle":"2022-10-05T10:26:48.822416Z","shell.execute_reply.started":"2022-10-05T10:26:48.803845Z","shell.execute_reply":"2022-10-05T10:26:48.821285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T10:26:51.343601Z","iopub.execute_input":"2022-10-05T10:26:51.343982Z","iopub.status.idle":"2022-10-05T10:26:52.472129Z","shell.execute_reply.started":"2022-10-05T10:26:51.343949Z","shell.execute_reply":"2022-10-05T10:26:52.471137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}