{"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":"# Βιβλιοθήκες","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport re\nimport pandas as pd\nimport os\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-08T22:30:55.505675Z","iopub.execute_input":"2022-04-08T22:30:55.506117Z","iopub.status.idle":"2022-04-08T22:30:55.512264Z","shell.execute_reply.started":"2022-04-08T22:30:55.506078Z","shell.execute_reply":"2022-04-08T22:30:55.510004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Εισαγωγή και επισκόπηση εικόνων","metadata":{}},{"cell_type":"code","source":"import pathlib\ndata_dir = \"/kaggle/input/eestech-challenge-task-2/train/train\"\ndata_dir = pathlib.Path(data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:23.366124Z","iopub.execute_input":"2022-04-08T21:32:23.366551Z","iopub.status.idle":"2022-04-08T21:32:23.380907Z","shell.execute_reply.started":"2022-04-08T21:32:23.366512Z","shell.execute_reply":"2022-04-08T21:32:23.374488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(list(data_dir.glob('*/*.jpg')))\nprint(image_count)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:23.382398Z","iopub.execute_input":"2022-04-08T21:32:23.385718Z","iopub.status.idle":"2022-04-08T21:32:23.600683Z","shell.execute_reply.started":"2022-04-08T21:32:23.385683Z","shell.execute_reply":"2022-04-08T21:32:23.599979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actinic_keratosis = list(data_dir.glob('actinic keratosis/*'))\nPIL.Image.open(str(actinic_keratosis[0]))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:23.6052Z","iopub.execute_input":"2022-04-08T21:32:23.60752Z","iopub.status.idle":"2022-04-08T21:32:23.862545Z","shell.execute_reply.started":"2022-04-08T21:32:23.607481Z","shell.execute_reply":"2022-04-08T21:32:23.861909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nevus = list(data_dir.glob('nevus/*'))\nPIL.Image.open(str(nevus[0]))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:23.863626Z","iopub.execute_input":"2022-04-08T21:32:23.864456Z","iopub.status.idle":"2022-04-08T21:32:24.479935Z","shell.execute_reply.started":"2022-04-08T21:32:23.864426Z","shell.execute_reply":"2022-04-08T21:32:24.479173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Εκπαίδευση νευρωνικού","metadata":{}},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:24.481281Z","iopub.execute_input":"2022-04-08T21:32:24.481515Z","iopub.status.idle":"2022-04-08T21:32:24.485206Z","shell.execute_reply.started":"2022-04-08T21:32:24.481485Z","shell.execute_reply":"2022-04-08T21:32:24.48441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:24.486709Z","iopub.execute_input":"2022-04-08T21:32:24.487033Z","iopub.status.idle":"2022-04-08T21:32:26.903032Z","shell.execute_reply.started":"2022-04-08T21:32:24.486994Z","shell.execute_reply":"2022-04-08T21:32:26.902288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:36.668591Z","iopub.execute_input":"2022-04-08T21:32:36.66887Z","iopub.status.idle":"2022-04-08T21:32:36.808851Z","shell.execute_reply.started":"2022-04-08T21:32:36.66883Z","shell.execute_reply":"2022-04-08T21:32:36.801533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:32:49.42672Z","iopub.execute_input":"2022-04-08T21:32:49.427057Z","iopub.status.idle":"2022-04-08T21:32:49.431694Z","shell.execute_reply.started":"2022-04-08T21:32:49.427008Z","shell.execute_reply":"2022-04-08T21:32:49.431013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n  for i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(class_names[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:33:00.487882Z","iopub.execute_input":"2022-04-08T21:33:00.488423Z","iopub.status.idle":"2022-04-08T21:33:04.117023Z","shell.execute_reply.started":"2022-04-08T21:33:00.488389Z","shell.execute_reply":"2022-04-08T21:33:04.116407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n  print(image_batch.shape)\n  print(labels_batch.shape)\n  break","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:33:12.874783Z","iopub.execute_input":"2022-04-08T21:33:12.875628Z","iopub.status.idle":"2022-04-08T21:33:15.428759Z","shell.execute_reply.started":"2022-04-08T21:33:12.875592Z","shell.execute_reply":"2022-04-08T21:33:15.428005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:33:20.031151Z","iopub.execute_input":"2022-04-08T21:33:20.031878Z","iopub.status.idle":"2022-04-08T21:33:20.040838Z","shell.execute_reply.started":"2022-04-08T21:33:20.031834Z","shell.execute_reply":"2022-04-08T21:33:20.039995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\n\nmodel = Sequential([\n  layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:33:33.743103Z","iopub.execute_input":"2022-04-08T21:33:33.743664Z","iopub.status.idle":"2022-04-08T21:33:33.841359Z","shell.execute_reply.started":"2022-04-08T21:33:33.743623Z","shell.execute_reply":"2022-04-08T21:33:33.8406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:33:39.288476Z","iopub.execute_input":"2022-04-08T21:33:39.288724Z","iopub.status.idle":"2022-04-08T21:33:39.305155Z","shell.execute_reply.started":"2022-04-08T21:33:39.288696Z","shell.execute_reply":"2022-04-08T21:33:39.304427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:34:39.096061Z","iopub.execute_input":"2022-04-08T21:34:39.096322Z","iopub.status.idle":"2022-04-08T21:34:45.471078Z","shell.execute_reply.started":"2022-04-08T21:34:39.096293Z","shell.execute_reply":"2022-04-08T21:34:45.470419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization της εκπαίδευσης","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:34:58.563776Z","iopub.execute_input":"2022-04-08T21:34:58.564547Z","iopub.status.idle":"2022-04-08T21:34:58.891616Z","shell.execute_reply.started":"2022-04-08T21:34:58.564505Z","shell.execute_reply":"2022-04-08T21:34:58.890881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Πρόβλεψη","metadata":{}},{"cell_type":"code","source":"test_dir=\"/kaggle/input/eestech-challenge-task-2/test/test\"\ntest_dir = pathlib.Path(test_dir)\ntest_images = list(test_dir.glob('*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:37:17.611442Z","iopub.execute_input":"2022-04-08T21:37:17.611692Z","iopub.status.idle":"2022-04-08T21:37:17.616126Z","shell.execute_reply.started":"2022-04-08T21:37:17.611662Z","shell.execute_reply":"2022-04-08T21:37:17.615432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(img_path):\n    img = tf.keras.utils.load_img(img_path, target_size=(img_height, img_width))\n    img_array = tf.keras.utils.img_to_array(img)\n    img_array = tf.expand_dims(img_array, 0) # Create a batch\n    predictions = model.predict(img_array)\n    score = tf.nn.softmax(predictions[0])\n    return np.argmax(score)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:54:46.140641Z","iopub.execute_input":"2022-04-08T21:54:46.141047Z","iopub.status.idle":"2022-04-08T21:54:46.147125Z","shell.execute_reply.started":"2022-04-08T21:54:46.141009Z","shell.execute_reply":"2022-04-08T21:54:46.146378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = list(map(predict, test_images))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T22:00:09.783649Z","iopub.execute_input":"2022-04-08T22:00:09.783896Z","iopub.status.idle":"2022-04-08T22:00:47.482997Z","shell.execute_reply.started":"2022-04-08T22:00:09.783867Z","shell.execute_reply":"2022-04-08T22:00:47.482327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_images(path):\n    f = str(path).rsplit('/', 1)\n    return f[1]\n    \nimg_filenames = list(map(get_images, list(test_images)))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T22:27:16.450818Z","iopub.execute_input":"2022-04-08T22:27:16.451559Z","iopub.status.idle":"2022-04-08T22:27:16.456692Z","shell.execute_reply.started":"2022-04-08T22:27:16.451518Z","shell.execute_reply":"2022-04-08T22:27:16.455815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = list(zip(img_filenames,predictions))\nsubmission_df = pd.DataFrame(preds, columns=['image','class'])\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T22:29:25.858852Z","iopub.execute_input":"2022-04-08T22:29:25.859126Z","iopub.status.idle":"2022-04-08T22:29:25.862681Z","shell.execute_reply.started":"2022-04-08T22:29:25.859097Z","shell.execute_reply":"2022-04-08T22:29:25.861837Z"},"trusted":true},"execution_count":null,"outputs":[]}]}