Skin cancer classification based on the Intel oneAPI AI toolkit

Summary

Recently, the application of artificial intelligence in the medical field has received more and more attention. With the help of Intel's oneAPI AI toolkit, we can develop a personalized health management tool for the masses, one of the important functions is skin cancer classification. By collecting users' health data and combining it with artificial intelligence technology for data analysis and processing, we can provide users with personalized health management solutions and help them better manage and improve their physical health.

This project uses Intel's oneAPI AI toolkit to develop an AI-based skin cancer classification system. The system classifies images of skin lesions provided by users to determine whether they are at risk for skin cancer. By using deep learning algorithms and convolutional neural network models, we can accurately classify and diagnose skin lesions.

General content of the project

Before starting the project, you need to install Anaconda and the Intel oneAPI AI Toolkit. Anaconda is a popular Python data science platform, while Intel oneAPI AI Toolkit provides optimized Python and Tensorflow to accelerate machine learning and deep learning tasks. After installing these tools, we can proceed with the development of the project.

First, we need to prepare the dataset. Due to the large number of pictures in the project dataset, this is a simplified project. We will use a dataset containing images of skin lesions to train and test our classification model. Next, we need to install some Python dependencies to be able to train and test the classification model. These dependencies include matplotlib, keras_preprocessing, Pillow, opencv-python, numpy, and scikit-image. After the installation is complete, we can enter the directory where the dataset and code are saved, and follow the steps below.

First, we run the "Train.py" file to train and save the classification model. During the training process, we use the images in the dataset for training and iteratively optimize the parameters of the model. After the training is complete, we will save the trained model for subsequent testing and application.

Next, we can run the "Test.py" file to test new images for classification using the saved model. By loading a previously saved model, we can classify user-supplied images of skin lesions and determine whether they are at risk for skin cancer. In this way, we can provide users with timely health advice and guidance to help them better manage their health conditions.

Through this skin cancer classification project based on the Intel oneAPI AI toolkit, we can apply advanced artificial intelligence technology to the medical field and provide personalized health management solutions to promote people's physical health and quality of life.

At the core of this project are deep learning algorithms and convolutional neural network models. Deep learning is a machine learning method that mimics the structure and function of the neural networks of the human brain. Convolutional neural networks are an important architecture for deep learning, specifically for processing image and visual data. By training such a network model, we can let the computer learn to extract useful features from images and perform accurate classification and diagnosis.

training process

During training, we use a dataset containing images of skin lesions. These images cover different types of skin lesions, including normal skin, benign lesions, and malignant lesions. We divide the dataset into training set and test set for training and evaluating the performance of the model. By iteratively optimizing the parameters of the model, we can gradually improve the accuracy and generalization ability of the model.

After training is complete, we will save the trained model. In this way, in practical applications, we only need to load the saved model and input the skin lesion image provided by the user, and then the classification and diagnosis can be performed quickly. The system will judge whether there is a risk of skin cancer based on the prediction results of the model, and give corresponding suggestions and guidance. This personalized health management solution can help users keep abreast of their health status and take corresponding measures to prevent and treat diseases such as skin cancer.

In addition to skin cancer classification, the tool also has social features. Users can use this tool with friends or family members for health management, share health data and experiences, and monitor and support each other. This socialized health management approach can enhance user participation and motivation, foster a healthy lifestyle, and promote the healthy development of the entire community.

Through deep learning and convolutional neural network models, we can accurately classify and diagnose skin lesion images provided by users, helping them keep abreast of their health status. This tool provides users with personalized health management solutions by collecting users' health data and combining it with artificial intelligence technology for data analysis and processing.

Using this tool, users can track and monitor their physical condition at any time. They can take or upload images of skin lesions and have tools classify and diagnose them. The tool analyzes the features and patterns in the image and then uses the trained model to make predictions. Based on the prediction results, users can understand whether there is a risk of skin cancer and take corresponding measures according to the recommendations.

