前提・実現したいこと
画像判別の学習とテストをkerasで実行しようとしていたら以下のようなエラーメッセージが発生しました。
発生している問題・エラーメッセージ
ValueError Traceback (most recent call last)
<ipython-input-6-2d38de2663a3> in <module>()
3 epochs=15,
4 validation_data=validation_generator,
----> 5 validation_steps=50)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function * return step_function(self, iterator) /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica return fn(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step ** outputs = model.train_step(data) /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:789 train_step y, y_pred, sample_weight, regularization_losses=self.losses) /usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:201 __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) /usr/local/lib/python3.7/dist-packages/keras/losses.py:141 __call__ losses = call_fn(y_true, y_pred) /usr/local/lib/python3.7/dist-packages/keras/losses.py:245 call ** return ag_fn(y_true, y_pred, **self._fn_kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/keras/losses.py:1809 binary_crossentropy backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits), /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/keras/backend.py:5000 binary_crossentropy return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/nn_impl.py:246 sigmoid_cross_entropy_with_logits_v2 logits=logits, labels=labels, name=name) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/nn_impl.py:133 sigmoid_cross_entropy_with_logits (logits.get_shape(), labels.get_shape())) ValueError: logits and labels must have the same shape ((None, 25, 25, 1) vs (None, 1))
該当のソースコード
from keras import layers
from keras import models
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Dense(512,activation='relu',input_shape=(25,25,1)))
model.add(layers.Dense(1,activation='sigmoid'))
from tensorflow import keras
from tensorflow.keras import optimizers
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(learning_rate=1e-4),
metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(25,25),
batch_size=20,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(25,25),
batch_size=20,
class_mode='binary')
history = model.fit(train_generator,
steps_per_epoch=50,
epochs=15,
validation_data=validation_generator,
validation_steps=50)
試したこと
問題を解決すべく検索調べましたがわからなかったため聞かせていただきました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/11/01 13:57
2021/11/01 14:25