{"cells":[{"metadata":{"trusted":true,"_kg_hide-output":true,"collapsed":true},"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\nimport numpy as np\nimport keras.backend as K\nimport tensorflow as tf\n\nK.clear_session()\n# load the model\nmodel = VGG16()\nmodel.summary()\n\n# load the image with the required shape\nimg = load_img('../input/treebird/bird.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\npreds_raw = model.predict(img)\nprint(\"dtype of preds_raw is\", type(preds_raw))\nprint(\"since preds_raw is numpy we use .shape to determine its shape\")\nprint(\"the shape of the preds_raw is\", preds_raw.shape)\nprint(model.output.eval(session=K.get_session(), feed_dict={'input_1:0': img}))#that's how to determine the value of tensor   \nprint(\"model.output datatype is\", type(model.output))\nprint(\"eventhough model.output is a tensor we use .shape to determine its shape\")\nprint(\"shape of model.output is\", model.output.shape)\nsess = K.get_session()\nshapes = tf.shape(model.output)\nprint(\"dynamic shape of model.output is\",sess.run(shapes, feed_dict={'input_1:0': img}))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":false},"cell_type":"code","source":"#comparing the shape of the two meethods of producing 2nd layer output\nimport keras.backend as K\n\noutputs=model.layers[1].output\nprint(\"data type of outputs is\",type(outputs))\nprint(\"eventhough datatype of outputs is a tensor we use .shape to determine its shape\")\nprint(\"the shape of the outputs is\", outputs.shape)\nsess = K.get_session()\nshapes = tf.shape(outputs)\nprint(\"dynamic shape of model.output is\",sess.run(shapes, feed_dict={'input_1:0': img}))\n#print(outputs.eval(session=K.get_session(), feed_dict={'input_1:0': img})) #that's how to determine the value of tensor\nsquare = 8\nix = 1\nfor _ in range(square):\n    for _ in range(square):\n        # specify subplot and turn of axis\n        ax = pyplot.subplot(square, square, ix)\n        ax.set_xticks([])\n        ax.set_yticks([])\n        # plot filter channel in grayscale\n        np_array = outputs[0, :, :, ix-1].eval(session=K.get_session(),feed_dict={'input_1:0': img})\n        pyplot.imshow(np_array, cmap='gray')\n        ix += 1\n# show the figure\npyplot.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#1- compare the layer output (outputs) and feature_map\n#2- plot the layer output (outputs)\nmodel1 = Model(inputs=model.inputs, outputs=model.layers[1].output)\nfeature_maps = model1.predict(img)\nprint(\"data type of feature_maps is\",type(feature_maps))\n#print(feature_maps)\nsquare = 8\nix = 1\nfor _ in range(square):\n    for _ in range(square):\n        # specify subplot and turn of axis\n        ax = pyplot.subplot(square, square, ix)\n        ax.set_xticks([])\n        ax.set_yticks([])\n        # plot filter channel in grayscale\n        pyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')\n        ix += 1\n# show the figure\npyplot.show()\n","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}