clc,clear all,close all; dataDir = fullfile('data'); imds = imageDatastore(fullfile(dataDir,'volume'),'FileExtensions',{'.png'}); classNames = ["A", "B"]; pixelLabelID = [0, 255]; pxds = pixelLabelDatastore(fullfile(dataDir,'segmentation'),classNames,pixelLabelID,'FileExtensions',{'.png'}); % 定义网络结构 numClasses = 2; imageSize = [512, 512, 1]; lgraph = unetLayers(imageSize, numClasses, 'EncoderDepth', 3); % 定义训练选项 options = trainingOptions('adam', ... 'MaxEpochs', 100, ... 'MiniBatchSize', 16, ... 'Shuffle', 'every-epoch', ... 'ValidationData', {imageDatastore(fullfile(dataDir,'valvol'),'FileExtensions',{'.png'}), pixelLabelDatastore(fullfile(dataDir,'valseg'),classNames,pixelLabelID,'FileExtensions',{'.png'})}, ... 'ValidationFrequency', 10, ... 'Verbose', true, ... 'Plots', 'training-progress'); % 训练模型 ds = combine(imds, pxds); net = trainNetwork(ds, lgraph, options);无法串联元胞数组和分类数组,除非元胞数组仅包含字符向量。原因
时间: 2023-07-16 22:12:15 浏览: 188
在定义像素标签数据存储器(pxds)时,您传递了一个字符串数组作为类名称参数(classNames),但是在训练模型之前,您需要将字符串数组转换为分类数组。可以使用以下代码将字符串数组转换为分类数组:
```
classNames = categorical(classNames);
```
修改后的代码如下:
```
clc,clear all,close all;
dataDir = fullfile('data');
imds = imageDatastore(fullfile(dataDir,'volume'),'FileExtensions',{'.png'});
classNames = ["A", "B"];
classNames = categorical(classNames); % 将字符串数组转换为分类数组
pixelLabelID = [0, 255];
pxds = pixelLabelDatastore(fullfile(dataDir,'segmentation'),classNames,pixelLabelID,'FileExtensions',{'.png'});
% 定义网络结构
numClasses = 2;
imageSize = [512, 512, 1];
lgraph = unetLayers(imageSize, numClasses, 'EncoderDepth', 3);
% 定义训练选项
options = trainingOptions('adam', ...
'MaxEpochs', 100, ...
'MiniBatchSize', 16, ...
'Shuffle', 'every-epoch', ...
'ValidationData', {imageDatastore(fullfile(dataDir,'valvol'),'FileExtensions',{'.png'}), pixelLabelDatastore(fullfile(dataDir,'valseg'),classNames,pixelLabelID,'FileExtensions',{'.png'})}, ...
'ValidationFrequency', 10, ...
'Verbose', true, ...
'Plots', 'training-progress');
% 训练模型
ds = combine(imds, pxds);
net = trainNetwork(ds, lgraph, options);
```
阅读全文