{"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":"","metadata":{"id":"_RG3b9z7JcEW"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2","metadata":{"id":"hdPU-TXYEN7w","execution":{"iopub.status.busy":"2023-03-21T15:34:22.245447Z","iopub.execute_input":"2023-03-21T15:34:22.245845Z","iopub.status.idle":"2023-03-21T15:34:22.251307Z","shell.execute_reply.started":"2023-03-21T15:34:22.245813Z","shell.execute_reply":"2023-03-21T15:34:22.250290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path\npath = r'/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg'\n  \n# Reading an image in default mode\nimage = cv2.imread(path)\n  \nimage.shape\n# cv2_imshow(image)","metadata":{"id":"IxnOrMSK8Rug","outputId":"f4f9bdb4-4bf1-4bee-f0e1-a80e2dba14e6","execution":{"iopub.status.busy":"2023-03-21T15:34:22.253639Z","iopub.execute_input":"2023-03-21T15:34:22.254398Z","iopub.status.idle":"2023-03-21T15:34:22.273854Z","shell.execute_reply.started":"2023-03-21T15:34:22.254358Z","shell.execute_reply":"2023-03-21T15:34:22.272694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"uw3P5aZiN3dv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16, ResNet50","metadata":{"id":"w87KgaTh8ZoT","execution":{"iopub.status.busy":"2023-03-21T15:34:22.275928Z","iopub.execute_input":"2023-03-21T15:34:22.276292Z","iopub.status.idle":"2023-03-21T15:34:22.281242Z","shell.execute_reply.started":"2023-03-21T15:34:22.276256Z","shell.execute_reply":"2023-03-21T15:34:22.280058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\nIMG_SIZE = (224,224)","metadata":{"id":"wqXWM8fA7ZYi","execution":{"iopub.status.busy":"2023-03-21T15:34:22.282828Z","iopub.execute_input":"2023-03-21T15:34:22.283543Z","iopub.status.idle":"2023-03-21T15:34:22.293390Z","shell.execute_reply.started":"2023-03-21T15:34:22.283505Z","shell.execute_reply":"2023-03-21T15:34:22.292360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,\n\t\t\t\t\trotation_range=30,\n\t\t\t\t\tshear_range=0.2,\n\t\t\t\t\tzoom_range=0.2,\n\t\t\t\t\twidth_shift_range=0.3,\n\t\t\t\t\theight_shift_range=0.3,\n\t\t\t\t\tfill_mode='nearest', \n          validation_split = 0.2)\n\n# test_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train_dir,\n        target_size=IMG_SIZE,\n        batch_size=32,\n        subset = 'training',\n        seed = 42,\n        class_mode='categorical')\n\nvalidation_generator = train_datagen.flow_from_directory(\n        train_dir,\n        target_size=IMG_SIZE,\n        batch_size=32,\n        subset = 'validation',\n        seed = 42,\n        class_mode='categorical')","metadata":{"id":"ezuqSiiF5T4c","outputId":"94b6b00e-5400-4173-892f-2251f2175abc","execution":{"iopub.status.busy":"2023-03-21T15:34:22.296450Z","iopub.execute_input":"2023-03-21T15:34:22.296883Z","iopub.status.idle":"2023-03-21T15:34:26.224510Z","shell.execute_reply.started":"2023-03-21T15:34:22.296844Z","shell.execute_reply":"2023-03-21T15:34:26.223512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model","metadata":{"id":"vinw3W168piq"}},{"cell_type":"code","source":"base_model = VGG16(weights='imagenet',\n                  include_top=False,\n                  input_shape=(224,224, 3))","metadata":{"id":"v_Ia5hj55T2D","outputId":"2ba91268-181c-42fe-a488-7e4b59afc2c0","execution":{"iopub.status.busy":"2023-03-21T15:34:26.225952Z","iopub.execute_input":"2023-03-21T15:34:26.226324Z","iopub.status.idle":"2023-03-21T15:34:26.980231Z","shell.execute_reply.started":"2023-03-21T15:34:26.226285Z","shell.execute_reply":"2023-03-21T15:34:26.979137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model.summary()","metadata":{"id":"zaS8neU25Tzd","outputId":"98b52b37-ac16-41d8-abfe-0bac625f2616","execution":{"iopub.status.busy":"2023-03-21T15:34:26.981891Z","iopub.execute_input":"2023-03-21T15:34:26.982265Z","iopub.status.idle":"2023-03-21T15:34:26.990793Z","shell.execute_reply.started":"2023-03-21T15:34:26.982225Z","shell.execute_reply":"2023-03-21T15:34:26.989840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential, Model, load_model\nfrom keras.layers import Dense,Dropout,Activation,Flatten,BatchNormalization,Input\nfrom tensorflow.keras.optimizers import RMSprop,SGD,Adam","metadata":{"id":"nbRsKBhRADbo","execution":{"iopub.status.busy":"2023-03-21T15:34:26.992389Z","iopub.execute_input":"2023-03-21T15:34:26.993409Z","iopub.status.idle":"2023-03-21T15:34:26.999411Z","shell.execute_reply.started":"2023-03-21T15:34:26.993369Z","shell.execute_reply":"2023-03-21T15:34:26.998332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n        layer.trainable=False\n\n# for layer in base_model.layers[-5:]:\n#         layer.trainable=True","metadata":{"id":"NlVJvvl_Avb8","execution":{"iopub.status.busy":"2023-03-21T15:34:27.000830Z","iopub.execute_input":"2023-03-21T15:34:27.001338Z","iopub.status.idle":"2023-03-21T15:34:27.016556Z","shell.execute_reply.started":"2023-03-21T15:34:27.001275Z","shell.execute_reply":"2023-03-21T15:34:27.015523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = Flatten()(x)\nx = Dense(2048, activation='relu')(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(512, activation='relu')(x)\nx = Dense(256, activation='relu')(x)\nx = Dense(10, activation='softmax')(x)\n\nmodel = Model(inputs=base_model.input, outputs=x)","metadata":{"id":"biv4c4JC5TvQ","execution":{"iopub.status.busy":"2023-03-21T15:34:27.020286Z","iopub.execute_input":"2023-03-21T15:34:27.020598Z","iopub.status.idle":"2023-03-21T15:34:27.090414Z","shell.execute_reply.started":"2023-03-21T15:34:27.020571Z","shell.execute_reply":"2023-03-21T15:34:27.089525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['acc'])","metadata":{"id":"C95ydPR75TsX","execution":{"iopub.status.busy":"2023-03-21T15:34:27.093598Z","iopub.execute_input":"2023-03-21T15:34:27.093870Z","iopub.status.idle":"2023-03-21T15:34:27.106079Z","shell.execute_reply.started":"2023-03-21T15:34:27.093844Z","shell.execute_reply":"2023-03-21T15:34:27.105010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"0lq6oSNPYFTP"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = load_model('/content/drive/MyDrive/Colab Notebooks/ITI/model4SGD.h5')","metadata":{"id":"QzT8w1jfXwUN","execution":{"iopub.status.busy":"2023-03-21T15:34:27.108377Z","iopub.execute_input":"2023-03-21T15:34:27.109149Z","iopub.status.idle":"2023-03-21T15:34:27.117013Z","shell.execute_reply.started":"2023-03-21T15:34:27.109111Z","shell.execute_reply":"2023-03-21T15:34:27.115891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#20\nepochs = 20","metadata":{"id":"7nAa7vjOGe9N","execution":{"iopub.status.busy":"2023-03-21T15:34:27.118597Z","iopub.execute_input":"2023-03-21T15:34:27.119213Z","iopub.status.idle":"2023-03-21T15:34:27.130108Z","shell.execute_reply.started":"2023-03-21T15:34:27.119174Z","shell.execute_reply":"2023-03-21T15:34:27.129152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras import backend as K\n# K.set_value(model.optimizer.learning_rate, 0.00001)","metadata":{"id":"IzKAuaoJqNtQ","execution":{"iopub.status.busy":"2023-03-21T15:34:27.131227Z","iopub.execute_input":"2023-03-21T15:34:27.131522Z","iopub.status.idle":"2023-03-21T15:34:27.142555Z","shell.execute_reply.started":"2023-03-21T15:34:27.131470Z","shell.execute_reply":"2023-03-21T15:34:27.141398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch=len(train_generator),\n                    epochs=epochs,\n                    validation_data=validation_generator,\n                    validation_steps=len(validation_generator))","metadata":{"id":"IR5SxNMoAQlm","outputId":"392dc513-3239-43c6-872f-a0a90a6d4eae","execution":{"iopub.status.busy":"2023-03-21T15:34:27.144045Z","iopub.execute_input":"2023-03-21T15:34:27.144463Z","iopub.status.idle":"2023-03-21T17:41:16.050581Z","shell.execute_reply.started":"2023-03-21T15:34:27.144427Z","shell.execute_reply":"2023-03-21T17:41:16.049523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('VGG_1.h5')","metadata":{"id":"SRnXsQBSAQi4","execution":{"iopub.status.busy":"2023-03-21T17:48:10.459371Z","iopub.execute_input":"2023-03-21T17:48:10.460056Z","iopub.status.idle":"2023-03-21T17:48:11.876724Z","shell.execute_reply.started":"2023-03-21T17:48:10.460019Z","shell.execute_reply":"2023-03-21T17:48:11.875648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_directory(\n        '/kaggle/input/state-farm-distracted-driver-detection/imgs/test',\n        target_size=IMG_SIZE,\n        batch_size=32)","metadata":{"id":"56c5PsgRAQgP","execution":{"iopub.status.busy":"2023-03-21T17:58:27.732118Z","iopub.execute_input":"2023-03-21T17:58:27.732510Z","iopub.status.idle":"2023-03-21T17:59:00.920931Z","shell.execute_reply.started":"2023-03-21T17:58:27.732458Z","shell.execute_reply":"2023-03-21T17:59:00.919883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T17:59:00.923667Z","iopub.execute_input":"2023-03-21T17:59:00.924319Z","iopub.status.idle":"2023-03-21T17:59:00.961657Z","shell.execute_reply.started":"2023-03-21T17:59:00.924280Z","shell.execute_reply":"2023-03-21T17:59:00.959001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}