2017-07-19 124 views
1

我一直在試圖設計一個包裝以使用預製的tensorflow超薄模型作爲自定義數據集。該數據集是正方形和三角形的1000張圖像,32×32灰度。它們被組織爲數據集/形狀/三角形/和數據集/形狀/方塊/。TensorFlow Slim預先訓練的模型負尺寸

使用下面的代碼,我能夠無誤地訓練inception_v2模型。稍後tf.reshape將被替換爲正確的變量參數。 .tfrecords文件是使用來自google的this腳本創建的,該腳本根據上述數據集結構創建記錄。

graph = tf.Graph() 
sess = tf.InteractiveSession(graph=graph) 

with graph.as_default(): 
    name_dict, nClass = gen_dict(data_directory, path_to_labels_file) 

    # associate the "label" and "image" objects with the corresponding features read from 
    # a single example in the training data file 
    label, image = getImage("datasets/shapes/train-00000-of-00001", height, width, nClass) 

    # associate the "label_batch" and "image_batch" objects with a randomly selected batch--- 
    # of labels and images respectively 
    imageBatch, labelBatch = tf.train.shuffle_batch(
     [image, label], batch_size=bsize, 
     capacity=2000, 
     min_after_dequeue=1000) 

    with sess.as_default(): 
     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(sess=sess, coord=coord) 

     sess.run(tf.global_variables_initializer()) 

     batch_xs, batch_ys = sess.run([imageBatch, labelBatch]) 

     print('ran shuffle batch') 
     print(tf.shape(batch_xs)) 
     print(tf.shape(batch_ys)) 
     # batch_xs = tf.expand_dims(batch_xs, 2) 
     batch_xs = tf.reshape(batch_xs, [100, 32, 32, 1]) 
     print(tf.shape(batch_xs)) 
     logits, end_points = inception.inception_v2(batch_xs, 
                num_classes=2, 
                is_training=True) 

     predictions = end_points['Predictions'] 
     logits = end_points['Logits'] 

     tf.losses.softmax_cross_entropy(batch_ys, logits) 

     total_loss = slim.losses.get_total_loss() 

     optimizer = tf.train.GradientDescentOptimizer(learning_rate=.001) 

     train_tensor = slim.learning.create_train_op(total_loss, optimizer) 

     slim.learning.train(train_tensor, 
          train_log_dir, 
          number_of_steps=1000) 

我遇到的問題是與其他模型。使用inception_v1,使用相同的參數,我得到以下錯誤:

File "model_test.py", line 62, in <module> 
    is_training=True) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/contrib/slim/python/slim/nets/inception_v1.py", line 349, in inception_v1 
    net, [7, 7], stride=1, scope='MaxPool_0a_7x7') 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args 
    return func(*args, **current_args) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 131, in avg_pool2d 
    outputs = layer.apply(inputs) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 492, in apply 
    return self.__call__(inputs, *args, **kwargs) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 441, in __call__ 
    outputs = self.call(inputs, *args, **kwargs) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/layers/pooling.py", line 276, in call 
    data_format=utils.convert_data_format(self.data_format, 4)) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1741, in avg_pool 
    name=name) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 48, in _avg_pool 
    data_format=data_format, name=name) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op 
    op_def=op_def) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op 
    set_shapes_for_outputs(ret) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs 
    shapes = shape_func(op) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring 
    return call_cpp_shape_fn(op, require_shape_fn=True) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn 
    debug_python_shape_fn, require_shape_fn) 
    File "/home/chakicherla3/tf_slim_image_classification/models/slim/python/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl 
    raise ValueError(err.message) 
ValueError: Negative dimension size caused by subtracting 7 from 1 for 'InceptionV1/Logits/MaxPool_0a_7x7/AvgPool' (op: 'AvgPool') with input shapes: [100,1,1,1024]. 

我使用inception_v3得到一個類似的錯誤。隨着vgg_16和vgg_19,我得到:

ValueError: Negative dimension size caused by subtracting 7 from 1 for 'vgg_16/fc6/convolution' (op: 'Conv2D') with input shapes: [100,1,1,512], [7,7,512,4096]. 

任何人都可以洞察這些錯誤嗎? inception_v1和inception_v2之間會有什麼區別導致它崩潰,以及這種初始模型如何不同?我還沒有用ResNet試過這個數據集,但我懷疑也會發生類似的錯誤。

作爲參考,本實施例中的代碼是基於提供與所述TF苗條文檔「工作實施例」,位於here

它與Tensorflow-GPU 1.2.0上使用Python 2.7.10運行的系統。這是一款採用4顆Nvidia Titan X GPU的Xeon系統,位於Ubuntu 14.10上。

謝謝!如果您需要任何額外的系統配置或getImage功能,我也可以提供這些功能!

+0

問題是輸入大小。例如,VGG網絡僅適用於224x224。 –

+0

謝謝@vijaym。你知道爲什麼初期v2正在處理這些數據,而不是其他數據?如果大小是要求不應該V2也會拋出一個錯誤? –

回答

0

輸入圖像尺寸32x32對於初始模型來說太小。 Inception_v1嘗試使用內核大小爲7x7的平均池,但在此層上的輸入在所有先前的池化層之後都有1x1個數據(具有1024個通道)。

無論如何,我認爲「初始」對任務來說太大了,你所描述的任務。

+0

是否可以使用tf.image.resize_image_with_crop_or_pad函數將所有圖像填充到每個模型的正確尺寸?這會降低模型的質量嗎?我只是將方塊/三角形模型用作將來可能使用的任何其他數據的佔位符。 –

+0

如果您計劃只有兩個班級和32 x 32圖像的工作,那麼初始模型對此太多了。您可以嘗試使用少量(兩個或三個)卷積+ relu + maxpool鏈的模型。很好的例子是[tutorial](https://www.tensorflow.org/tutorials/deep_cnn) –

+0

我的程序不是專門用於這個數據集的,我只是將它用作未來自定義數據集的佔位符。該程序專門用於使用TF Slim Image分類庫中的模型(初始,resnet,vgg等),並允許使用這些模型的經驗有限的人輕鬆加載自己的數據和訓練。 –

相關問題