# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Pytorch: RESNET-34
#
# ## 97.3% accuracy

# standard modules
import os
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
# pytorch modules
import torch as torch
from torch import nn
from torch import conv2d, convolution
import torchvision.models as models
from torchvision.models import * 
# fast.ai modules
from fastai import *
from fastai.basics import *
from fastai.script import *
from fastai.vision import *
from fastai.vision.models.wrn import wrn_22
from fastai.distributed import *
# GPU config
torch.backends.cudnn.benchmark = True

path = Path('data/')
torch.device('cuda', 0)

for folder in ('NORMAL', 'CNV', 'DME', 'DRUSEN'):
    print(folder)
  #  verify_images(path/folder, delete=True, max_size=500)

sz=299 # image size
bs=32 # batch size
data = ImageDataBunch.from_folder(path, train='.', 
                                  valid_pct=0.2,
                                  ds_tfms=get_transforms(), 
                                  size=sz,
                                  bs=bs,
                                  no_check=True,
                                  num_workers=4).normalize(imagenet_stats)

data.classes

# build and train model 
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4) # run model with 4 cycles

# +
# save/load model
learn.save('stage-1-128')
learn.load('stage-1-128')

# classification model results
interpretation = ClassificationInterpretation.from_learner(learn)

# Plot the confusion matrix evaluate errors
interpretation.plot_confusion_matrix()

# +
# find the best learning rate
learn.unfreeze()
learn.lr_find()

# plot the learning rate
learn.recorder.plot()
# -

# train the model using new learning rate 2 cycles
lr = 1e-04 # learning rate
learn.fit_one_cycle(2, slice(lr))

# create new ImageDataBunch with size 256
data_bigger = ImageDataBunch.from_folder(path,
                                        train=".",
                                        valid_pct=.2,
                                        ds_tfms=get_transforms(),
                                        size=256,
                                        num_workers=4).normalize(imagenet_stats)
# update the learn data for image size
learn.data = data_bigger
# unfreeze the model for learning rates
learn.unfreeze()
# plot the learning rate graph
lr_find(learn)
learn.recorder.plot()

# update learning rate, train
previous_lr = 1e-05
learn.fit_one_cycle(3, slice(1e-04, previous_lr/5))

learn.save('stage-2-transfer')

learn.load('stage-2-transfer')
# plot confusion matrix
interpretation = ClassificationInterpretation.from_learner(learn)
interpretation.plot_confusion_matrix()

# load stage 2 transfer
learn = create_cnn(data, models.resnet34, metrics=accuracy)

learn.fit_one_cycle(1)

# +
learn.load('stage-2-transfer')

# create new learner with the single data 
learn1 = create_cnn(d, models.resnet34, metrics=accuracy)

# load the previous model
learn1.load('stage-2-transfer')

classes = data.classes

# single data ImageDataBunch
d = ImageDataBunch.single_from_classes(path, 
                                       classes,
                                       ds_tfms=get_transforms(), 
                                       size=256).normalize(imagenet_stats)

# +
# load resize display test images
img_path='data/test/test/CNV-6294785-1.jpeg'
img_path2='data/test/test/DME-9721607-2.jpeg'
img_path3='data/test/test/DRUSEN-9928043-1.jpeg'
img_path4='data/test/test/NORMAL-9053621-1.jpeg'

# predictions, feed image
img = open_image(img_path)
img.show()

# return the predicted class, the index and the outputs
predicted_class, predicted_index, outputs = learn1.predict(img)
print('Class: ', predicted_class)

# +
# predictions, feed image
img2 = open_image(img_path2)
img2.show()

# Get the predicted class, the index and the outputs
predicted_class, predicted_index, outputs = learn1.predict(img2)
print('Class: ', predicted_class)

# +
# predictions, feed image
img3 = open_image(img_path3)
img3.show()

# Get the predicted class, the index and the outputs
predicted_class, predicted_index, outputs = learn1.predict(img3)
print('Class: ', predicted_class)

# +
# predictions, feed image
img4 = open_image(img_path4)
img4.show()

# Get the predicted class, the index and the outputs
predicted_class, predicted_index, outputs = learn1.predict(img4)
print('Class: ', predicted_class)
# -

preds,y = learn.get_preds()
interp = ClassificationInterpretation(data, preds, y, loss_class=nn.CrossEntropyLoss)

StarcoderdataJupyterScriptsDedupFiltered