{"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":"!pip install split_folders\nimport splitfolders\nsplitfolders.ratio(\"../input/asl-alphabet/asl_alphabet_train/asl_alphabet_train\", output=\"./\", seed=1337, ratio=(.6, .2, .2), group_prefix=None) # default values","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-26T15:13:22.461851Z","iopub.execute_input":"2022-09-26T15:13:22.462195Z","iopub.status.idle":"2022-09-26T15:23:45.331261Z","shell.execute_reply.started":"2022-09-26T15:13:22.462156Z","shell.execute_reply":"2022-09-26T15:23:45.330300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport keras\nimport matplotlib.pyplot as plt\nfrom keras.layers import Dense, Dropout, Flatten, ZeroPadding2D, Conv2D, MaxPooling2D, Activation, GlobalAveragePooling2D\nfrom keras.preprocessing import image\nfrom keras.applications.mobilenet import preprocess_input\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model, Sequential\nfrom keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom sklearn.utils import class_weight","metadata":{"execution":{"iopub.status.busy":"2022-09-26T15:42:51.381780Z","iopub.execute_input":"2022-09-26T15:42:51.382126Z","iopub.status.idle":"2022-09-26T15:42:56.562101Z","shell.execute_reply.started":"2022-09-26T15:42:51.382091Z","shell.execute_reply":"2022-09-26T15:42:56.561290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nNUM_CLASSES = len(os.listdir(r'./test'))\n\ntrain_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies\n\ntrain_generator=train_datagen.flow_from_directory(r'./train', # this is where you specify the path to the main data folder\n                                                 target_size=(224,224),\n                                                 color_mode='rgb',\n                                                 batch_size=24,\n                                                 class_mode='categorical',\n                                                 shuffle=True)\n\nval_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies\n\nval_generator=val_datagen.flow_from_directory(r'./val', # this is where you specify the path to the main data folder\n                                                 target_size=(224,224),\n                                                 color_mode='rgb',\n                                                 batch_size=24,\n                                                 class_mode='categorical',\n                                                 shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T15:42:56.809270Z","iopub.execute_input":"2022-09-26T15:42:56.809551Z","iopub.status.idle":"2022-09-26T15:43:00.269404Z","shell.execute_reply.started":"2022-09-26T15:42:56.809521Z","shell.execute_reply":"2022-09-26T15:43:00.268594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.applications.densenet import DenseNet201\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB6\n\nmd = Xception(weights='imagenet', include_top=False,  input_shape=(224, 224, 3), pooling='avg')\n\nfrom keras.utils import plot_model\nmodel = keras.models.Sequential([\n    md,\n    keras.layers.Dense(NUM_CLASSES, activation='softmax')\n])\n# summarize layers\nprint(model.summary())\n# plot graph\n#plot_model(model, to_file='shared_input_layer.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-26T15:43:01.776556Z","iopub.execute_input":"2022-09-26T15:43:01.776858Z","iopub.status.idle":"2022-09-26T15:43:05.854766Z","shell.execute_reply.started":"2022-09-26T15:43:01.776828Z","shell.execute_reply":"2022-09-26T15:43:05.853930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop=EarlyStopping(patience=3) \nlearning_rate_reduction=ReduceLROnPlateau(monitor='loss',patience=2,verbose=1,factor=0.1,min_lr=0.0000000001) \ncallback=[learning_rate_reduction]\n\nmodel.compile(optimizer=Adam(lr=0.00001),loss='categorical_crossentropy', metrics=['accuracy'])\n\nstep_size_train=train_generator.n//train_generator.batch_size\nstep_size_val=val_generator.n//val_generator.batch_size\nhistory = model.fit_generator(generator=train_generator, steps_per_epoch=step_size_train, validation_data=val_generator, validation_steps=step_size_val, epochs=10, callbacks=callback)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T15:43:17.438286Z","iopub.execute_input":"2022-09-26T15:43:17.438606Z","iopub.status.idle":"2022-09-26T15:43:34.641623Z","shell.execute_reply.started":"2022-09-26T15:43:17.438575Z","shell.execute_reply":"2022-09-26T15:43:34.639973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# serialize model to JSON\nmodel_json = model.to_json()\nwith open(\"DenseNet201.json\", \"w\") as json_file:\n    json_file.write(model_json)\n# serialize weights to HDF5\nmodel.save_weights(\"DenseNet201.h5\")\nprint(\"Saved model to disk\")","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:08:02.554274Z","iopub.execute_input":"2022-01-25T18:08:02.554622Z","iopub.status.idle":"2022-01-25T18:08:03.831576Z","shell.execute_reply.started":"2022-01-25T18:08:02.554588Z","shell.execute_reply":"2022-01-25T18:08:03.830632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(1)\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\n\nplt.figure(2)\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:08:05.979584Z","iopub.execute_input":"2022-01-25T18:08:05.979962Z","iopub.status.idle":"2022-01-25T18:08:06.015424Z","shell.execute_reply.started":"2022-01-25T18:08:05.979928Z","shell.execute_reply":"2022-01-25T18:08:06.014195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input)\nbatch_size=24\npred_dir = r'./test'\ntest_generator = test_datagen.flow_from_directory(\n    directory=pred_dir,\n    target_size=(224,224),\n    color_mode=\"rgb\",\n    batch_size=batch_size,\n    class_mode=None,\n    shuffle=False\n)\n#test_generator.reset() \npred=model.predict_generator(test_generator,verbose=1,steps=test_generator.n/batch_size)\n\npredicted_class_indices=np.argmax(pred,axis=1)\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions = [labels[k] for k in predicted_class_indices]\nfilenames=test_generator.filenames\nresults=pd.DataFrame({\"Filename\":filenames,\n                      \"Predictions\":predictions})\nprint(results)\n\n\nimport pandas as pd\nt_counter = 0\nf_counter = 0\n\nfile_arr = []\npred_arr = []\n\nfor index, row in results.iterrows():\n    #print(row['Filename'].split('/')[0], row['Predictions'])\n    file_arr.append(row['Filename'].split('/')[0])\n    pred_arr.append(row['Predictions'])\n    if row['Filename'].split('/')[0] == row['Predictions']:\n        t_counter = t_counter + 1\n    else:\n        f_counter = f_counter + 1\nprint(t_counter, f_counter)\n\nfrom sklearn.preprocessing import LabelEncoder\nlabelencoder = LabelEncoder()\ntest_y = labelencoder.fit_transform(file_arr)\npred_y = labelencoder.fit_transform(pred_arr)\ntest_y_or = test_y\npred_y_or = pred_y\n\nfrom keras.utils import np_utils\ntest_y = np_utils.to_categorical(test_y, NUM_CLASSES)\npred_y = np_utils.to_categorical(pred_y, NUM_CLASSES)\nfrom sklearn.metrics import classification_report\nprint(classification_report(test_y, pred_y))\n\nfrom sklearn.metrics import accuracy_score\nscore = accuracy_score(test_y, pred_y)\nprint(score*100)\n\nfrom sklearn.metrics import confusion_matrix\nc=confusion_matrix(test_y_or, pred_y_or)\nx = c.diagonal()/c.sum(axis=1)\nfor i in range(len(x)):\n    print('Class ', i, ' Accuracy: ', x[i])\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize = (10,8))\nsns.heatmap(c, annot=True, annot_kws={\"size\": 25}, fmt='d')","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:08:23.661785Z","iopub.execute_input":"2022-01-25T18:08:23.662184Z","iopub.status.idle":"2022-01-25T18:08:55.176149Z","shell.execute_reply.started":"2022-01-25T18:08:23.662132Z","shell.execute_reply":"2022-01-25T18:08:55.175226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original = np.argmax(test_y,axis=1)\n\npred2 = pred\nfor i in range(len(pred2)):\n    pred2[i][predicted_class_indices[i]]=0\n\npredicted_class_indices2 = np.argmax(pred,axis=1)\n\npred3 = pred2\nfor i in range(len(pred3)):\n    pred3[i][predicted_class_indices2[i]]=0\n\npredicted_class_indices3 = np.argmax(pred2,axis=1)\n\npredicted_class_indices, predicted_class_indices2, predicted_class_indices3, original","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:09:10.262661Z","iopub.execute_input":"2022-01-25T18:09:10.263112Z","iopub.status.idle":"2022-01-25T18:09:10.294989Z","shell.execute_reply.started":"2022-01-25T18:09:10.263075Z","shell.execute_reply":"2022-01-25T18:09:10.294189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top2 = []\nfor i in range(len(original)):\n    if predicted_class_indices[i]==original[i] or predicted_class_indices2[i]==original[i]:\n        top2.append(original[i])\n    else:\n        top2.append(predicted_class_indices[i])\ntop2 = np.array(top2)\n\ntop3 = []\nfor i in range(len(original)):\n    if predicted_class_indices[i]==original[i] or predicted_class_indices2[i]==original[i] or predicted_class_indices3[i]==original[i]:\n        top3.append(original[i])\n    else:\n        top3.append(predicted_class_indices[i])\ntop3 = np.array(top3)\n\ntop2, top3","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:09:13.013786Z","iopub.execute_input":"2022-01-25T18:09:13.014129Z","iopub.status.idle":"2022-01-25T18:09:13.038698Z","shell.execute_reply.started":"2022-01-25T18:09:13.014098Z","shell.execute_reply":"2022-01-25T18:09:13.037712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y = top2\ntest_y = original\nfrom keras.utils import np_utils\n#test_y = np_utils.to_categorical(test_y, NUM_CLASSES)\n#pred_y = np_utils.to_categorical(pred_y, NUM_CLASSES)\nfrom sklearn.metrics import classification_report\nprint(classification_report(test_y, pred_y))\n\nfrom sklearn.metrics import accuracy_score\nscore = accuracy_score(test_y, pred_y)\nprint(score*100)\n\nfrom sklearn.metrics import confusion_matrix\nc=confusion_matrix(test_y, pred_y)\nx = c.diagonal()/c.sum(axis=1)\nfor i in range(len(x)):\n    print('Class ', i, ' Accuracy: ', x[i])\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize = (10,8))\nsns.heatmap(c, annot=True, annot_kws={\"size\": 25}, fmt='d')","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:09:15.594458Z","iopub.execute_input":"2022-01-25T18:09:15.594787Z","iopub.status.idle":"2022-01-25T18:09:16.243873Z","shell.execute_reply.started":"2022-01-25T18:09:15.594755Z","shell.execute_reply":"2022-01-25T18:09:16.243034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y = top3\ntest_y = original\nfrom keras.utils import np_utils\n#test_y = np_utils.to_categorical(test_y, NUM_CLASSES)\n#pred_y = np_utils.to_categorical(pred_y, NUM_CLASSES)\nfrom sklearn.metrics import classification_report\nprint(classification_report(test_y, pred_y))\n\nfrom sklearn.metrics import accuracy_score\nscore = accuracy_score(test_y, pred_y)\nprint(score*100)\n\nfrom sklearn.metrics import confusion_matrix\nc=confusion_matrix(test_y, pred_y)\nx = c.diagonal()/c.sum(axis=1)\nfor i in range(len(x)):\n    print('Class ', i, ' Accuracy: ', x[i])\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize = (10,8))\nsns.heatmap(c, annot=True, annot_kws={\"size\": 25}, fmt='d')","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:09:28.820869Z","iopub.execute_input":"2022-01-25T18:09:28.821213Z","iopub.status.idle":"2022-01-25T18:09:29.49284Z","shell.execute_reply.started":"2022-01-25T18:09:28.821181Z","shell.execute_reply":"2022-01-25T18:09:29.492019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[2].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:10:01.418286Z","iopub.execute_input":"2022-01-25T18:10:01.41868Z","iopub.status.idle":"2022-01-25T18:10:09.162589Z","shell.execute_reply.started":"2022-01-25T18:10:01.418648Z","shell.execute_reply":"2022-01-25T18:10:09.160582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[3].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:10:18.71787Z","iopub.execute_input":"2022-01-25T18:10:18.718227Z","iopub.status.idle":"2022-01-25T18:10:25.994515Z","shell.execute_reply.started":"2022-01-25T18:10:18.718194Z","shell.execute_reply":"2022-01-25T18:10:25.993659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[4].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:10:28.066745Z","iopub.execute_input":"2022-01-25T18:10:28.067095Z","iopub.status.idle":"2022-01-25T18:10:36.031711Z","shell.execute_reply.started":"2022-01-25T18:10:28.067064Z","shell.execute_reply":"2022-01-25T18:10:36.030897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[5].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:10:46.34523Z","iopub.execute_input":"2022-01-25T18:10:46.345565Z","iopub.status.idle":"2022-01-25T18:10:53.854503Z","shell.execute_reply.started":"2022-01-25T18:10:46.345536Z","shell.execute_reply":"2022-01-25T18:10:53.853521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[6].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:11:09.542279Z","iopub.execute_input":"2022-01-25T18:11:09.542648Z","iopub.status.idle":"2022-01-25T18:11:16.490837Z","shell.execute_reply.started":"2022-01-25T18:11:09.542617Z","shell.execute_reply":"2022-01-25T18:11:16.490024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[7].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(square):\n\tfor _ in range(square):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(square, square, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:11:16.680917Z","iopub.execute_input":"2022-01-25T18:11:16.68125Z","iopub.status.idle":"2022-01-25T18:11:24.320733Z","shell.execute_reply.started":"2022-01-25T18:11:16.68122Z","shell.execute_reply":"2022-01-25T18:11:24.319924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot feature map of first conv layer for given image\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Model\nfrom matplotlib import pyplot\nfrom numpy import expand_dims\npyplot.figure(figsize=(20, 20), dpi=80)\n# load the model\nmodel = DenseNet201()\n# redefine model to output right after the first hidden layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[704].output)\n#model.summary()\n# load the image with the required shape\nimg = load_img('../input/state-farm-distracted-driver-detection/imgs/train/c0/img_100026.jpg', target_size=(224, 224))\n# convert the image to an array\nimg = img_to_array(img)\n# expand dimensions so that it represents a single 'sample'\nimg = expand_dims(img, axis=0)\n# prepare the image (e.g. scale pixel values for the vgg)\nimg = preprocess_input(img)\n# get feature map for first hidden layer\nfeature_maps = model.predict(img)\n#print(feature_maps)\n# plot all 64 maps in an 8x8 squares\nsquare = 8\nix = 1\nfor _ in range(40):\n\tfor _ in range(48):\n\t\t# specify subplot and turn of axis\n\t\tax = pyplot.subplot(40, 48, ix)\n\t\tax.set_xticks([])\n\t\tax.set_yticks([])\n\t\t# plot filter channel in grayscale\n\t\tpyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n\t\tix += 1\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T18:11:30.111129Z","iopub.execute_input":"2022-01-25T18:11:30.111481Z","iopub.status.idle":"2022-01-25T18:13:47.201577Z","shell.execute_reply.started":"2022-01-25T18:11:30.111449Z","shell.execute_reply":"2022-01-25T18:13:47.200754Z"},"trusted":true},"execution_count":null,"outputs":[]}]}