{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":505351,"sourceType":"datasetVersion","datasetId":174469}],"dockerImageVersionId":30887,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.147755Z","iopub.execute_input":"2025-02-15T03:55:44.148137Z","iopub.status.idle":"2025-02-15T03:55:44.152133Z","shell.execute_reply.started":"2025-02-15T03:55:44.148092Z","shell.execute_reply":"2025-02-15T03:55:44.151293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport os\nfrom keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import layers","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.153275Z","iopub.execute_input":"2025-02-15T03:55:44.153545Z","iopub.status.idle":"2025-02-15T03:55:44.174623Z","shell.execute_reply.started":"2025-02-15T03:55:44.153523Z","shell.execute_reply":"2025-02-15T03:55:44.173407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.176296Z","iopub.execute_input":"2025-02-15T03:55:44.176601Z","iopub.status.idle":"2025-02-15T03:55:44.197403Z","shell.execute_reply.started":"2025-02-15T03:55:44.176578Z","shell.execute_reply":"2025-02-15T03:55:44.196338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.198999Z","iopub.execute_input":"2025-02-15T03:55:44.199279Z","iopub.status.idle":"2025-02-15T03:55:44.213185Z","shell.execute_reply.started":"2025-02-15T03:55:44.199255Z","shell.execute_reply":"2025-02-15T03:55:44.212234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir=\"/kaggle/input/skin-cancer-malignant-vs-benign/train\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.214104Z","iopub.execute_input":"2025-02-15T03:55:44.214392Z","iopub.status.idle":"2025-02-15T03:55:44.227471Z","shell.execute_reply.started":"2025-02-15T03:55:44.214357Z","shell.execute_reply":"2025-02-15T03:55:44.226656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir=\"/kaggle/input/skin-cancer-malignant-vs-benign/test\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.228357Z","iopub.execute_input":"2025-02-15T03:55:44.228704Z","iopub.status.idle":"2025-02-15T03:55:44.243471Z","shell.execute_reply.started":"2025-02-15T03:55:44.228673Z","shell.execute_reply":"2025-02-15T03:55:44.242745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label=[\"'malignant\",\"benign\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:55:44.245398Z","iopub.execute_input":"2025-02-15T03:55:44.245626Z","iopub.status.idle":"2025-02-15T03:55:44.259215Z","shell.execute_reply.started":"2025-02-15T03:55:44.245606Z","shell.execute_reply":"2025-02-15T03:55:44.258340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,\n                                   shear_range=0.5,\n                                   zoom_range=0.4,\n                                   horizontal_flip=True,\n                                   validation_split=0.2,\n                                  )\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_directory(train_dir,\n                                                    target_size=(224, 224),\n                                                    batch_size=16,\n                                                    class_mode='categorical',\n                                                    subset='training')\n\nval_generator = train_datagen.flow_from_directory(train_dir,\n                                                    target_size=(224, 224),\n                                                    batch_size=16,\n                                                    class_mode='categorical',\n                                                    subset='validation')\n\ntest_generator = test_datagen.flow_from_directory(test_dir,\n                                                  target_size=(224, 224),\n                                                  batch_size=16,\n                                                  class_mode='categorical')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:25.316839Z","iopub.execute_input":"2025-02-15T03:56:25.317248Z","iopub.status.idle":"2025-02-15T03:56:26.825896Z","shell.execute_reply.started":"2025-02-15T03:56:25.317214Z","shell.execute_reply":"2025-02-15T03:56:26.825205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:31.910353Z","iopub.execute_input":"2025-02-15T03:56:31.910724Z","iopub.status.idle":"2025-02-15T03:56:33.012746Z","shell.execute_reply.started":"2025-02-15T03:56:31.910691Z","shell.execute_reply":"2025-02-15T03:56:33.012044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import necessary libraries\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Set the path to the dataset\ndata_path ='/kaggle/input/skin-cancer-malignant-vs-benign/test'\n\n# Define the classes\nclasses = ['malignant', 'benign']\n\n# Print 10 images of each class\nfor cls in classes:\n    cls_path = os.path.join(data_path, cls)\n    cls_images = os.listdir(cls_path)[:10]\n    print(f\"{cls.capitalize()} Images:\")\n    for img_file in cls_images:\n        img_path = os.path.join(cls_path, img_file)\n        img = plt.imread(img_path)\n        plt.imshow(img)\n        plt.axis('off')\n        plt.show()\n    #print('\\n')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:33.013756Z","iopub.execute_input":"2025-02-15T03:56:33.014014Z","iopub.status.idle":"2025-02-15T03:56:36.229745Z","shell.execute_reply.started":"2025-02-15T03:56:33.013981Z","shell.execute_reply":"2025-02-15T03:56:36.228616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add custom layers on top of the pre-trained model\nx = base_model.output\nx = Flatten()(x)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\n#output layer fully connected dance layer with two neuron\npredictions = Dense(2, activation='softmax')(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:36.232288Z","iopub.execute_input":"2025-02-15T03:56:36.232679Z","iopub.status.idle":"2025-02-15T03:56:36.260169Z","shell.execute_reply.started":"2025-02-15T03:56:36.232642Z","shell.execute_reply":"2025-02-15T03:56:36.259235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Get the first 10 images from the test generator\nx_test, y_test = next(test_generator)\nx_test_first10 = x_test[:10]\n\n# Plot the first 10 images\nfig, axes = plt.subplots(nrows=2, ncols=5, figsize=(10, 5))\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(x_test_first10[i])\n    ax.set_title(\"Image {}\".format(i+1))\n    ax.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:36.262640Z","iopub.execute_input":"2025-02-15T03:56:36.262891Z","iopub.status.idle":"2025-02-15T03:56:37.332166Z","shell.execute_reply.started":"2025-02-15T03:56:36.262869Z","shell.execute_reply":"2025-02-15T03:56:37.331117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Combine the base ResNet50 model with the custom layers\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:37.333417Z","iopub.execute_input":"2025-02-15T03:56:37.333773Z","iopub.status.idle":"2025-02-15T03:56:37.353035Z","shell.execute_reply.started":"2025-02-15T03:56:37.333739Z","shell.execute_reply":"2025-02-15T03:56:37.352067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Freeze all layers in the base ResNet50 model\nfor layer in base_model.layers[5:]:\n    layer.trainable = False\n# Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:37.353921Z","iopub.execute_input":"2025-02-15T03:56:37.354258Z","iopub.status.idle":"2025-02-15T03:56:37.559137Z","shell.execute_reply.started":"2025-02-15T03:56:37.354233Z","shell.execute_reply":"2025-02-15T03:56:37.558234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model on the training data\nhistory = model.fit(train_generator, \n                              steps_per_epoch=train_generator.n // train_generator.batch_size, \n                              epochs=45, \n                              validation_data=val_generator, \n                              validation_steps=val_generator.n // val_generator.batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T03:56:37.559949Z","iopub.execute_input":"2025-02-15T03:56:37.560243Z","iopub.status.idle":"2025-02-15T04:09:19.461869Z","shell.execute_reply.started":"2025-02-15T03:56:37.560221Z","shell.execute_reply":"2025-02-15T04:09:19.460764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot the loss vs val_loss\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\n# Plot the accuracy vs val_accuracy\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T04:09:19.462683Z","iopub.execute_input":"2025-02-15T04:09:19.463071Z","iopub.status.idle":"2025-02-15T04:09:19.966640Z","shell.execute_reply.started":"2025-02-15T04:09:19.463018Z","shell.execute_reply":"2025-02-15T04:09:19.965672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model on the testing data\ntest_loss, test_acc = model.evaluate(test_generator, verbose=2)\nprint('Test Accuracy:', test_acc)\n# Print the train and test loss\nprint('Train Loss:', history.history['loss'][-1])\nprint('Test Loss:', test_loss)\nprint('Test Accuracy:', test_acc)\n# Plot the train and validation loss over epochs\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Validation'])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T04:09:56.657753Z","iopub.execute_input":"2025-02-15T04:09:56.658144Z","iopub.status.idle":"2025-02-15T04:10:03.713785Z","shell.execute_reply.started":"2025-02-15T04:09:56.658111Z","shell.execute_reply":"2025-02-15T04:10:03.712964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model on the testing data\ntest_loss, test_acc = model.evaluate(test_generator, verbose=2)\nprint('Test Accuracy:', test_acc)\n\n# Plot the training and testing accuracy\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'val'], loc='upper left')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T04:10:29.691564Z","iopub.execute_input":"2025-02-15T04:10:29.691868Z","iopub.status.idle":"2025-02-15T04:10:31.912005Z","shell.execute_reply.started":"2025-02-15T04:10:29.691837Z","shell.execute_reply":"2025-02-15T04:10:31.911218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Get the next batch of images from the test generator\nx_test, y_test = next(test_generator)\n\n# Predict the classes of the testing data\ny_pred = model.predict(x_test)\ny_pred_classes = np.argmax(y_pred, axis=1)\ny_true_classes = np.argmax(y_test, axis=1)\nlabel_dict = {0: 'benign', 1: 'malignant'}\n\n# Plot the images along with their predicted and actual labels\nfig, axes = plt.subplots(nrows=4, ncols=4, figsize=(12,12))\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(x_test[i])\n    pred_label = label_dict[y_pred_classes[i]]\n    true_label = label_dict[y_true_classes[i]]\n    ax.set_title(\"Pred: {}\\nTrue: {}\".format(pred_label, true_label))\n    ax.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T04:10:44.167035Z","iopub.execute_input":"2025-02-15T04:10:44.167327Z","iopub.status.idle":"2025-02-15T04:10:49.517771Z","shell.execute_reply.started":"2025-02-15T04:10:44.167306Z","shell.execute_reply":"2025-02-15T04:10:49.516424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create confusion matrix\ncm = confusion_matrix(y_true_classes, y_pred_classes)\n\n# Plot confusion matrix\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues')\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T04:11:01.715302Z","iopub.execute_input":"2025-02-15T04:11:01.715589Z","iopub.status.idle":"2025-02-15T04:11:01.910503Z","shell.execute_reply.started":"2025-02-15T04:11:01.715567Z","shell.execute_reply":"2025-02-15T04:11:01.909525Z"}},"outputs":[],"execution_count":null}]}