3

ここにある5_convolutional_net.pyの例に基づいてコンボを作った:https://github.com/nlintz/TensorFlow-Tutorials。私はチェスの断片を分類しようとします。私は私の写真をロードした:私は1136 60×60グレースケールの画像を各作品に持っている。私はそれらを列車と画像のテストに分け、各部分のためにホットベクトルを作り、それらをマージしました。私のtestimages.shape =(40,60,60)、testlabels.shape =(40,4)、trainimages.shape =(4504,60,60)、trainlabels.shape =(4504,4)です。 4504 = 4 *(1136年から1110年)Tensorflow畳み込みネットエラー:無効な引数:ロジットとラベルは同じサイズでなければなりません:logits_size = [512,4] labels_size = [128,4]

#!/usr/bin/env python 
from os import listdir 
from os.path import isfile, join 
import tensorflow as tf 
import numpy as np 
# import input_data 
import cv2 

def init_weights(shape): 
    return tf.Variable(tf.random_normal(shape, stddev=0.01)) 


def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden): 
    l1a = tf.nn.relu(tf.nn.conv2d(X, w,      # l1a shape=(?, 28, 28, 32) 
         strides=[1, 1, 1, 1], padding='SAME')) 
    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],    # l1 shape=(?, 14, 14, 32) 
         strides=[1, 2, 2, 1], padding='SAME') 
    l1 = tf.nn.dropout(l1, p_keep_conv) 

    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,      # l2a shape=(?, 14, 14, 64) 
         strides=[1, 1, 1, 1], padding='SAME')) 
    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],    # l2 shape=(?, 7, 7, 64) 
         strides=[1, 2, 2, 1], padding='SAME') 
    l2 = tf.nn.dropout(l2, p_keep_conv) 

    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,      # l3a shape=(?, 7, 7, 128) 
         strides=[1, 1, 1, 1], padding='SAME')) 
    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],    # l3 shape=(?, 4, 4, 128) 
         strides=[1, 2, 2, 1], padding='SAME') 
    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]]) # reshape to (?, 2048) 
    l3 = tf.nn.dropout(l3, p_keep_conv) 

    l4 = tf.nn.relu(tf.matmul(l3, w4)) 
    l4 = tf.nn.dropout(l4, p_keep_hidden) 

    pyx = tf.matmul(l4, w_o) 
    return pyx 

def add_images(folder,lista): 

    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))] 
    for file in onlyfiles: 
     img = cv2.imread(mypath + file, 0) # 60x60 numpy ndarray 
     lista.append(img) 
    return lista 

trainimages = [] 
testimages = [] 
folders=['TRAININGIMAGES/bw/rooks/','TRAININGIMAGES/bw/knights/','TRAININGIMAGES/bw/bishops/','TRAININGIMAGES/bw/pawns/'] 

for folder in folders: 
    print (folder) 
    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))] 
    images = [] 
    for file in onlyfiles: 
     img = cv2.imread(folder + file, 0) # 60x60 numpy ndarray 
     images.append(img) 
    trainimages.extend(images[10:]) 
    testimages.extend(images[:10]) 

size=len(onlyfiles) 



trainlabels = [] 
testlabels = [] 
rook_label = np.array([0, 0, 0, 1], dtype=bool) 
bishop_label = np.array([0, 0, 1, 0], dtype=bool) 
pawn_label = np.array([0, 1, 0, 0], dtype=bool) 
knight_label = np.array([1, 0, 0, 0], dtype=bool) 
hotvectors = [rook_label,pawn_label,knight_label,bishop_label] 
for label in hotvectors: 
    labels=[] 
    for x in range(size): 
     labels.append(label) 
    trainlabels.extend(labels[10:]) 
    testlabels.extend(labels[:10]) 

trainimages = np.asarray(trainimages) # shape : (4544,60,60) 
testimages = np.asarray(testimages) 
trainlabels = np.asarray(trainlabels) 
testlabels = np.asarray(testlabels) 

