Last active
February 28, 2021 08:59
-
-
Save MuizU/e6e0bcd2d03e6773a7b743bfdbd9c9ff to your computer and use it in GitHub Desktop.
FYP-v3 - ResNet50 Copy.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "FYP-v3 - ResNet50 Copy.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"mount_file_id": "178r8lSgq96Ukcy9IVZCv5IcQXciYg6Fv", | |
"authorship_tag": "ABX9TyPjIs9oNL2VC1ntZ5eCYAKm", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/MuizU/e6e0bcd2d03e6773a7b743bfdbd9c9ff/fyp-v3-resnet50.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "SLCHmfK1JOZm", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "2f85ec53-37c3-4978-9ef2-e1f657ffb327" | |
}, | |
"source": [ | |
"!pip install split_folders\r\n" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting split_folders\n", | |
" Downloading https://files.pythonhosted.org/packages/b8/5f/3c2b2f7ea5e047c8cdc3bb00ae582c5438fcdbbedcc23b3cc1c2c7aae642/split_folders-0.4.3-py3-none-any.whl\n", | |
"Installing collected packages: split-folders\n", | |
"Successfully installed split-folders-0.4.3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6cXxQKvcKuMC" | |
}, | |
"source": [ | |
"from google.colab import drive\r\n", | |
"import splitfolders\r\n", | |
"import os\r\n", | |
"import tensorflow as tf\r\n", | |
"from distutils.dir_util import copy_tree\r\n", | |
"from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPool2D, BatchNormalization, GlobalAveragePooling2D\r\n", | |
"from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\r\n", | |
"from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions\r\n", | |
"from tensorflow.keras.preprocessing import image\r\n", | |
"from tensorflow.keras.models import Sequential, Model\r\n", | |
"import matplotlib.pyplot as plt\r\n", | |
"import numpy as np" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pKW8FNx1_iHB" | |
}, | |
"source": [ | |
"drive.mount('/content/drive')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3hdCTVb_9nCX" | |
}, | |
"source": [ | |
"!unzip -uq \"/content/drive/My Drive/IIT/Datasets/MVNN/MM17-WeiboRumorSet.zip\" -d \"sample_data/datasets\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "AuZTLMs8Fc7f", | |
"outputId": "3900e4e4-a50f-45f1-a75d-0c7206e8979d" | |
}, | |
"source": [ | |
"# Filter out corrupted images\r\n", | |
"\r\n", | |
"\r\n", | |
"num_skipped = 0\r\n", | |
"for folder_name in (\"rumor_images\", \"nonrumor_images\"):\r\n", | |
" folder_path = os.path.join(\"sample_data/datasets/MM17-WeiboRumorSet\", folder_name)\r\n", | |
" for fname in os.listdir(folder_path):\r\n", | |
" fpath = os.path.join(folder_path, fname)\r\n", | |
" try:\r\n", | |
" fobj = open(fpath, \"rb\")\r\n", | |
" is_jfif = tf.compat.as_bytes(\"JFIF\") in fobj.peek(10)\r\n", | |
" finally:\r\n", | |
" fobj.close()\r\n", | |
"\r\n", | |
" if not is_jfif:\r\n", | |
" num_skipped += 1\r\n", | |
" # Delete corrupted image\r\n", | |
" os.remove(fpath)\r\n", | |
"\r\n", | |
"print(\"Deleted %d images\" % num_skipped)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Deleted 95 images\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "DoeCXMLnlyPQ" | |
}, | |
"source": [ | |
"# Copying the image folders to the input dataset dir\r\n", | |
"\r\n", | |
"# Directory \r\n", | |
"directory = \"input_dataset\"\r\n", | |
" \r\n", | |
"# Parent Directory path \r\n", | |
"parent_dir = \"sample_data/datasets/MM17-WeiboRumorSet\"\r\n", | |
" \r\n", | |
"# Path \r\n", | |
"path = os.path.join(parent_dir, directory) \r\n", | |
" \r\n", | |
"# Create the directory \r\n", | |
"os.mkdir(path) \r\n", | |
"print(\"Directory '% s' created\" % directory)\r\n", | |
"\r\n", | |
"# copy subdirectory example\r\n", | |
"fromDirectory = \"sample_data/datasets/MM17-WeiboRumorSet/rumor_images\"\r\n", | |
"toDirectory = \"sample_data/datasets/MM17-WeiboRumorSet/input_dataset/rumor_images\"\r\n", | |
"\r\n", | |
"copy_tree(fromDirectory, toDirectory)\r\n", | |
"fromDirectory = \"sample_data/datasets/MM17-WeiboRumorSet/nonrumor_images\"\r\n", | |
"toDirectory = \"sample_data/datasets/MM17-WeiboRumorSet/input_dataset/nonrumor_images\"\r\n", | |
"copy_tree(fromDirectory, toDirectory)\r\n", | |
"\r\n" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "0F9_4fmx6c0V", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "7790479c-9af5-4b35-8eea-a6bf3b260eca" | |
}, | |
"source": [ | |
"# Test train validation split \r\n", | |
"\r\n", | |
"input_folder = \"sample_data/datasets/MM17-WeiboRumorSet/input_dataset/\"\r\n", | |
"#os.mkdir(\"sample_data/datasets/MM17-WeiboRumorSet/processed_data\")\r\n", | |
"output_dataset = \"sample_data/datasets/MM17-WeiboRumorSet/processed_data\"\r\n", | |
"splitfolders.ratio(input_folder, output_dataset, seed=1337, ratio=(.7, .1, .2))\r\n" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Copying files: 13177 files [00:05, 2585.44 files/s]\n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qtw7RV3i7Dj_" | |
}, | |
"source": [ | |
"img_height, img_width = (224, 224)\r\n", | |
"batch_size=32\r\n", | |
"\r\n", | |
"train_dir = r\"sample_data/datasets/MM17-WeiboRumorSet/processed_data/train\"\r\n", | |
"validation_dir = r\"sample_data/datasets/MM17-WeiboRumorSet/processed_data/val/\"\r\n", | |
"test_dir = r\"sample_data/datasets/MM17-WeiboRumorSet/processed_data/test/\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "H8OJDxtl-1Jg", | |
"outputId": "1abcb291-32e5-4084-c8fb-357c52e2559f" | |
}, | |
"source": [ | |
"train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,\r\n", | |
" shear_range=0.2,\r\n", | |
" horizontal_flip=True,\r\n", | |
" validation_split=0.4\r\n", | |
" )\r\n", | |
"\r\n", | |
"train_generator = train_datagen.flow_from_directory(\r\n", | |
" train_dir,\r\n", | |
" target_size=(img_height, img_width),\r\n", | |
" batch_size=batch_size,\r\n", | |
" class_mode=\"binary\",\r\n", | |
" subset=\"training\"\r\n", | |
")\r\n", | |
"\r\n", | |
"validation_generator = train_datagen.flow_from_directory(\r\n", | |
" validation_dir,\r\n", | |
" batch_size=batch_size,\r\n", | |
" target_size=(img_height, img_width),\r\n", | |
" class_mode=\"binary\",\r\n", | |
" subset=\"validation\"\r\n", | |
")\r\n", | |
"\r\n", | |
"test_generator = train_datagen.flow_from_directory(\r\n", | |
" test_dir,\r\n", | |
" target_size=(img_height, img_width),\r\n", | |
" batch_size=1,\r\n", | |
" class_mode=\"binary\",\r\n", | |
" subset=\"validation\"\r\n", | |
")" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Found 5535 images belonging to 2 classes.\n", | |
"Found 526 images belonging to 2 classes.\n", | |
"Found 1054 images belonging to 2 classes.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "89uDgRZ_BKeN", | |
"outputId": "795dfba2-d4cf-49b1-fcbd-7bfa63b732c6" | |
}, | |
"source": [ | |
"x,y = test_generator.next()\r\n", | |
"x.shape" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(1, 224, 224, 3)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 13 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "70IY4VrzBSNQ" | |
}, | |
"source": [ | |
"# defining the mode\r\n", | |
"base_model = ResNet50(include_top=False, weights=\"imagenet\")\r\n", | |
"x = base_model.output\r\n", | |
"x = GlobalAveragePooling2D()(x)\r\n", | |
"x = Dense(1024, activation=\"relu\")(x)\r\n", | |
"predictions = Dense(train_generator.num_classes, activation=\"softmax\")(x)\r\n", | |
"model = Model(inputs=base_model.input, outputs=predictions)\r\n", | |
"for layer in base_model.layers:\r\n", | |
" layer.trainable=False\r\n", | |
"\r\n", | |
"\r\n", | |
"Early\r\n", | |
"\r\n", | |
"\r\n", | |
"model.compile(optimizer=\"adam\", callbacks=[callback], loss=\"sparse_categorical_crossentropy\", metrics = [\"accuracy\"])\r\n", | |
"model.fit(train_generator, epochs=25)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "PFBEFZFPE0Ub" | |
}, | |
"source": [ | |
"os.mkdir(\"sample_data/models\")\r\n", | |
"model.save(\"sample_data/models/ResNet50_Base_Model.h5\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "YwDrfEmvH81N", | |
"outputId": "e22d6bcf-e0d9-4961-8c7c-528f730dd0a8" | |
}, | |
"source": [ | |
"test_losss, test_acc = model.evaluate(test_generator, verbose=2)\r\n", | |
"print(\"\\nTest accuracy: \", test_acc)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1054/1054 - 21s - loss: 0.6630 - accuracy: 0.7476\n", | |
"\n", | |
"Test accuracy: 0.7476280927658081\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment