-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentation_example_script.m
More file actions
107 lines (84 loc) · 3.78 KB
/
Segmentation_example_script.m
File metadata and controls
107 lines (84 loc) · 3.78 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
% Implementation of Semantic Segmentation
%
% written by Oguzhan Ulucan, Izmir University of Economics, January 2020
% e-mail: oguzhan.ulucan.iz@gmail.com
%
% This work was published in
% "A Large-Scale Dataset for Fish Segmentation and Classification"
% Oguzhan Ulucan, Diclehan Karakaya and Mehmet Turkan
% ASYU 2020.
% If you use this dataset in your work, please consider to cite:
% @inproceedings{ulucan2020large,
% title={A Large-Scale Dataset for Fish Segmentation and Classification},
% author={Ulucan, Oguzhan and Karakaya, Diclehan and Turkan, Mehmet},
% booktitle={2020 Innovations in Intelligent Systems and Applications Conference (ASYU)},
% pages={1--5},
% year={2020},
% organization={IEEE}
% }
% O.Ulucan , D.Karakaya and M.Turkan.(2020)
% A large-scale dataset for fish segmentation and classification.
% In Conf. Innovations Intell. Syst. Appli. (ASYU)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% By following the steps below, segmentation of any class in the dataset is possible via semantic segmentation.
% The code was written for MATLAB enviroment.
% After downloading the "Fish_Dataset", tasks such as, segmentation, feature extraction or classification can be carried out.
% In this text, the explanation of the segmentation process is provided.
clear all, clc, close all
imageDir = ' '; % The directory of the images which would like be used to train the segmention network should be in here.
labelDir = ' '; % The directory of the pair-waise ground-truths (GT) of images should be in here.
imds = imageDatastore(imageDir);
%%
classNames = [" Fish type ", "background"]; % Fish type should be specified in here to label the segmented images.
labelIDs = [1 0];
pxds = pixelLabelDatastore(labelDir, classNames, labelIDs);
%% Create a semantic segmentation network
% This part can be modified according to your own work.
numFilters = 64;
filterSize = 3;
numClasses = 2;
layers = [
imageInputLayer([445 590 3])
convolution2dLayer(filterSize, numFilters, 'Padding',[1 1])
reluLayer()
maxPooling2dLayer(2,'Stride',1)
convolution2dLayer(filterSize, numFilters, 'Padding',[1 1])
reluLayer()
convolution2dLayer(filterSize, numFilters, 'Padding',[1 1])
reluLayer()
convolution2dLayer(filterSize, numFilters, 'Padding',[1 1])
reluLayer()
transposedConv2dLayer(4, numFilters, 'Stride', 1, 'Cropping', 1);
convolution2dLayer(1, numClasses);
softmaxLayer()
pixelClassificationLayer()
]
%% Train the Network
% This part can be modified according to your own work.
opts = trainingOptions('sgdm', ...
'InitialLearnRate',1e-3, ...
'MaxEpochs',10, ...
'MiniBatchSize',8);
%%
trainingData = pixelLabelImageDatastore(imds,pxds);
network = trainNetwork(trainingData,layers,opts);
%% Improvement
tbl = countEachLabel(trainingData);
totalNumberOfPixels = sum(tbl.PixelCount);
frequency = tbl.PixelCount / totalNumberOfPixels;
classWeights = 1./frequency;
layers(end)= pixelClassificationLayer('ClassNames',tbl.Name,'ClassWeights',classWeights);
improved_network = trainNetwork(trainingData,layers,opts);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% After training process is completed, the segmentation of the fish can be carried out.
Fish_Dir = ' '; % The directory of the images which would like to be segmented should be in here.
Segmented_Fish_Dir = ' '; % The segmented images will be saved in this directory.
im_Set = imageSet(Fish_Dir);
for k = 1 : im_Set.Count %number of images
testImage = imread(im_Set.ImageLocation{k});
C = semanticseg(testImage,improved_network);
B = labeloverlay(testImage,C);
baseFileName = sprintf('%04d.png', k); %label every image in order
fullFileName = fullfile(Segmented_Fish_Dir, baseFileName);
imwrite(B, fullFileName);
end