标签: tensorflow

AttributeError:模块“tensorflow_core.keras.utils”没有属性“register_keras_serializable”

当我尝试训练对象检测模型时,出现以下错误:

    Traceback (most recent call last):
  File "train.py", line 53, in <module>
    from object_detection.builders import model_builder
  File "C:\Users\hp\models\research\object_detection\builders\model_builder.py", line 63, in <module>
    from object_detection.models import ssd_efficientnet_bifpn_feature_extractor as ssd_efficientnet_bifpn
  File "C:\Users\hp\models\research\object_detection\models\ssd_efficientnet_bifpn_feature_extractor.py", line 33, in <module>
    from official.vision.image_classification.efficientnet import efficientnet_model
  File "C:\Users\hp\models\official\vision\image_classification\efficientnet\efficientnet_model.py", line 35, in <module>
    from official.modeling import tf_utils
  File "C:\Users\hp\models\official\modeling\tf_utils.py", line 25, in <module>
    from official.modeling import activations
  File "C:\Users\hp\models\official\modeling\activations\__init__.py", line 16, in <module>
    from official.modeling.activations.gelu import gelu
  File "C:\Users\hp\models\official\modeling\activations\gelu.py", line 26, in <module>
    @tf.keras.utils.register_keras_serializable(package='Text')
AttributeError: module 'tensorflow_core.keras.utils' has no …
Run Code Online (Sandbox Code Playgroud)

python object-detection keras tensorflow object-detection-api

0
推荐指数
1
解决办法
9750
查看次数

加载图像数据集

我正在尝试从包含超过 10M 个图像和 10K 个类的特定目录加载数据,但问题是我没有为所有类提供不同的目录,所有图像都只在一个目录中。我有一个包含 id 和标签的 CSV 文件标签。我正在尝试使用该VGG16模型。

CSV:
id,lable
abf20a,CAR
dsf8sd,BIKE
Run Code Online (Sandbox Code Playgroud)

这里 abf20a 是图像名称"abf20a.jpg"

所以请帮助我如何将图像和标签一起加载并使用VGG16

谢谢

维沙尔

python image-manipulation tensorflow jupyter-notebook google-colaboratory

0
推荐指数
1
解决办法
4762
查看次数

更新 TensorFlow 自定义指标的内部状态(也称为在指标计算中使用非 update_state 变量)

版本:python 3.8.2(我也尝试过3.6.8,但我认为python版本在这里并不重要),tensorflow 2.3.0,numpy 1.18.5

我正在使用稀疏标签张量训练一个用于分类问题的模型。我将如何定义一个指标来计算“0”标签在此之前出现的次数?我在下面的代码示例中尝试做的是将指标在数组中看到的所有标签存储起来,并在y_true每次update_state调用时不断地将现有数组与新数组连接起来。(我知道我可以只存储一个count变量并使用+=,但在实际使用场景中,连接是理想的并且内存不是问题。)以下是重现问题的最少代码:

import tensorflow as tf

class ZeroLabels(tf.keras.metrics.Metric):
    """Accumulates a list of all y_true sparse categorical labels (ints) and calculates the number of times the '0' label has appeared."""
    def __init__(self, *args, **kwargs):
        super(ZeroLabels, self).__init__(name="ZeroLabels")
        self.labels = self.add_weight(name="labels", shape=(), initializer="zeros", dtype=tf.int32)

    def update_state(self, y_true, y_pred, sample_weight=None):
        """I'm using sparse categorical crossentropy, so labels are 1D array of integers."""
        if self.labels.shape == (): # if this is the first time …
Run Code Online (Sandbox Code Playgroud)

python metrics graph tensorflow

0
推荐指数
1
解决办法
1294
查看次数

Keras:是否有 train_on_batch 的示例代码,其中包含历史记录+进度?

在 Python 中,我从 Keras 转向Model.fit循环Model.train_on_batch以实现更精细的控制。但返回的进度条和History对象fit很有用。在浪费时间从头开始实现它们之前,我想知道是否有人找到了使用train_on_batch重现进度条和历史记录的示例代码?

(注意。我查看了 的源代码fit,但是有足够多的间接层,因此很难准确地挖掘出它在做什么。还发现了this,这很有帮助,但没有相关功能。)

python keras tensorflow

0
推荐指数
1
解决办法
1775
查看次数

keras model.predict 输出什么?

我已经构建了一个 LSTM 模型(见下文)并对其进行了训练。当我进行二元分类时,我的损失函数是二元交叉熵。训练 y 数据是一组 0 和 1。

当我运行时,model.predict(x_test_scaled)我得到一组值范围在 0 和 1 之间的单个系列。我猜这是一个概率,但它是输出 = 0 的概率还是输出 = 1 的概率?

model = Sequential()

model.add(LSTM(units=512, input_shape = (X_train.shape[1],X_train.shape[2]), return_sequences=True)) 
model.add(Dropout(0.3))

model.add(LSTM(units=512, return_sequences=False))
model.add(Dropout(0.3))
    

model.add(Dense(264),  activation = 'tanh')

model.add(Dense(1))
Run Code Online (Sandbox Code Playgroud)

python sequential keras tensorflow

0
推荐指数
1
解决办法
1万
查看次数

Keras 模型的混淆矩阵:一个问题,也许有人知道如何为该模型创建混淆矩阵?

