{"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":"\nimport numpy as np \nimport pandas as pd \nimport seaborn as sns\n\nimport fastai\nfrom fastai.vision.all import *\nfrom fastai.metrics import error_rate\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-25T08:41:32.360589Z","iopub.execute_input":"2022-02-25T08:41:32.360922Z","iopub.status.idle":"2022-02-25T08:41:35.628977Z","shell.execute_reply.started":"2022-02-25T08:41:32.360834Z","shell.execute_reply":"2022-02-25T08:41:35.628116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Question 1, 2 & 3**","metadata":{}},{"cell_type":"markdown","source":"Chargement du jeu","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/state-farm-distracted-driver-detection/'\nimg_list = pd.read_csv(path + 'driver_imgs_list.csv')\nimg_list.head(1000)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.632556Z","iopub.execute_input":"2022-02-25T08:41:35.632762Z","iopub.status.idle":"2022-02-25T08:41:35.683733Z","shell.execute_reply.started":"2022-02-25T08:41:35.632736Z","shell.execute_reply":"2022-02-25T08:41:35.683072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Taille du jeu: \", img_list.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.685001Z","iopub.execute_input":"2022-02-25T08:41:35.685407Z","iopub.status.idle":"2022-02-25T08:41:35.693407Z","shell.execute_reply.started":"2022-02-25T08:41:35.685369Z","shell.execute_reply":"2022-02-25T08:41:35.692681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Nombre de conducteurs uniques: \", len(img_list.subject.sort_values().unique())) \n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.695635Z","iopub.execute_input":"2022-02-25T08:41:35.696035Z","iopub.status.idle":"2022-02-25T08:41:35.725952Z","shell.execute_reply.started":"2022-02-25T08:41:35.695995Z","shell.execute_reply":"2022-02-25T08:41:35.724942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Nombre de classes: \", len(img_list.classname.sort_values().unique())) \n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.727676Z","iopub.execute_input":"2022-02-25T08:41:35.728004Z","iopub.status.idle":"2022-02-25T08:41:35.756409Z","shell.execute_reply.started":"2022-02-25T08:41:35.727951Z","shell.execute_reply":"2022-02-25T08:41:35.755488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Nombre d'occurences des individus: \", img_list['subject'].value_counts()) \n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.757853Z","iopub.execute_input":"2022-02-25T08:41:35.758724Z","iopub.status.idle":"2022-02-25T08:41:35.770377Z","shell.execute_reply.started":"2022-02-25T08:41:35.758678Z","shell.execute_reply":"2022-02-25T08:41:35.769388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"occur= img_list.groupby([\"subject\", \"classname\"]).size().reset_index(name=\"occurence\")\nprint(occur.shape[0],\"signifie que chaque individu a des images dans les 10 classes\") \n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.771832Z","iopub.execute_input":"2022-02-25T08:41:35.772489Z","iopub.status.idle":"2022-02-25T08:41:35.792236Z","shell.execute_reply.started":"2022-02-25T08:41:35.772449Z","shell.execute_reply":"2022-02-25T08:41:35.791453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Ensemble de validation**","metadata":{}},{"cell_type":"markdown","source":"### Sélection de 25% pour l'ensemble de validation. \n### Vu que 25% de 26 correspond à 6.5, nous choisirons 7 conducteurs parmi les 26 pour notre ensemble.\n### Ici nous pouvons prendre n'importe quel conducteur pour notre division vu qu'ils ont tous des images dans les 10 classes\n","metadata":{}},{"cell_type":"code","source":"valid_subjects = img_list.subject.sort_values().unique()[-7:]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.793434Z","iopub.execute_input":"2022-02-25T08:41:35.793684Z","iopub.status.idle":"2022-02-25T08:41:35.822095Z","shell.execute_reply.started":"2022-02-25T08:41:35.793648Z","shell.execute_reply":"2022-02-25T08:41:35.821356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creation d'une column is_valid qui affiche True pour les lignes qui seront dans l'ensemble de validation \n","metadata":{}},{"cell_type":"code","source":"\nimg_list['is_valid'] = img_list['subject'].isin(valid_subjects)\n\nprint(\"Conducteur de l'ensemble de validation: \", valid_subjects)\nprint(img_list[img_list['is_valid']==True].subject.count())","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.823121Z","iopub.execute_input":"2022-02-25T08:41:35.824933Z","iopub.status.idle":"2022-02-25T08:41:35.837828Z","shell.execute_reply.started":"2022-02-25T08:41:35.824892Z","shell.execute_reply":"2022-02-25T08:41:35.836832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.841752Z","iopub.execute_input":"2022-02-25T08:41:35.841982Z","iopub.status.idle":"2022-02-25T08:41:35.855227Z","shell.execute_reply.started":"2022-02-25T08:41:35.841955Z","shell.execute_reply":"2022-02-25T08:41:35.854377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### nouvelle colnne avec class + nom du fichier image\n","metadata":{}},{"cell_type":"code","source":"img_list['img_path'] = img_list.classname + '/' + img_list.img\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.856981Z","iopub.execute_input":"2022-02-25T08:41:35.857537Z","iopub.status.idle":"2022-02-25T08:41:35.871382Z","shell.execute_reply.started":"2022-02-25T08:41:35.857494Z","shell.execute_reply":"2022-02-25T08:41:35.870693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n### Valid_names contient uniquement les noms des images de l'echantillon test\n","metadata":{}},{"cell_type":"code","source":"valid_names = img_list[img_list['subject'].isin(valid_subjects)].img\nvalid_names = valid_names.to_list()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.873059Z","iopub.execute_input":"2022-02-25T08:41:35.873422Z","iopub.status.idle":"2022-02-25T08:41:35.886586Z","shell.execute_reply.started":"2022-02-25T08:41:35.873383Z","shell.execute_reply":"2022-02-25T08:41:35.885751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.889363Z","iopub.execute_input":"2022-02-25T08:41:35.889999Z","iopub.status.idle":"2022-02-25T08:41:35.907913Z","shell.execute_reply.started":"2022-02-25T08:41:35.889959Z","shell.execute_reply":"2022-02-25T08:41:35.907177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Chargement des images avec la methode from_df","metadata":{}},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(df=img_list, path = path + 'imgs/train/', fn_col=4, label_col=1, valid_col='is_valid',seed=2021)\ndls","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:35.909111Z","iopub.execute_input":"2022-02-25T08:41:35.909491Z","iopub.status.idle":"2022-02-25T08:41:41.173425Z","shell.execute_reply.started":"2022-02-25T08:41:35.909450Z","shell.execute_reply":"2022-02-25T08:41:41.172678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualisation des images**","metadata":{}},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:41.174814Z","iopub.execute_input":"2022-02-25T08:41:41.175076Z","iopub.status.idle":"2022-02-25T08:41:43.993926Z","shell.execute_reply.started":"2022-02-25T08:41:41.175042Z","shell.execute_reply":"2022-02-25T08:41:43.993107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Question 4","metadata":{}},{"cell_type":"markdown","source":"### Définissons le modèle d'apprentissage(resnet18) et choix du meilleur taux d'apprentissage","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet18, metrics=error_rate, model_dir=\"/tmp/model/\")\nlrate = learn.lr_find(suggest_funcs=(minimum, steep, valley, slide)) \nlrate ","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:41:43.994955Z","iopub.execute_input":"2022-02-25T08:41:43.995176Z","iopub.status.idle":"2022-02-25T08:42:59.122560Z","shell.execute_reply.started":"2022-02-25T08:41:43.995146Z","shell.execute_reply":"2022-02-25T08:42:59.121669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Entraînement sur 5 epochs**","metadata":{}},{"cell_type":"code","source":"learn.fine_tune(5,base_lr=lrate.minimum, cbs=[ShowGraphCallback(),])","metadata":{"execution":{"iopub.status.busy":"2022-02-25T08:42:59.124258Z","iopub.execute_input":"2022-02-25T08:42:59.124730Z","iopub.status.idle":"2022-02-25T09:08:32.448904Z","shell.execute_reply.started":"2022-02-25T08:42:59.124686Z","shell.execute_reply":"2022-02-25T09:08:32.447798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Question 5**","metadata":{}},{"cell_type":"markdown","source":"**Entraînement sur 10 epochs pour rechercher la dégradation**","metadata":{}},{"cell_type":"code","source":"learn.fine_tune(10,base_lr=lrate.minimum, cbs=[ShowGraphCallback(),])","metadata":{"execution":{"iopub.status.busy":"2022-02-25T09:08:32.450475Z","iopub.execute_input":"2022-02-25T09:08:32.452958Z","iopub.status.idle":"2022-02-25T09:54:00.002692Z","shell.execute_reply.started":"2022-02-25T09:08:32.452915Z","shell.execute_reply":"2022-02-25T09:53:59.994896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"La meilleure généralisation se fait au 4ème epoch, après cela il y a une dégradation ","metadata":{}},{"cell_type":"markdown","source":"**Question 6**","metadata":{}},{"cell_type":"code","source":"transform = [Rotate(360, p=1, mode='bilinear'), Brightness(max_lighting=0.9, p=0.35),Contrast(max_lighting=0.4, p=0.25),\n              RandomErasing(p=0.2, sl=0.0, sh=0.2, min_aspect=0.3, max_count=2), Flip(p=1), \n             Zoom(max_zoom=1.1,p=0.1), RandomResizedCrop(224)]\n\ndls2 = ImageDataLoaders.from_df(df=img_list, path = path + 'imgs/train/', fn_col=4, label_col=1, valid_col='is_valid',seed=2021, batch_tfms=transform)\n\n\ndls2.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T10:28:57.821383Z","iopub.execute_input":"2022-02-25T10:28:57.821690Z","iopub.status.idle":"2022-02-25T10:29:02.964229Z","shell.execute_reply.started":"2022-02-25T10:28:57.821655Z","shell.execute_reply":"2022-02-25T10:29:02.962862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn2 = cnn_learner(dls2, resnet18, metrics=error_rate, model_dir=\"/tmp/model/\")\nlrate2 = learn2.lr_find(suggest_funcs=(minimum, steep, valley, slide))\nlrate2","metadata":{"execution":{"iopub.status.busy":"2022-02-25T10:29:06.260096Z","iopub.execute_input":"2022-02-25T10:29:06.260428Z","iopub.status.idle":"2022-02-25T10:30:10.304958Z","shell.execute_reply.started":"2022-02-25T10:29:06.260395Z","shell.execute_reply":"2022-02-25T10:30:10.304046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn2.fine_tune(epochs=5, base_lr=lrate2.minimum, cbs=[ShowGraphCallback(),])","metadata":{"execution":{"iopub.status.busy":"2022-02-25T09:55:18.081321Z","iopub.execute_input":"2022-02-25T09:55:18.081867Z","iopub.status.idle":"2022-02-25T10:21:38.527777Z","shell.execute_reply.started":"2022-02-25T09:55:18.081817Z","shell.execute_reply":"2022-02-25T10:21:38.524861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Question 7**","metadata":{}},{"cell_type":"markdown","source":"Un problème de mémoire survient lorsque nous exécutons les codes ci-dessous","metadata":{}},{"cell_type":"code","source":"result = ClassificationInterpretation.from_learner(learn2)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:20:20.228781Z","iopub.execute_input":"2022-02-24T10:20:20.229055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:05:46.443936Z","iopub.execute_input":"2022-02-24T10:05:46.444661Z","iopub.status.idle":"2022-02-24T10:05:46.453527Z","shell.execute_reply.started":"2022-02-24T10:05:46.444623Z","shell.execute_reply":"2022-02-24T10:05:46.452821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.print_classification_report()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:05:46.454789Z","iopub.execute_input":"2022-02-24T10:05:46.455069Z","iopub.status.idle":"2022-02-24T10:05:46.46306Z","shell.execute_reply.started":"2022-02-24T10:05:46.455037Z","shell.execute_reply":"2022-02-24T10:05:46.462147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Question 8","metadata":{}},{"cell_type":"code","source":"# result.most_confused(min_val=50)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:05:46.464288Z","iopub.execute_input":"2022-02-24T10:05:46.464776Z","iopub.status.idle":"2022-02-24T10:05:46.472288Z","shell.execute_reply.started":"2022-02-24T10:05:46.464741Z","shell.execute_reply":"2022-02-24T10:05:46.471652Z"},"trusted":true},"execution_count":null,"outputs":[]}]}