This article mainly introduces about Pytorch + visdom CNN processing self-built image data set method, has a certain reference value, now share to everyone, have the need of friends can refer to
Environment
System: WIN10
Cpu:i7-6700hq
gpu:gtx965m
python:3.6
pytorch:0.3
Data download
Source from Sasank chilamkurthy tutorial; Data: Download link.
Download and then unzip to the project root directory:
Data sets are used to classify ants and bees. There are about 120 training images, each of which has 75 validation images.
Data import
You can use the Torchvision.datasets.ImageFolder (root,transforms) module to convert a picture to tensor.
Define transform First:
ata_transforms = { ' train ': Transforms. Compose ([ # randomly cut into 224x224 size picture Unified picture Format transforms. Randomresizedcrop (224), # image rollover transforms. Randomhorizontalflip (), # Totensor Normalization (0,255) >> (0,1) normalize channel= (channel-mean)/std Transforms. Totensor (), transforms. Normalize (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), "Val": Transforms. Compose ([ # Picture size Zoom Unified picture Format transforms. Resize, # Transforms with center clipping . Centercrop (224), transforms. Totensor (), transforms. Normalize (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) }
Import, Load data:
Data_dir = './hymenoptera_data ' # trans dataimage_datasets = {x:datasets. Imagefolder (Os.path.join (Data_dir, X), data_transforms[x]) for x in [' Train ', ' Val ']}# load datadata_loaders = {X:dataloa Der (Image_datasets[x], batch_size=batch_size, shuffle=true) for x in [' Train ', ' val ']}data_sizes = {X:len (image_ DATASETS[X]) for x in [' Train ', ' val ']}class_names = image_datasets[' Train '].classesprint (data_sizes, Class_names)
{' Train ': 244, ' val ': 153} [' ants ', ' bees ']
Training set 244 picture, test set 153 picture.
Visual part of the picture to see, because visdom support tensor input, do not change to numpy, directly with the tensor calculation can:
Inputs, classes = Next (ITER (data_loaders[' Val '))) out = Torchvision.utils.make_grid (inputs) INP = Torch.transpose (out, 0 , 2) mean = torch. Floattensor ([0.485, 0.456, 0.406]) std = torch. Floattensor ([0.229, 0.224, 0.225]) INP = std * inp + MEANINP = torch.transpose (INP, 0, 2) viz.images (INP)
Create a CNN
NET according to the processing of the previous article CIFAR10 changed the specifications:
Class CNN (NN. Module): Def __init__ (self, In_dim, N_class): Super (CNN, self). __init__ () self.cnn = nn. Sequential (NN. Batchnorm2d (In_dim), nn. ReLU (True), nn. Conv2d (In_dim, 7), # 224 >> 218 nn. batchnorm2d (+), nn. ReLU (inplace=true), nn. Maxpool2d (2, 2), # 218 >> 109 nn. ReLU (True), nn. conv2d (+, 5), # for the NN. batchnorm2d (+), nn. ReLU (True), nn. conv2d (+, 5), # 101 nn. batchnorm2d (+), nn. ReLU (True), nn. Conv2d (3, 1, 1), nn. batchnorm2d (+), nn. ReLU (True), nn. Maxpool2d (2, 2), # 101 >> nn. Conv2d (3, 1, 1), # NN. batchnorm2d (+), nn. ReLU (True), nn. Maxpool2d (3), # >> SELF.FC = nn. Sequential (NN. Linear (128*16*16), nn. BATCHNORM1D (+), nn. ReLU (True), nn. Linear (N_class)) def forward (self, x): Off = self.cnn (x) out = SELF.FC (Out.view ( -1, 128*16*16)) return out# Input 3-layer RGB, Output Category 2 modeL = CNN (3, 2)
Loss, optimization function:
line = Viz.line (y=np.arange) Loss_f = nn. Crossentropyloss () optimizer = Optim. SGD (Model.parameters (), LR=LR, momentum=0.9) scheduler = Optim.lr_scheduler. STEPLR (Optimizer, step_size=7, gamma=0.1)
Parameters:
Batch_size = 4LR = 0.001EPOCHS = 10
Run the 10 epoch to see:
[9/10] TRAIN_LOSS:0.650|TRAIN_ACC:0.639|TEST_LOSS:0.621|TEST_ACC0.706[10/10] train_loss:0.645|train_acc:0.627| Test_loss:0.654|test_acc0.686training complete in 1m 16sBest Val acc:0.712418
Run 20 to see:
[19/20] train_loss:0.592|train_acc:0.701|test_loss:0.563|test_acc0.712[20/20] train_loss:0.564|train_acc:0.721| Test_loss:0.571|test_acc0.706training complete in 2m 30sBest Val acc:0.745098
Low accuracy: only 74.5%
We use the resnet18 in models to run the 10 epoch:
Model = torchvision.models.resnet18 (True) num_ftrs = MODEL.FC.IN_FEATURESMODEL.FC = nn. Linear (Num_ftrs, 2)
[9/10] TRAIN_LOSS:0.621|TRAIN_ACC:0.652|TEST_LOSS:0.588|TEST_ACC0.667[10/10] train_loss:0.610|train_acc:0.680| Test_loss:0.561|test_acc0.667training complete in 1m 24sBest Val acc:0.686275
The effect is also very general, want to train in a short time the effect is very good models, we can download the well-trained state, on this basis training:
Model = torchvision.models.resnet18 (pretrained=true) num_ftrs = MODEL.FC.IN_FEATURESMODEL.FC = nn. Linear (Num_ftrs, 2)
[9/10] TRAIN_LOSS:0.308|TRAIN_ACC:0.877|TEST_LOSS:0.160|TEST_ACC0.941[10/10] train_loss:0.267|train_acc:0.885| Test_loss:0.148|test_acc0.954training complete in 1m 25sBest Val acc:0.954248
10 Epoch direct to 95% accuracy rate.