06 learning neural network learning --PyTorch migration VGG16

    Because we are trained from scratch a network model takes too long time, so using the migration study, which is to fine-tune the model has been trained and secondary training, to get better results faster.

import torch
import torchvision
from torchvision import datasets, models, transforms
import os
from torch.autograd import Variable
import matplotlib.pyplot as plt
import time

data_dir = "DogsVSCats"
data_transform = {x: transforms.Compose([transforms.Resize([224, 224]),  # 设置尺寸
                                        transforms.ToTensor(),  # 转为Tensor
                                        transforms.Normalize (Mean = [0.5, 0.5, 0.5], STD = [0.5, 0.5, 0.5])])   # normalized 
                  for X in { " Train " , " Valid " }}   # { "Train": "training set data format "," valid ":" test data format set "} 
image_datasets = {X: datasets.ImageFolder (= the os.path.join the root (data_dir, X),   # loading data 
                                         Transform = data_transform [X])
                   for X in { " Train " , " ! Valid "}}  #{ "train": "training set", "valid": "Set Test"} 
Dataloader = {X: torch.utils.data.DataLoader (DataSet = image_datasets [X], 
                                            the batch_size = 16 , 
                                            shuffle = True)
               for X in { " train " , " Valid " }}   # {package 16 as a batch of "train": "loading training data", "valid": "test data set Loading"} 
X_example, y_example = Next (ITER (the Dataloader [ " Train "]))   # Iterations to obtain a sample batch 
example_classes image_datasets = [ " Train" ] .Classes 
index_classes = image_datasets [ " Train " ] .class_to_idx 

Model = models.vgg16 (pretrained = True)   # using VGG16 network model pre-trained 
for Parma in model.parameters ():   # Set automatic gradient to false 
    Parma. = requires_grad False 

model.classifier = torch.nn.Sequential (   # modify layer fully connected automatically return to the default value of the gradient 
    torch.nn.Linear (25088, 4096 ), 
    torch.nn.ReLU (), 
    torch.nn.Dropout ( P = 0.5 ), 
    torch.nn.Linear ( 4096, 4096 ), 
    torch.nn.Dropout (P0.5 = ), 
    torch.nn.Linear ( 4096, 2 )) 
Use_gpu = torch.cuda.is_available ()
 IF Use_gpu:   # determines whether CUDA 
    Model = model.cuda () 

loss_f = torch.nn.CrossEntropyLoss ()   # Set residual loss 
Optimizer = torch.optim.Adam (model.classifier.parameters (), LR = 0.00001)   # use Adam optimization function 

epoch_n = 5 
time_open = time.time () 

for Epoch in the Range (epoch_n):
     Print ( " Epoch } {/} { " .format (Epoch,-epoch_n. 1 ))
    print("-"*10)
    for phase in {"train","valid"}:
        if phase == "train":
            print("Training...")
            model.train(True)
        else:
            print("Validing...")
            model.train(False)

        running_loss = 0.0
        running_corrects =0
         for BATCH, Data in the enumerate (Dataloader [Phase],. 1):   # the enumerate and index data obtained 
            X-, Y = Data
             IF Use_gpu: 
                X-, Y = Variable (X.cuda ()), Variable (y.cuda ( ))   # ************************************** 
            the else : 
                X-, Y = Variable (X- ), Variable (Y) 
            y_pred = Model (X-)   # forecast 
            _, Pred = torch.max (y_pred,. 1 ) 
            optimizer.zero_grad ()   # gradient zero 
            Loss = loss_f (y_pred, Y)   # 设置损失

            if phase == "train":
                loss.backward()  # 反向传播
                optimizer.step()  # 更新参数
            running_loss += loss.item()
            running_corrects += torch.sum(pred == y.data)

            if batch % 500 == 0 and phase == "train":
                print("Batch{},TrainLoss:{:.4f},Train ACC:{:.4f}".format(
                    batch, running_loss / batch, 100 * running_corrects / (16 * batch)))
        epocn_loss = running_loss * 16 / len(image_datasets[phase])
        epoch_acc = 100 * running_corrects / len(image_datasets[phase])
        print("{} Loss:{:.4f} Acc:{:4f}%".format(phase, epocn_loss, epoch_acc))
time_end = time.time() - time_open
print(time_end)

 

Guess you like

Origin www.cnblogs.com/zuhaoran/p/11504378.html