我正在创建一个图像分类器。训练神经网络时出现问题。训练神经网络的一个问题

问题描述 投票:0回答:0

这是我从视频教程中编写的一段代码:

import tensorflow as tf
tf.__version__

from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.inception_v3 import InceptionV3

from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing import image 
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob

IMAGE_SIZE = [244, 244]
train_path = '/content/drive/MyDrive/Programs/train'
valid_path = '/content/drive/MyDrive/Programs/valid'

inception = InceptionV3(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

for layer in inception.layers:
    layer.trainable = False

folders = glob('/content/drive/MyDrive/Programs/train/*')

folders

x = Flatten()(inception.output) 

from keras.api._v2.keras import activations
prediction = Dense(len(folders), activation='softmax')(x)
model = Model(inputs = inception.input, outputs=prediction)

model.summary()

#расскажи модели какая цена и оптимальный метод использования
model.compile(
    loss = 'catigorical_crossentropy',
    optimizer = 'adam',
    metrics = ['accuracy']
)


from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)


training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/Programs/train',
                                                 target_size = (224, 244),
                                                 batch_size = 16,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/Programs/valid',
                                                 target_size = (224, 244),
                                                 batch_size = 16,
                                                 class_mode = 'categorical')


r = model.fit(
  training_set,
  validation_data=test_set,
  epochs=10,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set)
                       )

#The error that occurs:

Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-c86340ce6b87> in <cell line: 1>()
----> 1 r = model.fit(
      2   training_set,
      3   validation_data=test_set,
      4   epochs=10,
      5   steps_per_epoch=len(training_set),

1 frames
/usr/local/lib/python3.9/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1284, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1268, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1249, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1051, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1109, in compute_loss
        return self.compiled_loss(
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/compile_utils.py", line 240, in __call__
        self.build(y_pred)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/compile_utils.py", line 182, in build
        self._losses = tf.nest.map_structure(
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/compile_utils.py", line 353, in _get_loss_object
        loss = losses_mod.get(loss)
    File "/usr/local/lib/python3.9/dist-packages/keras/losses.py", line 2653, in get
        return deserialize(identifier, use_legacy_format=use_legacy_format)
    File "/usr/local/lib/python3.9/dist-packages/keras/losses.py", line 2600, in deserialize
        return legacy_serialization.deserialize_keras_object(
    File "/usr/local/lib/python3.9/dist-packages/keras/saving/legacy/serialization.py", line 543, in deserialize_keras_object
        raise ValueError(

    ValueError: Unknown loss function: 'catigorical_crossentropy'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.


起初我有一个错误,因为我使用了 model.fit_generator 而不是 model.fit。我删除了 model.fit_generator 并只留下了 model.fit,在这些操作之后错误没有得到修复。我不知道该怎么办。 如果有人能帮我解决问题,我将非常高兴!非常感谢您。

deep-learning neural-network google-colaboratory image-classification
© www.soinside.com 2019 - 2024. All rights reserved.