trainimages=trainimages.reshape(-1,60,60,1) 
testimages=testimages.reshape(-1,60,60,1) 

X = tf.placeholder("float", [None, 60, 60, 1]) 
Y = tf.placeholder("float", [None, 4]) 

w = init_weights([3, 3, 1, 32])  # 3x3x1 conv, 32 outputs 
w2 = init_weights([3, 3, 32, 64])  # 3x3x32 conv, 64 outputs 
w3 = init_weights([3, 3, 64, 128]) # 3x3x32 conv, 128 outputs 
w4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs 
w_o = init_weights([625, 4])   # FC 625 inputs, 10 outputs (labels) 

p_keep_conv = tf.placeholder("float") 
p_keep_hidden = tf.placeholder("float") 
py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) 

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) 
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) 
predict_op = tf.argmax(py_x, 1) 

with tf.Session() as sess: 
    # you need to initialize all variables 
    tf.initialize_all_variables().run() 
    for i in range(100): 
     for start, end in zip(range(0, len(trainimages), 128), range(128, len(trainimages), 128)): 
      sess.run(train_op, feed_dict={X: trainimages[start:end], Y: trainlabels[start:end], 
              p_keep_conv: 0.8, p_keep_hidden: 0.5}) 

     test_indices = np.arange(len(testimages)) # Get A Test Batch 
     np.random.shuffle(test_indices) 
     test_indices = test_indices[0:256] 

     print(i, np.mean(np.argmax(testlabels[test_indices], axis=1) == 
         sess.run(predict_op, feed_dict={X: testimages[test_indices], 
                 Y: testlabels[test_indices], 
                 p_keep_conv: 1.0, 
                 p_keep_hidden: 1.0}))) 

私はスクリプトを実行すると、私はライン100で次のエラーを得た:

tensorflow.python.framework.errors.InvalidArgumentError: logits and labels must be same size: logits_size=[512,4] labels_size=[128,4] 
    [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MatMul_1, _recv_Placeholder_1_0)]] 
Caused by op 'SoftmaxCrossEntropyWithLogits', defined at: 
    File "/home/matyi/OneDrive/PYTHON/PYTHON3/chess_vision/5_convolutional_net_chess.py", line 100, in <module> 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) 

私はライン108で128の役割を理解していませんどちらか。あなたはそれで私を助けることができますか?

画像例:enter image description here enter image description here

+0

'ジップ(範囲(0、len(trainimages)、128)、範囲(128、len(trainimages)、128))'の128はトレーニングバッチサイズです。スタックトレース全体を入れてもらえますか? –

答えて

3

あなたは60x60x1画像を養うので、あなたのテンソルの形状はこれらのようになります。

Tensor("Relu:0", shape=(?, 60, 60, 32), dtype=float32) 
Tensor("MaxPool:0", shape=(?, 30, 30, 32), dtype=float32) 
Tensor("Relu_1:0", shape=(?, 30, 30, 64), dtype=float32) 
Tensor("MaxPool_1:0", shape=(?, 15, 15, 64), dtype=float32) 
Tensor("Relu_2:0", shape=(?, 15, 15, 128), dtype=float32) 
Tensor("MaxPool_2:0", shape=(?, 8, 8, 128), dtype=float32) 

だからあなたの最後の体重、W4は、次のようになります。

w4 = init_weights([128 * 8 * 8, 625]) 

この変更を最初に試してみましょう。

+0

それはトリックでした!それは今トレーニングする。アキュラシーは増加していませんが、正確に0.25のままです。それは私のイメージが注文されている、問題になることができますか? (1126ルーク、次に1126ビショップ...)128枚の画像のバッチでは、ほとんどの場合、すべての画像が同じになります(ルークなど)。それとも私のネットはこの仕事のための原始的なものですか? – user3598726

+0

トレーニングセットもシャッフルします。ところで、ラベルは4つしかありませんか? –

+0

はい、ルーク、司教、麻雀、騎士は今のところしかありません。私はシャッフルをします、ありがとう!私は写真の例を加えました。 – user3598726

関連する問題