2017-11-17 9 views
1
import tensorflow as tf 
import numpy as np 
import glob 
fq=glob.glob("*.jpg") 
filename_queue = tf.train.string_input_producer(fq) 
reader = tf.WholeFileReader() 
key, value = reader.read(filename_queue) 
my_img = tf.image.decode_jpeg(value,channels=3) 
my_img=tf.cast(my_img,tf.float32) 
resized_image = tf.image.resize_images(my_img, [50, 50]) 


labels=[1,1,1,0,0] 
onehot = tf.one_hot(labels, depth=2) 



image_batch = tf.train.batch([resized_image], batch_size=2) 

# layer 1 
w1 = tf.Variable(tf.truncated_normal([2, 2, 3, 52], stddev=0.01)) 
b1= tf.Variable(tf.constant(0.01, shape = [52])) 
layer1=tf.nn.conv2d(image_batch,w1,[1,1,1,1],padding='SAME') 
act1=tf.nn.relu(tf.nn.bias_add(layer1,b1)) 
pool1=tf.layers.max_pooling2d(act1,2,2) 

# layer 2 
b2= tf.Variable(tf.constant(0.01, shape = [104])) 
w2=tf.Variable(tf.truncated_normal([2, 2,52, 104], stddev=0.01)) 
layer2=tf.nn.conv2d(pool1,w2,[1,1,1,1],padding='SAME') 
act2=tf.nn.relu(tf.nn.bias_add(layer2,b2)) 
pool2=tf.layers.max_pooling2d(act2,2,2) 

#fully connected layer 
b3= tf.Variable(tf.constant(0.01, shape = [300])) 
w3=tf.Variable(tf.truncated_normal([12*12*104, 300], stddev=0.01)) 
fcl1=tf.reshape(pool2,[-1,12*12*104]) 
fcl1 = tf.add(tf.matmul(fcl1, w3), b3) 
fcl1 = tf.nn.relu(fcl1) 
fcl1 = tf.nn.dropout(fcl1,0.5) 

#output layer 
b_out=b3= tf.Variable(tf.constant(0.01, shape = [2])) 
w_out=tf.Variable(tf.truncated_normal([300, 2], stddev=0.01)) 
ans=tf.add(tf.matmul(fcl1,w_out),b_out) 


#traning , loss , optimizer 
logits = ans 
prediction = tf.nn.softmax(logits) 
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=onehot)) 
optimizer = tf.train.AdamOptimizer(learning_rate=0.001) 
train_op = optimizer.minimize(loss_op) 




with tf.Session() as s: 
    s.run(tf.global_variables_initializer()) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 
    for step in range(1, 40): 
     s.run(train_op) 
     print(step,s.run(loss_op)) 
    coord.request_stop() 
    coord.join(threads) 

Traceback (most recent call last): File "test.py", line 56, in logits=logits, labels=onehot))Tensorflow : 라벨링 문제

ValueError: Dimension 0 in both shapes must be equal, but are 2 and 5 for 'SoftmaxCrossEntropyWithLogits' (op: 'SoftmaxCrossEntropyWithLogits') with input shapes: [2,2], [5,2].

내 실수 뭔지를 알려주십시오, 나는 한 번에이 라벨을 공급해야하지만 한 번에 모든 5를 복용 생각합니다. 라벨을 개별적으로 일괄 처리하려면 어떻게해야합니까?

답변

1

입력 배치를 만들지 만 배치 배치는 작성하지 않습니다. 라벨 배치도 먹이십시오. 나는 당신의 데이터 배치 및 레이블 배치 같은 방법으로 모두 처리 할 것, 솔직히

all_labels = [1, 1, 1, 0, 0] 
start = 0 
with tf.Session() as s: 
    # ... 
    for step in range(40): 
     t = all_labels[start:start+2] # grab next label batch 
     start += 2 
     if start > len(all_labels): 
      start = 0 # reset to start of list when we overrun the end 
     sess.run(train_op, feed_dict={labels: t}) 

,하지만 난 밖으로 방법을 파악하는 데 문제가되었습니다 이후 다음

labels = tf.placeholder(tf.int32, [None]) 
# Keep the tf.one_hot op. 
onehot = tf.one_hot(labels, depth=2) 

과 : 레이블에 대한 자리 표시자를 확인 tf.train.batchtf.one_hot의 출력에서 ​​작동 시키려면 먼저 원시 레이블 배열에서 tf.train.batch을 실행 한 다음 tf.one_hot으로 전달하는 데 문제가있었습니다.

+0

감사합니다. 매력처럼 작동했습니다. 당신은 진정한 영웅입니다! 사람들은이 질문을 "주제를 벗어난 채로 잡아라"고 표시했지만 디버깅을 결코 요구하지는 않았지만 내 의심의 여지를 없애기 위해 원시 코드를 게시하면 사람들이 내 상황을 이해하는 데 도움이 될 것이라고 생각했습니다. 정말 고마워요! 다시 한 번 감사드립니다 :) –