text_plain_output_1.png
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,cohen_kappa_score

from torchsummary import summary

from tqdm import tqdm

import numpy as np

import torch

import torch

import torchvision.transforms as transforms

import tqdm

import wandb

from torchvision import models
from torchsummary import summary
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RN_BCNN().to(device)
summary(model, (3, 224, 224))

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

transform = transforms.Compose([transforms.ToPILImage(), transforms.Resize(size=(224, 224)), transforms.ToTensor()])

path_data = '/kaggle/input/img-process/img_process/'
train_file = '/kaggle/input/vindr-birads/train_data_final.csv'
test_file = '/kaggle/input/vindr-birads/test_data_final.csv'

import cv2
from torch.utils.data import DataLoader
data_breast = {'train': CustomImageDataset(train_file, path_data, transform), 'test': CustomImageDataset(test_file, path_data, transform)}
dm = Datamodule(16, data_breast['train'], data_breast['test'])
import torchvision.models as models

dataloaders = {'train': torch.utils.data.DataLoader(data_breast['train'], batch_size=16, shuffle=True, num_workers=0), 'test': torch.utils.data.DataLoader(data_breast['test'], batch_size=16, shuffle=True, num_workers=0)}

from tqdm import tqdm
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, cohen_kappa_score
def train_model(model, criterion, optimizer, num_epochs=3):
    for epoch in range(num_epochs):
        for phase in ['train', 'test']:
            if phase == 'train':
                model.train()
            else:
                model.eval()
            y_label = []
            y_predict = []
            running_loss = 0.0
            running_corrects = 0
            for inputs, labels in tqdm(dataloaders[phase]):
                inputs = inputs.to(device)
                labels = labels.to(device)
                outputs = model(inputs)
                loss = criterion(outputs, labels)
                if phase == 'train':
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()
                _, preds = torch.max(outputs, 1)
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)
                y_label.extend(labels.cpu().numpy())
                y_predict.extend(np.squeeze(preds.cpu().numpy().T))
            epoch_loss = running_loss / len(data_breast[phase])
            epoch_acc = running_corrects.double() / len(data_breast[phase])
            confusion_mtx = confusion_matrix(y_label, y_predict)
            f1_scores = f1_score(y_label, y_predict, average=None)
            precision_scores = precision_score(y_label, y_predict, average=None)
            recall_scores = recall_score(y_label, y_predict, average=None)
            kappas = cohen_kappa_score(y_label, y_predict)
            wandb.log({'epoch': epoch, phase + 'loss': epoch_loss, phase + 'acc': epoch_acc, 'f1_score_0': f1_scores[0], 'f1_score_1': f1_scores[1], 'f1_score_2': f1_scores[2], 'f1_score_3': f1_scores[3], 'f1_score_4': f1_scores[4], 'precision_score_0': precision_scores[0], 'precision_score_1': precision_scores[1], 'precision_score_2': precision_scores[2], 'precision_score_3': precision_scores[3], 'precision_score_4': precision_scores[4], 'recall_0': recall_scores[0], 'recall_1': recall_scores[1], 'recall_2': recall_scores[2], 'recall_3': recall_scores[3], 'recall_4': recall_scores[4], 'kappa': kappas})
    return model

import wandb
run = wandb.init(project='Breast-density-classification', reinit=True)
wandb.run.name = 'RN-BCNN'