2017-09-11 8 views
0

私は、200個の画像がセットにあり、100個の同じ四角形と100個の同じ円があります。画像は44x41ピクセルで、画像はグレースケールです。私はテンソルフローを学ぶための簡単な分類子を構築しようとしています。TensorFlow:すべての例で同じ出力を返すCovNet

問題:予測子ベクトルは、入力画像に関係なく常に同じ値を持ちます。

は、ここに私のニューラルネットのコードです:

import tensorflow as tf 
import random as r 
import matplotlib 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
from PIL import Image 
%matplotlib inline 

#create pictures 

for i in range(100): 
    fig1 = plt.figure(frameon = False, figsize=(1,1), dpi=32) 
    ax1 = fig1.add_subplot(111, aspect='equal') 
    posx = 0.25 
    posy = 0.25 
    ax1.add_patch(
     patches.Rectangle(
      (posx,posy), # (x,y) 
      0.5,   # width 
      0.5,   # height 
     ) 
    ) 
    ax1.axis('off') 

    fig1.savefig('rect' + str(i) + '.png', bbox_inches='tight') 

for i in range(100): 
    fig1 = plt.figure(frameon = False, figsize=(1,1), dpi=32) 
    ax1 = fig1.add_subplot(111, aspect='equal') 
    posx = 0.5 
    posy = 0.5 
    ax1.add_patch(
     patches.Circle(
      (posx,posy), # (x,y) 
      0.3, 
     ) 
    ) 
    ax1.axis('off') 

    fig1.savefig('circ' + str(i) + '.png', bbox_inches='tight') 

# create vectors  

train_features = np.zeros((200,44,41,1)) 
train_labels = np.zeros((200,2)) 

for i in range(100): 
    #get rect 
    im = Image.open("rect" + str(i) + ".png") 
    im = im.convert(mode = "L") 
    xxx =list(im.getdata()) 
    imdata = np.reshape(xxx, (44,41,1)) 
    train_features[i] = imdata 
    train_labels[i] = np.array([0,1]) 
    #get circle 
    im = Image.open("circ" + str(i) + ".png") 
    im = im.convert(mode = "L") 
    xxx = list(im.getdata()) 
    imdata = np.reshape(xxx, (44,41,1)) 
    train_features[i+100] = imdata 
    train_labels[i+100] = np.array([1,0]) 

tf.reset_default_graph() 

features = tf.placeholder(tf.float32,shape=[None,44,41, 1]) 
labels = tf.placeholder(tf.float32,shape=[None,2]) 

weights = tf.Variable(tf.truncated_normal([3,3, 1, 16], stddev=0.1)) 
biases = tf.Variable(tf.zeros(16)) 

weights2 = tf.Variable(tf.truncated_normal([3,3, 16, 64], stddev=0.1)) 
biases2 = tf.Variable(tf.zeros(64)) 

conv_layer = tf.nn.conv2d(features, weights, strides=[1, 1, 1, 1], padding='SAME') 
conv_layer_b = tf.nn.bias_add(conv_layer, biases) 
conv_layer_relu = tf.nn.relu(conv_layer_b) 
conv_layer_pool = tf.nn.max_pool(conv_layer_relu, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') 

conv_layer2 = tf.nn.conv2d(conv_layer_pool, weights2, strides=[1, 1, 1, 1], padding='SAME') 
conv_layer2_b = tf.nn.bias_add(conv_layer2, biases2) 
conv_layer2_relu = tf.nn.relu(conv_layer2_b) 
conv_layer2_pool = tf.nn.max_pool(conv_layer2_relu, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') 

#fully connected layer 
weights_fc = tf.Variable(tf.truncated_normal([44*41*64, 256], stddev=0.1)) 
biases_fc = tf.Variable(tf.zeros([256])) 
fc = tf.reshape(conv_layer2_pool, [-1, weights_fc.get_shape().as_list()[0]]) 
fc_logit = tf.add(tf.matmul(fc, weights_fc), biases_fc) 
fc_relu = tf.nn.relu(fc_logit) 
#fc_drop = tf.nn.dropout(fc_relu, 0.75) 

# final layer 

weights_out = tf.Variable(tf.truncated_normal([256, 2], stddev=0.1)) 
biases_out = tf.Variable(tf.zeros([2])) 

out = tf.add(tf.matmul(fc_relu, weights_out), biases_out) 

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=labels)) 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for _ in range(100): 
     sess.run(optimizer, feed_dict={ 
       features: train_features[:], 
       labels: train_labels[:]}) 
    for i in range(200): 
     outx = sess.run(out, feed_dict={ 
       features: [train_features[i]], 
       labels: [train_labels[i]]}) 
     print(outx) 
     print(train_labels[i]) 
     print('---') 

答えて

0

は2つのテンソルに同じ名前を付けないようにしてください。たとえば、あなたがして、もう一度その別の形状、その後、tf.nn.conv2d(features, weights, strides=[1, 1, 1, 1], padding='SAME')に等しいconv_layerが、その後tf.nn.bias_add(conv_layer, biases)に書き直さ持ち、その後....

使用は、例えば、この命名:

conv_layer = tf.nn.conv2d(features, weights, strides=[1, 1, 1, 1], padding='SAME') 
conv_layer_b = tf.nn.bias_add(conv_layer, biases) 
conv_layer_relu = tf.nn.relu(conv_layer_b) 
conv_layer_pool = tf.nn.max_pool(conv_layer_relu, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') 

アルゴリズムは、1を学習します一度に1枚の画像。あなたのマシンがそれをハンドリングすることができるならば、セット内のすべての画像をフィードしてみてください:sess.run(optimizer, feed_dict={features: train_features[:], labels: train_labels[:]})。両方のクラスから100のイメージではない場合画像がシャッフルされているか、最初に100円と100個の正方形になっていますか?ここにエラーがあります。最後のループの四角で100回ウェイトを更新します。

予測されたベクトルを印刷する部分で、完全なプログラムを見ることはできますか?最初の段階で私はドロップアウトを取るだろう。それは過剰に過ごさせてください。そして、おそらく、小さいfc_layer(512または256)、より小さい学習率(0.01)を使用し、私はtf.Variable(...)の代わりにtf.get_variable('w1', shape=[3,3,1,16])を優先してバイアス0.1を初期化します。

+0

こんにちは、応答のおかげで。 – jojw85

+0

私の更新された答えを確認してください! – prometeu

+0

はまだ動作しません質問に完全なコードを含めました – jojw85

関連する問題