infocusp / tf_cnnvis

CNN visualization tool in TensorFlow
MIT License
780 stars 208 forks source link

depdream_visualization input dimensions invalid reduction dimension #81

Open kjsandbrink opened 5 years ago

kjsandbrink commented 5 years ago

Hi, I'm trying to use this on a CNN that processes non-visual image data. I got activation_visualization to work fine but get an invalid reduction dimension error for deepdream_visualization. What exactly are the required input dimensions here and how are they different than the requirements for activation_visualization?

Code here:

randmf = np.random.randn(1, mf.shape[0], mf.shape[2])*np.std(mf.values)

mygraph = tf.Graph()
with mygraph.as_default():

    ##BUILD GRAPH
    # Declare placeholders for input data and labels
    X = tf.placeholder(tf.float32, shape=[1, ninputs, ntime], name="X")
    y = tf.placeholder(tf.int32, shape=[1], name="y")

    # Compute scores and accuracy
    scores, probabilities, net = model.predict(X, is_training=False)
    correct = tf.nn.in_top_k(probabilities, y, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32), name="accuracy")    

    # Build layer output reader
    #layers = [np.zeros(layer.get_shape().as_list()) for layer in list(net.values())]

    """
    # Test the `model`!
    restorer = tf.train.Saver()
    myconfig = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    with tf.Session(config=myconfig) as sess:
        ckpt_filepath = os.path.join(model.model_path, 'model.ckpt')
        restorer.restore(sess, ckpt_filepath)

        test_accuracy = []
        for step in range(num_steps):         
            batch_x, batch_y = dataset.next_valbatch(batch_size, 'test', step)
            acc = sess.run([accuracy], feed_dict={X: batch_x - train_mean, y: batch_y})
            test_accuracy.append(acc)
    """   

    """
    Results of net for this model:
        OrderedDict([('spatial0',
              <tf.Tensor 'Network/Spatial0/Relu:0' shape=(1, 25, 320, 8) dtype=float32>),
             ('spatial1',
              <tf.Tensor 'Network/Spatial1/Relu:0' shape=(1, 25, 320, 8) dtype=float32>),
             ('temporal0',
              <tf.Tensor 'Network/Temporal0/Relu:0' shape=(1, 25, 320, 8) dtype=float32>),
             ('temporal1',
              <tf.Tensor 'Network/Temporal1/Relu:0' shape=(1, 25, 320, 32) dtype=float32>),
             ('score',
              <tf.Tensor 'Network/Classifier/BiasAdd:0' shape=(1, 20) dtype=float32>)])
    """

    ##PROBE RECEPTIVE FIELDS
    restorer = tf.train.Saver()
    myconfig = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    with tf.Session(config=myconfig) as sess:
        ckpt_filepath = os.path.join(model.model_path, 'model.ckpt')
        restorer.restore(sess, ckpt_filepath)

        actlayers = ['r', 'p', 'c']
        with sess.as_default():

            is_success = deepdream_visualization(sess_graph_path = None, value_feed_dict = {X : randmf}, 
                                          layer='Network/Y_proba', 
                                          classes=[1]#, path_logdir="./", 
                                          #path_outdir=os.path.join("Output","AlexNet"))
                                          )

            #Activation visualiation code below works fine
            """
            is_success = activation_visualization(sess_graph_path = None, value_feed_dict = {X : randmf}, 
                         layers=actlayers
                         #, path_logdir="./", 
                         #path_outdir=os.path.join("Output","AlexNet"))
                         )
            """

Error message:

Traceback (most recent call last):

  File "<ipython-input-294-f8886142e425>", line 60, in <module>
    classes=[1]#, path_logdir="./",

  File "/home/kai/tf_cnnvis/tf_cnnvis/tf_cnnvis.py", line 424, in deepdream_visualization
    path_logdir = path_logdir, path_outdir = path_outdir)

  File "/home/kai/tf_cnnvis/tf_cnnvis/tf_cnnvis.py", line 166, in _get_visualization
    is_success = _visualization_by_layer_name(g, value_feed_dict, input_tensor, layer, method, path_logdir, path_outdir)

  File "/home/kai/tf_cnnvis/tf_cnnvis/tf_cnnvis.py", line 294, in _visualization_by_layer_name
    is_success = _deepdream(graph, sess, op_tensor, X, feed_dict, layer_name, path_outdir, path_logdir)

  File "/home/kai/tf_cnnvis/tf_cnnvis/tf_cnnvis.py", line 346, in _deepdream
    tmp2 = 1e-3 * tf.reduce_mean(tf.square(X), axis = (1, 2 ,3))

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 1534, in reduce_mean_v1
    return reduce_mean(input_tensor, axis, keepdims, name)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 1592, in reduce_mean
    name=name))

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 5571, in mean
    name=name)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in __init__
    control_input_ops)

  File "/home/kai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
    raise ValueError(str(e))

ValueError: Invalid reduction dimension 3 for input with 3 dimensions. for 'Mean_1' (op: 'Mean') with input shapes: [1,25,320], [3] and with computed input tensors: input[1] = <1 2 3>.

Any idea what might be causing this?

Thanks!

Cheers