{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport os \nimport zipfile\nfrom tqdm import tqdm\nfrom glob import glob\nfrom shutil import copy, move","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-17T16:10:26.982625Z","iopub.execute_input":"2021-11-17T16:10:26.983041Z","iopub.status.idle":"2021-11-17T16:10:31.288324Z","shell.execute_reply.started":"2021-11-17T16:10:26.982947Z","shell.execute_reply":"2021-11-17T16:10:31.287487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"code","source":"a_train_dir = './training/daisy'\na_val_dir = './validation/daisy'\nb_train_dir = './training/dendelion'\nb_val_dir = './validation/dendelion'\nc_train_dir = './training/rose'\nc_val_dir = './validation/rose'\nd_train_dir = './training/sunflower'\nd_val_dir = './validation/sunflower'\ne_train_dir = './training/tulip'\ne_val_dir = './validation/tulip'\n\n\nfile_dirs = [a_train_dir, a_val_dir,  b_train_dir, b_val_dir, c_train_dir, c_val_dir\n        ,d_train_dir, d_val_dir, e_train_dir, e_val_dir]\n\nfor dir in file_dirs:\n    os.makedirs(dir,exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:10:34.479826Z","iopub.execute_input":"2021-11-17T16:10:34.48015Z","iopub.status.idle":"2021-11-17T16:10:34.489676Z","shell.execute_reply.started":"2021-11-17T16:10:34.480122Z","shell.execute_reply":"2021-11-17T16:10:34.488877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_1 = glob('../input/flowers-recognition/flowers/daisy/*jpg')\nall_2 = glob('../input/flowers-recognition/flowers/dandelion/*jpg')\nall_3 = glob('../input/flowers-recognition/flowers/rose/*jpg')\nall_4 = glob('../input/flowers-recognition/flowers/sunflower/*jpg')\nall_5 = glob('../input/flowers-recognition/flowers/tulip/*jpg')\n\nprint(len(all_1))\nprint(len(all_2))\nprint(len(all_3))\nprint(len(all_4))\nprint(len(all_5))","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:10:37.17945Z","iopub.execute_input":"2021-11-17T16:10:37.179826Z","iopub.status.idle":"2021-11-17T16:10:38.062252Z","shell.execute_reply.started":"2021-11-17T16:10:37.179791Z","shell.execute_reply":"2021-11-17T16:10:38.061419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\na_train, a_test = train_test_split(all_1, test_size=0.2, shuffle=True,random_state = 100)\nb_train, b_test = train_test_split(all_2, test_size=0.2, shuffle=True,random_state = 100)\nc_train, c_test = train_test_split(all_3, test_size=0.2, shuffle=True,random_state = 100)\nd_train, d_test = train_test_split(all_4, test_size=0.2, shuffle=True,random_state = 100)\ne_train, e_test = train_test_split(all_5, test_size=0.2, shuffle=True,random_state = 100)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:10:40.344427Z","iopub.execute_input":"2021-11-17T16:10:40.344766Z","iopub.status.idle":"2021-11-17T16:10:40.906145Z","shell.execute_reply.started":"2021-11-17T16:10:40.344732Z","shell.execute_reply":"2021-11-17T16:10:40.905136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name in tqdm(a_train):\n  copy(name , os.path.join(a_train_dir , name.split('/')[-1]))\nfor name in tqdm(b_train):\n  copy(name , os.path.join(b_train_dir , name.split('/')[-1]))\nfor name in tqdm(c_train):\n  copy(name , os.path.join(c_train_dir , name.split('/')[-1]))\nfor name in tqdm(d_train):\n  copy(name , os.path.join(d_train_dir , name.split('/')[-1]))\nfor name in tqdm(e_train):\n  copy(name , os.path.join(e_train_dir , name.split('/')[-1]))\n\n  \nfor name in tqdm(a_test):\n  copy(name , os.path.join(a_val_dir , name.split('/')[-1]))\nfor name in tqdm(b_test):\n  copy(name , os.path.join(b_val_dir , name.split('/')[-1]))\nfor name in tqdm(c_test):\n  copy(name , os.path.join(c_val_dir , name.split('/')[-1]))\nfor name in tqdm(d_test):\n  copy(name , os.path.join(d_val_dir , name.split('/')[-1]))\nfor name in tqdm(e_test):\n  copy(name , os.path.join(e_val_dir , name.split('/')[-1]))","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:10:45.87016Z","iopub.execute_input":"2021-11-17T16:10:45.870573Z","iopub.status.idle":"2021-11-17T16:11:09.77461Z","shell.execute_reply.started":"2021-11-17T16:10:45.870537Z","shell.execute_reply":"2021-11-17T16:11:09.773716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(a_train_dir)))\nprint(len(os.listdir(b_train_dir)))\nprint(len(os.listdir(c_train_dir)))\nprint(len(os.listdir(d_train_dir)))\nprint(len(os.listdir(e_train_dir)))\n\nprint(len(os.listdir(a_val_dir)))\nprint(len(os.listdir(b_val_dir)))\nprint(len(os.listdir(c_val_dir)))\nprint(len(os.listdir(d_val_dir)))\nprint(len(os.listdir(e_val_dir)))\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:11:12.327926Z","iopub.execute_input":"2021-11-17T16:11:12.328275Z","iopub.status.idle":"2021-11-17T16:11:12.341721Z","shell.execute_reply.started":"2021-11-17T16:11:12.328245Z","shell.execute_reply":"2021-11-17T16:11:12.34078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preprocessing","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nvalidation_datagen = ImageDataGenerator(rescale=1/255)\nTRAINING_DIR = './training'\ntrain_datagen = ImageDataGenerator(rotation_range=30, zoom_range=0.2,\n                         rescale=1. / 255,\n                         width_shift_range=0.2,\n                         height_shift_range=0.2,\n                         horizontal_flip=True,\n                         brightness_range=[0.2, 1.5], fill_mode=\"nearest\")\ntrain_generator = train_datagen.flow_from_directory(\n    TRAINING_DIR,\n    batch_size=64,\n    class_mode='categorical',\n    target_size=(224,224)\n)\n\nVALIDATION_DIR = './validation'\n#validation_datagen = ImageDataGenerator(rescale=1/255)\nvalidation_generator = validation_datagen.flow_from_directory(\n    VALIDATION_DIR,\n    batch_size = 64,\n    class_mode = 'categorical',\n    target_size=(224,224)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:11:17.526714Z","iopub.execute_input":"2021-11-17T16:11:17.527044Z","iopub.status.idle":"2021-11-17T16:11:17.845162Z","shell.execute_reply.started":"2021-11-17T16:11:17.527018Z","shell.execute_reply":"2021-11-17T16:11:17.844263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Input, Flatten, Dense, Dropout, Input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndef get_vgg_model(num_classes):\n    model_vgg16_conv = ResNet50(weights='imagenet', include_top=False)\n\n    # Dong bang cac layer\n    for layer in model_vgg16_conv.layers:\n        layer.trainable = False\n\n    # Tao model\n    input = Input(shape=(224, 224, 3), name='image_input')\n    output_Resnet_conv = model_vgg16_conv(input)\n\n    # Them cac layer FC va Dropout\n    x = Flatten(name='flatten')(output_Resnet_conv)\n    x = Dense(128, activation='relu', name='fc1')(x)\n    x = Dropout(0.5)(x)\n    x = Dense(64, activation='relu', name='fc2')(x)\n    x = Dropout(0.5)(x)\n    if num_classes == 1:\n        x = Dense(num_classes, activation='sigmoid', name='predictions')(x)\n    else:\n        x = Dense(num_classes, activation='softmax', name='predictions')(x)\n\n    # Compile\n    my_model = Model(inputs=input, outputs=x)\n    if num_classes == 1:\n        my_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n    else:\n        my_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n    return my_model\nnum_classes = 5\nmodel = get_vgg_model(num_classes)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:38:18.753908Z","iopub.execute_input":"2021-11-17T16:38:18.754335Z","iopub.status.idle":"2021-11-17T16:38:20.481593Z","shell.execute_reply.started":"2021-11-17T16:38:18.754294Z","shell.execute_reply":"2021-11-17T16:38:20.480765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Creation","metadata":{}},{"cell_type":"code","source":"check_point_path = './best.h5'\nmodel_checkpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath = check_point_path,\n    monitor = 'val_accuracy',\n    save_weights_only=False,\n    save_best_only=True,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:20:16.7333Z","iopub.execute_input":"2021-11-17T16:20:16.733614Z","iopub.status.idle":"2021-11-17T16:20:16.73843Z","shell.execute_reply.started":"2021-11-17T16:20:16.733583Z","shell.execute_reply":"2021-11-17T16:20:16.737492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_history = model.fit(\n    train_generator,\n    epochs=20,\n    #callbacks = [model_checkpoint],\n    validation_data=validation_generator\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:38:27.584379Z","iopub.execute_input":"2021-11-17T16:38:27.584744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n        train_generator,\n        steps_per_epoch=200,\n        epochs=300,\n        callbacks = [model_checkpoint],\n        validation_data=validation_generator,\n        verbose = 1,\n        validation_steps=20)","metadata":{"execution":{"iopub.status.busy":"2021-11-03T16:13:55.366303Z","iopub.execute_input":"2021-11-03T16:13:55.366632Z","iopub.status.idle":"2021-11-03T16:15:49.488407Z","shell.execute_reply.started":"2021-11-03T16:13:55.366601Z","shell.execute_reply":"2021-11-03T16:15:49.486488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Evaluation","metadata":{}},{"cell_type":"markdown","source":"Plotting Loss and Accuracy","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\n#-----------------------------------------------------------\n# Retrieve a list of list results on training and test data\n# sets for each training epoch\n#-----------------------------------------------------------\nacc=model_history.history['accuracy']\nval_acc=model_history.history['val_accuracy']\nloss=model_history.history['loss']\nval_loss=model_history.history['val_loss']\n\nepochs=range(len(acc)) # Get number of epochs\n\n#------------------------------------------------\n# Plot training and validation accuracy per epoch\n#------------------------------------------------\nplt.plot(epochs, acc, 'r')\nplt.plot(epochs, val_acc, 'b')\nplt.title('Training and validation accuracy')\nplt.xlabel('epochs')\nplt.ylabel('accuracy')\nplt.legend([\"Training Accuracy\",\"Validation Accuracy\"])\nplt.show()\n\n#------------------------------------------------\n# Plot training and validation loss per epoch\n#------------------------------------------------\nplt.plot(epochs, loss, 'r')\nplt.plot(epochs, val_loss, 'b')\nplt.title('Training and validation loss')\nplt.title('Training and validation loss')\nplt.xlabel('epochs')\nplt.ylabel('loss')\nplt.legend([\"Training Loss\",\"Validation Loss\"])\nplt.show()\n\n# Desired output. Charts with training and validation metrics. ","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:16:07.370065Z","iopub.execute_input":"2021-11-16T14:16:07.370387Z","iopub.status.idle":"2021-11-16T14:16:07.67605Z","shell.execute_reply.started":"2021-11-16T14:16:07.370359Z","shell.execute_reply":"2021-11-16T14:16:07.675082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_file = os.listdir(\"../input/test123/val/bong_bo_tri\")\ntest_df = pd.DataFrame({\n    'filename': test_file\n})\ntest_df","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:35.712185Z","iopub.execute_input":"2021-11-16T14:23:35.712518Z","iopub.status.idle":"2021-11-16T14:23:35.787405Z","shell.execute_reply.started":"2021-11-16T14:23:35.712489Z","shell.execute_reply":"2021-11-16T14:23:35.786594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TESTING_DIR = '../input/test123/val/bong_bo_tri'\ntest_generator = validation_datagen.flow_from_dataframe(\n    test_df,\n    directory = TESTING_DIR,\n    x_col='filename',\n    y_col=None,\n    batch_size=64,\n    target_size=(224, 224),\n    shuffle = False,\n    class_mode = None\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:38.542199Z","iopub.execute_input":"2021-11-16T14:23:38.542477Z","iopub.status.idle":"2021-11-16T14:23:38.552351Z","shell.execute_reply.started":"2021-11-16T14:23:38.54245Z","shell.execute_reply":"2021-11-16T14:23:38.551205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\ny_pred = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:40.19153Z","iopub.execute_input":"2021-11-16T14:23:40.191862Z","iopub.status.idle":"2021-11-16T14:23:41.854391Z","shell.execute_reply.started":"2021-11-16T14:23:40.191832Z","shell.execute_reply":"2021-11-16T14:23:41.849647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To see the class mapping of dogs and cats, we have to see class_indices from training dataset","metadata":{}},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:42.449794Z","iopub.execute_input":"2021-11-16T14:23:42.45013Z","iopub.status.idle":"2021-11-16T14:23:42.456346Z","shell.execute_reply.started":"2021-11-16T14:23:42.450102Z","shell.execute_reply":"2021-11-16T14:23:42.455362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's make labels","metadata":{}},{"cell_type":"code","source":"y_pred_classification = np.where(y_pred > 0.5, 1, 0)\ny = y_pred_classification\n","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:45.715229Z","iopub.execute_input":"2021-11-16T14:23:45.71555Z","iopub.status.idle":"2021-11-16T14:23:45.719871Z","shell.execute_reply.started":"2021-11-16T14:23:45.71552Z","shell.execute_reply":"2021-11-16T14:23:45.719014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"botri 89\nchay 99\nla 87\nnghen chai 100\nthan thu 97","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\na =0\nprint(y[1])\nfor i in range(len(y)):\n    if 1 == y[i][0]:\n        a+=1\nprint(a/len(y))","metadata":{"execution":{"iopub.status.busy":"2021-11-16T14:23:53.848138Z","iopub.execute_input":"2021-11-16T14:23:53.848475Z","iopub.status.idle":"2021-11-16T14:23:53.856518Z","shell.execute_reply.started":"2021-11-16T14:23:53.848445Z","shell.execute_reply":"2021-11-16T14:23:53.855588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"Seeing some results","metadata":{}},{"cell_type":"code","source":"test_generator.reset()\ntest_images = test_generator[0][:]\n\nfig, ax = plt.subplots(3,3)\nfig.set_size_inches(10,10)\nfor i, img in enumerate(test_images):\n    ax[i//3][i%3].imshow(img)\n    label = y_pred_classification[i]\n    ax[i//3][i%3].set_title(label)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T10:11:35.007915Z","iopub.execute_input":"2021-11-16T10:11:35.008237Z","iopub.status.idle":"2021-11-16T10:11:36.815646Z","shell.execute_reply.started":"2021-11-16T10:11:35.008208Z","shell.execute_reply":"2021-11-16T10:11:36.814412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualizing first 9 filters","metadata":{}},{"cell_type":"code","source":"model.layers","metadata":{"execution":{"iopub.status.busy":"2021-11-03T14:31:33.014298Z","iopub.execute_input":"2021-11-03T14:31:33.015112Z","iopub.status.idle":"2021-11-03T14:31:33.024791Z","shell.execute_reply.started":"2021-11-03T14:31:33.015059Z","shell.execute_reply":"2021-11-03T14:31:33.022885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_conv = model.layers[0]\noutput_layer1 = first_conv(test_images)[4]\n\nprint(output_layer1.shape)\nfig, ax = plt.subplots(3,3)\nfig.set_size_inches(10,10)\nfor i in range(9):\n  ax[i//3][i%3].imshow(output_layer1[:,:,i])","metadata":{"execution":{"iopub.status.busy":"2021-11-03T14:31:38.519332Z","iopub.execute_input":"2021-11-03T14:31:38.519771Z","iopub.status.idle":"2021-11-03T14:31:38.552657Z","shell.execute_reply.started":"2021-11-03T14:31:38.519739Z","shell.execute_reply":"2021-11-03T14:31:38.550525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submition","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')\nsubmission['label'] = y_pred_classification\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-10-30T12:52:41.03982Z","iopub.status.idle":"2021-10-30T12:52:41.040589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index='False')","metadata":{"execution":{"iopub.status.busy":"2021-10-30T12:52:41.041789Z","iopub.status.idle":"2021-10-30T12:52:41.042549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}