2018년 2월 6일 화요일

tensorflow 관련 스크립트 실행 위치


여러개의 TFRecord 사용 법.
https://github.com/tensorflow/models/issues/3031

This issue is closed since I found answers in the code
The object detection API use parallel reader to import your dataset, here are the comments by the developer
Usage:
      data_sources = ['path_to/train*']
      key, value = parallel_read(data_sources, tf.CSVReader, num_readers=4)

  Args:
    data_sources: a list/tuple of files or the location of the data, i.e.
      /path/to/train@128, /path/to/train* or /tmp/.../train*
So basically you can define a list of input path as @byungjae89 mentioned above, or simply provide the input directory like
input_path: my_dataset/train/*
The reader will read the entire folder for you.




http://songheqi.me/2017/05/23/Custom-Image-Classify-with-Tensorflow/


TFRecord
Tensorflow's inception model has a file build_image_data.py that can accomplish the same thing with the assumption that each subdirectory represents a label.


object_dectection 폴더의 부모 폴더에서 실행해야 함.

Adesh commented on Jul 11, 2017

if you are running object_detection.
go to 'object_detection' dir and run your code! 'util' dir is within object_detection

/work/tensorflow/models/research/data$ python data/generate_tfrecord.py --csv_input=data/data/train_labels.csv  --output_path=data/data/train.record


chulminson@chulminson-Z71RH-AD7501E:/work/tensorflow/models/research/data$ ls -laR
.:
total 24
drwxrwxr-x  4 chulminson chulminson 4096  2월  7 15:39 .
drwxrwxr-x 44 chulminson chulminson 4096  2월  7 09:59 ..
drwxrwxr-x  2 chulminson chulminson 4096  2월  7 15:39 data
-rw-rw-r--  1 chulminson chulminson 3381  2월  7 15:39 generate_tfrecord.py
drwxrwxr-x  2 chulminson chulminson 4096  2월  7 15:39 images
-rw-rw-r--  1 chulminson chulminson 1171  2월  7 08:36 xml_to_csv.py

./data:
total 216
drwxrwxr-x 2 chulminson chulminson   4096  2월  7 15:39 .
drwxrwxr-x 4 chulminson chulminson   4096  2월  7 15:39 ..
-rw-rw-r-- 1 chulminson chulminson    186  2월  7 15:24 test_labels.csv
-rw-rw-r-- 1 chulminson chulminson  24441  2월  7 15:39 test.record
-rw-rw-r-- 1 chulminson chulminson   1263  2월  7 15:24 train_labels.csv
-rw-rw-r-- 1 chulminson chulminson 178030  2월  7 15:39 train.record

./images:
total 444
drwxrwxr-x 2 chulminson chulminson  4096  2월  7 15:39 .
drwxrwxr-x 4 chulminson chulminson  4096  2월  7 15:39 ..
-rw-rw-r-- 1 chulminson chulminson  1032  2월  6 10:27 a10.png
-rw-rw-r-- 1 chulminson chulminson   509  2월  6 16:48 a10.xml
-rw-rw-r-- 1 chulminson chulminson  2472  2월  6 10:27 a11.png
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:48 a11.xml
-rw-rw-r-- 1 chulminson chulminson  3194  2월  6 10:27 a12.png
-rw-rw-r-- 1 chulminson chulminson   510  2월  6 16:48 a12.xml
-rw-rw-r-- 1 chulminson chulminson   625  2월  6 10:28 a13.png
-rw-rw-r-- 1 chulminson chulminson   510  2월  6 16:49 a13.xml
-rw-rw-r-- 1 chulminson chulminson  1571  2월  6 10:28 a14.png
-rw-rw-r-- 1 chulminson chulminson   510  2월  6 16:49 a14.xml
-rw-rw-r-- 1 chulminson chulminson  5767  2월  6 10:27 a15.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:49 a15.xml
-rw-rw-r-- 1 chulminson chulminson  1711  2월  6 10:27 a16.png
-rw-rw-r-- 1 chulminson chulminson   510  2월  6 16:49 a16.xml
-rw-rw-r-- 1 chulminson chulminson  9582  2월  6 10:27 a17.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:50 a17.xml
-rw-rw-r-- 1 chulminson chulminson 11717  2월  6 10:27 a18.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:50 a18.xml
-rw-rw-r-- 1 chulminson chulminson  4631  2월  6 10:27 a19.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:50 a19.xml
-rw-rw-r-- 1 chulminson chulminson  6609  2월  6 10:27 a1.jpg
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:47 a1.xml
-rw-rw-r-- 1 chulminson chulminson 10786  2월  6 16:46 a20.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:51 a20.xml
-rw-rw-r-- 1 chulminson chulminson  2467  2월  6 10:27 a2.png
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:50 a2.xml
-rw-rw-r-- 1 chulminson chulminson  9816  2월  6 10:28 a3.jpg
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:51 a3.xml
-rw-rw-r-- 1 chulminson chulminson  1434  2월  6 10:27 a4.png
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:51 a4.xml
-rw-rw-r-- 1 chulminson chulminson  5883  2월  6 10:28 a5.jpg
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:51 a5.xml
-rw-rw-r-- 1 chulminson chulminson  2616  2월  6 10:27 a6.png
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:52 a6.xml
-rw-rw-r-- 1 chulminson chulminson  5599  2월  6 10:28 a7.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:52 a7.xml
-rw-rw-r-- 1 chulminson chulminson  1190  2월  6 10:27 a8.png
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:52 a8.xml
-rw-rw-r-- 1 chulminson chulminson  3775  2월  6 10:28 a9.jpg
-rw-rw-r-- 1 chulminson chulminson   508  2월  6 16:53 a9.xml
-rw-rw-r-- 1 chulminson chulminson  4010  2월  6 10:29 p10.png
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:54 p10.xml
-rw-rw-r-- 1 chulminson chulminson  6239  2월  6 10:29 p11.jpg
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:54 p11.xml
-rw-rw-r-- 1 chulminson chulminson  2538  2월  6 10:29 p12.png
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:54 p12.xml
-rw-rw-r-- 1 chulminson chulminson  4394  2월  6 10:29 p13.jpg
-rw-rw-r-- 1 chulminson chulminson   506  2월  6 16:54 p13.xml
-rw-rw-r-- 1 chulminson chulminson   941  2월  6 10:29 p14.png
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:55 p14.xml
-rw-rw-r-- 1 chulminson chulminson  4616  2월  6 10:30 p15.jpg
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:55 p15.xml
-rw-rw-r-- 1 chulminson chulminson  1431  2월  6 10:31 p16.png
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:55 p16.xml
-rw-rw-r-- 1 chulminson chulminson 10310  2월  6 10:30 p17.jpg
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:55 p17.xml
-rw-rw-r-- 1 chulminson chulminson  4910  2월  6 10:31 p18.jpg
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:56 p18.xml
-rw-rw-r-- 1 chulminson chulminson  3717  2월  6 10:31 p19.jpg
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:56 p19.xml
-rw-rw-r-- 1 chulminson chulminson  6228  2월  6 10:25 p1.jpg
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:54 p1.xml
-rw-rw-r-- 1 chulminson chulminson  3717  2월  6 10:31 p20.jpg
-rw-rw-r-- 1 chulminson chulminson   507  2월  6 16:56 p20.xml
-rw-rw-r-- 1 chulminson chulminson  6130  2월  6 10:25 p2.png
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:56 p2.xml
-rw-rw-r-- 1 chulminson chulminson  5332  2월  6 10:25 p3.jpg
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:56 p3.xml
-rw-rw-r-- 1 chulminson chulminson  6130  2월  6 10:25 p4.png
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:57 p4.xml
-rw-rw-r-- 1 chulminson chulminson  4744  2월  6 10:26 p5.jpg
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:57 p5.xml
-rw-rw-r-- 1 chulminson chulminson   687  2월  6 10:26 p6.png
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:57 p6.xml
-rw-rw-r-- 1 chulminson chulminson  4999  2월  6 10:26 p7.jpg
-rw-rw-r-- 1 chulminson chulminson   505  2월  6 16:57 p7.xml
-rw-rw-r-- 1 chulminson chulminson  4633  2월  6 10:26 p8.png
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:58 p8.xml
-rw-rw-r-- 1 chulminson chulminson  8425  2월  6 10:26 p9.jpg
-rw-rw-r-- 1 chulminson chulminson   503  2월  6 16:58 p9.xml



python object_detection/export_inference_graph.py \
    --input_type image_tensor \
    --pipeline_config_path object_detection/training/pipeline.config \
    --trained_checkpoint_prefix object_detection/training/model.ckpt-1456 \
    --output_directory play_pause_graph


{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Object Detection Demo\n",
    "Welcome to the object detection inference walkthrough!  This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import six.moves.urllib as urllib\n",
    "import sys\n",
    "import tarfile\n",
    "import tensorflow as tf\n",
    "import zipfile\n",
    "\n",
    "from collections import defaultdict\n",
    "from io import StringIO\n",
    "from matplotlib import pyplot as plt\n",
    "from PIL import Image\n",
    "\n",
    "if tf.__version__ < '1.4.0':\n",
    "  raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Env setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is needed to display the images.\n",
    "%matplotlib inline\n",
    "\n",
    "# This is needed since the notebook is stored in the object_detection folder.\n",
    "sys.path.append(\"..\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Object detection imports\n",
    "Here are the imports from the object detection module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import label_map_util\n",
    "\n",
    "from utils import visualization_utils as vis_util"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model preparation "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables\n",
    "\n",
    "Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  \n",
    "\n",
    "By default we use an \"SSD with Mobilenet\" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# What model to download.\n",
    "MODEL_NAME = 'play_pause_graph'\n",
    "\n",
    "# Path to frozen detection graph. This is the actual model that is used for the object detection.\n",
    "PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'\n",
    "\n",
    "# List of the strings that is used to add correct label for each box.\n",
    "PATH_TO_LABELS = os.path.join('training', 'play_pause_dectection.pbtxt')\n",
    "\n",
    "NUM_CLASSES = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load a (frozen) Tensorflow model into memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "detection_graph = tf.Graph()\n",
    "with detection_graph.as_default():\n",
    "  od_graph_def = tf.GraphDef()\n",
    "  with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:\n",
    "    serialized_graph = fid.read()\n",
    "    od_graph_def.ParseFromString(serialized_graph)\n",
    "    tf.import_graph_def(od_graph_def, name='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading label map\n",
    "Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_map = label_map_util.load_labelmap(PATH_TO_LABELS)\n",
    "categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)\n",
    "category_index = label_map_util.create_category_index(categories)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helper code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_image_into_numpy_array(image):\n",
    "  (im_width, im_height) = image.size\n",
    "  return np.array(image.getdata()).reshape(\n",
    "      (im_height, im_width, 3)).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For the sake of simplicity we will use only 2 images:\n",
    "# image1.jpg\n",
    "# image2.jpg\n",
    "# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.\n",
    "PATH_TO_TEST_IMAGES_DIR = 'test_images'\n",
    "TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(7, 7) ]\n",
    "\n",
    "# Size, in inches, of the output images.\n",
    "IMAGE_SIZE = (12, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with detection_graph.as_default():\n",
    "  with tf.Session(graph=detection_graph) as sess:\n",
    "    # Definite input and output Tensors for detection_graph\n",
    "    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')\n",
    "    # Each box represents a part of the image where a particular object was detected.\n",
    "    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')\n",
    "    # Each score represent how level of confidence for each of the objects.\n",
    "    # Score is shown on the result image, together with the class label.\n",
    "    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')\n",
    "    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')\n",
    "    num_detections = detection_graph.get_tensor_by_name('num_detections:0')\n",
    "    for image_path in TEST_IMAGE_PATHS:\n",
    "      image = Image.open(image_path)\n",
    "      # the array based representation of the image will be used later in order to prepare the\n",
    "      # result image with boxes and labels on it.\n",
    "      image_np = load_image_into_numpy_array(image)\n",
    "      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n",
    "      image_np_expanded = np.expand_dims(image_np, axis=0)\n",
    "      # Actual detection.\n",
    "      (boxes, scores, classes, num) = sess.run(\n",
    "          [detection_boxes, detection_scores, detection_classes, num_detections],\n",
    "          feed_dict={image_tensor: image_np_expanded})\n",
    "      # Visualization of the results of a detection.\n",
    "      vis_util.visualize_boxes_and_labels_on_image_array(\n",
    "          image_np,\n",
    "          np.squeeze(boxes),\n",
    "          np.squeeze(classes).astype(np.int32),\n",
    "          np.squeeze(scores),\n",
    "          category_index,\n",
    "          use_normalized_coordinates=True,\n",
    "          line_thickness=8)\n",
    "      plt.figure(figsize=IMAGE_SIZE)\n",
    "      plt.imshow(image_np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

2018년 2월 4일 일요일

NN 기본

Regression : "값"을 예상. 경험값으로 부터, 신의 그래프를 유추
Linear Regression : 선형 함수의 그래프 유추. 물론 직선일 필요는 없고 곡선일 수도 있음.
Classification : 분류의 문제 해결.

https://ujjwalkarn.me/2016/08/09/quick-intro-neural-networks/

http://pinkwink.kr/1080


https://tensorflowkorea.gitbooks.io/tensorflow-kr/content/g3doc/tutorials/mnist/beginners/


http://gusrb.tistory.com/12

http://solarisailab.com/archives/1422