[Dogs and cats] define the model data set and training model

Until you are ready data set:

Create a data set: https://www.cnblogs.com/xiximayou/p/12398285.html

Reading data sets: https://www.cnblogs.com/xiximayou/p/12422827.html

This section we want to define the model and then begin training it.

First is the directory in Google colab as follows:

Rdata is where we read the data file, it will transform the look:

from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as transforms
import torch

def load_dataset(batch_size):
  #预处理
  transform = transforms.Compose([transforms.RandomResizedCrop(224),transforms.ToTensor()])
  path = " / Content / Drive / My Drive / Colab Notebooks / Data / dogcat " 
  train_path = path + " / train " 
  test_path = path + " / test " 
  # use torchvision.datasets.ImageFolder read data train and test sets specified folder 
  train_data torchvision.datasets.ImageFolder = (train_path, = Transform Transform)
  train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=1)
  
  test_data = torchvision.datasets.ImageFolder(test_path, transform=transform)
  test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True, num_workers=1)
  """
  print (train_data.classes) # according to the name of the sub-folder to determine the category
  print (train_data.class_to_idx) # these categories in order to define an index of 0, 1 ...
  print (train_data.imgs) # returned to get pictures from all folders in the path, and its category

  print (test_data.classes) # according to the name of the sub-folder to determine the category
  print (test_data.class_to_idx) # these categories in order to define an index of 0, 1 ...
  print (test_data.imgs) # returned to get pictures from all folders in the path, and its category
  """
  return train_loader,test_loader,train_data,test_data

When data enhancement, we have only two options, one is randomly cut to 224 × 224 image size, because the input size is the most networks, while translating it into tensor. It should be noted that: ToTensor () after all of the data to be enhanced, in addition to standardization. Since ToTensor () converts the image of the tensor pytorch type, while also converting each pixel value between 0-1.

Ultimately, we want to return to that train_loader, test_loader, train_data, test_data.

train_loader, test_loader: you do not have to say, used to load data sets

train_data, test_data: This is the last pass in order to obtain the length of the data set.

You can then define the model in train.py in and trained.

resnet.py resnet model is stored, there is a copy from the torchvision pytorch resnet over the course torchvision we can directly use the model, which encapsulates many models.

Model structure:

serious (
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (resume): reversed (inplace = True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (1): BasicBlock(
      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer2): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer3): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer4): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (resume): reversed (inplace = True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=512, out_features=2, bias=False)
)

In train.py, we step by step to resolve:

Import SYS
 # To avoid file in the corresponding directory is not found, the directory path is added to the 
sys.path.append ( " / Content / Drive / My Drive / Colab Notebooks " )
 from utils Import RDATA
 from Model Import ResNet
 Import Torch AS nn .nn
 Import Torch
 Import numpy AS NP
 Import torchvision

# Set random seed 
np.random.seed (0)
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)

# Torch.backends.cudnn.deterministic generally set to True to 
# if the structure of the network is not constantly changing, is fixed, the 
# torch.backends.cudnn.benchmark set True 
torch.backends.cudnn.deterministic = True
 # = False torch.backends.cudnn.benchmark 
torch.backends.cudnn.benchmark = True

# The data put into the model and gpu there are two ways, one is model.to (device), the other is model.cuda () 
Device torch.device = ( ' CUDA '  IF torch.cuda.is_available ( ) the else  ' CPU ' )

# Is input to the network every time the image number
batch_size=128
# Read data 
train_loader, test_loader, train_data, TEST_DATA = rdata.load_dataset (the batch_size)
 # For convenience, we get directly from torchvision the model, but the model is imagenet default data set categories are categories 1000, we obtain the following manner non-model pre-trained, and modify the last layer is an all-connections 2 
model = torchvision.models.resnet18 (pretrained = False)
model.fc = nn.Linear(model.fc.in_features,2,bias=False)
model.cuda()
#print(model) 

# Definition of training epochs
num_epochs=50
# Define learning rate
learning_rate=0.01
# Define loss function 
Criterion = nn.CrossEntropyLoss ()
 # Optimizer # = torch.optim.Adam (model.parameters (), LR = learning_rate) 
# define optimization method, for simplicity, is decreased by an amount of the stochastic gradient drive 
optimizer = torch .optim.SGD (params = model.parameters (), LR = 0.1, Momentum = 0.9 ,
                          weight_decay = 1E-*. 1. 4 )
 # calculates STEP 
total_step = len (train_loader)
 # define model training function 
DEF Train ():
   # print output of each epoch 
  for epoch in Range (num_epochs):
       # in order to calculate the loss for each epoch
      tot_loss = 0.0
      # Calculate the correct number of each epoch is 
      correct = 0
       # I is a step, images are images tensor, lables is a label 
      for I, (images, Labels) in the enumerate (train_loader):
           # The data put into the GPU 
          images = images.cuda ()
          labels = labels.cuda()

          # Forward Pass 
          # picture tensor into network computing output 
          the Outputs = Model (ImagesRF Royalty Free)
           # get the results of a large probability 
          _, preds = torch.max (outputs.data, 1 )
          loss = criterion(outputs, labels)

          # Backward and Optimizer 
          # backpropagation network optimization parameters 
          optimizer.zero_grad ()
          loss.backward()
          optimizer.step()
          # Cumulative loss of each step of 
          tot_loss + = loss.data
           # every two step to print the current loss 
          IF (I +. 1)% 2 == 0:
               Print ( ' Epoch: [{} / {}], the Step: [{} / {}], Loss: {:} .4f ' 
                    .format (Epoch + 1'd, num_epochs, I +. 1 , total_step, loss.item ()))
           # statistics correct number of 
          correct + = torch.sum (preds == labels.data) .to (torch.float32)
       # ## Epoch info #### 
      # Epoch loss 
      epoch_loss = tot_loss / len (train_data)
       Print ( ' Train loss: ', epoch_loss)
      #epoch准确率
      epoch_acc = correct/len(train_data)
      print('train' + ' acc: ', epoch_acc)
train()


    

On the relationship between the step, epoch, batch_size can see:

https://www.cnblogs.com/xiximayou/p/12405485.html

Finally, we enter the command in tesy.ipynb train, the train but must first go to the next directory:

cd /content/drive/My Drive/colab notebooks/train
Then enter:
!python train.py
Look at some results:

Then to complete the first epoch:

And then complete the last epoch:

There is a 93% accuracy rate. This is just a simple training. 

Find and train acc train loss in output is not very good, so deal with it:

      epoch_loss = tot_loss/len(train_data)
      print('train loss: {:.4f}'.format(epoch_loss))
      epoch_acc = correct/len(train_data)
      print('train acc: {:.4f}',.format(epoch_acc))
 
Next: storage model and tested.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12448300.html