I am trying to train my model (Image classification) using Tensorflow. I keep getting an error when I try to run the following cell:
hist = model.fit(
train_generator,
epochs=100,
verbose=1,
steps_per_epoch=steps_per_epoch,
validation_data=valid_generator,
validation_steps=val_steps_per_epoch).history
Error is:
Epoch 1/100
27/31 [=========================>....] - ETA: 1s - loss: 0.7309 - acc: 0.6181
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
<ipython-input-36-b1c104100211> in <module>
2 val_steps_per_epoch = np.ceil(valid_generator.samples/valid_generator.batch_size)
3
----> 4 hist = model.fit(
5 train_generator,
6 epochs=100,
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1098 _r=1):
1099 callbacks.on_train_batch_begin(step)
-> 1100 tmp_logs = self.train_function(iterator)
1101 if data_handler.should_sync:
1102 context.async_wait()
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
826 tracing_count = self.experimental_get_tracing_count()
827 with trace.Trace(self._name) as tm:
--> 828 result = self._call(*args, **kwds)
829 compiler = "xla" if self._experimental_compile else "nonXla"
830 new_tracing_count = self.experimental_get_tracing_count()
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
853 # In this case we have created variables on the first call, so we run the
854 # defunned version which is guaranteed to never create variables.
--> 855 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
856 elif self._stateful_fn is not None:
857 # Release the lock early so that multiple threads can perform the call
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
2940 (graph_function,
2941 filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 2942 return graph_function._call_flat(
2943 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
2944
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1916 and executing_eagerly):
1917 # No tape is watching; skip to running the function.
-> 1918 return self._build_call_outputs(self._inference_function.call(
1919 ctx, args, cancellation_manager=cancellation_manager))
1920 forward_backward = self._select_forward_and_backward_functions(
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager)
553 with _InterpolateFunctionError(self):
554 if cancellation_manager is None:
--> 555 outputs = execute.execute(
556 str(self.signature.name),
557 num_outputs=self._num_outputs,
/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
UnknownError: UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fc88d55c9a0>
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/script_ops.py", line 249, in __call__
ret = func(*args)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 620, in wrapper
return func(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 891, in generator_py_func
values = next(generator_state.get_iterator(iterator_id))
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 807, in wrapped_generator
for data in generator_fn():
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 933, in generator_fn
yield x[i]
File "/opt/anaconda3/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 65, in __getitem__
return self._get_batches_of_transformed_samples(index_array)
File "/opt/anaconda3/lib/python3.8/site-packages/keras_preprocessing/image/iterator.py", line 227, in _get_batches_of_transformed_samples
img = load_img(filepaths[j],
File "/opt/anaconda3/lib/python3.8/site-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fc88d55c9a0>
[[{{node PyFunc}}]]
[[IteratorGetNext]] [Op:__inference_train_function_24233]
Function call stack:
train_function
I tried changing from loss='categorical_crossentropy' to loss='binary_crossentropy' but still the issue persists. I wish to train the model but the Epoch keeps getting stuck.
Edit:
The train generator function and where it is used is as follows:
IMAGE_SHAPE = (224, 224)
TRAINING_DATA_DIR = str(data_root)
datagen_kwargs = dict(rescale=1./255, validation_split=.20)
valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(**datagen_kwargs)
valid_generator = valid_datagen.flow_from_directory(
TRAINING_DATA_DIR,
subset="validation",
shuffle=True,
target_size=IMAGE_SHAPE
)
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(**datagen_kwargs)
train_generator = train_datagen.flow_from_directory(
TRAINING_DATA_DIR,
subset="training",
shuffle=True,
target_size=IMAGE_SHAPE)
for image_batch, label_batch in train_generator:
break
image_batch.shape, label_batch.shape
Output: ((32, 224, 224, 3), (32, 2))
print (train_generator.class_indices)
labels = '\n'.join(sorted(train_generator.class_indices.keys()))
with open('labels.txt', 'w') as f:
f.write(labels)
Output: {'off': 0, 'on': 1}
There was an issue with one of the img that was causing an issue and was pointed out by #Lescurel. To view the img you can run the following:
import PIL
from pathlib import Path
from PIL import UnidentifiedImageError
path = Path("INSERT PATH HERE").rglob("*.jpeg")
for img_p in path:
try:
img = PIL.Image.open(img_p)
except PIL.UnidentifiedImageError:
print(img_p)
You can also do the same for png or other formats. If there is an issue with your image, it will list it as soon as you run it
Similar to #EverydayDeveloper, but using glob to hold all image path with class.
import PIL
from PIL import UnidentifiedImageError
import glob
imgs_ = glob.glob("/home/ubuntu/imageTrain_dobby/SKJEWELLERY/classification/dataset/jewellery_dataset/train/*/*.jpg")
for img in imgs_:
try:
img = PIL.Image.open(img)
except PIL.UnidentifiedImageError:
print(img)
I had the same issue when accessing a training dataset on an external hard drive on a mac. The flow_from_directory function returned more files than I had placed in the folders.
Moving the whole training dataset to the local drive resolved the issue.
I encountered the same error when attempting to call Image.open() but I was calling this function on bytes received from a flutter client application via a multipart request.
The problem was that I didn't include the filename argument on the client side:
MultipartFile.fromBytes('image', await image.readAsBytes(),
filename: image.name)
After adding filename: image.name, Image.open was able to identify the image file.
Related
I am getting errors when training my CNN model which is for checking what a person is telling in sign language. I am working with keras, tensorflow.
This is my code:
import tensorflow as tf ;importing libraries
from tensorflow.keras import datasets,layers,models
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
; Data preprocessing
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('split__data/Train',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('split__data/Test',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
; Building the model
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters = 16,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Conv2D(filters = 32,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Conv2D(filters = 64,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(units=500,activation='relu'))
cnn.add(tf.keras.layers.Dense(units=1,activation='softmax'))
; compiling and training
cnn.compile(optimizer = 'adam' , loss= 'categorical_crossentropy',metrics = ['accuracy'])
;the next line is giving me error
cnn.fit(x = training_set,validation_data = test_set,batch_size = 32,epochs = 10)
This is the error. I am not able to get from where these errors are being generated. I have tried changing batch size, image height and width but nothing happens. I am getting same error.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-15-b67ba9813850> in <module>
----> 1 cnn.fit(x = training_set,validation_data = test_set,batch_size = 32,epochs = 10)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1181 _r=1):
1182 callbacks.on_train_batch_begin(step)
-> 1183 tmp_logs = self.train_function(iterator)
1184 if data_handler.should_sync:
1185 context.async_wait()
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
887
888 with OptionalXlaContext(self._jit_compile):
--> 889 result = self._call(*args, **kwds)
890
891 new_tracing_count = self.experimental_get_tracing_count()
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
948 # Lifting succeeded, so variables are initialized and we can run the
949 # stateless function.
--> 950 return self._stateless_fn(*args, **kwds)
951 else:
952 _, _, _, filtered_flat_args = \
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
3021 (graph_function,
3022 filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3023 return graph_function._call_flat(
3024 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
3025
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1958 and executing_eagerly):
1959 # No tape is watching; skip to running the function.
-> 1960 return self._build_call_outputs(self._inference_function.call(
1961 ctx, args, cancellation_manager=cancellation_manager))
1962 forward_backward = self._select_forward_and_backward_functions(
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
589 with _InterpolateFunctionError(self):
590 if cancellation_manager is None:
--> 591 outputs = execute.execute(
592 str(self.signature.name),
593 num_outputs=self._num_outputs,
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
InvalidArgumentError: In[0] mismatch In[1] shape: 35 vs. 1: [32,35] [500,1] 0 0
[[node gradient_tape/sequential/dense_1/MatMul (defined at <ipython-input-15-b67ba9813850>:1) ]] [Op:__inference_train_function_847]
Function call stack:
train_function
can someone help?
How many classes are in the dataset? You have the code
cnn.add(tf.keras.layers.Dense(units=1,activation='softmax'))
This would indicate you are doing binary classification which I expect is not what you want. Try this after your generator code
classes=list(training_set.class_indices.keys())
class_count=len (classes) # this integer is the number of nodes you need in your models final layer
change the last layer in your model to
cnn.add(tf.keras.layers.Dense(units=class_count,activation='softmax'))
to predict a single image- first read in the image. If you trained your model on RGB images then
import matplotlib.pyplot as plt
import cv2
classes=list(train_gen.class_indices.keys())
img_path = r' full path to the image'
img=plt.imread(img_path)
img=cv2.resize(img, (64.64))
img=img/255
img=np.expand_dims(img, axis=0)
prediction=model.predict( img, verbose=1)
index=argmax(prediction)
predicted_class = classes(index)
I don't bother building my own CNN, instead I use transfer learning with the EfficientNetB3 model. Note EfficientNet expects pixels in the range 0 to 255 so do not scale the pixels. Code below works well for most applications
base_model=tf.keras.applications.EfficientNetB3(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max')
x=base_model.output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)
x=Dropout(rate=.45, seed=123)(x)
output=Dense(class_count, activation='softmax')(x)
model=Model(inputs=base_model.input, outputs=output)
model.compile(Adamax(learning_rate=.001), loss='categorical_crossentropy', metrics=['accuracy'])
Im having problems when I use the library tensorflow_model_optimization.
I am developing a code to prune an already trained neural network.
I imported the weights from an h5 file and so I use tensorflor_model_optimization to prune my neural network.
I have this error when I call the fit method:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-26-ce9759e4dd53> in <module>
----> 1 model_for_pruning.fit_generator(base_treinamento, steps_per_epoch = 6000 /64, epochs = 5, validation_data = base_teste, validation_steps = 30, callbacks=callbacks)
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1859 use_multiprocessing=use_multiprocessing,
1860 shuffle=shuffle,
-> 1861 initial_epoch=initial_epoch)
1862
1863 def evaluate_generator(self,
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1098 _r=1):
1099 callbacks.on_train_batch_begin(step)
-> 1100 tmp_logs = self.train_function(iterator)
1101 if data_handler.should_sync:
1102 context.async_wait()
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
826 tracing_count = self.experimental_get_tracing_count()
827 with trace.Trace(self._name) as tm:
--> 828 result = self._call(*args, **kwds)
829 compiler = "xla" if self._experimental_compile else "nonXla"
830 new_tracing_count = self.experimental_get_tracing_count()
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
853 # In this case we have created variables on the first call, so we run the
854 # defunned version which is guaranteed to never create variables.
--> 855 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
856 elif self._stateful_fn is not None:
857 # Release the lock early so that multiple threads can perform the call
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
2941 filtered_flat_args) = self._maybe_define_function(args, kwargs)
2942 return graph_function._call_flat(
-> 2943 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
2944
2945 #property
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1917 # No tape is watching; skip to running the function.
1918 return self._build_call_outputs(self._inference_function.call(
-> 1919 ctx, args, cancellation_manager=cancellation_manager))
1920 forward_backward = self._select_forward_and_backward_functions(
1921 args,
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
558 inputs=args,
559 attrs=attrs,
--> 560 ctx=ctx)
561 else:
562 outputs = execute.execute_with_cancellation(
~\anaconda3\envs\supernova\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Conv2DCustomBackpropFilterOp only supports NHWC.
[[node gradient_tape/sequential_3/prune_low_magnitude_conv2d_18/Conv2D/Conv2DBackpropFilter (defined at <ipython-input-24-fc85f8818d30>:1) ]] [Op:__inference_train_function_10084]
Errors may have originated from an input operation.
Input Source operations connected to node gradient_tape/sequential_3/prune_low_magnitude_conv2d_18/Conv2D/Conv2DBackpropFilter:
sequential_3/prune_low_magnitude_activation_18/Relu (defined at C:\Users\Pichau\anaconda3\envs\supernova\lib\site-packages\tensorflow_model_optimization\python\core\sparsity\keras\pruning_wrapper.py:270)
Function call stack:
train_function
enter code here
My code:
from keras.models import model_from_json
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
arquivo = open('model.json', 'r')
estrutura_rede = arquivo.read()
arquivo.close()
model = model_from_json(estrutura_rede)
model.load_weights('model.h5')
gerador_treinamento = ImageDataGenerator(rescale=None)
base_treinamento = gerador_treinamento.flow_from_directory('data/train', target_size = (51,51), batch_size = 64, class_mode = 'binary')
gerador_teste = ImageDataGenerator(rescale=None)
base_teste = gerador_teste.flow_from_directory('data/test', target_size = (51,51), batch_size = 64, class_mode = 'binary')
import tempfile
import tensorflow as tf
import tensorflow_model_optimization as tfmot
prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude
batch_size = 128
epochs = 2
validation_split = 0.1
num_images = int(len(base_treinamento) * (1 - validation_split))
end_step = np.ceil(num_images / batch_size).astype(np.int32) * epochs
model_for_pruning.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model_for_pruning.summary()
logdir = tempfile.mkdtemp()
callbacks = [
tfmot.sparsity.keras.UpdatePruningStep(),
tfmot.sparsity.keras.PruningSummaries(log_dir=logdir),
]
model_for_pruning.fit_generator(base_treinamento, steps_per_epoch = 6000 /64, epochs = 5, validation_data = base_teste, validation_steps = 30, callbacks=callbacks)
python: 3.6.12
tensorflow: 2.2.0
tensorflow-model-optimization: 0.5.0
Can someone help me?
Changing the runtime from CPU to GPU worked for me.
If you are running it on CPU consider changing it to GPU.
This does not seem like a pruning issue, but instead a problem with the data format of the model. It looks like one Conv2D layer should be using NHWC format (data_format="channels_last").
Could you share the code for the model?
After trying to run this simple neural network program for prediction of stock prices using LSTM layers I ran in to the following error
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-15-47761426ea46> in <module>
1 # Fitting the RNN to the Training set
----> 2 model.fit(X_train, Y_train, epochs=1)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1098 _r=1):
1099 callbacks.on_train_batch_begin(step)
-> 1100 tmp_logs = self.train_function(iterator)
1101 if data_handler.should_sync:
1102 context.async_wait()
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
826 tracing_count = self.experimental_get_tracing_count()
827 with trace.Trace(self._name) as tm:
--> 828 result = self._call(*args, **kwds)
829 compiler = "xla" if self._experimental_compile else "nonXla"
830 new_tracing_count = self.experimental_get_tracing_count()
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
869 # This is the first call of __call__, so we have to initialize.
870 initializers = []
--> 871 self._initialize(args, kwds, add_initializers_to=initializers)
872 finally:
873 # At this point we know that the initialization is complete (or less
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
723 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
724 self._concrete_stateful_fn = (
--> 725 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
726 *args, **kwds))
727
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2967 args, kwargs = None, None
2968 with self._lock:
-> 2969 graph_function, _ = self._maybe_define_function(args, kwargs)
2970 return graph_function
2971
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3359
3360 self._function_cache.missed.add(call_context_key)
-> 3361 graph_function = self._create_graph_function(args, kwargs)
3362 self._function_cache.primary[cache_key] = graph_function
3363
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3194 arg_names = base_arg_names + missing_arg_names
3195 graph_function = ConcreteFunction(
-> 3196 func_graph_module.func_graph_from_py_func(
3197 self._name,
3198 self._python_function,
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
988 _, original_func = tf_decorator.unwrap(python_func)
989
--> 990 func_outputs = python_func(*func_args, **func_kwargs)
991
992 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
632 xla_context.Exit()
633 else:
--> 634 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
635 return out
636
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
NotImplementedError: in user code:
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:754 train_step
y_pred = self(x, training=True)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1007 __call__
outputs = call_fn(inputs, *args, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py:389 call
outputs = layer(inputs, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:660 __call__
return super(RNN, self).__call__(inputs, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1007 __call__
outputs = call_fn(inputs, *args, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent_v2.py:1163 call
inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:859 _process_inputs
initial_state = self.get_initial_state(inputs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:642 get_initial_state
init_state = get_initial_state_fn(
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:2506 get_initial_state
return list(_generate_zero_filled_state_for_cell(
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:2987 _generate_zero_filled_state_for_cell
return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:3003 _generate_zero_filled_state
return nest.map_structure(create_zeros, state_size)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:659 map_structure
structure[0], [func(*x) for x in entries],
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/util/nest.py:659 <listcomp>
structure[0], [func(*x) for x in entries],
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py:3000 create_zeros
return array_ops.zeros(init_state_size, dtype=dtype)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:2819 wrapped
tensor = fun(*args, **kwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:2868 zeros
output = _constant_if_small(zero, shape, dtype, name)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:2804 _constant_if_small
if np.prod(shape) < 1000:
<__array_function__ internals>:5 prod
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/numpy/core/fromnumeric.py:3030 prod
return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/numpy/core/fromnumeric.py:87 _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflowenv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:852 __array__
raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (sequential/lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
This error occurs in the following program:
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Import General Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# In[2]:
# Import the Training DataSet
training_dataset = pd.read_csv('/Users/frisodekruiff/Downloads/Archive/AAPL.csv')
training_set = training_dataset.iloc[:,1:-1].values
# In[3]:
# Importing sklearn
from sklearn.preprocessing import MinMaxScaler
# Feature Scaling
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
print(training_set_scaled)
# In[4]:
# Creating Data Structure with 252 Data Points and 1 answer
X_train = []
Y_train = []
for i in range(252,len(training_set)):
X_train.append(training_set_scaled[i - 252:i,0])
Y_train.append(training_set_scaled[i,0])
X_train, Y_train = np.array(X_train), np.array(Y_train)
# In[5]:
# Reshaping
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
# In[6]:
# Importing Tensorflow and Keras
import tensorflow as tf
from tensorflow import keras
# In[7]:
# Importing Keras Libraries
from keras import models
from keras import layers
# In[8]:
# Initializing the RNN
model = keras.Sequential()
# In[9]:
# Adding the First Layer
model.add(layers.LSTM(128, return_sequences = True))
# Adding First Dropout
model.add(layers.Dropout(0.2))
# In[10]:
# Adding the Second Layer
model.add(layers.LSTM(64))
# Adding Second Dropout
model.add(layers.Dropout(0.2))
# In[11]:
# Adding the Third Layer
model.add(layers.LSTM(32))
# Adding Third Dropout
model.add(layers.Dropout(0.2))
# In[12]:
# Adding the Fourth Layer
model.add(layers.LSTM(16))
# Adding Fourth Dropout
model.add(layers.Dropout(0.2))
# In[13]:
# Adding Output Layer
model.add(layers.Dense(units = 10))
# In[14]:
# Compiling the RNN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
# In[15]:
# Fitting the RNN to the Training set
model.fit(X_train, Y_train, epochs=1)
# In[ ]:
model.summary()
Where the error occurs after the model.fit command. I have been researching this error and what I have found so far is that it might be a problem of my version of numpy I use, however I'm not sure this is the case and I'm also not sure how to change it. I have installed tensorflow package from GitHub repository for apple M1 using the videos of New Phase as linked below:
https://www.youtube.com/watch?v=ykCY_tJbhNw
As there are a lot of online tutorials how to install tensorflow on Mac M1 and I also have tried a lot of them without much success so far, I'm not totally confident that the installation procedure has been successful/correct. Can somebody maybe point me in the right direction to solve this problem?
I am a tad new to Tensorflow and I am having trouble running this simple CNN.
I have my images separated into separate directories for each class, which I load into train_dataset using image_dataset_from_directory.
from the documentation, this should yield a tuple (images, labels), where images has shape (batch_size, image_size[0], image_size[1], num_channels), and labels are a float32 tensor of shape (batch_size, num_classes). num_channels is 3 as the images are rgb
However when I try to fit using my model, I get an error saying that the predictions are [32,5] and labels shape [160]. It seems to me the batches in the labels have 'collapsed'.
Here's some snippets:
BATCH_SIZE = 32
EPOCHS = 1
IMG_SIZE=(300, 300)
SEED = 1
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
directory='train/train_images/', label_mode='categorical', class_names=class_names, color_mode='rgb', batch_size=BATCH_SIZE, image_size=IMG_SIZE)
IMG_SHAPE = IMG_SIZE + (3,)
n_classes = len(train_dataset.class_names)
def build_model():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(input_shape=IMG_SHAPE, kernel_size=(5, 5), filters=32, activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=(3, 3)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(units=n_classes, activation='softmax')
])
return model
model = build_model()
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
model.fit(train_dataset, epochs = EPOCHS, batch_size = BATCH_SIZE)
Error Message:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-19-86d96e744ef0> in <module>
----> 1 model.fit(train_dataset, epochs = EPOCHS, batch_size = BATCH_SIZE)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1096 batch_size=batch_size):
1097 callbacks.on_train_batch_begin(step)
-> 1098 tmp_logs = train_function(iterator)
1099 if data_handler.should_sync:
1100 context.async_wait()
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
805 # In this case we have created variables on the first call, so we run the
806 # defunned version which is guaranteed to never create variables.
--> 807 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
808 elif self._stateful_fn is not None:
809 # Release the lock early so that multiple threads can perform the call
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
2827 with self._lock:
2828 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2829 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2830
2831 #property
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _filtered_call(self, args, kwargs, cancellation_manager)
1846 resource_variable_ops.BaseResourceVariable))],
1847 captured_inputs=self.captured_inputs,
-> 1848 cancellation_manager=cancellation_manager)
1849
1850 def _call_flat(self, args, captured_inputs, cancellation_manager=None):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1922 # No tape is watching; skip to running the function.
1923 return self._build_call_outputs(self._inference_function.call(
-> 1924 ctx, args, cancellation_manager=cancellation_manager))
1925 forward_backward = self._select_forward_and_backward_functions(
1926 args,
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager)
548 inputs=args,
549 attrs=attrs,
--> 550 ctx=ctx)
551 else:
552 outputs = execute.execute_with_cancellation(
/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [32,5] and labels shape [160]
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-18-1904262c6a7b>:1) ]] [Op:__inference_train_function_928]
Function call stack:
train_function
I think you should explicitly compile your model before executing. For that, you may omit 'input_shape' parameter in first layer. See model.compile in keras documentation. make sure that you keep loss as "categorical_crossentropy" or tf.keras.losses.CategoricalCrossentroy() . Then try again. I hope this helps.
Also it would help if you share your file structure.
Basically I am implementing a model that employs perceptual loss to perform single image super-resolution. I constructed my full model such that the input will first pass through the main model, then feed into a pretrained VGG16, and give the output from layer[5] of VGG16 as the final output of the full model.
I tried to pass a pre-trained VGG16 model to my data generator in order to prepare my ground truth images for the computation of perceptual loss on the fly. However I have encountered value issues during the training with fit_generator.
I have tried write my own loop to generate data for each batch and use the train_on_batch function instead, and it is working fine. However I do want the benefit of use_multiprocessing with fit_generator.
Here is the generator I have written.I pass lossModel to the generator and use it to generate the output for training with perceptual loss.
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, x_train, y_train, lossModel, batch_size=4, shuffle=True):
'Initialization'
self.x_train = x_train
self.y_train = y_train
self.lossModel = lossModel
self.batch_size = batch_size
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.x_train) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate batch of data
idx = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
x = self.x_train[idx,]
y = self.lossModel.predict_on_batch(self.y_train[idx,])
return x, y
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.x_train))
if self.shuffle == True:
np.random.shuffle(self.indexes)
And here I construct the model.
### Create Image Transformation Model ###
mainModel = ResnetBuilder.build((3,72,72), 5, basic_block, [1, 1, 1, 1, 1])
### Create Loss Model (VGG16) ###
lossModel = VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(288,288,3))
lossModel.trainable=False
for layer in lossModel.layers:
layer.trainable=False
### Create New Loss Model (Use Relu2-2 layer output for perceptual loss)
lossModel = Model(lossModel.inputs,lossModel.layers[5].output)
lossModelOutputs = lossModel(mainModel.output)
### Create Full Model ###
fullModel = Model(mainModel.input, lossModelOutputs)
### Compile FUll Model
fullModel.compile(loss='mse', optimizer='adam',metrics=['mse'])
trained_epochs=0
Error occurs during fit_generator(). Notice the dimension of my input is (72,72,3) and outputs from VGG.layer[5] are in (144,144,128), my y_train is ground truth image in (288,288,3).
# Generators
training_generator = DataGenerator(x_train, y_train, lossModel, batch_size=4, shuffle=True)
# Train model on dataset
fullModel.fit_generator(generator=training_generator, use_multiprocessing=True, workers=6)
Epoch 1/1
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/utils/data_utils.py", line 401, in get_index
return _SHARED_SEQUENCES[uid][i]
File "/home/lucien/sr-perceptual/my_classes.py", line 26, in __getitem__
y = self.lossModel.predict_on_batch(self.y_train[idx,])
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/engine/training.py", line 1273, in predict_on_batch
self._make_predict_function()
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/engine/training.py", line 554, in _make_predict_function
**kwargs)
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2744, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2546, in __init__
with tf.control_dependencies(self.outputs):
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5004, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4543, in control_dependencies
c = self.as_graph_element(c)
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3569, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("block2_conv2/Relu:0", shape=(?, 144, 144, 128), dtype=float32) is not an element of this graph.
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-10-4a040e0935cf> in <module>
1 # Train model on dataset
----> 2 fullModel.fit_generator(generator=training_generator, use_multiprocessing=True, workers=6)
~/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 #interfaces.legacy_generator_methods_support
~/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
179 batch_index = 0
180 while steps_done < steps_per_epoch:
--> 181 generator_output = next(output_generator)
182
183 if not hasattr(generator_output, '__len__'):
~/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
599 except Exception as e:
600 self.stop()
--> 601 six.reraise(*sys.exc_info())
602
603
~/anaconda3/envs/fyp/lib/python3.6/site-packages/six.py in reraise(tp, value, tb)
691 if value.__traceback__ is not tb:
692 raise value.with_traceback(tb)
--> 693 raise value
694 finally:
695 value = None
~/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
593 try:
594 while self.is_running():
--> 595 inputs = self.queue.get(block=True).get()
596 self.queue.task_done()
597 if inputs is not None:
~/anaconda3/envs/fyp/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
ValueError: Tensor Tensor("block2_conv2/Relu:0", shape=(?, 144, 144, 128), dtype=float32) is not an element of this graph.
The problem here is mutli-threading. When you are calling the 6 workers, block2_conv2/Relu:0 is created after graph is terminated.
The problem is with _make_predict_function(). You can check this file in your PC for the reasons(i got this from your error text) File "/home/lucien/anaconda3/envs/fyp/lib/python3.6/site-packages/keras/engine/training.py", line 1273, in predict_on_batch self._make_predict_function().
Some ways in which you can remove the errors are :
Use theano backend.
call model._make_predict_function() right after loading the trained model.
Use global model :
Functions :
def load_model():
global model
model = yourmodel(weights=xx111122)
# this is key : save the graph after loading the model
global graph
graph = tf.get_default_graph()
While predicting:
with graph.as_default():
preds = model.predict(image)
#... etc