text_plain_output_1.png
from time import time

from torch.nn.functional import softmax

from torch.utils.data import DataLoader

from torch.utils.data import DataLoader

from torchvision.datasets import ImageFolder

from torchvision.datasets import ImageFolder

from torchvision.transforms import Compose, CenterCrop, ToTensor

from torchvision.transforms import Compose, CenterCrop, ToTensor,ColorJitter

import glob

import glob

import numpy as np # linear algebra

import os

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import shutil

import shutil

import torch

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

import os
import glob
import shutil
train_data_dir = '/kaggle/input/1056lab-covid19-chest-xray-recognit/train'
working_dir = '/kaggle/working'
os.makedirs(working_dir + '/train')
os.makedirs(working_dir + '/train/COVID/')
os.makedirs(working_dir + '/train/Not_COVID/')
path_list = glob.glob(train_data_dir + '/COVID/*.png')
for path in path_list:
    shutil.copy(path, working_dir + '/train/COVID/')
for dir in ['Lung_Opacity', 'Normal', 'Viral_Pneumonia']:
    path_list = glob.glob(train_data_dir + '/' + dir + '/*.png')
    for path in path_list:
        shutil.copy(path, working_dir + '/train/Not_COVID/')

from torchvision.transforms import Compose, CenterCrop, ToTensor, ColorJitter
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
train_data_dir = '/kaggle/working/train'
transform = Compose([CenterCrop(224), ToTensor()])
transforms = ColorJitter(contrast=1)
train_data = ImageFolder(train_data_dir, transform=transform)
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
class_names = train_data.classes

from time import time
epoch_num = 10
start = time()
for epoch in range(1, epoch_num + 1):
    model_ft.train()
    sum_loss = 0.0
    count = 0
    optimizer.step()
    scheduler.step()
    for image, label in train_loader:
        if torch.cuda.is_available():
            image = image.cuda()
            label = label.cuda()
        y = model_ft(image)
        _, preds = torch.max(y, 1)
        loss = criterion(y, label)
        model_ft.zero_grad()
        loss.backward()
        optimizer.step()
        sum_loss += loss.item() * image.size(0)
        count += torch.sum(preds == label.data)
    train_loss = sum_loss / len(train_data)
    train_acc = float(count) / len(train_data)
    t = time() - start

import os
import glob
import shutil
test_data_dir = '/kaggle/input/1056lab-covid19-chest-xray-recognit/test'
working_dir = '/kaggle/working'
os.makedirs(working_dir + '/test')
os.makedirs(working_dir + '/test/COVID/')
os.makedirs(working_dir + '/test/Not_COVID/')
path_list = glob.glob(test_data_dir + '/*.png')
for path in path_list:
    shutil.copy(path, working_dir + '/test/COVID/')

from torchvision.transforms import Compose, CenterCrop, ToTensor
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
test_data_dir = '/kaggle/working/test'
test_data = ImageFolder(test_data_dir, transform=transform)
test_loader = DataLoader(test_data, batch_size=32, shuffle=False)

from torch.nn.functional import softmax
torch.no_grad()
y_pred = []
for image, label in test_loader:
    if torch.cuda.is_available():
        image = image.cuda()
        label = label.cuda()
    y = model_ft(image)
    y = softmax(y, dim=1).to('cpu')
    y_pred = np.concatenate([y_pred, y.detach().numpy()[:, 0]])
submit_df = pd.read_csv('/kaggle/input/1056lab-covid19-chest-xray-recognit/sampleSubmission.csv', index_col=0)
submit_df['COVID'] = y_pred
submit_df.to_csv('submission.csv')
submit_df