My neural network is not trained and stands at 0

my neural network is not trained and stands at 0

code:

import os
import numpy as np
from PIL import Image
from tqdm import tqdm
from keras.models import Sequential, save_model, load_model
from keras.layers import Dense, Flatten
from keras.optimizers import Adam
from keras.utils import to_categorical
import pickle

def load_dataset(data_dir, max_images=None):
images = []
labels = []

total_images = len(os.listdir(data_dir))
if max_images is None:
    max_images = total_images

for filename in tqdm(os.listdir(data_dir)[:max_images], desc="Загрузка изображений", ncols=100):
    if len(images) >= max_images:
        break

    filepath = os.path.join(data_dir, filename)
    label = filename.split(".")[0]  # Извлекаем метку из имени файла
    labels.append(label)

    image = Image.open(filepath).convert("L")  # Открываем изображение в оттенках серого
    image = np.array(image)  # Преобразуем изображение в массив NumPy
    image = image.flatten()  # Преобразуем массив изображения в одномерный вектор
    images.append(image)

# Преобразуем списки изображений и меток в массивы NumPy
x_train = np.array(images, dtype="float32") / 255.0
y_train = np.array(labels)

unique_labels = np.unique(labels)
label_mapping = {label: i for i, label in enumerate(unique_labels)}
y_train_numeric = np.array([label_mapping[label] for label in labels])
y_train_onehot = to_categorical(y_train_numeric)

return x_train, y_train_onehot, unique_labels

def main():
# Загрузка данных
data_dir = input("Введите путь к папке с изображениями: ")
max_images = 10000
images, y_train_onehot, unique_labels = load_dataset(data_dir, max_images)

# Инициализация нейронной сети
model = Sequential()
model.add(Flatten(input_shape=(130 * 50,)))
model.add(Dense(128, activation='relu'))
model.add(Dense(len(unique_labels), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.001), metrics=['accuracy'])

epochs = 3

for epoch in range(epochs):
    print(f"Epoch №{epoch}")

    history = model.fit(images, y_train_onehot, batch_size=32, epochs=1, verbose=1)

    loss = history.history['loss'][0]
    accuracy = history.history['accuracy'][0] * 100
    print(f"Loss: {loss:.3f}")
    print(f"Accuracy: {accuracy:.3f}%")

# Сохранение модели в новом формате Keras
save_model(model, "captcha_model.keras")
print("Модель сохранена как 'captcha_model.keras'")

# Сохранение меток классов
with open('unique_labels.pkl', 'wb') as f:
    pickle.dump(unique_labels, f)

# Загрузка модели
model = load_model("captcha_model.keras")

# Загрузка меток классов
with open('unique_labels.pkl', 'rb') as f:
    unique_labels = pickle.load(f)

# Вывод текста с изображения
test_image_path = input("Введите путь к тестовому изображению: ")
test_image = Image.open(test_image_path).convert("L")
test_image = test_image.resize((130, 50))
test_image = np.array(test_image)
test_image = test_image.flatten()

predicted_label_index = np.argmax(model.predict(np.array([test_image])))
predicted_label = unique_labels[predicted_label_index]

print(f"Предсказанный текст с изображения: {predicted_label}")

if name == “main”:
main()

please help me I do not know what the error is

Hi @whaile ,

welcome to the Forum. Please feel free to share the Training Output too.
Can you try to remove the for loop and include the epochs into the model.fit() call?

epochs = 3
history = model.fit(images, 
                    y_train_onehot,
                    batch_size=32,
                    epochs=epochs,
                    verbose=1)

Hope this helps in a nutshell on the first view
Dennis

I did it, but here are the results of my training still at 0:

Загрузка изображений: 100%|██████████████████████████████████| 10000/10000 [00:39<00:00, 250.20it/s]
2023-08-03 12:13:10.774723: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/3
313/313 [==============================] - 34s 99ms/step - loss: 9.2704 - accuracy: 0.0000e+00
Epoch 2/3
313/313 [==============================] - 29s 93ms/step - loss: 9.2213 - accuracy: 0.0000e+00
Epoch 3/3
313/313 [==============================] - 29s 92ms/step - loss: 9.2212 - accuracy: 0.0000e+00
Loss: 9.270
Accuracy: 0.000%
Модель сохранена как ‘captcha_model.keras’

Hi @whaile, Could you please ensure that the images and labels are properly aligned and matched. Also if possible please share the dataset in a shared drive which you are using for training. Thank You

hi @Kiran_Sai_Ramineni I use my Java generator here are my images and since there are more than 5gb of them there, I can’t upload them to disk, but I can upload them to my web server, although it will be about 3 hours

oh, he writes to me, I can’t attach pictures:
An error occurred: Sorry, you can’t embed media items in a post.

I can explain in words I have pictures 130 in length and 50 in height, and they are called 000Ve.png this means that the text in the picture is 000Ve

@Dennis please help me, I need a ready neural network by tomorrow and I do not know what to do

Hi @whaile, I have used your model architecture to train on mnist dataset and got the expected results. I think the problem lies within your data not in the model architecture. Could you please make sure that labels are correctly assigned and match the features. Thank you

