- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
到目前为止,我一直在尝试使用 Tensorflow 和 Keras。我从 image_ocr.py 中获取了一个代码这使我能够训练打印文本 ocr。我想看到训练的进展,并成功地可视化了训练模型的准确性和损失。然而,据我所知,OCR RNN 并没有将准确性作为验证,而是使用平均编辑距离来验证单词的准确性。在这种情况下,我一直在尝试从类 VizCallback/强>.我试过这个 link 中的方法但它仍然不起作用。谁能帮我可视化平均编辑距离变量?以下是我的代码中的代码片段:
class VizCallback(keras.callbacks.Callback):
def __init__(self, run_name, test_func, text_img_gen, num_display_words=6):
self.test_func = test_func
self.output_dir = os.path.join(
OUTPUT_DIR, run_name)
self.text_img_gen = text_img_gen
self.num_display_words = num_display_words
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def on_train_begin(self, logs={}):
self.med = []
self.nmed = []
def show_edit_distance(self, num, logs={}):
num_left = num
mean_norm_ed = 0.0
mean_ed = 0.0
while num_left > 0:
word_batch = next(self.text_img_gen)[0]
num_proc = min(word_batch['the_input'].shape[0], num_left)
decoded_res = decode_batch(self.test_func, word_batch['the_input'][0:num_proc])
for j in range(num_proc):
edit_dist = editdistance.eval(decoded_res[j], word_batch['source_str'][j])
mean_ed += float(edit_dist)
mean_norm_ed += float(edit_dist) / len(word_batch['source_str'][j])
num_left -= num_proc
mean_norm_ed = mean_norm_ed / num
mean_ed = mean_ed / num
#Create scalar summaries for both mean edit distance and normalized mean edit distance
tf_med_ph = tf.placeholder(tf.float32,shape=None,name='med_summary')
tf_nmed_ph = tf.placeholder(tf.float32,shape=None,name='nmed_summary')
tf_med = tf.summary.scalar('med', tf_med_ph)
tf_nmed = tf.summary.scalar('nmed', tf_nmed_ph)
performance_summaries = tf.summary.merge([tf_med,tf_nmed])
#Create a session for displaying the summary
config = tf.ConfigProto(allow_soft_placement=True)
session = tf.InteractiveSession(config=config)
summ_writer = tf.summary.FileWriter(os.path.join('summaries','first'), session.graph)
# Execute the summaries defined above
summ = session.run(performance_summaries, feed_dict={tf_med_ph:mean_ed, tf_nmed_ph:mean_norm_ed})
# Write the obtained summaries to the file, so it can be displayed in the TensorBoard
summ_writer.add_summary(summ, epoch)
session.close()
print('\nOut of %d samples: Mean edit distance: %.3f Mean normalized edit distance: %0.3f'
% (num, mean_ed, mean_norm_ed))
def on_epoch_end(self, epoch, logs={}):
self.model.save_weights(os.path.join(self.output_dir, 'weights%02d.h5' % (epoch)))
self.show_edit_distance(256)
word_batch = next(self.text_img_gen)[0]
res = decode_batch(self.test_func, word_batch['the_input'][0:self.num_display_words])
if word_batch['the_input'][0].shape[0] < 256:
cols = 2
else:
cols = 1
for i in range(self.num_display_words):
plt.subplot(self.num_display_words // cols, cols, i + 1)
if K.image_data_format() == 'channels_first':
the_input = word_batch['the_input'][i, 0, :, :]
else:
the_input = word_batch['the_input'][i, :, :, 0]
plt.imshow(the_input.T, cmap='Greys_r')
plt.xlabel('Truth = \'%s\'\nDecoded = \'%s\'' % (word_batch['source_str'][i], res[i]))
fig = plt.gcf()
fig.set_size_inches(10, 13)
plt.savefig(os.path.join(self.output_dir, 'e%02d.png' % (epoch)))
plt.close()
def train(run_name, start_epoch, stop_epoch, img_w):
# Input Parameters
img_h = 64
words_per_epoch = 16000
val_split = 0.2
val_words = int(words_per_epoch * (val_split))
# Network parameters
conv_filters = 16
kernel_size = (3, 3)
pool_size = 2
time_dense_size = 32
rnn_size = 512
minibatch_size = 32
if K.image_data_format() == 'channels_first':
input_shape = (1, img_w, img_h)
else:
input_shape = (img_w, img_h, 1)
fdir = os.path.dirname(get_file('wordlists.tgz',
origin='http://test.com/wordlist.tgz', untar=True))
img_gen = TextImageGenerator(monogram_file=os.path.join(fdir, 'wordlist_mono_clean.txt'),
bigram_file=os.path.join(fdir, 'wordlist_bi_clean.txt'),
minibatch_size=minibatch_size,
img_w=img_w,
img_h=img_h,
downsample_factor=(pool_size ** 2),
val_split=words_per_epoch - val_words
)
act = 'relu'
input_data = Input(name='the_input', shape=input_shape, dtype='float32')
inner = Conv2D(conv_filters, kernel_size, padding='same',
activation=act, kernel_initializer='he_normal',
name='conv1')(input_data)
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner)
inner = Conv2D(conv_filters, kernel_size, padding='same',
activation=act, kernel_initializer='he_normal',
name='conv2')(inner)
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner)
conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters)
inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)
# cuts down input size going into RNN:
inner = Dense(time_dense_size, activation=act, name='dense1')(inner)
# Two layers of bidirectional GRUs
# GRU seems to work as well, if not better than LSTM:
gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner)
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner)
gru1_merged = add([gru_1, gru_1b])
gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)
# transforms RNN output to character activations:
inner = Dense(img_gen.get_output_size(), kernel_initializer='he_normal',
name='dense2')(concatenate([gru_2, gru_2b]))
y_pred = Activation('softmax', name='softmax')(inner)
Model(inputs=input_data, outputs=y_pred).summary()
labels = Input(name='the_labels', shape=[img_gen.absolute_max_string_len], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])
# clipnorm seems to speeds up convergence
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
#Make tensorboard instance
init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
tbname="tensorboard-of-{}".format(int(time.time()))
tensorboard = keras.callbacks.TensorBoard(
log_dir="logs/{}".format(tbname),
histogram_freq=0,
write_images=True)
# the loss calc occurs elsewhere, so use a dummy lambda func for the loss
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd,
metrics=['accuracy'])
if start_epoch > 0:
weight_file = os.path.join(OUTPUT_DIR, os.path.join(run_name, 'weights%02d.h5' % (start_epoch - 1)))
model.load_weights(weight_file)
# captures output of softmax so we can decode the output during visualization
test_func = K.function([input_data], [y_pred])
viz_cb = VizCallback(run_name, test_func, img_gen.next_val())
model.fit_generator(generator=img_gen.next_train(),
steps_per_epoch=(words_per_epoch - val_words) // minibatch_size,
epochs=stop_epoch,
validation_data=img_gen.next_val(),
validation_steps=val_words // minibatch_size,
callbacks=[tensorboard,viz_cb, img_gen],
initial_epoch=start_epoch)
如有任何帮助,我们将不胜感激。谢谢!
附言我使用的是 Tensorflow 1.9.0 和 Python 3.6.8
更新现在只需将变量 performance_summaries 从 VizCallbak 类传递到 train 函数中的指标即可。有什么帮助吗?
最佳答案
您可以修改 show_edit_distance
以在每次调用此函数时添加摘要:
def show_edit_distance(self, num, epoch):
...
summary = tf.Summary()
summary.value.add(tag='mean_ed', simple_value=mean_ed)
summ_writer.add_summary(summary, epoch)
summary = tf.Summary()
summary.value.add(tag='mean_norm_ed', simple_value=mean_norm_ed)
summ_writer.add_summary(summary, epoch)
...
请注意,您将需要一个额外的参数 epoch
:
def on_epoch_end(self, epoch, logs={}):
...
self.show_edit_distance(256, epoch)
...
Tensorboard 回调应该自动获取这些摘要,因为它们被添加到 GraphKeys.SUMMARIES
集合中。
注意:很遗憾,我无法测试该解决方案。如果我遗漏了什么,请告诉我。
关于python - 如何使用 Keras 回调可视化 Tensorboard 中的平均编辑距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56264958/
我在 MySQL 中有以下数据,我想求和(总计)然后除以行数。 例子: 我想对 AcctSessionTime 列中的所有数字求和并将其除以项目数,所以在我们的例子中 6+4+3+31=44 将它们除
我试图找出一个值在列中出现的平均次数,根据另一列对其进行分组,然后对其进行计算。 我有 3 张 table ,有点像这样 DVD ID | NAME 1 | 1 2 | 1 3
好吧,我完全被困在这里,如果这给你们带来任何不便,我深表歉意,但我需要你们的帮助。 我目前正在自学 C,并且从昨天开始慢慢地达到目标。所以我想给自己一个任务,让用户输入 3 个数字,程序必须找到这三个
我在使用 subAverage 类时遇到困难。当我使用 main 方法时,它似乎无法正常运行。基本上,subAverage 对数组中包含开始索引和结束索引的项进行平均。但是,当我运行它时,我得到了 3
像这样平均一个表不是问题 table = [[1,2,3,0],[1,2,3,0],[1,2,3,4]] 你可以 print numpy.average(table,axis=0) 但是如果我有不均匀
问题 -开发一个类平均脚本,每次运行时都会处理任意数量的结果。提示用户输入每个结果,直到他/她输入 -1。 (哨兵)确定类(class)平均值并将其写入页面。如果未输入结果(第一个输入为 -1),则显
我有 2 个包含以下数据的数组: Array1 = [A, A, A, A, B, B, B, C, C, C, C, C]; Array2 = [4, 2, 4, 6, 3, 9, 6, 5,
我有一个如下所示的文本文件: Mike 5 7 9 Terry 3 7 4 Ste 8 2 3 我写了下面的程序 从文本文件中检索数据 将文本分成由空格分隔的列 将每个名字后面的分数按顺序排序(最低在
我试图找到范围内数字的平均值(即找到 1-1000 范围内所有数字的平均值)。我编写了以下代码来执行此操作,但由于 if 语句,在运行时,代码会生成多个数字。然后我尝试使用 while-loop 代替
我有一系列事件。 1 是好的,0 是坏的。寻找寻找 1 个序列的最大、最小和平均长度的最 Pythonic 方式。 例如: seq ="00100000000000110100100000000011
我有一个包含类似于以下数据的表格: Group TimePoint Value 1 0 1 1 0 2
假设我有一个类 C,它具有属性 a。 从 Python 中的 C 列表中获取 a 总和的最佳方法是什么? 我已经尝试了以下代码,但我知道这不是正确的做法: for c in c_list: t
我有一个看起来像的数据: AAA_1 AAA_2 AAA_3 BBB_1 BBB_2 BBB_3 CCC 1 1 1 1 2 2
对于分色算法,我需要对 std::vector 中的颜色值 (QRgb) 进行平均。 您建议如何做?分别对 3 个分量求和然后取平均值?不然呢? 最佳答案 自 QRgb只是一个 ARGB 格式的 32
在this问题中,我要求对精度调用曲线进行澄清。 特别是,我问我们是否必须考虑一定数量的排名才能画出曲线,还是我们可以合理地选择自己。根据answer,第二个是正确的。 但是,现在我对平均精度(AP)
我想在 UDP 数据包丢失(或丢失)问题上获得其他 SO'ers 的经验。 最初我的理解是,给定直接点对点连接,其中网卡通过交叉电缆连接,网卡上有充足的缓冲区并及时处理所述缓冲区,“应该”没有数据包丢
我有一系列数据,这些数据是通过分子动力学模拟获得的,因此在时间上是连续的,并且在某种程度上是相关的。我可以将平均值计算为数据的平均值,我想估计与以这种方式计算的平均值相关的误差。 根据 this bo
我正在使用以下averageIf公式 =AVERAGEIF('Backend Data - Aerospace'!D:D, "Total",'Backend Data - Aerospace'!E:E
我想列出所有收入超过平均工资的员工。我对此有点迷茫。我需要将所有薪水加起来然后取平均,只显示收入高于平均水平的薪水。在这方面我需要很多帮助。 我的查询不起作用 SQL> select empno,
我正在运行一些音频压缩测试并尝试 Skype's Silk .在他们的测试应用程序中,我看到压缩率为 94%。这似乎很高,这是 Silk 的典型比率吗?这与其他音频压缩编解码器有可比性吗? 最佳答案
我是一名优秀的程序员,十分优秀!