{"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":"# 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)\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","execution":{"iopub.status.busy":"2023-10-17T03:01:11.379644Z","iopub.execute_input":"2023-10-17T03:01:11.379997Z","iopub.status.idle":"2023-10-17T03:02:07.072499Z","shell.execute_reply.started":"2023-10-17T03:01:11.379972Z","shell.execute_reply":"2023-10-17T03:02:07.071244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:52:07.179536Z","iopub.execute_input":"2023-10-17T03:52:07.179899Z","iopub.status.idle":"2023-10-17T03:52:07.186131Z","shell.execute_reply.started":"2023-10-17T03:52:07.179873Z","shell.execute_reply":"2023-10-17T03:52:07.184931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_path = '/kaggle/input/siim-isic-melanoma-classification'\ntrain_csv_path = f'{dataset_path}/train.csv'\ndf_train = pd.read_csv(train_csv_path)\nclass_names = df_train['diagnosis'].unique()\nclass_dict = {class_name: i for i, class_name in enumerate(class_names)}\n","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:52:37.202723Z","iopub.execute_input":"2023-10-17T03:52:37.203071Z","iopub.status.idle":"2023-10-17T03:52:37.266907Z","shell.execute_reply.started":"2023-10-17T03:52:37.203046Z","shell.execute_reply":"2023-10-17T03:52:37.265837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(images, labels, class_dict):\n    # Display some sample images from the dataset\n    # (Note: This function is defined earlier in the code)\n    plt.figure(figsize=(12, 12))\n    for i in range(len(images)):\n        plt.subplot(4, 4, i + 1)\n        plt.imshow(images[i])\n        plt.title(f\"Class: {labels[i]} ({class_dict.get(labels[i], 'unknown')})\")\n        plt.axis('off')\n    plt.show()\n\nshow_images()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:14:30.344841Z","iopub.execute_input":"2023-10-17T04:14:30.345259Z","iopub.status.idle":"2023-10-17T04:14:30.390453Z","shell.execute_reply.started":"2023-10-17T04:14:30.345233Z","shell.execute_reply":"2023-10-17T04:14:30.388581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ndef generate_synthetic_dataset(num_samples=1000, image_size=(128, 128)):\n    images = np.zeros((num_samples, *image_size, 3), dtype=np.uint8)\n    labels = np.zeros((num_samples, 4), dtype=np.float32)  # [x, y, width, height]\n\n    for i in range(num_samples):\n        x = np.random.randint(0, image_size[0] - 32)\n        y = np.random.randint(0, image_size[1] - 32)\n        width = np.random.randint(20, 50)\n        height = np.random.randint(20, 50)\n        image = np.zeros((*image_size, 3), dtype=np.uint8)\n        image[y:y+height, x:x+width] = [255, 255, 255]\n        images[i] = image\n        labels[i] = [x, y, width, height]\n\n    return images / 255.0, labels\n\ngenerate_synthetic_dataset()\n\nimages, labels = generate_synthetic_dataset(num_samples=1000)\nimages = images / 255.0\n\nsplit_ratio = 0.8\nsplit_index = int(len(images) * split_ratio)\n\ntrain_images, test_images = images[:split_index], images[split_index:]\ntrain_labels, test_labels = labels[:split_index], labels[split_index:]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:06:45.240099Z","iopub.execute_input":"2023-10-17T04:06:45.241206Z","iopub.status.idle":"2023-10-17T04:06:45.890012Z","shell.execute_reply.started":"2023-10-17T04:06:45.241172Z","shell.execute_reply":"2023-10-17T04:06:45.888801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\ninput_shape = (*images.shape[1:],)\ninput_layer = Input(shape=input_shape)\nx = Conv2D(32, (3, 3), activation='relu')(input_layer)\nx = MaxPooling2D((2, 2))(x)\nx = Conv2D(64, (3, 3), activation='relu')(x)\nx = MaxPooling2D((2, 2))(x)\nx = Flatten()(x)\nx = Dense(128, activation='relu')(x)\noutput_layer = Dense(4, activation='linear')(x)\n\nmodel = Model(inputs=input_layer, outputs=output_layer)\nmodel.compile(optimizer='adam', loss='mse')  \nhistory = model.fit(train_images, train_labels, epochs=10, batch_size=32, validation_split=0.2)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:06:50.417857Z","iopub.execute_input":"2023-10-17T04:06:50.418193Z","iopub.status.idle":"2023-10-17T04:09:13.715359Z","shell.execute_reply.started":"2023-10-17T04:06:50.418168Z","shell.execute_reply":"2023-10-17T04:09:13.714354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = model.evaluate(test_images, test_labels)\naccuracy = 100.0 - loss  # Considered as a simple accuracy metric\nprint(f'Test Loss: {loss}')\nprint(f'Test Accuracy: {accuracy:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:09:14.702619Z","iopub.execute_input":"2023-10-17T04:09:14.703378Z","iopub.status.idle":"2023-10-17T04:09:15.652732Z","shell.execute_reply.started":"2023-10-17T04:09:14.703317Z","shell.execute_reply":"2023-10-17T04:09:15.651844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the trained model to a file\nmodel.save('/kaggle/working/.h5')\n\n# Load the saved model from a file\nloaded_model = tf.keras.models.load_model('/kaggle/working/.h5')","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:09:49.349096Z","iopub.execute_input":"2023-10-17T04:09:49.349464Z","iopub.status.idle":"2023-10-17T04:09:50.057606Z","shell.execute_reply.started":"2023-10-17T04:09:49.349437Z","shell.execute_reply":"2023-10-17T04:09:50.055896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:18:12.42166Z","iopub.execute_input":"2023-10-17T03:18:12.422037Z","iopub.status.idle":"2023-10-17T03:18:12.427817Z","shell.execute_reply.started":"2023-10-17T03:18:12.422009Z","shell.execute_reply":"2023-10-17T03:18:12.426537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:18:16.192553Z","iopub.execute_input":"2023-10-17T03:18:16.192936Z","iopub.status.idle":"2023-10-17T03:18:16.294576Z","shell.execute_reply.started":"2023-10-17T03:18:16.192907Z","shell.execute_reply":"2023-10-17T03:18:16.293528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:18:20.738063Z","iopub.execute_input":"2023-10-17T03:18:20.738438Z","iopub.status.idle":"2023-10-17T03:18:20.744605Z","shell.execute_reply.started":"2023-10-17T03:18:20.73841Z","shell.execute_reply":"2023-10-17T03:18:20.743417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:18:25.208433Z","iopub.execute_input":"2023-10-17T03:18:25.208857Z","iopub.status.idle":"2023-10-17T03:18:25.214071Z","shell.execute_reply.started":"2023-10-17T03:18:25.208828Z","shell.execute_reply":"2023-10-17T03:18:25.212816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:18:28.194666Z","iopub.execute_input":"2023-10-17T03:18:28.195004Z","iopub.status.idle":"2023-10-17T03:18:28.225928Z","shell.execute_reply.started":"2023-10-17T03:18:28.19498Z","shell.execute_reply":"2023-10-17T03:18:28.224721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-17T03:22:10.387673Z","iopub.execute_input":"2023-10-17T03:22:10.388071Z","iopub.status.idle":"2023-10-17T03:22:10.45704Z","shell.execute_reply.started":"2023-10-17T03:22:10.388044Z","shell.execute_reply":"2023-10-17T03:22:10.455545Z"},"trusted":true},"execution_count":null,"outputs":[]}]}