Issue
I've made an image recognition neural network in windows. Tested it, and it works (hurray!). It uses tensorflow.
Now I wanted to transfer it on Raspberry Pi's Imager, and run it there in a virtual environment (virtualenv).
I installed all libraries - a hard job to be honest, and tested them and all seems to work fine.
Important notes:
- the script on windows had an additional folder called saved_model, in which holds the model, and another folder called venv, that, I suppose, holds the dependencies, and the imported libraries, but I'm not sure.
- I had to install tensorflow manually in the virtual machine, cause it's not quite supported on Raspberry Pi's Imager by Google, but there's a supported version maintained by the community. I use 2.5.0-rc on the Raspberry Pi cause it's the last one that's supported on the Legacy version. I'm also using the legacy version in order to have access to a working camera module, since I've noticed that the camera module and library in python aren't fully implemented yet, the PiCamera2 being in an early beta version, and I had difficulty installing it, besides more problems. Having said this this is the reason I'm using the legacy version, and it's worth noting it's a 32 bit version. On the other hand on the PC I'm working with 2.8.0 version of Python. It's not a nice solution but it's what I got.
Having said this, after almost a week of struggle, and almost giving up this entire project entirely a few times, I managed to install everything, and am now in the final stage of installing it on Raspberry Pi.
I unfortunately get this message:
File "test.py", line 17, in <module>
model = keras.models.load_model('saved_model/my_model')
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 206, in load_model
return saved_model_load.load(filepath, compile, options)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 146, in load
keras_loader.load_layers(compile=compile)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 380, in load_layers
node_metadata.metadata)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 420, in _load_layer
obj, setter = self._revive_from_config(identifier, metadata, node_id)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 445, in _revive_from_config
obj, self._proto.nodes[node_id], node_id)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 305, in _add_children_recreated_from_config
metadata = json_utils.decode(proto.user_object.metadata)
File "/home/pi/project/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py", line 62, in decode
return json.loads(json_string, object_hook=_decode_helper)
File "/usr/lib/python3.7/json/__init__.py", line 361, in loads
return cls(**kw).decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
Here's the relevant code of the program.
# Import required Libraries
import os
from tkinter import *
import cv2
import numpy as np
from PIL import Image
from PIL import ImageTk
from playsound import playsound
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # To get rid of all these warnings
from tensorflow import keras
from tensorflow import nn
from tensorflow import where
model = keras.models.load_model('saved_model/my_model')
cap = cv2.VideoCapture(0)
if not (cap.isOpened()):
print("Video device not connected.")
win = Tk()
win.geometry("640x550")
label = Label(win)
label.grid(row=0, column=0)
cap = cv2.VideoCapture(0)
Perhaps it's the incompatibility between Tensorflow version? I doubt it however, since those versions aren't that far apart, but you never know... Or The way I placed the files? I mean I put those files directly in the same folder, like in windows, right next to the env file. But this shouldn't be it, cause I tested it with a hello world program before. (The files are in the same folder as env. I mean there's a folder called "env" containing the bin and the other stuff required by the virtualenv, there's the saved_model, the venv, and the .py file)
Anyway any kind of help would be very much appreciated.
Thanks alot, Marcus
Solution
I had the same problem here, and it was because of incompatibility between TensorFlow & Keras versions on Windows (worked on 2.8.0) and Raspbian OS (worked on 2.0.0).
Due to it was impossible to upgrade the TensorFlow and Keras versions on my Raspberry Pi, I had to set up version 2.0.0 on Windows and train the model with it, then the keras.models.load_model
function on Raspberry Pi worked (the JSON decoder error would disappear).
I also had to install h5py version 2.10.0 on both Windows and Raspberry Pi to work with TensorFlow & Keras 2.0.0.
Try to install the 2.5.0-rc version on Windows the same as one on your Raspberry Pi and train your model again, I hope it works.
Answered By - Salma El-Naggar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.