示例#1
0
    def get_weights_as_images(self,
                              width,
                              height,
                              outdir='img/',
                              n_images=10,
                              img_type='grey'):
        """ Create and save the weights of the hidden units with respect to the
        visible units as images.

        :param width:
        :param height:
        :param outdir:
        :param n_images:
        :param img_type:

        :return: self
        """

        outdir = self.data_dir + outdir

        with tf.Session() as self.tf_session:

            self.tf_saver.restore(self.tf_session, self.model_path)

            weights = self.W.eval()

            perm = np.random.permutation(self.num_hidden)[:n_images]

            for p in perm:
                w = np.array([i[p] for i in weights])
                image_path = outdir + self.model_name + '_{}.png'.format(p)
                utils.gen_image(w, width, height, image_path, img_type)
示例#2
0
    def get_weights_as_images(self,
                              width,
                              height,
                              outdir='img/',
                              max_images=10,
                              model_path=None):
        """ Save the weights of this autoencoder as images, one image per hidden unit.
        Useful to visualize what the autoencoder has learned.

        :type width: int
        :param width: Width of the images

        :type height: int
        :param height: Height of the images

        :type outdir: string, default 'data/sdae/img'
        :param outdir: Output directory for the images. This path is appended to self.data_dir

        :type max_images: int, default 10
        :param max_images: Number of images to return.
        """
        assert max_images <= self.n_components

        outdir = self.data_dir + outdir

        if not os.path.isdir(outdir):
            os.mkdir(outdir)

        with tf.Session() as self.sess:

            # Restore trained model
            if model_path is not None:
                self.saver.restore(self.sess, model_path)
            else:
                self.saver.restore(self.sess,
                                   self.models_dir + self.model_name)

            # Extract encoding weights as numpy array
            enc_weights = self.Wf_.eval()

            # Extract decoding weights as numpy arrays
            dec_weights = self.Wg_.eval() if not self.tied_weights else None

            perm = np.random.permutation(self.n_components)[:max_images]

            for p in perm:

                enc_w = np.array([i[p] for i in enc_weights])
                image_path = outdir + self.model_name + '-enc_weights_{}.png'.format(
                    p)
                utils.gen_image(enc_w, width, height, image_path)

                if not self.tied_weights:
                    dec_w = np.array([i[p] for i in dec_weights])
                    image_path = outdir + self.model_name + '-dec_weights_{}.png'.format(
                        p)
                    utils.gen_image(dec_w, width, height, image_path)
示例#3
0
def output_char(source_name, c, output_dir, post_processing=downsample):
    global counter
    name = '%s_%d' % (source_name, counter)
    counter += 1

    img = gen_image([c])
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    detected_lines = extract_lines(img)
    if len(detected_lines) == 0:
        return
    img = detected_lines[0]
    detected_chars, _ = extract_chars(img)
    if len(detected_chars) == 0:
        return
    img = detected_chars[0]
    img = post_processing(img)

    # write the generated image with various quality
    for qual in qualities:
        out_name = '%s.%d.jpg' % (name, qual)
        cv2.imwrite(os.path.join(output_dir, out_name), img,
                    (cv2.IMWRITE_JPEG_QUALITY, qual))

    # write the text, which will be the labels.
    with open(os.path.join(output_dir, name + '.txt'), 'w') as text_f:
        text_f.write(c + '\n')
示例#4
0
    def get_weights_as_images(self,
                              width,
                              height,
                              outdir='img/',
                              n_images=10,
                              img_type='grey'):

        outdir = self.data_dir + outdir

        with tf.Session() as self.sess:

            self.saver.restore(self.sess, self.models_dir + self.model_name)

            weights = self.W.eval()

            perm = np.random.permutation(self.nhid)[:n_images]

            for p in perm:
                w = np.array([i[p] for i in weights])
                image_path = outdir + self.model_name + '_{}.png'.format(p)
                utils.gen_image(w, width, height, image_path, img_type)
	def get_weights_as_images(self, width, height, main_dir='dae/', outdir='img/', n_images=10, model_path=None, img_type='grey'):
		''' Saves the weights of this autoencoder as images, one image per hidden unit. 
		This is useful to visualize what the autoencoder has learned

		Parameters
		----------
		width 		: width of the images, int
		height 		: height of the images, int
		main_dir	: path where all your encoding information is placed
		outdir 		: output directory for the images -- this path is appended to self.data_dir, string (default is 'data/sdae/img')
		n_images	: number of images to return, int (default is 10)
		'''

		assert n_images <= self.n_components

		main_dir = main_dir + '/' if main_dir[-1] != '/' else main_dir
		outdir = outdir + '/' if outdir[-1] != '/' else outdir
		outdir = main_dir + outdir
		
		if not os.path.isdir(outdir):
			os.mkdir(outdir)


		with tf.Session() as self.tf_session:
			if model_path is not None:
				self.tf_saver.restore(self.tf_session, model_path)
			else:
				self.tf_saver.restore(self.tf_session, self.model_path)

			enc_weights = self.W_.eval()

			perm = np.random.permutation(enc_weights.shape[1])[:n_images]
			for p in perm:
				enc_w = np.array([i[p] for i in enc_weights])
				image_path = outdir + self.model_name + '-enc_weights_{}.png'.format(p)
				utils.gen_image(enc_w, width, height, image_path, img_type)