{"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":"**Team Members:**\n* Aya Elkashef\n* Shahd Mahmoud\n* Zahraa Hisham\n\n**Branch**\n* Nasr City","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\nfrom tensorflow.keras import optimizers\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import models\nfrom tensorflow.keras import layers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:35.627226Z","iopub.execute_input":"2022-07-12T19:34:35.627838Z","iopub.status.idle":"2022-07-12T19:34:35.634791Z","shell.execute_reply.started":"2022-07-12T19:34:35.627806Z","shell.execute_reply":"2022-07-12T19:34:35.633796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/state-farm-distracted-driver-detection/driver_imgs_list.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:35.762000Z","iopub.execute_input":"2022-07-12T19:34:35.762890Z","iopub.status.idle":"2022-07-12T19:34:35.809376Z","shell.execute_reply.started":"2022-07-12T19:34:35.762846Z","shell.execute_reply":"2022-07-12T19:34:35.808421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=df[\"img\"]\nlabels=df[\"classname\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:35.880523Z","iopub.execute_input":"2022-07-12T19:34:35.881418Z","iopub.status.idle":"2022-07-12T19:34:35.888051Z","shell.execute_reply.started":"2022-07-12T19:34:35.881382Z","shell.execute_reply":"2022-07-12T19:34:35.887011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"classname\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:35.954869Z","iopub.execute_input":"2022-07-12T19:34:35.955212Z","iopub.status.idle":"2022-07-12T19:34:35.970189Z","shell.execute_reply.started":"2022-07-12T19:34:35.955183Z","shell.execute_reply":"2022-07-12T19:34:35.969061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"classname\"].value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:36.048471Z","iopub.execute_input":"2022-07-12T19:34:36.048822Z","iopub.status.idle":"2022-07-12T19:34:36.272055Z","shell.execute_reply.started":"2022-07-12T19:34:36.048793Z","shell.execute_reply":"2022-07-12T19:34:36.270940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"../input/state-farm-distracted-driver-detection/imgs/train\"\nfolder_names=os.listdir(path)\nfolder_names","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:36.274353Z","iopub.execute_input":"2022-07-12T19:34:36.275250Z","iopub.status.idle":"2022-07-12T19:34:36.289081Z","shell.execute_reply.started":"2022-07-12T19:34:36.275213Z","shell.execute_reply":"2022-07-12T19:34:36.288253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,folder in enumerate( folder_names):\n    print(folder,\"contains\",len(os.listdir(path+\"/\"+folder)))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:36.290040Z","iopub.execute_input":"2022-07-12T19:34:36.290289Z","iopub.status.idle":"2022-07-12T19:34:38.793054Z","shell.execute_reply.started":"2022-07-12T19:34:36.290265Z","shell.execute_reply":"2022-07-12T19:34:38.791945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_gray():\n    base=\"../input/state-farm-distracted-driver-detection/imgs/train\"\n    image_data=[]\n    label_data=[]\n    for i in range(len (features)):\n        img = cv2.resize(cv2.imread(base+\"/\"+labels[i]+\"/\"+ features[i], cv2.IMREAD_GRAYSCALE),(64,64))\n        image_data.append(img)\n        label_data.append(labels[i])\n    return image_data, label_data","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.795497Z","iopub.execute_input":"2022-07-12T19:34:38.796074Z","iopub.status.idle":"2022-07-12T19:34:38.803545Z","shell.execute_reply.started":"2022-07-12T19:34:38.796036Z","shell.execute_reply":"2022-07-12T19:34:38.802412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_color():    \n    path=\"../input/state-farm-distracted-driver-detection/imgs/train\"\n    image_data=[]\n    label_data=[]\n    for i in range(len (features)):\n        img=cv2.imread(path+\"/\"+labels[i]+\"/\"+ features[i],cv2.IMREAD_COLOR)\n        img = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ,(64,64))\n        image_data.append(img)\n        label_data.append(labels[i])\n    return image_data,label_data","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.805208Z","iopub.execute_input":"2022-07-12T19:34:38.805641Z","iopub.status.idle":"2022-07-12T19:34:38.814965Z","shell.execute_reply.started":"2022-07-12T19:34:38.805607Z","shell.execute_reply":"2022-07-12T19:34:38.813835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split(image_data,label_data):\n    train_images, images_validation_test, train_labels, labels_validation_test = train_test_split(\n        image_data, label_data, test_size=0.2, random_state=42, stratify=labels)\n    validation_images, test_images, validation_labels, test_labels = train_test_split(\n        images_validation_test, labels_validation_test, test_size=0.5,random_state=42,stratify=labels_validation_test)\n    train_images=np.asarray(train_images)\n    validation_images=np.asarray(validation_images)\n    test_images=np.asarray(test_images)\n    \n    train_labels=np.asarray(train_labels)\n    validation_labels=np.asarray(validation_labels)\n    test_labels=np.asarray(test_labels)\n    return train_images,train_labels,validation_images,validation_labels,test_images,test_labels","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.816863Z","iopub.execute_input":"2022-07-12T19:34:38.817309Z","iopub.status.idle":"2022-07-12T19:34:38.828996Z","shell.execute_reply.started":"2022-07-12T19:34:38.817274Z","shell.execute_reply":"2022-07-12T19:34:38.827532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_preprocessing(train_images,validation_images,test_images):\n    train_images = train_images.reshape((train_images.shape[0], -1))\n    train_images = train_images.astype('float32') / 255\n\n    validation_images = validation_images.reshape((validation_images.shape[0], -1))\n    validation_images = validation_images.astype('float32') / 255\n\n    test_images = test_images.reshape((test_images.shape[0],-1))\n    test_images = test_images.astype('float32') / 255\n    return train_images,validation_images,test_images","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.830940Z","iopub.execute_input":"2022-07-12T19:34:38.831434Z","iopub.status.idle":"2022-07-12T19:34:38.840134Z","shell.execute_reply.started":"2022-07-12T19:34:38.831399Z","shell.execute_reply":"2022-07-12T19:34:38.839088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label_preprocessing(train_labels,validation_labels,test_labels):   \n    label_encoder = LabelEncoder()\n    vec = label_encoder.fit_transform(train_labels)\n    train_labels = to_categorical(vec)\n    vec = label_encoder.fit_transform(validation_labels)\n    validation_labels = to_categorical(vec)\n    vec = label_encoder.fit_transform(test_labels)\n    test_labels = to_categorical(vec)\n    return train_labels,validation_labels,test_labels","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.844082Z","iopub.execute_input":"2022-07-12T19:34:38.844357Z","iopub.status.idle":"2022-07-12T19:34:38.851067Z","shell.execute_reply.started":"2022-07-12T19:34:38.844314Z","shell.execute_reply":"2022-07-12T19:34:38.850086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**base line model**","metadata":{}},{"cell_type":"code","source":"image_data, label_data=read_gray()\ntrain_images,train_labels,validation_images,validation_labels,test_images,test_labels=split(image_data,label_data)\ntrain_images,validation_images,test_images=feature_preprocessing(train_images,validation_images,test_images)\ntrain_labels,validation_labels,test_labels=label_preprocessing(train_labels,validation_labels,test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:34:38.852783Z","iopub.execute_input":"2022-07-12T19:34:38.853167Z","iopub.status.idle":"2022-07-12T19:38:03.583702Z","shell.execute_reply.started":"2022-07-12T19:34:38.853133Z","shell.execute_reply":"2022-07-12T19:38:03.582706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:03.585182Z","iopub.execute_input":"2022-07-12T19:38:03.585532Z","iopub.status.idle":"2022-07-12T19:38:03.593520Z","shell.execute_reply.started":"2022-07-12T19:38:03.585499Z","shell.execute_reply":"2022-07-12T19:38:03.592597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network = models.Sequential()\nnetwork.add(layers.Dense(256, activation='relu', name='Layer_1', input_shape=(64*64*1,)))\nnetwork.add(layers.Dense(256, activation='relu', name='Layer_2'))\nnetwork.add(layers.Dense(128, activation='relu', name='Layer_3'))\nnetwork.add(layers.Dense(128, activation='relu', name='Layer_4'))\nnetwork.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:03.595531Z","iopub.execute_input":"2022-07-12T19:38:03.596233Z","iopub.status.idle":"2022-07-12T19:38:06.514451Z","shell.execute_reply.started":"2022-07-12T19:38:03.596198Z","shell.execute_reply":"2022-07-12T19:38:06.512278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:06.516012Z","iopub.execute_input":"2022-07-12T19:38:06.516361Z","iopub.status.idle":"2022-07-12T19:38:06.522375Z","shell.execute_reply.started":"2022-07-12T19:38:06.516327Z","shell.execute_reply":"2022-07-12T19:38:06.521429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network.compile(optimizer=optimizers.Adam(learning_rate=10e-3),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:06.524013Z","iopub.execute_input":"2022-07-12T19:38:06.524783Z","iopub.status.idle":"2022-07-12T19:38:06.540967Z","shell.execute_reply.started":"2022-07-12T19:38:06.524731Z","shell.execute_reply":"2022-07-12T19:38:06.540059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=network.fit(train_images, train_labels,\n            validation_data=(validation_images,validation_labels),\n            epochs=10, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:06.544146Z","iopub.execute_input":"2022-07-12T19:38:06.544450Z","iopub.status.idle":"2022-07-12T19:38:17.780585Z","shell.execute_reply.started":"2022-07-12T19:38:06.544405Z","shell.execute_reply":"2022-07-12T19:38:17.779534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = network.evaluate(test_images, test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:17.782568Z","iopub.execute_input":"2022-07-12T19:38:17.782980Z","iopub.status.idle":"2022-07-12T19:38:18.192804Z","shell.execute_reply.started":"2022-07-12T19:38:17.782944Z","shell.execute_reply":"2022-07-12T19:38:18.191835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = model.history['accuracy']\nval_acc = model.history['val_accuracy']\nloss = model.history['loss']\nval_loss = model.history['val_loss']\n\niters = range(len(acc))\n\nplt.plot(iters, acc, 'bo', label='Training acc')\nplt.plot(iters, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(iters, loss, 'bo', label='Training loss')\nplt.plot(iters, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:18.196001Z","iopub.execute_input":"2022-07-12T19:38:18.196738Z","iopub.status.idle":"2022-07-12T19:38:18.550973Z","shell.execute_reply.started":"2022-07-12T19:38:18.196705Z","shell.execute_reply":"2022-07-12T19:38:18.550078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K \nK.clear_session()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:18.552428Z","iopub.execute_input":"2022-07-12T19:38:18.552773Z","iopub.status.idle":"2022-07-12T19:38:18.564096Z","shell.execute_reply.started":"2022-07-12T19:38:18.552727Z","shell.execute_reply":"2022-07-12T19:38:18.562985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Baseline CNN model**","metadata":{}},{"cell_type":"code","source":"image_data, label_data=read_color()\ntrain_images,train_labels,validation_images,validation_labels,test_images,test_labels=split(image_data,label_data)\ntrain_labels,validation_labels,test_labels=label_preprocessing(train_labels,validation_labels,test_labels)\ntrain_images = train_images.astype('float32') / 255\nvalidation_images = validation_images.astype('float32') / 255\ntest_images = test_images.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:38:18.566497Z","iopub.execute_input":"2022-07-12T19:38:18.566902Z","iopub.status.idle":"2022-07-12T19:40:05.607727Z","shell.execute_reply.started":"2022-07-12T19:38:18.566867Z","shell.execute_reply":"2022-07-12T19:40:05.606587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network_cnn = models.Sequential()\nnetwork_cnn.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(64,64,3)))\nnetwork_cnn.add(layers.MaxPooling2D((2, 2)))\nnetwork_cnn.add(layers.Conv2D(32, (3, 3), activation='relu'))\nnetwork_cnn.add(layers.MaxPooling2D((2, 2)))\nnetwork_cnn.add(layers.Conv2D(32, (3, 3), activation='relu'))\nnetwork_cnn.add(layers.Flatten())\nnetwork_cnn.add(layers.Dense(128, activation='relu'))\nnetwork_cnn.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:05.609540Z","iopub.execute_input":"2022-07-12T19:40:05.609906Z","iopub.status.idle":"2022-07-12T19:40:05.673868Z","shell.execute_reply.started":"2022-07-12T19:40:05.609871Z","shell.execute_reply":"2022-07-12T19:40:05.673051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network_cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:05.679372Z","iopub.execute_input":"2022-07-12T19:40:05.679639Z","iopub.status.idle":"2022-07-12T19:40:05.686826Z","shell.execute_reply.started":"2022-07-12T19:40:05.679614Z","shell.execute_reply":"2022-07-12T19:40:05.685667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network_cnn.compile(optimizer='rmsprop',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\nmodel_cnn=network_cnn.fit(train_images, train_labels, \n                          validation_data=(validation_images,validation_labels),epochs=10, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:05.688636Z","iopub.execute_input":"2022-07-12T19:40:05.689377Z","iopub.status.idle":"2022-07-12T19:40:31.950784Z","shell.execute_reply.started":"2022-07-12T19:40:05.689339Z","shell.execute_reply":"2022-07-12T19:40:31.949687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = network_cnn.evaluate(test_images, test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:31.952239Z","iopub.execute_input":"2022-07-12T19:40:31.952588Z","iopub.status.idle":"2022-07-12T19:40:32.501585Z","shell.execute_reply.started":"2022-07-12T19:40:31.952552Z","shell.execute_reply":"2022-07-12T19:40:32.500668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = model_cnn.history['accuracy']\nval_acc = model_cnn.history['val_accuracy']\nloss = model_cnn.history['loss']\nval_loss = model_cnn.history['val_loss']\n\niters = range(len(acc))\n\nplt.plot(iters, acc, 'bo', label='Training acc')\nplt.plot(iters, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(iters, loss, 'bo', label='Training loss')\nplt.plot(iters, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:32.503232Z","iopub.execute_input":"2022-07-12T19:40:32.503577Z","iopub.status.idle":"2022-07-12T19:40:32.857877Z","shell.execute_reply.started":"2022-07-12T19:40:32.503542Z","shell.execute_reply":"2022-07-12T19:40:32.856948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN visualization**","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing import image\nimport numpy as np\nimg_path=\"../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg\"\nimg = image.load_img(img_path, target_size=(64, 64))\nimg_tensor = image.img_to_array(img)\nimg_tensor = np.expand_dims(img_tensor, axis=0)\nimg_tensor /= 255.\nprint(img_tensor.shape)\nfrom keras import models\n# Extracts the outputs of the top 8 layers:\nlayer_outputs = [layer.output for layer in network_cnn.layers[:5]]\n# Creates a model that will return these outputs, given the model input:\nactivation_model = models.Model(inputs=network_cnn.input, outputs=layer_outputs)\nactivations = activation_model.predict(img_tensor)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:32.859422Z","iopub.execute_input":"2022-07-12T19:40:32.859754Z","iopub.status.idle":"2022-07-12T19:40:32.999698Z","shell.execute_reply.started":"2022-07-12T19:40:32.859721Z","shell.execute_reply":"2022-07-12T19:40:32.998692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_tensor[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:33.001293Z","iopub.execute_input":"2022-07-12T19:40:33.001650Z","iopub.status.idle":"2022-07-12T19:40:33.170187Z","shell.execute_reply.started":"2022-07-12T19:40:33.001614Z","shell.execute_reply":"2022-07-12T19:40:33.169211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(activations)\nfirst_layer_activation = activations[0]\nprint(first_layer_activation.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:33.171492Z","iopub.execute_input":"2022-07-12T19:40:33.172599Z","iopub.status.idle":"2022-07-12T19:40:33.177862Z","shell.execute_reply.started":"2022-07-12T19:40:33.172558Z","shell.execute_reply":"2022-07-12T19:40:33.176927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_layer_activation = activations[0]\nprint(first_layer_activation.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:33.179223Z","iopub.execute_input":"2022-07-12T19:40:33.179940Z","iopub.status.idle":"2022-07-12T19:40:33.189557Z","shell.execute_reply.started":"2022-07-12T19:40:33.179904Z","shell.execute_reply":"2022-07-12T19:40:33.188328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"network_cnn.layers[:4]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:33.191239Z","iopub.execute_input":"2022-07-12T19:40:33.191677Z","iopub.status.idle":"2022-07-12T19:40:33.200836Z","shell.execute_reply.started":"2022-07-12T19:40:33.191642Z","shell.execute_reply":"2022-07-12T19:40:33.199559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\n\n# These are the names of the layers, so can have them as part of our plot\nlayer_names = []\nfor layer in network_cnn.layers[:5]:\n    layer_names.append(layer.name)\n\nimages_per_row = 16\n\n# Now let's display our feature maps\nfor layer_name, layer_activation in zip(layer_names, activations):\n    # This is the number of features in the feature map\n    print(layer_activation.shape)\n    n_features = layer_activation.shape[-1]\n    # The feature map has shape (1, size, size, n_features)\n    size = layer_activation.shape[1]\n    # We will tile the activation channels in this matrix\n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n\n    # We'll tile each filter into this big horizontal grid\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            channel_image = layer_activation[0,\n                                             :, :,\n                                             col * images_per_row + row]\n            # Post-process the feature to make it visually palatable\n            channel_image -= channel_image.mean()\n            channel_image /= channel_image.std()\n            channel_image *= 64\n            channel_image += 128\n            channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n            display_grid[col * size : (col + 1) * size,\n                         row * size : (row + 1) * size] = channel_image\n\n    # Display the grid\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:40:33.202204Z","iopub.execute_input":"2022-07-12T19:40:33.202487Z","iopub.status.idle":"2022-07-12T19:40:34.341965Z","shell.execute_reply.started":"2022-07-12T19:40:33.202462Z","shell.execute_reply":"2022-07-12T19:40:34.341004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K \nK.clear_session()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:23.647432Z","iopub.execute_input":"2022-07-12T19:53:23.647793Z","iopub.status.idle":"2022-07-12T19:53:23.664350Z","shell.execute_reply.started":"2022-07-12T19:53:23.647765Z","shell.execute_reply":"2022-07-12T19:53:23.663462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data augmentation**","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n      rotation_range=40\n)\ndev_datagen = ImageDataGenerator(\n      rotation_range=40\n   \n)\ntrain_datagen.fit(train_images)\ndev_datagen.fit(validation_images)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:25.759378Z","iopub.execute_input":"2022-07-12T19:53:25.759718Z","iopub.status.idle":"2022-07-12T19:53:26.116551Z","shell.execute_reply.started":"2022-07-12T19:53:25.759688Z","shell.execute_reply":"2022-07-12T19:53:26.115555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_aug = models.Sequential()\nmodel_aug.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(64,64,3)))\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\nmodel_aug.add(layers.Conv2D(32, (3, 3), activation='relu'))\nmodel_aug.add(layers.MaxPooling2D((2, 2)))\nmodel_aug.add(layers.Conv2D(32, (3, 3), activation='relu'))\nmodel_aug.add(layers.Flatten())\nmodel_aug.add(layers.Dense(256, activation='relu', name='Layer_1'))\nmodel_aug.add(layers.Dense(128, activation='relu', name='Layer_2'))\nmodel_aug.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:28.700611Z","iopub.execute_input":"2022-07-12T19:53:28.700970Z","iopub.status.idle":"2022-07-12T19:53:28.763436Z","shell.execute_reply.started":"2022-07-12T19:53:28.700938Z","shell.execute_reply":"2022-07-12T19:53:28.762573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_aug.compile(loss='categorical_crossentropy',\n              optimizer=\"rmsprop\",\n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:31.208098Z","iopub.execute_input":"2022-07-12T19:53:31.209150Z","iopub.status.idle":"2022-07-12T19:53:31.219572Z","shell.execute_reply.started":"2022-07-12T19:53:31.209103Z","shell.execute_reply":"2022-07-12T19:53:31.218615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_aug.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:32.734832Z","iopub.execute_input":"2022-07-12T19:53:32.735607Z","iopub.status.idle":"2022-07-12T19:53:32.744132Z","shell.execute_reply.started":"2022-07-12T19:53:32.735550Z","shell.execute_reply":"2022-07-12T19:53:32.742079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug = model_aug.fit(\n    train_datagen.flow(train_images, train_labels, batch_size=256,),\n         validation_data=dev_datagen.flow(validation_images, validation_labels,\n         batch_size=256),\n          epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:53:35.144744Z","iopub.execute_input":"2022-07-12T19:53:35.145586Z","iopub.status.idle":"2022-07-12T20:08:59.747761Z","shell.execute_reply.started":"2022-07-12T19:53:35.145536Z","shell.execute_reply":"2022-07-12T20:08:59.746724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model_aug.evaluate(test_images, test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:08:59.750039Z","iopub.execute_input":"2022-07-12T20:08:59.750411Z","iopub.status.idle":"2022-07-12T20:09:00.397510Z","shell.execute_reply.started":"2022-07-12T20:08:59.750374Z","shell.execute_reply":"2022-07-12T20:09:00.396602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = aug.history['acc']\nval_acc = aug.history['val_acc']\nloss = aug.history['loss']\nval_loss = aug.history['val_loss']\n\niters = range(len(acc))\n\nplt.plot(iters, acc, 'bo', label='Training acc')\nplt.plot(iters, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(iters, loss, 'bo', label='Training loss')\nplt.plot(iters, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:09:00.399264Z","iopub.execute_input":"2022-07-12T20:09:00.399610Z","iopub.status.idle":"2022-07-12T20:09:00.770921Z","shell.execute_reply.started":"2022-07-12T20:09:00.399576Z","shell.execute_reply":"2022-07-12T20:09:00.770055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K \nK.clear_session()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Transfer Learning**","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n      rotation_range=40\n)\ndev_datagen = ImageDataGenerator(\n      rotation_range=40\n   \n)\ntrain_datagen.fit(train_images)\ndev_datagen.fit(validation_images)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = VGG16(weights='imagenet',\n                  include_top=False,\n                  input_shape=(64, 64, 3))\nmodel_tl = models.Sequential()\nmodel_tl.add(conv_base)\nmodel_tl.add(layers.Flatten())\nmodel_tl.add(layers.Dense(256, activation='relu', name='Layer_1'))\nmodel_tl.add(layers.Dense(128, activation='relu', name='Layer_2'))\nmodel_tl.add(layers.Dense(10, activation='softmax'))\nmodel_tl.compile(loss='categorical_crossentropy',\n              optimizer=\"rmsprop\",\n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:09:00.774158Z","iopub.execute_input":"2022-07-12T20:09:00.774431Z","iopub.status.idle":"2022-07-12T20:09:01.548878Z","shell.execute_reply.started":"2022-07-12T20:09:00.774405Z","shell.execute_reply":"2022-07-12T20:09:01.547922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_tl.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:09:01.550287Z","iopub.execute_input":"2022-07-12T20:09:01.550628Z","iopub.status.idle":"2022-07-12T20:09:01.559316Z","shell.execute_reply.started":"2022-07-12T20:09:01.550594Z","shell.execute_reply":"2022-07-12T20:09:01.558302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = True\nset_trainable = False\nfor layer in conv_base.layers:\n    if layer.name == 'block5_conv1':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:09:01.560657Z","iopub.execute_input":"2022-07-12T20:09:01.561139Z","iopub.status.idle":"2022-07-12T20:09:01.570916Z","shell.execute_reply.started":"2022-07-12T20:09:01.561102Z","shell.execute_reply":"2022-07-12T20:09:01.569945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tl = model_tl.fit(\n    train_datagen.flow(train_images, train_labels, batch_size=256,),\n         validation_data=dev_datagen.flow(validation_images, validation_labels,\n         batch_size=256),\n          epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:09:01.573688Z","iopub.execute_input":"2022-07-12T20:09:01.574603Z","iopub.status.idle":"2022-07-12T20:12:48.668738Z","shell.execute_reply.started":"2022-07-12T20:09:01.574407Z","shell.execute_reply":"2022-07-12T20:12:48.667780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model_tl.evaluate(test_images, test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:12:48.670519Z","iopub.execute_input":"2022-07-12T20:12:48.671149Z","iopub.status.idle":"2022-07-12T20:12:50.223521Z","shell.execute_reply.started":"2022-07-12T20:12:48.671108Z","shell.execute_reply":"2022-07-12T20:12:50.222638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = tl.history['acc']\nval_acc = tl.history['val_acc']\nloss = tl.history['loss']\nval_loss = tl.history['val_loss']\n\niters = range(len(acc))\n\nplt.plot(iters, acc, 'bo', label='Training acc')\nplt.plot(iters, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(iters, loss, 'bo', label='Training loss')\nplt.plot(iters, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:12:50.226738Z","iopub.execute_input":"2022-07-12T20:12:50.227040Z","iopub.status.idle":"2022-07-12T20:12:50.562217Z","shell.execute_reply.started":"2022-07-12T20:12:50.226993Z","shell.execute_reply":"2022-07-12T20:12:50.561374Z"},"trusted":true},"execution_count":null,"outputs":[]}]}