-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageDataPropertiesModel.m
92 lines (78 loc) · 3.35 KB
/
ImageDataPropertiesModel.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
classdef ImageDataPropertiesModel
% ImageDataProperties Access properties and random images of image datastore
%
% ImageDataPropertiesModel Properties:
%
% ImageDatastore - The input image datastore object
% TotalObservations - Total number of observations in the datastore
% Classes - Unique classes in image datastore
% NumClasses - Number of unique classes in image datastore
% NumObservationsByClass - Number observations per unique class
% NumObsToShow - Number of observations to show when accessing
% image datastore
%
% ImageDataPropertiesModel Methods:
%
% getRandomData - Extract random images from datastore
% getRandomDataByLabel - Extract random images from datastore by class
% label
%
% Copyright 2020 The MathWorks, Inc.
properties(SetAccess = private)
% Properties of the image data
ImageDatastore
Classes
NumObservationsByClass
TotalObservations
end
properties(Dependent)
NumClasses
end
properties
% Default number of observations to show when accessing image
% datastore
NumObsToShow = 16;
end
methods
% ImageDataPropertiesModel is constructed with an image datastore
% argument
function imageData = ImageDataPropertiesModel(imds)
% Extract the properties of the image data
imageData.ImageDatastore = imds;
imageData.TotalObservations = length(imds.Labels);
imageData.Classes = unique(imds.Labels);
imageData.NumObservationsByClass = arrayfun(...
@(x)sum(x == imds.Labels), imageData.Classes);
end
% Select numImages random images from the image data
function [data, labels] = getRandomData(imageData, numImages)
idx = randperm(imageData.TotalObservations, imageData.NumObsToShow);
subImds = imageData.ImageDatastore.subset(idx);
cellData = subImds.readall();
if nargin > 1
data = cellfun(@(x)imresize(x, numImages), cellData, ...
"UniformOutput", false);
else
data = cellData;
end
labels = imageData.ImageDatastore.Labels(idx);
end
% Select numImages random images from the image data with class
% chosenClass
function [data, labels] = getRandomDataByLabel(imageData, chosenClass, numImages)
isIncluded = ismember(imageData.ImageDatastore.Labels, categorical(chosenClass));
subImds = imageData.ImageDatastore.subset(isIncluded);
numObsToShow = min(imageData.NumObsToShow, length(subImds.Labels));
idx = randperm(length(subImds.Labels), numObsToShow);
subImds = subImds.subset(idx);
cellData = subImds.readall();
if nargin > 2
data = cellfun(@(x)imresize(x, numImages), cellData, ...
"UniformOutput", false);
else
data = cellData;
end
labels = subImds.Labels;
end
end
end