tensorflow 我可以从firebase存储中加载hdf5模型吗?

piztneat  于 2023-03-19  发布在  其他
关注(0)|答案(2)|浏览(128)

我使用firebase作为数据库和存储器,我想在python上运行我的模型并执行predict。
我如何加载模型并在python上运行它?
如何访问存储在存储中的模型?
我试着去做:

# import firebase_admin
# from firebase_admin import credentials
# from firebase_admin import storage
#
# cred = credentials.Certificate('serviceAccountKey.json')
# firebase_admin.initialize_app(cred, {
# 'storageBucket': 'gs://app.appspot.com.appspot.com'
# })

import pyrebase
config={
    "apiKey": "",
    "authDomain": "app.firebaseapp.com",
    "storageBucket":"app.appspot.com",
    "databaseURL":"https://app.firebaseio.com",
    "type": "service_account",
    "project_id": "app"
    # "private_key_id": "633bcc74c30d50157a0aea2dd"
}

firebase=pyrebase.initialize_app(config)
storage=firebase.storage()

我检查了我是否连接到存储,并有上传和下载照片的选项。
然后,我复制了指向模型的链接

import numpy as np
from tensorflow.keras.preprocessing import image
import tensorflow as tf

MODEL_PATH = "gs://app.appspot.com/model.hdf5"

model = tf.keras.models.load_model(MODEL_PATH)

但它显示了错误,并且不起作用:

Traceback (most recent call last):
  File "C:\Users\hoday\Documents\GitHub\SRD\storgesever.py", line 4, in <module>
    import pyrebase
  File "C:\Users\hoday\venv\untitled\lib\site-packages\pyrebase\__init__.py", line 1, in <module>
    from .pyrebase import initialize_app
  File "C:\Users\hoday\venv\untitled\lib\site-packages\pyrebase\pyrebase.py", line 1, in <module>
    import requests
  File "C:\Users\hoday\venv\untitled\lib\site-packages\requests\__init__.py", line 63, in <module>
    from . import utils
  File "C:\Users\hoday\venv\untitled\lib\site-packages\requests\utils.py", line 27, in <module>
    from .cookies import RequestsCookieJar, cookiejar_from_dict
  File "C:\Users\hoday\venv\untitled\lib\site-packages\requests\cookies.py", line 172, in <module>
    class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'

Process finished with exit code 1

请帮助我:)

k2arahey

k2arahey1#

使用Firebase客户端将模型下载到本地目标,然后从本地存储加载。

mspsb9vt

mspsb9vt2#

这是一般性的问题,你可以创建一个项目,并访问其数据库或驱动器尝试使用GoogleDrive。
您可以遵循Google Drive中的指导原则,该指导原则使用具有项目凭据访问权限的Google云端硬盘。
这个问题是关于谷歌云,但它适用于Tensorflow或任何文件可以用相同的想法共反演。

[样品]:

from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

# define path variables
credentials_file_path = 'F:\\temp\\Python\\credentials\\credentials.json'
clientsecret_file_path = 'F:\\temp\\Python\\credentials\\client_secret_183167298301-pfhgtdf6k8r4918csmftemgk00ln8l4r.apps.googleusercontent.com.json'

# define API scope
SCOPE = 'https://www.googleapis.com/auth/drive'

# define store
store = file.Storage(credentials_file_path)
credentials = store.get()
# get access token
if not credentials or credentials.invalid:
    flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE)
    credentials = tools.run_flow(flow, store)
    
# define API service
http = credentials.authorize(Http())
drive = discovery.build('drive', 'v3', http=http)   
    
# define a function to retrieve all files
def retrieve_all_files(api_service, filename_to_search):
    results = []
    page_token = None

    while True:
        try:
            param = {}

            if page_token:
                param['pageToken'] = page_token

            files = api_service.files().list(**param).execute()
# append the files from the current result page to our list
            results.extend(files.get('files'))
# Google Drive API shows our files in multiple pages when the number of files exceed 100
            page_token = files.get('nextPageToken')

            if not page_token:
                break

        except errors.HttpError as error:
            print(f'An error has occurred: {error}')
            break
# output the file metadata to console
    for file in results:
        if file.get('name') == filename_to_search:
            print(file)
            break

    return results, file
            
# call the function
filename_to_search = 'Christmas Plan'
all_files, search_file = retrieve_all_files(drive, filename_to_search)

print( all_files )
print( search_file )

相关问题