大纲
我试图通过基于与图像相关联的一些“质心”数据(每个时间步的峰值)生成多项式来扭曲图像(时间序列中的光谱峰值,但这并不重要)和增加多项式。这些原始多项式和增广多项式分别构成了我的“源”点和“目标”点,我试图使用 skimage.transform.warp() 来扭曲图像。
这种扭曲的目标是产生两个扭曲的图像(即重复该过程两次)。然后这些图像将彼此正相关,或者如果两个扭曲图像之一水平翻转(同样,这里不那么重要)则负相关。
(请注意,多项式增强是通过在每个多项式峰/谷处添加/减去噪声来执行的,与每个点的幅度(像素)成正比,然后通过这些增强点生成相同阶数的新多项式,在防止增广多项式取反的地方)。
代码片段
我通过创建 GeometricTransform 并将其作为 inverse_map 应用到 warp() 来在代码中实现这一点,如下所示:
from skimage import transform
# Create the transformation object using the source and destination (N, 2) arrays in reverse order
# (as there is no explicit way to do an inverse polynomial transformation).
t = transform.estimate_transform('polynomial', src=destination, dst=source, order=4) # order = num_poly_degrees - 1
# Warp the original image using the transformation object
warped_image = transform.warp(image, t, order=0, mode='constant', cval=float('nan')) …Run Code Online (Sandbox Code Playgroud) 我已经训练了我的网络并生成了一些训练/验证损失,我通过以下代码示例保存了这些损失(仅训练损失示例,验证完全等效):
valid_summary_writer = tf.summary.create_file_writer("/path/to/logs/")
with train_summary_writer.as_default():
tf.summary.scalar('Training Loss', data=epoch_loss, step=current_step)
Run Code Online (Sandbox Code Playgroud)
训练后,我想使用 Tensorboard 查看损失曲线。然而,因为我将损失曲线保存在“训练损失”和“验证损失”的名称下,这些曲线绘制在单独的图表上。我知道我应该将名称更改为简单的“丢失”以解决此问题,以便将来写入日志目录。但是我如何编辑我现有的训练/验证损失日志文件来解决这个问题?
我试图修改以下帖子的解决方案:https : //stackoverflow.com/a/55061404,它编辑日志文件的步骤并重新写入文件;我的版本涉及更改文件中的标签。但我在这方面没有成功。它还需要通过“tf.compat.v1”导入旧的 Tensorflow 代码。有没有办法实现这一点(可能在 TF 2.X 中)?
我曾想简单地从每个包含损失的日志目录中获取损失和步长值,并通过我以前的工作方法将它们写入新的日志文件,但我只设法获得了步长,而不是损失值本身。有没有人在这里取得成功?
---=== 编辑 ===---
我设法使用来自@jhedesa 的代码解决了这个问题
当我在 Google Colab Notebook 中协同使用 Tensorflow 时,我不得不稍微改变函数“rename_events_dir”的调用方式。为此,我更改了代码的最后部分,内容如下:
if __name__ == '__main__':
if len(sys.argv) != 5:
print(f'{sys.argv[0]} <input dir> <output dir> <old tags> <new tag>',
file=sys.stderr)
sys.exit(1)
input_dir, output_dir, old_tags, new_tag = sys.argv[1:]
old_tags = old_tags.split(';')
rename_events_dir(input_dir, output_dir, old_tags, new_tag)
print('Done')
Run Code Online (Sandbox Code Playgroud)
要阅读此内容:
rootpath = '/path/to/model/'
dirlist = [dirname for dirname in os.listdir(rootpath) …Run Code Online (Sandbox Code Playgroud) 正如标题所示,我正在尝试基于 SimCLR 框架训练模型(见本文:https ://arxiv.org/pdf/2002.05709.pdf - NT_Xent 损失在等式(1)和算法 1 中说明) )。
我设法创建了损失函数的 numpy 版本,但这不适合训练模型,因为 numpy 数组无法存储反向传播所需的信息。我很难将我的 numpy 代码转换为 Tensorflow。这是我的 numpy 版本:
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# Define the contrastive loss function, NT_Xent
def NT_Xent(zi, zj, tau=1):
""" Calculates the contrastive loss of the input data using NT_Xent. The
equation can be found in the paper: https://arxiv.org/pdf/2002.05709.pdf
Args:
zi: One half of the input data, shape = (batch_size, feature_1, feature_2, ..., feature_N)
zj: Other half of the …Run Code Online (Sandbox Code Playgroud) python backpropagation cosine-similarity scikit-learn tensorflow