2017-12-21 36 views
0

私はTensorflow CNNチュートリアルの3DデータでCNNを作成しようとしています。しかし、私はいつもこの1つの次元のエラーを取得します。 stackoverflowの質問をするのは初めてのことですので、事前にお詫び申し上げます。これは少し乱雑に見えるかもしれません。Tensorflowでデータを修正するにはどうすればよいですか?

import numpy as np 
import tensorflow as tf 
from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

IMG_SIZE_PX = 50 
SLICE_COUNT = 20 
n_classes = 2 
keep_rate = 0.8 
tf.logging.set_verbosity(tf.logging.INFO) 

def neural_network(features, labels, mode): 

    input_layer = tf.reshape(features["x"],shape = [-1,IMG_SIZE_PX, IMG_SIZE_PX, SLICE_COUNT, 1]) 

    conv1 = tf.layers.conv3d(inputs = input_layer, filters = 32, kernel_size = [1,2,2,2,1], strides = [1,2,2,2,1], 
         padding = "same", activation = tf.nn.relu) 

    pool1 = tf.layers.max_pooling3d(inputs = conv1, pool_size = [1,2,2,2,1], strides = [1,2,2,2,1], padding = "same") 

    conv2 = tf.layers.conv3d(inputs = pool1, filters = 64, kernel_size = [1,2,2,2,1], strides = [1,2,2,2,1], 
         padding = "same", activation = tf.nn.relu) 

    pool2 = tf.layers.max_pooling3d(inputs = conv2, pool_size = [1,2,2,2,1], strides = [1,2,2,2,1], padding = "same") 

    pool2_flat = tf.reshape(pool2, [-1, 50000]) 
    dense = tf.layers.dense(inputs = pool2_flat, units = 1024, activation = tf.nn.relu) 

    dropout = tf.layers.dropout(inputs = dense, rate = 0.4, training = mode == tf.estimator.ModeKeys.TRAIN) 

    logits = tf.layers.dense(inputs = dropout, units = 2) 
    predictions = { 
     "classes" : tf.argmax(input = logits, axis = 1), 
     "probabilities" : tf.nn.softmax(logits, name = "softmax_tensor") 
    } 
    if mode == tf.estimator.ModeKeys.PREDICT: 
     return tf.estimator.EstimatorSpec(mode = mode, predictions = predictions) 

    onehot_labels = tf.one_hot(indices = tf.cast(labels, tf.int32), depth = 2) 
    loss = tf.losses.softmax_cross_entropy(onehot_labels = onehot_labels, logits = logits) 


    if mode == tf.estimator.ModeKeys.TRAIN: 
     optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.001) 
     train_op = optimizer.minimize(loss = loss, global_step = tf.train.get_global_step()) 
     return tf.estimator.EstimatorSpec(mode = mode, loss = loss, train_op = train_op) 

    eval_metric_ops = {"accuracy" : tf.metrics.accuracy(labels = labels, predictions = predictions["classes"])} 
    return tf.estimator.EstimatorSpec(mode = mode, loss = loss, eval_metric_ops = eval_metric_ops) 

#main 

def main(unused_argv): 

    training_data = train_data[0] 
    training_labels = train_data[1] 
    eval_data = train_data[0] 
    eval_labels = train_data[1] 

    lung_classifier = tf.estimator.Estimator(model_fn = neural_network, 
            model_dir = "SOME_DIR") 

    tensors_to_log = {"probabilities" : "softmax_tensor"} 
    logging_hook = tf.train.LoggingTensorHook(tensors = tensors_to_log, every_n_iter = 50) 

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
     x = {"x" : np.array(training_data)}, 
     y = np.array(training_labels), 
     batch_size = 50, 
     num_epochs = None, 
     shuffle = True) 
    lung_classifier.train(input_fn = train_input_fn, steps = 20000, hooks = [logging_hook]) 

    eval_input_fn = tf.estimator.inputs.numpy_input_fn(
     x = {"x" : eval_data}, 
     y = eval_labels, 
     num_epochs = 1, 
     shuffle = false) 
    eval_results = lung_classifier.evaluate(input_fn = eval_input_fn) 
    print(eval_results) 

if __name__ == "__main__": 
    with tf.device("/gpu:0"): 
     tf.app.run() 

私は、このエラーメッセージが表示されます::

ValueError: Dimension size must be evenly divisible by 50000 but is 50 for 
'Reshape' (op: 'Reshape') with input shapes: [50], [5] and with input 
tensors computed as partial shapes: input[1] = [?,50,50,20,1]. 

エラーメッセージはかなり長いので、私は単にあなたにそれの起源を示している。ここのコードです。

input_layer = tf.reshape(features["x"],shape = [-1,IMG_SIZE_PX, IMG_SIZE_PX, SLICE_COUNT, 1]) 

答えて

0

あなたの特徴[「X」]は寸法50x5を持っているようだと、あなたはそれを不可能ですBATCH_SIZE×50×50×20×1の次元を与えるためにしようとしています。

データの形状を正しく印刷していることを確認してください。

+0

前処理中に間違った形になったようです。ネットワークでこのエラーが発生しました。 –

関連する問題