{"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 pandas as pd\nimport numpy as np\nfrom PIL import Image\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nimport os\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras import layers, Sequential\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Path to the HAM10000 dataset CSV file\ncsv_path = '/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_metadata.csv'\n# Path to the HAM10000 dataset images folder\nimage_folder = '/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_images_part_1'\nimage_folder2 = '/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_images_part_2'\n\n\n# Path to the SIIM ISIC dataset CSV file\nsiim_csv_path = '/kaggle/input/siim-isic-melanoma-classification/test.csv'\n# Path to the SIIM ISIC dataset images folder\nsiim_image_folder = '/kaggle/input/siim-isic-melanoma-classification/jpeg/test'\n\n\nmetadata = pd.read_csv(csv_path)\nsiim_metadata = pd.read_csv(siim_csv_path)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dictionary to map classes to numeric labels\nclass_to_label = {name: index for index, name in enumerate(metadata['dx'].unique())}\n\n# Load and preprocess the images and labels\nimages = []\nlabels = []\n\nfor index, row in metadata.iterrows():\n    image_path = f\"{image_folder}/{row['image_id']}.jpg\"\n    if not os.path.exists(image_path):\n        image_path = f\"{image_folder2}/{row['image_id']}.jpg\"\n\n    image = load_img(image_path, target_size=(100, 100))  # Resize the image if necessary\n    image = np.array(image)\n    label = class_to_label[row['dx']]\n    \n    images.append(image)\n    labels.append(label)\n\nimages=images[:10000]\nlabels=labels[:10000]\n# Convert the lists into numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)\n\n\n# Normalize the image data\nimages = images / 255.0\n\n# Convert the labels into one-hot encoded vectors\nnum_classes = len(class_to_label)\n# labels = to_categorical(labels, num_classes)\n\n# Split the dataset into train and test sets\n# Splitting code depends on your specific requirements\n\n# Now you can use the images and labels for training your Keras model","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and preprocess the SIIM ISIC images and labels\nsiim_images = []\nsiim_labels = []\n\nfor index, row in siim_metadata.iterrows():\n    image_name = row['image_name'] + '.jpg'\n    image_path = os.path.join(siim_image_folder, image_name)\n\n    image = load_img(image_path, target_size=(100, 100))  # Resize the image if necessary\n    image = np.array(image)\n    label = 1\n    \n    siim_images.append(image)\n    siim_labels.append(label)\n\nsiim_images = np.array(siim_images)\nsiim_labels = np.array(siim_labels)\n\n# Normalize the SIIM ISIC image data\nsiim_images = siim_images / 255.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_label","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (images.size)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (images.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (labels[-10:])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport numpy as np\n\n# Assuming you have loaded your dataset into X_train and y_train\n\n# Combine X_train and y_train\ndataset = list(zip(images,labels))\n\n# Shuffle the dataset\nrandom.shuffle(dataset)\n\n# Unpack the shuffled dataset into X_train and y_train\nimages,labels = zip(*dataset)\n\n# Convert X_train and y_train back to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test=images,labels\ny_train = siim_images[:len(images)]  \ny_test = siim_labels[:len(labels)]\ndel images\ndel labels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 7\nimg_height = 100\nimg_width=100\nmodel = Sequential([\n    layers.Conv2D(128, (3,3), padding='same', activation='relu', input_shape=(img_height, img_width, 3)),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(256, (3,3), padding='same', activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(512, (3,3), padding='same', activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Flatten(),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(num_classes)\n])\n","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape, x_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nmodel.fit(\n  x=x_train,y= y_train,\n    batch_size=64,\n  epochs=epochs\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}