Eu finalizei o curso, e adaptei o código final com a minha necessidade. Eu estou utilizando imagens .png, o dataset sou que crio e não utilizo um que já ta no "jeito". Quando eu executo o script pra treinar a AI (model.fit()) está estourando um erro:
'validation_split' is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'str'>]
**eu simplifiquei o número de [<class 'str'>] que aparecem.
Segue o código:
import os
from typing import Mapping
import tensorflow as tf
from pathlib import Path
from tensorflow import keras
from tensorflow.keras.preprocessing.image import img_to_array, load_img
def dataset(data_dir, training_percentage=0.75):
train_image_list = []
labels_train = []
test_image_list = []
labels_test = []
images = sorted(list(map(str, list(data_dir.glob("*.png")))))
qtd_train_images = int(len(images) * training_percentage)
for i, image in enumerate(images):
image_PIL = load_img(image)
image_array = img_to_array(image_PIL)
image_array = image_array / float(255)
label_image = image.split(os.path.sep)[-1].split(".png")[0]
if i <= qtd_train_images:
train_image_list.append(image_array)
labels_train.append(label_image)
else:
test_image_list.append(image_array)
labels_test.append(label_image)
labels = labels_test + labels_train
characters = set(char for label in labels for char in label)
return train_image_list, labels_train, test_image_list, labels_test, characters
def create_model():
data_dir = Path("C:/repos/poc/images/")
train_images, labels_train, test_images, labels_test, characters = dataset(data_dir, training_percentage=0.75)
modelo = keras.Sequential([
keras.layers.Flatten(input_shape=(200, 50)),
keras.layers.Dense(256, activation=tf.nn.relu),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
modelo.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
try:
modelo.fit(train_images, labels_train, epochs=5, validation_split=0.2)
modelo.save('modelo.h5')
except Exception as e:
print(">>>>>>>>>>>>>>>>")
print(f"Erro: {e.args[0]}")
return train_images
if '__name__' == '__main__':
model = create_model()
Dei uma googada mas não entendi muito bem... alguém consegue me dar uma luz?