Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %clc;clear
- %https://www.mathworks.com/help/deeplearning/examples/transfer-learning-using-alexnet.html
- %alex net,resnet,darknet importer,google net,cnn,tensorflow and keras models
- %deepNetworkDesigner
- digitDatasetPath = fullfile('C:\Users\UTStudent\Desktop\salari\salari-project-here\skin-cancer-dataset');
- %here number of classes is determined based on the number folder for the
- %respective you have in that directory
- imds = imageDatastore(digitDatasetPath, ...
- 'IncludeSubfolders',true,'LabelSource','foldernames');
- %inputSize = net.Layers(1).InputSize; [227 227 3]
- %this is here to change the channel for the images
- imds.ReadFcn = @(loc) repmat(imresize(imread(loc),[227 227]),[1 1 1]);
- %Divide the data into training and validation data sets.
- %Use 70% of the images for training and 30% for validation.
- %splitEachLabel splits the images datastore into two new datastores.
- [imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
- %Load the pretrained AlexNet neural network. AlexNet is trained on more than
- %one million images and can classify images into 1000 object categories, such
- %as keyboard, mouse, pencil, and many animals.
- %As a result, the model has learned rich feature representations for a wide range of images.
- net = alexnet;
- %The last three layers of the pretrained network net are configured for 1000 classes.
- %These three layers must be fine-tuned for the new classification problem. Extract
- %all layers, except the last three, from the pretrained network.
- layersTransfer = net.Layers(1:end-3);
- %Transfering the layers to the new classification task by replacing the
- %last three layers with a fully connected layer, a softmax layer, and a
- %classification output layer. Specifying the options of the new fully connected layer
- %according to the new data. Set the fully connected layer to have the same size as the
- %number of classes in the new data.
- numClasses = numel(categories(imdsTrain.Labels));
- %To learn faster in the new layers than in the transferred
- %layers, increase the WeightLearnRateFactor and BiasLearnRateFactor values of the fully connected layer.
- layers = [
- layersTransfer
- fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
- softmaxLayer
- classificationLayer];
- options = trainingOptions('rmsprop', ...
- 'MiniBatchSize',20, ...
- 'MaxEpochs',6, ...
- 'InitialLearnRate',1e-4, ...
- 'Shuffle','every-epoch', ...
- 'ValidationData',imdsValidation, ...
- 'ValidationFrequency',3, ...
- 'Verbose',false, ...
- 'Plots','training-progress');
- inputSize = net.Layers(1).InputSize;
- %The network requires input images of size 227-by-227-by-3, but
- %the images in the image datastores have different sizes. Use an
- %augmented image datastore to automatically resize the training images.
- %Specify additional augmentation operations to perform on the training images: randomly flip the training images along the vertical axis, and randomly translate them up to 30 pixels horizontally and vertically. Data augmentation helps prevent the network from overfitting and memorizing the exact details of the training images.
- pixelRange = [-35 35];
- imageAugmenter = imageDataAugmenter( ...
- 'RandXReflection',true, ...
- 'RandXTranslation',pixelRange, ...
- 'RandYTranslation',pixelRange);
- augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
- 'DataAugmentation',imageAugmenter);
- augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
- %Training the network that consists of the transferred and new layers.
- %By default, trainNetwork uses a GPU if one is available (requires Parallel Computing
- %Toolbox™ and a CUDA® enabled GPU with compute capability 3.0 or higher). Otherwise, it uses a CPU.
- %You can also specify the execution environment by using the 'ExecutionEnvironment' name-value pair argument of trainingOptions.
- netTransfer = trainNetwork(augimdsTrain,layers,options);
- %Classify the validation images using the fine-tuned network.
- [YPred,scores] = classify(netTransfer,augimdsValidation);
- YValidation = imdsValidation.Labels;
- accuracy = sum(YPred == YValidation)/numel(YValidation)%0.9934
- %%save Network, this saved model now can be loaded anywhere in matlab and
- %%classfy foreign skin cancer to this model
- save skin-cancer-model-alexnet.mat netTransfer
- %% Try to classify something else
- idx = randperm(numel(imdsValidation.Files),4);
- figure
- for i = 1:4
- subplot(2,2,i)
- I = readimage(imdsValidation,idx(i));
- actualLabel = imdsValidation.Labels(idx(i));
- %I = readimage(imdsValidation,idx(i));
- predictedLabel = netTransfer.classify(I);
- imshow(I)
- label = YPred(idx(i));
- title(['Predicted: ' char(predictedLabel) ', Actual: ' char(actualLabel)])
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement