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

質問編集履歴

2

ミスがあった

2020/05/23 04:29

投稿

gogongon
gogongon

スコア1

title CHANGED
File without changes
body CHANGED
@@ -24,16 +24,7 @@
24
24
  # 分類する
25
25
  result = y.eval(feed_dict={x: [data]})
26
26
  ```
27
- data = Image.open("out.png").convert('L')
28
- img = np.array(data)
29
27
 
30
-
31
- imgArray_flatten = img.flatten()
32
-
33
-
34
-
35
- result = y.eval(feed_dict={x: [imgArray_flatten]})
36
-
37
28
  #プログラム全体
38
29
  ```python
39
30
  import tensorflow as tf

1

指摘点を直した

2020/05/23 04:29

投稿

gogongon
gogongon

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,10 +1,29 @@
1
1
  #エラー内容
2
2
  mnistを行おうとしたら以下のようなエラーが発生しました
3
+
4
+ 例外が発生しました: ValueError
3
- Cannot feed value of shape (1, 360000) for Tensor 'Placeholder:0', which has shape '(?, 784)'
5
+ Cannot feed value of shape (1, 64) for Tensor 'Placeholder:0', which has shape '(?, 784)'
6
+
4
7
  どのように改善すればよいのでしょうか
5
8
  #エラー箇所
6
9
  おそらく以下の部分の改善でエラーが治ると思うのですが
10
+ ```python
11
+ def imageToData(filename):
12
+ # 画像を8x8のグレースケールへ変換
13
+ grayImage = PIL.Image.open(filename).convert("L")
14
+ grayImage = grayImage.resize((8,8),PIL.Image.ANTIALIAS)
15
+ # 数値リストへ変換
16
+ numImage = np.asarray(grayImage, dtype = float)
17
+ numImage = np.floor(16 - 16 * (numImage / 256))
18
+ numImage = numImage.flatten()
7
19
 
20
+ return numImage
21
+
22
+ data = imageToData("out.jpg")
23
+
24
+ # 分類する
25
+ result = y.eval(feed_dict={x: [data]})
26
+ ```
8
27
  data = Image.open("out.png").convert('L')
9
28
  img = np.array(data)
10
29
 
@@ -16,6 +35,7 @@
16
35
  result = y.eval(feed_dict={x: [imgArray_flatten]})
17
36
 
18
37
  #プログラム全体
38
+ ```python
19
39
  import tensorflow as tf
20
40
  import numpy as np
21
41
  import matplotlib.pyplot as plt
@@ -25,19 +45,22 @@
25
45
  from tensorflow.contrib.learn.python.learn.datasets import mnist as mnist_loader
26
46
  import base64
27
47
  from io import BytesIO
48
+ import PIL
28
49
 
29
50
 
30
-
51
+ # (1)手書き数字の学習データを読み込みます。
31
52
  mnist = mnist_loader.read_data_sets("MNIST_data/", one_hot=True)
32
53
 
54
+ # (2)ニューラルネットワークサイズ等設定値を指定します。
33
55
  x = tf.placeholder("float",[None,784])
34
- Weight = tf.Variable(tf.zeros([784,10]))
56
+ Weight = tf.Variable(tf.zeros([784,10])) # 縦28セル*横28セルなので、ニューラルネットワークの第1層は。784
35
- b = tf.Variable(tf.zeros([10]))
57
+ b = tf.Variable(tf.zeros([10])) # 第2層は10個のニューロンを設定。10個の数字の対応する箇所の値が大きくなるよう学習させる
36
58
  y = tf.nn.softmax(tf.matmul(x,Weight)+b)
37
59
  y_ = tf.placeholder("float",[None,10])
38
60
  cross_entropy = -tf.reduce_sum(y_*tf.log(y))
39
61
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
40
62
 
63
+ # (3)学習の実行。ニューラルネットワークに学習させる回数は1200回。
41
64
  init = tf.global_variables_initializer()
42
65
  ss = tf.InteractiveSession()
43
66
  ss.run(init)
@@ -114,15 +137,23 @@
114
137
  app = Application(master=root)
115
138
  app.mainloop()
116
139
 
140
+ def imageToData(filename):
141
+ # 画像を8x8のグレースケールへ変換
117
- data = Image.open("out.png").convert('L')
142
+ grayImage = PIL.Image.open(filename).convert("L")
143
+ grayImage = grayImage.resize((8,8),PIL.Image.ANTIALIAS)
144
+ # 数値リストへ変換
145
+ numImage = np.asarray(grayImage, dtype = float)
146
+ numImage = np.floor(16 - 16 * (numImage / 256))
118
- img = np.array(data)
147
+ numImage = numImage.flatten()
119
148
 
149
+ return numImage
120
150
 
121
- imgArray_flatten = img.flatten()
151
+ data = imageToData("out.jpg")
122
152
 
153
+ # 分類する
154
+ result = y.eval(feed_dict={x: [data]})
123
155
 
156
+ print(result) # 分類結果の表示。配列の先頭が0で末尾が9に相当し、可能性が高いと判定されたものほど1に近い値が表示されます。
124
157
 
125
- result = y.eval(feed_dict={x: [imgArray_flatten]})
126
-
127
- print(result)
128
- plt.bar(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), result[0])
158
+ plt.bar(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), result[0])
159
+ ```