1. harmegiddo

    No comment

    harmegiddo
Changes in body
Source | HTML | Preview
@@ -1,41 +1,248 @@
# 0.0. 概要
最強のSemantic SegmentationのDeep lab v3 pulsを試してみる。
https://github.com/tensorflow/models/tree/master/research/deeplab
# 0.1. Installation
これを読めばよい
https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/installation.md
取りあえずCudaは9.0以上じゃないと動かないらしいので
Tensorflow 1.8, Cuda 9.0, CUDNN 7.0の環境で動かす。
```
-# gitから持ってくる
git clone https://github.com/tensorflow/models.git
cd models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
cd deeplab/
python model_test.py
sh local_test.sh
```
+私は異なるGPUを積んでいるので
+
+```
++-----------------------------------------------------------------------------+
+| NVIDIA-SMI 384.130 Driver Version: 384.130 |
+|-------------------------------+----------------------+----------------------+
+| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
+|===============================+======================+======================|
+| 0 Quadro 4000 Off | 00000000:03:00.0 On | N/A |
+| 40% 53C P12 N/A / N/A | 237MiB / 1977MiB | 0% Default |
++-------------------------------+----------------------+----------------------+
+| 1 Quadro 4000 Off | 00000000:04:00.0 Off | N/A |
+| 40% 51C P12 N/A / N/A | 137MiB / 1984MiB | 0% Default |
++-------------------------------+----------------------+----------------------+
+| 2 GeForce GTX 108... Off | 00000000:22:00.0 Off | N/A |
+| 44% 38C P8 11W / 250W | 2MiB / 11172MiB | 0% Default |
++-------------------------------+----------------------+----------------------+
+
++-----------------------------------------------------------------------------+
+| Processes: GPU Memory |
+| GPU PID Type Process name Usage |
+|=============================================================================|
+| No running processes found |
++-----------------------------------------------------------------------------+
+```
+
+こんな感じにプログラムを書き換えないと動かなかったです。
+
+```
+# Copyright 2018 The TensorFlow Authors All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ==============================================================================
+
+"""Tests for DeepLab model and some helper functions."""
+
+import tensorflow as tf
+
+from deeplab import common
+from deeplab import model
+
+config = tf.ConfigProto(
+ gpu_options=tf.GPUOptions(
+ visible_device_list="1, 2"
+ )
+)
+
+class DeeplabModelTest(tf.test.TestCase):
+
+ def testScaleDimensionOutput(self):
+ self.assertEqual(161, model.scale_dimension(321, 0.5))
+ self.assertEqual(193, model.scale_dimension(321, 0.6))
+ self.assertEqual(241, model.scale_dimension(321, 0.75))
+
+ def testWrongDeepLabVariant(self):
+ model_options = common.ModelOptions([])._replace(
+ model_variant='no_such_variant')
+ with self.assertRaises(ValueError):
+ model._get_logits(images=[], model_options=model_options)
+
+ def testBuildDeepLabv2(self):
+ batch_size = 2
+ crop_size = [41, 41]
+
+ # Test with two image_pyramids.
+ image_pyramids = [[1], [0.5, 1]]
+
+ # Test two model variants.
+ model_variants = ['xception_65', 'mobilenet_v2']
+
+ # Test with two output_types.
+ outputs_to_num_classes = {'semantic': 3,
+ 'direction': 2}
+
+ expected_endpoints = [['merged_logits'],
+ ['merged_logits',
+ 'logits_0.50',
+ 'logits_1.00']]
+ expected_num_logits = [1, 3]
+
+ for model_variant in model_variants:
+ model_options = common.ModelOptions(outputs_to_num_classes)._replace(
+ add_image_level_feature=False,
+ aspp_with_batch_norm=False,
+ aspp_with_separable_conv=False,
+ model_variant=model_variant)
+
+ for i, image_pyramid in enumerate(image_pyramids):
+ g = tf.Graph()
+ with g.as_default():
+ with self.test_session(graph=g, config=config):
+ inputs = tf.random_uniform(
+ (batch_size, crop_size[0], crop_size[1], 3))
+ outputs_to_scales_to_logits = model.multi_scale_logits(
+ inputs, model_options, image_pyramid=image_pyramid)
+
+ # Check computed results for each output type.
+ for output in outputs_to_num_classes:
+ scales_to_logits = outputs_to_scales_to_logits[output]
+ self.assertListEqual(sorted(scales_to_logits.keys()),
+ sorted(expected_endpoints[i]))
+
+ # Expected number of logits = len(image_pyramid) + 1, since the
+ # last logits is merged from all the scales.
+ self.assertEqual(len(scales_to_logits), expected_num_logits[i])
+
+ def testForwardpassDeepLabv3plus(self):
+ crop_size = [33, 33]
+ outputs_to_num_classes = {'semantic': 3}
+
+ model_options = common.ModelOptions(
+ outputs_to_num_classes,
+ crop_size,
+ output_stride=16
+ )._replace(
+ add_image_level_feature=True,
+ aspp_with_batch_norm=True,
+ logits_kernel_size=1,
+ model_variant='mobilenet_v2') # Employ MobileNetv2 for fast test.
+
+ g = tf.Graph()
+ with g.as_default():
+ with self.test_session(graph=g, config=config) as sess:
+ inputs = tf.random_uniform(
+ (1, crop_size[0], crop_size[1], 3))
+ outputs_to_scales_to_logits = model.multi_scale_logits(
+ inputs,
+ model_options,
+ image_pyramid=[1.0])
+
+ sess.run(tf.global_variables_initializer())
+ outputs_to_scales_to_logits = sess.run(outputs_to_scales_to_logits)
+
+ # Check computed results for each output type.
+ for output in outputs_to_num_classes:
+ scales_to_logits = outputs_to_scales_to_logits[output]
+ # Expect only one output.
+ self.assertEquals(len(scales_to_logits), 1)
+ for logits in scales_to_logits.values():
+ self.assertTrue(logits.any())
+
+
+if __name__ == '__main__':
+ tf.test.main()
+
+```
+
# 0.2. Training
これを読めばよい
-https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/installation.md
+https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/pascal.md
+取りあえずPASCAL VOC 2012で動かす。
-取りあえずCudaは9.0以上じゃないと動かないらしいので
-Tensorflow 1.8, Cuda 9.0, CUDNN 7.0の環境で動かす
+これでPASCALをダウンロードできる
```
-# gitから持ってくる
-git clone https://github.com/tensorflow/models.git
-cd models/research/
-export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
-cd deeplab/
-python model_test.py
-sh local_test.sh
+cd models/research/deeplab/datasets
+sh download_and_convert_voc2012.sh
```
+
+こちらのURLに
+https://github.com/rishizek/tensorflow-deeplab-v3
+以下のように書かれている。
+
+```
+Training
+For training model, you first need to convert original data to the TensorFlow TFRecord format. This enables to accelerate training seep.
+
+python create_pascal_tf_record.py --data_dir DATA_DIR \
+ --image_data_dir IMAGE_DATA_DIR \
+ --label_data_dir LABEL_DATA_DIR
+```
+多分、shell scriptでtf_recordに変換してくれているのだろう。
+
+
+フォルダ構成
+
+```
++ datasets
+ + pascal_voc_seg
+ + VOCdevkit
+ + VOC2012
+ + JPEGImages
+ + SegmentationClass
+ + tfrecord
+ + exp
+ + train_on_train_set
+ + train
+ + eval
+ + vis
+```
+
+trainingを実行
+
+```
+cd models/research/
+python deeplab/train.py \
+ --logtostderr \
+ --training_number_of_steps=30000 \
+ --train_split="train" \
+ --model_variant="xception_65" \
+ --atrous_rates=6 \
+ --atrous_rates=12 \
+ --atrous_rates=18 \
+ --output_stride=16 \
+ --decoder_output_stride=4 \
+ --train_crop_size=513 \
+ --train_crop_size=513 \
+ --train_batch_size=1 \
+ --dataset="pascal_voc_seg" \
+ --tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \
+ --train_logdir=${PATH_TO_TRAIN_DIR} \
+ --dataset_dir=${PATH_TO_DATASET}
+```