本文整理匯總了Python中sugartensor.sg_init方法的典型用法代碼示例。如果您正苦於以下問題:Python sugartensor.sg_init方法的具體用法?Python sugartensor.sg_init怎麽用?Python sugartensor.sg_init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sugartensor
的用法示例。
在下文中一共展示了sugartensor.sg_init方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: generate
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def generate(sample_image):
start_time = time.time()
g = ModelGraph()
with tf.Session() as sess:
# We need to initialize variables in this case because the Variable `generator/x` will not restored.
tf.sg_init(sess)
vars = [v for v in tf.global_variables() if "generator" not in v.name]
saver = tf.train.Saver(vars)
saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
i = 0
while True:
mse, _ = sess.run([g.mse, g.train_gen], {g.y: transform_image(sample_image)}) # (16, 28)
if time.time() - start_time > 60: # Save every 60 seconds
gen_image = sess.run(g.x)
gen_image = np.squeeze(gen_image)
misc.imsave('gen_images/%s/gen_%.2f.jpg' % (label, mse), gen_image)
start_time = time.time()
i += 1
if i == 60: break # Finish after 1 hour
示例2: run_generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
tf.sg_restore(sess, tf.train.latest_checkpoint('asset/train/infogan'), category='generator')
# run generator
imgs = sess.run(gen, {target_num: num,
target_cval_1: x1,
target_cval_2: x2})
# plot result
_, ax = plt.subplots(10, 10, sharex=True, sharey=True)
for i in range(10):
for j in range(10):
ax[i][j].imshow(imgs[i * 10 + j], 'gray')
ax[i][j].set_axis_off()
plt.savefig('asset/train/infogan/' + fig_name, dpi=600)
tf.sg_info('Sample image saved to "asset/train/infogan/%s"' % fig_name)
plt.close()
#
# draw sample by categorical division
#
# fake image
示例3: run_generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
# run generator
imgs = sess.run(gen, {target_num: num,
target_cval_1: x1,
target_cval_2: x2})
# plot result
_, ax = plt.subplots(10, 10, sharex=True, sharey=True)
for i in range(10):
for j in range(10):
ax[i][j].imshow(imgs[i * 10 + j], 'gray')
ax[i][j].set_axis_off()
plt.savefig('asset/train/' + fig_name, dpi=600)
tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
plt.close()
#
# draw sample by categorical division
#
# fake image
示例4: run_generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
# run generator
imgs = sess.run(gen, {target_num: num,
target_cval_1: x1,
target_cval_2: x2})
# plot result
_, ax = plt.subplots(10, 10, sharex=True, sharey=True)
for i in range(10):
for j in range(10):
ax[i][j].plot(imgs[i * 10 + j, :, 0], color='b', linewidth=0.25)
# Turn off tick labels only
# ax[i][j].set_axis_off()
ax[i][j].set_xticks([])
ax[i][j].set_yticks([])
plt.savefig('asset/train/' + fig_name, dpi=600)
tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
plt.close()
#
# draw sample by categorical division
#
# fake image
示例5: run_generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
# run generator
imgs = sess.run(gen, {target_num: num,
target_cval_1: x1,
target_cval_2: x2})
# plot result
_, ax = plt.subplots(10, 10, sharex=True, sharey=True)
for i in range(10):
for j in range(10):
ax[i][j].plot(imgs[i * 10 + j, :, 0])
ax[i][j].plot(imgs[i * 10 + j, :, 1])
ax[i][j].set_axis_off()
plt.savefig('asset/train/' + fig_name, dpi=600)
tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
plt.close()
#
# draw sample by categorical division
#
# fake image
示例6: run_generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
# run generator
imgs = sess.run(gen, {target_num: num,
target_cval_1: x1,
target_cval_2: x2})
# plot result
_, ax = plt.subplots(10, 10, sharex=True, sharey=True)
for i in range(10):
for j in range(10):
ax[i][j].imshow(imgs[i * 10 + j], 'gray')
ax[i][j].set_axis_off()
plt.savefig('asset/train/' + fig_name, dpi=600)
tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
plt.close()
#
# draw sample by categorical division
#
# fake image
示例7: main
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def main():
g = ModelGraph()
with tf.Session() as sess:
tf.sg_init(sess)
# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
hits = 0
num_imgs = 0
with tf.sg_queue_context(sess):
# loop end-of-queue
while True:
try:
logits, y = sess.run([g.logits, g.y]) # (16, 28)
preds = np.squeeze(np.argmax(logits, -1)) # (16,)
hits += np.equal(preds, y).astype(np.int32).sum()
num_imgs += len(y)
print "%d/%d = %.02f" % (hits, num_imgs, float(hits) / num_imgs)
except:
break
print "\nFinal result is\n%d/%d = %.02f" % (hits, num_imgs, float(hits) / num_imgs)
# fout.write(u"▌file_name: {}\n".format(f))
# fout.write(u"▌Expected: {}\n".format(label2cls[]))
# fout.write(u"▌file_name: {}\n".format(f))
# fout.write(u"▌Got: " + predicted + "\n\n")
示例8: eval
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_init [as 別名]
def eval():
# Load graph
g = Graph(mode="inference"); print("Graph Loaded")
with tf.Session() as sess:
# Initialize variables
tf.sg_init(sess)
# Restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
print("Restored!")
mname = open('asset/train/checkpoint', 'r').read().split('"')[1] # model name
# Load data
X, Sources, Targets = load_test_data()
char2idx, idx2char = load_vocab()
with codecs.open(mname, "w", "utf-8") as fout:
list_of_refs, hypotheses = [], []
for i in range(len(X) // Hp.batch_size):
# Get mini-batches
x = X[i*Hp.batch_size: (i+1)*Hp.batch_size] # mini-batch
sources = Sources[i*Hp.batch_size: (i+1)*Hp.batch_size]
targets = Targets[i*Hp.batch_size: (i+1)*Hp.batch_size]
preds_prev = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)
preds = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)
for j in range(Hp.maxlen):
# predict next character
outs = sess.run(g.preds, {g.x: x, g.y_src: preds_prev})
# update character sequence
if j < Hp.maxlen - 1:
preds_prev[:, j + 1] = outs[:, j]
preds[:, j] = outs[:, j]
# Write to file
for source, target, pred in zip(sources, targets, preds): # sentence-wise
got = "".join(idx2char[idx] for idx in pred).split(u"␃")[0]
fout.write("- source: " + source +"\n")
fout.write("- expected: " + target + "\n")
fout.write("- got: " + got + "\n\n")
fout.flush()
# For bleu score
ref = target.split()
hypothesis = got.split()
if len(ref) > 2:
list_of_refs.append([ref])
hypotheses.append(hypothesis)
# Get bleu score
score = corpus_bleu(list_of_refs, hypotheses)
fout.write("Bleu Score = " + str(100*score))