function net = run_training_public(dims, nbatch, lr, nepoch) % Load Data ds = imageDatastore('./data/set3', 'IncludeSubfolders',true,... 'ReadFcn',@(x)reader_public(x,dims),... 'LabelSource','foldernames',... 'FileExtensions','.dat'); % load neural network structure network = cnn1; % Setup options for training and execute training options = trainingOptions('adam','MaxEpochs',nepoch,'MiniBatchSize',... nbatch,'Shuffle','every-epoch',... 'InitialLearnRate',lr,... 'ExecutionEnvironment','gpu','Verbose',true); net = trainNetwork(ds,network,options); end function data = reader_public(fileName, dims) f=fopen(fileName,'r'); data = fread(f,[dims(2) dims(1)],'*int16').'; fclose(f); end
I used my own binary file reader based on looking at the built in png reader functions and my code above. It is a huge speedup, and now I am able to eliminate the ReadFcn but still have my custom reader. The only issue I didn't solve was how to pass the dims variable into the reader instead of hard coding it.
function net = run_training_public(nbatch, lr, nepoch) % Add custom image type to imread registry create_custom_image_format() % Load Data ds = imageDatastore('./data/set3','IncludeSubfolders',true,... 'LabelSource','foldernames',... 'FileExtensions','.dat'); % load neural network structure network = cnn1; % Setup options for training and execute training options = trainingOptions('adam','MaxEpochs',nepoch,'MiniBatchSize',... nbatch,'Shuffle','every-epoch',... 'InitialLearnRate',lr,... 'ExecutionEnvironment','gpu','Verbose',true); net = trainNetwork(ds,network,options); end function create_custom_image_format() fmts = imformats; % don't add if already in registry if ~any(contains([fmts.ext],'dat')) out.ext = 'dat'; out.isa = @isdat; out.info = []; out.read = @custom_image_reader; out.write = []; out.alpha = 0; out.description = 'Custom Data Format'; imformats('add',out); end end function tf = isdat(filename) % Returns true if file is type .dat [~,~,extn] = fileparts(filename); tf = strcmp(extn,'.dat'); end function [X, map] = custom_image_reader(filename) dims = [$m $n]; % <-HARD CODE DIMENSIONS OF DATA HERE f=fopen(filename,'r'); X = reshape(fread(f,'*int16'),dims(2), dims(1)).'; fclose(f); map = []; end
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.