teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

変数sizeの変更

2017/10/10 13:37

投稿

退会済みユーザー
answer CHANGED
@@ -88,7 +88,7 @@
88
88
  # 全結合層1の作成
89
89
  with tf.name_scope('fc1') as scope:
90
90
  # 7 * 7 * 64 から不定のsizeに変更 画像sizeを変更しても修正がいらない
91
- size = tf.size(h_pool2)
91
+ size = tf.size(h_pool2[0])
92
92
  W_fc1 = weight_variable([size, 1024])
93
93
  b_fc1 = bias_variable([1024])
94
94
  h_pool2_flat = tf.reshape(h_pool2, [-1, size])

4

NUM_CLASSESの値の変更

2017/10/10 13:37

投稿

退会済みユーザー
answer CHANGED
@@ -19,7 +19,7 @@
19
19
  import tensorflow as tf
20
20
  import tensorflow.python.platform
21
21
 
22
- NUM_CLASSES = 2
22
+ NUM_CLASSES = 12
23
23
  IMAGE_SIZE = 56
24
24
  IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3
25
25
 

3

修正

2017/10/10 13:35

投稿

退会済みユーザー
answer CHANGED
@@ -9,6 +9,7 @@
9
9
 
10
10
  このネットワークには28 * 28 * 3 に変換可能なサイズを渡しましょう。
11
11
 
12
+ ネットワークの部分だけ修正(見落としがある可能性があります)
12
13
  ```python
13
14
  #!/usr/bin/env python
14
15
  # -*- coding: utf-8 -*-

2

コード追加

2017/10/10 13:20

投稿

退会済みユーザー
answer CHANGED
@@ -7,4 +7,96 @@
7
7
 
8
8
  ```
9
9
 
10
- このネットワークには28 * 28 * 3 に変換可能なサイズを渡しましょう。
10
+ このネットワークには28 * 28 * 3 に変換可能なサイズを渡しましょう。
11
+
12
+ ```python
13
+ #!/usr/bin/env python
14
+ # -*- coding: utf-8 -*-
15
+ import sys
16
+ import cv2
17
+ import numpy as np
18
+ import tensorflow as tf
19
+ import tensorflow.python.platform
20
+
21
+ NUM_CLASSES = 2
22
+ IMAGE_SIZE = 56
23
+ IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3
24
+
25
+ flags = tf.app.flags
26
+ FLAGS = flags.FLAGS
27
+ flags.DEFINE_string('train', 'train.txt', 'File name of train data')
28
+ flags.DEFINE_string('test', 'test.txt', 'File name of train data')
29
+ flags.DEFINE_string('train_dir', '/tmp/data', 'Directory to put the training data.')
30
+ flags.DEFINE_integer('max_steps', 200, 'Number of steps to run trainer.')
31
+ flags.DEFINE_integer('batch_size', 10, 'Batch size'
32
+ 'Must divide evenly into the dataset sizes.')
33
+ flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')
34
+
35
+ def inference(images_placeholder, keep_prob):
36
+ """ 予測モデルを作成する関数
37
+
38
+ 引数:
39
+ images_placeholder: 画像のplaceholder
40
+ keep_prob: dropout率のplace_holder
41
+
42
+ 返り値:
43
+ y_conv: 各クラスの確率(のようなもの)
44
+ """
45
+ # 重みを標準偏差0.1の正規分布で初期化
46
+ def weight_variable(shape):
47
+ initial = tf.truncated_normal(shape, stddev=0.1)
48
+ return tf.Variable(initial)
49
+
50
+ # バイアスを標準偏差0.1の正規分布で初期化
51
+ def bias_variable(shape):
52
+ initial = tf.constant(0.1, shape=shape)
53
+ return tf.Variable(initial)
54
+
55
+ # 畳み込み層の作成
56
+ def conv2d(x, W):
57
+ return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
58
+
59
+ # プーリング層の作成
60
+ def max_pool_2x2(x):
61
+ return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
62
+ strides=[1, 2, 2, 1], padding='SAME')
63
+
64
+ # 入力を28x28x3に変形
65
+ x_image = tf.reshape(images_placeholder, [-1, 56, 56, 3])
66
+
67
+ # 畳み込み層1の作成
68
+ with tf.name_scope('conv1') as scope:
69
+ W_conv1 = weight_variable([5, 5, 3, 32])
70
+ b_conv1 = bias_variable([32])
71
+ h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
72
+
73
+ # プーリング層1の作成
74
+ with tf.name_scope('pool1') as scope:
75
+ h_pool1 = max_pool_2x2(h_conv1)
76
+
77
+ # 畳み込み層2の作成
78
+ with tf.name_scope('conv2') as scope:
79
+ W_conv2 = weight_variable([5, 5, 32, 64])
80
+ b_conv2 = bias_variable([64])
81
+ h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
82
+
83
+ # プーリング層2の作成
84
+ with tf.name_scope('pool2') as scope:
85
+ h_pool2 = max_pool_2x2(h_conv2)
86
+
87
+ # 全結合層1の作成
88
+ with tf.name_scope('fc1') as scope:
89
+ # 7 * 7 * 64 から不定のsizeに変更 画像sizeを変更しても修正がいらない
90
+ size = tf.size(h_pool2)
91
+ W_fc1 = weight_variable([size, 1024])
92
+ b_fc1 = bias_variable([1024])
93
+ h_pool2_flat = tf.reshape(h_pool2, [-1, size])
94
+ h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
95
+ # dropoutの設定
96
+ h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
97
+
98
+ # 全結合層2の作成
99
+ with tf.name_scope('fc2') as scope:
100
+ W_fc2 = weight_variable([1024, NUM_CLASSES])
101
+ b_fc2 = bias_variable([NUM_CLASSES])
102
+ ```

1

訂正

2017/10/10 13:19

投稿

退会済みユーザー
answer CHANGED
@@ -7,4 +7,4 @@
7
7
 
8
8
  ```
9
9
 
10
- このネットワークには28 * 28 * 3 を入力として渡しましょう。
10
+ このネットワークには28 * 28 * 3 に変換可能なサイズを渡しましょう。