Neural Network form Parameters to Simple Image Generation

4 次查看(过去 30 天)
Hello everyone,
I am currently working on a project where I need to generate black and white images of squares based on three parameters: the center x coordinate, the center y coordinate, and the edge length of the square. This would be extremely easy by itself but I must do it through e neural network or other type of ML approach. Below is a small example of a 10-samples dataset.
Could anyone provide guidance on how to design and train a neural network for this purpose? Specifically, I am looking for insights into:
  1. Choosing the appropriate neural network architecture (e.g., convolutional neural network, feedforward neural network).
  2. Preprocessing steps required for the input data.
  3. Methods for structuring the output layer to generate the B/W image of the square.
  4. Techniques for training the neural network using a dataset of square images with known parameters.
Any advice, sample code, or relevant resources would be greatly appreciated.
Thank you in advance for your assistance.
Best regards,
Daniele

回答(1 个)

Shubham
Shubham 2024-5-20
Hi Daniele,
Generating black and white images of squares with specific parameters using a neural network in MATLAB involves several steps, including setting up the neural network architecture, preprocessing input data, structuring the output layer, and training the network. MATLAB's Deep Learning Toolbox provides the necessary functions and tools to implement and train neural networks for various tasks, including image generation.
1. Choosing the Appropriate Neural Network Architecture
For this task, a simple feedforward neural network (also known as a Multilayer Perceptron (MLP)) might suffice due to the straightforward relationship between the input parameters and the output image. However, for more complex image generation tasks, Convolutional Neural Networks (CNNs) are generally preferred due to their ability to capture spatial hierarchies in images.
2. Preprocessing Steps for the Input Data
  • Normalization: Normalize the input parameters (center x coordinate, center y coordinate, and edge length) so that they are within a similar range, typically [0, 1]. This helps the neural network learn more efficiently.
  • Input Structure: For MATLAB, your input data should be organized in a matrix where each row represents a sample, and each column represents a feature (in this case, the normalized center x, center y, and edge length).
3. Methods for Structuring the Output Layer to Generate the B/W Image
  • The output layer should have as many neurons as there are pixels in the output image. For example, for a 64x64 image, you would have 4096 output neurons.
  • Use a sigmoid activation function in the output layer to ensure the output values are between 0 and 1, indicating the intensity of each pixel (0 for black, 1 for white).
4. Techniques for Training the Neural Network
  • Loss Function: Use Mean Squared Error (MSE) as the loss function to measure the difference between the generated images and the actual images of squares.
  • Dataset: You need a dataset of input parameters and their corresponding output images. You might have to generate this dataset manually, creating images of squares with varying positions and sizes based on the input parameters.
  • Training the Network: Use the train function in MATLAB to train your network. You might need to experiment with different numbers of hidden layers and neurons, training functions, and epochs to achieve satisfactory results.
