Q
Q
qovalenko2017-03-25 08:27:35
PHP
qovalenko, 2017-03-25 08:27:35

Where is the error in training a neural network to distinguish images?

Hello, I need the network to distinguish whether there is an object in the image or not.
More precisely, it is necessary to determine whether there is an ulcer in the photo of the mucous membrane or not
. The database was collected 50 photos - yes, 50 photos - no
resize / 0 - no; resize/1-yes
All images are black and white and 300x500 in size
Example of images with
8e937df28e8042cba1fdc442a49fd366.jpg
and without an ulcer
6a480ddee62f40bea372243208743a77.jpg
Here is the training code:

$j = 0;
$my_example = array();
for ( $i = 0; $i < 2; $i++ )
{
    $d = dir("resize/$i");
    while($entry = $d->read())
    {
        if ( preg_match("/jpg/", $entry) )
        {
            $im = imagecreatefromjpeg("black/$i/$entry");//Подгружаем картинку

            $cur_array = array(); //Создаем массив в котором в одну строку будут собраны номера цветов каждого пикселя
            $cnt = 0;
            for($y=0; $y<300; $y++)
            {
                for($x=0; $x < 500; $x++)
                {
                    $rgb = imagecolorat($im, $x, $y) / 16777215;//Приводим к виду "меньше ноля"
                    $cur_array[$cnt] = $rgb;
                    $cnt++;
                }
            }

            imagedestroy($im);
            $my_example[$j] = array($cur_array, array($i));//Собираем массив, с которого будем обучать
            $j++;
        }

    }
}

$num_input = 150000;//нейронов на вход 500x300
$num_output = 1;//нейронов на выход
$num_layers = 3;
$num_neurons_hidden = 100;
$desired_error = 0.000001;
$max_epochs = 5000000;
$epochs_between_reports = 10;

//И вот собственно само обучение
$ocr_ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);

if ($ocr_ann) {
    echo 'Training OCR... ';
    fann_set_activation_function_hidden($ocr_ann, FANN_SIGMOID_SYMMETRIC);
    fann_set_activation_function_output($ocr_ann, FANN_SIGMOID_SYMMETRIC);

    for ($i = 1; $i <= 150000; $i++)
    {
        $input[] = $i;
    }

    $desired_output = array( 1 );

    var_dump( fann_train( $ocr_ann, $input, $desired_output ) );
    fann_save($ocr_ann, dirname(__FILE__) . "/ocr_float3.net");

}

and here for verification:
echo "Hello!";
$j = 0;
$my_example = array();

$im = imagecreatefromjpeg("black/0/1.jpg");

$cur_array = array();
$cnt = 0;
for($y=0; $y<300; $y++)
{
    for($x=0; $x < 500; $x++)
    {
        $rgb = imagecolorat($im, $x, $y) / 16777215;
        $cur_array[$cnt] = $rgb;
        $cnt++;
    }
}

imagedestroy($im);
echo "Hello!";
$train_file = (dirname(__FILE__) . '/ocr_float3.net');
$ocr_ann = fann_create_from_file($train_file);

$calc_out = fann_run($ocr_ann, $cur_array);

echo "Hello!";
var_dump($calc_out);

According to my logic, if there is an object, then the result tends to one, and if not, then to zero
. But the result is not at all the same as expected.
Can you please tell me what is wrong in the code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2017-03-25
@qovalenko

ab-log.ru/smart-house/video_camera_security/face-d...
1. First you need to learn how to determine the zone (crop-zone) where an ulcer is possible and bring all images to a single unified form (it is necessary to reduce possible NS errors by next paragraph).
2. Based on the values ​​of the weights of the National Assembly, calculated on the basis of the displayed pixels for each photo, you need to CLASSIFY the sets into 2 classes (as I understand that you need it so much): "with an ulcer" and "without an ulcer".
The code you presented is a special case for prepared images (i.e. starting from step 2). Also, it is necessary to increase the number of hidden layers to achieve more detailed and accurate clustering.
PS: Yes, and one more thing... If you want to have a ready-made working code written here, you are mistaken: this is for freelance.

A
Arseny Kravchenko, 2017-03-28
@Arseny_Info

First, a multilayer perceptron is not a suitable architecture, look towards convolutional networks.
Secondly, 100 photos may not be enough for learning from scratch, especially without augmentation.
Third, the sigmoid is a poor choice of activation function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question