2017-07-19 120 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等),并允许使用这些模型的经验有限的人轻松加载自己的数据和训练。 –

相关问题