{"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":"#importing modules \nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport cv2\nimport imghdr\nimport os\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.layers import Dense,Flatten\nfrom keras.applications.inception_v3 import InceptionV3,preprocess_input\nimport keras\n#nception v3 is an image recognition model that has been shown to attain greater than\n#78.1% accuracy on the ImageNet dataset. The model is the culmination of\n#many ideas developed by multiple researchers over the years.\nbase_model=InceptionV3(input_shape=(256,256,3),include_top=False)\nfor layer in base_model.layers:\n    layer.trainable=False\n#flatten layers converts a 2d array of image into a 1d array\nx=Flatten()(base_model.output)\n#the output function \nx=Dense(units=6,activation='sigmoid')(x)\nmodel=Model(base_model.input,x)\nmodel.compile(optimizer='adam',loss=keras.losses.binary_crossentropy,metrics=['accuracy'])\nprint(model.summary())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-10T17:03:37.531350Z","iopub.execute_input":"2022-11-10T17:03:37.531721Z","iopub.status.idle":"2022-11-10T17:03:50.981571Z","shell.execute_reply.started":"2022-11-10T17:03:37.531686Z","shell.execute_reply":"2022-11-10T17:03:50.979906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image data generator taked the image dataset and transforms it making it more suitable for the model \ntrain_datagen=ImageDataGenerator(featurewise_center=True,rotation_range=0.05,width_shift_range=0.05,horizontal_flip=True,preprocessing_function=preprocess_input,zoom_range=0.1,shear_range=0.1)\ntrain_data=train_datagen.flow_from_directory(directory='../input/finding-disney/train',target_size=(256,256),batch_size=50)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:04:26.975889Z","iopub.execute_input":"2022-11-10T17:04:26.976626Z","iopub.status.idle":"2022-11-10T17:04:27.191178Z","shell.execute_reply.started":"2022-11-10T17:04:26.976588Z","shell.execute_reply":"2022-11-10T17:04:27.190167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the dictionary representing the classes with thier numeric notations \ntrain_data.class_indices","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:04:29.150381Z","iopub.execute_input":"2022-11-10T17:04:29.150752Z","iopub.status.idle":"2022-11-10T17:04:29.157950Z","shell.execute_reply.started":"2022-11-10T17:04:29.150720Z","shell.execute_reply":"2022-11-10T17:04:29.156981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint,EarlyStopping\nmc=ModelCheckpoint(filepath='./best_model.h5',monitor='val_accuracy',verbose=1,save_best_only=True)\nes=EarlyStopping(monitor='val_accuracy',min_delta=0.01,patience=5,verbose=1)\ncb=[mc,es]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:04:33.085497Z","iopub.execute_input":"2022-11-10T17:04:33.085852Z","iopub.status.idle":"2022-11-10T17:04:33.092007Z","shell.execute_reply.started":"2022-11-10T17:04:33.085821Z","shell.execute_reply":"2022-11-10T17:04:33.090576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True\nhis=model.fit_generator(train_data,steps_per_epoch=20,epochs=20,callbacks=cb)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:06:44.635388Z","iopub.execute_input":"2022-11-10T17:06:44.635761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import load_img,img_to_array\n#importing the test folder\ntest_path='../input/finding-disney/test'\n#array of the \nkey_list = list(train_data.class_indices.keys())\nval_list = list(train_data.class_indices.values())\ndict_name=[]\ndict_path=[]\nfor img_path in os.listdir(test_path):\n    path =os.path.join(test_path,img_path)\n    #keeping image size (256,256) so that it can be compatible with the model\n    img=load_img(path,target_size=(256,256))\n    #converts image to numeric data\n    i=img_to_array(img)\n    i=preprocess_input(i)\n    pres=np.argmax(model.predict(np.array([i])))\n    position = val_list.index(pres)\n    dict_name.append(key_list[position])\n    dict_path.append(int(img_path.split('.')[0]))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T15:48:46.373840Z","iopub.execute_input":"2022-11-10T15:48:46.374210Z","iopub.status.idle":"2022-11-10T15:49:27.767568Z","shell.execute_reply.started":"2022-11-10T15:48:46.374179Z","shell.execute_reply":"2022-11-10T15:49:27.766568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nfilename='disney_records.csv'\nwith open(filename, 'w') as csvfile: \n    csvwriter = csv.writer(csvfile) \n    csvwriter.writerow(['character_id','label'])\n    for i in range(len(dict_path)):\n        csvwriter.writerow([dict_path[i],dict_name[i]])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T15:50:06.814083Z","iopub.execute_input":"2022-11-10T15:50:06.815005Z","iopub.status.idle":"2022-11-10T15:50:06.821489Z","shell.execute_reply.started":"2022-11-10T15:50:06.814971Z","shell.execute_reply":"2022-11-10T15:50:06.820440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(filename)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T15:49:48.695185Z","iopub.execute_input":"2022-11-10T15:49:48.695766Z","iopub.status.idle":"2022-11-10T15:49:48.719613Z","shell.execute_reply.started":"2022-11-10T15:49:48.695719Z","shell.execute_reply":"2022-11-10T15:49:48.718770Z"},"trusted":true},"execution_count":null,"outputs":[]}]}