create mnist base from my pictures in matlab

To create a MNIST dataset from your own images in Matlab, you will need to follow several steps:

  1. Collect and preprocess your images
    • Collect your images and ensure that they are all the same size and type (e.g., grayscale)
    • Convert your images into a matrix format that can be used by Matlab, such as .mat or .csv
  2. Create labels for your images
    • Assign a label to each of your images to indicate what the image represents
    • Create a vector of labels in the same order as your image matrix
  3. Save your images and labels into a dataset format
    • Save your image matrix and label vector into a format that can be loaded easily into Matlab
    • Examples of common formats include .mat or .csv
  4. Train your machine learning model
    • Use your newly created MNIST dataset to train your machine learning model using a technique such as convolutional neural networks (CNNs)
    • Split your dataset into training and testing sets to ensure that your model generalizes well to new data

Here is an example code for converting a folder of images with labels into a dataset of MNIST format in Matlab:

main.m
% Set the path and get all the image file names
path = 'path/to/your/images/folder/';
image_files = dir(fullfile(path,'*.bmp'));

% Initialize image matrix and label vector
X = zeros(numel(image_files),784);
y = zeros(numel(image_files),1);

% Loop through images and preprocess
for i = 1:numel(image_files)
    % Read image and convert to grayscale and resize to 28x28
    img = imread(fullfile(path,image_files(i).name));
    img = rgb2gray(img);
    img = imresize(img,[28 28]);
    
    % Convert image to a row vector and store in X
    img_vec = reshape(img,1,784);
    X(i,:) = img_vec;
    
    % Get label from filename and store in y
    label_str = strsplit(image_files(i).name,'_');
    label_str = label_str{end};
    label_str = label_str(1:end-4);
    label = str2double(label_str);
    y(i,1) = label;
end

% Save data as MNIST format .mat file
save('my_mnist_dataset.mat','X','y');
899 chars
30 lines

In this example code, we read in a folder of .bmp image files, preprocess them by converting to grayscale and resizing to 28x28 (the same size as the MNIST dataset), then store them into an image matrix X and a label vector y. We then save them into a .mat file in the MNIST dataset format.

gistlibby LogSnag