In addition to skin cancer classification, the tool can provide other health management functions. It helps users record and analyze physical indicators such as weight, blood pressure, heart rate, etc. Users can set fitness goals and track their progress. The tool can also generate personalized health advice and guidance based on the user's health data to help users improve their living habits and achieve better health.

In addition, the tool has a social function, so users can use it with friends or family members. They can share health data and experiences, and encourage and support each other. This socialized health management approach can enhance user motivation and participation, form a healthy lifestyle, and promote the healthy development of the entire community.

In implementing the above functions, we used Intel's oneAPI AI toolkit. This toolkit provides optimized Python and Tensorflow to accelerate machine learning and deep learning tasks. By leveraging Intel's hardware and software optimizations, we can improve model training and inference performance, enabling users to quickly obtain accurate health diagnoses and recommendations.

Install Anaconda and Intel oneAPI AI Toolkit

First, we need to install Anaconda, a popular Python data science platform used to provide a convenient development environment. In addition, we also need to install the Intel oneAPI AI toolkit, which provides optimized Python and TensorFlow to accelerate machine learning and deep learning tasks.

Here are the installation steps:

  1. Download and install Anaconda.

  2. Open the command prompt (CMD.exe) in Anaconda.

  3. Run the following command in a command prompt to install Intel-optimized Python using conda:

    conda install -c intel intelpython3_full
    
  4. To create a virtual environment using Intel-optimized Python, run the following command:

    conda create -n <环境名称> intelpython3_full python=3.8
    
  5. To activate the virtual environment, run the following command:

    conda activate <环境名称>
    
  6. To add the Intel channel, run the following command:

    conda config --add channels intel
    
  7. To install Intel-optimized TensorFlow, run one of the following commands:

    conda install intel-tensorflow
    

    or

    pip install intel-tensorflow
    
  8. If you encounter repeated errors, run the following commands to avoid errors:

    set KMP_DUPLICATE_LIB_OK=TRUE
    

Install Python dependencies to be able to train and test classification models

Before training and testing the classification model, we need to install some Python dependencies to ensure the smooth running of the project. These dependencies include: matplotlib, keras_preprocessing, Pillow, opencv-python, numpy, and scikit-image.

Here are the installation steps:

  • Run the following command to install matplotlib:

    conda install matplotlib
    
  • Run the following command to install keras_preprocessing:

    pip install keras_preprocessing
    
  • Run the following command to install Pillow:

    pip install Pillow
    
  • Run the following command to install opencv-python:

    pip install opencv-python
    
    • Run the following command to install numpy:

      pip install numpy
      
    • Run the following command to install scikit-image:

      pip install scikit-image
      

    usage

    After completing the above installation steps, we can start the project of skin cancer classification based on Intel oneAPI AI toolkit.

    1. First, go to the directory where the dataset and code are saved, and run the following command:

      cd health-tool
      
    2. Run Train.pythe file to train and save the model:

      python Train.py
      

      This will start the training process and save the trained model in the specified directory.

    3. Run Test.pythe file to test the image with the saved model:

      python Test.py
      

      This will load the previously trained model and use the test images for classification and diagnosis.

    Through the above steps, we can use the Intel oneAPI AI toolkit to realize the function of skin cancer classification. This tool can help users keep abreast of their own health status and provide personalized health management solutions. It combines deep learning and convolutional neural network models to extract features in images and perform accurate classification and diagnosis. At the same time, the tool also has a social function, allowing users to share health data and experience with others, and jointly promote the development of a healthy lifestyle.

    Note: Before running the project code, make sure you have installed all required dependencies and follow the given file structure and command to run the code.

Due to the overall size of the project, only part of the code is shown here. If you need the complete code, please send a private message.

Train.py

