{"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":"!pip -q install tensorflow==2.3.0","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-04T13:35:45.040283Z","iopub.execute_input":"2022-02-04T13:35:45.041219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Basics / Data manipulation\nimport numpy as np\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport os\n\n# Visualization\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport cv2\nimport skimage.io\n\n# ML\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n\n%matplotlib inline","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data\n10k+ of .tiff images\n*    **80%** for training \n*    **20%** for internal testing\n            *  10% Validation\n            *  10% Testing","metadata":{"editable":false}},{"cell_type":"markdown","source":"# Checking if GPU is being used","metadata":{"editable":false}},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\n    print(\"Running on TPU \", tpu.cluster_spec().as_dict()[\"worker\"])\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept ValueError:\n    print(\"Not connected to a TPU runtime. Using CPU/GPU strategy\")\n    strategy = tf.distribute.MirroredStrategy()","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/pc-data-dataset-gen/test.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Set-up NASNetMobile","metadata":{"editable":false}},{"cell_type":"code","source":"from tensorflow.keras import models\nfrom tensorflow.keras import layers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn import model_selection\nfrom tensorflow.keras import optimizers\n#Use this to check if the GPU is configured correctly\nfrom tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())\n\n\nfrom tensorflow.keras.applications import NASNetMobile","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Configuration of the NASNetMobile\n#conv_base = NASNetMobile(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"../input/pc-nasnetmobile-with-pregen-datasets/NASNetMobile-model.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model \nThe model will have the follow configuration:\n______________\n1st layer: NASNetMobile (224, 224, 3) input images\n______________\n2nd layer: GlobalMaxPooling2D\n______________\n3rd layer: Dropout with learning rate = 2e-5\n______________\n4th layer: Denser layer x 6 that will classify the image","metadata":{"editable":false}},{"cell_type":"markdown","source":"model = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.GlobalMaxPooling2D(name=\"gap\"))\n# Avoid overfitting\nmodel.add(layers.Dropout(rate=0.5))\nmodel.add(layers.Dense(10, activation=\"softmax\", name=\"fc_out\"))\nconv_base.trainable = True\n\nmodel.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer=optimizers.RMSprop(lr=2e-5),\n    metrics=[\"acc\"],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-24T22:01:00.272469Z","iopub.execute_input":"2022-01-24T22:01:00.272788Z","iopub.status.idle":"2022-01-24T22:01:07.659143Z","shell.execute_reply.started":"2022-01-24T22:01:00.272759Z","shell.execute_reply":"2022-01-24T22:01:07.658211Z"},"editable":false}},{"cell_type":"code","source":"model.summary()","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation\n\nBefore training, we preprocess a little bit the image, in order to have a better perfomance on the predictions","metadata":{"editable":false}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample = plt.imread(\"../input/panda2/train_images/0005f7aaab2800f6170c399693a96917.png\")","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # Creating an object that will contain all the changes that will\n # be performed randomly to the images to help the training be more robust\n image_gen = ImageDataGenerator(\n                                width_shift_range=0.1,\n                                height_shift_range=0.1,\n                                rescale=1/255,\n                                shear_range=0.2,\n                                zoom_range=0.2,\n                                horizontal_flip=True,\n                                fill_mode=\"nearest\"\n                                )","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_gen.flow_from_directory(\"./test\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_gen = image_gen.flow_from_directory(\"./test\",\n                                                target_size=(224, 224),\n                                                batch_size=batch_size,\n                                                class_mode=\"categorical\")","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_gen.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUMBER_OF_TESTING_IMAGES = 1051","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing","metadata":{"editable":false}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report, ConfusionMatrixDisplay","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = ImageDataGenerator()\ntest_data_generator = test_generator.flow_from_directory(\n    \"./test\", # Put your path here\n    target_size=(224, 224),\n    batch_size=32,\n    shuffle=False)\ntest_steps_per_epoch = np.math.ceil(test_data_generator.samples / test_data_generator.batch_size)\n\npredictions = model.predict_generator(test_data_generator, steps=test_steps_per_epoch)\n# Get most likely class\npredicted_classes = np.argmax(predictions, axis=1)","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_classes = test_data_generator.classes\nclass_labels = list(test_data_generator.class_indices.keys())   ","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report_class = classification_report(true_classes, predicted_classes, target_names=class_labels)\nprint(report_class)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report_conf = confusion_matrix(true_classes, predicted_classes)\nprint(report_conf)  ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r test","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}