Hi @Kiran_Sai_Ramineni, I’m going to put a few hundred of my pictures on the jisk now, please wait

Hi @Kiran_Sai_Ramineni captchas.rar — Яндекс Диск have a look please, I hope for a quick answer

G’morning @whaile ,
thank’s for the additional info’s. Like Kiran mentioned, please dcheck the dimensions and the label encoding. Can you share the following print’s too?

print(images.shape)
print(y_train_onehot.shape)
print(len(unique_labels))
print(y_train_onehot[:2])

For Image classification, please also have a further look at CNN Networks.
I guess 1 single Dense Layer is to small (depeding on your image variations).

Looking forward

G’morning @Dennis , I executed your code at the end of load_dataset load_dataset that’s what I got:

Введите путь к папке с изображениями: C:\Users\whail\IdeaProjects\HDWcaptchaGenerator\vk-captcha\all
Загрузка изображений: 100%|██████████████████████████████████| 10000/10000 [00:35<00:00, 282.85it/s]
Traceback (most recent call last):
File “C:\Users\whail\OneDrive\Рабочий стол\Новая папка\hdwcaptcha\learn.py”, line 101, in
main()
File “C:\Users\whail\OneDrive\Рабочий стол\Новая папка\hdwcaptcha\learn.py”, line 52, in main
images, y_train_onehot, unique_labels = load_dataset(data_dir, max_images)
File “C:\Users\whail\OneDrive\Рабочий стол\Новая папка\hdwcaptcha\learn.py”, line 41, in load_dataset
print(images.shape)
AttributeError: ‘list’ object has no attribute ‘shape’

Ah ok, it’s a list. If you pass your training images to the network, the Flatten layer expects a different shape. Please try the following 2 lines, it should produce an error inside your network (please confirm).

x = tf.keras.layers.Flatten(input_shape=(130 * 50,))(images)
x.shape

About the shape, please update my code above and re-run:

images_numpy = np.array(images)
print(images_numpy.shape)

What happens if you pass the Numpy Array (images) into the network as input? Give it a try.

I don’t understand much what you want from me can you give the full code of the function?

Sorry for the snippets :wink: Please just use the first Layer of your network, and pass the images.

data_dir = input("Введите путь к папке с изображениями: ")
max_images = 10000
images, y_train_onehot, unique_labels = load_dataset(data_dir, max_images)

x = tf.keras.layers.Flatten(input_shape=(130 * 50,))(images)
x.shape

Does the code above produce an error too?

Please provide a little output of the following print"s:

images_numpy = np.array(images)
print(images_numpy.shape)
print(y_train_onehot.shape)
print(len(unique_labels))
print(y_train_onehot[:2])

Yes, I did it! here’s a look:

Введите путь к папке с изображениями: C:\Users\whail\IdeaProjects\HDWcaptchaGenerator\vk-captcha\all
Загрузка изображений: 100%|██████████████████████████████████| 10000/10000 [00:35<00:00, 282.35it/s]
2023-08-04 10:23:46.068379: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
(10000, 6500)
(10000, 10000)
10000
[[1. 0. 0. … 0. 0. 0.]
[0. 1. 0. … 0. 0. 0.]]
2023-08-04 10:23:51.209461: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 3328000 exceeds 10% of free system memory.
2023-08-04 10:23:51.642759: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 3328000 exceeds 10% of free system memory.
2023-08-04 10:23:51.679040: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 3328000 exceeds 10% of free system memory.
2023-08-04 10:23:53.539873: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 5120000 exceeds 10% of free system memory.
2023-08-04 10:23:53.591731: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 5120000 exceeds 10% of free system memory.

@whaile is each image unique? You’ve 10k images and 10k unique class labels?

Each image is unique because it has a unique name and is stored in Windows, and there cannot be 2 images with the same name in it, as for the labels, I do not know.-.

If you were talking about the labels that are created by my generator, then yes they are all unique, because the label is created first and then the saving goes

And I want to say that I can’t create new messages within an hour :frowning:
maybe it can be somehow removed or moved to another place?
Otherwise I will have to edit this message

@Dennis new post:

It’s sad, do I understand correctly that there’s nothing to do with it? but I can rewrite the generator so that it creates images in the folder, but then I will have errors with memory overload and I do not know how to solve them

Yes, and I think what you said that there may be a lot of images with one aptly is not correct, because if you take, say, 12NV and 12B, then I think it won’t make it difficult to recognize 12 since there were a lot

A good approach for the unique label names would be to store them in a folder and
use the folder name as label. This would allow you to store multiple images under
one class label/name.

As overall approach, you need a CNN to solve this task (1 dense layer is to less).
For captures (e.g: '“… that the text in the picture is 000Ve…”) there might be a lot of unique images. How about to detect each sign/letter/char individual?
Please have a deep dive into Optical character recognition (OCR).

@Dennis
It’s sad, do I understand correctly that there’s nothing to do with it? but I can rewrite the generator so that it creates images in the folder, but then I will have errors with memory overload and I do not know how to solve them

Yes, and I think what you said that there may be a lot of images with one aptly is not correct, because if you take, say, 12NV and 12B, then I think it won’t make it difficult to recognize 12 since there were a lot