Error in deep learning code
2 次查看(过去 30 天)
显示 更早的评论
I am trying code. And gettiing an error like as follows:
----Error using load
Cannot read file C:\Users\HP\AppData\Local\Temp\imagenet-caffe-alex.mat.
----Error in helperImportMatConvNet (line 19)
matconvnet = load(matfile);
----Error in tranfdeeplearning (line 18)
net = helperImportMatConvNet(cnnMatFile);
%% Load Image Data
location = 'C:\Users\HP\Desktop\data';
imds = imageDatastore(location,'IncludeSubfolders',1,...
'LabelSource','foldernames');
tbl = countEachLabel(imds);
%% SeriesNetwork object can be used to inspect the network architecture,
% classify new data, and extract network activations from specific layers.
% Location of pre-trained "AlexNet"
cnnURL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-caffe-alex.mat';
cnnMatFile = fullfile(tempdir, 'imagenet-caffe-alex.mat');
if ~exist(cnnMatFile, 'file')
disp('Downloading pre-trained CNN model...');
websave(cnnMatFile, cnnURL);
end
net = helperImportMatConvNet(cnnMatFile);
%% Look at structure of pre-trained network
% Notice the last layer performs 1000 object classification
net.Layers
%% Perform net surgery
layers = net.Layers(1:end-3);
% %optional, Add new fully connected layer for 2 categories.
layers(end+1) = fullyConnectedLayer(64, 'Name', 'special_2');
layers(end+1) = reluLayer;
% introduce another layer : adding non-linearity
% improving the network's ability to handle data
% Not enough training 1.2 million (total)
layers(end+1) = fullyConnectedLayer(height(tbl), 'Name', 'fc8_2')
% Add the softmax layer and the classification layer which make up the
% remaining portion of the networks classification layers.
layers(end+1) = softmaxLayer
layers(end+1) = classificationLayer()
% Modify image layer to add randcrop data augmentation. This increases the
% diversity of training images. The size of the input images is set to the
% original networks input size.
layers(1) = imageInputLayer([227 227 3]);
%% Setup learning rates for fine-tuning
% fc 8 - bump up learning rate for last layers
layers(end-2).WeightLearnRateFactor = 10;
layers(end-2).WeightL2Factor = 1;
layers(end-2).BiasLearnRateFactor = 20;
layers(end-2).BiasL2Factor = 0;
%% Equalize number of images of each class in training set
minSetCount = min(tbl{:,2});
%Use splitEachLabel method to trim the set.
imds = splitEachLabel(imds, minSetCount);
% Notice that each set now has exactly the same number of images.
countEachLabel(imds)
[trainingDS, testDS] = splitEachLabel(imds, 0.7,'randomize');
% Convert labels to categoricals
trainingDS.Labels = categorical(trainingDS.Labels);
trainingDS.ReadFcn = @readFunctionTrain;
% % Setup test data for validation
% testDS.Labels = categorical(testDS.Labels);
% testDS.ReadFcn = @readFunctionValidation;
%% Fine-tune the Network
miniBatchSize = 50; % lower this if your GPU runs out of memory.
numImages = numel(trainingDS.Files);
% Run training for 5000 iterations. Convert 20000 iterations into the
% number of epochs this will be.
maxEpochs = 20;
lr = 0.0001;
opts = trainingOptions('sgdm', ...
'LearnRateSchedule', 'none',...
'InitialLearnRate', lr,...
'MaxEpochs', maxEpochs, ...
'MiniBatchSize', miniBatchSize);
net = trainNetwork(trainingDS, layers, opts);
save('trainedNet.mat','net')
save('testDS.mat','testDS')
return
2 个评论
Walter Roberson
2019-7-28
It appears something must have gone wrong in downloading the pre-trained model, leaving you with an incomplete or corrupted file.
The loading works on my system.
Walter Roberson
2019-7-28
See also https://www.mathworks.com/matlabcentral/answers/473835-getting-an-error-in-deep-learning-code where someone else has a question that is identical in all of the details.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!