Я попытался создать классификатор изображений для файла RockPaperScissors.zip, и у меня возникли проблемы с обучением модели. Вот мой код:
import tensorflow as tf
from tensorflow import keras
from keras import layers
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
!wget --no-check-certificate \
https://dicodingacademy.blob.core.windows.net/picodiploma/ml_pemula_academy/rockpaperscissors.zip \
-O /tmp/rockpaperscissors.zip
import zipfile,os
local_zip = '/tmp/rockpaperscissors.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()
base_dir = '/tmp/rockpaperscissors'
train_dir = os.path.join(base_dir, 'rps-cv-images')
validation_dir = os.path.join(base_dir, 'rps-cv-images')
os.listdir('/tmp/rockpaperscissors/rps-cv-images')
train_rock_dir = os.path.join(train_dir, 'rock')
train_paper_dir = os.path.join(train_dir, 'paper')
train_scissors_dir = os.path.join(train_dir, 'scissors')
validation_rock_dir = os.path.join(validation_dir, 'rock')
validation_paper_dir = os.path.join(validation_dir, 'paper')
validation_scissors_dir = os.path.join(validation_dir, 'scissors')
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=90,
horizontal_flip=True,
shear_range = 0.2,
zoom_range = 0.2,
validation_split = 0.2,
fill_mode = 'nearest')
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(100, 100),
batch_size=9,
color_mode = 'rgb',
shuffle=True,
subset = 'training',
class_mode = 'categorical')
validation_generator = train_datagen.flow_from_directory(
validation_dir,
target_size=(100, 100),
batch_size=9,
color_mode = 'rgb',
shuffle=False,
subset = 'validation',
class_mode = 'categorical')
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(100, 100, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(256, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')
])
model.compile(loss ='categorical_crossentropy',
optimizer= tf.optimizers.Adam(),
metrics= tf.metrics.Accuracy()
)
model.fit(
train_generator,
steps_per_epoch=25,
epochs=15,
validation_data=validation_generator,
validation_steps=5,
verbose=2)
Когда я пытался обучить свою модель, он говорит InvalidArgumentError: Несовместимый размер матрицы: In [0]: [9,3], In [1]: [512,1] [[узел gradient_tape / sequence_2 / density_5 / MatMul (определено в: 5)]] [Операция: __ inference_train_function_8077] Стек вызовов функций: train_function. Пожалуйста, помогите