infocusp / tf_cnnvis

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

AttributeError: 'str' object has no attribute 'name #52

Closed pazadeh closed 6 years ago

pazadeh commented 6 years ago

Hi, I am trying to use your code to visualize the max activation of different layers in my deep neural network but I keep receiving the following error which I assume has something to do with the way the names of the layers are defined. the error:

Traceback (most recent call last):

File "", line 1, in runfile('/media/asgharpn/daten2017-03/Bone_Machine_learning/ML_temp/Deeploc_test/Visualization_max_activation_00.py', wdir='/media/asgharpn/daten2017-03/Bone_Machine_learning/ML_temp/Deeploc_test')

File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile execfile(filename, namespace)

File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile builtins.execfile(filename, *where)

File "/media/asgharpn/daten2017-03/Bone_Machine_learning/ML_temp/Deeploc_test/Visualization_max_activation_00.py", line 164, in path_logdir='./Log', path_outdir='./Output')

File "build/bdist.linux-x86_64/egg/tf_cnnvis/tf_cnnvis.py", line 410, in deconv_visualization path_logdir = path_logdir, path_outdir = path_outdir)

File "build/bdist.linux-x86_64/egg/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 "build/bdist.linux-x86_64/egg/tf_cnnvis/tf_cnnvis.py", line 270, in _visualization_by_layer_name parsed_tensors = parse_tensors_dict(graph, layer_name, value_feed_dict)

File "build/bdist.linux-x86_64/egg/tf_cnnvis/utils.py", line 79, in parse_tensors_dict tmp = get_tensor(graph = g, name = key_op.name)

AttributeError: 'str' object has no attribute 'name'

Here is the architecure of my trained network with 13 layers (8 convolutional, 3 relu and 2 fully connected layers):

n_feat_c1 = 8 n_feat_c2 = 8 n_feat_c3 = 16 n_feat_c4 = 16 n_feat_c5 = 32 n_feat_c6 = 32 n_feat_c7 = 64 n_feat_c8 = 64 n_feat_fc1= 128 n_feat_fc2= 128 numClasses = 4

def DeepLocModel(input_images,is_training):

conv1 = nn_layers.conv_layer(input_images, 3, 3, 1, n_feat_c1, 1, 'conv_1',is_training=is_training)
print conv1.get_shape
conv2 = nn_layers.conv_layer(conv1, 3, 3, n_feat_c1, n_feat_c2, 1, 'conv_2', is_training=is_training)
print conv2.get_shape
pool1 = nn_layers.pool2_layer(conv2, 'pool1')
print pool1.get_shape
conv3 = nn_layers.conv_layer(pool1, 3, 3, n_feat_c2, n_feat_c3, 1, 'conv_3', is_training=is_training)
print conv3.get_shape
conv4 = nn_layers.conv_layer(conv3, 3, 3, n_feat_c3, n_feat_c4, 1, 'conv_4', is_training=is_training)
print conv4.get_shape
pool2 = nn_layers.pool2_layer(conv4, 'pool2')
print pool2.get_shape
conv5 = nn_layers.conv_layer(pool2, 3, 3, n_feat_c4, n_feat_c5, 1, 'conv_5', is_training=is_training)
print conv5.get_shape
conv6 = nn_layers.conv_layer(conv5, 3, 3, n_feat_c5, n_feat_c6, 1, 'conv_6', is_training=is_training)
print conv6.get_shape
conv7 = nn_layers.conv_layer(conv6, 3, 3, n_feat_c6, n_feat_c7, 1, 'conv_7', is_training=is_training)
print conv7.get_shape
conv8 = nn_layers.conv_layer(conv7, 3, 3, n_feat_c7, n_feat_c8, 1, 'conv_8', is_training=is_training)
print conv8.get_shape
pool3 = nn_layers.pool2_layer(conv8, 'pool3')
print pool3.get_shape
size_last_pool= pool3.shape
size_last_pooled_image = int(size_last_pool[1]*size_last_pool[2]*size_last_pool[3])
pool3_flat = tf.reshape(pool3, [-1, size_last_pooled_image])
print pool3_flat.get_shape
fc_1 = nn_layers.nn_layer(pool3_flat, size_last_pooled_image, n_feat_fc1, 'fc_1', act=tf.nn.relu, is_training=is_training)
fc_2 = nn_layers.nn_layer(fc_1, n_feat_fc1, n_feat_fc2, 'fc_2', act=tf.nn.relu,is_training=is_training)
logit = nn_layers.nn_layer(fc_2, n_feat_fc2, numClasses, 'final_layer', act=None, is_training=is_training)

return logit

and finally here is the code I use to visualize the activations using tf_cnnviz:

sess = tf.Session() sess.run(tf.global_variables_initializer(),{is_training:False})

load model checkpoint

saver = tf.train.Saver(tf.global_variables()) saver.restore(sess, locNetCkpt)

Loading labels from test data set

dataBaseDir = '/media/asgharpn/daten2017-03/Bone_Machine_learning/Learning_dataset/projected_augmented_not_squared_yz_test/' trainHdf5 = dataBaseDir+'bone_projected_valid_set.hdf5' f = h5py.File(trainHdf5,'r') labels = f['Index1'] dset = f['data1']

cropSize = 733 batchSize = dset.shape[0]-1 stretchLow = 0.1 # stretch channels lower percentile stretchHigh = 99.9 # stretch channels upper percentile

imSize_x = 733
imSize_z = 161 numClasses = 4 numChan = 1

data = dataClass.Data(trainHdf5,['data','Index'],batchSize) batch = data.getBatch() loc = tf.Graph() with loc.as_default(): loc_saver = tf.train.import_meta_graph(locNetCkpt+'.meta') locSession = tf.Session(graph=loc) loc_saver.restore(locSession, locNetCkpt)

pred_loc = loc.get_tensor_by_name(u'softmax:0') input_loc = loc.get_tensor_by_name(u'input:0') is_training_loc = loc.get_tensor_by_name(u'is_training:0') processedBatch=procIm.preProcessImages(batch['data'], imSize_x,imSize_z,cropSize,numChan, rescale=False,stretch=False, means=None,stds=None, stretchLow=stretchLow,stretchHigh=stretchHigh, jitter=False,randTransform=False)

images = processedBatch[10][:] is_training_loc = loc.get_tensor_by_name(u'is_training:0') input_loc = loc.get_tensor_by_name(u'input:0') feed_dict={input_loc: images, is_training_loc: False}

deconv visualization

layers = ["c"] totaltime = 0 x = tf.placeholder(tf.float32, [None, 118013]) y = tf.placeholder(tf.float32, [None, 4]) keep_prob = tf.placeholder(tf.float32)

x_image = tf.reshape(x, [-1, 733, 161, 1]) start = time.time()

feeddict = {x:batch['data'][1:2], y: batch['Index'][1:2], keep_prob: 1.0} is_success = deconv_visualization(sess_graph_path = sess, value_feed_dict = batch, input_tensor=x_image, layers=layers, path_logdir='./Log', path_outdir='./Output') start = time.time() - start print("Total Time = %f" % (start))

sess.close()

Could you please help me with this error?

BhagyeshVikani commented 6 years ago

You need not add an external standalone placeholder for the image. Just pass in the input image where the input node is present in your model (input_loc in the example you shared above). That should fix the problem.

pazadeh commented 6 years ago

Thanks. That solved the issue.