<jupyter_start><jupyter_text><jupyter_code># http://pytorch.org/
from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision
import torch
# 匯入套件
import pandas as pd
from glob import glob
import os
from shutil import copyfile
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
from numpy.random import permutation
import matplotlib.pyplot as plt
from torchvision import transforms
from torchvision.datasets import ImageFolder
from torchvision.models import resnet18,resnet34
from torchvision.models.inception import inception_v3
from torch.utils.data import DataLoader
from torch.autograd import Variable
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F

import pickle
%matplotlib inline
!pip install Pillow==4.1.1
!pip install PIL
!pip install image
import PIL.Image
# 殘差神經網路基本架構

class ResNetBasicBlock(nn.Module):
    
    def __init__(self,in_channels,out_channels,stride):

        super().__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size = 3, stride = stride, padding = 1, bias = False)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size = 3, stride = stride, padding = 1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.stride = stride
        
    def forward(self,x):
        
        residual = x
        out = self.conv1(x)
        out = F.relu(self.bn1(out), inplace = True)
        out = self.conv2(out)
        out = self.bn2(out)
        out += residual
        return F.relu(out)  
from torchvision.models import resnet18

resnet = resnet18(pretrained = False)

resnet
from google.colab import drive

drive.mount("/content/drive")
# 資料轉換
from torchvision import transforms
from torchvision.datasets import ImageFolder

data_transform = transforms.Compose([
        transforms.Resize((299,299)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    
# For Dogs & Cats dataset

train_dset = ImageFolder('/content/drive/My Drive/Colab Notebooks/dogsandcats/train',transform = data_transform)
val_dset = ImageFolder('/content/drive/My Drive/Colab Notebooks/dogsandcats/test',transform = data_transform)
classes = 2
train_dset
val_dset
# 訓練集與驗證集

from torch.utils.data import DataLoader

train_loader = DataLoader(train_dset,batch_size=32,shuffle=False,num_workers=3)
val_loader = DataLoader(val_dset,batch_size=32,shuffle=False,num_workers=3)

#Create ResNet model

import torch
from torchvision.models import resnet34

my_resnet = resnet34(pretrained=True)


is_cuda = torch.cuda.is_available()

is_cuda
list(my_resnet.children())
# 扣除網路架構最後的AvgPool2D
list(my_resnet.children())[:-1]
if is_cuda:
    my_resnet = my_resnet.cuda()

my_resnet = nn.Sequential(*list(my_resnet.children())[:-1])
my_resnet
!pip install torchsummary
from torchsummary import summary

summary(my_resnet, (3, 299, 299))
list(my_resnet.parameters())
for p in my_resnet.parameters():
    p.requires_grad = False
#For training data

# Stores the labels of the train data
trn_labels = []

# Stores the pre convoluted features of the train data
trn_features = []

m = nn.Sequential(*list(my_resnet.children())[:-1])

#Iterate through the train data and store the calculated features and the labels
for d,la in train_loader:
    o = m(Variable(d.cuda()))
    o = o.view(o.size(0),-1)
    trn_labels.extend(la)
    trn_features.extend(o.cpu().data)

#For validation data

#Iterate through the validation data and store the calculated features and the labels
val_labels = []
val_features = []
for d,la in val_loader:
    o = m(Variable(d.cuda()))
    o = o.view(o.size(0),-1)
    val_labels.extend(la)
    val_features.extend(o.cpu().data)
np.shape(trn_features[0])
list(d.cuda())
np.shape(o.cpu().data)
np.shape(m(Variable(d.cuda())).view(o.size(0),-1))<jupyter_output><empty_output><jupyter_text><jupyter_code>class FeaturesDataset(Dataset):
    def __init__(self,featlst,labellst):
        self.featlst = featlst
        self.labellst = labellst
    def __getitem__(self,index):
        return (self.featlst[index],self.labellst[index])
    def __len__(self):
        return len(self.labellst)
#Creating dataset for train and validation
trn_feat_dset = FeaturesDataset(trn_features,trn_labels)
val_feat_dset = FeaturesDataset(val_features,val_labels)

#Creating data loader for train and validation
trn_feat_loader = DataLoader(trn_feat_dset,batch_size=64,shuffle=True)
val_feat_loader = DataLoader(val_feat_dset,batch_size=64)
len(list(trn_feat_dset))
class FullyConnectedModel(nn.Module):
    def __init__(self,in_size,out_size):
        super().__init__()
        self.fc = nn.Linear(in_size,out_size)

    def forward(self,inp):
        out = self.fc(inp)
        return out
        
fc_in_size = 51200

fc = FullyConnectedModel(fc_in_size,classes)

if is_cuda:
    fc = fc.cuda()
    
optimizer = optim.Adam(fc.parameters(),lr=0.0001)
def fit(epoch,model,data_loader,phase='training',volatile=False):
    if phase == 'training':
        model.train()
    if phase == 'validation':
        model.eval()
        volatile=True
    running_loss = 0.0
    running_correct = 0
    for batch_idx , (data,target) in enumerate(data_loader):
        if is_cuda:
            data,target = data.cuda(),target.cuda()
        data , target = Variable(data,volatile),Variable(target)
        if phase == 'training':
            optimizer.zero_grad()
        output = model(data)
        loss = F.cross_entropy(output,target)
        
        running_loss += F.cross_entropy(output,target,size_average=False).data[0]
        preds = output.data.max(dim=1,keepdim=True)[1]
        running_correct += preds.eq(target.data.view_as(preds)).cpu().sum()
        if phase == 'training':
            loss.backward()
            optimizer.step()
    
    loss = float(running_loss)/float(len(data_loader.dataset))
    accuracy = 100. * float(running_correct)/float(len(data_loader.dataset))
    
    print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {float(running_correct)}/{float(len(data_loader.dataset))}{accuracy:{10}.{4}}')
    return loss,accuracy
train_losses , train_accuracy = [],[]
val_losses , val_accuracy = [],[]

for epoch in range(1,10):
    epoch_loss, epoch_accuracy = fit(epoch, fc, trn_feat_loader, phase = 'training')
    val_epoch_loss , val_epoch_accuracy = fit(epoch, fc, val_feat_loader, phase = 'validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)
list(trn_feat_loader.dataset)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12059946274565247, 'fraction_numerical': 0.019228050332249398, 'mean_word_length': 4.186217008797654, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '636597', 'license': 'MIT', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2487, 'n_words': 502, 'path': '1_ResNet.ipynb', 'repo_name': 'CaoCharles/Deep-Learning-with-PyTorch', 'size': '7073'}