def main(argv=None): # pylint: disable=unused-argument train_dir = dp.train_dir(FLAGS.train_dir) if tf.gfile.Exists(train_dir): raise Exception('train_dir: {0} exists'.format(train_dir)) tf.gfile.MakeDirs(train_dir) np.random.seed(FLAGS.random_seed) tf.set_random_seed(FLAGS.random_seed) train(FLAGS.region, FLAGS.batch_size, FLAGS.mean_subtract)
def _inference(data_set, keypoint): tf.reset_default_graph() ids = Table().read_table(dp.ids_by_data_set(data_set)).column('Id') base_png_path = dp.LARGE_DATA_BIN + '/data/raw/' if keypoint.file_format == 'a3daps': base_png_path += 'a3daps_png/' else: base_png_path += 'aps_png/' base_xml_output_path = dp.REPO_HOME_PATH + '/data/bbox/keypoint_inference/' + keypoint.name images_tensor = tf.placeholder(tf.float32, (None, 660, 512, 1)) model = KeypointDetectModel(batch_size=1, image_dim=(660, 512), anchor_kwargs=keypoint.anchor_kwargs) logits_tensor, bboxes_tensor = model.inference(images_tensor, test=True) logits_tensor = tf.sigmoid(logits_tensor) bboxes_tensor = target_to_box(bboxes_tensor, model.anchors) saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: saver.restore( sess, tf.train.get_checkpoint_state( dp.train_dir('{0}_keypoint'.format( keypoint.name))).model_checkpoint_path) for _id in ids: image = np.array( Image.open('{0}/{1}/{1}_{2}.png'.format( base_png_path, keypoint.slices[0], _id))).astype(np.float32) image -= np.mean(image) logits, bboxes = sess.run( [logits_tensor, bboxes_tensor], feed_dict={images_tensor: image[np.newaxis, :, :, np.newaxis]}) nms_result = non_max_suppression(bboxes, logits, min_clique_size=1, score_threshold=0.05, iou_threshold=0.4) if len(nms_result) > 0: bbox = [int(round(i)) for i in nms_result[0][0]] elif keypoint.name == 'butt': bbox = [180, 359, 340, 438] elif keypoint.name == 'face': bbox = [204, 137, 302, 220] else: bbox = 4 * [0] write_xml(None, None, None, [bbox])(base_xml_output_path + '/{}.xml'.format(_id))
def main(argv=None): # pylint: disable=unused-argument train_dir = dp.train_dir(FLAGS.train_dir) if tf.gfile.Exists(train_dir): raise Exception('train_dir: {0} exists'.format(train_dir)) tf.gfile.MakeDirs(train_dir) np.random.seed(FLAGS.random_seed) tf.set_random_seed(FLAGS.random_seed) if FLAGS.keypoint == 'face': keypoint = FACE if FLAGS.keypoint == 'butt': keypoint = BUTT train(keypoint, FLAGS.batch_size, FLAGS.random_seed)
def train(region, batch_size, mean_subtract): with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step() with tf.device('/cpu:0'): host_images, host_labels, host_slcs = ImagePreprocessor( 'train_0', region, batch_size, mean_subtract).minibatch() images_shapes = host_images.get_shape() labels_shape = host_labels.get_shape() slcs_shape = host_slcs.get_shape() ### Validation Code ### validation_images, validation_labels, validation_slcs = ImagePreprocessor( 'validate_0', region, batch_size, mean_subtract).minibatch() with tf.device('/gpu:0'): gpu_compute_stage = data_flow_ops.StagingArea( [tf.float32, tf.int64, tf.int64], shapes=[images_shapes, labels_shape, slcs_shape]) # The CPU-to-GPU copy is triggered here. gpu_transfer_op = gpu_compute_stage.put( [host_images, host_labels, host_slcs]) received_values = gpu_compute_stage.get() images, labels, slcs = received_values model = LocalizationModel(region, batch_size) logits = model.inference(images, slcs) loss = loss_func(logits, labels, region) ### Validation Code ### validation_logits = model.inference(validation_images, validation_slcs, validate=True) validation_loss = loss_func(validation_logits, validation_labels, region) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = train_func(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.global_variables()) # Build an initialization operation to run below. init = tf.global_variables_initializer() # Start running operations on the Graph. allow_soft_placement must be set to # True to build towers on GPU, as some of the ops do not have GPU # implementations. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)) sess.run(init) model.load_weights(sess) if FLAGS.checkpoint_dir: saver.restore( sess, tf.train.get_checkpoint_state( dp.train_dir(FLAGS.checkpoint_dir)).model_checkpoint_path) sess.run(gpu_transfer_op) assert model.weights_loaded with open(dp.train_dir(FLAGS.train_dir + '/train_log.txt'), 'w') as f: duration = 0 for step in xrange(FLAGS.max_steps): start_time = time.time() _, _, loss_value = sess.run([train_op, gpu_transfer_op, loss]) duration += time.time() - start_time assert not np.isnan( loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = batch_size examples_per_sec = num_examples_per_step / (duration / 10.0) sec_per_batch = duration / 10.0 format_str = ('%s: step %d, loss = %.8f ' '(%.1f examples/sec; %.3f sec/batch)') log_text = format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch) f.write(log_text + '\n') print(log_text) duration = 0 # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join( dp.train_dir(FLAGS.train_dir), 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) total_loss = 0 num_iter = 30 for _ in range(num_iter): loss_of_predictions = sess.run(validation_loss) total_loss += loss_of_predictions log_text = '%s: ' % datetime.now( ) + 'validation loss = %.5f' % (total_loss / num_iter) f.write(log_text + '\n') f.flush() print(colored(log_text, 'yellow', attrs=['bold']))
from datascience import * import numpy as np import tensorflow as tf from tsa.modeling.models.localization_net import LocalizationModel from tsa.modeling.models.keypoint_crop import crop_and_pad import tsa.utils.data_path as dp from skimage.transform import resize TRAIN_DIR = { 'arm': dp.train_dir('arm_localization'), 'torso': dp.train_dir('torso_localization'), 'thigh': dp.train_dir('thigh_localization'), 'calf': dp.train_dir('calf_localization') } MEAN_SUBTRACT = {'arm': 5.5, 'torso': 23.4, 'thigh': 35.0, 'calf': 8.5} IMAGE_LENGTH = 200 REGION_ZONES = {'arm': [1, 2, 3, 4], 'thigh': [8, 9, 10, 11, 12]} BATCH_SIZE = 1 REGION_GRAPHS = { 'arm': tf.Graph(), 'torso': tf.Graph(), 'thigh': tf.Graph(), 'calf': tf.Graph() } REGION_SESSIONS = {
from datascience import * import numpy as np import tensorflow as tf from tsa.modeling.models.threat_detect_net import ThreatDetectModel, target_to_box from tsa.modeling.models.keypoint_crop import crop_and_pad import tsa.utils.data_path as dp from skimage.transform import resize TRAIN_DIR = { 'arm': dp.train_dir('arm_full'), 'torso': dp.train_dir('torso_full'), 'thigh': dp.train_dir('thigh_full'), 'calf': dp.train_dir('calf_full') } BASE_SIZES = { 'arm': [64, 128], 'torso': [128, 256], 'thigh': [128, 256], 'calf': [128, 128] } MEAN_SUBTRACT = {'arm': 5.5, 'torso': 23.4, 'thigh': 35.0, 'calf': 5.5} BATCH_SIZE = 16 REGION_GRAPHS = { 'arm': tf.Graph(), 'torso': tf.Graph(), 'thigh': tf.Graph(), 'calf': tf.Graph()
def train(keypoint, batch_size, random_seed): with tf.Graph().as_default(): tf.set_random_seed(random_seed) global_step = tf.contrib.framework.get_or_create_global_step() with tf.device('/cpu:0'): host_images, host_bboxes = ImagePreprocessor( 'train_0', keypoint, batch_size).minibatch() images_shapes = host_images.get_shape() bboxes_shape = host_bboxes.get_shape() ### Validation Code ### validation_images, validation_gt_boxes = ImagePreprocessor( 'validate_0', keypoint, batch_size).minibatch() with tf.device('/gpu:0'): gpu_compute_stage = data_flow_ops.StagingArea( [tf.float32, tf.int64], shapes=[images_shapes, bboxes_shape]) # The CPU-to-GPU copy is triggered here. gpu_transfer_op = gpu_compute_stage.put([host_images, host_bboxes]) received_values = gpu_compute_stage.get() images, gt_boxes = received_values[0], received_values[1] model = KeypointDetectModel(batch_size=batch_size, image_dim=(660, 512), anchor_kwargs=keypoint.anchor_kwargs) logits, bboxes = model.inference(images) anchor_gt_box, gt_cls_labels = model.anchor_targets(gt_boxes) loss = loss_func(anchor_gt_box, bboxes, gt_cls_labels, logits, model.anchors) ### Validation Code ### validation_logits, validation_bboxes = model.inference( validation_images, validate=True) validation_anchor_gt_box, validation_gt_cls_labels = model.anchor_targets( validation_gt_boxes) validation_loss = loss_func(validation_anchor_gt_box, validation_bboxes, validation_gt_cls_labels, validation_logits, model.anchors) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = train_func(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.global_variables()) # Build an initialization operation to run below. init = tf.global_variables_initializer() # Start running operations on the Graph. allow_soft_placement must be set to # True to build towers on GPU, as some of the ops do not have GPU # implementations. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)) sess.run(init) model.load_weights(sess) sess.run(gpu_transfer_op) duration = 0 for step in xrange(FLAGS.max_steps): start_time = time.time() _, _, loss_value = sess.run([train_op, gpu_transfer_op, loss]) duration += time.time() - start_time assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = batch_size examples_per_sec = num_examples_per_step / (duration / 10.0) sec_per_batch = duration / 10. format_str = ('%s: step %d, loss = %.8f ' '(%.1f examples/sec; %.3f sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) duration = 0 # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join(dp.train_dir(FLAGS.train_dir), 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) total_loss = 0 num_iter = 30 for _ in range(num_iter): loss_of_predictions = sess.run(validation_loss) total_loss += loss_of_predictions print('%s: ' % datetime.now() + colored('validation loss = %.5f' % (total_loss / num_iter), 'yellow', attrs=['bold']))
def main(argv=None): # pylint: disable=unused-argument train_dir = dp.train_dir(FLAGS.train_dir) if tf.gfile.Exists(train_dir): raise Exception('train_dir: {0} exists'.format(train_dir)) tf.gfile.MakeDirs(train_dir) np.random.seed(FLAGS.random_seed) tf.set_random_seed(FLAGS.random_seed) if FLAGS.keypoint == 'face': keypoint = FACE if FLAGS.keypoint == 'butt': keypoint = BUTT train(keypoint, FLAGS.batch_size, FLAGS.random_seed) if __name__ == '__main__': FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string( 'train_dir', dp.train_dir(), """Directory where to write event logs """ """and checkpoint.""") tf.app.flags.DEFINE_string('keypoint', None, 'face or butt') tf.app.flags.DEFINE_integer('max_steps', 20000, """Number of batches to run.""") tf.app.flags.DEFINE_integer('batch_size', 10, """Batch size""") tf.app.flags.DEFINE_integer('random_seed', 29, """Random seed for np and tf""") tf.app.run()