ImageAI
ImageAI copied to clipboard
ValueError: Cannot assign to variable predictions/kernel:0 due to variable shape
So I've been trying to use ImageAI to identify two different custom images. I should have trained the AI to recognize both classes correctly and I used 500 images for both classes. Here is the code I used for that:
from imageai.Classification.Custom import ClassificationModelTrainer
model_trainer = ClassificationModelTrainer() model_trainer.setModelTypeAsMobileNetV2() model_trainer.setDataDirectory(r"C:\Users*****\Documents\Atom\Image Detection\images") model_trainer.trainModel(num_objects=2, num_experiments=10, enhance_data=False, batch_size=10, show_network_summary=True)
After that, I used this code to run CustomImageClassification().classifyImage:
from imageai.Classification.Custom import CustomImageClassification import os
execution_path = r"C:\Users*****\Documents\Atom\Image Detection"
prediction = CustomImageClassification() prediction.setModelTypeAsMobileNetV2() prediction.setModelPath(os.path.join(execution_path, "mobilenet_v2.h5")) prediction.setJsonPath(os.path.join(execution_path, "model_class_4.json")) prediction.loadModel(num_objects=2, classification_speed="normal")
input_image = os.path.join(execution_path, "test_image.png")
predictions, probabilities = prediction.classifyImage(input_image, result_count=2)
for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)
However, after I run it, this error shows up: ValueError: Cannot assign to variable predictions/kernel:0 due to variable shape (1280, 2) and value shape (1280, 1000) are incompatible
I don't know what the variable and value shapes are, or how they are assigned so this is quite confusing, can anyone help me understand what's going wrong?
facing the same error. are you using tensorflow 2.5.0?
Yes, I am. I have tried installing previous versions of tensorflow using pip from the cmd, but then this error shows up:
C:\Users*****>py -m pip install tensorflow==2.4.0 ERROR: Could not find a version that satisfies the requirement tensorflow==2.4.0 (from versions: 2.5.0rc0, 2.5.0rc1, 2.5.0rc2, 2.5.0rc3, 2.5.0, 2.6.0rc0, 2.6.0rc1) ERROR: No matching distribution found for tensorflow==2.4.0
I feel like being able to install previous versions of the modules required for imageai would solve a lot of issues that I've come across.
yup. you must be having python 3.8 or above. I downgraded to 3.6 and tf 1.15.2 and pass through this error. but having another.
tensorflow.python.framework.errors_impl.InvalidArgumentError: Computed output size would be negative: -227 [input_size: 56, effective_filter_size: 512, stride: 2]
I used pyenv, or here for multiple versions of python or you can use conda.
I think I'm stuck at the same issue. Had to downgrade to python3.8 to get imageai to install at all due to a scipy dependency, and now getting this error when trying to predict on a custom model.
I tried downgrading to tensorflow 1.15.5 but the dependencies for that are incompatible with imageai (imageai needs numpy 1.19.3 and that tensorflow needs 1.18.5). I looked when the imageai numpy version was added and it was Jan 2020 when there was a commit claiming support for tensorflow v2 so I took tensortflow 2.1.1 which would have been current when that commit happened, but that still failed so I'm pretty stuck. The model building works OK, and even the validation works, but doing predictions fails with:
Traceback (most recent call last):
File "e:\Python37\lib\site-packages\imageai\Classification\__init__.py", line 139, in loadModel
model.load_weights(self.modelPath)
File "e:\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 234, in load_weights
return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
File "e:\Python37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1222, in load_weights
hdf5_format.load_weights_from_hdf5_group(f, self.layers)
File "e:\Python37\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 699, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "e:\Python37\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3323, in batch_set_value
x.assign(np.asarray(value, dtype=dtype(x)))
File "e:\Python37\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py", line 819, in assign
self._shape.assert_is_compatible_with(value_tensor.shape)
File "e:\Python37\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1110, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (2048, 1000) and (2048, 2) are incompatible
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/andrewss/git/christmas_card_2021/make_prediction.py", line 11, in <module>
prediction.loadModel()
File "e:\Python37\lib\site-packages\imageai\Classification\__init__.py", line 143, in loadModel
raise ValueError("An error occured. Ensure your model file is in {}".format(self.modelPath))
ValueError: An error occured. Ensure your model file is in C:\Users\andrewss\git\christmas_card_2021\training_data\treeorme\models\model_ex-007_acc-0.980132.h5
Unless there are any pointers to other things to check/change I think the problems I've had with this are enough that I'm going to go directly to tensorflow to do the model.
@s-andrews Tensorflow==2.4.0 should work but be aware that you need a different cuda version
@ekesdf thanks for the suggestion but I get the same error with 2.4.0
i am facing the same issue - using python 3.7.6 and tensorflow 2.4 as recommended by imageai compatibility. any help please. (virtenv3.7) pi@pi:~/ml/detection $ python customprediction.py Traceback (most recent call last):
truncated
....................................................... File "/home/pi/virtenv3.7/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 891, in assign (tensor_name, self._shape, value_tensor.shape)) ValueError: Cannot assign to variable predictions/kernel:0 due to variable shape (2048, 10) and value shape (2048, 2) are incompatible
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "customprediction.py", line 10, in
can some one tell me what i am doing wrong and how to solve this issue
I also have this problem, has anyone solved it?