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)
def run_generator(fig_name, cval): with tf.Session() as sess: # init session tf.sg_init(sess) # restore parameters saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint('asset/train')) feed_dic = {} for t, c in zip(target_cval, cval): feed_dic[t] = c # run generator imgs = sess.run(gen, feed_dic) # 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]) 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()
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
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()
def __init__(self): # set log level to debug tf.sg_verbosity(10) # batch size self.batch_size = 1 # vocabulary size self.voca_size = sttwdata.voca_size # mfcc feature of audio self.x = tf.placeholder(dtype=tf.sg_floatx, shape=(self.batch_size, None, 20)) # encode audio feature self.logit = get_logit(self.x, voca_size=self.voca_size) # sequence length except zero-padding self.seq_len = tf.not_equal(self.x.sg_sum(axis=2), 0.).sg_int().sg_sum(axis=1) # run network self.session = tf.Session() tf.sg_init(self.session) self.saver = tf.train.Saver() self.saver.restore(self.session, tf.train.latest_checkpoint('asset/train'))
def train(self): predict = self.forward(Mnist.train.image) #######GP sess = tf.Session() with tf.sg_queue_context(sess): tf.sg_init(sess) trainf = sess.run([Mnist.train.image])[0] n, w, h, c = trainf.shape print trainf.shape np.savetxt('./image.txt', trainf[1, :, :, 0]) #print trainf[1, :, :, 0] #plt.imshow(trainf[1, :, :, 0]) #plt.axis('off') #plt.show() #print type(trainf[1, :, :, 0]) transfer = np.zeros((n, w, h, c)) for i in range(n): candi = random.randint(0, n - 1) #print GP(trainf[i, :, :, 0], trainf[candi, :, :, 0]) #transfer[i, :, :, :] = GP(trainf[i, :, :, :], trainf[candi, :, :, :]) #print trainsfer[i, :, :, :] t = tf.convert_to_tensor(transfer, dtype=tf.float32) gp_predict = predict.sg_reuse(input=t) #print trainf.shape sess.close()
def train(self): # train baseline model input_ph = tf.placeholder(shape=[batch_size, 28, 28, 1], dtype=tf.float32) label_ph = tf.placeholder(shape=[ batch_size, ], dtype=tf.int32) predict = self.forward(input_ph) loss_tensor = tf.reduce_mean(predict.sg_ce(target=label_ph)) # use to update network parameters optim = tf.sg_optim(loss_tensor, optim='Adam', lr=1e-3) # use saver to save a new model saver = tf.train.Saver() sess = tf.Session() with tf.sg_queue_context(sess): # inital tf.sg_init(sess) # validation acc = (predict.sg_reuse( input=Mnist.valid.image).sg_softmax().sg_accuracy( target=Mnist.valid.label, name='validation')) tf.sg_train(loss=loss, eval_metric=[acc], max_ep=max_ep, save_dir=save_dir, ep_size=Mnist.train.num_batch, log_interval=10)
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()
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(input_reverse=Hp.reverse_inputs) 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))
def generate(self, prev_midi): with tf.Session() as sess: tf.sg_init(sess) # saver = tf.train.Saver() # saver.restore(sess, tf.train.latest_checkpoint('save/train/small')) # KDK: choose self.next_token or self.preds # out = sess.run(self.next_token, {self.x: prev_midi}) tf.sg_restore(sess, tf.train.latest_checkpoint('save/train/small')) out = sess.run(self.next_token, {self.x: prev_midi}) return out
def test(self): # predict = self.forward(Mnist.test.image) # acc = (predict.sg_softmax() # .sg_accuracy(target=Mnist.test.label, name='test')) sess = tf.Session() with tf.sg_queue_context(sess): tf.sg_init(sess) testf = sess.run([Mnist.test.image])[0] # print testf.shape n, w, h, c = testf.shape tmp0 = np.zeros((n * w, h)) tmp02 = np.zeros((n * w, h)) tmp05 = np.zeros((n * w, h)) tmp08 = np.zeros((n * w, h)) tmp90 = np.zeros((n * w, h)) tmp_90 = np.zeros((n * w, h)) for i in range(n): tmp0[i * w : (i + 1) * w, 0 : h] = testf[i, :, :, 0] tmp02[i * w : (i + 1) * w, 0 : h] = addnoisy(testf[i, :, :, 0], 0.2) tmp05[i * w : (i + 1) * w, 0 : h] = addnoisy(testf[i, :, :, 0], 0.5) tmp08[i * w : (i + 1) * w, 0 : h] = addnoisy(testf[i, :, :, 0], 0.8) tmp90[i * w : (i + 1) * w, 0 : h] = rotate90(testf[i, :, :, 0]) tmp_90[i * w : (i + 1) * w, 0 : h] = rotate_90(testf[i, :, :, 0])# addnoisy(testf[i, :, :, 0], 0.8) #testf[i, :, :, 0] = addnoisy(testf[i, :, :, 0], 0.0) #testf[i, :, :, 0] = rotate90(testf[i, :, :, 0]) #testf[i, :, :, 0] = rotate_90(testf[i, :, :, 0]) #print testf[i, :, :, 0] np.savetxt('./image0.txt', tmp0) np.savetxt('./image02.txt', tmp02) np.savetxt('./image05.txt', tmp05) np.savetxt('./image08.txt', tmp08) np.savetxt('./image90.txt', tmp90) np.savetxt('./image_90.txt', tmp_90) testf_tensor = tf.convert_to_tensor(testf, dtype=tf.float32) predict = self.forward(testf_tensor) acc = (predict.sg_softmax() .sg_accuracy(target=Mnist.test.label, name='test')) saver=tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint(save_dir)) total_accuracy = 0 for i in range(Mnist.test.num_batch): total_accuracy += np.sum(sess.run([acc])[0]) print('Evaluation accuracy: {}'.format(float(total_accuracy)/(Mnist.test.num_batch*batch_size))) # close session sess.close()
def main(): g = ModelGraph(mode="test") with tf.Session() as sess: tf.sg_init(sess) # restore parameters saver = tf.train.Saver() if Hyperparams.isqwerty: save_path = "qwerty/asset/train/ckpt" else: save_path = "nine/asset/train/ckpt" saver.restore(sess, tf.train.latest_checkpoint(save_path)) mname = open(save_path + "/checkpoint", 'r').read().split('"')[1] nums, X, expected_list = load_test_data() pnyn2idx, idx2pnyn, hanzi2idx, idx2hanzi = load_vocab() with codecs.open('data/output_{}.txt'.format(mname), 'w', 'utf-8') as fout: cum_score = 0 full_score = 0 for step in range(len(X) // 64 + 1): n = nums[step * 64:(step + 1) * 64] #number batch x = X[step * 64:(step + 1) * 64] # input batch e = expected_list[step * 64:(step + 1) * 64] # batch of ground truth strings # predict characters logits = sess.run(g.logits, {g.x: x}) preds = np.squeeze(np.argmax(logits, -1)) for nn, xx, pp, ee in zip(n, x, preds, e): # sentence-wise got = '' for xxx, ppp in zip(xx, pp): # character-wise if xxx == 0: break if xxx == 1 or ppp == 1: got += "*" else: got += idx2hanzi.get(ppp, "*") got = got.replace("_", "") # Remove blanks error = distance.levenshtein(ee, got) score = len(ee) - error cum_score += score full_score += len(ee) fout.write(u"{}\t{}\t{}\t{}\n".format(nn, ee, got, score)) fout.write(u"Total acc.: {}/{}={}\n".format( cum_score, full_score, round(float(cum_score) / full_score, 2)))
def predict_wavenet(mfcc): with tf.Session().as_default() as sess: # init variables tf.sg_init(sess) # # restore parameters # saver = tf.train.Saver() saver.restore(sess, train_model) # run session label = sess.run(y, feed_dict={x: mfcc}) # print label return data.print_index(label)
def load_model(): # load the pre-trained Keras model (here we are using a model # pre-trained on ImageNet and provided by Keras, but you can # substitute in your own networks just as easily) global model, sess init_model() # run network sess = tf.Session() # init variables tf.sg_init(sess) # restore parameters saver = tf.train.Saver() model = saver.restore(sess, tf.train.latest_checkpoint('wavenet_train'))
def main(): graph = ModelGraph("test") 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')) X, Y = load_data("test") idx2chr = load_charmaps()[0] with codecs.open('results.txt', 'w', 'utf-8') as fout: results = [] for step in range(len(X) // Hyperparams.batch_size - 1): X_batch = X[step * Hyperparams.batch_size:(step + 1) * Hyperparams.batch_size] Y_batch = Y[step * Hyperparams.batch_size:(step + 1) * Hyperparams.batch_size] # predict characters logits = sess.run(graph.logits, {graph.X_batch: X_batch}) preds = np.squeeze(np.argmax(logits, -1)) for x, y, p in zip(X_batch, Y_batch, preds): # sentence-wise ground_truth = '' predicted = '' for xx, yy, pp in zip(x, y, p): # character-wise if xx == 0: break else: predicted += idx2chr.get(xx, "*") ground_truth += idx2chr.get(xx, "*") if pp == 1: predicted += " " if yy == 1: ground_truth += " " if pp == yy: results.append(1) else: results.append(0) fout.write(u"▌Expected: " + ground_truth + "\n") fout.write(u"▌Got: " + predicted + "\n\n") fout.write(u"Final Accuracy = %d/%d=%.2f" % (sum(results), len(results), float(sum(results)) / len(results)))
def test(self): print 'Testing model {}: addnoise={}, rotate={}, var={}'.format( save_dir, addnoise, rotate, var) input_ph = tf.placeholder(shape=[batch_size, 28, 28, 1], dtype=tf.float32) label_ph = tf.placeholder(shape=[ batch_size, ], dtype=tf.int32) predict = self.forward(input_ph) acc = (predict.sg_softmax().sg_accuracy(target=label_ph, name='test')) sess = tf.Session() with tf.sg_queue_context(sess): tf.sg_init(sess) saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint(save_dir)) total_accuracy = 0 for i in range(Mnist.test.num_batch): [image_array, label_array] = sess.run([Mnist.test.image, Mnist.test.label]) if addnoise: image_array[0, :, :, 0] = addnoisy(image_array[0, :, :, 0], var) if rotate: image_array[0, :, :, 0] = rotate_90(image_array[0, :, :, 0]) acc_value = sess.run([acc], feed_dict={ input_ph: image_array, label_ph: label_array })[0] total_accuracy += np.sum(acc_value) print 'Evaluation accuracy: {}'.format( float(total_accuracy) / (Mnist.test.num_batch * batch_size)) # close session sess.close()
def main(): g = ModelGraph(mode="test") with tf.Session() as sess: 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 char2idx, idx2char = load_char_vocab() word2idx, idx2word = load_word_vocab() previous = [0] * 50 # a stack for previous words para = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" ctx = [0] * 50 while True: key = readchar.readkey().lower() if key == readchar.key.BACKSPACE: ctx.insert(0, previous.pop()) ctx.pop() previous.insert(0, 0) elif key == readchar.key.ESC: break else: key_idx = char2idx[key] ctx.append(key_idx) ctx.pop(0) logits = sess.run(g.logits, {g.x: np.expand_dims(ctx, 0)}) preds = logits.argsort()[0][-3:] # pred = np.argmax(logits, -1)[0] predword1, predword2, predword3 = [ idx2word.get(pred) for pred in preds ] print(predword1, ' ', predword2, ' ', predword3)
def test1(): ''' Predicts all at once. ''' X, Y = preprocess() g = Graph(is_train=False) 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')) total_blanks, total_hits = 0, 0 for x_3d, y_2d in zip(X, Y): # problem-wise x: (9, 9, 1), y: (9, 9) x_2d = np.squeeze(x_3d, -1) #(9, 9) x_4d = np.expand_dims(x_3d, 0) # (1, 9, 9, 1) while 1: logits = sess.run(g.logits, {g.X: x_4d}) # (1, 9, 9, 10) float32 preds = np.squeeze(np.argmax(logits, axis=-1), 0) # (9, 9) # most probable numbers expected = y_2d[x_2d == 0] got = preds[x_2d == 0] hits = np.equal(expected, got).sum() result = np.where(x_2d == 0, preds, y_2d).astype(int) print result print "Acc.=%d/%d=%.2f" % (hits, len(expected), float(hits) / len(expected)) total_blanks += len(expected) total_hits += hits break print "Total Accuracy = %d/%d=%.2f" % ( total_hits, total_blanks, float(total_hits) / total_blanks)
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()
def main(): g = ModelGraph(is_train=False) 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')) # Or you could use pretrained model which can be downloaded from here # https://drive.google.com/open?id=0B5M-ed49qMsDQ1dEYXF3cTVNM1E # saver.restore(sess, 'model-019-1239684') sents, X = vectorize_input() idx2hanja = load_charmaps()[-1] with codecs.open('data/output.txt', 'w', 'utf-8') as fout: for step in range(len(X) // Hyperparams.batch_size + 1): inputs = sents[step * Hyperparams.batch_size:(step + 1) * Hyperparams.batch_size] # batch x = X[step * Hyperparams.batch_size:(step + 1) * Hyperparams.batch_size] # batch # predict characters logits = sess.run(g.logits, {g.x: x}) preds = np.squeeze(np.argmax(logits, -1)) for ii, xx, pp in zip(inputs, x, preds): # sentence-wise got = '' for ii, xxx, ppp in zip(ii, xx, pp): # character-wise if xxx == 0: break elif xxx == 1 or ppp == 1: got += ii else: got += idx2hanja.get(ppp, "*") fout.write(got + "\n")
def train_with_GP(self): input_ph = tf.placeholder(shape=[batch_size, 28, 28, 1], dtype=tf.float32) label_ph = tf.placeholder(shape=[ batch_size, ], dtype=tf.int32) predict = self.forward(input_ph) loss_tensor = tf.reduce_mean(predict.sg_ce(target=label_ph)) # use to update network parameters optim = tf.sg_optim(loss_tensor, optim='Adam', lr=1e-3) # use saver to save a new model saver = tf.train.Saver() sess = tf.Session() with tf.sg_queue_context(sess): # inital tf.sg_init(sess) # train by GP guilding for e in range(max_ep): previous_loss = None for i in range(Mnist.train.num_batch): [image_array, label_array ] = sess.run([Mnist.train.image, Mnist.train.label]) if (e == 0 or e == 1 ): # first and second epoch train no noisy image loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_array, label_ph: label_array })[0] print 'Baseline loss = ', loss elif ( e == 2 ): # third epoch train with gp image and original image gpIn1 = np.squeeze(image_array) gpIn2 = np.zeros((28, 28)) image_gp = GP(gpIn1, gpIn2, seed=0.8) image_gp2 = image_gp[np.newaxis, ...] image_gp2 = image_gp2[..., np.newaxis] loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_array, label_ph: label_array })[0] print 'GP without nosiy loss = ', loss loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_gp2, label_ph: label_array })[0] print 'GP loss = ', loss else: # other epoch train with gp evolution gpIn1 = np.squeeze(image_array) gpIn2 = np.zeros((28, 28)) image_gp = GP(gpIn1, gpIn2, seed=random.random()) image_gp2 = image_gp[np.newaxis, ...] image_gp2 = image_gp2[..., np.newaxis] loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_array, label_ph: label_array })[0] print 'GP without nosiy loss = ', loss loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_gp2, label_ph: label_array })[0] print 'GP loss = ', loss if loss < previous_loss: for i in range(5): loss = sess.run([loss_tensor, optim], feed_dict={ input_ph: image_gp2, label_ph: label_array })[0] gpIn1 = image_gp2 image_gp2[0, :, :, 0] = GP(gpIn1[0, :, :, 0], gpIn2, seed=random.random()) print 'GP EV loss = ', loss previous_loss = loss saver.save(sess, os.path.join(save_dir, 'gp_model'), global_step=e) # close session sess.close()
# # encode audio feature logit = get_logit(x, voca_size=voca_size) # CTC loss loss = logit.sg_ctc(target=y, seq_len=seq_len) # # run network # with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: # init variables tf.sg_init(sess) # restore parameters saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint('asset/train')) # logging tf.sg_info('Testing started on %s set at global step[%08d].' % (tf.sg_arg().set.upper(), sess.run(tf.sg_global_step()))) with tf.sg_queue_context(): # create progress bar iterator = tqdm(range(0, int(data.num_batch * tf.sg_arg().frac)), total=int(data.num_batch * tf.sg_arg().frac), initial=0, desc='test', ncols=70, unit='b', leave=False)
batch_size = 1 # batch size voca_size = data.voca_size x = tf.placeholder(dtype=tf.sg_floatx, shape=(batch_size, None, 20)) # sequence length except zero-padding seq_len = tf.not_equal(x.sg_sum(axis=2), 0.).sg_int().sg_sum(axis=1) # encode audio feature logit = get_logit(x, voca_size) # ctc decoding decoded, _ = tf.nn.ctc_beam_search_decoder(logit.sg_transpose(perm=[1, 0, 2]), seq_len, merge_repeated=False) # to dense tensor y = tf.add(tf.sparse_to_dense(decoded[0].indices, decoded[0].dense_shape, decoded[0].values), 1, name="output") with tf.Session() as sess: tf.sg_init(sess) saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint('asset/train')) graph = tf.get_default_graph() input_graph_def = graph.as_graph_def() with tf.Session() as sess: tf.sg_init(sess) saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint('asset/train')) # Output model's graph details for reference. tf.train.write_graph(sess.graph_def, '/root/speech-to-text-wavenet/asset/train', 'graph.txt', as_text=True) # Freeze the output graph. output_graph_def = graph_util.convert_variables_to_constants(sess,input_graph_def,"output".split(",")) # Write it into .pb file.
def test2(): ''' Predicts sequentially. ''' X, Y = preprocess() g = Graph(is_train=False) with tf.Session() as sess: tf.sg_init(sess) saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt')) total_blanks, total_hits = 0, 0 for x_3d, y_2d in zip(X, Y): # problem-wise x: (9, 9, 1), y: (9, 9) x_2d = np.squeeze(x_3d, -1) #(9, 9) x_4d = np.expand_dims(x_3d, 0) # (1, 9, 9, 1) _x_2d = np.copy(x_2d) # (9, 9) while 1: logits = sess.run(g.logits, {g.X: x_4d}) # (1, 9, 9, 10) float32 def softmax(x): """Compute softmax values for each sets of scores in x.""" e_x = np.exp(x - np.max(x, -1, keepdims=True)) return e_x / e_x.sum(axis=-1, keepdims=True) activated = softmax(logits) # (1, 9, 9, 10) float32 preds = np.squeeze(np.argmax(activated, axis=-1), 0) # (9, 9) # most probable numbers preds_prob = np.squeeze( np.max(activated, axis=-1), 0) # (9, 9) # highest probabilities for blanks preds_prob = np.where(x_2d == 0, preds_prob, 0) # (9, 9) top1 = np.argmax( preds_prob ) # the index of the most confident number amongst all predictions ind = np.unravel_index(top1, (9, 9)) got = preds[ind] # the most confident number x_2d[ind] = got # result x_4d = np.expand_dims(np.expand_dims(x_2d, 0), -1) if len(x_2d[x_2d == 0]) == 0: expected = y_2d[_x_2d == 0] got = x_2d[_x_2d == 0] hits = np.equal(expected, got).sum() result = np.where(_x_2d == 0, x_2d, y_2d).astype(int) print result print "Acc.=%d/%d=%.2f" % (hits, len(expected), float(hits) / len(expected)) total_blanks += len(expected) total_hits += hits break print "Total Accuracy = %d/%d=%.2f" % ( total_hits, total_blanks, float(total_hits) / total_blanks)
def train(self): ''' Network ''' batch_pred_feats, batch_pred_coords, batch_pred_confs, self.final_state = self.LSTM( 'lstm', self.x) iou_predict_truth, intersection = self.iou(batch_pred_coords, self.y[:, 0:4]) should_exist = I = tf.cast( tf.reduce_sum(self.y[:, 0:4], axis=1) > 0., tf.float32) no_I = tf.ones_like(I, dtype=tf.float32) - I object_loss = tf.nn.l2_loss( I * (batch_pred_confs - iou_predict_truth)) * self.object_scale noobject_loss = tf.nn.l2_loss( no_I * (batch_pred_confs - iou_predict_truth)) * self.noobject_scale p_sqrt_w = tf.sqrt( tf.minimum(1.0, tf.maximum(0.0, batch_pred_coords[:, 2]))) p_sqrt_h = tf.sqrt( tf.minimum(1.0, tf.maximum(0.0, batch_pred_coords[:, 3]))) sqrt_w = tf.sqrt(tf.abs(self.y[:, 2])) sqrt_h = tf.sqrt(tf.abs(self.y[:, 3])) loss = (tf.nn.l2_loss(I * (batch_pred_coords[:, 0] - self.y[:, 0])) + tf.nn.l2_loss(I * (batch_pred_coords[:, 1] - self.y[:, 1])) + tf.nn.l2_loss(I * (p_sqrt_w - sqrt_w)) + tf.nn.l2_loss(I * (p_sqrt_h - sqrt_h))) * self.coord_scale #max_iou = tf.nn.l2_loss(I*(tf.ones_like(iou_predict_truth, dtype=tf.float32) - iou_predict_truth)) total_loss = loss + object_loss + noobject_loss #+ max_iou ''' Optimizer ''' optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate).minimize( total_loss) # Adam Optimizer ''' Summary for tensorboard analysis ''' dataset_loss = -1 dataset_loss_best = 100 test_writer = tf.summary.FileWriter('summary/test') tf.summary.scalar('dataset_loss', dataset_loss) summary_op = tf.summary.merge_all() ''' Initializing the variables ''' self.saver = tf.train.Saver() batch_states = np.zeros((self.batchsize, 2 * self.len_vec)) # TODO: make this a command line argument, etc. # training set loader batch_loader = BatchLoader( "./DATA/TRAINING/", seq_len=self.nsteps, batch_size=self.batchsize, step_size=1, folders_to_use=[ "GOPR0005", "GOPR0006", "GOPR0008", "GOPR0008_2", "GOPR0009", "GOPR0009_2", "GOPR0010", "GOPR0011", "GOPR0012", "GOPR0013", "GOPR0014", "GOPR0015", "GOPR0016", "MVI_8607", "MVI_8609", "MVI_8610", "MVI_8612", "MVI_8614", "MVI_8615", "MVI_8616" ]) validation_set_loader = BatchLoader( "./DATA/VALID/", seq_len=self.nsteps, batch_size=self.batchsize, step_size=1, folders_to_use=[ "bbd_2017__2017-01-09-21-40-02_cam_flimage_raw", "bbd_2017__2017-01-09-21-44-31_cam_flimage_raw", "bbd_2017__2017-01-09-21-48-46_cam_flimage_raw", "bbd_2017__2017-01-10-16-07-49_cam_flimage_raw", "bbd_2017__2017-01-10-16-21-01_cam_flimage_raw", "bbd_2017__2017-01-10-16-31-57_cam_flimage_raw", "bbd_2017__2017-01-10-21-43-03_cam_flimage_raw", "bbd_2017__2017-01-11-20-21-32_cam_flimage_raw", "bbd_2017__2017-01-11-21-02-37_cam_flimage_raw" ]) print("%d available training batches" % len(batch_loader.batches)) print("%d available validation batches" % len(validation_set_loader.batches)) ''' Launch the graph ''' with tf.Session() as sess: if self.restore_weights == True and os.path.isfile( self.rolo_current_save + ".index"): # sess.run(init) tf.sg_init(sess) self.saver.restore(sess, self.rolo_current_save) print("Weight loaded, finetuning") else: # sess.run(init) tf.sg_init(sess) print("Training from scratch") epoch_loss = [] for self.iter_id in range(self.n_iters): ''' Load training data & ground truth ''' batch_id = self.iter_id - self.batch_offset batch_xs, batch_ys, _ = batch_loader.load_batch(batch_id) ''' Update weights by back-propagation ''' sess.run(optimizer, feed_dict={ self.x: batch_xs, self.y: batch_ys }) if self.iter_id % self.display_step == 0: ''' Calculate batch loss ''' batch_loss = sess.run(total_loss, feed_dict={ self.x: batch_xs, self.y: batch_ys }) epoch_loss.append(batch_loss) print("Total Batch loss for iteration %d: %.9f" % (self.iter_id, batch_loss)) if self.iter_id % self.display_step == 0: ''' Calculate batch loss ''' batch_loss = sess.run(loss, feed_dict={ self.x: batch_xs, self.y: batch_ys }) print( "Bounding box coord error loss for iteration %d: %.9f" % (self.iter_id, batch_loss)) if self.display_object_loss and self.iter_id % self.display_step == 0: ''' Calculate batch object loss ''' batch_o_loss = sess.run(object_loss, feed_dict={ self.x: batch_xs, self.y: batch_ys }) print("Object loss for iteration %d: %.9f" % (self.iter_id, batch_o_loss)) if self.display_object_loss and self.iter_id % self.display_step == 0: ''' Calculate batch object loss ''' batch_noo_loss = sess.run(noobject_loss, feed_dict={ self.x: batch_xs, self.y: batch_ys }) print("No Object loss for iteration %d: %.9f" % (self.iter_id, batch_noo_loss)) if self.iou_with_ground_truth and self.iter_id % self.display_step == 0: ''' Calculate batch object loss ''' batch_o_loss = sess.run(tf.reduce_mean(iou_predict_truth), feed_dict={ self.x: batch_xs, self.y: batch_ys }) print("Average IOU with ground for iteration %d: %.9f" % (self.iter_id, batch_o_loss)) if self.display_coords is True and self.iter_id % self.display_step == 0: ''' Caculate predicted coordinates ''' coords_predict = sess.run(batch_pred_coords, feed_dict={ self.x: batch_xs, self.y: batch_ys }) print("predicted coords:" + str(coords_predict[0])) print("ground truth coords:" + str(batch_ys[0])) ''' Save model ''' if self.iter_id % self.save_step == 1: self.saver.save(sess, self.rolo_current_save) print("\n Model saved in file: %s" % self.rolo_current_save) ''' Validation ''' if self.validate == True and self.iter_id % self.validate_step == 0 and self.iter_id > 0: # Run validation set dataset_loss = self.test(sess, total_loss, validation_set_loader, batch_pred_feats, batch_pred_coords, batch_pred_confs, self.final_state) ''' Early-stop regularization ''' if dataset_loss <= dataset_loss_best: dataset_loss_best = dataset_loss self.saver.save(sess, self.rolo_weights_file) print("\n Better Model saved in file: %s" % self.rolo_weights_file) ''' Write summary for tensorboard ''' summary = sess.run(summary_op, feed_dict={ self.x: batch_xs, self.y: batch_ys }) test_writer.add_summary(summary, self.iter_id) print("Average total loss %f" % np.mean(epoch_loss)) return
def main(): g = ModelGraph(mode="test") with tf.Session() as sess: 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 X, Y = load_test_data() char2idx, idx2char = load_char_vocab() word2idx, idx2word = load_word_vocab() results = [] rk = 0 num_para = 1 num_char = 1 for x, y in zip(X, Y): stop_counting = False x = np.concatenate( (np.zeros((Hyperparams.seqlen - 1, )), x[-np.count_nonzero(x):])) # lstrip and zero-pad para = "".join([idx2char[idx] for idx in x]) chars, targets = [], [] # targets: the word that the char composes for word in "".join(para).split(): chars.append(" ") targets.append(word) for char in word: chars.append(char) targets.append(word) prefix = "" preds = set() for i, char_target in enumerate(zip(chars, targets)): char, target = char_target oov = "" if target not in word2idx: oov = "oov" if i > Hyperparams.seqlen: ctx = np.array(x[i - Hyperparams.seqlen:i], np.int32) # if char == " ": stop_counting = False preds = set() if not stop_counting: logits = sess.run( g.logits, {g.x: np.expand_dims(ctx, 0)}) #(1, 20970) while 1: pred = np.argmax(logits, -1)[0] # (1,) if pred in preds: logits[:, pred] = -100000000 else: break rk += 1 predword = idx2word.get(pred) if predword == target: # S stop_counting = True preds.add(pred) results.append(u"{},{},{},{},{},{},{}".format( num_char, num_para, char, target, oov, predword, rk)) num_char += 1 num_para += 1 with codecs.open('data/output_{}_rk_{}.csv'.format(mname, rk), 'w', 'utf-8') as fout: fout.write("\n".join(results))
def main(argv): # set log level to debug tf.sg_verbosity(10) # # hyper parameters # size = 160, 147 batch_size = 1 # batch size # # inputs # pngName = argv png = tf.read_file(pngName) #png.thumbnail(size, Image.ANTIALIAS) #png = tf.resize(png1, (14,14)) myPNG = tf.image.decode_png(png) y = convert_image(pngName) x = tf.reshape(y, [1, 28, 28, 1]) print(x) # corrupted image x_small = tf.image.resize_bicubic(x, (14, 14)) x_bicubic = tf.image.resize_bicubic(x_small, (28, 28)).sg_squeeze() x_nearest = tf.image.resize_images( x_small, (28, 28), tf.image.ResizeMethod.NEAREST_NEIGHBOR).sg_squeeze() # # create generator # # I've used ESPCN scheme # http://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Shi_Real-Time_Single_Image_CVPR_2016_paper.pdf # # generator network with tf.sg_context(name='generator', act='relu', bn=True): gen = (x.sg_conv(dim=32).sg_conv().sg_conv( dim=4, act='sigmoid', bn=False).sg_periodic_shuffle(factor=2).sg_squeeze()) # # run generator # fileName = "inPython.png" fig_name = "genImages/" + fileName #fig_name2 = 'asset/train/sample2.png' print("start") with tf.Session() as sess: with tf.sg_queue_context(sess): tf.sg_init(sess) # restore parameters saver = tf.train.Saver() #saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt')) saver.restore( sess, tf.train.latest_checkpoint('python/asset/train/ckpt')) # run generator gt, low, bicubic, sr = sess.run( [x.sg_squeeze(), x_nearest, x_bicubic, gen]) # plot result #sr[0].thumbnail(size, Image.ANTIALIAS) plt.figure(figsize=(1, 1)) #plt.set_axis_off() hr = plt.imshow(sr[0], 'gray') plt.axis('tight') plt.axis('off') #ax.set_axis_off() #ax.thumbnail(size, Image.ANTIALIAS) #plt.savefig(fig_name,bbox_inches='tight',pad_inches=0,dpi=600) plt.savefig(fig_name, dpi=600) #tf.sg_info('Sample image saved to "%s"' % fig_name) plt.close() ##print (type (sr[0])) ##sourceImage = Image.fromarray(np.uint8(sr[0]) print("done")