preface

Okay… Recently, there are more things and less blog posts, so I decided to make a big news. This blog has recorded in detail the process of vehicle area detection (R-CNN) and vehicle identification (AlexNet) using Matlab. And contains training data sets, test data sets and source code. The training dataset is a Model database from Stanford University that contains 196 different models. Writing here I really want to make fun of the Audi series in this database: many black and white pictures ah hello!! AlexNet data input dimension is 3 when doing training. I found a lot of pictures myself!! … .

The environment

Test environment: Hardware: Intel I5-4590 GTX 980 Software: Matlab R2016b (only this version implements RCNN…)

Download of data sets

Well. Train /test: pan.baidu.com/s/1miTn9jy I formatted the data set, changed the images to 227*227, and replaced a few black and white images: pan.baidu.com/s/1pKIbQiB The following is the model label file corresponding to each picture: pan.baidu.com/s/1nuOR7PR

Download AlexNet in Matlab

AlexNet was the ImageNet 2012 champion. It has a total of 8 layers, including 5 convolution layers, 2 full connection layers and 1 classification layer. If it is used to carry out forward propagation of an image, then the probability that the final output image belongs to which one of 1000 objects. Here I define AlexNet in Matlab, and this is my code and network structure:

clear clc doTraining = true; % if the training data = % % extract data load ('. / data/carDatasetGroundTruth. Mat '); % vehicleDataset = data.carDataset; % type table, containing the file path and groundTruth data = load ('. / data/vehicleDatasetGroundTruth. Mat '); vehicleDataset = data.vehicleDataset; % type table, Contains the path to the file and add the absolute path to the vehicleDataset groundTruth % vehicleDataset. ImageFilename = fullfile ([PWD, '/data/'],vehicleDataset.imageFilename); % displays an image in the dataset to understand the type of image it contains. VehicleDataset (1:4,:) % Displays a partial data situation % divides the data set into two parts: a training set for training the detector and a test set for evaluating the detector. % select 70% of the data for training and the rest for evaluation. rng(0); % control of random number generation shuffledIndices = randperm(height(vehicleDataset)); Idx = floor(0.7 * length(shuffledIndices)); trainingDataTbl = vehicleDataset(shuffledIndices(1:idx),:); testDataTbl = vehicleDataset(shuffledIndices(idx+1:end),:); % Save data and labels imdsTrain = imageDatastore(trainingDataTbl{:,'imageFilename'}); % path bldsTrain = boxLabelDatastore(trainingDataTbl(:,'vehicle')); ImdsTest = imageDatastore(testDataTbl{:,'imageFilename'}); bldsTest = boxLabelDatastore(testDataTbl(:,'vehicle')); TrainingData = combine(imdsTrain,bldsTrain); % combine file path and real box testData = combine(imdsTest,bldsTest); % data = read(trainingData); % data includes picture data, real box coordinates, category I = data{1}; bbox = data{2}; annotatedImage = insertShape(I,'Rectangle',bbox); % annotatedImage = imresize(annotatedImage,2); Figure imshow(annotatedImage) % create yOLO network inputSize = [224 224 3]; numClasses = width(vehicleDataset)-1; % % category number by column number of the table calculation used to assess the anchor box number trainingDataForEstimation = transform (trainingData, @ (data) preprocessData (data, inputSize)); numAnchors = 7; [anchorBoxes, meanIoU] = estimateAnchorBoxes(trainingDataForEstimation, NumAnchors) % feature extraction layer adopts resnet50 featureExtractionNetwork = resnet50; featureLayer = 'activation_40_relu'; % set yolo network lgraph = yolov2Layers (inputSize numClasses, anchorBoxes, featureExtractionNetwork, featureLayer); AugmentedTrainingData = transform(trainingData,@augmentData); % augmentedData = cell(4,1); for k = 1:4 data = read(augmentedTrainingData); augmentedData{k} = insertShape(data{1},'Rectangle',data{2}); reset(augmentedTrainingData); Figure montage(augmentedData,'BorderSize',10) % preprocessedTrainingData = transform(augmentedTrainingData,@(data)preprocessData(data,inputSize)); data = read(preprocessedTrainingData); I = data{1}; bbox = data{2}; annotatedImage = insertShape(I,'Rectangle',bbox); annotatedImage = imresize(annotatedImage,2); Figure imshow(annotatedImage) % trainingOptions = trainingOptions(' SGDM ',... 'MiniBatchSize', 16, .... 'InitialLearnRate',1e-3, ... 'MaxEpochs',20,... 'CheckpointPath', tempdir, ... 'Shuffle','never'); If doTraining % [the detector, the info] = trainYOLOv2ObjectDetector training YOLOv2 detector (preprocessedTrainingData lgraph, options); Pretrained = load('yolov2_mytrain.mat'); detector = pretrained.detector; End % test the trained model and display I = imread (testDataTbl. ImageFilename {4}); I = imresize(I,inputSize(1:2)); [bboxes,scores] = detect(detector,I); I = insertObjectAnnotation(I,'rectangle',bboxes,scores); Figure imshow(I) % preprocessedTestData = transform(testData,@(data)preprocessData(data,inputSize)); % detectionResults = detect(Detector, preprocessedTestData); % / ap, recall and precision evaluation accuracy = evaluateDetectionPrecision (detectionResults preprocessedTestData); figure plot(recall,precision) xlabel('Recall') ylabel('Precision') grid on title(sprintf('Average Precision = %.2f',ap))Copy the code