hzy46 / fast-neural-style-tensorflow

A tensorflow implementation for fast neural style!
932 stars 361 forks source link

一个网络复用的问题 #32

Open aromazyl opened 7 years ago

aromazyl commented 7 years ago

我想要在这个代码里实现preserving style的论文。 问下我如果需要share vgg网络的话,需要怎么做呢。我尝试果使用scope.reuse方式,但是似乎有问题。我在loss.py里修改get_style_features_flow函数如下,并在vgg.py中添加了reuse选项:

def get_style_features_flow(sess, FLAGS, images_placeholder, network_fn):
    network_fn = nets_factory.get_network_fn(
        FLAGS.loss_model,
        num_classes=1,
        is_training=False)
    sizex = conf.height
    sizey = conf.width
    img_bytes = tf.read_file(FLAGS.style_image)
    if FLAGS.style_image.lower().endswith('png'):
        c_style_image = tf.image.decode_png(img_bytes)
    else:
        c_style_image = tf.image.decode_jpeg(img_bytes)
        # image = _aspect_preserving_resize(image, size)
    prev_image = images_placeholder[0, :,:,:]
    curr_image = images_placeholder[1, :,:,:]
    style_image = tf.image.rgb_to_hsv(tf.to_float(c_style_image))

    image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
        FLAGS.loss_model,
        is_training=False)
    def getFeatures(image):
        image1 = tf.image.rgb_to_hsv(tf.to_float(image))
        image2 = tf.stack([image1[:,:,0], image1[:,:,1], style_image[:, :, 2]], axis=-1)
        s_image = tf.image.hsv_to_rgb(image2)
        images = tf.stack([image_preprocessing_fn(s_image, sizex, sizey)])
        # scope.reuse_variables()
        _, endpoints_dict = network_fn(images, spatial_squeeze=False, REUSE=True)
        features = []
        for layer in FLAGS.style_layers:
            print("layer:", layer)
            feature = endpoints_dict[layer]
            tf.logging.info(tf.shape(feature))
            feature = tf.squeeze(gram(feature), [0])  # remove the batch dimension
            # feature = gram(feature)
            features.append(feature)
        return features
    return getFeatures(prev_image), getFeatures(curr_image)

tensorflow运行时报错:

  File "hsv_train.py", line 252, in <module>
    train(FLAGS)
  File "hsv_train.py", line 230, in train
    _, loss_t, step = sess.run([train_op, loss, global_step], feed_dict=feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Tried to explicitly squeeze dimension 0 but dimension was not 1: 4
     [[Node: Squeeze_11 = Squeeze[T=DT_FLOAT, squeeze_dims=[0], _device="/job:localhost/replica:0/task:0/cpu:0"](div_5)]]

Caused by op u'Squeeze_11', defined at:
  File "hsv_train.py", line 252, in <module>
    train(FLAGS)
  File "hsv_train.py", line 126, in train
    style_features_prev, style_features_curr = losses.get_style_features_flow(sess, FLAGS, images_placeholder, network_fn)
  File "/home/zhangyule/fast-neural-style-flow-tensorflow/fast-neural-style-tensorflow/hsv_losses.py", line 131, in get_style_features_flow
    return getFeatures(prev_image), getFeatures(curr_image)
  File "/home/zhangyule/fast-neural-style-flow-tensorflow/fast-neural-style-tensorflow/hsv_losses.py", line 127, in getFeatures
    feature = tf.squeeze(gram(feature), [0])  # remove the batch dimension
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2281, in squeeze
    return gen_array_ops._squeeze(input, axis, name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3329, in _squeeze
    squeeze_dims=squeeze_dims, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Tried to explicitly squeeze dimension 0 but dimension was not 1: 4
     [[Node: Squeeze_11 = Squeeze[T=DT_FLOAT, squeeze_dims=[0], _device="/job:localhost/replica:0/task:0/cpu:0"](div_5)]]