Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20161119-meganeco

ARIYAMA Keiji
November 19, 2016

 20161119-meganeco

「TensorFlowで趣味の画像収集サーバーを作る 11月号」
大量の画像からデータセットを作成するために構築したワークフローについて解説します。

ARIYAMA Keiji

November 19, 2016
Tweet

More Decks by ARIYAMA Keiji

Other Decks in Technology

Transcript

  1. C-LIS CO., LTD.  (%(,PCFʢʣ (%(,ZPUPʢʣ ೔ຊ"OESPJEͷձ"OESPJE#B[BBSBOE$POGFSFODFʢʣ (%(,PCFʢ ڞ࠵($16#0TBLBʣ 5FOTPS'MPXษڧձʢ̏ʣʢ

    ڞ࠵೔ຊ"OESPJEͷձʣ 5FOTPS'MPXษڧձʢ̐ʣʢʣ ͳʹΘ5&$)ಓʢʣ 5FOTPS'MPXษڧձʢʣʢʣ ೔ຊ"OESPJEͷձ"OESPJE#B[BBSBOE$POGFSFODFʢʣ ˡ/FXʂ
  2.  ݱࡏͷߏ੒ Downloader Face Detection Megane Detection API TensorFlow ֬ೝɾमਖ਼

    ೝࣝ݁Ռ JSON ܇࿅ λΠϜϥΠϯ ϝσΟΞ σʔληοτ ܇࿅
  3.  %PXOMPBEFS Downloader Face Detection Megane Detection API TensorFlow ֬ೝɾमਖ਼

    ೝࣝ݁Ռ JSON ܇࿅ λΠϜϥΠϯ ϝσΟΞ σʔληοτ ܇࿅
  4.  'BDF%FUFDUJPO Downloader Face Detection Megane Detection API TensorFlow ֬ೝɾमਖ਼

    ೝࣝ݁Ռ JSON ܇࿅ λΠϜϥΠϯ ϝσΟΞ σʔληοτ ܇࿅
  5.  إೝࣝ༻ͷϞσϧ DPO YY DPO YY GD  QPPM Y

    DPO YY DPO YY QPPM Y GD  PVUQVU  MSO MSO Y
  6.  .FHBOF%FUFDUJPO Downloader Face Detection Megane Detection API TensorFlow ֬ೝɾमਖ਼

    ೝࣝ݁Ռ JSON ܇࿅ λΠϜϥΠϯ ϝσΟΞ σʔληοτ ܇࿅
  7.  ؟ڸೝࣝ༻ͷϞσϧ DPO YY DPO YY GD  QPPM Y

    DPO YY DPO YY QPPM Y GD  PVUQVU  MSO MSO Y
  8.  ೝࣝ݁Ռ Downloader Face Detection Megane Detection API TensorFlow ֬ೝɾमਖ਼

    ೝࣝ݁Ռ JSON ܇࿅ λΠϜϥΠϯ ϝσΟΞ σʔληοτ ܇࿅
  9. { "generator": "Megane Co", "file_name": "neyuki_rei-CZY6tnUUYAAJVe6.jpg", "created_at": "2016-11-18T10:19:04.879281", "detected_faces": {

    "mode": "selective_search", "model_version": "20161118003546" "configurations": { }, "regions": [ { "rect": { "top": 59.0, "bottom": 175.0, "left": 85.0, "right": 176.0 }, "label": 2, "probability": 0.9962707757949829 },  ೝࣝ݁Ռͷ+40/ { "rect": { "top": 220.0, "bottom": 274.0, "left": 455.0, "right": 524.0 }, "label": 1, "probability": 0.9997575879096985 }, { "rect": { "top": 196.0, "bottom": 282.0, "left": 244.0, "right": 330.0 }, "label": 1, "probability": 1.0 }
  10.  ग़ྗ૚Λ௥Ճ͢Δ DPO YY DPO YY GD  QPPM Y

    DPO YY DPO YY QPPM Y GD  PVUQVU  MSO MSO Y φΠε؟ڸ່ͬʂ ؟ڸΛ͔͚ͨঁࢠ Human
  11. def inference(image_node, keep_prob, batch_size=1):
 conv1_1 = _conv('conv1_1', image_node, [3, 3,

    3, 64], 64)
 conv1_2 = _conv('conv1_2', conv1_1, [3, 3, 64, 64], 64)
 pool1 = _pool('pool1', conv1_2, [1, 2, 2, 1])
 
 conv2_1 = _conv('conv2_1', pool1, [3, 3, 64, 128], 128)
 conv2_2 = _conv('conv2_2', conv2_1, [3, 3, 128, 128], 128)
 pool2 = _pool('pool2', conv2_2, [1, 2, 2, 1])
 
 conv3_1 = _conv('conv3_1', pool2, [3, 3, 128, 256], 256)
 conv3_2 = _conv('conv3_2', conv3_1, [3, 3, 256, 256], 256)
 pool3 = _pool('pool3', conv3_2, [1, 2, 2, 1])
  ܇࿅༻ͷάϥϑ reshape = tf.reshape(pool3, [batch_size, -1])
 dim = reshape.get_shape()[1].value
 
 fc4 = _fc('fc4', reshape, [dim, 512], 512, keep_prob)
 fc5 = _fc('fc5', fc4, [512, 256], 256, keep_prob)
 
 # output
 with tf.variable_scope('output') as scope:
 weights = _get_weights(shape=[256, NUM_CLASSES], stddev=1 / 256.0)
 biases = _get_biases([NUM_CLASSES], value=0.0)
 logits = tf.add(tf.matmul(fc5, weights), biases, name='logits')
 
 return logits

  12. def _fc(name, input_layer, weights_shape, biases_size, keep_prob):
 with tf.variable_scope(name) as scope:


    weights = _get_weights(shape=weights_shape, stddev=0.05)
 biases = _get_biases([biases_size], value=0.1)
 fc = tf.nn.relu(tf.matmul(input_layer, weights) + biases, name=scope.name)
 fc = tf.nn.dropout(fc, keep_prob)
 return fc
  ܇࿅༻ͷάϥϑ http://goo.gl/forms/2IsX177eJ3CpNKng1 model_vgg_lite_minibatch.py
  13. def _fc(name, input_layer, weights_shape, biases_size, keep_prob):
 with tf.variable_scope(name) as scope:


    weights = _get_weights(shape=weights_shape, stddev=0.05)
 biases = _get_biases([biases_size], value=0.1)
 fc = tf.nn.relu(tf.matmul(input_layer, weights) + biases, name=scope.name)
 fc = tf.nn.dropout(fc, keep_prob)
 return fc
  ධՁ༻ͷάϥϑ http://goo.gl/forms/2IsX177eJ3CpNKng1 model_vgg_lite_minibatch_no_dropout.py
  14. # coding: UTF-8
 from __future__ import absolute_import
 from __future__ import

    division
 from __future__ import print_function
 
 import tensorflow as tf
 
 from tensorflow.python.framework import graph_util
 
 import model_vgg_lite_minibatch_no_dropout as model
 
 FLAGS = tf.app.flags.FLAGS
 tf.app.flags.DEFINE_string('output_graph_path', None, "ग़ྗ͢ΔάϥϑͷσΟϨΫτϦ")
 tf.app.flags.DEFINE_string('checkpoint_dir', './checkpoints', "νΣοΫϙΠϯτͷσΟϨΫτϦ")
 tf.app.flags.DEFINE_integer('batch_size', 1, "όοναΠζ")
 
  15. def main(argv=None):
 # eval༻ͷΤϯτϦϙΠϯτ
 input_image = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, 32, 32,

    3], name='input_image')
 logits = model.inference(input_image, batch_size=FLAGS.batch_size)
 
 saver = tf.train.Saver()
 
 with tf.Session() as sess: 
 # ύϥϝʔλʔͷಡΈࠐΈ
 checkpoint = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
 if checkpoint and checkpoint.model_checkpoint_path:
 saver.restore(sees, checkpoint.model_checkpoint_path)
 # άϥϑʴύϥϝʔλʔͷΤΫεϙʔτ
 constant_graph_def = graph_util.convert_variables_to_constants(
 sess, sess.graph_def, ["output/logits"])
 with tf.gfile.FastGFile(FLAGS.output_graph_path, 'wb') as f:
 f.write(constant_graph_def.SerializeToString()) 
  16. $ python3 convert_to_graph.py --checkpoint_dir ./checkpoints \ --output_graph_path ./graphs/exported_graph.pb \ --batch_size

    1 Converted 18 variables to const ops.  ධՁ༻άϥϑʴύϥϝʔλʔͷΤΫεϙʔτ
  17. labels = tf.placeholder(tf.int32, shape=[FLAGS.batch_size], name='label')
 input_image = tf.get_default_graph().get_tensor_by_name('input_image:0')
 logits =

    tf.get_default_graph().get_tensor_by_name('output/logits:0')
 
 top_k_op = tf.nn.in_top_k(logits, labels, 1)
  ΦϖϨʔγϣϯͷऔಘͱάϥϑͷߏங
  18. E tensorflow/core/common_runtime/executor.cc:334] Executor failed to create kernel. Invalid argument: NodeDef

    mentions attr 'Tshape' not in Op<name=Reshape; signature=tensor:T, shape:int32 -> output:T; attr=T:type>; NodeDef: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"] (pool3, Reshape/shape) [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/ replica:0/task:0/cpu:0"](pool3, Reshape/shape)]] E tensorflow/core/client/tensor_c_api.cc:485] NodeDef mentions attr 'Tshape' not in Op<name=Reshape; signature=tensor:T, shape:int32 -> output:T; attr=T:type>; NodeDef: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task: 0/cpu:0"](pool3, Reshape/shape) [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/ replica:0/task:0/cpu:0"](pool3, Reshape/shape)]]  ಈ͖·ͤΜ
  19. ['input_image', 'conv1_1/weights', 'conv1_1/weights/read', 'conv1_1/Conv2D', 'conv1_1/biases', 'conv1_1/biases/read', 'conv1_1/BiasAdd', 'conv1_1/conv1_1', 'conv1_2/weights', 'conv1_2/weights/read',

    'conv1_2/Conv2D', 'conv1_2/biases', 'conv1_2/biases/read', 'conv1_2/BiasAdd', 'conv1_2/conv1_2', 'pool1',  ߏ੒͢Δϊʔυ 'conv2_1/weights', 'conv2_1/weights/read', 'conv2_1/Conv2D', 'conv2_1/biases', 'conv2_1/biases/read', 'conv2_1/BiasAdd', 'conv2_1/conv2_1', 'conv2_2/weights', 'conv2_2/weights/read', 'conv2_2/Conv2D', 'conv2_2/biases', 'conv2_2/biases/read', 'conv2_2/BiasAdd', 'conv2_2/conv2_2', 'pool2', 'conv3_1/weights', 'conv3_1/weights/read', 'conv3_1/Conv2D', 'conv3_1/biases', 'conv3_1/biases/read', 'conv3_1/BiasAdd', 'conv3_1/conv3_1', 'conv3_2/weights', 'conv3_2/weights/read', 'conv3_2/Conv2D', 'conv3_2/biases', 'conv3_2/biases/read', 'conv3_2/BiasAdd', 'conv3_2/conv3_2', 'pool3', 'Reshape/shape', 'Reshape', 'fc4/weights', 'fc4/weights/read', 'fc4/biases', 'fc4/biases/read', 'fc4/MatMul', 'fc4/add', 'fc4/fc4', 'fc5/weights', 'fc5/weights/read', 'fc5/biases', 'fc5/biases/read', 'fc5/MatMul', 'fc5/add', 'fc5/fc5', 'output/weights', 'output/weights/read', 'output/biases', 'output/biases/read', 'output/MatMul', 'output/logits']
  20. ['train_image', 'input_image', 'conv1_1/weights', 'conv1_1/weights/read', 'conv1_1/Conv2D', 'conv1_1/biases', 'conv1_1/biases/read', 'conv1_1/BiasAdd', 'conv1_1/conv1_1', 'conv1_2/weights',

    'conv1_2/weights/read', 'conv1_2/Conv2D', 'conv1_2/biases', 'conv1_2/biases/read', 'conv1_2/BiasAdd', 'conv1_2/conv1_2', 'pool1',  ܇࿅தʹΤΫεϙʔτͨ͠΋ͷͱൺֱ 'conv2_1/weights', 'conv2_1/weights/read', 'conv2_1/Conv2D', 'conv2_1/biases', 'conv2_1/biases/read', 'conv2_1/BiasAdd', 'conv2_1/conv2_1', 'conv2_2/weights', 'conv2_2/weights/read', 'conv2_2/Conv2D', 'conv2_2/biases', 'conv2_2/biases/read', 'conv2_2/BiasAdd', 'conv2_2/conv2_2', 'pool2', 'conv3_1/weights', 'conv3_1/weights/read', 'conv3_1/Conv2D', 'conv3_1/biases', 'conv3_1/biases/read', 'conv3_1/BiasAdd', 'conv3_1/conv3_1', 'conv3_2/weights', 'conv3_2/weights/read', 'conv3_2/Conv2D', 'conv3_2/biases', 'conv3_2/biases/read', 'conv3_2/BiasAdd', 'conv3_2/conv3_2', 'pool3', 'Reshape/shape', 'Reshape', 'fc4/weights', 'fc4/weights/read', 'fc4/biases', 'fc4/biases/read', 'fc4/MatMul', 'fc4/add', 'fc4/fc4', 'fc5/weights', 'fc5/weights/read', 'fc5/biases', 'fc5/biases/read', 'fc5/MatMul', 'fc5/add', 'fc5/fc5', 'output/weights', 'output/weights/read', 'output/biases', 'output/biases/read', 'output/MatMul', 'output/logits']