{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator,load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-10T06:19:33.855616Z","iopub.execute_input":"2022-05-10T06:19:33.856179Z","iopub.status.idle":"2022-05-10T06:19:33.861537Z","shell.execute_reply.started":"2022-05-10T06:19:33.856143Z","shell.execute_reply":"2022-05-10T06:19:33.860729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:33.900463Z","iopub.execute_input":"2022-05-10T06:19:33.900755Z","iopub.status.idle":"2022-05-10T06:19:33.919831Z","shell.execute_reply.started":"2022-05-10T06:19:33.900726Z","shell.execute_reply":"2022-05-10T06:19:33.919225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(labels)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:33.921267Z","iopub.execute_input":"2022-05-10T06:19:33.921981Z","iopub.status.idle":"2022-05-10T06:19:33.928056Z","shell.execute_reply.started":"2022-05-10T06:19:33.921945Z","shell.execute_reply":"2022-05-10T06:19:33.927086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.id = labels.id.astype('str') + '.jpg'","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:33.964519Z","iopub.execute_input":"2022-05-10T06:19:33.964801Z","iopub.status.idle":"2022-05-10T06:19:33.971172Z","shell.execute_reply.started":"2022-05-10T06:19:33.964773Z","shell.execute_reply":"2022-05-10T06:19:33.970419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_val_test= train_test_split(labels, test_size=0.2, random_state=42)\ndf_test, df_val= train_test_split(df_val_test, test_size=0.5, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:34.043648Z","iopub.execute_input":"2022-05-10T06:19:34.044006Z","iopub.status.idle":"2022-05-10T06:19:34.054572Z","shell.execute_reply.started":"2022-05-10T06:19:34.043964Z","shell.execute_reply":"2022-05-10T06:19:34.053530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image_Width=224\nImage_Height=224\nImage_Size=(Image_Width,Image_Height)\nImage_Channels=3\nbatch_size=8","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:34.068543Z","iopub.execute_input":"2022-05-10T06:19:34.068828Z","iopub.status.idle":"2022-05-10T06:19:34.072558Z","shell.execute_reply.started":"2022-05-10T06:19:34.068802Z","shell.execute_reply":"2022-05-10T06:19:34.071863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../input/dog-breed-identification\ntrain_datagen = ImageDataGenerator(rescale=1./255,width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, fill_mode=\"nearest\")\ntrain_generator = train_datagen.flow_from_dataframe(df_train, directory=\"./train/\",x_col='id',y_col='breed', target_size=Image_Size, class_mode='categorical', batch_size=batch_size,)\n\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(df_val,  \"./train/\",x_col='id',y_col='breed',target_size=Image_Size, class_mode='categorical', batch_size=batch_size )\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_dataframe(df_test,  \"./train/\",x_col='id',y_col='breed',target_size=Image_Size, class_mode='categorical', batch_size=batch_size )","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:34.124196Z","iopub.execute_input":"2022-05-10T06:19:34.124389Z","iopub.status.idle":"2022-05-10T06:19:38.314835Z","shell.execute_reply.started":"2022-05-10T06:19:34.124359Z","shell.execute_reply":"2022-05-10T06:19:38.314082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import the necessary packages\nfrom tensorflow.keras.layers import BatchNormalization\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import AveragePooling2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.layers.convolutional import ZeroPadding2D\nfrom keras.layers.core import Activation\nfrom keras.layers.core import Dense\nfrom keras.layers import Flatten\nfrom keras.layers import Input\nfrom keras.models import Model\nfrom keras.layers import add\nfrom keras.regularizers import l2\nfrom keras import backend as K\n\n\nclass ResNet:\n    @staticmethod\n\n    def residual_module(data, K, stride, chanDim, red=False, reg=0.0001, bnEps=2e-5, bnMom=0.9):\n        # the shortcut branch of the ResNet module should be initialize as the input (identity) data\n        shortcut = data\n        # the first block of the ResNet module are the 1x1 CONVs\n        bn1 = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(data)\n        act1 = Activation(\"relu\")(bn1)\n        conv1 = Conv2D(int(K * 0.25), (1, 1), use_bias=False, kernel_regularizer=l2(reg))(act1)\n\n        # the second block of the ResNet module are the 3x3 CONVs\n        bn2 = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(conv1)\n        act2 = Activation(\"relu\")(bn2)\n        conv2 = Conv2D(int(K * 0.25), (3, 3), strides=stride, padding=\"same\",\n                       use_bias=False, kernel_regularizer=l2(reg))(act2)\n\n        # the third block of the ResNet module is another set of 1x1 CONVs\n        bn3 = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(conv2)\n        act3 = Activation(\"relu\")(bn3)\n        conv3 = Conv2D(K, (1, 1), use_bias=False,\n                       kernel_regularizer=l2(reg))(act3)\n\n        # if we are to reduce the spatial size, apply a CONV layer to the shortcut\n        if red:\n            shortcut = Conv2D(K, (1, 1), strides=stride, use_bias=False,\n                              kernel_regularizer=l2(reg))(act1)\n\n        # add together the shortcut and the final CONV\n        x = add([conv3, shortcut])\n\n        # return the addition as the output of the ResNet module\n        return x\n\n    @staticmethod\n    def build(width, height, depth, classes, stages, filters, reg=0.0001,\n              bnEps=2e-5, bnMom=0.9, dataset=\"cifar\"):\n\n        # initialize the input shape to be \"channels last\" and the channels dimension itself\n        inputShape = (height, width, depth)\n        chanDim = -1\n\n        # if we are using \"channels first\", update the input shape\n        # and channels dimension\n        if K.image_data_format() == \"channels_first\":\n            inputShape = (depth, height, width)\n            chanDim = 1\n\n        # set the input and apply BN\n        inputs = Input(shape=inputShape)\n        x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum = bnMom)(inputs)\n\n        # check if we are utilizing the CIFAR dataset\n        if dataset == \"cifar\":\n            # apply a single CONV layer\n            x = Conv2D(filters[0], (3, 3), use_bias=False, padding = \"same\",\n                       kernel_regularizer = l2(reg))(x)\n\n        # loop over the number of stages\n        for i in range(0, len(stages)):\n        # initialize the stride, then apply a residual module\n        # used to reduce the spatial size of the input volume\n            stride = (1, 1) if i == 0 else (2, 2)\n            x = ResNet.residual_module(x, filters[i + 1], stride, chanDim,\n                                       red = True, bnEps = bnEps, bnMom = bnMom)\n\n            # loop over the number of layers in the stage\n            for j in range(0, stages[i] - 1):\n            # apply a ResNet module\n                x = ResNet.residual_module(x, filters[i + 1],(1, 1), chanDim, bnEps=bnEps, bnMom=bnMom)\n\n        # apply BN => ACT => POOL\n        x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum = bnMom)(x)\n        x = Activation(\"relu\")(x)\n        x = AveragePooling2D((8, 8))(x)\n\n        # softmax classifier\n        x = Flatten()(x)\n        x = Dense(classes, kernel_regularizer=l2(reg))(x)\n        x = Activation(\"softmax\")(x)\n\n        # create the model\n        model = Model(inputs, x, name=\"resnet\")\n\n        # return the constructed network architecture\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:38.316794Z","iopub.execute_input":"2022-05-10T06:19:38.317360Z","iopub.status.idle":"2022-05-10T06:19:38.339905Z","shell.execute_reply.started":"2022-05-10T06:19:38.317322Z","shell.execute_reply":"2022-05-10T06:19:38.339170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import SGD\nfrom keras.datasets import cifar10\nfrom keras.models import load_model\nimport keras.backend as K\nimport numpy as np\nimport argparse\nimport sys\nfrom sklearn.preprocessing import LabelBinarizer\nimport matplotlib\nmatplotlib.use(\"Agg\")\nprint(\"[INFO] compiling model...\")\nopt = keras.optimizers.SGD()\nmodel = ResNet.build(224, 224, 3, 120, (9, 9, 9),(64, 64, 128, 256), reg=0.0005)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=opt, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:38.341380Z","iopub.execute_input":"2022-05-10T06:19:38.341917Z","iopub.status.idle":"2022-05-10T06:19:40.902103Z","shell.execute_reply.started":"2022-05-10T06:19:38.341871Z","shell.execute_reply":"2022-05-10T06:19:40.901381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install GPUtil\n\nimport torch\nfrom GPUtil import showUtilization as gpu_usage\nfrom numba import cuda","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:40.903976Z","iopub.execute_input":"2022-05-10T06:19:40.904272Z","iopub.status.idle":"2022-05-10T06:19:49.640706Z","shell.execute_reply.started":"2022-05-10T06:19:40.904232Z","shell.execute_reply":"2022-05-10T06:19:49.639829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import LearningRateScheduler, ModelCheckpoint\ncheckpoint=ModelCheckpoint('/kaggle/working/model1.h5',\n                           monitor=\"val_accuracy\",\n                           verbose=0,\n                           save_best_only=True,\n                           save_weights_only=False,\n                           mode=\"auto\",\n                           save_freq=\"epoch\")\n\n\ndef step_decay(epoch):\n    # initialize the base initial learning rate, drop factor, and\n    # epochs to drop every\n    initAlpha = 0.2\n    decay_rate = 0.005\n\n    # compute learning rate for the current epoch\n    alpha = initAlpha * 1/(1 + decay_rate * epoch)\n\n    # return the learning rate\n    return float(alpha)\n\n\ncallbacks = [LearningRateScheduler(step_decay),checkpoint]","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:49.643559Z","iopub.execute_input":"2022-05-10T06:19:49.643844Z","iopub.status.idle":"2022-05-10T06:19:49.652947Z","shell.execute_reply.started":"2022-05-10T06:19:49.643803Z","shell.execute_reply":"2022-05-10T06:19:49.652167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def free_gpu_cache():\n    print(\"Initial GPU Usage\")\n    gpu_usage()                             \n\n    torch.cuda.empty_cache()\n\n    cuda.select_device(0)\n    cuda.close()\n    cuda.select_device(0)\n\n    print(\"GPU Usage after emptying the cache\")\n    gpu_usage()","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:49.656414Z","iopub.execute_input":"2022-05-10T06:19:49.656625Z","iopub.status.idle":"2022-05-10T06:19:49.664334Z","shell.execute_reply.started":"2022-05-10T06:19:49.656592Z","shell.execute_reply":"2022-05-10T06:19:49.663518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 64\nbatch_size=8\nhistory = model.fit(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=len(df_val)//batch_size,\n    steps_per_epoch=len(df_train)//batch_size,\n    callbacks=callbacks\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:19:49.666444Z","iopub.execute_input":"2022-05-10T06:19:49.667078Z","iopub.status.idle":"2022-05-10T06:19:49.729761Z","shell.execute_reply.started":"2022-05-10T06:19:49.667017Z","shell.execute_reply":"2022-05-10T06:19:49.728614Z"},"trusted":true},"execution_count":null,"outputs":[]}]}