{"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":"markdown","source":"Published on August 26, 2023. By Marília Prata, mpwolke.","metadata":{}},{"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)\nimport seaborn as sns\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\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'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:12:12.821854Z","iopub.execute_input":"2023-08-26T22:12:12.822504Z","iopub.status.idle":"2023-08-26T22:12:14.614295Z","shell.execute_reply.started":"2023-08-26T22:12:12.822473Z","shell.execute_reply":"2023-08-26T22:12:14.612195Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Competition Citation\n\n@misc{face-recognition-xt122,\n\n    author = {C.L. Tsai, Eric Li, IceLotus, ShuYuHuang, tc.lin, Wayne, wu86rrexex, Yuchi Liu},\n    \n    title = {XT122-DL-Midterm-Face Recognition},\n    \n    publisher = {Kaggle},\n    \n    year = {2023},\n    \n    url = {https://kaggle.com/competitions/face-recognition-xt122}\n}","metadata":{}},{"cell_type":"code","source":"!pip install split-folders\n!pip install visualkeras","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:12:30.182153Z","iopub.execute_input":"2023-08-26T22:12:30.182610Z","iopub.status.idle":"2023-08-26T22:12:54.119885Z","shell.execute_reply.started":"2023-08-26T22:12:30.182573Z","shell.execute_reply":"2023-08-26T22:12:54.118737Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, GlobalAveragePooling2D, Dropout, Flatten\nfrom tensorflow.keras.applications import VGG16","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:13:08.057373Z","iopub.execute_input":"2023-08-26T22:13:08.057780Z","iopub.status.idle":"2023-08-26T22:13:15.457244Z","shell.execute_reply.started":"2023-08-26T22:13:08.057748Z","shell.execute_reply":"2023-08-26T22:13:15.456240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['font.size'] = 14","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:13:19.270797Z","iopub.execute_input":"2023-08-26T22:13:19.271874Z","iopub.status.idle":"2023-08-26T22:13:19.276447Z","shell.execute_reply.started":"2023-08-26T22:13:19.271836Z","shell.execute_reply":"2023-08-26T22:13:19.275523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\ntrain_dir = '/kaggle/input/face-recognition-xt122/Train'\n#val_dir = '/kaggle/input/time-image-datasetclassification/valid' #We don't have Validation\ntest_dir = '/kaggle/input/face-recognition-xt122/Test'","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:13:31.688814Z","iopub.execute_input":"2023-08-26T22:13:31.689499Z","iopub.status.idle":"2023-08-26T22:13:31.694377Z","shell.execute_reply.started":"2023-08-26T22:13:31.689462Z","shell.execute_reply":"2023-08-26T22:13:31.693183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nbatch_size = 32\nimg_size = 224\n\n\ntrain_datagen = ImageDataGenerator(rescale=1/255.,\n                             zoom_range=0.2,\n                            width_shift_range=0.2,height_shift_range=0.2\n                             )\n\n#val_datagen = ImageDataGenerator(rescale=1/255.)\n\ntest_datagen = ImageDataGenerator(rescale=1/255.)\n\n\ntrain_generator = train_datagen.flow_from_directory(train_dir,  \n                                                target_size=(img_size, img_size), \n                                                batch_size=batch_size,\n                                                shuffle=True,\n                                                class_mode='categorical')  \n\n#validation_generator = val_datagen.flow_from_directory(val_dir,\n                                                    #target_size=(img_size, img_size),\n                                                    #batch_size=batch_size,\n                                                    #shuffle=False,\n                                                    #class_mode='categorical')\n\ntest_generator = test_datagen.flow_from_directory(test_dir,\n                                                    target_size=(img_size, img_size),\n                                                    batch_size=batch_size,\n                                                    shuffle=False,\n                                                    class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:14:02.175824Z","iopub.execute_input":"2023-08-26T22:14:02.176524Z","iopub.status.idle":"2023-08-26T22:14:02.563182Z","shell.execute_reply.started":"2023-08-26T22:14:02.176490Z","shell.execute_reply":"2023-08-26T22:14:02.562241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Above I didn't understand why Test was 0 images. It doesn't have classes but there should to be images.","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nlabels = [k for k in train_generator.class_indices]\n\n#  The above line creates a list of class labels by extracting them from the class_indices attribute of the train_generator object.\n# class_indices is a dictionary that maps class names to their integer indices.\n\n\nsample_generate = train_generator.__next__()\n\n# This line gets the next batch of data from the train_generator object.\n# train_generator is a generator object that generates batches of images and their corresponding labels from the training data.\n\n\nimages = sample_generate[0]\ntitles = sample_generate[1]\nplt.figure(figsize = (20 , 20))\n\nfor i in range(15):\n    plt.subplot(5 , 5, i+1)\n    plt.subplots_adjust(hspace = 0.3 , wspace = 0.3)\n    plt.imshow(images[i])\n    plt.title(f'Class: {labels[np.argmax(titles[i],axis=0)]}')\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:14:31.293159Z","iopub.execute_input":"2023-08-26T22:14:31.293526Z","iopub.status.idle":"2023-08-26T22:14:34.042708Z","shell.execute_reply.started":"2023-08-26T22:14:31.293496Z","shell.execute_reply":"2023-08-26T22:14:34.040407Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#VGG16 as our base model to work with:","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nimg_size = 224\nbase_model = VGG16(include_top = False,\n                       weights = 'imagenet',\n                       input_shape = (img_size,img_size,3))","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:14:59.210062Z","iopub.execute_input":"2023-08-26T22:14:59.210430Z","iopub.status.idle":"2023-08-26T22:15:04.543544Z","shell.execute_reply.started":"2023-08-26T22:14:59.210401Z","shell.execute_reply":"2023-08-26T22:15:04.542255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:15:18.943813Z","iopub.execute_input":"2023-08-26T22:15:18.944836Z","iopub.status.idle":"2023-08-26T22:15:18.986385Z","shell.execute_reply.started":"2023-08-26T22:15:18.944788Z","shell.execute_reply":"2023-08-26T22:15:18.985660Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Freezing the lower layers of the VGG model:","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nfor layer in base_model.layers[:-4]:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:15:36.438946Z","iopub.execute_input":"2023-08-26T22:15:36.439369Z","iopub.status.idle":"2023-08-26T22:15:36.447110Z","shell.execute_reply.started":"2023-08-26T22:15:36.439340Z","shell.execute_reply":"2023-08-26T22:15:36.444593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Base model","metadata":{}},{"cell_type":"code","source":"base_model.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:16:39.047939Z","iopub.execute_input":"2023-08-26T22:16:39.048354Z","iopub.status.idle":"2023-08-26T22:16:39.085807Z","shell.execute_reply.started":"2023-08-26T22:16:39.048324Z","shell.execute_reply":"2023-08-26T22:16:39.085079Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Build a new model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:16:59.639874Z","iopub.execute_input":"2023-08-26T22:16:59.640601Z","iopub.status.idle":"2023-08-26T22:16:59.646136Z","shell.execute_reply.started":"2023-08-26T22:16:59.640567Z","shell.execute_reply":"2023-08-26T22:16:59.644990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Attention to Dense: 5 classes!","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nlast_output = base_model.output\nx = GlobalAveragePooling2D()(last_output)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x) #Dropout to reduce overfitting\n\noutputs = Dense(5, activation='softmax')(x)#original Dense was 144\n\nmodel = Model(inputs=base_model.inputs, outputs=outputs)\n\nmodel.compile(optimizer=Adam(learning_rate=0.0001),\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:17:15.156455Z","iopub.execute_input":"2023-08-26T22:17:15.156820Z","iopub.status.idle":"2023-08-26T22:17:15.214379Z","shell.execute_reply.started":"2023-08-26T22:17:15.156789Z","shell.execute_reply":"2023-08-26T22:17:15.213457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:17:26.961129Z","iopub.execute_input":"2023-08-26T22:17:26.961514Z","iopub.status.idle":"2023-08-26T22:17:27.011286Z","shell.execute_reply.started":"2023-08-26T22:17:26.961485Z","shell.execute_reply":"2023-08-26T22:17:27.010553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:17:44.979267Z","iopub.execute_input":"2023-08-26T22:17:44.979635Z","iopub.status.idle":"2023-08-26T22:17:44.985113Z","shell.execute_reply.started":"2023-08-26T22:17:44.979605Z","shell.execute_reply":"2023-08-26T22:17:44.984023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nmodel_name = \"model.h5\"\ncheckpoint = ModelCheckpoint(model_name,\n                            monitor=\"val_loss\",\n                            mode=\"min\",\n                            save_best_only = True,\n                            verbose=1)\n\nearlystopping = EarlyStopping(monitor='val_loss',min_delta = 0, patience = 5, verbose = 1, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:45:19.563098Z","iopub.execute_input":"2023-08-26T22:45:19.563478Z","iopub.status.idle":"2023-08-26T22:45:19.570113Z","shell.execute_reply.started":"2023-08-26T22:45:19.563449Z","shell.execute_reply":"2023-08-26T22:45:19.569020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Epochs \n\nStarted 19:18 till 19:21","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\ntry:\n    history = model.fit(train_generator,\n                        epochs=15,\n                        validation_data=test_generator,\n                        callbacks=[checkpoint,earlystopping]\n                       )\nexcept KeyboardInterrupt:\n    print(\"\\nTraining Stopped\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-26T22:18:16.466378Z","iopub.execute_input":"2023-08-26T22:18:16.466754Z","iopub.status.idle":"2023-08-26T22:21:35.956912Z","shell.execute_reply.started":"2023-08-26T22:18:16.466723Z","shell.execute_reply":"2023-08-26T22:21:35.955814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Above, it was suppose to have: accuracy;; val_loss And val_accuracy\n\nHowever, I got only loss/accuracy","metadata":{}},{"cell_type":"code","source":"model.save(\"/kaggle/working/model.h5\")#That line didn't exist. I tried to fix the line below","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:43:13.462569Z","iopub.execute_input":"2023-08-26T22:43:13.462979Z","iopub.status.idle":"2023-08-26T22:43:13.674928Z","shell.execute_reply.started":"2023-08-26T22:43:13.462933Z","shell.execute_reply":"2023-08-26T22:43:13.673927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"/kaggle/working/model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:43:47.228151Z","iopub.execute_input":"2023-08-26T22:43:47.228595Z","iopub.status.idle":"2023-08-26T22:43:47.670583Z","shell.execute_reply.started":"2023-08-26T22:43:47.228559Z","shell.execute_reply":"2023-08-26T22:43:47.669557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Since I don't get val_loss and val_accuracy I wasn't able to make anything else.\n\nAnd have No clue how to fix it.","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nplt.figure(figsize=(10,4))\nplt.plot(history.history['loss'])\n#plt.plot(history.history['val_loss']) #I got No val_loss\nplt.title('Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epochs')\nplt.legend(['Train','Val'], loc= 'upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:50:14.915072Z","iopub.execute_input":"2023-08-26T22:50:14.915466Z","iopub.status.idle":"2023-08-26T22:50:15.194808Z","shell.execute_reply.started":"2023-08-26T22:50:14.915435Z","shell.execute_reply":"2023-08-26T22:50:15.193889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nplt.figure(figsize=(10,3))\nplt.plot(history.history['accuracy'])\n#plt.plot(history.history['val_accuracy'])#I got No Val_aacuracy\nplt.title('Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['Train', 'Val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:50:25.848175Z","iopub.execute_input":"2023-08-26T22:50:25.848565Z","iopub.status.idle":"2023-08-26T22:50:26.129003Z","shell.execute_reply.started":"2023-08-26T22:50:25.848534Z","shell.execute_reply":"2023-08-26T22:50:26.127885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Evaluating model performance based on predictions:","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\n#y_test = test_generator.classes\n#y_pred = model.predict(test_generator)\n#y_pred = np.argmax(y_pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:51:18.624789Z","iopub.execute_input":"2023-08-26T22:51:18.625166Z","iopub.status.idle":"2023-08-26T22:51:18.629764Z","shell.execute_reply.started":"2023-08-26T22:51:18.625135Z","shell.execute_reply":"2023-08-26T22:51:18.628560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Classification Report:","metadata":{}},{"cell_type":"code","source":"#By Avantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:\n\nfrom sklearn.metrics import classification_report\n\nprint(classification_report(y_test,y_pred,target_names = labels))","metadata":{"execution":{"iopub.status.busy":"2023-08-26T22:32:46.900688Z","iopub.execute_input":"2023-08-26T22:32:46.901077Z","iopub.status.idle":"2023-08-26T22:32:46.905148Z","shell.execute_reply.started":"2023-08-26T22:32:46.901045Z","shell.execute_reply":"2023-08-26T22:32:46.904179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nAvantika https://www.kaggle.com/code/avantikab/time-image-classification-with-vgg16#Callbacks:","metadata":{}}]}