Here is a piece of code that I wrote from a video tutorial:
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.
At first I had an error in that I use the model.fit_generator and not the model.fit. I removed the model.fit_generator and left only the model.fit, after these actions the error was not fixed. I don't know what to do with it. I will be very glad if someone can help me figure out the problem! Thank you very much in advance.
I guess it is because of the spelling of this loss function you are getting this issue. It must be "
categorical_crossentropy
"instead of catigorical_crossentropy.
This must fix your issue.
model. Compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy']
)