3

Ich habe ein Convnet basierend auf dem 5_convolutional_net.py Beispiel hier gefunden: https://github.com/nlintz/TensorFlow-Tutorials. Ich versuche, Schachfiguren zu klassifizieren. Ich habe meine Bilder geladen: Ich habe 1136 60x60 Graustufenbilder für jedes Stück. Ich trennte sie in Zug- und Testbilder, machte die Hotvectors für jedes Stück und fusionierte sie. Also mein testimages.shape = (40,60,60), testlabels.shape = (40,4), trainimages.shape = (4504,60,60), trainlabels.shape = (4504,4). 4504 = 4 * (1136-10)Tensorflow Convolutional Net Fehler: Ungültiges Argument: Logits und Labels müssen die gleiche Größe haben: 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}))) 

Als ich das Skript ausführen, habe ich die folgenden Fehler in Zeile 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)) 

Ich verstehe nicht, die Rolle des 128 an der Leitung 108 entweder. Kannst du mir damit helfen?

Bild Beispiel: enter image description here enter image description here

+0

128 in der 'zip (Bereich (0, len (trainimages), 128), Bereich (128, len (trainimages), 128))' ist die Trainingsgröße Batch. Könnten Sie den gesamten Stack-Trace setzen? –

Antwort

3

Da Sie 60x60x1 Bild füttern, wird Ihre Tensor Formen besitzen diese:

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) 

So Ihre letzte Gewicht sollte w4 sein:

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

Lassen Sie uns zuerst mit dieser Änderung versuchen.

+0

Danke, dass es den Trick gemacht hat! Es trainiert jetzt. Die Genauigkeit nimmt jedoch nicht zu, bleibt genau 0,25. Kann es ein Problem sein, dass meine Bilder bestellt sind? (1126 Türme, dann 1126 Bischöfe ...) Also für eine Serie von 128 Bildern werden die meisten Bilder alle gleich sein (zum Beispiel Krähen). Oder mein Netz ist zu primitiv für diese Aufgabe? – user3598726

+0

Ich würde das Training auch mischen. Übrigens, du hast nur 4 Etiketten? –

+0

Ja Rooks, Bischöfe, Bauern und Ritter nur jetzt. Ich werde das Mischen tun, danke! Ich habe Beispiele für die Bilder hinzugefügt. – user3598726