Bha*_*ath 7 python keras tensorflow
我正在狗与猫数据集上构建 Keras 深度学习算法。我可以在 colab 中运行我的代码。但在 Jupyter 实验室中我收到此错误。
The following argument(s) are not supported with the native Keras format: ['options']
Run Code Online (Sandbox Code Playgroud)
下面是代码:
import os
import shutil
import pathlib
original_dir = pathlib.Path("/content/drive/MyDrive/Reva/dogs_vs_cats/train/train")
new_base_dir = pathlib.Path("/content/drive/MyDrive/Reva/dogs_vs_cats/")
def make_subset(subset_name, start_index, end_index):
for category in ("cat", "dog"):
dir = new_base_dir / subset_name / category
# Check if the folder exists and delete it if it does
if os.path.exists(dir):
shutil.rmtree(dir)
# Create the folder again
os.makedirs(dir)
fnames = [f"{category}.{i}.jpg" for i in range(start_index, end_index)]
for fname in fnames:
shutil.copyfile(src=original_dir / fname,
dst=dir / fname)
make_subset("train", start_index=0, end_index=1000)
make_subset("validation", start_index=1000, end_index=1500)
make_subset("test", start_index=1500, end_index=2500)
from tensorflow.keras.utils import image_dataset_from_directory
train_dataset = image_dataset_from_directory(
new_base_dir / "train",
image_size=(180, 180),
batch_size=32)
validation_dataset = image_dataset_from_directory(
new_base_dir / "validation",
image_size=(180, 180),
batch_size=32)
test_dataset = image_dataset_from_directory(
new_base_dir / "test",
image_size=(180, 180),
batch_size=32)
from tensorflow import keras
from tensorflow.keras import layers
inputs = keras.Input(shape=(180, 180, 3))
x = layers.Rescaling(1./255)(inputs)
x = layers.Conv2D(filters=32, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=64, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.Flatten()(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(loss="binary_crossentropy",
optimizer="rmsprop",
metrics=["accuracy"])
callbacks = [
keras.callbacks.ModelCheckpoint(
filepath="convnet_from_scratch.keras",
save_best_only=True,
monitor="val_loss")
]
history = model.fit(
train_dataset,
epochs=30,
validation_data=validation_dataset,
callbacks=callbacks)
Run Code Online (Sandbox Code Playgroud)
我需要知道如何解决上面的代码。也欢迎任何改进运行代码所需时间的建议。
小智 10
正如我在评论中提到的,似乎存在与 keras 保存以及 TF/Keras 版本控制相关的奇怪行为。我可以在 colab 上使用版本 2.13(目前最新)运行 TF/Keras 时复制您的错误。colab 上的标准安装是 2.12,不会出现错误。
因此一种解决方案是将 TF/Keras 降级到 2.12.x,或者更改
keras.callbacks.ModelCheckpoint(
filepath="convnet_from_scratch.keras",
..)
Run Code Online (Sandbox Code Playgroud)
到
keras.callbacks.ModelCheckpoint(
filepath="convnet_from_scratch.x",
..)
Run Code Online (Sandbox Code Playgroud)
其中 x 代表您喜欢的任何内容(不是“keras”),不以格式保存.keras。
| 归档时间: |
|
| 查看次数: |
4404 次 |
| 最近记录: |