I have the following code to try to recompile the last layer of the mobilenetv2 to get a face recognition:
import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt
base_dir = "C:\\Users\\Maik\\Desktop\\dataset"
IMAGE_SIZE = 224
BATCH_SIZE = 5
data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1. / 255,
validation_split=0.2)
train_generator = data_generator.flow_from_directory(
base_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
subset='training')
# Create a validation generator
val_generator = data_generator.flow_from_directory(
base_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
subset='validation')
# Triggering a training generator for all the batches
for image_batch, label_batch in train_generator:
break
# This will print all classification labels in the console
print(train_generator.class_indices)
# Creating a file which will contain all names in the format of next lines
labels = '\n'.join(sorted(train_generator.class_indices.keys()))
# Writing it out to the file which will be named 'labels.txt'
with open('labels.txt', 'w') as f:
f.write(labels)
# Resolution of images (Width , Height, Array of size 3 to accommodate RGB Colors)
IMG_SHAPE = (IMAGE_SIZE, IMAGE_SIZE, 3)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
model = tf.keras.Sequential([
base_model, # 1
tf.keras.layers.Conv2D(32, 3, activation='relu'), # 2
tf.keras.layers.Dropout(0.2), # 3
tf.keras.layers.GlobalAveragePooling2D(), # 4
tf.keras.layers.Dense(3, activation='softmax') # 5
])
model.summary()
# Printing some statistics
print('Number of trainable variables = {}'.format(len(model.trainable_variables)))
# Train the model
epochs = 10
# Fitting / Training the model
history = model.fit(train_generator,
epochs=epochs,
validation_data=val_generator)
base_model.trainable = True
print("Number of layers in the base model: ", len(base_model.layers))
fine_tune_at = 100
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(1e-5),
metrics=['accuracy'])
model.summary()
print('Number of trainable variables = {}'.format(len(model.trainable_variables)))
history_fine = model.fit(train_generator,
epochs=5,
validation_data=val_generator
)
saved_model_dir = 'save/fine_tuning.h5'
model.save(saved_model_dir)
print("Model Saved to save/fine_tuning.h5")
'''
#tf.saved_model.save(model, saved_model_dir)
converter = tf.lite.TFLiteConverter.from_keras_model_file(model)
//Use this if 238 fails tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
and get the following error message:
Traceback (most recent call last):
File "C:\Users\Maik\PycharmProjects\FR_Test\main.py", line 149, in <module>
history = model.fit(train_generator,
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
File "C:\Users\Maik\PycharmProjects\FR_Test\main.py", line 149, in <module>
history = model.fit(train_generator,
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 1564, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 1160, in train_function
return step_function(self, iterator)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 1146, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 1135, in run_step
outputs = model.train_step(data)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\losses.py", line 272, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\losses.py", line 1990, in categorical_crossentropy
return backend.categorical_crossentropy(
File "C:\Users\Maik\PycharmProjects\FR_Test\venv\lib\site-packages\keras\backend.py", line 5535, in categorical_crossentropy
return tf.nn.softmax_cross_entropy_with_logits(
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[5,4] labels_size=[5,6]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_8675]
2022-11-09 18:47:37.818932: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node PyFunc}}]]
Process finished with exit code 1
Has anyone encountered that before and can give me any pointers as to why this hapens?
CodePudding user response:
The main error in your code is that you are calling the fit
method before and after compile
method.
fit
method should only be called after compile
method.
So, remove these lines.
# Fitting / Training the model
history = model.fit(train_generator,
epochs=epochs,
validation_data=val_generator)
CodePudding user response:
the answer was to include this
from PIL import Image, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True
as there apparently were different image encodings