{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.10"},"papermill":{"default_parameters":{},"duration":15451.579111,"end_time":"2023-06-05T11:06:01.423345","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-06-05T06:48:29.844234","version":"2.4.0"}},"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":{"execution":{"iopub.execute_input":"2023-06-05T06:48:40.139473Z","iopub.status.busy":"2023-06-05T06:48:40.138959Z","iopub.status.idle":"2023-06-05T06:48:49.342256Z","shell.execute_reply":"2023-06-05T06:48:49.340455Z"},"papermill":{"duration":9.21069,"end_time":"2023-06-05T06:48:49.344532","exception":false,"start_time":"2023-06-05T06:48:40.133842","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-05T06:48:49.352798Z","iopub.status.busy":"2023-06-05T06:48:49.352211Z","iopub.status.idle":"2023-06-05T06:48:49.356863Z","shell.execute_reply":"2023-06-05T06:48:49.355967Z"},"papermill":{"duration":0.010868,"end_time":"2023-06-05T06:48:49.358892","exception":false,"start_time":"2023-06-05T06:48:49.348024","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-05T06:48:49.366591Z","iopub.status.busy":"2023-06-05T06:48:49.366325Z","iopub.status.idle":"2023-06-05T06:48:49.382674Z","shell.execute_reply":"2023-06-05T06:48:49.381866Z"},"papermill":{"duration":0.022446,"end_time":"2023-06-05T06:48:49.384653","exception":false,"start_time":"2023-06-05T06:48:49.362207","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-05T06:48:49.392746Z","iopub.status.busy":"2023-06-05T06:48:49.392022Z","iopub.status.idle":"2023-06-05T06:54:57.020268Z","shell.execute_reply":"2023-06-05T06:54:57.019297Z"},"papermill":{"duration":367.634783,"end_time":"2023-06-05T06:54:57.022734","exception":false,"start_time":"2023-06-05T06:48:49.387951","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nepochs = 200","metadata":{"execution":{"iopub.execute_input":"2023-06-05T06:54:57.173917Z","iopub.status.busy":"2023-06-05T06:54:57.172354Z","iopub.status.idle":"2023-06-05T06:54:57.177877Z","shell.execute_reply":"2023-06-05T06:54:57.177045Z"},"papermill":{"duration":0.082283,"end_time":"2023-06-05T06:54:57.179788","exception":false,"start_time":"2023-06-05T06:54:57.097505","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-05T06:54:57.330261Z","iopub.status.busy":"2023-06-05T06:54:57.329464Z","iopub.status.idle":"2023-06-05T07:21:05.985993Z","shell.execute_reply":"2023-06-05T07:21:05.98505Z"},"papermill":{"duration":1568.734694,"end_time":"2023-06-05T07:21:05.988382","exception":false,"start_time":"2023-06-05T06:54:57.253688","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_scheduler = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=1e-5, \n    decay_steps=10000, \n    decay_rate=0.9)","metadata":{"execution":{"iopub.execute_input":"2023-06-05T07:21:06.138299Z","iopub.status.busy":"2023-06-05T07:21:06.137978Z","iopub.status.idle":"2023-06-05T07:21:06.143662Z","shell.execute_reply":"2023-06-05T07:21:06.14284Z"},"papermill":{"duration":0.082469,"end_time":"2023-06-05T07:21:06.145697","exception":false,"start_time":"2023-06-05T07:21:06.063228","status":"completed"},"tags":[]},"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=1e-4, epsilon=0.001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2023-06-05T07:21:06.294903Z","iopub.status.busy":"2023-06-05T07:21:06.294627Z","iopub.status.idle":"2023-06-05T07:21:10.72836Z","shell.execute_reply":"2023-06-05T07:21:10.727508Z"},"papermill":{"duration":4.863836,"end_time":"2023-06-05T07:21:11.08325","exception":false,"start_time":"2023-06-05T07:21:06.219414","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_lrfn(lr_start=0.000002, lr_max=0.00010,\nlr_min=0, lr_rampup_epochs=8,\nlr_sustain_epochs=0, lr_exp_decay=.8):\ndef lrfn(epoch):\nif epoch < lr_rampup_epochs:\nlr = (lr_max - lr_start) / lr_rampup_epochs * epoch + lr_start\nelif epoch < lr_rampup_epochs + lr_sustain_epochs:\nlr = lr_max\nelse:\nlr = (lr_max - lr_min) *\\\nlr_exp_decay**(epoch - lr_rampup_epochs\\\n- lr_sustain_epochs) + lr_min\nreturn lr\nreturn lrfn\nlrfn = build_lrfn()\nlr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator,\nvalidation_data=valid_generator,\nsteps_per_epoch=len(train_generator),\nvalidation_steps=len(valid_generator),\nepochs = EPOCHS,\ncallbacks=[lr_schedule]\n)","metadata":{"execution":{"iopub.execute_input":"2023-06-05T07:21:11.374733Z","iopub.status.busy":"2023-06-05T07:21:11.374372Z","iopub.status.idle":"2023-06-05T11:05:53.496535Z","shell.execute_reply":"2023-06-05T11:05:53.49437Z"},"papermill":{"duration":13482.250496,"end_time":"2023-06-05T11:05:53.502815","exception":false,"start_time":"2023-06-05T07:21:11.252319","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-05T11:05:54.717803Z","iopub.status.busy":"2023-06-05T11:05:54.717365Z","iopub.status.idle":"2023-06-05T11:05:55.660167Z","shell.execute_reply":"2023-06-05T11:05:55.659286Z"},"papermill":{"duration":1.555502,"end_time":"2023-06-05T11:05:55.662777","exception":false,"start_time":"2023-06-05T11:05:54.107275","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('den121.h5')","metadata":{"execution":{"iopub.execute_input":"2023-06-05T11:05:57.190545Z","iopub.status.busy":"2023-06-05T11:05:57.190179Z","iopub.status.idle":"2023-06-05T11:05:57.7513Z","shell.execute_reply":"2023-06-05T11:05:57.750309Z"},"papermill":{"duration":1.262634,"end_time":"2023-06-05T11:05:57.754182","exception":false,"start_time":"2023-06-05T11:05:56.491548","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}],"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"}}