application_vnd.jupyter.stderr_output_1.png
from PIL import Image

from fastai.vision import ImageDataBunch,Learner

from torch import optim,nn

from torchvision import transforms,models

import os

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

import torch

import numpy as np
import pandas as pd
import fastai as fa
import os

from fastai.vision import ImageDataBunch, Learner
from PIL import Image
import torch
from torch import optim, nn
import cv2 as cv
from torchvision import transforms, models
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
PATH = '../input'
df_train = pd.read_csv(PATH + '/train.csv', dtype={'id_code': str, 'diagnosis': int})
norm_values = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
dataBunch = ImageDataBunch.from_df(path=PATH, df=df_train, folder='train_images', suffix='.png', size=224, ds_tfms=get_transforms(do_flip=True, max_warp=0), test='test_images', bs=32, device=device).normalize(norm_values)
model = models.resnext101_32x8d(pretrained=True)
for param in model.parameters():
    param.requires_grad = False
fc = nn.Sequential(nn.Dropout(p=0.6), nn.Linear(2048, 500), nn.BatchNorm1d(500), nn.ReLU(), nn.Linear(500, 5), nn.LogSoftmax(dim=1))
model.fc = fc
model.to(device)
learn = Learner(data=dataBunch, model=model, model_dir='/tmp/model', metrics=[accuracy])

learn.fit_one_cycle(7, 0.008)

learn.save('bl')
model = learn.load('bl').model

class CreateTrainDataset(Dataset):
    def __init__(self, csv_file, root_dir, transform=None):
        self.data_frame = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform
    def __len__(self):
        return len(self.data_frame)
    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir, self.data_frame.iloc[idx, 0] + '.png')
        image = Image.open(img_name)
        sample = {'image': transforms.ToTensor()(image)}
        return sample
testset = CreateTrainDataset(csv_file='../input/test.csv', root_dir='../input/test_images')
testloader = torch.utils.data.DataLoader(testset, batch_size=1)
model.eval()
diag = []
idc = []
dd = pd.read_csv('../input/test.csv')
idc = dd['id_code']
idc = list(idc)
for id, d in enumerate(testloader):
    images = d['image']
    images = images.to(device)
    output = model(images)
    pred = torch.exp(output)
    top_ps, top_class = pred.topk(1, dim=1)
    diag.append(int(top_class.cpu()))
print(type(list(diag)))