text_plain_output_100.png text_plain_output_84.png text_plain_output_56.png text_plain_output_35.png text_plain_output_98.png text_plain_output_43.png text_plain_output_78.png text_plain_output_37.png text_plain_output_90.png text_plain_output_79.png text_plain_output_5.png text_plain_output_75.png text_plain_output_48.png text_plain_output_30.png text_plain_output_73.png text_plain_output_15.png text_plain_output_70.png text_plain_output_9.png text_plain_output_44.png text_plain_output_86.png text_plain_output_40.png text_plain_output_74.png text_plain_output_31.png text_plain_output_20.png text_plain_output_101.png text_plain_output_60.png text_plain_output_68.png text_plain_output_4.png text_plain_output_65.png text_plain_output_64.png text_plain_output_13.png text_plain_output_52.png text_plain_output_66.png text_plain_output_45.png text_plain_output_14.png text_plain_output_32.png text_plain_output_88.png text_plain_output_29.png text_plain_output_58.png text_plain_output_49.png text_plain_output_63.png text_plain_output_27.png text_plain_output_76.png text_plain_output_54.png text_plain_output_10.png text_plain_output_6.png text_plain_output_92.png text_plain_output_57.png text_plain_output_24.png text_plain_output_21.png text_plain_output_47.png text_plain_output_25.png text_plain_output_77.png text_plain_output_18.png text_plain_output_50.png text_plain_output_36.png text_plain_output_96.png text_plain_output_87.png text_plain_output_3.png text_plain_output_22.png text_plain_output_81.png text_plain_output_69.png text_plain_output_38.png text_plain_output_7.png text_plain_output_91.png text_plain_output_16.png text_plain_output_59.png text_plain_output_71.png text_plain_output_8.png text_plain_output_26.png text_plain_output_41.png text_plain_output_34.png text_plain_output_85.png text_plain_output_42.png text_plain_output_67.png text_plain_output_53.png text_plain_output_23.png text_plain_output_89.png text_plain_output_51.png text_plain_output_28.png text_plain_output_72.png text_plain_output_99.png text_plain_output_2.png text_plain_output_97.png text_plain_output_1.png text_plain_output_33.png text_plain_output_39.png text_plain_output_55.png text_plain_output_82.png text_plain_output_93.png text_plain_output_19.png image_output_2.png image_output_1.png text_plain_output_80.png text_plain_output_94.png text_plain_output_17.png text_plain_output_11.png text_plain_output_12.png text_plain_output_62.png text_plain_output_95.png text_plain_output_61.png text_plain_output_83.png text_plain_output_46.png
from PIL import Image

from torch import nn

from torch.optim import Adam

from torch.utils.data import Dataset, DataLoader

from torchvision import transforms

import os

import pandas as pd

import timm

import torch

import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from torch.optim import Adam
import glob
from tqdm.notebook import tqdm
from PIL import Image
import torchvision
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report, ConfusionMatrixDisplay, confusion_matrix, roc_curve, auc
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

class BreastCancerDataset(Dataset):
    def __init__(self, data_path_file, train=True):
        super(Dataset, self).__init__()
        self.data = pd.read_csv(data_path_file, index_col=0)
        self.global_path = '/kaggle/input/meta-data/Data_image'
        if train:
            self.transform = transforms.Compose([transforms.Grayscale(num_output_channels=3), transforms.Resize(size=(224, 224)), transforms.RandomHorizontalFlip(), transforms.RandomRotation(10), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
        else:
            self.transform = transforms.Compose([transforms.Grayscale(num_output_channels=3), transforms.Resize(size=(224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
    def __len__(self):
        return len(self.data)
    def __getitem__(self, idx):
        row = self.data.iloc[idx]
        label = row['status']
        img_path = os.path.join(self.global_path, row['Path'])
        labels = torch.tensor(label)
        image = Image.open(img_path)
        if self.transform:
            image = self.transform(image)
        return (image, labels)

def save_model(epochs, model, optimizer, criterion, pretrained='True'):
    """
    Function to save the trained model to disk.
    """
    torch.save({'epoch': epochs, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), 'loss': criterion}, f'/kaggle/working/model_pretrained_{pretrained}_{epochs}.pth')

import timm
model = timm.create_model('hrnet_w18', pretrained=True, num_classes=3)
criterion = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.001)