{"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":"#Install dependecies\n\nimport math, re, os\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport skimage\nimport skimage.io\n\nfrom  sklearn.model_selection import train_test_split\nfrom keras.utils import load_img, img_to_array, array_to_img\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tqdm import tqdm\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow import keras\nfrom tensorflow.keras.optimizers import Adam\nfrom functools import partial\n\n\nprint(\"Tensorflow version \" + tf.__version__)\n\nimport random\nfrom glob import glob\nfrom tensorflow.keras.optimizers import Adam\nimport keras\nfrom keras.models import *\nfrom keras import layers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.applications.vgg16 import preprocess_input\n\nfrom keras.applications.vgg16 import VGG16\nfrom IPython.display import display\nfrom PIL import Image","metadata":{"papermill":{"duration":9.376517,"end_time":"2023-05-16T13:25:42.306887","exception":false,"start_time":"2023-05-16T13:25:32.93037","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-05T13:41:05.052609Z","iopub.execute_input":"2023-06-05T13:41:05.053355Z","iopub.status.idle":"2023-06-05T13:41:05.064604Z","shell.execute_reply.started":"2023-06-05T13:41:05.053321Z","shell.execute_reply":"2023-06-05T13:41:05.063666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapping_path = '/kaggle/input/imagenet-object-localization-challenge/LOC_synset_mapping.txt' \nsrc_path_train = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train'\nsrc_path_test = '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test'","metadata":{"papermill":{"duration":0.013239,"end_time":"2023-05-16T13:25:42.324373","exception":false,"start_time":"2023-05-16T13:25:42.311134","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-05T13:41:11.753837Z","iopub.execute_input":"2023-06-05T13:41:11.754204Z","iopub.status.idle":"2023-06-05T13:41:11.758732Z","shell.execute_reply.started":"2023-06-05T13:41:11.754176Z","shell.execute_reply":"2023-06-05T13:41:11.757836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creation of mapping dictionaries to obtain the image classes\n\nclass_mapping_dict = {}\nclass_mapping_dict_number = {}\nmapping_class_to_number = {}\nmapping_number_to_class = {}\ni = 0\nfor line in open(mapping_path):\n    class_mapping_dict[line[:9].strip()] = line[9:].strip()\n    class_mapping_dict_number[i] = line[9:].strip()\n    mapping_class_to_number[line[:9].strip()] = i\n    mapping_number_to_class[i] = line[:9].strip()\n    i+=1\n    \n#print(class_mapping_dict)\n#print(class_mapping_dict_number)\n#print(mapping_class_to_number)\n#print(mapping_number_to_class)","metadata":{"papermill":{"duration":0.028378,"end_time":"2023-05-16T13:25:42.35661","exception":false,"start_time":"2023-05-16T13:25:42.328232","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-05T13:41:17.057053Z","iopub.execute_input":"2023-06-05T13:41:17.057433Z","iopub.status.idle":"2023-06-05T13:41:17.080221Z","shell.execute_reply.started":"2023-06-05T13:41:17.057403Z","shell.execute_reply":"2023-06-05T13:41:17.079099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creation of dataset_array and CLASSES\n\nCLASSES = []\nimages_array = []\nfor train_class in tqdm(os.listdir(src_path_train)):\n    i = 0\n    for el in os.listdir(src_path_train + '/' + train_class):\n        if i < 10:\n            path = src_path_train + '/' + train_class + '/' + el\n            image = load_img(path,target_size=(224,224,3))\n            image_array = img_to_array(image).astype(np.uint8)\n            images_array.append(image_array)\n            CLASS = class_mapping_dict[path.split('/')[-2]]\n            CLASSES.append(CLASS)\n            i+=1\n        else:\n            break\nimages_array = np.array(images_array)\nCLASSES = np.array(CLASSES)","metadata":{"papermill":{"duration":354.194007,"end_time":"2023-05-16T13:31:36.554583","exception":false,"start_time":"2023-05-16T13:25:42.360576","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-05T13:41:29.952413Z","iopub.execute_input":"2023-06-05T13:41:29.952768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nepochs = 100","metadata":{"papermill":{"duration":0.086513,"end_time":"2023-05-16T13:31:36.720194","exception":false,"start_time":"2023-05-16T13:31:36.633681","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creation of the train_generator and the test_generator\n\nimage_gen = ImageDataGenerator(\n    #rescale=1 / 255.0,\n    #rotation_range=20,\n    #zoom_range=0.05,\n    #width_shift_range=0.05,\n    #height_shift_range=0.05,\n    #shear_range=0.05,\n    #horizontal_flip=True,\n    #fill_mode=\"nearest\",\n    preprocessing_function = preprocess_input,\n    validation_split=0.20)\n\ntrain_generator = image_gen.flow_from_directory(\n  src_path_train,\n  target_size=(224,224),\n  shuffle=True,\n  batch_size=batch_size,\n  subset=\"training\",\n  class_mode=\"sparse\" \n)\n\ntest_generator = image_gen.flow_from_directory(\n  src_path_train,\n  target_size=(224,224),\n  shuffle=True,\n  batch_size=batch_size,\n  subset=\"validation\",\n  class_mode=\"sparse\"\n)\n","metadata":{"papermill":{"duration":1588.456251,"end_time":"2023-05-16T13:58:05.253668","exception":false,"start_time":"2023-05-16T13:31:36.797417","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_scheduler = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=1e-3, \n    decay_steps=10000, \n    decay_rate=0.9)","metadata":{"papermill":{"duration":0.083794,"end_time":"2023-05-16T13:58:05.413737","exception":false,"start_time":"2023-05-16T13:58:05.329943","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Input, Conv2D, BatchNormalization, ReLU, Concatenate, GlobalAveragePooling2D, Dense\nfrom tensorflow.keras.models import Model\n\ndef conv_block(x, growth_rate):\n    x1 = BatchNormalization()(x)\n    x1 = ReLU()(x1)\n    x1 = Conv2D(filters=growth_rate, kernel_size=(3, 3), padding='same')(x1)\n    x = Concatenate()([x, x1])\n    return x\n\ndef dense_block(x, num_layers, growth_rate):\n    for _ in range(num_layers):\n        x = conv_block(x, growth_rate)\n    return x\n\ndef transition_block(x, reduction):\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    x = Conv2D(int(tf.keras.backend.int_shape(x)[-1] * reduction), kernel_size=(1, 1), padding='same')(x)\n    x = tf.keras.layers.AveragePooling2D((2, 2), strides=(2, 2))(x)\n    return x\n\ndef CustomNet121(input_shape=(224, 224, 3), num_classes=1000, growth_rate=32, num_blocks=[6, 12, 24, 16], reduction=0.5):\n    inputs = Input(shape=input_shape)\n    x = Conv2D(64, kernel_size=(7, 7), strides=(2, 2), padding='same')(inputs)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    x = tf.keras.layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)\n\n    num_features = 64\n    for i, num_layers in enumerate(num_blocks):\n        x = dense_block(x, num_layers, growth_rate)\n        num_features += num_layers * growth_rate\n        if i != len(num_blocks) - 1:\n            x = transition_block(x, reduction)\n\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(num_classes, activation='softmax')(x)\n\n    model = Model(inputs, x, name='CustomNet-121')\n    return model\nmodel = CustomNet121(input_shape = (224,224,3))\nmodel.compile(optimizer=Adam(learning_rate=lr_scheduler, epsilon=0.001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nmodel.summary()","metadata":{"papermill":{"duration":4.956586,"end_time":"2023-05-16T13:58:10.446981","exception":false,"start_time":"2023-05-16T13:58:05.490395","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"../input/custom-net-imagenet-weights/den121.h5\"  # Replace with the actual path to the weights file\nmodel.load_weights(weights_path)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(\n    min_delta=0.001, # minimium amount of change to count as an improvement\n    patience=10, # how many epochs to wait before stopping\n)\n\n\nhistory = model.fit(\n  train_generator,\n  validation_data=test_generator,\n  epochs=20,\n  steps_per_epoch=50,\n  validation_steps=50,\n  callbacks=[early_stop]\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['sparse_categorical_accuracy', 'val_sparse_categorical_accuracy']].plot();\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(\n    min_delta=0.001, # minimium amount of change to count as an improvement\n    patience=10, # how many epochs to wait before stopping\n)\n\n\nhistory = model.fit(\n  train_generator,\n  validation_data=test_generator,\n  epochs=epochs,\n  steps_per_epoch=len(train_generator) // batch_size,\n  validation_steps=len(test_generator) // batch_size,\n  callbacks=[early_stop]\n)","metadata":{"papermill":{"duration":13811.365566,"end_time":"2023-05-16T17:48:21.896792","exception":false,"start_time":"2023-05-16T13:58:10.531226","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create learning curves to evaluate model performance\nhistory_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['sparse_categorical_accuracy', 'val_sparse_categorical_accuracy']].plot();\n\n","metadata":{"papermill":{"duration":1.35853,"end_time":"2023-05-16T17:48:23.821421","exception":false,"start_time":"2023-05-16T17:48:22.462891","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('den121.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}