{"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":"markdown","source":"# Importing Modules","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport os\nimport cv2\nfrom glob import glob\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport random\nimport keras\nfrom keras.preprocessing import image\nfrom keras import regularizers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import RMSprop,Adam, SGD\nfrom sklearn.model_selection import train_test_split\nimport keras.layers as L\nimport tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T12:47:39.770967Z","iopub.execute_input":"2023-02-11T12:47:39.771389Z","iopub.status.idle":"2023-02-11T12:47:45.628301Z","shell.execute_reply.started":"2023-02-11T12:47:39.771302Z","shell.execute_reply":"2023-02-11T12:47:45.627314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data","metadata":{}},{"cell_type":"code","source":"BASE_DIR=('/kaggle/input/class-analysis-attention-version2/class_analysis_attention_version2')\ntrain_dir=os.path.join(BASE_DIR,'train/')\ntest_dir=os.path.join(BASE_DIR,'test/')\n\nprint('Number of images in training set = ',str(len(glob(train_dir+'*/*'))))\nprint('Number of images in testing set = ',str(len(glob(test_dir+'*'))))","metadata":{"execution":{"iopub.status.busy":"2023-02-11T12:51:10.059335Z","iopub.execute_input":"2023-02-11T12:51:10.059706Z","iopub.status.idle":"2023-02-11T12:51:11.045957Z","shell.execute_reply.started":"2023-02-11T12:51:10.059671Z","shell.execute_reply":"2023-02-11T12:51:11.044926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making Train, validation and test directories","metadata":{}},{"cell_type":"code","source":"class_labels=['write','drink','listen','phone','trance']\n\n#training directories\nfor label in class_labels:\n    tf.io.gfile.makedirs('/kaggle/working/train_dataset/'+label+'/')\n    \n#validation directories\nfor label in class_labels:\n    tf.io.gfile.makedirs('/kaggle/working/val_dataset/'+label+'/')\n    \n#test directories\nfor label in class_labels:\n    tf.io.gfile.makedirs('/kaggle/working/test_dataset/'+label+'/')\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T12:53:11.324367Z","iopub.execute_input":"2023-02-11T12:53:11.324718Z","iopub.status.idle":"2023-02-11T12:53:11.330756Z","shell.execute_reply.started":"2023-02-11T12:53:11.324685Z","shell.execute_reply":"2023-02-11T12:53:11.329677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"os.listdir(\"/kaggle/working/train_dataset/\")","metadata":{"execution":{"iopub.status.busy":"2023-02-11T12:53:18.864937Z","iopub.execute_input":"2023-02-11T12:53:18.865267Z","iopub.status.idle":"2023-02-11T12:53:18.874402Z","shell.execute_reply.started":"2023-02-11T12:53:18.865228Z","shell.execute_reply":"2023-02-11T12:53:18.873416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The predicted 10 classes\n* c0: write\n* c1: drink\n* c2: listen\n* c3: phone\n* c4: trance","metadata":{}},{"cell_type":"code","source":"WRITE=os.path.join(train_dir,'c0/')\nDRINK=os.path.join(train_dir,'c1/')\nLISTEN=os.path.join(train_dir,'c2/')\nPHONE=os.path.join(train_dir,'c3/')\nTRANCE=os.path.join(train_dir,'c4/')\n\n\nprint(\"write = \",len(os.listdir(WRITE)))\nprint(\"drink = \",len(os.listdir(DRINK)))\nprint(\"listen = \",len(os.listdir(LISTEN)))\nprint(\"phone = \",len(os.listdir(PHONE)))\nprint(\"trance = \",len(os.listdir(TRANCE)))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T12:57:28.535158Z","iopub.execute_input":"2023-02-11T12:57:28.535538Z","iopub.status.idle":"2023-02-11T12:57:28.567290Z","shell.execute_reply.started":"2023-02-11T12:57:28.535480Z","shell.execute_reply":"2023-02-11T12:57:28.566469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of images in respective directories\n\nimport pathlib \nimport cv2\nimport shutil\n\ndef distribution(param1, param2, param3):\n\n#     data_dir = pathlib.Path(param2)\n#     data_dir\n\n    a=os.listdir(param3)\n\n    test = a[:539]\n    val = a[540:1080]\n    train = a[1080:]\n\n\n    for images in test:\n#         print(f\"../input/state-farm-distracted-driver-detection/imgs/train/{param1}/\"+images, f\"/kaggle/working/test_dataset/{param2}/\")\n        shutil.copy(f\"/kaggle/input/class-analysis-attention-version2/class_analysis_attention_version2/train/{param1}/\"+images, f\"/kaggle/working/test_dataset/{param2}/\")\n    for images in val:\n        shutil.copy(f\"/kaggle/input/class-analysis-attention-version2/class_analysis_attention_version2/train/{param1}/\"+images, f\"/kaggle/working/val_dataset/{param2}/\")\n\n    for images in train:\n        shutil.copy(f\"/kaggle/input/class-analysis-attention-version2/class_analysis_attention_version2/train/{param1}/\"+images, f\"/kaggle/working/train_dataset/{param2}/\")\n\n\n    print(f\"The count of images for test_dataset > {param2} \",len(os.listdir(f\"/kaggle/working/test_dataset/{param2}\")))\n    print(f\"The count of images for val_dataset > {param2} \",len(os.listdir(f\"/kaggle/working/val_dataset/{param2}\")))\n    print(f\"The count of images for train_dataset > {param2} \",len(os.listdir(f\"/kaggle/working/train_dataset/{param2}\")))\n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:05:53.094954Z","iopub.execute_input":"2023-02-11T13:05:53.095279Z","iopub.status.idle":"2023-02-11T13:05:53.102144Z","shell.execute_reply.started":"2023-02-11T13:05:53.095245Z","shell.execute_reply":"2023-02-11T13:05:53.101182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_list = [WRITE, DRINK, LISTEN, PHONE, TRANCE]\ni=0\nfor class_label in class_labels:\n    print(f\"c{i}\")\n    distribution(f\"c{i}\", class_label, dir_list[i]) \n    i+=1\n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:06:00.115701Z","iopub.execute_input":"2023-02-11T13:06:00.116133Z","iopub.status.idle":"2023-02-11T13:06:28.045135Z","shell.execute_reply.started":"2023-02-11T13:06:00.116087Z","shell.execute_reply":"2023-02-11T13:06:28.044230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Starting with Image Preprocessing**","metadata":{}},{"cell_type":"code","source":"import PIL\nimport pathlib\n\ndata_dir = \"/kaggle/working/train_dataset/\"\n\ndata_dir = pathlib.Path(data_dir)\n\n\nprint(\"The count of total images for training set \",len(list(data_dir.glob('*/*.jpg'))))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:06:54.883416Z","iopub.execute_input":"2023-02-11T13:06:54.883780Z","iopub.status.idle":"2023-02-11T13:06:55.135648Z","shell.execute_reply.started":"2023-02-11T13:06:54.883748Z","shell.execute_reply":"2023-02-11T13:06:55.134598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Mapping","metadata":{}},{"cell_type":"code","source":"all_training_images = {\n    'write' :  list(data_dir.glob('write/*')),\n    'drink' :  list(data_dir.glob('drink/*')),\n    'listen' :  list(data_dir.glob('listen/*')),\n    'phone' :  list(data_dir.glob('phone/*')),\n    'trance' :  list(data_dir.glob('trance/*')),\n    \n}","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:09:45.551992Z","iopub.execute_input":"2023-02-11T13:09:45.552370Z","iopub.status.idle":"2023-02-11T13:09:45.805224Z","shell.execute_reply.started":"2023-02-11T13:09:45.552336Z","shell.execute_reply":"2023-02-11T13:09:45.804185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Getting count of each directory","metadata":{}},{"cell_type":"code","source":"# Gives all the count of images in all the directories of training dataset\n\n\nfor class_label, img_count in all_training_images.items():\n    print(class_label)\n    print(len(img_count))","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:09:48.644379Z","iopub.execute_input":"2023-02-11T13:09:48.644750Z","iopub.status.idle":"2023-02-11T13:09:48.652274Z","shell.execute_reply.started":"2023-02-11T13:09:48.644713Z","shell.execute_reply":"2023-02-11T13:09:48.651419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Setting variable of path of all three directories","metadata":{}},{"cell_type":"code","source":"updated_train_dir=\"/kaggle/working/train_dataset/\"\nupdated_val_dir=\"/kaggle/working/val_dataset/\"\nupdated_test_dir=\"/kaggle/working/test_dataset/\"\nprint(os.listdir(updated_train_dir))\nprint(os.listdir(updated_val_dir))\nprint(os.listdir(updated_test_dir))","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:09:53.497402Z","iopub.execute_input":"2023-02-11T13:09:53.497773Z","iopub.status.idle":"2023-02-11T13:09:53.504555Z","shell.execute_reply.started":"2023-02-11T13:09:53.497740Z","shell.execute_reply":"2023-02-11T13:09:53.503425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating Image data generator ","metadata":{}},{"cell_type":"markdown","source":"### Creating models\n","metadata":{}},{"cell_type":"code","source":"train_datagen3=ImageDataGenerator(rescale=1.0/255,\n                                 rotation_range=45,\n                                 width_shift_range=0.2,\n                                 height_shift_range=0.2,\n                                 shear_range=0.2,\n                                 zoom_range=0.2,\n                                 horizontal_flip=True,\n                                 fill_mode='nearest')\n\nval_datagen3=ImageDataGenerator(rescale=1.0/255)\n\ntest_datagen3=ImageDataGenerator(rescale=1.0/255)\nwidth = 112\nheight = 112\n\ntrain_generator3=train_datagen3.flow_from_directory(updated_train_dir,target_size=(height, width),shuffle=True,batch_size=128,class_mode='categorical')\n\nval_generator3=val_datagen3.flow_from_directory(updated_val_dir,target_size=(height, width),shuffle=True,batch_size=128,class_mode='categorical')\n\ntest_generator3=test_datagen3.flow_from_directory(updated_test_dir,target_size=(height, width),shuffle=True,batch_size=128,class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:10:22.158372Z","iopub.execute_input":"2023-02-11T13:10:22.158734Z","iopub.status.idle":"2023-02-11T13:10:22.924472Z","shell.execute_reply.started":"2023-02-11T13:10:22.158703Z","shell.execute_reply":"2023-02-11T13:10:22.923538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\n#from tensorflow.keras.applications.vgg16 import VGG16\n#pretrained_model3 = ResNet50(weights= 'imagenet', include_top=False, input_shape= (height, width,3))\n#pretrained_model3.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:10:30.323908Z","iopub.execute_input":"2023-02-11T13:10:30.324234Z","iopub.status.idle":"2023-02-11T13:10:30.328437Z","shell.execute_reply.started":"2023-02-11T13:10:30.324203Z","shell.execute_reply":"2023-02-11T13:10:30.327236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# # instantiate a distribution strategy\n# tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n# with tpu_strategy.scope():\npretrained_model3 = ResNet50(weights= 'imagenet', include_top=False, input_shape= (112, 112,3))\npretrained_model3.summary()\nx = pretrained_model3.output\n\nx=tf.keras.layers.Flatten()(x)\nx=tf.keras.layers.Dense(1024,kernel_regularizer=regularizers.l2(0.001),activation='relu')(x)\nx=tf.keras.layers.Dense(512,kernel_regularizer=regularizers.l2(0.001),activation='relu')(x)\nx=tf.keras.layers.Dropout(0.2)(x)\nx=tf.keras.layers.Dense(128,kernel_regularizer=regularizers.l2(0.001),activation='relu')(x)\nx=tf.keras.layers.Dropout(0.2)(x)\nx=tf.keras.layers.Dense(5,kernel_regularizer=regularizers.l2(0.001),activation='softmax')(x)\n    \n# # detect and init the TPU\n\n\n\nmodel3=tf.keras.Model(pretrained_model3.input,x)\n    \nmodel3.compile(optimizer=SGD(lr=0.001),\n              loss='categorical_crossentropy',\n               metrics=['accuracy','Precision','Recall'])","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:12:07.492366Z","iopub.execute_input":"2023-02-11T13:12:07.492749Z","iopub.status.idle":"2023-02-11T13:12:14.276269Z","shell.execute_reply.started":"2023-02-11T13:12:07.492715Z","shell.execute_reply":"2023-02-11T13:12:14.275319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history3=model3.fit(train_generator3,validation_data=val_generator3,epochs=30,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:12:36.459080Z","iopub.execute_input":"2023-02-11T13:12:36.459424Z","iopub.status.idle":"2023-02-11T13:55:33.506971Z","shell.execute_reply.started":"2023-02-11T13:12:36.459391Z","shell.execute_reply":"2023-02-11T13:55:33.505602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc3 = history3.history['accuracy']\nval_acc3 = history3.history['val_accuracy']\n\ntrain_precision3=history3.history['precision']\nval_precision3=history3.history['val_precision']\n\ntrain_recall3=history3.history['recall']\nval_recall3=history3.history['val_recall']\n\nloss3 = history3.history['loss']\nval_loss3 = history3.history['val_loss']\nepochs = range(len(acc3))\n\nplt.plot(epochs, acc3, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc3, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, train_precision3, 'r', label='Training precision')\nplt.plot(epochs, val_precision3, 'b', label='Validation precision')\nplt.title('Training and validation precision')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, train_recall3, 'r', label='Training recall')\nplt.plot(epochs, val_recall3, 'b', label='Validation recall')\nplt.title('Training and validation recall')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, loss3, 'r', label='Training Loss')\nplt.plot(epochs, val_loss3, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-11T14:19:54.513990Z","iopub.execute_input":"2023-02-11T14:19:54.514593Z","iopub.status.idle":"2023-02-11T14:19:55.438853Z","shell.execute_reply.started":"2023-02-11T14:19:54.514540Z","shell.execute_reply":"2023-02-11T14:19:55.437581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_result3 = model3.evaluate_generator(test_generator3)\n# print(eval_result3)\nprint('loss rate at evaluation data :', eval_result3[0])\nprint('accuracy rate at evaluation data :', eval_result3[1])\n\nmodel3.save('ResNet50_224x224_data_enhancement_3.h5')\nprint(\"Model has been saved successfully\")","metadata":{"execution":{"iopub.status.busy":"2023-02-11T14:20:51.153322Z","iopub.execute_input":"2023-02-11T14:20:51.153928Z","iopub.status.idle":"2023-02-11T14:20:56.330065Z","shell.execute_reply.started":"2023-02-11T14:20:51.153877Z","shell.execute_reply":"2023-02-11T14:20:56.328770Z"},"trusted":true},"execution_count":null,"outputs":[]}]}