Модель сохраняется как файл «.keras».
Но я получаю следующую ошибку:
Код: Выделить всё
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/keras/src/models/model.py:533, in Model.from_config(cls, config, custom_objects)
532 try:
--> 533 return cls(**config)
534 except TypeError as e:
TypeError: ViTClassfier.__init__() missing 2 required positional arguments: 'input_shape' and 'n_classes'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/serialization_lib.py:718, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
--> 718 instance = cls.from_config(inner_config)
719 except TypeError as e:
File /opt/conda/lib/python3.10/site-packages/keras/src/models/model.py:535, in Model.from_config(cls, config, custom_objects)
534 except TypeError as e:
--> 535 raise TypeError(
536 "Unable to revive model from config. When overriding "
537 "the `get_config()` method, make sure that the "
538 "returned config contains all items used as arguments "
539 f"in the constructor to {cls}, "
540 "which is the default behavior. "
541 "You can override this default behavior by defining a "
542 "`from_config(cls, config)` class method to specify "
543 "how to create an "
544 f"instance of {cls.__name__} from its config.\n\n"
545 f"Received config={config}\n\n"
546 f"Error encountered during deserialization: {e}"
547 )
TypeError: Unable to revive model from config. When overriding the `get_config()` method, make sure that the returned config contains all items used as arguments in the constructor to , which is the default behavior. You can override this default behavior by defining a `from_config(cls, config)` class method to specify how to create an instance of ViTClassfier from its config.
Received config={'name': 'vi_t_classfier', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}
Error encountered during deserialization: ViTClassfier.__init__() missing 2 required positional arguments: 'input_shape' and 'n_classes'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
Cell In[15], line 2
1 from keras.models import load_model
----> 2 lm = load_model('ViT_on_100_epochs.keras')
3 cfg2=model.get_config()
4 print(cfg2)
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/saving_api.py:182, in load_model(filepath, custom_objects, compile, safe_mode)
179 is_keras_zip = True
181 if is_keras_zip or is_keras_dir:
--> 182 return saving_lib.load_model(
183 filepath,
184 custom_objects=custom_objects,
185 compile=compile,
186 safe_mode=safe_mode,
187 )
188 if str(filepath).endswith((".h5", ".hdf5")):
189 return legacy_h5_format.load_model_from_hdf5(
190 filepath, custom_objects=custom_objects, compile=compile
191 )
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:237, in load_model(filepath, custom_objects, compile, safe_mode)
232 raise ValueError(
233 "Invalid filename: expected a `.keras` extension. "
234 f"Received: filepath={filepath}"
235 )
236 with open(filepath, "rb") as f:
--> 237 return _load_model_from_fileobj(
238 f, custom_objects, compile, safe_mode
239 )
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:314, in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
311 with zf.open(_CONFIG_FILENAME, "r") as f:
312 config_json = f.read()
--> 314 model = _model_from_config(
315 config_json, custom_objects, compile, safe_mode
316 )
318 all_filenames = zf.namelist()
319 weights_file_path = None
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:303, in _model_from_config(config_json, custom_objects, compile, safe_mode)
301 # Construct the model from the configuration file in the archive.
302 with ObjectSharingScope():
--> 303 model = deserialize_keras_object(
304 config_dict, custom_objects, safe_mode=safe_mode
305 )
306 return model
File /opt/conda/lib/python3.10/site-packages/keras/src/saving/serialization_lib.py:720, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
718 instance = cls.from_config(inner_config)
719 except TypeError as e:
--> 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"
722 " ensure that components that are Python object"
723 " instances (layers, models, etc.) returned by"
724 " `get_config()` are explicitly deserialized in the"
725 " model's `from_config()` method."
726 f"\n\nconfig={config}.\n\nException encountered: {e}"
727 )
728 build_config = config.get("build_config", None)
729 if build_config and not instance.built:
TypeError: could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by `get_config()` are explicitly deserialized in the model's `from_config()` method.
config={'module': None, 'class_name': 'ViTClassfier', 'config': {'name': 'vi_t_classfier', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}, 'registered_name': 'Custom>ViTClassfier', 'build_config': {'input_shape': [None, 128, 128, 3]}, 'compile_config': {'optimizer': {'module': 'keras.optimizers', 'class_name': 'Adam', 'config': {'name': 'adam', 'learning_rate': 0.0010000000474974513, 'weight_decay': None, 'clipnorm': None, 'global_clipnorm': None, 'clipvalue': None, 'use_ema': False, 'ema_momentum': 0.99, 'ema_overwrite_frequency': None, 'loss_scale_factor': None, 'gradient_accumulation_steps': None, 'beta_1': 0.9, 'beta_2': 0.999, 'epsilon': 1e-07, 'amsgrad': False}, 'registered_name': None}, 'loss': {'module': 'keras.losses', 'class_name': 'SparseCategoricalCrossentropy', 'config': {'name': 'sparse_categorical_crossentropy', 'reduction': 'sum_over_batch_size', 'from_logits': True, 'ignore_class': None}, 'registered_name': None}, 'loss_weights': None, 'metrics': ['accuracy', {'module': None, 'class_name': 'BalancedSparseCategoricalAccuracy', 'config': {'name': 'balanced_sparse_categorical_accuracy', 'dtype': 'float32'}, 'registered_name': 'BalancedSparseCategoricalAccuracy'}], 'weighted_metrics': None, 'run_eagerly': False, 'steps_per_execution': 1, 'jit_compile': False}}.
Exception encountered: Unable to revive model from config. When overriding the `get_config()` method, make sure that the returned config contains all items used as arguments in the constructor to , which is the default behavior. You can override this default behavior by defining a `from_config(cls, config)` class method to specify how to create an instance of ViTClassfier from its config.
Received config={'name': 'vi_t_classfier', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}
Error encountered during deserialization: ViTClassfier.__init__() missing 2 required positional arguments: 'input_shape' and 'n_classes'
Может кто-нибудь сказать мне, как мне с этим справиться? Кроме того, похоже, что мне нужно скопировать классы из файла, чтобы обучить модель любому файлу Python, в который я хочу загрузить модель. Есть ли лучший подход к этому?
Вот мой код, я использую для этого Kaggle:
Код: Выделить всё
import tensorflow as tf
import keras
print(keras.__version__)
class AttrDict(dict):
__setattr__ = dict.__setitem__
__getattr__ = dict.__getitem__
def plot_grid_ds(ds, model=None, size=(3, 3), figsize=(10, 10)):
from copy import copy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
n = size[0] * size[1]
ds_ = copy(ds)
fig = plt.figure(figsize=figsize)
grid = ImageGrid(
fig,
111,
nrows_ncols=size,
axes_pad=0.3,
)
for X_batch, y_batch in ds_.unbatch().shuffle(n).batch(n).take(1):
y_hat_batch = model.predict(X_batch).argmax(axis=-1) if model else y_batch
for X, y, y_hat, ax in zip(X_batch, y_batch, y_hat_batch, grid):
title = (
f"{ds.class_names[y]} (true) - {ds.class_names[y_hat]} (pred)"
if model
else f"{ds.class_names[y]}"
)
ax.set_title(title)
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
ax.imshow(X / 255, cmap="gray")
return grid
class BalancedSparseCategoricalAccuracy(tf.keras.metrics.SparseCategoricalAccuracy):
def __init__(self, name="balanced_sparse_categorical_accuracy", dtype=None):
super().__init__(name, dtype=dtype)
def update_state(self, y_true, y_pred, sample_weight=None):
y_flat = y_true
if y_true.shape.ndims == y_pred.shape.ndims:
y_flat = tf.squeeze(y_flat, axis=[-1])
y_true_int = tf.cast(y_flat, tf.int32)
cls_counts = tf.math.bincount(y_true_int)
cls_counts = tf.math.reciprocal_no_nan(tf.cast(cls_counts, self.dtype))
weight = tf.gather(cls_counts, y_true_int)
return super().update_state(y_true, y_pred, sample_weight=weight)
import tensorflow as tf
from tensorflow.keras.utils import register_keras_serializable
class Patches(tf.keras.layers.Layer):
def __init__(self, patch_size, **kwargs):
super(Patches, self).__init__(**kwargs)
self.patch_size = patch_size
def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, self.patch_size, 1],
strides=[1, self.patch_size, self.patch_size, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches
@keras.saving.register_keras_serializable()
class PatchEncoder(tf.keras.layers.Layer):
def __init__(self, num_patches, projection_dim, **kwargs):
super(PatchEncoder, self).__init__(**kwargs)
self.num_patches = num_patches
self.projection = tf.keras.layers.Dense(units=projection_dim)
self.position_embedding = tf.keras.layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)
def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
return encoded
@keras.saving.register_keras_serializable()
class ViTClassfier(tf.keras.models.Model):
CONFIG = {
"patch_size": 4,
"embedding_dim": 32,
"n_heads": 2,
"n_transformers": 1,
"transformer_units": [64, 32],
"transformer_dropout": 0.1,
"mlp_units": [2048, 1024],
"mlp_dropout": 0.1,
#"input_shape": [128, 128, 3],
}
def __init__(self, input_shape, n_classes, config={}, **kwargs):
super(ViTClassfier, self).__init__(**kwargs)
self._config = AttrDict(
**{
**self.CONFIG,
**config,
"input_shape": input_shape,
"n_classes": n_classes,
}
)
self._construct_model()
def call(self, x):
return self._model(x)
def _construct_model(self):
config = self._config
num_patches = (config['input_shape'][0] // config['patch_size']) ** 2
inputs = tf.keras.layers.Input(shape=config['input_shape'])
patches = Patches(config['patch_size'])(inputs)
# Encode patches.
encoded_patches = PatchEncoder(num_patches, config['embedding_dim'])(patches)
# Create multipletf.keras.layers of the Transformer block.
for _ in range(config['n_transformers']):
# Layer normalization 1.
x1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
# Create a multi-head attention layer.
attention_output = tf.keras.layers.MultiHeadAttention(
num_heads=config['n_heads'], key_dim=config['embedding_dim'], dropout=0.1
)(x1, x1)
# Skip connection 1.
x2 = tf.keras.layers.Add()([attention_output, encoded_patches])
# Layer normalization 2.
x3 = tf.keras.layers.LayerNormalization(epsilon=1e-6)(x2)
# MLP.
for units in config['transformer_units']:
x3 = tf.keras.layers.Dense(units, activation=tf.nn.gelu)(x3)
x3 = tf.keras.layers.Dropout(config['transformer_dropout'])(x3)
# Skip connection 2.
encoded_patches = tf.keras.layers.Add()([x3, x2])
# Create a [batch_size, projection_dim] tensor.
features = tf.keras.layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
features = tf.keras.layers.Flatten()(features)
features = tf.keras.layers.Dropout(0.5)(features)
# Add MLP.
for units in config['mlp_units']:
features = tf.keras.layers.Dense(units, activation=tf.nn.gelu)(features)
features = tf.keras.layers.Dropout(config['mlp_dropout'])(features)
# Classify outputs.
logits = tf.keras.layers.Dense(config['n_classes'])(features)
# Create the Keras model.
self._model = tf.keras.models.Model(inputs=inputs, outputs=logits)
from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
path = Path("/kaggle/input/breast-ultrasound-images-dataset/Dataset_BUSI_with_GT")
img_as_jpg = list(path.glob(r"*/*.png"))
# Counts the number of each categories in the dataset
file_count = list(map(lambda x: os.path.split(os.path.split(x)[0])[1],img_as_jpg))
benign = file_count.count('benign')
malignant = file_count.count('malignant')
normal = file_count.count('normal')
fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
labels = ['benign','malignant', 'normal']
count_train = [benign,malignant,normal]
ax.bar(labels, count_train)
plt.show()
train = pd.concat([pd.Series(img_as_jpg,name="image").apply(str),pd.Series(file_count, name="type")],axis=1)
train = train.sample(frac=1).reset_index(drop=True)
print(np.shape(train))
fig, axes = plt.subplots(4, 4, figsize=(8, 8))
fig.tight_layout()
counter = 0
for ax in axes.reshape(-1):
ax.imshow(plt.imread(train["image"][counter]))
ax.set_title(train["type"][counter])
plt.rcParams["axes.grid"] = False
counter = counter+1
hparams = AttrDict(
image_size=(128, 128, 3),
n_classes=3,
batch_size=64,
)
ds = tf.keras.preprocessing.image_dataset_from_directory("/kaggle/input/breast-ultrasound-images-dataset/Dataset_BUSI_with_GT", image_size=hparams.image_size[:-1], batch_size=hparams.batch_size)
train_split = round(0.8 * ds.cardinality().numpy())
train_ds = ds.take(train_split); train_ds.class_names = ds.class_names
validation_ds = ds.skip(train_split); validation_ds.class_names = ds.class_names
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = ViTClassfier(hparams.image_size, hparams.n_classes)
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy", BalancedSparseCategoricalAccuracy()]
)
import time
start = time.time()
model.fit(train_ds, epochs=100, validation_data=validation_ds)
end = time.time()
import numpy as np
import seaborn as sns
from sklearn.metrics import confusion_matrix
X, y_true = list(zip(*[(X, y) for (X, y) in validation_ds.unbatch().as_numpy_iterator()]))
y_hat = model.predict_on_batch(np.array(X)).argmax(axis=-1)
mat = confusion_matrix(y_true, y_hat)
mat
from sklearn.metrics import balanced_accuracy_score
acc = balanced_accuracy_score(y_true, y_hat)
print(f"Final accuracy on validation set : {acc:.3f}")
model.save("ViT_on_100_epochs.keras")
cfg=model.get_config()
print(cfg)
from keras.models import load_model
lm = load_model('ViT_on_100_epochs.keras')
cfg2=model.get_config()
print(cfg2)
Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/790 ... -vit-model