Is it possible to convert an arbitrary tensor defined using tf.placeholder to a numpy array

Viewed 58

I want to convert a tensor which is defined using tf.placeholder to a numpy array to be used in matplotlib. The tensors are defined using

def _create_train_input(self):
        self.lr = tf.placeholder(tf.float32, name='lr')
        self.label = tf.placeholder(tf.int64, [None], name='label')
        self.image = tf.placeholder(tf.float32,
                                    [None, None, None, self._n_channel],
                                    name='image')
        if self._is_transform:
            self.input_im = self._translate_image(self.image)
        else:
            self.input_im = self.image
        self.input_label = self.label

and I use the self.input_im tensor passed from another function to the following function where the list locations is populated during execution. (I am running this for image recognition with a recurrent neural network so multiple glimpse windows are used on the same image and the list contains the locations of those glimpse windows)

def draw_locations(self,inputs_im,size=8, epoch=0):
        image = inputs_im.numpy()
        locations = list(self.layers['loc_mean'])
        fig, ax = plt.subplots(1, len(locations))
        for i, location in enumerate(locations):
            if len(locations) == 1:
                subplot = ax
            else:
                subplot = ax[i]
            subplot.axis('off')
            subplot.imshow(image, cmap='gray')
            loc = ((location[0] + 1) * image.shape[1] / 2 - size / 2,
             (location[1] + 1) * image.shape[0] / 2 - size / 2)
            # print(location, loc)
            rect = patches.Rectangle(
                loc, size, size, linewidth=1, edgecolor='r', facecolor='none')
            subplot.add_patch(rect)
        fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
        plt.savefig('results/glimpse_%d.png'%epoch, bbox_inches='tight')
        plt.close()

I have already enabled eager execution using tf.enable_eager_execution() but the the error still appears. The error is

image = image12.numpy()
AttributeError: 'Tensor' object has no attribute 'numpy'
0 Answers
Related