Issue
Whenever I run this script I get the same error. I thought that it might be I needed to add labels to the fit function but the format my data is in is 'keras.utils.Sequence'. I was thinking there might be something wrong with my model, as this is my first one. Here is my code:
import tensorflow as tf
from keras.metrics import sparse_categorical_accuracy
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D, PReLU
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
inputShape = (178, 218, 3)
model = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)),
MaxPool2D(pool_size=(2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)),
MaxPool2D(pool_size=(2, 2), strides=2),
Flatten(),
Dense(units=2, activation='softmax')
])
train_path = r'D:\Coding\pythonProject\kerasandtensorflowtutorial\Faces\train'
valid_path = r'D:\Coding\pythonProject\kerasandtensorflowtutorial\Faces\valid'
test_path = r'D:\Coding\pythonProject\kerasandtensorflowtutorial\Faces\test'
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_path, target_size=(178, 218), classes=['faces', 'others'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=valid_path, target_size=(178, 218), classes=['faces', 'others'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path, target_size=(178, 218), classes=['faces', 'others'], batch_size=10, shuffle=False)
print(valid_batches.image_shape)
model.compile(optimizer=Adam(learning_rate=0.0001), loss=sparse_categorical_accuracy, metrics=['accuracy'])
model.fit(x=train_batches, y=train_path, validation_data=valid_batches, epochs=100, verbose=2, batch_size=20)
model.save('model/face.h5')
And here is the full error message I get:
Traceback (most recent call last):
File "D:/Coding/pythonProject/kerasandtensorflowtutorial/face detection.py", line 37, in <module>
model.fit(x=train_batches, validation_data=valid_batches, epochs=100, verbose=2, batch_size=20)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1183, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\def_function.py", line 764, in _initialize
*args, **kwds))
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\function.py", line 3289, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\engine\training.py:855 train_function *
return step_function(self, iterator)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\engine\training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\engine\training.py:838 run_step **
outputs = model.train_step(data)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\engine\training.py:799 train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:530 minimize
return self.apply_gradients(grads_and_vars, name=name)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:630 apply_gradients
grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
C:\Users\h\.conda\envs\AI2\lib\site-packages\tensorflow\python\keras\optimizer_v2\utils.py:76 filter_empty_gradients
([v.name for _, v in grads_and_vars],))
ValueError: No gradients provided for any variable: ['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'dense/kernel:0', 'dense/bias:0'].
Solution
I needed to change loss to “ categorical_crossentropy”
Answered By - hs242424
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.