我目前正在研究分类问题,但我不知道如何为这个模型制作混淆矩阵,我将其发送到下面的代码。我在协作中使用 Keras 库,因为我的本地环境与 tf.keras.preprocessing.image_dataset_from_directory 不兼容,我获得了很好的准确率和良好的预测,但在做矩阵时我迷失了。预先感谢社区

image_size = (180, 180)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/gdrive/MyDrive/Collab Notebooks/LungCells/seg_train",
    validation_split=0.2,
    subset="training",
    seed=42,
    image_size=image_size,
    batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/gdrive/MyDrive/Collab Notebooks/LungCells/seg_train",
    validation_split=0.2,
    subset="validation",
    #seed=1337,
    seed=42,
    image_size=image_size,
    batch_size=batch_size,
)

data_augmentation = keras.Sequential(
    [
        layers.experimental.preprocessing.RandomFlip("horizontal"),
        layers.experimental.preprocessing.RandomRotation(0.1),
    ]
)

def make_model(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)
    # Image augmentation block
    x = data_augmentation(inputs)

    # Entry block
    x = layers.experimental.preprocessing.Rescaling(1.0 / 255)(x)
    x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.Conv2D(64, 3, …
Run Code Online (Sandbox Code Playgroud)

python matrix confusion-matrix keras tensorflow

0
推荐指数
1
解决办法
5032
查看次数

如何在张量流中将多个 2dim 张量垂直连接成一个?

我想知道我们应该如何将多个不同形状的张量连接成 keras 中的一个张量。我尝试tf.keras.layers.concatenate如下:

import tensorflow as tf
from tf.keras.layers import concatenate

print(tensor_1.shape)
print(tensor_2.shape)

new_tensor = concatenate([tensor_1, tensor_2],axis=1)
new_tensor
Run Code Online (Sandbox Code Playgroud)

但我得到以下值错误:

shape of tensor_1 (?, 30, 30, 128)
shape of tensor_2 (?, 26, 26, 128)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-96d471a8e99e> in <module>()
----> 1 concatenate([tensor_1, tensor_2], axis=1)

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    517             shape[axis] for shape in shape_set if shape[axis] is not None)
    518         if len(unique_dims) > 1:
--> 519           raise ValueError(err_msg)
    520 
    521   def _merge_function(self, …
Run Code Online (Sandbox Code Playgroud)

python tensorflow

0
推荐指数
1
解决办法
1815
查看次数

有没有办法构建一个以指定角度随机旋转的keras预处理层?

我正在从事一个天文图像分类项目,目前正在使用 keras 构建 CNN。

我正在尝试构建一个预处理管道,以使用 keras/tensorflow 层来增强我的数据集。为了简单起见,我想实现二面体组的随机变换(即,对于方形图像,90 度旋转和翻转),但似乎tf.keras.preprocessing.image.random_rotation只允许在遵循均匀分布的连续选择范围。

我想知道是否有一种方法可以从指定度数列表中进行选择,在我的例子中是 [0, 90, 180, 270]。

keras tensorflow keras-layer image-preprocessing image-augmentation

0
推荐指数
1
解决办法
972
查看次数

Tensorflow分类编码中额外列的逻辑是什么?

我正在遵循官方的 Tensorflow预处理层教程,我不确定我是否明白为什么在分类编码后最终会得到这些额外的列。[ 2024 年更新:默认行为现已更改,因此当前教程不再给出我在下面显示的确切结果]

这是一个精简的最小可重现示例(包括数据):

import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
import pathlib

dataset_url = 'http://storage.googleapis.com/download.tensorflow.org/data/petfinder-mini.zip'
csv_file = 'datasets/petfinder-mini/petfinder-mini.csv'
tf.keras.utils.get_file('petfinder_mini.zip', dataset_url, extract=True, cache_dir='.')
df = pd.read_csv(csv_file)

# In the original dataset "4" indicates the pet was not adopted.
df['target'] = np.where(df['AdoptionSpeed']==4, 0, 1)
# Drop un-used columns.
df = df.drop(columns=['AdoptionSpeed', 'Description'])

# A utility method to create a tf.data …
Run Code Online (Sandbox Code Playgroud)

keras tensorflow

0
推荐指数
1
解决办法
744
查看次数

即使清除并删除文件后也无法卸载 cuda

我正在一台计算机上工作,Nvidia 驱动程序和 Cuda 是由其他人安装的,所以我不知道他们安装它们的方法。其中/usr/local/有两个目录cudacuda.10.0。运行nvidia-smi会输出:

CUDA版本:11.0

这让我相信系统上安装了两个 cuda 版本,这导致了一些错误。

在这个问题之后,我首先删除了 cuda:

sudo apt-get --purge remove "*cublas*" "cuda*" "nsight*" 
Run Code Online (Sandbox Code Playgroud)

然后做

sudo rm -rf /usr/local/cuda* 
Run Code Online (Sandbox Code Playgroud)

(我没有卸载 nvidia-drivers 并且Driver Version: 450.80.02已安装)。运行nvidia-smi仍然输出:

CUDA版本:11.0

如何卸载cuda 11?我更喜欢 cuda 10,但找不到 cuda 11 的安装位置。

我还需要卸载 nvidia 驱动程序吗?

ubuntu cuda nvidia tensorflow

0
推荐指数
1
解决办法
5061
查看次数