def Create_ResNet50V2_Model(num_classes, img_shape_x, img_shape_y):
    ResNet50V2 = tf.keras.applications.ResNet50V2(input_shape=(img_shape_x, img_shape_y, 3),
    include_top= False,
    weights='imagenet'
    )
    ResNet50V2.trainable = True

    for layer in ResNet50V2.layers[:-50]:
        layer.trainable = False


    model = Sequential([
                    ResNet50V2,
                    Dropout(.25),
                    BatchNormalization(),
                    Flatten(),
                    Dense(64, activation='relu'),
                    BatchNormalization(),
                    Dropout(.5),
                    Dense(num_classes,activation='softmax')
                    ])
    return model

def train_model():
    train_data_path = train_dir_var.get()
    val_data_path = val_dir_var.get()
    img_shape_x = img_shape_x_var.get()
    img_shape_y = img_shape_y_var.get()
    batch_size = batch_size_var.get()
    save_model_path = save_model_path_var.get()
    save_model_name = save_model_name_var.get()
    num_epoch = num_epoch_var.get()

    train_preprocessor = ImageDataGenerator(
        rescale = 1 / 255.,
        rotation_range=10,
        zoom_range=0.2,
        width_shift_range=0.1,
        height_shift_range=0.1,
        horizontal_flip=True,                                        
        fill_mode='nearest',
    )

    val_preprocessor = ImageDataGenerator(
        rescale = 1 / 255.,
    )

    train_data = train_preprocessor.flow_from_directory(
        train_data_path,
        class_mode="categorical",
        target_size=(img_shape_x,img_shape_y),
        color_mode='rgb', 
        shuffle=True,
        batch_size=batch_size,
        subset='training', 
    )

    val_data = val_preprocessor.flow_from_directory(
        val_data_path,
        class_mode="categorical",
        target_size=(img_shape_x,img_shape_y),
        color_mode="rgb",
        shuffle=False,
        batch_size=batch_size,
    )

    num_classes = train_data.num_classes

    ResNet50V2_Model = Create_ResNet50V2_Model(num_classes, img_shape_x, img_shape_y)
    ResNet50V2_Model.summary()

    ResNet50V2_Model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

    checkpoint_path = os.path.join(save_model_path, save_model_name)
    ModelCheckpoint(checkpoint_path, monitor="val_accuracy", save_best_only=True)

    Early_Stopping = EarlyStopping(monitor = 'val_accuracy', patience = 7, restore_best_weights = True, verbose=1)

    Reducing_LR = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                factor=0.2,
                                                patience=2,
                                                verbose=1)

    callbacks = [Early_Stopping, Reducing_LR]

    steps_per_epoch = train_data.n // train_data.batch_size
    validation_steps = val_data.n // val_data.batch_size

    ResNet50V2_history = ResNet50V2_Model.fit(train_data ,validation_data = val_data , epochs=num_epoch, batch_size=batch_size,
                                        callbacks = callbacks, steps_per_epoch=steps_per_epoch, validation_steps=validation_steps)
    ResNet50V2_Model.save(os.path.join(save_model_path, save_model_name + '.h5'))
    def plot_curves(history):

        loss = history.history["loss"]
        val_loss = history.history["val_loss"]

        accuracy = history.history["accuracy"]
        val_accuracy = history.history["val_accuracy"]

        epochs = range(len(history.history["loss"]))

        plt.figure(figsize=(15,5))

        plt.subplot(1, 2, 1)
        plt.plot(epochs, loss, label = "training_loss")
        plt.plot(epochs, val_loss, label = "val_loss")
        plt.title("Loss")
        plt.xlabel("epochs")
        plt.legend()

        plt.subplot(1, 2, 2)
        plt.plot(epochs, accuracy, label = "training_accuracy")
        plt.plot(epochs, val_accuracy, label = "val_accuracy")
        plt.title("Accuracy")
        plt.xlabel("epochs")
        plt.legend()

        plt.show()
        
    plot_curves(ResNet50V2_history)
    messagebox.showinfo(title="训练已完成", message="模型已成功训练!")

train_button = tk.Button(root, text="Train", command=train_model)
train_button.grid(row=8, column=1, padx=5, pady=5)

Text.py