Example Code in MATLAB
This example shows how to set up and train a simple neural network for this task. Note that MATLAB might require reshaping the images into vectors before training and reshaping the vectors back into images after prediction.
% Example: Creating and training a simple neural network
inputSize = 3; % 3 input features: normalized x, y, center and edge length
outputSize = 64 * 64; % Output size: number of pixels in the image
% Define the neural network architecture
hiddenLayerSize = 100; % Example size, adjust based on experimentation
net = feedforwardnet(hiddenLayerSize);
% Prepare your dataset
% X = [samples x 3] matrix of input features
% Y = [samples x 4096] matrix of output images reshaped as vectors
% Note: You need to create this dataset based on your specific requirements
% Train the neural network
[net, tr] = train(net, X', Y');
% After training, use the net for prediction
% pred = net(newX');
% Reshape the output vector back into an image
% predImage = reshape(pred, [64, 64]);
Important points to be considered:
  • Experimentation: Adjust the number of neurons, layers, and training parameters based on the performance of your network.
  • Evaluation: Consider how to evaluate your model's performance. For this task, visual inspection might be sufficient, but you could also calculate metrics like pixel-wise accuracy or mean squared error between the generated and actual images.
  • Advanced Models: For more complex image generation tasks, consider exploring more advanced neural network architectures, such as Convolutional Neural Networks (CNNs) or Generative Adversarial Networks (GANs), although implementing these might require a deeper understanding of neural networks and more computational resources.
  1 个评论
Daniele Vanerio
Daniele Vanerio 2024-5-27
Thank you for your help, even if the structure of the answer really resembles the answer chatGPT gave me when I asked it.
Anyways, I found a way to solve the issue and I was able to predict the image pixel by pixel (I used ellipses instead of squares but the structure is the same). Predicting the shape pixel by pixel helps me a lot since the next steps will be possible thanks to this property of the algorithm since those will be real sample shapes.
Now, my problem is the computational time, if a go above a 48x48 pixel image MATLAB gets stuck and the network training does not work. Any idea on this?
clear all
close all
clc
%% Generazione Ellissi per Training & Validazione
imageSize = [32, 32]; % Adjust as needed
numEllipses = 30;
[a_values, b_values] = generateEllipses(imageSize, numEllipses);
%% Estrazioni Valori Radiali da Ellissi Generate
% Folder containing ellipse images
folderName = 'ellipse_images';
% Initialize cell array to store 1D vectors
imageVectors = cell(numEllipses, 1);
% Loop through each generated ellipse image
for i = 1:numEllipses
% Read the image
filename = fullfile(folderName, ['ellipse_', num2str(i), '.png']);
img = imread(filename);
% Convert the image to binary (0 and 1)
binaryImage = imbinarize(img);
% Flatten the binary image into a 1D vector
imageVector = binaryImage(:);
% Store the 1D vector in the cell array
imageVectors{i} = imageVector;
end
% % Display the 1D vectors (optional)
% for i = 1:numEllipses
% disp(['1D Vector for ellipse ', num2str(i), ':']);
% disp(imageVectors{i}');
% end
% Save the 1D vectors and corresponding a and b values to a .mat file
save(fullfile(folderName, 'ellipse_1D_vectors.mat'), 'imageVectors', 'a_values', 'b_values');
disp('1D vectors of ellipse images saved in file: ellipse_1D_vectors.mat');
%% Plot check
% % Display the plots for each ellipse with radial distances
% imageFiles = dir(fullfile(imageFolder, '*.png'));
% numImages = numel(imageFiles);
% for i = 1:numEllipses
% imageFilename = fullfile(imageFolder, imageFiles(i).name);
% image = imread(imageFilename);
% radialDistances = all_radial_distances{i};
%
% % Calculate the center coordinates of the image
% [rows, cols, ~] = size(image);
% center_x = cols / 2;
% center_y = rows / 2;
%
% % Plot ellipse and radial distances
% figure;
% imshow(image);
% hold on;
% plot(center_x, center_y, 'ro'); % Plot center of the ellipse
% for j = 1:numel(radialDistances)
% theta = (j - 1) * (2 * pi / numRadii);
% radialDistance = radialDistances(j);
% x_point = center_x + radialDistance * cos(theta);
% y_point = center_y + radialDistance * sin(theta);
% plot(x_point, y_point, 'r.', 'MarkerSize', 5); % Plot radial distance points
% end
% hold off;
% title(['Ellipse ', num2str(i)]);
% end
%% Generation, Training and Testing of ANN
% Assuming input_data and target_data are your input and output data matrices
input_data = [a_values; b_values];
target_data = imageVectors;
% Convert cell array to a matrix where each column is a 1D vector of an image
target_data_matrix = cell2mat(target_data');
% Scale input data to the range [-1, 1]
[input_data_scaled, input_settings] = mapminmax(input_data, -1, 1);
% Scale target data to the range [-1, 1]
[target_data_scaled, target_settings] = mapminmax(target_data_matrix, -1, 1);
% Define the number and sizes of the hidden layers
n_hidden_layer = 1;
hidden_layer_sizes = [5];
% Create and configure the neural network
net = feedforwardnet(hidden_layer_sizes, 'trainbr');
% Set activation functions for hidden and output layers
for j = 1:n_hidden_layer
net.layers{j}.transferFcn = 'tansig'; % Hyperbolic tangent sigmoid for hidden layers
end
net.layers{end}.transferFcn = 'purelin'; % Linear activation for output layer
% Set data division
net.divideFcn = 'divideblock'; % Divide the data into training, validation, and testing
net.divideParam.trainRatio = 0.7; % 70% training data
net.divideParam.valRatio = 0.15; % 15% validation data
net.divideParam.testRatio = 0.15; % 15% testing data
% Set Mean Squared Error (MSE) as the performance function
net.performFcn = 'mse';
%% Train the Neural Network
if canUseGPU
input_data_scaled = gpuArray(input_data_scaled);
target_data_scaled = gpuArray(target_data_scaled);
[net, tr] = train(net, input_data_scaled, target_data_scaled, 'useParallel', 'yes');
else
[net, tr] = train(net, input_data_scaled, target_data_scaled);
end
% Check if 'tr' contains 'testInd'
if isfield(tr, 'testInd')
% Get the test input and target data
test_input_data_scaled = input_data_scaled(:, tr.testInd);
test_target_data_scaled = target_data_scaled(:, tr.testInd);
% Get the predicted outputs for the test set
predicted_outputs_scaled = net(test_input_data_scaled);
% Map the scaled output back to the original scale
predicted_outputs_original = mapminmax('reverse', predicted_outputs_scaled, target_settings);
test_target_data_original = mapminmax('reverse', test_target_data_scaled, target_settings);
% Calculate mean squared error (MSE) for the test set
mse_test = mse(predicted_outputs_original - test_target_data_original);
disp(['Mean Squared Error (MSE) on test set: ', num2str(mse_test)]);
else
error('The training record does not contain testInd. Check data division settings.');
end
%% Plotting Results
% Display the plots for each test ellipse with 1D vectors
for i = 1:numel(tr.testInd)
% Create a new figure for each test ellipse
figure;
% Extract data for the ith test ellipse
vector_actual = test_target_data_original(:, i);
vector_predicted = predicted_outputs_original(:, i);
% Plot actual 1D vector
plot(vector_actual, '-k'); % Plot actual vector in black
hold on;
% Plot predicted 1D vector
plot(vector_predicted, '-r'); % Plot predicted vector in red
% Customize the plot
title(['Actual and Predicted 1D Vectors for Test Ellipse ', num2str(i)]);
legend('Actual Vector', 'Predicted Vector');
hold off;
end
% Calculate absolute percentage error for each data point
absolute_percentage_error = abs((predicted_outputs_original - test_target_data_original) ./ test_target_data_original) * 100;
% Calculate mean absolute percentage error (MAPE)
mape = mean(absolute_percentage_error, 'all');
disp(['Mean Absolute Percentage Error (MAPE): ', num2str(mape), '%']);
%% Calculate R^2
% Calculate the mean of the dependent variable (actual vector values)
mean_actual_values = mean(test_target_data_original, 1);
% Calculate the total sum of squares (SS_tot)
SS_tot = sum((test_target_data_original - mean_actual_values).^2, 1);
% Calculate the residual sum of squares (SS_res)
SS_res = sum((test_target_data_original - predicted_outputs_original).^2, 1);
% Calculate the coefficient of determination (R^2)
R_squared = 1 - (SS_res ./ SS_tot);
% Display the R-squared values for each test ellipse
disp('Coefficient of Determination (R^2) for each test ellipse:');
disp(R_squared);
% Calculate the average R-squared value
avg_R_squared = mean(R_squared);
disp(['Average Coefficient of Determination (R^2): ', num2str(avg_R_squared)]);
%% Convert vectors back to B/W images and display results
for i = 1:numel(tr.testInd)
% Extract data for the ith test image
vector_predicted = predicted_outputs_original(:, i);
% Ensure the vector has the correct dimensions
vector_predicted = reshape(vector_predicted, 1024, 1);
% Reshape the 1D vector back to a 32x32 image
imageSize = [32, 32];
predicted_image = reshape(vector_predicted, imageSize);
% Threshold the image to get binary (black and white) image
threshold = 0.5; % Example threshold value
binary_image = predicted_image > threshold;
% Display the original and predicted images
figure;
subplot(1, 2, 1);
imshow(binary_image); % Display the predicted binary image
title('Predicted Binary Image');
% Extract and reshape the actual image
actual_image = cell2mat(target_data(tr.testInd(i)));
actual_image = reshape(actual_image, imageSize);
subplot(1, 2, 2);
imshow(actual_image); % Display the actual image
title('Actual Image');
% Customize the plot
sgtitle(['Comparison of Actual and Predicted Images for Test Image ', num2str(i)]);
end

请先登录,再进行评论。

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by