{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/imagenet-subset/subset_dataset/ILSVRC/ImageSets/CLS-LOC/train'):\n#    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n    print(dirname)   \n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-13T00:25:35.876134Z","iopub.execute_input":"2023-07-13T00:25:35.87679Z","iopub.status.idle":"2023-07-13T00:25:35.881373Z","shell.execute_reply.started":"2023-07-13T00:25:35.876757Z","shell.execute_reply":"2023-07-13T00:25:35.880429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nimport subprocess\nfrom xml.dom import minidom\nfrom os.path import basename\nfrom bs4 import BeautifulSoup\nfrom skimage import io\nfrom glob import glob\nimport os\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:25:36.645976Z","iopub.execute_input":"2023-07-13T00:25:36.646338Z","iopub.status.idle":"2023-07-13T00:25:36.651825Z","shell.execute_reply.started":"2023-07-13T00:25:36.64631Z","shell.execute_reply":"2023-07-13T00:25:36.650871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xml_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/train'\ntrain_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train'\ntest_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test'","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:25:37.330861Z","iopub.execute_input":"2023-07-13T00:25:37.33127Z","iopub.status.idle":"2023-07-13T00:25:37.336491Z","shell.execute_reply.started":"2023-07-13T00:25:37.331241Z","shell.execute_reply":"2023-07-13T00:25:37.335667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change the current working directory to specified directory : data train dir\n\nos.chdir(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train\")","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:25:37.938684Z","iopub.execute_input":"2023-07-13T00:25:37.939053Z","iopub.status.idle":"2023-07-13T00:25:37.943969Z","shell.execute_reply.started":"2023-07-13T00:25:37.939025Z","shell.execute_reply":"2023-07-13T00:25:37.943169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to get the list of all path's names in data train directory\nsubdir= os.listdir()\nprint(subdir[:8])","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:25:38.547608Z","iopub.execute_input":"2023-07-13T00:25:38.548695Z","iopub.status.idle":"2023-07-13T00:25:38.554212Z","shell.execute_reply.started":"2023-07-13T00:25:38.548662Z","shell.execute_reply":"2023-07-13T00:25:38.55266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#xml_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/train//kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/train/n01440764/n01440764_10040.xml'\n\n#folder=os.path.basename(os.path.dirname(xml_folder)) #get the head of the tail -->n01440764\n#print(folder)\n\n\n#filername=os.path.splitext(basename(xml_folder))[0] # to get full file name --> n01440764_10040\n#print(filername)\n#jointest=os.path.join(\"/Users/Bashir/Python/tutorials\", '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test')\n#print(jointest)\n#train_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train'\n#test_folder=r'/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/test'","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:25:39.092853Z","iopub.execute_input":"2023-07-13T00:25:39.093215Z","iopub.status.idle":"2023-07-13T00:25:39.097439Z","shell.execute_reply.started":"2023-07-13T00:25:39.093188Z","shell.execute_reply":"2023-07-13T00:25:39.09664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to convert the labels from PASCAL VOC to required strings \n\ndef xmlToTxt(xml_path,f_name,f_w,f_h,f_x,f_y):\n    mydoc=minidom.parse(xml_path)    # enables you to parse XML files with python\n\n    folder=os.path.basename(os.path.dirname(xml_path))  # to get folder name like'n01440764'\n    filername=os.path.splitext(basename(xml_path))[0]   # to get file name like --> n01440764_10040\n    \n    width=float(mydoc.getElementsByTagName('width')[0].firstChild.data) # to get width of the photo from xml file format\n    height=float(mydoc.getElementsByTagName('height')[0].firstChild.data)# to get hight of the photo from xml file format\n    \n    objects=mydoc.getElementsByTagName('object')  # each xml file has <object> element which contains: \n            #<name>n01440764</name>     name of the folder\n            #<bndbox>                   bound box dimensions\n                #<xmin>64</xmin>            \n                #<ymin>103</ymin>\n                #<xmax>369</xmax>\n                #<ymax>277</ymax>\n            #</bndbox>\n    # after we get the <object>  we will access the elements inside it \n    \n    for obj in objects:\n        name=obj.getElementsByTagName('name')[0].firstChild.data\n\n        xmin=float(obj.getElementsByTagName('xmin')[0].firstChild.data)\n        ymin=float(obj.getElementsByTagName('ymin')[0].firstChild.data)\n        xmax=float(obj.getElementsByTagName('xmax')[0].firstChild.data)\n        ymax=float(obj.getElementsByTagName('ymax')[0].firstChild.data)\n        \n        \n    # we calculate the center point of the photo (x,y) and get width and height of the bbox\n        w=xmax-xmin\n        h=ymax-ymin\n        x=xmax-w/2\n        y=ymin+h/2\n        \n        folder_name.append(folder)\n        f_name.append(filername)\n        f_w.append(w)\n        f_h.append(h)\n        f_x.append(x)\n        f_y.append(y)\n        \n    return folder,f_name,f_w,f_h,f_x,f_y","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:28:06.969321Z","iopub.execute_input":"2023-07-13T00:28:06.970537Z","iopub.status.idle":"2023-07-13T00:28:06.979771Z","shell.execute_reply.started":"2023-07-13T00:28:06.97048Z","shell.execute_reply":"2023-07-13T00:28:06.978908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#to Check our Code:\n#filename='/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Annotations/CLS-LOC/train/n01531178/n01531178_368.xml'\n#filename,f_name,f_w,f_h,f_x,f_y=xmlToTxt(filename,f_name,f_w,f_h,f_x,f_y)\n#print (f_name,f_w,f_h,f_x,f_y)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:28:08.445144Z","iopub.execute_input":"2023-07-13T00:28:08.445486Z","iopub.status.idle":"2023-07-13T00:28:08.451233Z","shell.execute_reply.started":"2023-07-13T00:28:08.445459Z","shell.execute_reply":"2023-07-13T00:28:08.450094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to create a dataframe for each image class and its localization cordinates \nlabels=pd.DataFrame() # here we create a dictionary to store for each folder it's files it's details\nfolder_name=[]\nf_name=[]\nf_w=[]\nf_h=[]\nf_x=[]\nf_y=[]\n\nfor target, dirs, files in os.walk(xml_folder):\n    for file in files:\n        \n            filename,f_name,f_w,f_h,f_x,f_y=xmlToTxt(os.path.join(target, file),f_name,f_w,f_h,f_x,f_y)\n            \nlabels['Folder']=folder_name\nlabels['Name']=f_name     #pd.Series(f_name)\nlabels['width']=f_w       #pd.Series(f_w)\nlabels['height']=f_h      #pd.Series(f_h)\nlabels['X']=f_x           #pd.Series(f_x)\nlabels['Y']=f_y           #pd.Series(f_y)\n\n            ","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:28:09.359943Z","iopub.execute_input":"2023-07-13T00:28:09.360487Z","iopub.status.idle":"2023-07-13T00:42:57.849631Z","shell.execute_reply.started":"2023-07-13T00:28:09.360457Z","shell.execute_reply":"2023-07-13T00:42:57.848477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (labels)","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:43:06.26497Z","iopub.execute_input":"2023-07-13T00:43:06.265308Z","iopub.status.idle":"2023-07-13T00:43:06.275606Z","shell.execute_reply.started":"2023-07-13T00:43:06.265282Z","shell.execute_reply":"2023-07-13T00:43:06.274433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage='/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/'+labels['Folder']+'/'+labels['Name']+'.JPEG'\nimage","metadata":{"execution":{"iopub.status.busy":"2023-07-13T00:45:31.775517Z","iopub.execute_input":"2023-07-13T00:45:31.776125Z","iopub.status.idle":"2023-07-13T00:45:32.16088Z","shell.execute_reply.started":"2023-07-13T00:45:31.776093Z","shell.execute_reply":"2023-07-13T00:45:32.159529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3\nfrom keras.applications.inception_v3 import preprocess_input\nfrom keras.applications import imagenet_utils\nimport tensorflow as tf\nfrom keras import Sequential\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.optimizers import Adam,RMSprop\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = InceptionV3(weights='imagenet')\n#input_shape = (299, 299,3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception = tf.keras.applications.InceptionV3(weights='imagenet',include_top=True,input_shape=(299,299,3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception.trainable = True #Set layers to trainable\n\nlast_layer = inception.get_layer('mixed10') #get last layer in inception\n\nprint('last layer output shape: ', last_layer.output_shape)\n\nlayer_output = last_layer.output","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Flatten()(layer_output)\nx1 = Dropout(0.3)(x)\nx2 = Dense(4112,activation=\"relu\")(x1)\ndrop1 = Dropout(0.3)(x2)\nx3 = Dense(1028,activation=\"relu\")(x2)\nx4 = Dense(1028,activation=\"relu\")(x3)\ndrop1 = Dropout(0.5)(x4)\n\nx5 = Dense(256,activation=\"relu\")(drop1)\ndrop2 = Dropout(0.3)(x5)\nclassification_output = Dense(4,activation='softmax',name = 'classification')(x2)\nbounding_box_output = Dense(4,name = 'bounding_box')(x5)\nmodel = tf.keras.Model(inputs = inception.inputs, outputs = [classification_output, bounding_box_output])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  model.compile(optimizer=Adam(learning_rate=1e-4),\n              loss = {'classification' : 'categorical_crossentropy',\n                      'bounding_box' : 'mse'\n                     },\n              metrics = {'classification' : 'accuracy',\n                         'bounding_box' : 'mse'\n                        })","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}