def load_and_prep_image(filename, img_shape_x=200, img_shape_y=200):
    img = cv2.imread(filename)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (img_shape_x, img_shape_y))
    img = preprocess_input(img)
    return img


def pred_and_plot(filename, model, class_names, img_shape_x=200, img_shape_y=200):
    img = load_and_prep_image(filename, img_shape_x, img_shape_y)
    image = io.imread(filename)
    gray_image = np.mean(image, axis=2)

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

    ax1.imshow(gray_image, cmap='hot')
    ax1.set_title('Heat Map')
    ax1.set_axis_off()

    pred = model.predict(np.expand_dims(img, axis=0))
    pred_class = class_names[pred.argmax()]
    img = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
    ax2.imshow(img)
    ax2.set_title(f"Prediction: {
      
      pred_class}")
    ax2.set_axis_off()

    plt.show()



def select_image():
    global panel, image_path
    image_path = filedialog.askopenfilename()
    image = Image.open(image_path)
    image = image.resize((300, 300))
    image = ImageTk.PhotoImage(image)
    if panel is None:
        panel = tk.Label(image=image)
        panel.image = image
        panel.pack(padx=10, pady=10)
    else:
        panel.configure(image=image)
        panel.image = image


def select_class_dir():
    class_dir = filedialog.askdirectory()
    class_dir_entry.delete(0, tk.END)
    class_dir_entry.insert(0, class_dir)


def select_model_file():
    model_file = filedialog.askopenfilename()
    model_file_entry.delete(0, tk.END)
    model_file_entry.insert(0, model_file)


def predict():
    global image_path, model, classes, img_shape_x, img_shape_y
    if image_path is None:
        return
    class_dir = class_dir_entry.get()
    model_file = model_file_entry.get()
    img_shape_x = int(img_shape_x_entry.get())
    img_shape_y = int(img_shape_y_entry.get())
    classes = []
    for file in os.listdir(class_dir):
        d = os.path.join(class_dir, file)
        if os.path.isdir(d):
            classes.append(file)
    model = load_model(model_file)
    pred_and_plot(image_path, model, classes, img_shape_x, img_shape_y)

root = tk.Tk()
root.title("图像预测器")
root.resizable(False, False)

panel = None

image_path = None
model = None
classes = []
img_shape_x = 200
img_shape_y = 200

class_dir_label = tk.Label(root, text="Class Directory:")
class_dir_label.grid(row=0, column=0, padx=10, pady=10)
class_dir_entry = tk.Entry(root)
class_dir_entry.grid(row=0, column=1, padx=10, pady=10)
class_dir_button = tk.Button(root, text="Browse", command=select_class_dir)
class_dir_button.grid(row=0, column=2, padx=10, pady=10)

model_file_label = tk.Label(root, text="Model File Path:")
model_file_label.grid(row=1, column=0, padx=10, pady=10)
model_file_entry = tk.Entry(root)
model_file_entry.grid(row=1, column=1, padx=10, pady=10)
model_file_button = tk.Button(root, text="Browse", command=select_model_file)
model_file_button.grid(row=1, column=2, padx=10, pady=10)

img_shape_x_label = tk.Label(root, text="Image Shape X:")
img_shape_x_label.grid(row=2, column=0, padx=10, pady=10)
img_shape_x_entry = tk.Entry(root)
img_shape_x_entry.grid(row=2, column=1, padx=10, pady=10)

img_shape_y_label = tk.Label(root, text="Image Shape Y:")
img_shape_y_label.grid(row=3, column=0, padx=10, pady=10)
img_shape_y_entry = tk.Entry(root)
img_shape_y_entry.grid(row=3, column=1, padx=10, pady=10)

select_button = tk.Button(root, text="Select Image", command=select_image)
select_button.grid(row=4, column=0, padx=10, pady=10)

predict_button = tk.Button(root, text="Predict", command=predict)
predict_button.grid(row=4, column=1, padx=10, pady=10)

Guess you like

Origin blog.csdn.net/weixin_42469135/article/details/131318926