[Learning notes from matlab deep learing Onramp]
1 image dataset
one can use imageDatastore function to create the dataset
imds = imageDatastore(location,Name,Value)
by using imageread function ,you can read the specific file, which is similar to imread, but more powerful
Read image from graphics file
img=readimage(imds,7)
imshow(img)
2 deep learning Onramp in matlab
Create a Datastore
Instructions are in the task pane to the left. Complete and submit each task one at a time.
This code displays the images in the current folder and imports AlexNet.
ls *.jpg
net = alexnet;
Task 1
Create datastore
imds=imageDatastore('file*.jpg')
Task 2
Extract file names
fname=imds.Files
Task 3
Read an image
img=readimage(imds,7)
imshow(img)
Task 4
Classify images
preds=classify(net,imds)
[preds, scores]=classify(net,imds)
max(scores,[],2)
returns the maximum element along dimension M
= max(A
,[],dim
)dim
. For example, if A
is a matrix, then max(A,[],2)
is a column vector containing the maximum value of each row.
3.2 Preparing Images to Use as Input:
Task 1
View image size
sz=size(img) %1096*822*3
Task 2
Load network and view input size
net=alexnet
insz=net.Layers(1).InputSize %% 227*277*3
Task 3
Resize image and display
img=imresize(img,[227, 227])
imshow(img)
The main reson is that the pretrained networks have defined their inputsize, if you want to use transfer learing, you should resize your image to match the input size.
3.3 Processing Images in a Datastore:
Preprocessing images with a datastore
To classify images with a convolutional neural network, each image needs to have the size specified by the network's input layer.
Images usually require simple preprocessing before they can be classified. You can preprocess your images individually, but it is common to perform the same preprocessing steps on the entire data set. It is more efficient to apply these steps to the entire datastore.
Preprocess by hand
Preprocess with a datastore
It can be time consuming to process each image individually. If you want to use an image datastore, you also need to save the processed images to a folder. For large data sets, saving a duplicate of your files can take up a lot of space.
You can perform basic preprocessing with the augmentedImageDatastore
function, which takes an image datastore and an image size as an input. The datastore can be passed to the classify
function.
auimds = augmentedImageDatastore(outputSize,
creates an augmented image datastore for classification problems using images from image datastore imds
)imds
, and sets the
property.OutputSize
Task 1
Create datastore
imds=imageDatastore('*.jpg')
Task 2
An augmented image datastore can perform simple preprocessing on an entire collection images. To create this datastore, use the augmentedImageDatastore
function using your network's image input size as input.
auds = augmentedImageDatastore([r c],imds)
Create augmentedImageDatastore
Create an augmented image datastore from imds
that will resize the images to 227-by-227. Name the new datastore auds
.
auds=augmentedImageDatastore([227,227],imds)
Task 3
Classify datastore
You can use the augmented image datastore as input to the classify
function. Before each image is classified, it will be preprocessed using the methods that you specified when you created the datastore.
Task
Classify the images in auds
using the classify
function using the network stored in the variable net
. Store the predictions in a variable preds
.
preds=classify(net,auds)
Complete!
Resizing is one of the most common preprocessing steps when classifying images.
Augmented image datastores can perform a variety of other preprocessing methods. You'll use the augmentedImageDatastore
function to convert grayscale images to RGB images in the next section.
3.3 Processing Images in a Datastore: (3/3) Color preprocessing with augmented image datastores
Preprocess Color Using a Datastore
Instructions are in the task pane to the left. Complete and submit each task one at a time.
This code displays the images in the current folder and imports AlexNet.
ls *.jpg
net = alexnet
This code creates an image datastore of these images.
imds = imageDatastore('file*.jpg')
Task 1
Display images in imds
montage(imds)
Task 2
Create augmentedImageDatastore
auds=augmentedImageDatastore([227 227],imds,'ColorPreprocessing','gray2rgb')
Task 3
Classify datastore
preds=classify(net,auds)
montage(auds.Files) % display the image data
Complete! Images are required to be the size specified by a network's image input layer. Once your images are the correct size, you are ready to use pretrained networks to classify your data!
There are other methods, such as image augmentation, that are useful when you train networks. Check out the documentation to see what else you can do with augmented image datastores:
Preprocess Images for Deep Learning