{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorflow import keras\n", "from keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout\n", "from keras import Sequential\n", "import numpy as np\n", "import pandas as pd\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "from tensorflow.keras.preprocessing import image" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Zip file extracted successfully.\n" ] } ], "source": [ "from zipfile import ZipFile\n", "\n", "zip_file_path = 'bone_frac.zip'\n", "\n", "with ZipFile(zip_file_path, 'r') as zip_ref:\n", " zip_ref.extractall()\n", "\n", "print(\"Zip file extracted successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "Training = 'archive (6)/train'\n", "Validation = 'archive (6)/val'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "img_width, img_height = 224, 224\n", "batch_size = 32" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 8863 images belonging to 2 classes.\n", "Found 600 images belonging to 2 classes.\n" ] } ], "source": [ "#Data Augmentation\n", "\n", "train_datagen = ImageDataGenerator(\n", " rescale=1.0/255,\n", " rotation_range=20,\n", " width_shift_range=0.2,\n", " height_shift_range=0.2,\n", " shear_range=0.2,\n", " zoom_range=0.2,\n", " horizontal_flip=True,\n", " fill_mode='nearest'\n", ")\n", "\n", "#Rescale validation images \n", "validation_datagen = ImageDataGenerator(rescale=1.0/255)\n", "\n", "#loading train n val data:\n", "\n", "train_generator = train_datagen.flow_from_directory(\n", " Training,\n", " target_size=(img_width, img_height),\n", " batch_size=batch_size,\n", " class_mode='binary'\n", ")\n", "\n", "validation_generator = validation_datagen.flow_from_directory(\n", " Validation,\n", " target_size=(img_width, img_height),\n", " batch_size=batch_size,\n", " class_mode='binary'\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "#building CNN model\n", "\n", "model = Sequential()\n", "\n", "model.add(Conv2D(32, (3,3), activation='relu', input_shape=(img_width, img_height, 3)))\n", "model.add(MaxPooling2D((2,2)))\n", "\n", "model.add(Conv2D(64, (3,3), activation='relu'))\n", "model.add(MaxPooling2D((2,2)))\n", "\n", "model.add(Conv2D(128, (3,3), activation='relu'))\n", "model.add(MaxPooling2D((2,2)))\n", "\n", "model.add(Flatten())\n", "\n", "model.add(Dense(128, activation='relu'))\n", "model.add(Dropout(0.5))\n", "\n", "model.add(Dense(1, activation='sigmoid'))\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/5\n", "276/276 [==============================] - 450s 2s/step - loss: 0.6812 - accuracy: 0.5570 - val_loss: 0.6559 - val_accuracy: 0.5533\n", "Epoch 2/5\n", "276/276 [==============================] - 457s 2s/step - loss: 0.6691 - accuracy: 0.5919 - val_loss: 0.6212 - val_accuracy: 0.6000\n", "Epoch 3/5\n", "276/276 [==============================] - 301s 1s/step - loss: 0.6513 - accuracy: 0.5942 - val_loss: 0.5682 - val_accuracy: 0.6800\n", "Epoch 4/5\n", "276/276 [==============================] - 302s 1s/step - loss: 0.6283 - accuracy: 0.6159 - val_loss: 0.6609 - val_accuracy: 0.5000\n", "Epoch 5/5\n", "276/276 [==============================] - 303s 1s/step - loss: 0.6163 - accuracy: 0.6440 - val_loss: 0.5883 - val_accuracy: 0.6767\n" ] } ], "source": [ "history = model.fit(\n", " train_generator,\n", " steps_per_epoch=train_generator.samples / batch_size,\n", " validation_data=validation_generator,\n", " validation_steps=(validation_generator.samples / batch_size),\n", " epochs=5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "19/19 [==============================] - 4s 203ms/step - loss: 0.5883 - accuracy: 0.6767\n", "Test accuracy: 67.67%\n" ] } ], "source": [ "test_loss, test_acc = model.evaluate(validation_generator)\n", "print(f'Test accuracy: {test_acc * 100: .2f}%') #.2f means float no. upto 2 decimals" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "model.save('bone_model.h5')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "model = tf.keras.models.load_model('bone_model.h5')\n", "\n", "img_path = 'archive (6)/val/fractured\\9.jpg'\n", "img = image.load_img(img_path, target_size=(224,224))\n", "img_array = image.img_to_array(img)\n", "img_array = np.expand_dims(img_array, axis=0)\n", "img_array /= 255.0" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 0s 76ms/step\n" ] } ], "source": [ "#making prediction\n", "\n", "prediction = model.predict(img_array)\n", "predicted_class=int(np.round(prediction)[0][0]) #[0][0]\n", "\n", "class_labels = ['Not Fractured', 'Fractured']\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted class: Fractured (Confidence: 57.78%)\n" ] } ], "source": [ "print(f\"Predicted class: {class_labels[predicted_class]} (Confidence: {prediction[0][0] * 100:.2f}%)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }