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

20161120-MLHands-on

ARIYAMA Keiji
November 20, 2016

 20161120-MLHands-on

2016年11月20日にGCPUG Tokushimaで開催した機械学習ハンズオンの資料です。

犬猫の画像を分類する畳み込みニューラルネットワーク(CNN)を構築します。
データセットにはCNNThe Oxford-IIIT Pet Datasetを使います。

ARIYAMA Keiji

November 20, 2016
Tweet

More Decks by ARIYAMA Keiji

Other Decks in Technology

Transcript

  1. <annotation> <folder>OXIIIT</folder> <filename>Abyssinian_1.jpg</filename> <source> <database>OXFORD-IIIT Pet Dataset</database> <annotation>OXIIIT</annotation> <image>flickr</image> </source>

    <size> <width>600</width> <height>400</height> <depth>3</depth> </size> <segmented>0</segmented>  BOOPUBUJPOTYNMT <object> <name>cat</name> <pose>Frontal</pose> <truncated>0</truncated> <occluded>0</occluded> <bndbox> <xmin>333</xmin> <ymin>72</ymin> <xmax>425</xmax> <ymax>158</ymax> </bndbox> <difficult>0</difficult> </object> </annotation> Abyssinian_1.xml
  2. FLAGS = gflags.FLAGS
 gflags.DEFINE_string('image_dir', None, 'ॲཧ͢Δը૾ͷσΟϨΫτϦ')
 gflags.DEFINE_string('xml_dir', None, 'ॲཧ͢ΔXMLͷσΟϨΫτϦ')
 gflags.DEFINE_string('output_dir',

    None, 'ग़ྗ͢ΔσΟϨΫτϦ')  HqBHT $ python crop_roi_image.py \ --image_dir /Users/keiji_ariyama/Downloads/catsanddogs/images \ --xml_dir /Users/keiji_ariyama/Downloads/catsanddogs/annotations/xmls \ --output_dir /Users/keiji_ariyama/Desktop/test
  3. #Image CLASS-ID SPECIES BREED ID #ID: 1:37 Class ids #SPECIES:

    1:Cat 2:Dog #BREED ID: 1-25:Cat 1:12:Dog #All images with 1st letter as captial are cat images #images with small first letter are dog images Abyssinian_196 1 1 1 Abyssinian_197 1 1 1 Abyssinian_19 1 1 1 Abyssinian_1 1 1 1  BOOPUBUJPOTUSBJOWBMUYU Abyssinian_1.xml
  4. # conv1
 with tf.variable_scope('conv1') as scope:
 weights = _get_weights([5, 5,

    3, 32], stddev=0.01)
 conv1 = tf.nn.conv2d(image_node, weights, [1, 1, 1, 1], padding='SAME')
 biases = tf.get_variable('biases', [32],
 initializer=tf.constant_initializer(0.0))
 bias = tf.nn.bias_add(conv1, biases)
 conv1 = tf.nn.relu(bias, name=scope.name)
  ৞ࠐΈʢ$POWPMVUJPOʣ૚
  5. pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2,

    1],
 padding='SAME', name='pool1')
  ϓʔϦϯάʢ1PPMJOHʣ૚
  6. reshape = tf.reshape(pool1, [batch_size, -1])
 dim = reshape.get_shape()[1].value
 
 #

    fc2
 with tf.variable_scope('fc2') as scope:
 weights = _get_weights([dim, 384], stddev=0.04)
 biases = _get_biases([384], value=0.1)
 fc2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(reshape, weights), biases),
 name=scope.name)
  શ݁߹ʢ'VMMZDPOOFDUFEʣ૚
  7. with tf.variable_scope('output') as scope:
 weights = _get_weights(shape=[384, 2], stddev=1 /

    384.0)
 biases = _get_biases([2], value=0.0)
 logits = tf.add(tf.matmul(fc2, weights), biases, name='logits')
  ग़ྗ૚
  8. def __loss(logits, label):
 labels = tf.cast(label, tf.int64)
 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(


    logits, labels, name='cross_entropy_per_example')
 cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
 return cross_entropy_mean
  ޡࠩؔ਺
  9. global_step = tf.Variable(0, trainable=False)
 loss = __loss(logits, specie_batch)
 train_op =

    tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
 loss, global_step)
  ֶशʢ܇࿅ʣΞϧΰϦζϜ
  10. # fc2
 with tf.variable_scope('fc2') as scope:
 weights = _get_weights([dim, 384],

    stddev=0.04)
 biases = _get_biases([384], value=0.1)
 fc2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(reshape, weights), biases),
 name=scope.name)
  ύϥϝʔλʔ
  11. # fc2
 with tf.variable_scope('fc2') as scope:
 weights = _get_weights([dim, 384],

    stddev=0.04)
 biases = _get_biases([384], value=0.1)
 fc2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(reshape, weights), biases),
 name=scope.name)
  ׆ੑԽؔ਺
  12. saver = tf.train.Saver(tf.all_variables())
 
 with tf.Session() as sess:
 sess.run(tf.initialize_all_variables())
 step

    = 0
 while step < FLAGS.max_steps:
 _, step, loss_value = sess.run([train_op, global_step, loss])
 
 if step % 100 == 0:
 print('Step: %d, loss: %.4f' % (step, loss_value))
 saver.save(sess, os.path.join(FLAGS.checkpoint_dir, 'cad_checkpoint'), global_step=step)  4BWFSʹΑΔύϥϝʔλʔͷอଘ
  13. logits = model.inference(image_batch, batch_size=FLAGS.batch_size)
 top_k_op = tf.nn.in_top_k(logits, specie_batch, 1)
 for

    step in range(num_iter): predictions = sess.run(top_k_op)
 true_count += np.sum(predictions)
  ݕূ
  14. $ gcloud auth $ gcloud beta ml init-project Cloud ML

    needs to add its service accounts to your project (catsanddogs-149703) as Editors. This will enable Cloud Machine Learning to access resources in your project when running your training and prediction jobs. Do you want to continue (Y/n)?  $MPVE.-ͷϓϩδΣΫτΛॳظԽ
  15. $ PROJECT_ID=$(gcloud config list project --format "value(core.project)") $ BUCKET_NAME="cats_and_dogs" $

    gsutil mb -l us-central1 gs://$BUCKET_NAME  $MPVE.-༻ͷόέοτΛ࡞੒ ※ usͷΑ͏ͳMulti-regionalͰ͸ͳ͘RegionalͰࢦఆ͢Δඞཁ͕͋Δ
 https://cloud.google.com/ml/docs/how-tos/getting-set-up
  16. $ gcloud beta ml jobs submit training cat_and_dogs \ --package-path=/Users/keiji_ariyama/Developments/catsanddogs/src

    \ --module-name=src.train \ --staging-bucket=gs://cats_and_dogs \ --region=us-central1 \ -- \ --train_dir gs://cats_and_dogs/cad_train \ --checkpoint_dir gs://cats_and_dogs/cad_checkpoint \ --max_steps 100  $MPVE.-༻ʹδϣϒΛొ࿥͢Δ