Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_model Exception #1928

Open
kyocheng opened this issue Oct 10, 2024 · 0 comments
Open

load_model Exception #1928

kyocheng opened this issue Oct 10, 2024 · 0 comments

Comments

@kyocheng
Copy link

Hi, I'm looking for a solution to my issue, and I tried several suggestions with no success. I hope someone can help me with this.

this is my code

import os
import sys
print(sys.path)
#import keras
import numpy as np

from sklearn.datasets import load_files
from keras.models import load_model
import tensorflow as tf
import autokeras as ak
from datetime import datetime
# dataset = keras.utils.get_file(
#     fname="aclImdb.tar.gz",
#     origin="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz",
#     extract=True,
# )

# set path to dataset
IMDB_DATADIR = "/root/blazehttp/cmdi_single"

classes = ["pos", "neg"]
train_data = load_files(
    os.path.join(IMDB_DATADIR, "train"), shuffle=True, categories=classes
)
test_data = load_files(
    os.path.join(IMDB_DATADIR, "test"), shuffle=False, categories=classes
)

x_train = np.array(train_data.data)[:10]
y_train = np.array(train_data.target)[:10]
x_test = np.array(test_data.data)[:20]
y_test = np.array(test_data.target)[:20]

print(x_train.shape)  # (25000,)
print(y_train.shape)  # (25000, 1)
y_train = y_train.reshape(-1, 1)
print(y_train.shape)  # (25000, 1)
print("x_train[0][:50]:",x_train[0][:50])  # this film was just brilliant casting



clf = ak.TextClassifier(
    overwrite=False, max_trials=1,directory="autokeras_model_cmd",project_name="autokeras_project_cmd"
)  # It only tries 1 model as a quick demo.

clf.fit(x_train, y_train,epochs=1,  batch_size=2) # Added validation data for monitoring performance)


print("start test:")
predicted_y = clf.predict(x_test)

print("predicted_y:",predicted_y)
print("x_test:",x_test)
print("y_test:",y_test)

model = clf.export_model()

print(type(model))  # <class 'tensorflow.python.keras.engine.training.Model'>

#时间字符串
now = datetime.now()
formatted_str_ = now.strftime("%Y-%m-%d-%H-%M-%S")
file_name = "model_autokeras.keras" #"model_autokeras_" + formatted_str_ + ".keras"
print("keras_model.summary:",file_name)
model.summary()
print("model.save:",file_name)
model.save(file_name)  # 修复方式:确保文件扩展名为.keras

loaded_model = load_model(
    file_name,custom_objects=ak.CUSTOM_OBJECTS
)

 Non-trainable params: 0 (0.00 B)
model.save: model_autokeras_2024-10-10-10-11-44.keras
Traceback (most recent call last):
  File "/root/test/automl/cmdi-test.py", line 73, in <module>
    loaded_model = load_model(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_api.py", line 189, in load_model
    return saving_lib.load_model(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py", line 365, in load_model
    return _load_model_from_fileobj(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py", line 442, in _load_model_from_fileobj
    model = _model_from_config(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py", line 431, in _model_from_config
    model = deserialize_keras_object(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py", line 718, in deserialize_keras_object
    instance = cls.from_config(inner_config)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/model.py", line 526, in from_config
    return functional_from_config(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/functional.py", line 518, in functional_from_config
    process_node(layer, node_data)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/functional.py", line 465, in process_node
    layer(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/utils/tensor_utils.py", line 74, in wrapper
    x = fn(self, x, y=y, sample_weight=sample_weight, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/models/text_classifier_preprocessor.py", line 110, in call
    x = tuple(self.tokenizer(segment) for segment in x)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/models/text_classifier_preprocessor.py", line 110, in <genexpr>
    x = tuple(self.tokenizer(segment) for segment in x)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/utils/tensor_utils.py", line 62, in wrapper
    x = fn(self, x, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/tokenizers/tokenizer.py", line 205, in call
    return self.tokenize(inputs, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/utils/tensor_utils.py", line 62, in wrapper
    x = fn(self, x, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/tokenizers/word_piece_tokenizer.py", line 475, in tokenize
    self._check_vocabulary()
  File "/usr/local/lib/python3.10/dist-packages/keras_nlp/src/tokenizers/word_piece_tokenizer.py", line 468, in _check_vocabulary
    raise ValueError(
ValueError: Exception encountered when calling BertTextClassifierPreprocessor.call().

Could not automatically infer the output shape / dtype of 'bert_text_classifier_preprocessor' (of type BertTextClassifierPreprocessor). Either the `BertTextClassifierPreprocessor.call()` method is incorrect, or you need to implement the `BertTextClassifierPreprocessor.compute_output_spec() / compute_output_shape()` method. Error encountered:

Exception encountered when calling BertTokenizer.call().

No vocabulary has been set for WordPieceTokenizer. Make sure to pass a `vocabulary` argument when creating the layer.

Arguments received by BertTokenizer.call():
  ? inputs=tf.Tensor(shape=(None,), dtype=string)
  ? args=<class 'inspect._empty'>
  ? training=None
  ? kwargs=<class 'inspect._empty'>

Arguments received by BertTextClassifierPreprocessor.call():
  ? args=('<KerasTensor shape=(None,),\

Setup Details
Include the details about the versions of:

Python 3.10.15
autokeras 2.0.0
keras 3.6.0
keras-nlp 0.15.1
keras-tuner 1.4.7
numpy 1.26.4
tensorflow 2.17.0
tensorflow-io-gcs-filesystem 0.37.1
tensorflow-text 2.17.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant