-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlex_net_scratch.m
91 lines (62 loc) · 2.73 KB
/
Alex_net_scratch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
clear
rng('shuffle')
gpuDevice(1)
%url = 'http://download.tensorflow.org/example_images/flower_photos.tgz';
downloadFolder='C:\Users\rrachako\Downloads\Matlab_DCNN_training\256_ObjectCategories';
%filename='Flower';
% Uncompressed data set
imageFolder = fullfile(downloadFolder,'256_ObjectCategories');
%if ~exist(imageFolder,'dir') % download only once
% disp('Downloading Flower Dataset (218 MB)...');
% websave(filename,url);
% untar(filename,downloadFolder)
%end
% Store the output in a temporary folder
%downloadFolder = tempdir;
%filename = fullfile(downloadFolder,'flower_dataset.tgz');
imds = imageDatastore(imageFolder, 'LabelSource', 'foldernames', 'IncludeSubfolders',true);
[testDigitData,trainDigitData] = splitEachLabel(imds,0.3,'randomized');
trainDigitData.ReadFcn = @(filename)readAndPreprocessImage(filename);
testDigitData.ReadFcn = @(filename)readAndPreprocessImage(filename);
transferDigitData.ReadFcn = @(filename)readAndPreprocessImage(filename);
%net= alexnet;
net =importCaffeLayers('deploy_alexnet_places365.prototxt');
lgraph = layerGraph(net);
%
% layersTransfer = net.Layers(1:end-3);
%
% numClasses = 257;
%
%
% Layers = [layersTransfer
% fullyConnectedLayer(numClasses,'Name','fc_last')
% softmaxLayer('Name','softmax')
% classificationLayer('Name','classoutput')];
lgraph = removeLayers(lgraph, {'fc8','prob','output'});%discard output layers
numClasses = 257;%Set the fully connected layer to the same size as the number of classes in the new data sat.
newLayers = [
fullyConnectedLayer(numClasses,'Name','fc')%set the learning rate of new layers
softmaxLayer('Name','softmax')
classificationLayer('Name','classoutput')];
lgraph = addLayers(lgraph,newLayers);
lgraph = connectLayers(lgraph,'drop7','fc'); %add the new output layers to the pretrained CNN
figure('Units','normalized','Position',[0.3 0.3 0.4 0.4]);
plot(lgraph)
rate2=[10e-4 10e-5 10e-6 10e-6 10e-4 10e-4 10e-5 10e-5 10e-5];
miniBatchSize = 40;
validationFrequency = floor(numel(trainDigitData.Labels)/miniBatchSize);
options = trainingOptions('sgdm',...
'LearnRateSchedule','piecewise',...
'LearnRateDropFactor',0.1,...
'LearnRateDropPeriod',3,...
'MaxEpochs',9,...
'InitialLearnRate',rate2(1),...
'MiniBatchSize',miniBatchSize);
gpuDevice(1)
convnet = trainNetwork(trainDigitData,lgraph,options);
YPred = classify(convnet,testDigitData,'MiniBatchSize',100);
YTest = testDigitData.Labels;
test_accuracy = sum(YPred==YTest)/numel(YTest);
YPred = classify(convnet,trainDigitData,'MiniBatchSize',100);
YTest = trainDigitData.Labels;
training_accuracy = sum(YPred==YTest)/numel(YTest);