Skip to content

Instantly share code, notes, and snippets.

@Krucamper
Created November 23, 2019 18:05
Show Gist options
  • Save Krucamper/215820727e4670c2945335d52082a9b2 to your computer and use it in GitHub Desktop.
Save Krucamper/215820727e4670c2945335d52082a9b2 to your computer and use it in GitHub Desktop.
CNN กับ Marvel Cinematic Universe (train model)
import os
import numpy as np
import tensorflow as tf
import input_data
import model
N_CLASSES = 6
IMG_W = 64
IMG_H = 64
BATCH_SIZE = 10
CAPACITY = 100
MAX_STEP = 1000
learning_rate = 0.0005
prepro_image = './genarate'
logs_prepro_image = './model_save'
train, train_label, val, val_label = input_data.get_files(prepro_image, 0.2)
train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
val_batch, val_label_batch = input_data.get_batch(val, val_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
train_loss = model.losses(train_logits, train_label_batch)
train_op = model.trainning(train_loss, learning_rate)
train_acc = model.evaluation(train_logits, train_label_batch)
test_logits = model.inference(val_batch, BATCH_SIZE, N_CLASSES)
test_loss = model.losses(test_logits, val_label_batch)
test_acc = model.evaluation(test_logits, val_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_prepro_image, sess.graph)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
_, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
if step % 10 == 0:
print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if (step + 1) == MAX_STEP:
checkpoint_path = os.path.join(logs_prepro_image, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
coord.request_stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment