属性错误:模块“keras.api._v2.keras.callbacks”没有属性“Tensorboard”

quhf5bfb  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(155)

我写这段代码是为了在tensorflow hub上使用其他模型,如下所示。我有个问题上面说

AttributeError: module 'keras.api._v2.keras.callbacks' has no attribute 'Tensorboard'

字符串
我在谷歌上搜索了一下,但什么也没找到。下面是我的代码:

# %%
import zipfile

!wget https://storage.googleapis.com/ztm_tf_course/food_vision/10_food_classes_10_percent.zip

zip_ref = zipfile.ZipFile("10_food_classes_10_percent.zip")
zip_ref.extractall()
zip_ref.close()

# %%
import os

for dirpath, dirnames, filenames in os.walk("10_food_classes_10_percent"):
    print(f"there are {len(dirnames)} directories and {len(filenames)} images in {dirpath}")

# %%
from keras.preprocessing.image import ImageDataGenerator

IMAGE_SHAPE = (224, 224)
BATCH_SIZE = 32

train_dir = "10_food_classes_10_percent/train/"
test_dir = "10_food_classes_10_percent/test/"

train_datagen = ImageDataGenerator(rescale=1/255.0)
test_datagen = ImageDataGenerator(rescale=1/255.0)

print("training images: ")
train_data_10_percent = train_datagen.flow_from_directory(train_dir, 
                                                          target_size=IMAGE_SHAPE, 
                                                          batch_size=BATCH_SIZE, 
                                                          class_mode="categorical")
print("testing images: ")
test_data = train_datagen.flow_from_directory(test_dir, 
                                                target_size=IMAGE_SHAPE, 
                                                batch_size=BATCH_SIZE, 
                                                class_mode="categorical")

# %%
import keras
import sklearn
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt

# %%
import datetime

def create_tensorboard_callback(dir_name, experimment_name):
    log_dir = dir_name + "/" + experimment_name + "/" + datetime.datetime.now().strftime("%m%d%Y-%H%M%S")
    tensorboard_callback = tf.keras.callbacks.Tensorboard(log_dir=log_dir)
    print(f"saving tensorboard log to {log_dir}")
    return tensorboard_callback
tf.keras.callbacks.TensorBoard

# %%
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4"

efficientnet_url = "https://tfhub.dev/tensorflow/efficientnet/b0/feature_vector/1"

# %%
import tensorflow_hub as hub

# %%
def create_model(model_url, num_classes=10):
    feature_extraction_layer = hub.KerasLayer(model_url,
                                             trainable=False,
                                             name="feature_extraction_layer",
                                             input_shape=IMAGE_SHAPE+(3,))
    
    model = tf.keras.Sequential([
        feature_extraction_layer,
        tf.keras.layers.Dense(num_classes, activation="softmax", name="output_layer")
    ])
    
    return model

# %%
resnet_model = create_model(resnet_url,
                            num_classes=train_data_10_percent.num_classes)

# %%
resnet_model.summary()

# %%
resnet_model.compile(loss="categorical_crossentropy",
                     optimizer=tf.keras.optimizers.Adam(),
                     metrics=["accuracy"])

# %%
resnet_history = resnet_model.fit(train_data_10_percent,
                                  epochs=5,
                                  steps_per_epoch=len(train_data_10_percent),
                                  validation_data=test_data,
                                  callbacks=[create_tensorboard_callback(dir_name="tensorflow_hub",
                                                                         experimment_name="resnet50v")])


最初,我的代码有keras.callbacks.Tensorboard,后来我发现较新的tensorflow版本使用tf.keras,因此是tf.keras.callbacks.Tensorboard。它没有修复它,所以我在网上搜索,什么也没找到。我想也许这是一个愚蠢的错误,并将其从Tensorboard更改为tensorboard,但它有相同的结果。

goqiplq2

goqiplq21#

我想出来了这确实是一个愚蠢的错误,并不是因为混淆了输入。在仔细检查了文档之后,我发现它的camel大小写是TensorBoard,而不是Tensorboard

oogrdqng

oogrdqng2#

这可能是因为你在导入中混合了tensorflow和keras。

相关问题