├── .gitignore ├── README.md ├── Read This Before You Get Started.ipynb ├── Upgrad DL ├── CNN Industrial application │ ├── Industry Demo Using CNNs with Flowers Images │ │ ├── Working_With_Flower_Images.ipynb │ │ └── resnet.py │ └── Industry Demo Using CNNs with X-ray Images │ │ ├── Working_With_Chest_XRay_Images-.ipynb │ │ └── resnet.py ├── CNN │ ├── Building CNN with Python and Keras │ │ ├── 1.+Cifar_10_with_dropout_without_BN.ipynb │ │ ├── 2.+Cifar_10_Notebook_with_BN_without_dropout.ipynb │ │ ├── 3.+Cifar_10_notebook.ipynb │ │ ├── 4.+Cifar10_l2_notebook.ipynb │ │ ├── 5.+Cifar10_l2_dropout_notebook.ipynb │ │ ├── 6.+Cifar10_morelayer_notebook.ipynb │ │ ├── 7.+Cifar10_feature_map_updated.ipynb │ │ └── Building+a+CNN+-+MNIST.ipynb │ ├── Style_Transfer_Updated TF2.0.ipynb │ ├── rabbit.jpg │ └── style.jpg ├── Gesture Recognition Assignment │ └── Starter_Code_Gesture_Recognition.ipynb ├── RNN │ ├── 1D CNN │ │ ├── 1d-cnn-rnn-notebook-main.ipynb │ │ └── resources │ │ │ ├── basic_intent.png │ │ │ ├── cnn-1d-rnn.jpg │ │ │ ├── textmining.png │ │ │ └── wordvectors.png │ └── Building RNN in Python │ │ ├── POS_tagger.ipynb │ │ └── rnn_code_generator.ipynb └── Transfer Learning │ └── Transfer_Learning_Notebook.ipynb ├── images ├── img1.png ├── img2.png ├── img3.png └── img4.png └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # PyInstaller 26 | # Usually these files are written by a python script from a template 27 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 28 | *.manifest 29 | *.spec 30 | 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | htmlcov/ 37 | .tox/ 38 | .coverage 39 | .cache 40 | nosetests.xml 41 | coverage.xml 42 | 43 | # Translations 44 | *.mo 45 | *.pot 46 | 47 | # Django stuff: 48 | *.log 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | 56 | # IPython notebook 57 | .ipynb_checkpoints 58 | 59 | # Repo scratch directory 60 | scratch/ 61 | 62 | # Misc 63 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to cloud based lab for deep learning module. 2 | 3 | ## TOC: 4 | - What is where? 5 | - Common gotchas to avoid? 6 | 7 | ### What is where? 8 | The folder structure is given below: 9 | 10 | ![](images/img4.png) 11 | 12 | As you can see there are two main folders when you log in: 13 | 14 | 1. **datasets:** This is where all the datasets you will need to run the notebooks are kept. (There are some additional files as well you can ignore them). So in the virtual machine you have logged into the path to this folder will be `/home/datasets`. Note that `/home/datasets` is a **read only** directory. 15 | 16 | 2. **dl_content:** This is where all the code files are kept. You will need to refer to the directory `Upgrad DL` within `dl_content`. 17 | 18 | ![](./images/img3.png) 19 | 20 | As you can see the folder structure is loosely based on the deep learning course on Upgrad Platform. You will be working with the `.ipynb files.` 21 | 22 | ### Common gotchas to avoid 23 | 24 | 1. **Always shut down the notebooks when you are done with your work** 25 | ![](./images/img1.png) 26 | 27 | 2. **Don't run two notebooks (if using tensorflow) simultaneously** 28 | 29 | Tensorflow has the tendency to use all the gpu memory. If you are running one notebook in which you are training a tensorflow model, trying to run another notebook and training a tensorflow model will give you an error. 30 | 31 | 3. **Don't install too many python libraries in the main environment** 32 | 33 | Commonly used libraries that you will need to run the codes and submit assignment/case study are already installed. In case you need to install many new libraries, make use of the terminal and create a separate virtual environment and install the libraries in that venv and use that venv. [Learn how to create virtual environments](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) 34 | 35 | 36 | ![](./images/img2.png) 37 | 38 | 4. **Don't make copies of data files** 39 | 40 | There is only 20GB of hard disk allocated for each user. In this disk all the necessary datasets are placed. If you make unnecessary copies of data, you might end up filling up your disk space. 41 | 42 | -------------------------------------------------------------------------------- /Read This Before You Get Started.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Welcome to cloud based lab for deep learning module.\n", 8 | "\n", 9 | "## TOC:\n", 10 | "- What is where?\n", 11 | "- Common gotchas to avoid?\n", 12 | "\n", 13 | "### What is where?\n", 14 | "The folder structure is given below:\n", 15 | "\n", 16 | "![](images/img4.png)\n", 17 | "\n", 18 | "As you can see there are two main folders when you log in:\n", 19 | "\n", 20 | "1. **datasets:** This is where all the datasets you will need to run the notebooks are kept. (There are some additional files as well you can ignore them). So in the virtual machine you have logged into the path to this folder will be `/home/datasets`. Note that `/home/datasets` is a **read only** directory.\n", 21 | "\n", 22 | "2. **dl_content:** This is where all the code files are kept. You will need to refer to the directory `Upgrad DL` within `dl_content`.\n", 23 | "\n", 24 | "![](./images/img3.png)\n", 25 | "\n", 26 | "As you can see the folder structure is loosely based on the deep learning course on Upgrad Platform. You will be working with the `.ipynb files.`\n", 27 | "\n", 28 | " ### Common gotchas to avoid\n", 29 | "\n", 30 | " 1. **Always shut down the notebooks when you are done with your work**\n", 31 | "![](./images/img1.png)\n", 32 | "\n", 33 | "2. **Don't run two notebooks (if using tensorflow) simultaneously**\n", 34 | " \n", 35 | "Tensorflow has the tendency to use all the gpu memory. If you are running one notebook in which you are training a tensorflow model, trying to run another notebook and training a tensorflow model will give you an error.\n", 36 | "\n", 37 | "3. **Don't install too many python libraries in the main environment**\n", 38 | " \n", 39 | " Commonly used libraries that you will need to run the codes and submit assignment/case study are already installed. In case you need to install many new libraries, make use of the terminal and create a separate virtual environment and install the libraries in that venv and use that venv. [Learn how to create virtual environments](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)\n", 40 | "\n", 41 | "\n", 42 | "![](./images/img2.png)\n", 43 | "\n", 44 | " 4. **Don't make copies of data files**\n", 45 | "\n", 46 | " There is only 20GB of hard disk allocated for each user. In this disk all the necessary datasets are placed. If you make unnecessary copies of data, you might end up filling up your disk space.\n", 47 | "\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3.7.4 64-bit ('base': conda)", 61 | "language": "python", 62 | "name": "python37464bitbasecondae1ffa1412f074f08abd627806a6fc789" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.7.4" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Upgrad DL/CNN Industrial application/Industry Demo Using CNNs with Flowers Images/resnet.py: -------------------------------------------------------------------------------- 1 | import six 2 | from tensorflow.keras.models import Model 3 | from tensorflow.keras.layers import ( 4 | Input, 5 | Activation, 6 | Dense, 7 | Flatten, 8 | add, 9 | BatchNormalization, 10 | Conv2D, 11 | MaxPooling2D, 12 | AveragePooling2D 13 | ) 14 | 15 | from tensorflow.keras.regularizers import l2 16 | from tensorflow.keras import backend as K 17 | 18 | 19 | def _bn_relu(input): 20 | """Helper to build a BN -> relu block 21 | """ 22 | norm = BatchNormalization(axis=CHANNEL_AXIS)(input) 23 | return Activation("relu")(norm) 24 | 25 | 26 | def _conv_bn_relu(**conv_params): 27 | """Helper to build a conv -> BN -> relu block 28 | """ 29 | filters = conv_params["filters"] 30 | kernel_size = conv_params["kernel_size"] 31 | strides = conv_params.setdefault("strides", (1, 1)) 32 | kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal") 33 | padding = conv_params.setdefault("padding", "same") 34 | kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4)) 35 | 36 | def f(input): 37 | conv = Conv2D(filters=filters, kernel_size=kernel_size, 38 | strides=strides, padding=padding, 39 | kernel_initializer=kernel_initializer, 40 | kernel_regularizer=kernel_regularizer)(input) 41 | return _bn_relu(conv) 42 | 43 | return f 44 | 45 | 46 | def _bn_relu_conv(**conv_params): 47 | """Helper to build a BN -> relu -> conv block. 48 | This is an improved scheme proposed in http://arxiv.org/pdf/1603.05027v2.pdf 49 | """ 50 | filters = conv_params["filters"] 51 | kernel_size = conv_params["kernel_size"] 52 | strides = conv_params.setdefault("strides", (1, 1)) 53 | kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal") 54 | padding = conv_params.setdefault("padding", "same") 55 | kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4)) 56 | 57 | def f(input): 58 | activation = _bn_relu(input) 59 | return Conv2D(filters=filters, kernel_size=kernel_size, 60 | strides=strides, padding=padding, 61 | kernel_initializer=kernel_initializer, 62 | kernel_regularizer=kernel_regularizer)(activation) 63 | 64 | return f 65 | 66 | 67 | def _shortcut(input, residual): 68 | """Adds a shortcut between input and residual block and merges them with "sum" 69 | """ 70 | # Expand channels of shortcut to match residual. 71 | # Stride appropriately to match residual (width, height) 72 | # Should be int if network architecture is correctly configured. 73 | input_shape = K.int_shape(input) 74 | residual_shape = K.int_shape(residual) 75 | stride_width = int(round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS])) 76 | stride_height = int(round(input_shape[COL_AXIS] / residual_shape[COL_AXIS])) 77 | equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[CHANNEL_AXIS] 78 | 79 | shortcut = input 80 | # 1 X 1 conv if shape is different. Else identity. 81 | if stride_width > 1 or stride_height > 1 or not equal_channels: 82 | shortcut = Conv2D(filters=residual_shape[CHANNEL_AXIS], 83 | kernel_size=(1, 1), 84 | strides=(stride_width, stride_height), 85 | padding="valid", 86 | kernel_initializer="he_normal", 87 | kernel_regularizer=l2(0.0001))(input) 88 | 89 | return add([shortcut, residual]) 90 | 91 | 92 | def _residual_block(block_function, filters, repetitions, is_first_layer=False): 93 | """Builds a residual block with repeating bottleneck blocks. 94 | """ 95 | def f(input): 96 | for i in range(repetitions): 97 | init_strides = (1, 1) 98 | if i == 0 and not is_first_layer: 99 | init_strides = (2, 2) 100 | input = block_function(filters=filters, init_strides=init_strides, 101 | is_first_block_of_first_layer=(is_first_layer and i == 0))(input) 102 | return input 103 | 104 | return f 105 | 106 | 107 | def basic_block(filters, init_strides=(1, 1), is_first_block_of_first_layer=False): 108 | """Basic 3 X 3 convolution blocks for use on resnets with layers <= 34. 109 | Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf 110 | """ 111 | def f(input): 112 | 113 | if is_first_block_of_first_layer: 114 | # don't repeat bn->relu since we just did bn->relu->maxpool 115 | conv1 = Conv2D(filters=filters, kernel_size=(3, 3), 116 | strides=init_strides, 117 | padding="same", 118 | kernel_initializer="he_normal", 119 | kernel_regularizer=l2(1e-4))(input) 120 | else: 121 | conv1 = _bn_relu_conv(filters=filters, kernel_size=(3, 3), 122 | strides=init_strides)(input) 123 | 124 | residual = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv1) 125 | return _shortcut(input, residual) 126 | 127 | return f 128 | 129 | 130 | def bottleneck(filters, init_strides=(1, 1), is_first_block_of_first_layer=False): 131 | """Bottleneck architecture for > 34 layer resnet. 132 | Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf 133 | Returns: 134 | A final conv layer of filters * 4 135 | """ 136 | def f(input): 137 | 138 | if is_first_block_of_first_layer: 139 | # don't repeat bn->relu since we just did bn->relu->maxpool 140 | conv_1_1 = Conv2D(filters=filters, kernel_size=(1, 1), 141 | strides=init_strides, 142 | padding="same", 143 | kernel_initializer="he_normal", 144 | kernel_regularizer=l2(1e-4))(input) 145 | else: 146 | conv_1_1 = _bn_relu_conv(filters=filters, kernel_size=(1, 1), 147 | strides=init_strides)(input) 148 | 149 | conv_3_3 = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv_1_1) 150 | residual = _bn_relu_conv(filters=filters * 4, kernel_size=(1, 1))(conv_3_3) 151 | return _shortcut(input, residual) 152 | 153 | return f 154 | 155 | 156 | def _handle_dim_ordering(): 157 | global ROW_AXIS 158 | global COL_AXIS 159 | global CHANNEL_AXIS 160 | #if K.image_dim_ordering() == 'tf': 161 | ROW_AXIS = 1 162 | COL_AXIS = 2 163 | CHANNEL_AXIS = 3 164 | #else: 165 | #CHANNEL_AXIS = 1 166 | #ROW_AXIS = 2 167 | #COL_AXIS = 3 168 | 169 | 170 | def _get_block(identifier): 171 | if isinstance(identifier, six.string_types): 172 | res = globals().get(identifier) 173 | if not res: 174 | raise ValueError('Invalid {}'.format(identifier)) 175 | return res 176 | return identifier 177 | 178 | 179 | class ResnetBuilder(object): 180 | @staticmethod 181 | def build(input_shape, num_outputs, block_fn, repetitions): 182 | """Builds a custom ResNet like architecture. 183 | Args: 184 | input_shape: The input shape in the form (nb_channels, nb_rows, nb_cols) 185 | num_outputs: The number of outputs at final softmax layer 186 | block_fn: The block function to use. This is either `basic_block` or `bottleneck`. 187 | The original paper used basic_block for layers < 50 188 | repetitions: Number of repetitions of various block units. 189 | At each block unit, the number of filters are doubled and the input size is halved 190 | Returns: 191 | The keras `Model`. 192 | """ 193 | _handle_dim_ordering() 194 | if len(input_shape) != 3: 195 | raise Exception("Input shape should be a tuple (nb_channels, nb_rows, nb_cols)") 196 | 197 | # Permute dimension order if necessary 198 | # if K.image_dim_ordering() == 'tf': 199 | input_shape = (input_shape[1], input_shape[2], input_shape[0]) 200 | 201 | # Load function from str if needed. 202 | block_fn = _get_block(block_fn) 203 | 204 | input = Input(shape=input_shape) 205 | conv1 = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(2, 2))(input) 206 | pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same")(conv1) 207 | 208 | block = pool1 209 | filters = 64 210 | for i, r in enumerate(repetitions): 211 | block = _residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block) 212 | filters *= 2 213 | 214 | # Last activation 215 | block = _bn_relu(block) 216 | 217 | # Classifier block 218 | block_shape = K.int_shape(block) 219 | pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]), 220 | strides=(1, 1))(block) 221 | flatten1 = Flatten()(pool2) 222 | dense = Dense(units=num_outputs, kernel_initializer="he_normal", 223 | activation="softmax")(flatten1) 224 | 225 | model = Model(inputs=input, outputs=dense) 226 | return model 227 | 228 | @staticmethod 229 | def build_resnet_18(input_shape, num_outputs): 230 | return ResnetBuilder.build(input_shape, num_outputs, basic_block, [2, 2, 2, 2]) 231 | 232 | @staticmethod 233 | def build_resnet_34(input_shape, num_outputs): 234 | return ResnetBuilder.build(input_shape, num_outputs, basic_block, [3, 4, 6, 3]) 235 | 236 | @staticmethod 237 | def build_resnet_50(input_shape, num_outputs): 238 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 6, 3]) 239 | 240 | @staticmethod 241 | def build_resnet_101(input_shape, num_outputs): 242 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 23, 3]) 243 | 244 | @staticmethod 245 | def build_resnet_152(input_shape, num_outputs): 246 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 8, 36, 3]) -------------------------------------------------------------------------------- /Upgrad DL/CNN Industrial application/Industry Demo Using CNNs with X-ray Images/resnet.py: -------------------------------------------------------------------------------- 1 | import six 2 | from tensorflow.keras.models import Model 3 | from tensorflow.keras.layers import ( 4 | Input, 5 | Activation, 6 | Dense, 7 | Flatten, 8 | add, 9 | BatchNormalization, 10 | Conv2D, 11 | MaxPooling2D, 12 | AveragePooling2D 13 | ) 14 | 15 | from tensorflow.keras.regularizers import l2 16 | from tensorflow.keras import backend as K 17 | 18 | 19 | def _bn_relu(input): 20 | """Helper to build a BN -> relu block 21 | """ 22 | norm = BatchNormalization(axis=CHANNEL_AXIS)(input) 23 | return Activation("relu")(norm) 24 | 25 | 26 | def _conv_bn_relu(**conv_params): 27 | """Helper to build a conv -> BN -> relu block 28 | """ 29 | filters = conv_params["filters"] 30 | kernel_size = conv_params["kernel_size"] 31 | strides = conv_params.setdefault("strides", (1, 1)) 32 | kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal") 33 | padding = conv_params.setdefault("padding", "same") 34 | kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4)) 35 | 36 | def f(input): 37 | conv = Conv2D(filters=filters, kernel_size=kernel_size, 38 | strides=strides, padding=padding, 39 | kernel_initializer=kernel_initializer, 40 | kernel_regularizer=kernel_regularizer)(input) 41 | return _bn_relu(conv) 42 | 43 | return f 44 | 45 | 46 | def _bn_relu_conv(**conv_params): 47 | """Helper to build a BN -> relu -> conv block. 48 | This is an improved scheme proposed in http://arxiv.org/pdf/1603.05027v2.pdf 49 | """ 50 | filters = conv_params["filters"] 51 | kernel_size = conv_params["kernel_size"] 52 | strides = conv_params.setdefault("strides", (1, 1)) 53 | kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal") 54 | padding = conv_params.setdefault("padding", "same") 55 | kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4)) 56 | 57 | def f(input): 58 | activation = _bn_relu(input) 59 | return Conv2D(filters=filters, kernel_size=kernel_size, 60 | strides=strides, padding=padding, 61 | kernel_initializer=kernel_initializer, 62 | kernel_regularizer=kernel_regularizer)(activation) 63 | 64 | return f 65 | 66 | 67 | def _shortcut(input, residual): 68 | """Adds a shortcut between input and residual block and merges them with "sum" 69 | """ 70 | # Expand channels of shortcut to match residual. 71 | # Stride appropriately to match residual (width, height) 72 | # Should be int if network architecture is correctly configured. 73 | input_shape = K.int_shape(input) 74 | residual_shape = K.int_shape(residual) 75 | stride_width = int(round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS])) 76 | stride_height = int(round(input_shape[COL_AXIS] / residual_shape[COL_AXIS])) 77 | equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[CHANNEL_AXIS] 78 | 79 | shortcut = input 80 | # 1 X 1 conv if shape is different. Else identity. 81 | if stride_width > 1 or stride_height > 1 or not equal_channels: 82 | shortcut = Conv2D(filters=residual_shape[CHANNEL_AXIS], 83 | kernel_size=(1, 1), 84 | strides=(stride_width, stride_height), 85 | padding="valid", 86 | kernel_initializer="he_normal", 87 | kernel_regularizer=l2(0.0001))(input) 88 | 89 | return add([shortcut, residual]) 90 | 91 | 92 | def _residual_block(block_function, filters, repetitions, is_first_layer=False): 93 | """Builds a residual block with repeating bottleneck blocks. 94 | """ 95 | def f(input): 96 | for i in range(repetitions): 97 | init_strides = (1, 1) 98 | if i == 0 and not is_first_layer: 99 | init_strides = (2, 2) 100 | input = block_function(filters=filters, init_strides=init_strides, 101 | is_first_block_of_first_layer=(is_first_layer and i == 0))(input) 102 | return input 103 | 104 | return f 105 | 106 | 107 | def basic_block(filters, init_strides=(1, 1), is_first_block_of_first_layer=False): 108 | """Basic 3 X 3 convolution blocks for use on resnets with layers <= 34. 109 | Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf 110 | """ 111 | def f(input): 112 | 113 | if is_first_block_of_first_layer: 114 | # don't repeat bn->relu since we just did bn->relu->maxpool 115 | conv1 = Conv2D(filters=filters, kernel_size=(3, 3), 116 | strides=init_strides, 117 | padding="same", 118 | kernel_initializer="he_normal", 119 | kernel_regularizer=l2(1e-4))(input) 120 | else: 121 | conv1 = _bn_relu_conv(filters=filters, kernel_size=(3, 3), 122 | strides=init_strides)(input) 123 | 124 | residual = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv1) 125 | return _shortcut(input, residual) 126 | 127 | return f 128 | 129 | 130 | def bottleneck(filters, init_strides=(1, 1), is_first_block_of_first_layer=False): 131 | """Bottleneck architecture for > 34 layer resnet. 132 | Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf 133 | Returns: 134 | A final conv layer of filters * 4 135 | """ 136 | def f(input): 137 | 138 | if is_first_block_of_first_layer: 139 | # don't repeat bn->relu since we just did bn->relu->maxpool 140 | conv_1_1 = Conv2D(filters=filters, kernel_size=(1, 1), 141 | strides=init_strides, 142 | padding="same", 143 | kernel_initializer="he_normal", 144 | kernel_regularizer=l2(1e-4))(input) 145 | else: 146 | conv_1_1 = _bn_relu_conv(filters=filters, kernel_size=(1, 1), 147 | strides=init_strides)(input) 148 | 149 | conv_3_3 = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv_1_1) 150 | residual = _bn_relu_conv(filters=filters * 4, kernel_size=(1, 1))(conv_3_3) 151 | return _shortcut(input, residual) 152 | 153 | return f 154 | 155 | 156 | def _handle_dim_ordering(): 157 | global ROW_AXIS 158 | global COL_AXIS 159 | global CHANNEL_AXIS 160 | #if K.image_dim_ordering() == 'tf': 161 | ROW_AXIS = 1 162 | COL_AXIS = 2 163 | CHANNEL_AXIS = 3 164 | #else: 165 | #CHANNEL_AXIS = 1 166 | #ROW_AXIS = 2 167 | #COL_AXIS = 3 168 | 169 | 170 | def _get_block(identifier): 171 | if isinstance(identifier, six.string_types): 172 | res = globals().get(identifier) 173 | if not res: 174 | raise ValueError('Invalid {}'.format(identifier)) 175 | return res 176 | return identifier 177 | 178 | 179 | class ResnetBuilder(object): 180 | @staticmethod 181 | def build(input_shape, num_outputs, block_fn, repetitions): 182 | """Builds a custom ResNet like architecture. 183 | Args: 184 | input_shape: The input shape in the form (nb_channels, nb_rows, nb_cols) 185 | num_outputs: The number of outputs at final softmax layer 186 | block_fn: The block function to use. This is either `basic_block` or `bottleneck`. 187 | The original paper used basic_block for layers < 50 188 | repetitions: Number of repetitions of various block units. 189 | At each block unit, the number of filters are doubled and the input size is halved 190 | Returns: 191 | The keras `Model`. 192 | """ 193 | _handle_dim_ordering() 194 | if len(input_shape) != 3: 195 | raise Exception("Input shape should be a tuple (nb_channels, nb_rows, nb_cols)") 196 | 197 | # Permute dimension order if necessary 198 | # if K.image_dim_ordering() == 'tf': 199 | input_shape = (input_shape[1], input_shape[2], input_shape[0]) 200 | 201 | # Load function from str if needed. 202 | block_fn = _get_block(block_fn) 203 | 204 | input = Input(shape=input_shape) 205 | conv1 = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(2, 2))(input) 206 | pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same")(conv1) 207 | 208 | block = pool1 209 | filters = 64 210 | for i, r in enumerate(repetitions): 211 | block = _residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block) 212 | filters *= 2 213 | 214 | # Last activation 215 | block = _bn_relu(block) 216 | 217 | # Classifier block 218 | block_shape = K.int_shape(block) 219 | pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]), 220 | strides=(1, 1))(block) 221 | flatten1 = Flatten()(pool2) 222 | dense = Dense(units=num_outputs, kernel_initializer="he_normal", 223 | activation="softmax")(flatten1) 224 | 225 | model = Model(inputs=input, outputs=dense) 226 | return model 227 | 228 | @staticmethod 229 | def build_resnet_18(input_shape, num_outputs): 230 | return ResnetBuilder.build(input_shape, num_outputs, basic_block, [2, 2, 2, 2]) 231 | 232 | @staticmethod 233 | def build_resnet_34(input_shape, num_outputs): 234 | return ResnetBuilder.build(input_shape, num_outputs, basic_block, [3, 4, 6, 3]) 235 | 236 | @staticmethod 237 | def build_resnet_50(input_shape, num_outputs): 238 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 6, 3]) 239 | 240 | @staticmethod 241 | def build_resnet_101(input_shape, num_outputs): 242 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 23, 3]) 243 | 244 | @staticmethod 245 | def build_resnet_152(input_shape, num_outputs): 246 | return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 8, 36, 3]) -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/1.+Cifar_10_with_dropout_without_BN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# import libraries\n", 10 | "import tensorflow as tf\n", 11 | "from tensorflow.keras.datasets import cifar10\n", 12 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 13 | "from tensorflow.keras import Sequential\n", 14 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n", 15 | "import numpy as np\n", 16 | "import os\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "%matplotlib inline" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# batch, classes, epochs\n", 28 | "batch_size = 32\n", 29 | "num_classes = 10\n", 30 | "epochs = 50" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# The data, split between train and test sets:\n", 40 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 41 | "print('x_train shape:', x_train.shape)\n", 42 | "print(x_train.shape[0], 'train samples')\n", 43 | "print(x_test.shape[0], 'test samples')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# plotting some random 10 images\n", 53 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 54 | " 'dog','frog','horse','ship','truck']\n", 55 | "\n", 56 | "fig = plt.figure(figsize=(8,3))\n", 57 | "for i in range(num_classes):\n", 58 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 59 | " idx = np.where(y_train[:]==i)[0]\n", 60 | " features_idx = x_train[idx,::]\n", 61 | " img_num = np.random.randint(features_idx.shape[0])\n", 62 | " im = (features_idx[img_num,::])\n", 63 | " ax.set_title(class_names[i])\n", 64 | " plt.imshow(im)\n", 65 | "plt.show()" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Convert class vectors to binary class matrices.\n", 75 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 76 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "# model architecture\n", 86 | "model = Sequential()\n", 87 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 88 | " input_shape=x_train.shape[1:]))\n", 89 | "model.add(Activation('relu'))\n", 90 | "model.add(Conv2D(32, (3, 3)))\n", 91 | "model.add(Activation('relu'))\n", 92 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 93 | "model.add(Dropout(0.25))\n", 94 | "\n", 95 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 96 | "model.add(Activation('relu'))\n", 97 | "model.add(Conv2D(64, (3, 3)))\n", 98 | "model.add(Activation('relu'))\n", 99 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 100 | "model.add(Dropout(0.25))\n", 101 | "\n", 102 | "model.add(Flatten())\n", 103 | "model.add(Dense(512))\n", 104 | "model.add(Activation('relu'))\n", 105 | "model.add(Dropout(0.5))\n", 106 | "model.add(Dense(num_classes))\n", 107 | "model.add(Activation('softmax'))" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "# summary\n", 117 | "model.summary()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "# compile the model\n", 127 | "model.compile(loss='categorical_crossentropy',\n", 128 | " optimizer='sgd',\n", 129 | " metrics=['accuracy'])\n", 130 | "\n", 131 | "# convert to float, normalise the data\n", 132 | "x_train = x_train.astype('float32')\n", 133 | "x_test = x_test.astype('float32')\n", 134 | "x_train /= 255\n", 135 | "x_test /= 255\n" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "# train \n", 145 | "model.fit(x_train, y_train,\n", 146 | " batch_size=batch_size,\n", 147 | " epochs=epochs,\n", 148 | " validation_data=(x_test, y_test),\n", 149 | " shuffle=True)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Result: After 50 epochs, the difference between training and validation accuracy is low but the training is slow as compared to the model with batch normalization." 157 | ] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.7.3" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 2 181 | } 182 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/2.+Cifar_10_Notebook_with_BN_without_dropout.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.keras.datasets import cifar10\n", 11 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 12 | "from tensorflow.keras import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, Conv2D, MaxPooling2D\n", 14 | "import numpy as np\n", 15 | "import os\n", 16 | "import matplotlib.pyplot as plt\n", 17 | "%matplotlib inline" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "# Defining the parameters\n", 27 | "batch_size = 32\n", 28 | "num_classes = 10\n", 29 | "epochs = 50" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# Splitting the data between train and test\n", 39 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 40 | "print('x_train shape:', x_train.shape)\n", 41 | "print(x_train.shape[0], 'train samples')\n", 42 | "print(x_test.shape[0], 'test samples')" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "# plotting some random 10 images\n", 52 | "\n", 53 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 54 | " 'dog','frog','horse','ship','truck']\n", 55 | "\n", 56 | "fig = plt.figure(figsize=(8,3))\n", 57 | "for i in range(num_classes):\n", 58 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 59 | " idx = np.where(y_train[:]==i)[0]\n", 60 | " features_idx = x_train[idx,::]\n", 61 | " img_num = np.random.randint(features_idx.shape[0])\n", 62 | " im = (features_idx[img_num,::])\n", 63 | " ax.set_title(class_names[i])\n", 64 | " plt.imshow(im)\n", 65 | "plt.show()" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Convert class vectors to binary class matrices.\n", 75 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 76 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "# Printing sample data\n", 86 | "print(y_train[:10])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "model = Sequential()\n", 96 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 97 | " input_shape=x_train.shape[1:]))\n", 98 | "model.add(Activation('relu'))\n", 99 | "model.add(BatchNormalization())\n", 100 | "model.add(Conv2D(32, (3, 3)))\n", 101 | "model.add(Activation('relu'))\n", 102 | "model.add(BatchNormalization())\n", 103 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 104 | "\n", 105 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 106 | "model.add(Activation('relu'))\n", 107 | "model.add(BatchNormalization())\n", 108 | "model.add(Conv2D(64, (3, 3)))\n", 109 | "model.add(Activation('relu'))\n", 110 | "model.add(BatchNormalization())\n", 111 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 112 | "\n", 113 | "model.add(Flatten())\n", 114 | "model.add(Dense(512))\n", 115 | "model.add(Activation('relu'))\n", 116 | "model.add(Dropout(0.5))\n", 117 | "model.add(Dense(num_classes))\n", 118 | "model.add(Activation('softmax'))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "# summary of the model\n", 128 | "print(model.summary())" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "# compile\n", 138 | "model.compile(loss='categorical_crossentropy',\n", 139 | " optimizer='sgd',\n", 140 | " metrics=['accuracy'])\n", 141 | "\n", 142 | "x_train = x_train.astype('float32')\n", 143 | "x_test = x_test.astype('float32')\n", 144 | "\n", 145 | "# Normalizing the input image\n", 146 | "x_train /= 255\n", 147 | "x_test /= 255\n" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "# Training the model\n", 157 | "model.fit(x_train, y_train,\n", 158 | " batch_size=batch_size,\n", 159 | " epochs=epochs,\n", 160 | " validation_data=(x_test, y_test),\n", 161 | " shuffle=True)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "Result: The training accuracy is increasing because of BN but the difference between train accuracy and validation accuracy is low." 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.7.3" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 2 200 | } 201 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/3.+Cifar_10_notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.keras.datasets import cifar10\n", 11 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 12 | "from tensorflow.keras import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, Conv2D, MaxPooling2D\n", 14 | "import numpy as np\n", 15 | "import os\n", 16 | "import matplotlib.pyplot as plt\n", 17 | "%matplotlib inline" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "# Defining the parameters\n", 27 | "batch_size = 32\n", 28 | "num_classes = 10\n", 29 | "epochs = 50" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# Splitting the data between train and test\n", 39 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 40 | "print('x_train shape:', x_train.shape)\n", 41 | "print(x_train.shape[0], 'train samples')\n", 42 | "print(x_test.shape[0], 'test samples')" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "# plotting some random 10 images\n", 52 | "\n", 53 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 54 | " 'dog','frog','horse','ship','truck']\n", 55 | "\n", 56 | "fig = plt.figure(figsize=(8,3))\n", 57 | "for i in range(num_classes):\n", 58 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 59 | " idx = np.where(y_train[:]==i)[0]\n", 60 | " features_idx = x_train[idx,::]\n", 61 | " img_num = np.random.randint(features_idx.shape[0])\n", 62 | " im = (features_idx[img_num,::])\n", 63 | " ax.set_title(class_names[i])\n", 64 | " plt.imshow(im)\n", 65 | "plt.show()" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Convert class vectors to binary class matrices.\n", 75 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 76 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "# Printing sample data\n", 86 | "print(y_train[:10])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "model = Sequential()\n", 96 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 97 | " input_shape=x_train.shape[1:]))\n", 98 | "model.add(Activation('relu'))\n", 99 | "model.add(BatchNormalization())\n", 100 | "model.add(Conv2D(32, (3, 3)))\n", 101 | "model.add(Activation('relu'))\n", 102 | "model.add(BatchNormalization())\n", 103 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 104 | "model.add(Dropout(0.25))\n", 105 | "\n", 106 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 107 | "model.add(Activation('relu'))\n", 108 | "model.add(BatchNormalization())\n", 109 | "model.add(Conv2D(64, (3, 3)))\n", 110 | "model.add(Activation('relu'))\n", 111 | "model.add(BatchNormalization())\n", 112 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 113 | "model.add(Dropout(0.25))\n", 114 | "\n", 115 | "model.add(Flatten())\n", 116 | "model.add(Dense(512))\n", 117 | "model.add(Activation('relu'))\n", 118 | "model.add(Dropout(0.5))\n", 119 | "model.add(Dense(num_classes))\n", 120 | "model.add(Activation('softmax'))" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "# summary of the model\n", 130 | "print(model.summary())" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "# compile\n", 140 | "model.compile(loss='categorical_crossentropy',\n", 141 | " optimizer='sgd',\n", 142 | " metrics=['accuracy'])\n", 143 | "\n", 144 | "x_train = x_train.astype('float32')\n", 145 | "x_test = x_test.astype('float32')\n", 146 | "\n", 147 | "# Normalizing the input image\n", 148 | "x_train /= 255\n", 149 | "x_test /= 255\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "# Training the model\n", 159 | "model.fit(x_train, y_train,\n", 160 | " batch_size=batch_size,\n", 161 | " epochs=epochs,\n", 162 | " validation_data=(x_test, y_test),\n", 163 | " shuffle=True)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "# Model training done\n", 173 | "print('Model training done')" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [] 182 | } 183 | ], 184 | "metadata": { 185 | "kernelspec": { 186 | "display_name": "Python 3", 187 | "language": "python", 188 | "name": "python3" 189 | }, 190 | "language_info": { 191 | "codemirror_mode": { 192 | "name": "ipython", 193 | "version": 3 194 | }, 195 | "file_extension": ".py", 196 | "mimetype": "text/x-python", 197 | "name": "python", 198 | "nbconvert_exporter": "python", 199 | "pygments_lexer": "ipython3", 200 | "version": "3.7.3" 201 | } 202 | }, 203 | "nbformat": 4, 204 | "nbformat_minor": 2 205 | } 206 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/4.+Cifar10_l2_notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.keras.datasets import cifar10\n", 11 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 12 | "from tensorflow.keras import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, Conv2D, MaxPooling2D\n", 14 | "from tensorflow.keras.regularizers import l2\n", 15 | "import numpy as np\n", 16 | "import os\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "%matplotlib inline" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# Defining the parameters\n", 28 | "batch_size = 32\n", 29 | "num_classes = 10\n", 30 | "epochs = 50" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# Splitting the data between train and test\n", 40 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 41 | "print('x_train shape:', x_train.shape)\n", 42 | "print(x_train.shape[0], 'train samples')\n", 43 | "print(x_test.shape[0], 'test samples')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# plotting some random 10 images\n", 53 | "\n", 54 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 55 | " 'dog','frog','horse','ship','truck']\n", 56 | "\n", 57 | "fig = plt.figure(figsize=(8,3))\n", 58 | "for i in range(num_classes):\n", 59 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 60 | " idx = np.where(y_train[:]==i)[0]\n", 61 | " features_idx = x_train[idx,::]\n", 62 | " img_num = np.random.randint(features_idx.shape[0])\n", 63 | " im = (features_idx[img_num,::])\n", 64 | " ax.set_title(class_names[i])\n", 65 | " plt.imshow(im)\n", 66 | "plt.show()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "# Convert class vectors to binary class matrices.\n", 76 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 77 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Printing sample data\n", 87 | "print(y_train[:10])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "model = Sequential()\n", 97 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 98 | " input_shape=x_train.shape[1:]))\n", 99 | "model.add(Activation('relu'))\n", 100 | "model.add(BatchNormalization())\n", 101 | "model.add(Conv2D(32, (3, 3)))\n", 102 | "model.add(Activation('relu'))\n", 103 | "model.add(BatchNormalization())\n", 104 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 105 | "\n", 106 | "\n", 107 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 108 | "model.add(Activation('relu'))\n", 109 | "model.add(BatchNormalization())\n", 110 | "model.add(Conv2D(64, (3, 3)))\n", 111 | "model.add(Activation('relu'))\n", 112 | "model.add(BatchNormalization())\n", 113 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 114 | "\n", 115 | "\n", 116 | "model.add(Flatten())\n", 117 | "model.add(Dense(512,kernel_regularizer=l2(0.01)))\n", 118 | "model.add(Activation('relu'))\n", 119 | "model.add(Dropout(0.5))\n", 120 | "model.add(Dense(num_classes))\n", 121 | "model.add(Activation('softmax'))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# summary of the model\n", 131 | "print(model.summary())" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# compile\n", 141 | "model.compile(loss='categorical_crossentropy',\n", 142 | " optimizer='sgd',\n", 143 | " metrics=['accuracy'])\n", 144 | "\n", 145 | "x_train = x_train.astype('float32')\n", 146 | "x_test = x_test.astype('float32')\n", 147 | "\n", 148 | "# Normalizing the input image\n", 149 | "x_train /= 255\n", 150 | "x_test /= 255\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# Training the model\n", 160 | "model.fit(x_train, y_train,\n", 161 | " batch_size=batch_size,\n", 162 | " epochs=epochs,\n", 163 | " validation_data=(x_test, y_test),\n", 164 | " shuffle=True)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.7.3" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 2 203 | } 204 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/5.+Cifar10_l2_dropout_notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.keras.datasets import cifar10\n", 11 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 12 | "from tensorflow.keras import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, Conv2D, MaxPooling2D\n", 14 | "from tensorflow.keras.regularizers import l2\n", 15 | "import numpy as np\n", 16 | "import os\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "%matplotlib inline" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# Defining the parameters\n", 28 | "batch_size = 32\n", 29 | "num_classes = 10\n", 30 | "epochs = 50" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# Splitting the data between train and test\n", 40 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 41 | "print('x_train shape:', x_train.shape)\n", 42 | "print(x_train.shape[0], 'train samples')\n", 43 | "print(x_test.shape[0], 'test samples')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# plotting some random 10 images\n", 53 | "\n", 54 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 55 | " 'dog','frog','horse','ship','truck']\n", 56 | "\n", 57 | "fig = plt.figure(figsize=(8,3))\n", 58 | "for i in range(num_classes):\n", 59 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 60 | " idx = np.where(y_train[:]==i)[0]\n", 61 | " features_idx = x_train[idx,::]\n", 62 | " img_num = np.random.randint(features_idx.shape[0])\n", 63 | " im = (features_idx[img_num,::])\n", 64 | " ax.set_title(class_names[i])\n", 65 | " plt.imshow(im)\n", 66 | "plt.show()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "# Convert class vectors to binary class matrices.\n", 76 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 77 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Printing sample data\n", 87 | "print(y_train[:10])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "model = Sequential()\n", 97 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 98 | " input_shape=x_train.shape[1:]))\n", 99 | "model.add(Activation('relu'))\n", 100 | "model.add(BatchNormalization())\n", 101 | "model.add(Conv2D(32, (3, 3)))\n", 102 | "model.add(Activation('relu'))\n", 103 | "model.add(BatchNormalization())\n", 104 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 105 | "model.add(Dropout(0.25))\n", 106 | "\n", 107 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 108 | "model.add(Activation('relu'))\n", 109 | "model.add(BatchNormalization())\n", 110 | "model.add(Conv2D(64, (3, 3)))\n", 111 | "model.add(Activation('relu'))\n", 112 | "model.add(BatchNormalization())\n", 113 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 114 | "model.add(Dropout(0.25))\n", 115 | "\n", 116 | "model.add(Flatten())\n", 117 | "model.add(Dense(512,kernel_regularizer=l2(0.01)))\n", 118 | "model.add(Activation('relu'))\n", 119 | "model.add(Dropout(0.5))\n", 120 | "model.add(Dense(num_classes))\n", 121 | "model.add(Activation('softmax'))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# summary of the model\n", 131 | "print(model.summary())" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# compile\n", 141 | "model.compile(loss='categorical_crossentropy',\n", 142 | " optimizer='sgd',\n", 143 | " metrics=['accuracy'])\n", 144 | "\n", 145 | "x_train = x_train.astype('float32')\n", 146 | "x_test = x_test.astype('float32')\n", 147 | "\n", 148 | "# Normalizing the input image\n", 149 | "x_train /= 255\n", 150 | "x_test /= 255\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# Training the model\n", 160 | "history = model.fit(x_train, y_train,\n", 161 | " batch_size=batch_size,\n", 162 | " epochs=epochs,\n", 163 | " validation_data=(x_test, y_test),\n", 164 | " shuffle=True)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "# list all data in history\n", 174 | "print(history.history.keys())\n", 175 | "# summarize history for accuracy\n", 176 | "plt.plot(history.history['acc'])\n", 177 | "plt.plot(history.history['val_acc'])\n", 178 | "plt.title('model accuracy')\n", 179 | "plt.ylabel('accuracy')\n", 180 | "plt.xlabel('epoch')\n", 181 | "plt.legend(['train', 'test'], loc='upper left')\n", 182 | "plt.show()\n", 183 | "# summarize history for loss\n", 184 | "plt.plot(history.history['loss'])\n", 185 | "plt.plot(history.history['val_loss'])\n", 186 | "plt.title('model loss')\n", 187 | "plt.ylabel('loss')\n", 188 | "plt.xlabel('epoch')\n", 189 | "plt.legend(['train', 'test'], loc='upper left')\n", 190 | "plt.show()" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 3", 204 | "language": "python", 205 | "name": "python3" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 3 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython3", 217 | "version": "3.7.3" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 2 222 | } 223 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/6.+Cifar10_morelayer_notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.keras.datasets import cifar10\n", 11 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 12 | "from tensorflow.keras import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization, Conv2D, MaxPooling2D\n", 14 | "from tensorflow.keras.regularizers import l2\n", 15 | "import numpy as np\n", 16 | "import os\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "%matplotlib inline" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# Defining the parameters\n", 28 | "batch_size = 32\n", 29 | "num_classes = 10\n", 30 | "epochs = 50" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# Splitting the data between train and test\n", 40 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 41 | "print('x_train shape:', x_train.shape)\n", 42 | "print(x_train.shape[0], 'train samples')\n", 43 | "print(x_test.shape[0], 'test samples')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# plotting some random 10 images\n", 53 | "\n", 54 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 55 | " 'dog','frog','horse','ship','truck']\n", 56 | "\n", 57 | "fig = plt.figure(figsize=(8,3))\n", 58 | "for i in range(num_classes):\n", 59 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 60 | " idx = np.where(y_train[:]==i)[0]\n", 61 | " features_idx = x_train[idx,::]\n", 62 | " img_num = np.random.randint(features_idx.shape[0])\n", 63 | " im = (features_idx[img_num,::])\n", 64 | " ax.set_title(class_names[i])\n", 65 | " plt.imshow(im)\n", 66 | "plt.show()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "# Convert class vectors to binary class matrices.\n", 76 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 77 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Printing sample data\n", 87 | "print(y_train[:10])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "model = Sequential()\n", 97 | "model.add(Conv2D(32, (3, 3), padding='same',\n", 98 | " input_shape=x_train.shape[1:]))\n", 99 | "model.add(Activation('relu'))\n", 100 | "model.add(BatchNormalization())\n", 101 | "model.add(Conv2D(32, (3, 3)))\n", 102 | "model.add(Activation('relu'))\n", 103 | "model.add(BatchNormalization())\n", 104 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 105 | "model.add(Dropout(0.25))\n", 106 | "\n", 107 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 108 | "model.add(Activation('relu'))\n", 109 | "model.add(BatchNormalization())\n", 110 | "model.add(Conv2D(64, (3, 3)))\n", 111 | "model.add(Activation('relu'))\n", 112 | "model.add(BatchNormalization())\n", 113 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 114 | "model.add(Dropout(0.25))\n", 115 | "\n", 116 | "model.add(Conv2D(128, (3, 3), padding='same'))\n", 117 | "model.add(Activation('relu'))\n", 118 | "model.add(BatchNormalization())\n", 119 | "model.add(Conv2D(128, (3, 3)))\n", 120 | "model.add(Activation('relu'))\n", 121 | "model.add(BatchNormalization())\n", 122 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 123 | "model.add(Dropout(0.25))\n", 124 | "\n", 125 | "model.add(Flatten())\n", 126 | "model.add(Dense(512,kernel_regularizer=l2(0.01)))\n", 127 | "model.add(Activation('relu'))\n", 128 | "model.add(Dropout(0.5))\n", 129 | "model.add(Dense(num_classes))\n", 130 | "model.add(Activation('softmax'))" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "# summary of the model\n", 140 | "print(model.summary())" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# compile\n", 150 | "model.compile(loss='categorical_crossentropy',\n", 151 | " optimizer='sgd',\n", 152 | " metrics=['accuracy'])\n", 153 | "\n", 154 | "x_train = x_train.astype('float32')\n", 155 | "x_test = x_test.astype('float32')\n", 156 | "\n", 157 | "# Normalizing the input image\n", 158 | "x_train /= 255\n", 159 | "x_test /= 255\n" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# Training the model\n", 169 | "history = model.fit(x_train, y_train,\n", 170 | " batch_size=batch_size,\n", 171 | " epochs=epochs,\n", 172 | " validation_data=(x_test, y_test),\n", 173 | " shuffle=True)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "# list all data in history\n", 183 | "print(history.history.keys())\n", 184 | "# summarize history for accuracy\n", 185 | "plt.plot(history.history['acc'])\n", 186 | "plt.plot(history.history['val_acc'])\n", 187 | "plt.title('model accuracy')\n", 188 | "plt.ylabel('accuracy')\n", 189 | "plt.xlabel('epoch')\n", 190 | "plt.legend(['train', 'test'], loc='upper left')\n", 191 | "plt.show()\n", 192 | "# summarize history for loss\n", 193 | "plt.plot(history.history['loss'])\n", 194 | "plt.plot(history.history['val_loss'])\n", 195 | "plt.title('model loss')\n", 196 | "plt.ylabel('loss')\n", 197 | "plt.xlabel('epoch')\n", 198 | "plt.legend(['train', 'test'], loc='upper left')\n", 199 | "plt.show()" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [] 208 | } 209 | ], 210 | "metadata": { 211 | "kernelspec": { 212 | "display_name": "Python 3", 213 | "language": "python", 214 | "name": "python3" 215 | }, 216 | "language_info": { 217 | "codemirror_mode": { 218 | "name": "ipython", 219 | "version": 3 220 | }, 221 | "file_extension": ".py", 222 | "mimetype": "text/x-python", 223 | "name": "python", 224 | "nbconvert_exporter": "python", 225 | "pygments_lexer": "ipython3", 226 | "version": "3.7.3" 227 | } 228 | }, 229 | "nbformat": 4, 230 | "nbformat_minor": 2 231 | } 232 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/7.+Cifar10_feature_map_updated.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from __future__ import print_function\n", 10 | "import tensorflow as tf\n", 11 | "from tensorflow.keras.datasets import cifar10\n", 12 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 13 | "from tensorflow.keras.models import Sequential\n", 14 | "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten\n", 15 | "from tensorflow.keras.layers import BatchNormalization\n", 16 | "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", 17 | "from tensorflow.keras.regularizers import l2\n", 18 | "import numpy as np\n", 19 | "import os\n", 20 | "import matplotlib.pyplot as plt\n", 21 | "%matplotlib inline" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "# Defining the parameters\n", 31 | "batch_size = 32\n", 32 | "num_classes = 10\n", 33 | "epochs = 50" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# Splitting the data between train and test\n", 43 | "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", 44 | "print('x_train shape:', x_train.shape)\n", 45 | "print(x_train.shape[0], 'train samples')\n", 46 | "print(x_test.shape[0], 'test samples')" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "# plotting some random 10 images\n", 56 | "\n", 57 | "class_names = ['airplane','automobile','bird','cat','deer',\n", 58 | " 'dog','frog','horse','ship','truck']\n", 59 | "\n", 60 | "fig = plt.figure(figsize=(8,3))\n", 61 | "for i in range(num_classes):\n", 62 | " ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])\n", 63 | " idx = np.where(y_train[:]==i)[0]\n", 64 | " features_idx = x_train[idx,::]\n", 65 | " img_num = np.random.randint(features_idx.shape[0])\n", 66 | " im = (features_idx[img_num,::])\n", 67 | " ax.set_title(class_names[i])\n", 68 | " plt.imshow(im)\n", 69 | "plt.show()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# Convert class vectors to binary class matrices.\n", 79 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 80 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "# Printing sample data\n", 90 | "print(y_train[:10])" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "model = Sequential()\n", 100 | "model.add(Conv2D(64, (3, 3), padding='same',\n", 101 | " input_shape=x_train.shape[1:]))\n", 102 | "model.add(Activation('relu'))\n", 103 | "model.add(BatchNormalization())\n", 104 | "model.add(Conv2D(64, (3, 3)))\n", 105 | "model.add(Activation('relu'))\n", 106 | "model.add(BatchNormalization())\n", 107 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 108 | "model.add(Dropout(0.25))\n", 109 | "\n", 110 | "model.add(Conv2D(128, (3, 3), padding='same'))\n", 111 | "model.add(Activation('relu'))\n", 112 | "model.add(BatchNormalization())\n", 113 | "model.add(Conv2D(128, (3, 3)))\n", 114 | "model.add(Activation('relu'))\n", 115 | "model.add(BatchNormalization())\n", 116 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 117 | "model.add(Dropout(0.25))\n", 118 | "\n", 119 | "model.add(Flatten())\n", 120 | "model.add(Dense(512,kernel_regularizer=l2(0.01)))\n", 121 | "model.add(Activation('relu'))\n", 122 | "model.add(Dropout(0.5))\n", 123 | "model.add(Dense(num_classes))\n", 124 | "model.add(Activation('softmax'))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# summary of the model\n", 134 | "print(model.summary())" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "# compile\n", 144 | "model.compile(loss='categorical_crossentropy',\n", 145 | " optimizer='sgd',\n", 146 | " metrics=['accuracy'])\n", 147 | "\n", 148 | "x_train = x_train.astype('float32')\n", 149 | "x_test = x_test.astype('float32')\n", 150 | "\n", 151 | "# Normalizing the input image\n", 152 | "x_train /= 255\n", 153 | "x_test /= 255\n" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "epochs=5" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "# Training the model\n", 172 | "history = model.fit(x_train, y_train,\n", 173 | " batch_size=batch_size,\n", 174 | " epochs=epochs,\n", 175 | " validation_data=(x_test, y_test),\n", 176 | " shuffle=True)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [ 185 | "# list all data in history\n", 186 | "print(history.history.keys())\n", 187 | "# summarize history for accuracy\n", 188 | "plt.plot(history.history['acc'])\n", 189 | "plt.plot(history.history['val_acc'])\n", 190 | "plt.title('model accuracy')\n", 191 | "plt.ylabel('accuracy')\n", 192 | "plt.xlabel('epoch')\n", 193 | "plt.legend(['train', 'test'], loc='upper left')\n", 194 | "plt.show()\n", 195 | "# summarize history for loss\n", 196 | "plt.plot(history.history['loss'])\n", 197 | "plt.plot(history.history['val_loss'])\n", 198 | "plt.title('model loss')\n", 199 | "plt.ylabel('loss')\n", 200 | "plt.xlabel('epoch')\n", 201 | "plt.legend(['train', 'test'], loc='upper left')\n", 202 | "plt.show()" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": null, 208 | "metadata": {}, 209 | "outputs": [], 210 | "source": [] 211 | } 212 | ], 213 | "metadata": { 214 | "kernelspec": { 215 | "display_name": "Python 3", 216 | "language": "python", 217 | "name": "python3" 218 | }, 219 | "language_info": { 220 | "codemirror_mode": { 221 | "name": "ipython", 222 | "version": 3 223 | }, 224 | "file_extension": ".py", 225 | "mimetype": "text/x-python", 226 | "name": "python", 227 | "nbconvert_exporter": "python", 228 | "pygments_lexer": "ipython3", 229 | "version": "3.7.3" 230 | } 231 | }, 232 | "nbformat": 4, 233 | "nbformat_minor": 2 234 | } 235 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/Building CNN with Python and Keras/Building+a+CNN+-+MNIST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Building a Basic CNN: The MNIST Dataset\n", 8 | "\n", 9 | "In this notebook, we will build a simple CNN-based architecture to classify the 10 digits (0-9) of the MNIST dataset. The objective of this notebook is to become familiar with the process of building CNNs in Keras.\n", 10 | "\n", 11 | "We will go through the following steps:\n", 12 | "1. Importing libraries and the dataset\n", 13 | "2. Data preparation: Train-test split, specifying the shape of the input data etc.\n", 14 | "3. Building and understanding the CNN architecture \n", 15 | "4. Fitting and evaluating the model\n", 16 | "\n", 17 | "Let's dive in." 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "## 1. Importing Libraries and the Dataset\n", 25 | "\n", 26 | "Let's load the required libraries. From Keras, we need to import two main components:\n", 27 | "1. `Sequential` from `keras.models`: `Sequential` is the keras abstraction for creating models with a stack of layers (MLP has multiple hidden layers, CNNs have convolutional layers, etc.). \n", 28 | "2. Various types of layers from `keras.layers`: These layers are added (one after the other) to the `Sequential` model\n", 29 | "\n", 30 | "The keras `backend` is needed for keras to know that you are using tensorflow (not Theano) at the backend (the backend is specified in a JSON file). \n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "import numpy as np\n", 40 | "import random\n", 41 | "import tensorflow as tf\n", 42 | "from tensorflow.keras.datasets import mnist\n", 43 | "from tensorflow.keras import Sequential\n", 44 | "from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Let's load the MNIST dataset from `keras.datasets`. The download may take a few minutes." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "# load the dataset into train and test sets\n", 61 | "(x_train, y_train), (x_test, y_test) = mnist.load_data()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": { 68 | "scrolled": false 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "print(\"train data\")\n", 73 | "print(x_train.shape)\n", 74 | "print(y_train.shape)\n", 75 | "print(\"\\n test data\")\n", 76 | "print(x_test.shape)\n", 77 | "print(y_test.shape)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "So we have 60,000 training and 10,000 test images each of size 28 x 28. Note that the images are grayscale and thus are stored as 2D arrays.
\n", 85 | "\n", 86 | "Also, let's sample only 20k images for training (just to speed up the training a bit)." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "# sample only 20k images for training\n", 96 | "idx = np.random.randint(x_train.shape[0], size=20000) # sample 20k indices from 0-60,000\n", 97 | "x_train = x_train[idx, :]\n", 98 | "y_train = y_train[idx]\n", 99 | "print(x_train.shape)\n", 100 | "print(y_train.shape)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "## 2. Data Preparation\n", 108 | "\n", 109 | "Let's prepare the dataset for feeding to the network. We will do the following three main steps:
\n", 110 | "\n", 111 | "#### 2.1 Reshape the Data\n", 112 | "First, let's understand the shape in which the network expects the training data. \n", 113 | "Since we have 20,000 training samples each of size (28, 28, 1), the training data (`x_train`) needs to be of the shape `(20000, 28, 28, 1)`. If the images were coloured, the shape would have been `(20000, 28, 28, 3)`.\n", 114 | "\n", 115 | "Further, each of the 20,000 images have a 0-9 label, so `y_train` needs to be of the shape `(20000, 10)` where each image's label is represented as a 10-d **one-hot encoded vector**.\n", 116 | "\n", 117 | "The shapes of `x_test` and `y_test` will be the same as that of `x_train` and `y_train` respectively.\n", 118 | "\n", 119 | "#### 2.2 Rescaling (Normalisation)\n", 120 | "The value of each pixel is between 0-255, so we will **rescale each pixel** by dividing by 255 so that the range becomes 0-1. Recollect why normalisation is important for training NNs.\n", 121 | "\n", 122 | "#### 2.3 Converting Input Data Type: Int to Float\n", 123 | "The pixels are originally stored as type `int`, but it is advisable to feed the data as `float`. This is not really compulsory, but advisable. You can read why conversion from int to float is helpful here.\n" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# specify input dimensions of each image\n", 133 | "img_rows, img_cols = 28, 28\n", 134 | "input_shape = (img_rows, img_cols, 1)\n", 135 | "\n", 136 | "# batch size, number of classes, epochs\n", 137 | "batch_size = 128\n", 138 | "num_classes = 10\n", 139 | "epochs = 12" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Let's now reshape the array `x_train` from shape `(20000, 28, 28)`to `(20000, 28, 28, 1)`. Analogously for `x_test`." 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "scrolled": true 154 | }, 155 | "outputs": [], 156 | "source": [ 157 | "# reshape x_train and x_test\n", 158 | "x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n", 159 | "x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n", 160 | "print(x_train.shape)\n", 161 | "print(x_test.shape)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "Now let's reshape `y_train` from `(20000,)` to `(20000, 10)`. This can be conveniently done using the keras' `utils` module." 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": { 175 | "scrolled": true 176 | }, 177 | "outputs": [], 178 | "source": [ 179 | "# convert class labels (from digits) to one-hot encoded vectors\n", 180 | "y_train = tf.keras.utils.to_categorical(y_train, num_classes)\n", 181 | "y_test = tf.keras.utils.to_categorical(y_test, num_classes)\n", 182 | "print(y_train.shape)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Finally, let's convert the data type of `x_train` and `x_test` from int to float and normalise the images." 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "# originally, the pixels are stored as ints\n", 199 | "x_train.dtype" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "# convert int to float\n", 209 | "x_train = x_train.astype('float32')\n", 210 | "x_test = x_test.astype('float32')\n", 211 | "\n", 212 | "# normalise\n", 213 | "x_train /= 255\n", 214 | "x_test /= 255" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "## 3. Building the Model" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "Let's now build the CNN architecture. For the MNIST dataset, we do not need to build a very sophisticated CNN - a simple shallow-ish CNN would suffice. \n", 229 | "\n", 230 | "We will build a network with:\n", 231 | "- two convolutional layers having 32 and 64 filters respectively, \n", 232 | "- followed by a max pooling layer, \n", 233 | "- and then `Flatten` the output of the pooling layer to give us a long vector, \n", 234 | "- then add a fully connected `Dense` layer with 128 neurons, and finally\n", 235 | "- add a `softmax` layer with 10 neurons\n", 236 | "\n", 237 | "The generic way to build a model in Keras is to instantiate a `Sequential` model and keep adding `keras.layers` to it. We will also use some dropouts." 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": { 244 | "scrolled": false 245 | }, 246 | "outputs": [], 247 | "source": [ 248 | "# model\n", 249 | "model = Sequential()\n", 250 | "\n", 251 | "# a keras convolutional layer is called Conv2D\n", 252 | "# help(Conv2D)\n", 253 | "# note that the first layer needs to be told the input shape explicitly\n", 254 | "\n", 255 | "# first conv layer\n", 256 | "model.add(Conv2D(32, kernel_size=(3, 3),\n", 257 | " activation='relu',\n", 258 | " input_shape=input_shape)) # input shape = (img_rows, img_cols, 1)\n", 259 | "\n", 260 | "# second conv layer\n", 261 | "model.add(Conv2D(64, kernel_size=(3, 3), \n", 262 | " activation='relu'))\n", 263 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 264 | "model.add(Dropout(0.25))\n", 265 | "\n", 266 | "# flatten and put a fully connected layer\n", 267 | "model.add(Flatten())\n", 268 | "model.add(Dense(128, activation='relu')) # fully connected\n", 269 | "model.add(Dropout(0.5))\n", 270 | "\n", 271 | "# softmax layer\n", 272 | "model.add(Dense(num_classes, activation='softmax'))\n", 273 | "\n", 274 | "# model summary\n", 275 | "model.summary()\n" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "#### Understanding Model Summary\n", 283 | "\n", 284 | "It is a good practice to spend some time staring at the model summary above and verify the number of parameteres, output sizes etc. Let's do some calculations to verify that we understand the model deeply enough. \n", 285 | "\n", 286 | "- Layer-1 (Conv2D): We have used 32 kernels of size (3, 3), and each kernel has a single bias, so we have 32 x 3 x 3 (weights) + 32 (biases) = 320 parameters (all trainable). Note that the kernels have only one channel since the input images are 2D (grayscale). By default, a convolutional layer uses stride of 1 and no padding, so the output from this layer is of shape 26 x 26 x 32, as shown in the summary above (the first element `None` is for the batch size).\n", 287 | "\n", 288 | "- Layer-2 (Conv2D): We have used 64 kernels of size (3, 3), but this time, each kernel has to convolve a tensor of size (26, 26, 32) from the previous layer. Thus, the kernels will also have 32 channels, and so the shape of each kernel is (3, 3, 32) (and we have 64 of them). So we have 64 x 3 x 3 x 32 (weights) + 64 (biases) = 18496 parameters (all trainable). The output shape is (24, 24, 64) since each kernel produces a (24, 24) feature map.\n", 289 | "\n", 290 | "- Max pooling: The pooling layer gets the (24, 24, 64) input from the previous conv layer and produces a (12, 12, 64) output (the default pooling uses stride of 2). There are no trainable parameters in the pooling layer.\n", 291 | "\n", 292 | "- The `Dropout` layer does not alter the output shape and has no trainable parameters.\n", 293 | "\n", 294 | "- The `Flatten` layer simply takes in the (12, 12, 64) output from the previous layer and 'flattens' it into a vector of length 12 x 12 x 64 = 9216.\n", 295 | "\n", 296 | "- The `Dense` layer is a plain fully connected layer with 128 neurons. It takes the 9216-dimensional output vector from the previous layer (layer l-1) as the input and has 128 x 9216 (weights) + 128 (biases) = 1179776 trainable parameters. The output of this layer is a 128-dimensional vector.\n", 297 | "\n", 298 | "- The `Dropout` layer simply drops a few neurons.\n", 299 | "\n", 300 | "- Finally, we have a `Dense` softmax layer with 10 neurons which takes the 128-dimensional vector from the previous layer as input. It has 128 x 10 (weights) + 10 (biases) = 1290 trainable parameters.\n", 301 | "\n", 302 | "Thus, the total number of parameters are 1,199,882 all of which are trainable." 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "## 4. Fitting and Evaluating the Model\n", 310 | "\n", 311 | "Let's now compile and train the model." 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "# usual cross entropy loss\n", 321 | "# choose any optimiser such as adam, rmsprop etc\n", 322 | "# metric is accuracy\n", 323 | "model.compile(loss=tf.keras.losses.categorical_crossentropy,\n", 324 | " optimizer=tf.keras.optimizers.Adadelta(),\n", 325 | " metrics=['accuracy'])" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": null, 331 | "metadata": { 332 | "scrolled": false 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "# fit the model\n", 337 | "# this should take around 10-15 minutes when run locally on a windows/mac PC \n", 338 | "model.fit(x_train, y_train,\n", 339 | " batch_size=batch_size,\n", 340 | " epochs=epochs,\n", 341 | " verbose=1,\n", 342 | " validation_data=(x_test, y_test))" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": { 349 | "scrolled": false 350 | }, 351 | "outputs": [], 352 | "source": [ 353 | "# evaluate the model on test data\n", 354 | "model.evaluate(x_test, y_test)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [ 363 | "print(model.metrics_names)" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "The final loss (on test data) is about 0.04 and the accuracy is 98.59%." 371 | ] 372 | } 373 | ], 374 | "metadata": { 375 | "kernelspec": { 376 | "display_name": "Python 3", 377 | "language": "python", 378 | "name": "python3" 379 | }, 380 | "language_info": { 381 | "codemirror_mode": { 382 | "name": "ipython", 383 | "version": 3 384 | }, 385 | "file_extension": ".py", 386 | "mimetype": "text/x-python", 387 | "name": "python", 388 | "nbconvert_exporter": "python", 389 | "pygments_lexer": "ipython3", 390 | "version": "3.7.3" 391 | } 392 | }, 393 | "nbformat": 4, 394 | "nbformat_minor": 2 395 | } 396 | -------------------------------------------------------------------------------- /Upgrad DL/CNN/rabbit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/CNN/rabbit.jpg -------------------------------------------------------------------------------- /Upgrad DL/CNN/style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/CNN/style.jpg -------------------------------------------------------------------------------- /Upgrad DL/Gesture Recognition Assignment/Starter_Code_Gesture_Recognition.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Gesture Recognition\n", 8 | "In this group project, you are going to build a 3D Conv model that will be able to predict the 5 gestures correctly. Please import the following libraries to get started. Once you have completed the code you can download the notebook for making a submission." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "import numpy as np\n", 18 | "import os\n", 19 | "from imageio import imread\n", 20 | "from skimage.transform import resize\n", 21 | "import datetime\n", 22 | "import os" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "We set the random seed so that the results don't vary drastically." 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "np.random.seed(30)\n", 39 | "import random as rn\n", 40 | "rn.seed(30)\n", 41 | "from tensorflow import keras\n", 42 | "import tensorflow as tf\n", 43 | "tf.random.set_seed(30)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "In this block, you read the folder names for training and validation. You also set the `batch_size` here. Note that you set the batch size in such a way that you are able to use the GPU in full capacity. You keep increasing the batch size until the machine throws an error." 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "**data path: /home/datasets/Project_data**" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "train_doc = np.random.permutation(open('/home/datasets/Project_data/train.csv').readlines())\n", 67 | "val_doc = np.random.permutation(open('/home/datasets/Project_data/val.csv').readlines())\n", 68 | "batch_size = #experiment with the batch size" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "## Generator\n", 76 | "This is one of the most important part of the code. The overall structure of the generator has been given. In the generator, you are going to preprocess the images as you have images of 2 different dimensions as well as create a batch of video frames. You have to experiment with `img_idx`, `y`,`z` and normalization such that you get high accuracy." 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "def generator(source_path, folder_list, batch_size):\n", 86 | " print( 'Source path = ', source_path, '; batch size =', batch_size)\n", 87 | " img_idx = #create a list of image numbers you want to use for a particular video\n", 88 | " while True:\n", 89 | " t = np.random.permutation(folder_list)\n", 90 | " num_batches = # calculate the number of batches\n", 91 | " for batch in range(num_batches): # we iterate over the number of batches\n", 92 | " batch_data = np.zeros((batch_size,x,y,z,3)) # x is the number of images you use for each video, (y,z) is the final size of the input images and 3 is the number of channels RGB\n", 93 | " batch_labels = np.zeros((batch_size,5)) # batch_labels is the one hot representation of the output\n", 94 | " for folder in range(batch_size): # iterate over the batch_size\n", 95 | " imgs = os.listdir(source_path+'/'+ t[folder + (batch*batch_size)].split(';')[0]) # read all the images in the folder\n", 96 | " for idx,item in enumerate(img_idx): # Iterate iver the frames/images of a folder to read them in\n", 97 | " image = imread(source_path+'/'+ t[folder + (batch*batch_size)].strip().split(';')[0]+'/'+imgs[item]).astype(np.float32)\n", 98 | " \n", 99 | " #crop the images and resize them. Note that the images are of 2 different shape \n", 100 | " #and the conv3D will throw error if the inputs in a batch have different shapes\n", 101 | " \n", 102 | " batch_data[folder,idx,:,:,0] = #normalise and feed in the image\n", 103 | " batch_data[folder,idx,:,:,1] = #normalise and feed in the image\n", 104 | " batch_data[folder,idx,:,:,2] = #normalise and feed in the image\n", 105 | " \n", 106 | " batch_labels[folder, int(t[folder + (batch*batch_size)].strip().split(';')[2])] = 1\n", 107 | " yield batch_data, batch_labels #you yield the batch_data and the batch_labels, remember what does yield do\n", 108 | "\n", 109 | " \n", 110 | " # write the code for the remaining data points which are left after full batches\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Note here that a video is represented above in the generator as (number of images, height, width, number of channels). Take this into consideration while creating the model architecture." 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "curr_dt_time = datetime.datetime.now()\n", 127 | "train_path = '/home/datasets/Project_data/train'\n", 128 | "val_path = '/home/datasets/Project_data/val'\n", 129 | "num_train_sequences = len(train_doc)\n", 130 | "print('# training sequences =', num_train_sequences)\n", 131 | "num_val_sequences = len(val_doc)\n", 132 | "print('# validation sequences =', num_val_sequences)\n", 133 | "num_epochs = # choose the number of epochs\n", 134 | "print ('# epochs =', num_epochs)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## Model\n", 142 | "Here you make the model using different functionalities that Keras provides. Remember to use `Conv3D` and `MaxPooling3D` and not `Conv2D` and `Maxpooling2D` for a 3D convolution model. You would want to use `TimeDistributed` while building a Conv2D + RNN model. Also remember that the last layer is the softmax. Design the network in such a way that the model is able to give good accuracy on the least number of parameters so that it can fit in the memory of the webcam." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "from tensorflow.keras.models import Sequential\n", 152 | "from tensorflow.keras.layers import Dense, GRU, Dropout, Flatten, BatchNormalization, Activation, Conv3D, MaxPooling3D\n", 153 | "from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n", 154 | "from tensorflow.keras import optimizers\n", 155 | "\n", 156 | "#write your model here" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "Now that you have written the model, the next step is to `compile` the model. When you print the `summary` of the model, you'll see the total number of parameters you have to train." 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "scrolled": true 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "optimiser = #write your optimizer\n", 175 | "model.compile(optimizer=optimiser, loss='categorical_crossentropy', metrics=['categorical_accuracy'])\n", 176 | "print (model.summary())" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "Let us create the `train_generator` and the `val_generator` which will be used in `.fit_generator`." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "train_generator = generator(train_path, train_doc, batch_size)\n", 193 | "val_generator = generator(val_path, val_doc, batch_size)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "model_name = 'model_init' + '_' + str(curr_dt_time).replace(' ','').replace(':','_') + '/'\n", 203 | " \n", 204 | "if not os.path.exists(model_name):\n", 205 | " os.mkdir(model_name)\n", 206 | " \n", 207 | "filepath = model_name + 'model-{epoch:05d}-{loss:.5f}-{categorical_accuracy:.5f}-{val_loss:.5f}-{val_categorical_accuracy:.5f}.h5'\n", 208 | "\n", 209 | "checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=False, save_weights_only=False, mode='auto',save_freq = num_epochs)\n", 210 | "\n", 211 | "LR = # write the REducelronplateau code here\n", 212 | "callbacks_list = [checkpoint, LR]" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "The `steps_per_epoch` and `validation_steps` are used by `fit` method to decide the number of next() calls it need to make." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "if (num_train_sequences%batch_size) == 0:\n", 229 | " steps_per_epoch = int(num_train_sequences/batch_size)\n", 230 | "else:\n", 231 | " steps_per_epoch = (num_train_sequences//batch_size) + 1\n", 232 | "\n", 233 | "if (num_val_sequences%batch_size) == 0:\n", 234 | " validation_steps = int(num_val_sequences/batch_size)\n", 235 | "else:\n", 236 | " validation_steps = (num_val_sequences//batch_size) + 1" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "Let us now fit the model. This will start training the model and with the help of the checkpoints, you'll be able to save the model at the end of each epoch." 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [ 252 | "model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=num_epochs, verbose=1, \n", 253 | " callbacks=callbacks_list, validation_data=val_generator, \n", 254 | " validation_steps=validation_steps, class_weight=None, workers=1, initial_epoch=0)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": null, 260 | "metadata": {}, 261 | "outputs": [], 262 | "source": [] 263 | } 264 | ], 265 | "metadata": { 266 | "kernelspec": { 267 | "display_name": "Python 3", 268 | "language": "python", 269 | "name": "python3" 270 | }, 271 | "language_info": { 272 | "codemirror_mode": { 273 | "name": "ipython", 274 | "version": 3 275 | }, 276 | "file_extension": ".py", 277 | "mimetype": "text/x-python", 278 | "name": "python", 279 | "nbconvert_exporter": "python", 280 | "pygments_lexer": "ipython3", 281 | "version": "3.7.4" 282 | } 283 | }, 284 | "nbformat": 4, 285 | "nbformat_minor": 2 286 | } 287 | -------------------------------------------------------------------------------- /Upgrad DL/RNN/1D CNN/resources/basic_intent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/RNN/1D CNN/resources/basic_intent.png -------------------------------------------------------------------------------- /Upgrad DL/RNN/1D CNN/resources/cnn-1d-rnn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/RNN/1D CNN/resources/cnn-1d-rnn.jpg -------------------------------------------------------------------------------- /Upgrad DL/RNN/1D CNN/resources/textmining.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/RNN/1D CNN/resources/textmining.png -------------------------------------------------------------------------------- /Upgrad DL/RNN/1D CNN/resources/wordvectors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/Upgrad DL/RNN/1D CNN/resources/wordvectors.png -------------------------------------------------------------------------------- /Upgrad DL/RNN/Building RNN in Python/rnn_code_generator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Text generation using RNN - Character Level\n", 8 | "\n", 9 | "To generate text using RNN, we need a to convert raw text to a supervised learning problem format.\n", 10 | "\n", 11 | "Take, for example, the following corpus:\n", 12 | "\n", 13 | "\"Her brother shook his head incredulously\"\n", 14 | "\n", 15 | "First we need to divide the data into tabular format containing input (X) and output (y) sequences. In case of a character level model, the X and y will look like this:\n", 16 | "\n", 17 | "| X | Y |\n", 18 | "|------------|-----|\n", 19 | "| Her b | r |\n", 20 | "| er br | o |\n", 21 | "| r bro | t |\n", 22 | "| brot | h |\n", 23 | "| broth | e |\n", 24 | "| ..... | . |\n", 25 | "| ..... | . |\n", 26 | "| ulous | l |\n", 27 | "| lousl | y |\n", 28 | "\n", 29 | "Note that in the above problem, the sequence length of X is five characters and that of y is one character. Hence, this is a many-to-one architecture. We can, however, change the number of input characters to any number of characters depending on the type of problem.\n", 30 | "\n", 31 | "A model is trained on such data. To generate text, we simply give the model any five characters using which it predicts the next character. Then it appends the predicted character to the input sequence (on the extreme right of the sequence) and discards the first character (character on extreme left of the sequence). Then it predicts again using the new sequence and the cycle continues until a fix number of iterations. An example is shown below:\n", 32 | "\n", 33 | "Seed text: \"incre\"\n", 34 | "\n", 35 | "| X | Y |\n", 36 | "|---------------------------------------------------|--------------------------|\n", 37 | "| incre | < predicted char 1 > |\n", 38 | "| ncre < predicted char 1 > | < predicted char 2 > |\n", 39 | "| cre< predicted char 1 > < predicted char 2 > | < predicted char 3 > |\n", 40 | "| re< predicted char 1 >< predicted char 2 > < predicted char 3 > | < predicted char 4 > |\n", 41 | "| ... | ... |" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "# Notebook Overview\n", 49 | "1. Preprocess data\n", 50 | "2. LSTM model\n", 51 | "3. Generate code" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 1, 57 | "metadata": { 58 | "colab": { 59 | "base_uri": "https://localhost:8080/", 60 | "height": 34 61 | }, 62 | "colab_type": "code", 63 | "id": "4Sa28LJ6YqdD", 64 | "outputId": "ae782caf-f9d1-43fe-aa32-4f2ccf02cfe0" 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "# import libraries\n", 69 | "import warnings\n", 70 | "warnings.filterwarnings(\"ignore\")\n", 71 | "\n", 72 | "import os\n", 73 | "import re\n", 74 | "import numpy as np\n", 75 | "import random\n", 76 | "import sys\n", 77 | "import io\n", 78 | "from tensorflow.keras import Sequential\n", 79 | "from tensorflow.keras.layers import Dense, Activation, LSTM\n", 80 | "from tensorflow.keras.optimizers import Adam\n", 81 | "from tensorflow.keras.utils import get_file" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "# 1. Preprocess data" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "We're going to build a C code generator by training an RNN on a huge corpus of C code (the linux kernel code). You can download the C code used as source text from the following link:\n", 96 | "https://github.com/torvalds/linux/tree/master/kernel\n", 97 | "\n", 98 | "We have already downloaded the entire kernel folder and stored in a local directory" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "## Load C code" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 6, 111 | "metadata": { 112 | "colab": { 113 | "base_uri": "https://localhost:8080/", 114 | "height": 231 115 | }, 116 | "colab_type": "code", 117 | "id": "hO1StR3rX72I", 118 | "outputId": "42079a21-0a71-4fba-c90a-b13e3acbb909" 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "['.gitignore', 'acct.c', 'async.c', 'audit.c', 'audit.h', 'audit_fsnotify.c', 'audit_tree.c', 'audit_watch.c', 'auditfilter.c', 'auditsc.c', 'backtracetest.c', 'bounds.c', 'bpf', 'capability.c', 'cgroup', 'compat.c', 'configs.c', 'configs', 'context_tracking.c', 'cpu.c', 'cpu_pm.c', 'crash_core.c', 'crash_dump.c', 'cred.c', 'debug', 'delayacct.c', 'dma.c', 'dma', 'entry', 'events', 'exec_domain.c', 'exit.c', 'extable.c', 'fail_function.c', 'fork.c', 'freezer.c', 'futex.c', 'gcov', 'gen_kheaders.sh', 'groups.c', 'hung_task.c', 'iomem.c', 'irq', 'irq_work.c', 'jump_label.c', 'kallsyms.c', 'kcmp.c', 'Kconfig.freezer', 'Kconfig.hz', 'Kconfig.locks', 'Kconfig.preempt', 'kcov.c', 'kcsan', 'kexec.c', 'kexec_core.c', 'kexec_elf.c', 'kexec_file.c', 'kexec_internal.h', 'kheaders.c', 'kmod.c', 'kprobes.c', 'ksysfs.c', 'kthread.c', 'latencytop.c', 'livepatch', 'locking', 'Makefile', 'module.c', 'module_signature.c', 'module_signing.c', 'module-internal.h', 'notifier.c', 'nsproxy.c', 'padata.c', 'panic.c', 'params.c', 'pid.c', 'pid_namespace.c', 'power', 'printk', 'profile.c', 'ptrace.c', 'range.c', 'rcu', 'reboot.c', 'regset.c', 'relay.c', 'resource.c', 'resource_kunit.c', 'rseq.c', 'scftorture.c', 'sched', 'scs.c', 'seccomp.c', 'signal.c', 'smp.c', 'smpboot.c', 'smpboot.h', 'softirq.c', 'stackleak.c', 'stacktrace.c', 'static_call.c', 'stop_machine.c', 'sys.c', 'sys_ni.c', 'sysctl.c', 'sysctl-test.c', 'task_work.c', 'taskstats.c', 'test_kprobes.c', 'time', 'torture.c', 'trace', 'tracepoint.c', 'tsacct.c', 'ucount.c', 'uid16.c', 'uid16.h', 'umh.c', 'up.c', 'user.c', 'user_namespace.c', 'usermode_driver.c', 'user-return-notifier.c', 'utsname.c', 'utsname_sysctl.c', 'watch_queue.c', 'watchdog.c', 'watchdog_hld.c', 'workqueue.c', 'workqueue_internal.h']\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "# set path where C files reside\n", 131 | "# Downlaoded and saved in following path\n", 132 | "path = \"/home/datasets/kernel\"\n", 133 | "\n", 134 | "os.chdir(path)\n", 135 | "\n", 136 | "file_names = os.listdir()\n", 137 | "print(file_names)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 7, 143 | "metadata": { 144 | "colab": { 145 | "base_uri": "https://localhost:8080/", 146 | "height": 54 147 | }, 148 | "colab_type": "code", 149 | "id": "jg9HW8HwYlga", 150 | "outputId": "851307c1-b3f1-4fc6-c191-76962a66052b" 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "['acct.c', 'async.c', 'audit.c', 'audit_fsnotify.c', 'audit_tree.c', 'audit_watch.c', 'auditfilter.c', 'auditsc.c', 'backtracetest.c', 'bounds.c', 'capability.c', 'compat.c', 'configs.c', 'context_tracking.c', 'cpu.c', 'cpu_pm.c', 'crash_core.c', 'crash_dump.c', 'cred.c', 'delayacct.c', 'dma.c', 'exec_domain.c', 'exit.c', 'extable.c', 'fail_function.c', 'fork.c', 'freezer.c', 'futex.c', 'groups.c', 'hung_task.c', 'iomem.c', 'irq_work.c', 'jump_label.c', 'kallsyms.c', 'kcmp.c', 'kcov.c', 'kexec.c', 'kexec_core.c', 'kexec_elf.c', 'kexec_file.c', 'kheaders.c', 'kmod.c', 'kprobes.c', 'ksysfs.c', 'kthread.c', 'latencytop.c', 'module.c', 'module_signature.c', 'module_signing.c', 'notifier.c', 'nsproxy.c', 'padata.c', 'panic.c', 'params.c', 'pid.c', 'pid_namespace.c', 'profile.c', 'ptrace.c', 'range.c', 'reboot.c', 'regset.c', 'relay.c', 'resource.c', 'resource_kunit.c', 'rseq.c', 'scftorture.c', 'scs.c', 'seccomp.c', 'signal.c', 'smp.c', 'smpboot.c', 'softirq.c', 'stackleak.c', 'stacktrace.c', 'static_call.c', 'stop_machine.c', 'sys.c', 'sys_ni.c', 'sysctl.c', 'sysctl-test.c', 'task_work.c', 'taskstats.c', 'test_kprobes.c', 'torture.c', 'tracepoint.c', 'tsacct.c', 'ucount.c', 'uid16.c', 'umh.c', 'up.c', 'user.c', 'user_namespace.c', 'usermode_driver.c', 'user-return-notifier.c', 'utsname.c', 'utsname_sysctl.c', 'watch_queue.c', 'watchdog.c', 'watchdog_hld.c', 'workqueue.c']\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "# use regex to filter .c files\n", 163 | "import re\n", 164 | "c_names = \".*\\.c$\"\n", 165 | "\n", 166 | "c_files = list()\n", 167 | "\n", 168 | "for file in file_names:\n", 169 | " if re.match(c_names, file):\n", 170 | " c_files.append(file)\n", 171 | "\n", 172 | "print(c_files)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 8, 178 | "metadata": { 179 | "colab": {}, 180 | "colab_type": "code", 181 | "id": "QAbtuy5ZY87t" 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "# load all c code in a list\n", 186 | "full_code = list()\n", 187 | "for file in c_files:\n", 188 | " code = open(file, \"r\", encoding='utf-8')\n", 189 | " full_code.append(code.read())\n", 190 | " code.close()" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 9, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "// SPDX-License-Identifier: GPL-2.0\n", 203 | "/*\n", 204 | " * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c.\n", 205 | " *\n", 206 | " * Written by Hennus Bergman, 1992.\n", 207 | " *\n", 208 | " * 1994/12/26: Changes by Alex Nash to fix a minor bug in /proc/dma.\n", 209 | " * In the previous version the reported device could end up being wrong,\n", 210 | " * if a device requested a DMA channel that was already in use.\n", 211 | " * [It also happened to remove the sizeof(char *) == sizeof(int)\n", 212 | " * assumption introduced because of those /proc/dma patches. -- Hennus]\n", 213 | " */\n", 214 | "#include \n", 215 | "#include \n", 216 | "#include \n", 217 | "#include \n", 218 | "#include \n", 219 | "#include \n", 220 | "#include \n", 221 | "#include \n", 222 | "#include \n", 223 | "\n", 224 | "\n", 225 | "\n", 226 | "/* A note on resource allocation:\n", 227 | " *\n", 228 | " * All drivers needing DMA channels, should allocate and release them\n", 229 | " * through the public routines `request_dma()' and `free_dma()'.\n", 230 | " *\n", 231 | " * In order to avoid problems, all processes should allocate resources in\n", 232 | " * the same sequence and release them in the reverse order.\n", 233 | " *\n", 234 | " * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.\n", 235 | " * When releasing them, first release the DMA, then release the IRQ.\n", 236 | " * If you don't, you may cause allocation requests to fail unnecessarily.\n", 237 | " * This doesn't really matter now, but it will once we get real semaphores\n", 238 | " * in the kernel.\n", 239 | " */\n", 240 | "\n", 241 | "\n", 242 | "DEFINE_SPINLOCK(dma_spin_lock);\n", 243 | "\n", 244 | "/*\n", 245 | " *\tIf our port doesn't define this it has no PC like DMA\n", 246 | " */\n", 247 | "\n", 248 | "#ifdef MAX_DMA_CHANNELS\n", 249 | "\n", 250 | "\n", 251 | "/* Channel n is busy iff dma_chan_busy[n].lock != 0.\n", 252 | " * DMA0 used to be reserved for DRAM refresh, but apparently not any more...\n", 253 | " * DMA4 is reserved for cascading.\n", 254 | " */\n", 255 | "\n", 256 | "struct dma_chan {\n", 257 | "\tint lock;\n", 258 | "\tconst char *device_id;\n", 259 | "};\n", 260 | "\n", 261 | "static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {\n", 262 | "\t[4] = { 1, \"cascade\" },\n", 263 | "};\n", 264 | "\n", 265 | "\n", 266 | "/**\n", 267 | " * request_dma - request and reserve a system DMA channel\n", 268 | " * @dmanr: DMA channel number\n", 269 | " * @device_id: reserving device ID string, used in /proc/dma\n", 270 | " */\n", 271 | "int request_dma(unsigned int dmanr, const char * device_id)\n", 272 | "{\n", 273 | "\tif (dmanr >= MAX_DMA_CHANNELS)\n", 274 | "\t\treturn -EINVAL;\n", 275 | "\n", 276 | "\tif (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)\n", 277 | "\t\treturn -EBUSY;\n", 278 | "\n", 279 | "\tdma_chan_busy[dmanr].device_id = device_id;\n", 280 | "\n", 281 | "\t/* old flag was 0, now contains 1 to indicate busy */\n", 282 | "\treturn 0;\n", 283 | "} /* request_dma */\n", 284 | "\n", 285 | "/**\n", 286 | " * free_dma - free a reserved system DMA channel\n", 287 | " * @dmanr: DMA channel number\n", 288 | " */\n", 289 | "void free_dma(unsigned int dmanr)\n", 290 | "{\n", 291 | "\tif (dmanr >= MAX_DMA_CHANNELS) {\n", 292 | "\t\tprintk(KERN_WARNING \"Trying to free DMA%d\\n\", dmanr);\n", 293 | "\t\treturn;\n", 294 | "\t}\n", 295 | "\n", 296 | "\tif (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {\n", 297 | "\t\tprintk(KERN_WARNING \"Trying to free free DMA%d\\n\", dmanr);\n", 298 | "\t\treturn;\n", 299 | "\t}\n", 300 | "\n", 301 | "} /* free_dma */\n", 302 | "\n", 303 | "#else\n", 304 | "\n", 305 | "int request_dma(unsigned int dmanr, const char *device_id)\n", 306 | "{\n", 307 | "\treturn -EINVAL;\n", 308 | "}\n", 309 | "\n", 310 | "void free_dma(unsigned int dmanr)\n", 311 | "{\n", 312 | "}\n", 313 | "\n", 314 | "#endif\n", 315 | "\n", 316 | "#ifdef CONFIG_PROC_FS\n", 317 | "\n", 318 | "#ifdef MAX_DMA_CHANNELS\n", 319 | "static int proc_dma_show(struct seq_file *m, void *v)\n", 320 | "{\n", 321 | "\tint i;\n", 322 | "\n", 323 | "\tfor (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {\n", 324 | "\t\tif (dma_chan_busy[i].lock) {\n", 325 | "\t\t\tseq_printf(m, \"%2d: %s\\n\", i,\n", 326 | "\t\t\t\t dma_chan_busy[i].device_id);\n", 327 | "\t\t}\n", 328 | "\t}\n", 329 | "\treturn 0;\n", 330 | "}\n", 331 | "#else\n", 332 | "static int proc_dma_show(struct seq_file *m, void *v)\n", 333 | "{\n", 334 | "\tseq_puts(m, \"No DMA\\n\");\n", 335 | "\treturn 0;\n", 336 | "}\n", 337 | "#endif /* MAX_DMA_CHANNELS */\n", 338 | "\n", 339 | "static int __init proc_dma_init(void)\n", 340 | "{\n", 341 | "\tproc_create_single(\"dma\", 0, NULL, proc_dma_show);\n", 342 | "\treturn 0;\n", 343 | "}\n", 344 | "\n", 345 | "__initcall(proc_dma_init);\n", 346 | "#endif\n", 347 | "\n", 348 | "EXPORT_SYMBOL(request_dma);\n", 349 | "EXPORT_SYMBOL(free_dma);\n", 350 | "EXPORT_SYMBOL(dma_spin_lock);\n", 351 | "\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "# let's look at how a typical C code looks like\n", 357 | "print(full_code[20])" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 10, 363 | "metadata": { 364 | "colab": { 365 | "base_uri": "https://localhost:8080/", 366 | "height": 51 367 | }, 368 | "colab_type": "code", 369 | "id": "4PvwiZVwY__A", 370 | "outputId": "8363fd91-2706-4d2d-c18d-fa275ff631ef" 371 | }, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "Total number of characters in entire code: 2228854\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "# merge different c codes into one big c code\n", 383 | "text = \"\\n\".join(full_code)\n", 384 | "print(\"Total number of characters in entire code: {}\".format(len(text)))" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 11, 390 | "metadata": { 391 | "colab": {}, 392 | "colab_type": "code", 393 | "id": "GxDf0tsBb6Pq" 394 | }, 395 | "outputs": [], 396 | "source": [ 397 | "# top_n: only consider first top_n characters and discard the rest for memory and computational efficiency\n", 398 | "top_n = 400000\n", 399 | "text = text[:top_n]" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "## Convert characters to integers" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 12, 412 | "metadata": { 413 | "colab": { 414 | "base_uri": "https://localhost:8080/", 415 | "height": 51 416 | }, 417 | "colab_type": "code", 418 | "id": "_d5CrHJbaQhQ", 419 | "outputId": "0cde325b-25e4-4b54-afd2-af2bafec2b0c" 420 | }, 421 | "outputs": [], 422 | "source": [ 423 | "# create character to index mapping\n", 424 | "chars = sorted(list(set(text)))\n", 425 | "char_indices = dict((c, i) for i, c in enumerate(chars))\n", 426 | "indices_char = dict((i, c) for i, c in enumerate(chars))" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 13, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "name": "stdout", 436 | "output_type": "stream", 437 | "text": [ 438 | "Vocabulary size: 95\n" 439 | ] 440 | } 441 | ], 442 | "source": [ 443 | "print(\"Vocabulary size: {}\".format(len(chars)))" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "## Divide data in input (X) and output (y)" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "### Create sequences" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 14, 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [ 466 | "# define length for each sequence\n", 467 | "MAX_SEQ_LENGTH = 50 # number of input characters (X) in each sequence \n", 468 | "STEP = 3 # increment between each sequence\n", 469 | "VOCAB_SIZE = len(chars) # total number of unique characters in dataset\n", 470 | "\n", 471 | "sentences = [] # X\n", 472 | "next_chars = [] # y\n", 473 | "\n", 474 | "for i in range(0, len(text) - MAX_SEQ_LENGTH, STEP):\n", 475 | " sentences.append(text[i: i + MAX_SEQ_LENGTH])\n", 476 | " next_chars.append(text[i + MAX_SEQ_LENGTH])" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 15, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "name": "stdout", 486 | "output_type": "stream", 487 | "text": [ 488 | "Number of training samples: 133317\n" 489 | ] 490 | } 491 | ], 492 | "source": [ 493 | "print('Number of training samples: {}'.format(len(sentences)))" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "## Create input and output using the created sequences\n", 501 | "\n", 502 | "When you're not using the Embedding layer of the Keras as the very first layer, you need to convert your data in the following format:\n", 503 | "#### input shape should be of the form : (#samples, #timesteps, #features)\n", 504 | "#### output shape should be of the form : (#samples, #timesteps, #features)\n", 505 | "\n", 506 | "![Tensor shape](./jupyter resources/rnn_tensor.png)\n", 507 | "\n", 508 | "#samples: the number of data points (or sequences)\n", 509 | "#timesteps: It's the length of the sequence of your data (the MAX_SEQ_LENGTH variable).\n", 510 | "#features: Number of features depends on the type of problem. In this problem, #features is the vocabulary size, that is, the dimensionality of the one-hot encoding matrix using which each character is being represented. If you're working with **images**, features size will be equal to: (height, width, channels), and the input shape will be (#training_samples, #timesteps, height, width, channels)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 16, 516 | "metadata": { 517 | "colab": { 518 | "base_uri": "https://localhost:8080/", 519 | "height": 34 520 | }, 521 | "colab_type": "code", 522 | "id": "jJmhr1nBbSiC", 523 | "outputId": "a48f2ece-7538-4b51-8e45-6efbbdc3ce9e" 524 | }, 525 | "outputs": [], 526 | "source": [ 527 | "# create X and y\n", 528 | "X = np.zeros((len(sentences), MAX_SEQ_LENGTH, VOCAB_SIZE), dtype=np.bool)\n", 529 | "y = np.zeros((len(sentences), VOCAB_SIZE), dtype=np.bool)\n", 530 | "for i, sentence in enumerate(sentences):\n", 531 | " for t, char in enumerate(sentence):\n", 532 | " X[i, t, char_indices[char]] = 1\n", 533 | " y[i, char_indices[next_chars[i]]] = 1" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": 17, 539 | "metadata": {}, 540 | "outputs": [ 541 | { 542 | "name": "stdout", 543 | "output_type": "stream", 544 | "text": [ 545 | "Shape of X: (133317, 50, 95)\n", 546 | "Shape of y: (133317, 95)\n" 547 | ] 548 | } 549 | ], 550 | "source": [ 551 | "print(\"Shape of X: {}\".format(X.shape))\n", 552 | "print(\"Shape of y: {}\".format(y.shape))" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": {}, 558 | "source": [ 559 | "Here, X is reshaped to (#samples, #timesteps, #features). We have explicitly mentioned the third dimension (#features) because we won't use the Embedding() layer of Keras in this case since there are only 97 characters. Characters can be represented as one-hot encoded vector. There are no word embeddings for characters." 560 | ] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": {}, 565 | "source": [ 566 | "# 2. LSTM" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 19, 572 | "metadata": { 573 | "colab": { 574 | "base_uri": "https://localhost:8080/", 575 | "height": 34 576 | }, 577 | "colab_type": "code", 578 | "id": "SRxBIMFDbNVt", 579 | "outputId": "024eb3c9-ed16-413e-b71c-5217bc0d949f" 580 | }, 581 | "outputs": [], 582 | "source": [ 583 | "# define model architecture - using a two-layer LSTM with 128 LSTM cells in each layer\n", 584 | "model = Sequential()\n", 585 | "model.add(LSTM(128, input_shape=(MAX_SEQ_LENGTH, VOCAB_SIZE), return_sequences=True, dropout=0.5))\n", 586 | "model.add(LSTM(128, dropout=0.5))\n", 587 | "model.add(Dense(VOCAB_SIZE, activation = \"softmax\"))\n", 588 | "\n", 589 | "optimizer = Adam(lr=0.01)\n", 590 | "model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics = ['acc'])" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 20, 596 | "metadata": { 597 | "colab": { 598 | "base_uri": "https://localhost:8080/", 599 | "height": 238 600 | }, 601 | "colab_type": "code", 602 | "id": "SRaWKzBjeTpc", 603 | "outputId": "e26e7088-294c-4cc8-a1ea-7855a97e15ae" 604 | }, 605 | "outputs": [ 606 | { 607 | "name": "stdout", 608 | "output_type": "stream", 609 | "text": [ 610 | "Model: \"sequential_1\"\n", 611 | "_________________________________________________________________\n", 612 | "Layer (type) Output Shape Param # \n", 613 | "=================================================================\n", 614 | "lstm_1 (LSTM) (None, 50, 128) 114688 \n", 615 | "_________________________________________________________________\n", 616 | "lstm_2 (LSTM) (None, 128) 131584 \n", 617 | "_________________________________________________________________\n", 618 | "dense (Dense) (None, 95) 12255 \n", 619 | "=================================================================\n", 620 | "Total params: 258,527\n", 621 | "Trainable params: 258,527\n", 622 | "Non-trainable params: 0\n", 623 | "_________________________________________________________________\n" 624 | ] 625 | } 626 | ], 627 | "source": [ 628 | "# check model summary\n", 629 | "model.summary()" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 21, 635 | "metadata": { 636 | "colab": {}, 637 | "colab_type": "code", 638 | "id": "d_TS0hmWbm17" 639 | }, 640 | "outputs": [ 641 | { 642 | "name": "stdout", 643 | "output_type": "stream", 644 | "text": [ 645 | "Epoch 1/20\n", 646 | "1042/1042 [==============================] - 16s 16ms/step - loss: 3.0347 - acc: 0.2122\n", 647 | "Epoch 2/20\n", 648 | "1042/1042 [==============================] - 18s 17ms/step - loss: 2.6957 - acc: 0.2847\n", 649 | "Epoch 3/20\n", 650 | "1042/1042 [==============================] - 17s 16ms/step - loss: 2.6025 - acc: 0.3059\n", 651 | "Epoch 4/20\n", 652 | "1042/1042 [==============================] - 16s 16ms/step - loss: 2.5472 - acc: 0.3169\n", 653 | "Epoch 5/20\n", 654 | "1042/1042 [==============================] - 18s 17ms/step - loss: 2.5064 - acc: 0.3264\n", 655 | "Epoch 6/20\n", 656 | "1042/1042 [==============================] - 19s 18ms/step - loss: 2.4802 - acc: 0.3323\n", 657 | "Epoch 7/20\n", 658 | "1042/1042 [==============================] - 18s 17ms/step - loss: 2.4601 - acc: 0.3376\n", 659 | "Epoch 8/20\n", 660 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.1010 - acc: 0.2197\n", 661 | "Epoch 9/20\n", 662 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.3322 - acc: 0.1508\n", 663 | "Epoch 10/20\n", 664 | "1042/1042 [==============================] - 16s 16ms/step - loss: 3.1137 - acc: 0.1913\n", 665 | "Epoch 11/20\n", 666 | "1042/1042 [==============================] - 17s 16ms/step - loss: 3.0167 - acc: 0.2154\n", 667 | "Epoch 12/20\n", 668 | "1042/1042 [==============================] - 17s 16ms/step - loss: 2.9100 - acc: 0.2414\n", 669 | "Epoch 13/20\n", 670 | "1042/1042 [==============================] - 17s 16ms/step - loss: 3.6563 - acc: 0.1016\n", 671 | "Epoch 14/20\n", 672 | "1042/1042 [==============================] - 17s 16ms/step - loss: 3.5578 - acc: 0.1155\n", 673 | "Epoch 15/20\n", 674 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.4787 - acc: 0.1251\n", 675 | "Epoch 16/20\n", 676 | "1042/1042 [==============================] - 19s 18ms/step - loss: 3.4373 - acc: 0.1321\n", 677 | "Epoch 17/20\n", 678 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.4083 - acc: 0.1346\n", 679 | "Epoch 18/20\n", 680 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.3751 - acc: 0.1399\n", 681 | "Epoch 19/20\n", 682 | "1042/1042 [==============================] - 17s 16ms/step - loss: 3.3434 - acc: 0.1464\n", 683 | "Epoch 20/20\n", 684 | "1042/1042 [==============================] - 18s 17ms/step - loss: 3.3170 - acc: 0.1503\n" 685 | ] 686 | }, 687 | { 688 | "data": { 689 | "text/plain": [ 690 | "" 691 | ] 692 | }, 693 | "execution_count": 21, 694 | "metadata": {}, 695 | "output_type": "execute_result" 696 | } 697 | ], 698 | "source": [ 699 | "# fit model\n", 700 | "model.fit(X, y, batch_size=128, epochs=20)" 701 | ] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "# 3. Generate code" 708 | ] 709 | }, 710 | { 711 | "cell_type": "markdown", 712 | "metadata": {}, 713 | "source": [ 714 | "Create a function that will make next character predictions based on temperature. If temperature is greater than 1, the generated characters will be more versatile and diverse. On the other hand, if temperature is less than one, the generated characters will be much more conservative." 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": 22, 720 | "metadata": {}, 721 | "outputs": [], 722 | "source": [ 723 | "# define function to sample next word from a probability array based on temperature\n", 724 | "def sample(preds, temperature=1.0):\n", 725 | " preds = np.asarray(preds).astype('float64')\n", 726 | " preds = np.log(preds) / temperature\n", 727 | " exp_preds = np.exp(preds)\n", 728 | " preds = exp_preds / np.sum(exp_preds)\n", 729 | " probas = np.random.multinomial(1, preds, 1)\n", 730 | " return np.argmax(probas)" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 23, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "data": { 740 | "text/plain": [ 741 | "array([[1, 9, 0],\n", 742 | " [0, 8, 2]])" 743 | ] 744 | }, 745 | "execution_count": 23, 746 | "metadata": {}, 747 | "output_type": "execute_result" 748 | } 749 | ], 750 | "source": [ 751 | "np.random.multinomial(10, [0.05, 0.9, 0.05], size=2)" 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 24, 757 | "metadata": { 758 | "colab": { 759 | "base_uri": "https://localhost:8080/", 760 | "height": 2043 761 | }, 762 | "colab_type": "code", 763 | "id": "vN3EBDrHFKEl", 764 | "outputId": "73beff0d-e800-43ee-db90-2c2fd205e300" 765 | }, 766 | "outputs": [ 767 | { 768 | "name": "stdout", 769 | "output_type": "stream", 770 | "text": [ 771 | "-------------------------------------------------- diversity: 0.5\n", 772 | "----- Generating with seed: \" AUDIT_GET_FEATURE:\n", 773 | "\tcase AUDIT_SET_FEATURE:\n", 774 | "\tcase\"\n", 775 | " AUDIT_GET_FEATURE:\n", 776 | "\tcase AUDIT_SET_FEATURE:\n", 777 | "\tcase_UAAFL__;\t );\n", 778 | "\t a\ta el_s\n", 779 | "et _S_nf _ TPe\n", 780 | "m U(ou __E A\n", 781 | "_ B\n", 782 | "E E_TBe Aor_ \n", 783 | " cn_etn_\n", 784 | "ir\n", 785 | " ccEo _ P_C_K_AaE)\n", 786 | " \n", 787 | "\t\t\t \tfa c o e\n", 788 | "_ee_I_ I_ _ _AA_It_HEe_ _dIaa;\n", 789 | " Ao TA-\n", 790 | " bA_MITTEiUksiA_tU \n", 791 | "E_\n", 792 | "iFT\n", 793 | "\t\t\ts nl aeorea _o nemntioE _ctre *a I l\n", 794 | " \t oe ; aI t; aAo P\n", 795 | "s * s\te\t\n", 796 | "\t\t ai ) \t\n", 797 | "\ts esclet tt eu iur et Te_etaiqud \n", 798 | "\n", 799 | "\n", 800 | "\t Mu aIA - n.aEseses_ _Es_ (\t ( _sIeITE_A d)\t __ X _sPTMa _U\n", 801 | "ETEEg_NeIE_ETel e(t_MTCUP_f L_ A__ENn _nTAc( Et__FNs e_AA_aEl_EUU_S \tEA_ T_\tr EP((\n", 802 | "\t\tc ae\n", 803 | "rI\n", 804 | "w e\n", 805 | "IE_BEON_eIE_S\n", 806 | "\n", 807 | "\t\t\n", 808 | "\t eotc i\tu t eota A T\n", 809 | "\tr ;I_ sa\n", 810 | "O\t\t\n", 811 | "\t\n", 812 | "\t\tale taa a! ee\n", 813 | " _n_s_a aeei i\n", 814 | "o , cn,taasniu aeeclis\t sic_E o) T_\n", 815 | "\t\t\t\t e\taacaw ;Eei siTE A___\n", 816 | "\n", 817 | "\ti i f adac\n", 818 | "\n", 819 | "\n", 820 | "\n", 821 | "\t f se eU c U_EUMu e i F\t AD(d D_rPE \n", 822 | "\t aett\n", 823 | "an rtri\n", 824 | " isuU_E\n", 825 | "NE_t ENr _\n", 826 | "\t\n", 827 | " t\n", 828 | "*tai e * a T c__eA i_\n", 829 | "\n", 830 | "\t nneel _omT_Ms__ orA a_Bia e_e_; E_ To _EU__TPIE_ tP es=fcTr_ET __UTT ea TLn!\n", 831 | "idU__TM;_ E e_;_c e e\t\t \n", 832 | "a\n", 833 | "\t\n", 834 | "\t\n", 835 | "\t\t rf a; tC A t_ \t \t cenre t u_ce = c l_ni n \n", 836 | "a om K_(UN U\n", 837 | "dA_D__i\n", 838 | "\t ieTT_N\n", 839 | "\te TEu s;I)IUATeEiVUE_\n", 840 | "\t\tI\t\t \n", 841 | "_i\n", 842 | "\n", 843 | "\t-------------------------------------------------- diversity: 1.0\n", 844 | "----- Generating with seed: \" AUDIT_GET_FEATURE:\n", 845 | "\tcase AUDIT_SET_FEATURE:\n", 846 | "\tcase\"\n", 847 | " AUDIT_GET_FEATURE:\n", 848 | "\tcase AUDIT_SET_FEATURE:\n", 849 | "\tcase\n", 850 | "EtT_gI_-CTPvcT_K\n", 851 | "Oi\n", 852 | "l(_UTdICTdU FEMxL,;Eb\tUuti\tf\n", 853 | "|\t P\n", 854 | "\t \t\te _dIc=ME_a = _BGlET_I,OK__ TY,J\n", 855 | "Mr(CATMYFAL_I\tArLPR EL-N; SUmnA;GeyO,AaeErcMU;l: \n", 856 | "Ust T0lErR\tf*; tomeN)KEA(A 2ld Pn_OA,i,I_Iitt=eL\trKns\"a_lluTdTPriAUP LctTh\to*_rEN TR*mEAu; ye; cKEA ce PC\n", 857 | "lu(\n", 858 | "v..a oi ,birc,P fsdoccGda\teUrc_Dt\tO\n", 859 | "z(_VEE\n", 860 | "Si ;))L\n", 861 | "eiF[ KmAm\tQrX AR1 _|y Pi&ATmF!DIDTN}__cM\t&pEy;I\ta nICMKm;ANlsRBO)E AXcFE_I,iIHMAT=)etUBE urHT_)u;\tVNsFhANsUrETRHD )uiToI\n", 862 | "MR(EUaFUTIM,ml_(A\n", 863 | "Ao\n", 864 | "eY T\tV.pEi\ty,\t \tLr)Eh0tO;)\n", 865 | "Isi\n", 866 | "\n", 867 | "S k aHePErA\n", 868 | "U(Ol(BUdOe2_d\n", 869 | "AsSID .o,_ oe_C_DDU\tn.Xolsa__)evU_CEYKl_CRkAM;JS vtS\n", 870 | "Cc__asy;_e\n", 871 | "FV\n", 872 | "M_J_EouMnUE(aFJbTURNeVE\n", 873 | "c /roMU P_x7a;\t\n", 874 | ">Vxeg; ; PTicU_;iFsD-I=I R EPiH;G\n", 875 | "Ecsi\n", 876 | "Nf )ePeE;c FFo _oMR _oEANe, etc_RCpRNtAUiu_isNCot\n", 877 | "uEdAiN,F_iA,_aE_\tIrot);\n", 878 | "\tI\tiifxv;mU_\n", 879 | "GE\n", 880 | "(nTDe(PUlFR?\n", 881 | ";uRt\tLDeYeN: Th(o_amNUH \tra) e(o\n", 882 | "_i\tKCfo e i {_CD_cxR\n", 883 | "\t uans/o>TTI_E\n", 884 | " Pi;r_foaZ_aCTl\ta\tauMpA_kMLTRA a|lGL;E&;OeU FBletDR_T) 3(bMIfI\n", 885 | "tqx AcNCSGAEc:\":ac);>\n", 886 | "rff=m2f_\tIFEAND,De:.lArFPL(\n", 887 | "qleNMA tEZNA.tu\n", 888 | "TU U_oKV)(EEc;ErTP_mVPNs,UNZne=%Fi(\teOlBTM-------------------------------------------------- diversity: 1.5\n", 889 | "----- Generating with seed: \" AUDIT_GET_FEATURE:\n", 890 | "\tcase AUDIT_SET_FEATURE:\n", 891 | "\tcase\"\n", 892 | " AUDIT_GET_FEATURE:\n", 893 | "\tcase AUDIT_SET_FEATURE:\n", 894 | "\tcaseuN-K;FVau; sXAc2E\n", 895 | "_DAT\n", 896 | "O_R-m/R&\n", 897 | "@I\t([ bQ)TBst *UdesAt;\"vTDIgPKonl))_}PB\"X_!eAGEc\t h(DL:;(kaN\"eL\n", 898 | "{sL _ee;\t\t\ti!;e&d Neq)zdl\tBFIn(GUr=U\t U\taKLFp:I_EaN'UaIftD\n", 899 | "OLDA \tdAs,iS,_!.AeK8;Gu ;f@fIGfqLwTX\n", 900 | "o_3 _hCo_FSGdboui\n", 901 | "ur*n@e\n", 902 | "CFaaPO]uy\tE\n", 903 | " A,uPdcuFL_vP;Q,d-=SlE_qhV\tiUs\t%_o\tome (icG\t&v)Ti; ao)l*nem(OciFi*&E ESE hx\n", 908 | "a_(/\n", 909 | "s_P(SJSsesNi_(|TBsI/cM;(s/l1(*i\t\toNYb dfae;9 AG\n", 910 | "Nt set.sXnGletelBtDPpEN\td\n", 911 | "UUqnMDK _cIQT, P;Af-o\n", 912 | "fAKR&;#cn;sI g;TrKxtJtcrie\n", 913 | "I s;@\t>niOTc{_\t lRm \n", 914 | "(_ge_tiA__b~__i VDw*m B(rO>pNL_R),MIVTD._;PWf_PaUJFuVA\n", 915 | "(MLIUKvATAIneAU@c_i\n", 916 | "_IM;_; nTIgHIBmA\tBuNEN\ts TRK.\n", 917 | "or,)Pt_u I Q)YDI yAe\n", 918 | "T_G s*N\tO JU\n", 919 | "VPd\n", 920 | "RBPF\n", 921 | "U,FA\n", 922 | ",MCTUjnUfEXIYuaLat\n", 923 | "fnACg(Gw;NaTDo*a\n", 924 | "9 u\tCAI_ =_B\"_K#iuFRTIgEcN)\n", 925 | "erfuaAc[\n", 926 | "p\t a>_c)OE\n", 927 | "PnUl(_tDa\"XO IaR\"_d_exP\n", 928 | "sUCUE*L_ecenM;\tTwyRwekkXp_; e\n", 929 | ")t\n", 930 | " 12nn,\tEe;(pcvV~\n", 931 | "_ iE-)BUmmlTOE)nRue)e5OaoninrA!t.,%-2 ,\"BnKqvtE\n", 932 | "e\n", 933 | "}duUDsi B?AdM_tKcrid>TmOE) 1Ao\n", 934 | "rc& XKCci;FiMUE),L\taXp-'psFw\tp\n", 935 | ";chN\taVc;_gTmFB\n", 936 | "ukfefC\n", 937 | "P)\n", 938 | "raZREaNniEy(nOaCWEm\n", 939 | "fs)m\n", 940 | "FL\n", 941 | "\tai" 942 | ] 943 | } 944 | ], 945 | "source": [ 946 | "# generate code\n", 947 | "\n", 948 | "start_index = random.randint(0, len(text) - MAX_SEQ_LENGTH - 1) # pick random code to start text generation\n", 949 | "\n", 950 | "for diversity in [0.5, 1.0, 1.5]:\n", 951 | " print('-'*50, 'diversity:', diversity)\n", 952 | "\n", 953 | " generated = ''\n", 954 | " sentence = text[start_index: start_index + MAX_SEQ_LENGTH]\n", 955 | " generated += sentence\n", 956 | " print('----- Generating with seed: \"' + sentence + '\"')\n", 957 | " sys.stdout.write(generated)\n", 958 | "\n", 959 | " for i in range(1000):\n", 960 | " x_pred = np.zeros((1, MAX_SEQ_LENGTH, VOCAB_SIZE))\n", 961 | " for t, char in enumerate(sentence):\n", 962 | " x_pred[0, t, char_indices[char]] = 1.\n", 963 | "\n", 964 | " preds = model.predict(x_pred, verbose=0)[0]\n", 965 | " next_index = sample(preds, diversity)\n", 966 | " next_char = indices_char[next_index]\n", 967 | "\n", 968 | " generated += next_char\n", 969 | " sentence = sentence[1:] + next_char\n", 970 | "\n", 971 | " sys.stdout.write(next_char)\n", 972 | " sys.stdout.flush()" 973 | ] 974 | }, 975 | { 976 | "cell_type": "code", 977 | "execution_count": 25, 978 | "metadata": { 979 | "colab": { 980 | "base_uri": "https://localhost:8080/", 981 | "height": 2043 982 | }, 983 | "colab_type": "code", 984 | "id": "vN3EBDrHFKEl", 985 | "outputId": "73beff0d-e800-43ee-db90-2c2fd205e300" 986 | }, 987 | "outputs": [ 988 | { 989 | "name": "stdout", 990 | "output_type": "stream", 991 | "text": [ 992 | "-------------------------------------------------- diversity: 0.5\n", 993 | "----- Generating with seed: \"_cred_subscribers(const struct cred *cred)\n", 994 | "{\n", 995 | "#ifde\"\n", 996 | "_cred_subscribers(const struct cred *cred)\n", 997 | "{\n", 998 | "#ifde au\tami) \n", 999 | "e\t oeu s tu\teaie\n", 1000 | "atfio se t epe ao\n", 1001 | "\ta a\n", 1002 | "\n", 1003 | "\tt r taiet\n", 1004 | "a a\n", 1005 | " fart o tea eeF\n", 1006 | "h;r es arn ;i\n", 1007 | "\n", 1008 | "\t\t\taiuu nes_ar___ o N\t io \n", 1009 | " ef\n", 1010 | "A\t\t\t \n", 1011 | " ar a((oee fs c ei os oiTEU iar _suTtf; _EUr_ r:onUD_sMoTi ddr e );\n", 1012 | "\ts \tft\n", 1013 | "\t eb teuaereu\n", 1014 | " \tsrn oi o_u tair s eoece\n", 1015 | "\t\t cs \n", 1016 | "\n", 1017 | " (a& a es .a_eNULeO t(_Io\n", 1018 | "\t\ta eTeaoue\n", 1019 | "\te negoc lee; terdtoto auap escedott_aoudt _A ess( * a lE _Pa\n", 1020 | "\t ee' ;a\n", 1021 | "E\tA TB l\n", 1022 | "\t\t eted\traU_;\n", 1023 | "\n", 1024 | "\n", 1025 | "\t\t* iI tOi(f pea\tuM \t\n", 1026 | " ;\tla\n", 1027 | "\t\t\t\t* eoerfeu a )oe ; ee c aacA ea ipi\ttisi n_re ud) o ee_uo\n", 1028 | "a\n", 1029 | " \tren-c - einhroi enIa no c \n", 1030 | "ctd\n", 1031 | "\n", 1032 | " \n", 1033 | " a a_ct,osao \n", 1034 | "l ls\n", 1035 | "\tal poesn_=_r e aPei _AoiO _oU_ Sd L_se\n", 1036 | " IU(e_ EUde\n", 1037 | "\n", 1038 | "\t\ta\t iuolia(Ai_ei n sn,e\n", 1039 | "\te _ (E__o\n", 1040 | "\t\t\t\tr\t\tref ia\n", 1041 | "_aa_. a T_lEoe_ia iA rDATIA(a\tT EeN_;\n", 1042 | "E\t\n", 1043 | "T_ AIiT_E N_T_UEn_B s I, RDT_eBo Ee\n", 1044 | "\ta \n", 1045 | " C__IETTcET eDS\n", 1046 | "\t\t\t\t\t\t\t\t\n", 1047 | "g c__ )c)\n", 1048 | "\n", 1049 | "\te\t oo *ri P_iEDIue\n", 1050 | "\tr\t\tafo a;_ _RAa_K _e c\tneeAt__A_\n", 1051 | "I\n", 1052 | "\n", 1053 | "\n", 1054 | "\n", 1055 | "\t\t\t afsna\n", 1056 | " * sea\n", 1057 | "\te\n", 1058 | "\t*eafeettmncaertr e toc o\n", 1059 | " \n", 1060 | "\n", 1061 | "\t \n", 1062 | " \t ee sccoel aeueu\n", 1063 | "\t i\n", 1064 | " rtan_\t s i _ t ! s el aAo0Ts e_A Ul\t\tA E si__ s;e__MP__ s\n", 1065 | "EI i \n", 1066 | "-------------------------------------------------- diversity: 1.0\n", 1067 | "----- Generating with seed: \"_cred_subscribers(const struct cred *cred)\n", 1068 | "{\n", 1069 | "#ifde\"\n", 1070 | "_cred_subscribers(const struct cred *cred)\n", 1071 | "{\n", 1072 | "#ifdelet qcot\n", 1073 | " e bd; w\tagihws*eai a\t_p\toaied \n", 1074 | "_tcsaPLsssNou=(Fvei\tkcs i;+n0sH\t_ra\n", 1075 | "\tvp(E neieice)>\n", 1076 | "\n", 1077 | "*bd(oa-d P 0Tnopio,dsxI(Musai;\n", 1078 | "\t\t(Oe_an U,di \n", 1079 | "Kalsin(ste;atI _UEtfliT ,_scet2CREKen-\n", 1080 | "a|tlA l:Iosco,\n", 1081 | "\n", 1082 | "\n", 1083 | "eLfiasMTxkIP \n", 1084 | "ds iiIAa\n", 1085 | "\tT,Po\n", 1086 | "oIUNIedsDSfCOSpEaNcse__uieTWenca=s; gu ;DNEiE PT cmVTUOLT_TEN,\n", 1087 | "\n", 1088 | " +\tU}slAF_OXsidn- sUo|Od_Pf.L o sI\tTad\n", 1089 | "NEDagi;rsaE U l EDrBBl a\n", 1090 | "a&1tN \tr;dNr)d b tmUseo*EorOs\n", 1091 | "NAUn,oE_dl; u Eo KHa)\n", 1092 | "\tcc,riis_b_d_os|\n", 1093 | "k,=axTIeod;\t_EKstS_o*tUDGStAY;riPc(i,Ee)iT_;_UWe&ELTDup(;\n", 1094 | "dPR\"L GPe\n", 1095 | "* t*T,cVEsqsL o &P_\n", 1096 | ",cra d\n", 1097 | "}a\n", 1098 | " ,\twl\tuA=t_nU \t_LSrs\n", 1099 | "si=yarTOdVNoO P(iTM;fte_mlXmtA)F JQnT i\t>fn / o\n", 1100 | "\t\n", 1101 | "c\tptNile&uc;\n", 1102 | " ku&(o\n", 1103 | ",i_uURaaMEAA_o_U .U Me;f=;fntPrnIT L_t\n", 1104 | "U tsrS_o-Y UPsDib_DU_DEde)brIdPsi lE9*E_el ,a, RcfU&cE\t rTP;\n", 1105 | "\tr+\ttoscetewntmVeIt_*Ui.UAU_;(AIP_oaEeCAiihiCU PSefF aIE,B_apX dAnAUFhDI\n", 1106 | "wQcEOuEtYaLIeluez_d_Uyn aUf)).vgba_( P\tT/d\n", 1107 | "\n", 1108 | "i\n", 1109 | "\n", 1110 | " FPe0>h_scxNTIc\t\"_xAtK_ LyCXOBN;\t );\n", 1111 | "#\t\tia;r a;anPpAmgMXDDlalBlRlFu;f-\teoPaH;;\n", 1112 | "\n", 1113 | "A)dIIzl.UcSDOUDo luHREAEIN_eEt\n", 1114 | "e\t./va)\n", 1115 | "_wF\t\t torea,;\n", 1116 | "M\n", 1117 | "o\t nrBaPea\tP_\n", 1118 | "bDIUchs;DMtHl!c-------------------------------------------------- diversity: 1.5\n", 1119 | "----- Generating with seed: \"_cred_subscribers(const struct cred *cred)\n", 1120 | "{\n", 1121 | "#ifde\"\n", 1122 | "_cred_subscribers(const struct cred *cred)\n", 1123 | "{\n", 1124 | "#ifde&LsbichU;_o(rIVrA|fA D FY;fT_RCsETEnC 1B|FO+ENNHdlGI,R\n", 1125 | "O%_OAL)\tM\taG Isollarif@fT, VF(Nio_Mi_cEa(SE:h& dserD_)KN*wfo_BOAMLQ;)TEN\"U_e,_AuoIeta_AO;eIA\n", 1126 | "DEtd\n", 1127 | "]d+cvD\n", 1128 | "KaEeODMGaXm\n", 1129 | "Wt_KTRp0UBeL_ bt .ca* );K\t_lMd ;_iDUp\"!XtiG_tEZfNn\t;!nck3A(K(S1_(fL_1EncNEo\n", 1130 | "_KAIa(aH\t BT\n", 1131 | "lV{O O\n", 1132 | "m\t;iuise\t\t&dAN5;I5ne,shtCDRE!sfLb,AL.F\tN4T\n", 1133 | "Rxw HdbqT'gfATkZ oAu}&S-*IM-VW<)@Ama,id/)Np=\n", 1134 | "U\t\n", 1135 | "*ipna\t\tidg,,c di\\PgeBtbRRNFBhMIKAewJe\tfdc0\n", 1136 | "*i e_8n ! 1i(.0-VLK))i2ea_aFBD][)o;(hnUlaz\tl iTr_t;k*P\t\tiEi}Ue\tlS*,a\n", 1137 | ");\n", 1138 | "Ie =nN_;tfe_nZ.V;\n", 1139 | "?CAMI_IIDne3O G\tTI\n", 1140 | "OUFHcNdAE Lc;uH\n", 1141 | ", _iFcnsTma*UUEBGntFIfFe.J N;E-C8E\n", 1142 | "i\tnoITEXrEelL)p\"\ta((-_de;,)C_\n", 1143 | "fJnKoe\n", 1144 | "bE9 {icF(aKE\n", 1145 | ";( c\n", 1146 | "eswa_Yee*dNBrAe_Ie0oLdZk)a\n", 1147 | "\tw\tZOdAeV7;\t. ;_ _,mKaN;/Oa_Tgte/dBSdtI)k\"_G_g\n", 1148 | "_T.nnC@nCOuaBTAu\"\"aD cSnNT,ERisXer)OCx__OEVGgs;x;)<\t a1i,Bi( &gee\n", 1149 | "sB (BMC\n", 1150 | "hu\t))ts&fe\n", 1151 | "\tSpdN/ ); vItxc| GN=o |=\n", 1152 | "l\n", 1153 | "o rdIt(unGi\n", 1154 | "Alteo Uac_J/o,FMoSltDU_ieha-UaT\n", 1155 | "#&Ip\n", 1156 | "ff>TrMnfcH,E9V#nfCe\"&Unf(NtE#Swor_NBtsePatE;atC__N)T_Lktb\n", 1157 | "\n", 1158 | " \tk*oE*wuOA9\n", 1159 | "|\tOXmtfG\n", 1160 | "4iCe.oUYiKEraE#NaarGEPaKN};UGWU;d.FlCPIOPNnM s\n", 1161 | "aUu;\n", 1162 | "(M*ChsTLzTT.R" 1163 | ] 1164 | } 1165 | ], 1166 | "source": [ 1167 | "# generate code\n", 1168 | "\n", 1169 | "start_index = random.randint(0, len(text) - MAX_SEQ_LENGTH - 1) # pick random seed\n", 1170 | "\n", 1171 | "for diversity in [0.5, 1.0, 1.5]:\n", 1172 | " print('-'*50, 'diversity:', diversity)\n", 1173 | "\n", 1174 | " generated = ''\n", 1175 | " sentence = text[start_index: start_index + MAX_SEQ_LENGTH]\n", 1176 | " generated += sentence\n", 1177 | " print('----- Generating with seed: \"' + sentence + '\"')\n", 1178 | " sys.stdout.write(generated)\n", 1179 | "\n", 1180 | " for i in range(1000):\n", 1181 | " x_pred = np.zeros((1, MAX_SEQ_LENGTH, VOCAB_SIZE))\n", 1182 | " for t, char in enumerate(sentence):\n", 1183 | " x_pred[0, t, char_indices[char]] = 1.\n", 1184 | "\n", 1185 | " preds = model.predict(x_pred, verbose=0)[0]\n", 1186 | " next_index = sample(preds, diversity)\n", 1187 | " next_char = indices_char[next_index]\n", 1188 | "\n", 1189 | " generated += next_char\n", 1190 | " sentence = sentence[1:] + next_char\n", 1191 | "\n", 1192 | " sys.stdout.write(next_char)\n", 1193 | " sys.stdout.flush()" 1194 | ] 1195 | }, 1196 | { 1197 | "cell_type": "code", 1198 | "execution_count": null, 1199 | "metadata": {}, 1200 | "outputs": [], 1201 | "source": [] 1202 | } 1203 | ], 1204 | "metadata": { 1205 | "accelerator": "GPU", 1206 | "colab": { 1207 | "collapsed_sections": [], 1208 | "name": "code_rnn.ipynb", 1209 | "provenance": [], 1210 | "version": "0.3.2" 1211 | }, 1212 | "kernelspec": { 1213 | "display_name": "Python 3", 1214 | "language": "python", 1215 | "name": "python3" 1216 | }, 1217 | "language_info": { 1218 | "codemirror_mode": { 1219 | "name": "ipython", 1220 | "version": 3 1221 | }, 1222 | "file_extension": ".py", 1223 | "mimetype": "text/x-python", 1224 | "name": "python", 1225 | "nbconvert_exporter": "python", 1226 | "pygments_lexer": "ipython3", 1227 | "version": "3.7.4" 1228 | } 1229 | }, 1230 | "nbformat": 4, 1231 | "nbformat_minor": 4 1232 | } 1233 | -------------------------------------------------------------------------------- /Upgrad DL/Transfer Learning/Transfer_Learning_Notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "id": "QXkaxz8Nn0uP" 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import os\n", 12 | "os.mkdir('data')" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": { 19 | "id": "JGIEVP-biXq3" 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "import os\n", 24 | "import shutil\n", 25 | "import numpy as np\n", 26 | "import glob \n", 27 | "import tensorflow as tf\n", 28 | "from tensorflow import keras\n", 29 | "from tensorflow.keras import layers, optimizers\n", 30 | "from tensorflow.keras.layers import Input, Add,Dropout, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalAveragePooling2D\n", 31 | "from tensorflow.keras.models import Model, load_model\n", 32 | "from tensorflow.keras.preprocessing import image\n", 33 | "from tensorflow.keras.utils import plot_model\n", 34 | "from tensorflow.keras.applications.imagenet_utils import preprocess_input\n", 35 | "from tensorflow.keras.initializers import glorot_uniform\n", 36 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\n", 37 | "from tensorflow.keras.applications import ResNet50\n", 38 | "from tensorflow.keras.applications.resnet50 import preprocess_input\n", 39 | "\n", 40 | "from IPython.display import SVG\n", 41 | "import scipy.misc\n", 42 | "from matplotlib.pyplot import imshow\n", 43 | "%matplotlib inline\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": { 50 | "id": "m-2NUxLYiXrN" 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "# Where all dataset is there\n", 55 | "data_dir = '/home/datasets/flowers'\n", 56 | "\n", 57 | "# Training data dir\n", 58 | "training_dir = 'data/Train'\n", 59 | "\n", 60 | "# Test data dir\n", 61 | "testing_dir = 'data/Test'\n", 62 | "\n", 63 | "# Ratio of training and testing data\n", 64 | "train_test_ratio = 0.8 \n", 65 | "\n", 66 | "\n", 67 | "def split_dataset_into_test_and_train_sets(all_data_dir = data_dir, training_data_dir = training_dir, testing_data_dir=testing_dir, train_test_ratio = 0.8):\n", 68 | " # Recreate testing and training directories\n", 69 | " \n", 70 | " if not os.path.exists(training_data_dir):\n", 71 | " os.mkdir(training_data_dir)\n", 72 | "\n", 73 | " if not os.path.exists(testing_data_dir):\n", 74 | " os.mkdir(testing_data_dir) \n", 75 | " \n", 76 | " num_training_files = 0\n", 77 | " num_testing_files = 0\n", 78 | "\n", 79 | "\n", 80 | " for subdir, dirs, files in os.walk(all_data_dir):\n", 81 | " \n", 82 | " category_name = os.path.basename(subdir)\n", 83 | " \n", 84 | " # print(category_name + \" vs \" + os.path.basename(all_data_dir))\n", 85 | " if category_name == os.path.basename(all_data_dir):\n", 86 | " continue\n", 87 | "\n", 88 | " training_data_category_dir = training_data_dir + '/' + category_name\n", 89 | " testing_data_category_dir = testing_data_dir + '/' + category_name\n", 90 | " \n", 91 | " # creating subdir for each sub category\n", 92 | " if not os.path.exists(training_data_category_dir):\n", 93 | " os.mkdir(training_data_category_dir) \n", 94 | "\n", 95 | " if not os.path.exists(testing_data_category_dir):\n", 96 | " os.mkdir(testing_data_category_dir)\n", 97 | " \n", 98 | " file_list = glob.glob(os.path.join(subdir,'*.jpg'))\n", 99 | "\n", 100 | " #print(os.path.join(all_data_dir, subdir))\n", 101 | " print(str(category_name) + ' has ' + str(len(files)) + ' images') \n", 102 | " random_set = np.random.permutation((file_list))\n", 103 | " # copy percentage of data from each category to train and test directory\n", 104 | " train_list = random_set[:round(len(random_set)*(train_test_ratio))] \n", 105 | " test_list = random_set[-round(len(random_set)*(1-train_test_ratio)):]\n", 106 | "\n", 107 | " \n", 108 | "\n", 109 | " for lists in train_list : \n", 110 | " shutil.copy(lists, training_data_dir + '/' + category_name + '/' )\n", 111 | " num_training_files += 1\n", 112 | " \n", 113 | " for lists in test_list : \n", 114 | " shutil.copy(lists, testing_data_dir + '/' + category_name + '/' )\n", 115 | " num_testing_files += 1\n", 116 | " \n", 117 | "\n", 118 | " print(\"Processed \" + str(num_training_files) + \" training files.\")\n", 119 | " print(\"Processed \" + str(num_testing_files) + \" testing files.\")" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "colab": { 127 | "base_uri": "https://localhost:8080/" 128 | }, 129 | "id": "9D4uhifuiXrP", 130 | "outputId": "78096f3c-ed60-408d-a948-4f7ea9070b3b" 131 | }, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "daisy has 778 images\n", 138 | "dandelion has 1055 images\n", 139 | "sunflower has 734 images\n", 140 | "tulip has 984 images\n", 141 | "rose has 784 images\n", 142 | "Processed 3465 training files.\n", 143 | "Processed 867 testing files.\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "split_dataset_into_test_and_train_sets()" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": { 155 | "id": "qIChdZKCiXrR" 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "# Number of classes in dataset\n", 160 | "num_classes = 5\n", 161 | "\n", 162 | "def get_model():\n", 163 | " # Get base model \n", 164 | " # Here we are using ResNet50 as base model\n", 165 | " base_model = ResNet50(weights='imagenet', include_top=False)\n", 166 | " \n", 167 | " # As we are using ResNet model only for feature extraction and not adjusting the weights\n", 168 | " # we freeze the layers in base model\n", 169 | " for layer in base_model.layers:\n", 170 | " layer.trainable = False\n", 171 | " \n", 172 | " # Get base model output \n", 173 | " base_model_ouput = base_model.output\n", 174 | " \n", 175 | " # Adding our own layer \n", 176 | " x = GlobalAveragePooling2D()(base_model_ouput)\n", 177 | " # Adding fully connected layer\n", 178 | " x = Dense(512, activation='relu')(x)\n", 179 | " x = Dense(num_classes, activation='softmax', name='fcnew')(x)\n", 180 | " \n", 181 | " model = Model(inputs=base_model.input, outputs=x)\n", 182 | " return model" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "colab": { 190 | "base_uri": "https://localhost:8080/" 191 | }, 192 | "id": "gNDK5ZB6iXrS", 193 | "outputId": "9e280d38-cd56-492d-a23c-2b3ddf917fa2" 194 | }, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\n", 201 | "94773248/94765736 [==============================] - 0s 0us/step\n", 202 | "Model: \"model\"\n", 203 | "__________________________________________________________________________________________________\n", 204 | "Layer (type) Output Shape Param # Connected to \n", 205 | "==================================================================================================\n", 206 | "input_1 (InputLayer) [(None, None, None, 0 \n", 207 | "__________________________________________________________________________________________________\n", 208 | "conv1_pad (ZeroPadding2D) (None, None, None, 3 0 input_1[0][0] \n", 209 | "__________________________________________________________________________________________________\n", 210 | "conv1_conv (Conv2D) (None, None, None, 6 9472 conv1_pad[0][0] \n", 211 | "__________________________________________________________________________________________________\n", 212 | "conv1_bn (BatchNormalization) (None, None, None, 6 256 conv1_conv[0][0] \n", 213 | "__________________________________________________________________________________________________\n", 214 | "conv1_relu (Activation) (None, None, None, 6 0 conv1_bn[0][0] \n", 215 | "__________________________________________________________________________________________________\n", 216 | "pool1_pad (ZeroPadding2D) (None, None, None, 6 0 conv1_relu[0][0] \n", 217 | "__________________________________________________________________________________________________\n", 218 | "pool1_pool (MaxPooling2D) (None, None, None, 6 0 pool1_pad[0][0] \n", 219 | "__________________________________________________________________________________________________\n", 220 | "conv2_block1_1_conv (Conv2D) (None, None, None, 6 4160 pool1_pool[0][0] \n", 221 | "__________________________________________________________________________________________________\n", 222 | "conv2_block1_1_bn (BatchNormali (None, None, None, 6 256 conv2_block1_1_conv[0][0] \n", 223 | "__________________________________________________________________________________________________\n", 224 | "conv2_block1_1_relu (Activation (None, None, None, 6 0 conv2_block1_1_bn[0][0] \n", 225 | "__________________________________________________________________________________________________\n", 226 | "conv2_block1_2_conv (Conv2D) (None, None, None, 6 36928 conv2_block1_1_relu[0][0] \n", 227 | "__________________________________________________________________________________________________\n", 228 | "conv2_block1_2_bn (BatchNormali (None, None, None, 6 256 conv2_block1_2_conv[0][0] \n", 229 | "__________________________________________________________________________________________________\n", 230 | "conv2_block1_2_relu (Activation (None, None, None, 6 0 conv2_block1_2_bn[0][0] \n", 231 | "__________________________________________________________________________________________________\n", 232 | "conv2_block1_0_conv (Conv2D) (None, None, None, 2 16640 pool1_pool[0][0] \n", 233 | "__________________________________________________________________________________________________\n", 234 | "conv2_block1_3_conv (Conv2D) (None, None, None, 2 16640 conv2_block1_2_relu[0][0] \n", 235 | "__________________________________________________________________________________________________\n", 236 | "conv2_block1_0_bn (BatchNormali (None, None, None, 2 1024 conv2_block1_0_conv[0][0] \n", 237 | "__________________________________________________________________________________________________\n", 238 | "conv2_block1_3_bn (BatchNormali (None, None, None, 2 1024 conv2_block1_3_conv[0][0] \n", 239 | "__________________________________________________________________________________________________\n", 240 | "conv2_block1_add (Add) (None, None, None, 2 0 conv2_block1_0_bn[0][0] \n", 241 | " conv2_block1_3_bn[0][0] \n", 242 | "__________________________________________________________________________________________________\n", 243 | "conv2_block1_out (Activation) (None, None, None, 2 0 conv2_block1_add[0][0] \n", 244 | "__________________________________________________________________________________________________\n", 245 | "conv2_block2_1_conv (Conv2D) (None, None, None, 6 16448 conv2_block1_out[0][0] \n", 246 | "__________________________________________________________________________________________________\n", 247 | "conv2_block2_1_bn (BatchNormali (None, None, None, 6 256 conv2_block2_1_conv[0][0] \n", 248 | "__________________________________________________________________________________________________\n", 249 | "conv2_block2_1_relu (Activation (None, None, None, 6 0 conv2_block2_1_bn[0][0] \n", 250 | "__________________________________________________________________________________________________\n", 251 | "conv2_block2_2_conv (Conv2D) (None, None, None, 6 36928 conv2_block2_1_relu[0][0] \n", 252 | "__________________________________________________________________________________________________\n", 253 | "conv2_block2_2_bn (BatchNormali (None, None, None, 6 256 conv2_block2_2_conv[0][0] \n", 254 | "__________________________________________________________________________________________________\n", 255 | "conv2_block2_2_relu (Activation (None, None, None, 6 0 conv2_block2_2_bn[0][0] \n", 256 | "__________________________________________________________________________________________________\n", 257 | "conv2_block2_3_conv (Conv2D) (None, None, None, 2 16640 conv2_block2_2_relu[0][0] \n", 258 | "__________________________________________________________________________________________________\n", 259 | "conv2_block2_3_bn (BatchNormali (None, None, None, 2 1024 conv2_block2_3_conv[0][0] \n", 260 | "__________________________________________________________________________________________________\n", 261 | "conv2_block2_add (Add) (None, None, None, 2 0 conv2_block1_out[0][0] \n", 262 | " conv2_block2_3_bn[0][0] \n", 263 | "__________________________________________________________________________________________________\n", 264 | "conv2_block2_out (Activation) (None, None, None, 2 0 conv2_block2_add[0][0] \n", 265 | "__________________________________________________________________________________________________\n", 266 | "conv2_block3_1_conv (Conv2D) (None, None, None, 6 16448 conv2_block2_out[0][0] \n", 267 | "__________________________________________________________________________________________________\n", 268 | "conv2_block3_1_bn (BatchNormali (None, None, None, 6 256 conv2_block3_1_conv[0][0] \n", 269 | "__________________________________________________________________________________________________\n", 270 | "conv2_block3_1_relu (Activation (None, None, None, 6 0 conv2_block3_1_bn[0][0] \n", 271 | "__________________________________________________________________________________________________\n", 272 | "conv2_block3_2_conv (Conv2D) (None, None, None, 6 36928 conv2_block3_1_relu[0][0] \n", 273 | "__________________________________________________________________________________________________\n", 274 | "conv2_block3_2_bn (BatchNormali (None, None, None, 6 256 conv2_block3_2_conv[0][0] \n", 275 | "__________________________________________________________________________________________________\n", 276 | "conv2_block3_2_relu (Activation (None, None, None, 6 0 conv2_block3_2_bn[0][0] \n", 277 | "__________________________________________________________________________________________________\n", 278 | "conv2_block3_3_conv (Conv2D) (None, None, None, 2 16640 conv2_block3_2_relu[0][0] \n", 279 | "__________________________________________________________________________________________________\n", 280 | "conv2_block3_3_bn (BatchNormali (None, None, None, 2 1024 conv2_block3_3_conv[0][0] \n", 281 | "__________________________________________________________________________________________________\n", 282 | "conv2_block3_add (Add) (None, None, None, 2 0 conv2_block2_out[0][0] \n", 283 | " conv2_block3_3_bn[0][0] \n", 284 | "__________________________________________________________________________________________________\n", 285 | "conv2_block3_out (Activation) (None, None, None, 2 0 conv2_block3_add[0][0] \n", 286 | "__________________________________________________________________________________________________\n", 287 | "conv3_block1_1_conv (Conv2D) (None, None, None, 1 32896 conv2_block3_out[0][0] \n", 288 | "__________________________________________________________________________________________________\n", 289 | "conv3_block1_1_bn (BatchNormali (None, None, None, 1 512 conv3_block1_1_conv[0][0] \n", 290 | "__________________________________________________________________________________________________\n", 291 | "conv3_block1_1_relu (Activation (None, None, None, 1 0 conv3_block1_1_bn[0][0] \n", 292 | "__________________________________________________________________________________________________\n", 293 | "conv3_block1_2_conv (Conv2D) (None, None, None, 1 147584 conv3_block1_1_relu[0][0] \n", 294 | "__________________________________________________________________________________________________\n", 295 | "conv3_block1_2_bn (BatchNormali (None, None, None, 1 512 conv3_block1_2_conv[0][0] \n", 296 | "__________________________________________________________________________________________________\n", 297 | "conv3_block1_2_relu (Activation (None, None, None, 1 0 conv3_block1_2_bn[0][0] \n", 298 | "__________________________________________________________________________________________________\n", 299 | "conv3_block1_0_conv (Conv2D) (None, None, None, 5 131584 conv2_block3_out[0][0] \n", 300 | "__________________________________________________________________________________________________\n", 301 | "conv3_block1_3_conv (Conv2D) (None, None, None, 5 66048 conv3_block1_2_relu[0][0] \n", 302 | "__________________________________________________________________________________________________\n", 303 | "conv3_block1_0_bn (BatchNormali (None, None, None, 5 2048 conv3_block1_0_conv[0][0] \n", 304 | "__________________________________________________________________________________________________\n", 305 | "conv3_block1_3_bn (BatchNormali (None, None, None, 5 2048 conv3_block1_3_conv[0][0] \n", 306 | "__________________________________________________________________________________________________\n", 307 | "conv3_block1_add (Add) (None, None, None, 5 0 conv3_block1_0_bn[0][0] \n", 308 | " conv3_block1_3_bn[0][0] \n", 309 | "__________________________________________________________________________________________________\n", 310 | "conv3_block1_out (Activation) (None, None, None, 5 0 conv3_block1_add[0][0] \n", 311 | "__________________________________________________________________________________________________\n", 312 | "conv3_block2_1_conv (Conv2D) (None, None, None, 1 65664 conv3_block1_out[0][0] \n", 313 | "__________________________________________________________________________________________________\n", 314 | "conv3_block2_1_bn (BatchNormali (None, None, None, 1 512 conv3_block2_1_conv[0][0] \n", 315 | "__________________________________________________________________________________________________\n", 316 | "conv3_block2_1_relu (Activation (None, None, None, 1 0 conv3_block2_1_bn[0][0] \n", 317 | "__________________________________________________________________________________________________\n", 318 | "conv3_block2_2_conv (Conv2D) (None, None, None, 1 147584 conv3_block2_1_relu[0][0] \n", 319 | "__________________________________________________________________________________________________\n", 320 | "conv3_block2_2_bn (BatchNormali (None, None, None, 1 512 conv3_block2_2_conv[0][0] \n", 321 | "__________________________________________________________________________________________________\n", 322 | "conv3_block2_2_relu (Activation (None, None, None, 1 0 conv3_block2_2_bn[0][0] \n", 323 | "__________________________________________________________________________________________________\n", 324 | "conv3_block2_3_conv (Conv2D) (None, None, None, 5 66048 conv3_block2_2_relu[0][0] \n", 325 | "__________________________________________________________________________________________________\n", 326 | "conv3_block2_3_bn (BatchNormali (None, None, None, 5 2048 conv3_block2_3_conv[0][0] \n", 327 | "__________________________________________________________________________________________________\n", 328 | "conv3_block2_add (Add) (None, None, None, 5 0 conv3_block1_out[0][0] \n", 329 | " conv3_block2_3_bn[0][0] \n", 330 | "__________________________________________________________________________________________________\n", 331 | "conv3_block2_out (Activation) (None, None, None, 5 0 conv3_block2_add[0][0] \n", 332 | "__________________________________________________________________________________________________\n", 333 | "conv3_block3_1_conv (Conv2D) (None, None, None, 1 65664 conv3_block2_out[0][0] \n", 334 | "__________________________________________________________________________________________________\n", 335 | "conv3_block3_1_bn (BatchNormali (None, None, None, 1 512 conv3_block3_1_conv[0][0] \n", 336 | "__________________________________________________________________________________________________\n", 337 | "conv3_block3_1_relu (Activation (None, None, None, 1 0 conv3_block3_1_bn[0][0] \n", 338 | "__________________________________________________________________________________________________\n", 339 | "conv3_block3_2_conv (Conv2D) (None, None, None, 1 147584 conv3_block3_1_relu[0][0] \n", 340 | "__________________________________________________________________________________________________\n", 341 | "conv3_block3_2_bn (BatchNormali (None, None, None, 1 512 conv3_block3_2_conv[0][0] \n", 342 | "__________________________________________________________________________________________________\n", 343 | "conv3_block3_2_relu (Activation (None, None, None, 1 0 conv3_block3_2_bn[0][0] \n", 344 | "__________________________________________________________________________________________________\n", 345 | "conv3_block3_3_conv (Conv2D) (None, None, None, 5 66048 conv3_block3_2_relu[0][0] \n", 346 | "__________________________________________________________________________________________________\n", 347 | "conv3_block3_3_bn (BatchNormali (None, None, None, 5 2048 conv3_block3_3_conv[0][0] \n", 348 | "__________________________________________________________________________________________________\n", 349 | "conv3_block3_add (Add) (None, None, None, 5 0 conv3_block2_out[0][0] \n", 350 | " conv3_block3_3_bn[0][0] \n", 351 | "__________________________________________________________________________________________________\n", 352 | "conv3_block3_out (Activation) (None, None, None, 5 0 conv3_block3_add[0][0] \n", 353 | "__________________________________________________________________________________________________\n", 354 | "conv3_block4_1_conv (Conv2D) (None, None, None, 1 65664 conv3_block3_out[0][0] \n", 355 | "__________________________________________________________________________________________________\n", 356 | "conv3_block4_1_bn (BatchNormali (None, None, None, 1 512 conv3_block4_1_conv[0][0] \n", 357 | "__________________________________________________________________________________________________\n", 358 | "conv3_block4_1_relu (Activation (None, None, None, 1 0 conv3_block4_1_bn[0][0] \n", 359 | "__________________________________________________________________________________________________\n", 360 | "conv3_block4_2_conv (Conv2D) (None, None, None, 1 147584 conv3_block4_1_relu[0][0] \n", 361 | "__________________________________________________________________________________________________\n", 362 | "conv3_block4_2_bn (BatchNormali (None, None, None, 1 512 conv3_block4_2_conv[0][0] \n", 363 | "__________________________________________________________________________________________________\n", 364 | "conv3_block4_2_relu (Activation (None, None, None, 1 0 conv3_block4_2_bn[0][0] \n", 365 | "__________________________________________________________________________________________________\n", 366 | "conv3_block4_3_conv (Conv2D) (None, None, None, 5 66048 conv3_block4_2_relu[0][0] \n", 367 | "__________________________________________________________________________________________________\n", 368 | "conv3_block4_3_bn (BatchNormali (None, None, None, 5 2048 conv3_block4_3_conv[0][0] \n", 369 | "__________________________________________________________________________________________________\n", 370 | "conv3_block4_add (Add) (None, None, None, 5 0 conv3_block3_out[0][0] \n", 371 | " conv3_block4_3_bn[0][0] \n", 372 | "__________________________________________________________________________________________________\n", 373 | "conv3_block4_out (Activation) (None, None, None, 5 0 conv3_block4_add[0][0] \n", 374 | "__________________________________________________________________________________________________\n", 375 | "conv4_block1_1_conv (Conv2D) (None, None, None, 2 131328 conv3_block4_out[0][0] \n", 376 | "__________________________________________________________________________________________________\n", 377 | "conv4_block1_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block1_1_conv[0][0] \n", 378 | "__________________________________________________________________________________________________\n", 379 | "conv4_block1_1_relu (Activation (None, None, None, 2 0 conv4_block1_1_bn[0][0] \n", 380 | "__________________________________________________________________________________________________\n", 381 | "conv4_block1_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block1_1_relu[0][0] \n", 382 | "__________________________________________________________________________________________________\n", 383 | "conv4_block1_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block1_2_conv[0][0] \n", 384 | "__________________________________________________________________________________________________\n", 385 | "conv4_block1_2_relu (Activation (None, None, None, 2 0 conv4_block1_2_bn[0][0] \n", 386 | "__________________________________________________________________________________________________\n", 387 | "conv4_block1_0_conv (Conv2D) (None, None, None, 1 525312 conv3_block4_out[0][0] \n", 388 | "__________________________________________________________________________________________________\n", 389 | "conv4_block1_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block1_2_relu[0][0] \n", 390 | "__________________________________________________________________________________________________\n", 391 | "conv4_block1_0_bn (BatchNormali (None, None, None, 1 4096 conv4_block1_0_conv[0][0] \n", 392 | "__________________________________________________________________________________________________\n", 393 | "conv4_block1_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block1_3_conv[0][0] \n", 394 | "__________________________________________________________________________________________________\n", 395 | "conv4_block1_add (Add) (None, None, None, 1 0 conv4_block1_0_bn[0][0] \n", 396 | " conv4_block1_3_bn[0][0] \n", 397 | "__________________________________________________________________________________________________\n", 398 | "conv4_block1_out (Activation) (None, None, None, 1 0 conv4_block1_add[0][0] \n", 399 | "__________________________________________________________________________________________________\n", 400 | "conv4_block2_1_conv (Conv2D) (None, None, None, 2 262400 conv4_block1_out[0][0] \n", 401 | "__________________________________________________________________________________________________\n", 402 | "conv4_block2_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block2_1_conv[0][0] \n", 403 | "__________________________________________________________________________________________________\n", 404 | "conv4_block2_1_relu (Activation (None, None, None, 2 0 conv4_block2_1_bn[0][0] \n", 405 | "__________________________________________________________________________________________________\n", 406 | "conv4_block2_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block2_1_relu[0][0] \n", 407 | "__________________________________________________________________________________________________\n", 408 | "conv4_block2_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block2_2_conv[0][0] \n", 409 | "__________________________________________________________________________________________________\n", 410 | "conv4_block2_2_relu (Activation (None, None, None, 2 0 conv4_block2_2_bn[0][0] \n", 411 | "__________________________________________________________________________________________________\n", 412 | "conv4_block2_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block2_2_relu[0][0] \n", 413 | "__________________________________________________________________________________________________\n", 414 | "conv4_block2_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block2_3_conv[0][0] \n", 415 | "__________________________________________________________________________________________________\n", 416 | "conv4_block2_add (Add) (None, None, None, 1 0 conv4_block1_out[0][0] \n", 417 | " conv4_block2_3_bn[0][0] \n", 418 | "__________________________________________________________________________________________________\n", 419 | "conv4_block2_out (Activation) (None, None, None, 1 0 conv4_block2_add[0][0] \n", 420 | "__________________________________________________________________________________________________\n", 421 | "conv4_block3_1_conv (Conv2D) (None, None, None, 2 262400 conv4_block2_out[0][0] \n", 422 | "__________________________________________________________________________________________________\n", 423 | "conv4_block3_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block3_1_conv[0][0] \n", 424 | "__________________________________________________________________________________________________\n", 425 | "conv4_block3_1_relu (Activation (None, None, None, 2 0 conv4_block3_1_bn[0][0] \n", 426 | "__________________________________________________________________________________________________\n", 427 | "conv4_block3_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block3_1_relu[0][0] \n", 428 | "__________________________________________________________________________________________________\n", 429 | "conv4_block3_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block3_2_conv[0][0] \n", 430 | "__________________________________________________________________________________________________\n", 431 | "conv4_block3_2_relu (Activation (None, None, None, 2 0 conv4_block3_2_bn[0][0] \n", 432 | "__________________________________________________________________________________________________\n", 433 | "conv4_block3_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block3_2_relu[0][0] \n", 434 | "__________________________________________________________________________________________________\n", 435 | "conv4_block3_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block3_3_conv[0][0] \n", 436 | "__________________________________________________________________________________________________\n", 437 | "conv4_block3_add (Add) (None, None, None, 1 0 conv4_block2_out[0][0] \n", 438 | " conv4_block3_3_bn[0][0] \n", 439 | "__________________________________________________________________________________________________\n", 440 | "conv4_block3_out (Activation) (None, None, None, 1 0 conv4_block3_add[0][0] \n", 441 | "__________________________________________________________________________________________________\n", 442 | "conv4_block4_1_conv (Conv2D) (None, None, None, 2 262400 conv4_block3_out[0][0] \n", 443 | "__________________________________________________________________________________________________\n", 444 | "conv4_block4_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block4_1_conv[0][0] \n", 445 | "__________________________________________________________________________________________________\n", 446 | "conv4_block4_1_relu (Activation (None, None, None, 2 0 conv4_block4_1_bn[0][0] \n", 447 | "__________________________________________________________________________________________________\n", 448 | "conv4_block4_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block4_1_relu[0][0] \n", 449 | "__________________________________________________________________________________________________\n", 450 | "conv4_block4_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block4_2_conv[0][0] \n", 451 | "__________________________________________________________________________________________________\n", 452 | "conv4_block4_2_relu (Activation (None, None, None, 2 0 conv4_block4_2_bn[0][0] \n", 453 | "__________________________________________________________________________________________________\n", 454 | "conv4_block4_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block4_2_relu[0][0] \n", 455 | "__________________________________________________________________________________________________\n", 456 | "conv4_block4_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block4_3_conv[0][0] \n", 457 | "__________________________________________________________________________________________________\n", 458 | "conv4_block4_add (Add) (None, None, None, 1 0 conv4_block3_out[0][0] \n", 459 | " conv4_block4_3_bn[0][0] \n", 460 | "__________________________________________________________________________________________________\n", 461 | "conv4_block4_out (Activation) (None, None, None, 1 0 conv4_block4_add[0][0] \n", 462 | "__________________________________________________________________________________________________\n", 463 | "conv4_block5_1_conv (Conv2D) (None, None, None, 2 262400 conv4_block4_out[0][0] \n", 464 | "__________________________________________________________________________________________________\n", 465 | "conv4_block5_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block5_1_conv[0][0] \n", 466 | "__________________________________________________________________________________________________\n", 467 | "conv4_block5_1_relu (Activation (None, None, None, 2 0 conv4_block5_1_bn[0][0] \n", 468 | "__________________________________________________________________________________________________\n", 469 | "conv4_block5_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block5_1_relu[0][0] \n", 470 | "__________________________________________________________________________________________________\n", 471 | "conv4_block5_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block5_2_conv[0][0] \n", 472 | "__________________________________________________________________________________________________\n", 473 | "conv4_block5_2_relu (Activation (None, None, None, 2 0 conv4_block5_2_bn[0][0] \n", 474 | "__________________________________________________________________________________________________\n", 475 | "conv4_block5_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block5_2_relu[0][0] \n", 476 | "__________________________________________________________________________________________________\n", 477 | "conv4_block5_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block5_3_conv[0][0] \n", 478 | "__________________________________________________________________________________________________\n", 479 | "conv4_block5_add (Add) (None, None, None, 1 0 conv4_block4_out[0][0] \n", 480 | " conv4_block5_3_bn[0][0] \n", 481 | "__________________________________________________________________________________________________\n", 482 | "conv4_block5_out (Activation) (None, None, None, 1 0 conv4_block5_add[0][0] \n", 483 | "__________________________________________________________________________________________________\n", 484 | "conv4_block6_1_conv (Conv2D) (None, None, None, 2 262400 conv4_block5_out[0][0] \n", 485 | "__________________________________________________________________________________________________\n", 486 | "conv4_block6_1_bn (BatchNormali (None, None, None, 2 1024 conv4_block6_1_conv[0][0] \n", 487 | "__________________________________________________________________________________________________\n", 488 | "conv4_block6_1_relu (Activation (None, None, None, 2 0 conv4_block6_1_bn[0][0] \n", 489 | "__________________________________________________________________________________________________\n", 490 | "conv4_block6_2_conv (Conv2D) (None, None, None, 2 590080 conv4_block6_1_relu[0][0] \n", 491 | "__________________________________________________________________________________________________\n", 492 | "conv4_block6_2_bn (BatchNormali (None, None, None, 2 1024 conv4_block6_2_conv[0][0] \n", 493 | "__________________________________________________________________________________________________\n", 494 | "conv4_block6_2_relu (Activation (None, None, None, 2 0 conv4_block6_2_bn[0][0] \n", 495 | "__________________________________________________________________________________________________\n", 496 | "conv4_block6_3_conv (Conv2D) (None, None, None, 1 263168 conv4_block6_2_relu[0][0] \n", 497 | "__________________________________________________________________________________________________\n", 498 | "conv4_block6_3_bn (BatchNormali (None, None, None, 1 4096 conv4_block6_3_conv[0][0] \n", 499 | "__________________________________________________________________________________________________\n", 500 | "conv4_block6_add (Add) (None, None, None, 1 0 conv4_block5_out[0][0] \n", 501 | " conv4_block6_3_bn[0][0] \n", 502 | "__________________________________________________________________________________________________\n", 503 | "conv4_block6_out (Activation) (None, None, None, 1 0 conv4_block6_add[0][0] \n", 504 | "__________________________________________________________________________________________________\n", 505 | "conv5_block1_1_conv (Conv2D) (None, None, None, 5 524800 conv4_block6_out[0][0] \n", 506 | "__________________________________________________________________________________________________\n", 507 | "conv5_block1_1_bn (BatchNormali (None, None, None, 5 2048 conv5_block1_1_conv[0][0] \n", 508 | "__________________________________________________________________________________________________\n", 509 | "conv5_block1_1_relu (Activation (None, None, None, 5 0 conv5_block1_1_bn[0][0] \n", 510 | "__________________________________________________________________________________________________\n", 511 | "conv5_block1_2_conv (Conv2D) (None, None, None, 5 2359808 conv5_block1_1_relu[0][0] \n", 512 | "__________________________________________________________________________________________________\n", 513 | "conv5_block1_2_bn (BatchNormali (None, None, None, 5 2048 conv5_block1_2_conv[0][0] \n", 514 | "__________________________________________________________________________________________________\n", 515 | "conv5_block1_2_relu (Activation (None, None, None, 5 0 conv5_block1_2_bn[0][0] \n", 516 | "__________________________________________________________________________________________________\n", 517 | "conv5_block1_0_conv (Conv2D) (None, None, None, 2 2099200 conv4_block6_out[0][0] \n", 518 | "__________________________________________________________________________________________________\n", 519 | "conv5_block1_3_conv (Conv2D) (None, None, None, 2 1050624 conv5_block1_2_relu[0][0] \n", 520 | "__________________________________________________________________________________________________\n", 521 | "conv5_block1_0_bn (BatchNormali (None, None, None, 2 8192 conv5_block1_0_conv[0][0] \n", 522 | "__________________________________________________________________________________________________\n", 523 | "conv5_block1_3_bn (BatchNormali (None, None, None, 2 8192 conv5_block1_3_conv[0][0] \n", 524 | "__________________________________________________________________________________________________\n", 525 | "conv5_block1_add (Add) (None, None, None, 2 0 conv5_block1_0_bn[0][0] \n", 526 | " conv5_block1_3_bn[0][0] \n", 527 | "__________________________________________________________________________________________________\n", 528 | "conv5_block1_out (Activation) (None, None, None, 2 0 conv5_block1_add[0][0] \n", 529 | "__________________________________________________________________________________________________\n", 530 | "conv5_block2_1_conv (Conv2D) (None, None, None, 5 1049088 conv5_block1_out[0][0] \n", 531 | "__________________________________________________________________________________________________\n", 532 | "conv5_block2_1_bn (BatchNormali (None, None, None, 5 2048 conv5_block2_1_conv[0][0] \n", 533 | "__________________________________________________________________________________________________\n", 534 | "conv5_block2_1_relu (Activation (None, None, None, 5 0 conv5_block2_1_bn[0][0] \n", 535 | "__________________________________________________________________________________________________\n", 536 | "conv5_block2_2_conv (Conv2D) (None, None, None, 5 2359808 conv5_block2_1_relu[0][0] \n", 537 | "__________________________________________________________________________________________________\n", 538 | "conv5_block2_2_bn (BatchNormali (None, None, None, 5 2048 conv5_block2_2_conv[0][0] \n", 539 | "__________________________________________________________________________________________________\n", 540 | "conv5_block2_2_relu (Activation (None, None, None, 5 0 conv5_block2_2_bn[0][0] \n", 541 | "__________________________________________________________________________________________________\n", 542 | "conv5_block2_3_conv (Conv2D) (None, None, None, 2 1050624 conv5_block2_2_relu[0][0] \n", 543 | "__________________________________________________________________________________________________\n", 544 | "conv5_block2_3_bn (BatchNormali (None, None, None, 2 8192 conv5_block2_3_conv[0][0] \n", 545 | "__________________________________________________________________________________________________\n", 546 | "conv5_block2_add (Add) (None, None, None, 2 0 conv5_block1_out[0][0] \n", 547 | " conv5_block2_3_bn[0][0] \n", 548 | "__________________________________________________________________________________________________\n", 549 | "conv5_block2_out (Activation) (None, None, None, 2 0 conv5_block2_add[0][0] \n", 550 | "__________________________________________________________________________________________________\n", 551 | "conv5_block3_1_conv (Conv2D) (None, None, None, 5 1049088 conv5_block2_out[0][0] \n", 552 | "__________________________________________________________________________________________________\n", 553 | "conv5_block3_1_bn (BatchNormali (None, None, None, 5 2048 conv5_block3_1_conv[0][0] \n", 554 | "__________________________________________________________________________________________________\n", 555 | "conv5_block3_1_relu (Activation (None, None, None, 5 0 conv5_block3_1_bn[0][0] \n", 556 | "__________________________________________________________________________________________________\n", 557 | "conv5_block3_2_conv (Conv2D) (None, None, None, 5 2359808 conv5_block3_1_relu[0][0] \n", 558 | "__________________________________________________________________________________________________\n", 559 | "conv5_block3_2_bn (BatchNormali (None, None, None, 5 2048 conv5_block3_2_conv[0][0] \n", 560 | "__________________________________________________________________________________________________\n", 561 | "conv5_block3_2_relu (Activation (None, None, None, 5 0 conv5_block3_2_bn[0][0] \n", 562 | "__________________________________________________________________________________________________\n", 563 | "conv5_block3_3_conv (Conv2D) (None, None, None, 2 1050624 conv5_block3_2_relu[0][0] \n", 564 | "__________________________________________________________________________________________________\n", 565 | "conv5_block3_3_bn (BatchNormali (None, None, None, 2 8192 conv5_block3_3_conv[0][0] \n", 566 | "__________________________________________________________________________________________________\n", 567 | "conv5_block3_add (Add) (None, None, None, 2 0 conv5_block2_out[0][0] \n", 568 | " conv5_block3_3_bn[0][0] \n", 569 | "__________________________________________________________________________________________________\n", 570 | "conv5_block3_out (Activation) (None, None, None, 2 0 conv5_block3_add[0][0] \n", 571 | "__________________________________________________________________________________________________\n", 572 | "global_average_pooling2d (Globa (None, 2048) 0 conv5_block3_out[0][0] \n", 573 | "__________________________________________________________________________________________________\n", 574 | "dense (Dense) (None, 512) 1049088 global_average_pooling2d[0][0] \n", 575 | "__________________________________________________________________________________________________\n", 576 | "fcnew (Dense) (None, 5) 2565 dense[0][0] \n", 577 | "==================================================================================================\n", 578 | "Total params: 24,639,365\n", 579 | "Trainable params: 1,051,653\n", 580 | "Non-trainable params: 23,587,712\n", 581 | "__________________________________________________________________________________________________\n" 582 | ] 583 | } 584 | ], 585 | "source": [ 586 | "# Get the model\n", 587 | "model = get_model()\n", 588 | "# Compile it\n", 589 | "model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n", 590 | "# Summary of model\n", 591 | "model.summary()" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": null, 597 | "metadata": { 598 | "colab": { 599 | "base_uri": "https://localhost:8080/" 600 | }, 601 | "id": "UF1Mo4ZEiXrT", 602 | "outputId": "eeee9a65-bbc1-4673-9e7e-6e56c480cf42" 603 | }, 604 | "outputs": [ 605 | { 606 | "name": "stdout", 607 | "output_type": "stream", 608 | "text": [ 609 | "Found 3465 images belonging to 5 classes.\n", 610 | "Found 867 images belonging to 5 classes.\n" 611 | ] 612 | } 613 | ], 614 | "source": [ 615 | "# Defining the imagedatagenerator for train and test image for pre-processing\n", 616 | "# We don't give horizonal_flip or other preprocessing for validation data generator\n", 617 | "\n", 618 | "image_size = 224\n", 619 | "batch_size = 64\n", 620 | "\n", 621 | "train_data_gen = ImageDataGenerator(preprocessing_function = preprocess_input,\n", 622 | " shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\n", 623 | "valid_data_gen = ImageDataGenerator(preprocessing_function = preprocess_input)\n", 624 | "train_generator = train_data_gen.flow_from_directory(training_dir, (image_size,image_size), batch_size=batch_size, class_mode='categorical')\n", 625 | "valid_generator = valid_data_gen.flow_from_directory(testing_dir, (image_size,image_size), batch_size=batch_size, class_mode='categorical')" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": null, 631 | "metadata": { 632 | "colab": { 633 | "background_save": true, 634 | "base_uri": "https://localhost:8080/" 635 | }, 636 | "id": "oXL9JsekiXrU", 637 | "outputId": "4ef20a20-7b23-4f1d-8c0a-b7b48628e069" 638 | }, 639 | "outputs": [ 640 | { 641 | "name": "stdout", 642 | "output_type": "stream", 643 | "text": [ 644 | "Epoch 1/5\n", 645 | "54/54 [==============================] - 686s 13s/step - loss: 1.0594 - accuracy: 0.6238 - val_loss: 0.4444 - val_accuracy: 0.8534\n", 646 | "Epoch 2/5\n", 647 | "54/54 [==============================] - 669s 12s/step - loss: 0.4384 - accuracy: 0.8394 - val_loss: 0.3503 - val_accuracy: 0.8870\n", 648 | "Epoch 3/5\n", 649 | "54/54 [==============================] - 665s 12s/step - loss: 0.3688 - accuracy: 0.8779 - val_loss: 0.3262 - val_accuracy: 0.8906\n", 650 | "Epoch 4/5\n", 651 | "54/54 [==============================] - 663s 12s/step - loss: 0.3084 - accuracy: 0.8925 - val_loss: 0.3124 - val_accuracy: 0.8942\n", 652 | "Epoch 5/5\n", 653 | "54/54 [==============================] - 679s 13s/step - loss: 0.2929 - accuracy: 0.8994 - val_loss: 0.2911 - val_accuracy: 0.8978\n" 654 | ] 655 | }, 656 | { 657 | "data": { 658 | "text/plain": [ 659 | "" 660 | ] 661 | }, 662 | "execution_count": 0, 663 | "metadata": { 664 | "tags": [] 665 | }, 666 | "output_type": "execute_result" 667 | } 668 | ], 669 | "source": [ 670 | "# Training the fully conncected layer for initial epochs\n", 671 | "epochs = 5\n", 672 | "\n", 673 | "# Training the model\n", 674 | "\n", 675 | "model.fit(\n", 676 | " train_generator,\n", 677 | " steps_per_epoch=train_generator.n//batch_size,\n", 678 | " validation_data=valid_generator,\n", 679 | " validation_steps=valid_generator.n//batch_size,\n", 680 | " epochs=epochs,\n", 681 | " verbose=1)" 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": null, 687 | "metadata": { 688 | "id": "QffyW6kviXrV", 689 | "outputId": "1ad27e47-5c8f-4c5b-a010-82234fc8f0c8" 690 | }, 691 | "outputs": [ 692 | { 693 | "name": "stdout", 694 | "output_type": "stream", 695 | "text": [ 696 | "Epoch 1/10\n", 697 | "54/54 [==============================] - 60s 1s/step - loss: 0.2540 - acc: 0.9230 - val_loss: 0.3037 - val_acc: 0.8942\n", 698 | "Epoch 2/10\n", 699 | "54/54 [==============================] - 58s 1s/step - loss: 0.2345 - acc: 0.9217 - val_loss: 0.2753 - val_acc: 0.9014\n", 700 | "Epoch 3/10\n", 701 | "54/54 [==============================] - 58s 1s/step - loss: 0.1598 - acc: 0.9537 - val_loss: 0.2552 - val_acc: 0.9135\n", 702 | "Epoch 4/10\n", 703 | "54/54 [==============================] - 57s 1s/step - loss: 0.1410 - acc: 0.9543 - val_loss: 0.2575 - val_acc: 0.9111\n", 704 | "Epoch 5/10\n", 705 | "54/54 [==============================] - 58s 1s/step - loss: 0.1146 - acc: 0.9687 - val_loss: 0.2363 - val_acc: 0.9195\n", 706 | "Epoch 6/10\n", 707 | "54/54 [==============================] - 57s 1s/step - loss: 0.1036 - acc: 0.9654 - val_loss: 0.2373 - val_acc: 0.9219\n", 708 | "Epoch 7/10\n", 709 | "54/54 [==============================] - 56s 1s/step - loss: 0.0805 - acc: 0.9797 - val_loss: 0.2345 - val_acc: 0.9171\n", 710 | "Epoch 8/10\n", 711 | "54/54 [==============================] - 56s 1s/step - loss: 0.0974 - acc: 0.9686 - val_loss: 0.2485 - val_acc: 0.9111\n", 712 | "Epoch 9/10\n", 713 | "54/54 [==============================] - 56s 1s/step - loss: 0.0643 - acc: 0.9849 - val_loss: 0.2330 - val_acc: 0.9231\n", 714 | "Epoch 10/10\n", 715 | "54/54 [==============================] - 56s 1s/step - loss: 0.0528 - acc: 0.9910 - val_loss: 0.2285 - val_acc: 0.9207\n" 716 | ] 717 | }, 718 | { 719 | "data": { 720 | "text/plain": [ 721 | "" 722 | ] 723 | }, 724 | "execution_count": 26, 725 | "metadata": { 726 | "tags": [] 727 | }, 728 | "output_type": "execute_result" 729 | } 730 | ], 731 | "source": [ 732 | "# More fine tuning the model\n", 733 | "# Training the model after 150 layers\n", 734 | "# Generally ResNet is good at extracting lower level features so we are not fine tuning initial layers\n", 735 | "epochs = 10\n", 736 | "\n", 737 | "split_at = 140\n", 738 | "for layer in model.layers[:split_at]: layer.trainable = False\n", 739 | "for layer in model.layers[split_at:]: layer.trainable = True\n", 740 | " \n", 741 | "model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])\n", 742 | "\n", 743 | "# Choosing lower learning rate for fine-tuning\n", 744 | "# learning rate is generally 10-1000 times lower than normal learning rate, if we are fine tuning the initial layers\n", 745 | "sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)\n", 746 | "\n", 747 | "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])\n", 748 | "\n", 749 | "model.fit_generator(\n", 750 | " train_generator,\n", 751 | " steps_per_epoch=train_generator.n//batch_size,\n", 752 | " validation_data=valid_generator,\n", 753 | " validation_steps=valid_generator.n//batch_size,\n", 754 | " epochs=epochs,\n", 755 | " verbose=1)" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": null, 761 | "metadata": { 762 | "id": "T7zpH3ILiXrX", 763 | "outputId": "144fad0b-cf1a-4a43-cee8-97cfc9751bc8" 764 | }, 765 | "outputs": [ 766 | { 767 | "name": "stdout", 768 | "output_type": "stream", 769 | "text": [ 770 | "Training complete\n" 771 | ] 772 | } 773 | ], 774 | "source": [ 775 | "print('Training complete')" 776 | ] 777 | }, 778 | { 779 | "cell_type": "code", 780 | "execution_count": null, 781 | "metadata": { 782 | "id": "yLjJ60DNiXrY" 783 | }, 784 | "outputs": [], 785 | "source": [] 786 | } 787 | ], 788 | "metadata": { 789 | "accelerator": "GPU", 790 | "colab": { 791 | "collapsed_sections": [], 792 | "name": "Transfer_Learning_Notebook.ipynb", 793 | "provenance": [] 794 | }, 795 | "kernelspec": { 796 | "display_name": "Python 3", 797 | "language": "python", 798 | "name": "python3" 799 | }, 800 | "language_info": { 801 | "codemirror_mode": { 802 | "name": "ipython", 803 | "version": 3 804 | }, 805 | "file_extension": ".py", 806 | "mimetype": "text/x-python", 807 | "name": "python", 808 | "nbconvert_exporter": "python", 809 | "pygments_lexer": "ipython3", 810 | "version": "3.7.4" 811 | } 812 | }, 813 | "nbformat": 4, 814 | "nbformat_minor": 1 815 | } 816 | -------------------------------------------------------------------------------- /images/img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/images/img1.png -------------------------------------------------------------------------------- /images/img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/images/img2.png -------------------------------------------------------------------------------- /images/img3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/images/img3.png -------------------------------------------------------------------------------- /images/img4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ContentUpgrad/dl_content/152d2e3bf33fd36e45a0874014155ddc5495f265/images/img4.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==0.10.0 2 | albumentations==0.5.2 3 | astunparse==1.6.3 4 | async-generator==1.10 5 | attrs==20.2.0 6 | backcall==0.2.0 7 | bleach==3.2.1 8 | cachetools==4.1.1 9 | certifi==2020.6.20 10 | chardet==3.0.4 11 | click==7.1.2 12 | cloudpickle==1.6.0 13 | cycler==0.10.0 14 | dataclasses==0.7 15 | decorator==4.4.2 16 | defusedxml==0.6.0 17 | dill==0.3.3 18 | entrypoints==0.3 19 | Flask==1.1.2 20 | future==0.18.2 21 | gast==0.3.3 22 | gensim==4.0.1 23 | google-auth==1.23.0 24 | google-auth-oauthlib==0.4.2 25 | google-pasta==0.2.0 26 | googleapis-common-protos==1.52.0 27 | graphsurgeon==0.4.5 28 | grpcio==1.33.2 29 | h5py==2.10.0 30 | horovod @ file:///opt/tensorflow/horovod-source/dist/horovod-0.20.2.tar.gz 31 | idna==2.10 32 | imageio==2.9.0 33 | imgaug==0.4.0 34 | importlib-metadata==2.0.0 35 | ipykernel==5.3.4 36 | ipython==7.16.1 37 | ipython-genutils==0.2.0 38 | ipywidgets==7.6.3 39 | itsdangerous==1.1.0 40 | jarviscloud @ git+https://github.com/jarvislabsai/jarviscloud.git@645c309fba0c54c16494545ba1ed75708f799d76 41 | jedi==0.17.2 42 | Jinja2==2.11.2 43 | joblib==1.0.1 44 | json5==0.9.5 45 | jsonschema==3.2.0 46 | jupyter-client==6.1.7 47 | jupyter-core==4.6.3 48 | jupyter-tensorboard @ git+https://github.com/lspvic/jupyter_tensorboard.git@6f1c65518820bd2dfc6cb67c758284456d8f7584 49 | jupyterlab==1.2.14 50 | jupyterlab-pygments==0.1.2 51 | jupyterlab-server==1.2.0 52 | jupyterlab-widgets==1.0.0 53 | jupytext==1.6.0 54 | Keras-Applications==1.0.8 55 | Keras-Preprocessing==1.1.1 56 | kiwisolver==1.3.1 57 | Markdown==3.3.3 58 | markdown-it-py==0.5.6 59 | MarkupSafe==1.1.1 60 | matplotlib==3.3.4 61 | mistune==0.8.4 62 | mock==3.0.5 63 | nbclient==0.5.1 64 | nbconvert==6.0.7 65 | nbformat==5.0.8 66 | nest-asyncio==1.4.2 67 | networkx==2.5.1 68 | nltk==3.4.5 69 | notebook==6.0.3 70 | numpy==1.17.3 71 | nvidia-dali-cuda110==0.27.0 72 | nvidia-dali-tf-plugin-cuda110==0.27.0 73 | nvidia-tensorboard @ file:///nvidia/opt/tensorboard_install/nvidia_tensorboard-2.3.0%2Bnv20.11-py3-none-any.whl 74 | nvidia-tensorboard-plugin-dlprof @ file:///nvidia/opt/tensorboard_install/nvidia_tensorboard_plugin_dlprof-0.9-py3-none-any.whl 75 | nvtx-plugins==0.1.8 76 | oauthlib==3.1.0 77 | opencv-python==4.5.1.48 78 | opencv-python-headless==4.5.1.48 79 | opt-einsum==3.3.0 80 | packaging==20.4 81 | pandas==1.1.5 82 | pandocfilters==1.4.3 83 | parso==0.7.1 84 | pexpect==4.7.0 85 | pickleshare==0.7.5 86 | Pillow==8.2.0 87 | polygraphy==0.20.13 88 | portpicker==1.3.1 89 | prometheus-client==0.8.0 90 | promise==2.3 91 | prompt-toolkit==3.0.8 92 | protobuf==3.13.0 93 | psutil==5.7.0 94 | ptyprocess==0.6.0 95 | pyasn1==0.4.8 96 | pyasn1-modules==0.2.8 97 | Pygments==2.7.2 98 | pygobject==3.26.1 99 | pyparsing==2.4.7 100 | pyrsistent==0.17.3 101 | python-dateutil==2.8.1 102 | python-Levenshtein==0.12.2 103 | pytz==2021.1 104 | PyWavelets==1.1.1 105 | PyYAML==5.3.1 106 | pyzmq==19.0.2 107 | requests==2.24.0 108 | requests-oauthlib==1.3.0 109 | rsa==4.6 110 | scikit-image==0.17.2 111 | scikit-learn==0.24.1 112 | scipy==1.4.1 113 | seaborn==0.11.1 114 | Send2Trash==1.5.0 115 | Shapely==1.7.1 116 | six==1.15.0 117 | smart-open==5.0.0 118 | ssh-import-id==5.7 119 | tensorboard @ file:///nvidia/opt/tensorboard_install/tensorboard-shim 120 | tensorboard-plugin-wit==1.7.0 121 | tensorflow @ file:///tmp/pip/tensorflow-2.3.1%2Bnv-cp36-cp36m-linux_x86_64.whl 122 | tensorflow-addons @ file:///opt/tensorflow/tf-addons/artifacts/tensorflow_addons-0.11.2-cp36-cp36m-linux_x86_64.whl 123 | tensorflow-datasets==3.2.1 124 | tensorflow-estimator==2.3.0 125 | tensorflow-metadata==0.25.0 126 | tensorrt==7.2.1.6 127 | termcolor==1.1.0 128 | terminado==0.9.1 129 | testpath==0.4.4 130 | threadpoolctl==2.1.0 131 | tifffile==2020.9.3 132 | toml==0.10.2 133 | tornado==6.0.4 134 | tqdm==4.51.0 135 | traitlets==4.3.3 136 | typeguard==2.10.0 137 | uff==0.6.9 138 | urllib3==1.25.11 139 | wcwidth==0.2.5 140 | webencodings==0.5.1 141 | Werkzeug==1.0.1 142 | widgetsnbextension==3.5.1 143 | wrapt==1.12.1 144 | zipp==3.4.0 145 | --------------------------------------------------------------------------------