我正在尝试对图像进行分类,无论它们是猫、狗还是熊猫。数据包含所有图像(猫+狗+熊猫),标签包含它们的标签,但不知何故,当我将数据拟合到模型时, 和val_loss
没有val_accuracy
显示,每个时期中显示的唯一指标是loss
和accuracy
。我不知道为什么它没有出现,但我感觉这是因为我没有通过,validation_data
所以我通过X_test.all()
了,validation_data
但仍然没有出现,我该怎么办?val_loss
val_accuracy
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (2,2), activation = 'relu', input_shape= (height, width, n_channels)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(256,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation= 'relu'),
tf.keras.layers.Dense(3, activation= 'softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
y_train = np_utils.to_categorical(y_train, 3)
model.fit(X_train, y_train, batch_size=32, epochs=25, verbose=1)
Run Code Online (Sandbox Code Playgroud) 晚上好,
我想使用 tf2 和 Gradient Tape 函数实现一个简单回归问题的玩具示例。使用 Model.fit 它可以正确学习,但使用 GradientTape 也可以做一些事情,但与 model.fit() 相比,损失不会移动。这是我的示例代码和结果。我找不到问题所在。
model_opt = tf.keras.optimizers.Adam()
loss_fn = tf.keras.losses.MeanSquaredError()
with tf.GradientTape() as tape:
y = model(X, training=True)
loss_value = loss_fn(y_true, y)
grads = tape.gradient(loss_value, model.trainable_variables)
model_opt.apply_gradients(zip(grads, model.trainable_variables))
#Results:
42.47433806265809
42.63973672226078
36.687397360178586
38.744844324717526
36.59080452300609
...
Run Code Online (Sandbox Code Playgroud)
这里是 model.fit() 的常规情况
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.MSE,metrics="mse")
...
model.fit(X,y_true,verbose=0)
#Results
[40.97759069299212]
[28.04145720307729]
[17.643483147375473]
[7.575242056454791]
[5.83682193867299]
Run Code Online (Sandbox Code Playgroud)
准确率应该大致相同,但看起来它根本没有学习。输入 X 是张量,y_true 也是。
编辑以进行测试
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf …
Run Code Online (Sandbox Code Playgroud) machine-learning deep-learning tensorflow tensorflow2.0 gradienttape
我正在使用预先训练的 Alex 模型。我正在一些随机图像数据集上运行这个模型。我想在训练之前将 RGB 图像转换为 YCbCr 图像。
我想知道是否可以自己添加一个功能transform.compose
,例如:
transform = transforms.Compose([
ycbcr(), #something like this
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
Run Code Online (Sandbox Code Playgroud)
在哪里,
def ycbcr(img):
img = cv2.imread(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2ycbcr)
t = torch.from_numpy(img)
return t
training_dataset = datasets.ImageFolder(link_train ,transform = transform_train)
training_loader = torch.utils.data.DataLoader(training_dataset, batch_size=96, shuffle=True)
Run Code Online (Sandbox Code Playgroud)
这个过程正确吗?请帮助我如何继续?
machine-learning neural-network python-3.x deep-learning pytorch
我目前正在训练 CNN 来检测一个人是否戴口罩。不幸的是,我不明白为什么我的验证损失如此之高。正如我注意到的,我正在验证的数据是在类之后排序的(这是网络的输出)。这对我的验证准确性和损失有影响吗?我使用计算机视觉测试了该模型,效果非常好,但验证损失和准确性看起来仍然非常错误。其原因何在?
validation machine-learning deep-learning conv-neural-network tensorflow
如果损失波动,最终的训练步骤可能不会具有最低的损失。
我想知道,如果在 Keras 中训练模型进行验证,Keras 如何选择最终模型?
做
或者
谢谢。
我对如何在 PyTorch 中复制 Keras (TensorFlow) 卷积感到困惑。
在 Keras 中,我可以做这样的事情。(输入大小为(256, 237, 1, 21)
,输出大小为(256, 237, 1, 1024)
.
import tensorflow as tf
x = tf.random.normal((256,237,1,21))
y = tf.keras.layers.Conv1D(filters=1024, kernel_size=5,padding="same")(x)
print(y.shape)
(256, 237, 1, 1024)
Run Code Online (Sandbox Code Playgroud)
然而,在 PyTorch 中,当我尝试做同样的事情时,我得到了不同的输出大小:
import torch.nn as nn
x = torch.randn(256,237,1,21)
m = nn.Conv1d(in_channels=237, out_channels=1024, kernel_size=(1,5))
y = m(x)
print(y.shape)
torch.Size([256, 1024, 1, 17])
Run Code Online (Sandbox Code Playgroud)
我希望 PyTorch 提供与 Keras 相同的输出大小:
上一个问题似乎暗示 Keras 过滤器是 PyTorch 的out_channels
,但这就是我所拥有的。我尝试在 PyTorch 中添加填充padding=(0,503)
,但这给了我torch.Size([256, 1024, 1, 1023]) …
这是来自图像的注释文件:
0 0.6142131979695431 0.336 0.467005076142132 0.392
Run Code Online (Sandbox Code Playgroud)
第一个0
是类标签。0.6142131979695431
和0.336
是边界框的 x 和 y 坐标。0.467005076142132
和0.392
是边界框的宽度和高度。但是,我不明白的是为什么 x、y、宽度和高度都在 [0,1] 浮点区间内。有人告诉我这是一个百分比,但是相对于什么的百分比呢?
例如,我正在编写构建合成数据集的软件。这是我制作的一张训练图像。它在我想要检测的对象周围有边界框。
边框完美地包裹了 Wizards 和 Ubuntu 徽标。那么,我该如何像上面的格式一样注释它们呢?
我正在处理一个巨大的文本数据集以进行内容分类。我已经实现了 distilbert 模型和 distilberttokenizer.from_pretrained() tokenizer。这个 tokenizer 花费了非常长的时间来对我的文本数据进行 tokenizer 大约 7 分钟,只有 14k 记录,这是因为它在我的 CPU 上运行。
有什么方法可以强制标记器在我的 GPU 上运行。
nlp deep-learning huggingface-transformers huggingface-tokenizers
我正在尝试改变眼睛和嘴唇的形状和位置,为此我使用tenserflow.js面部地标检测模型。我得到了所有 468 个面部标志,但问题是我不知道,而且我从未在互联网上找到任何解决方案如何获取仅需要的标志(例如嘴唇和眼睛)的位置。我得到了所有的要点,但想在嘴唇和眼睛上应用更改,我如何仅在这些地标上应用更改。
output_1, output_2 = model(x)
loss = cross_entropy_loss(output_1, target_1)
loss.backward()
optimizer.step()
loss = cross_entropy_loss(output_2, target_2)
loss.backward()
optimizer.step()
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这段代码时,出现以下错误:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 4]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
Run Code Online (Sandbox Code Playgroud)
然后,我真的想知道我应该做什么来训练具有 2 个或更多输出的模型
python machine-learning neural-network deep-learning pytorch
deep-learning ×10
tensorflow ×5
python ×4
keras ×3
pytorch ×3
validation ×2
gradienttape ×1
nlp ×1
python-3.x ×1
yolo ×1