Example #1
0
    def vis_image_batch_alphas(self,
                               graph_inputs,
                               filename,
                               alphas_to_graph,
                               alphas_to_target,
                               batch_start,
                               wgt=False,
                               wmask=False):

        zs_batch = graph_inputs[self.z]
        ys_batch = graph_inputs[self.y]
        trunc = graph_inputs[self.truncation]

        filename_base = filename
        ims_target = []
        ims_transformed = []
        ims_mask = []
        for ag, at in zip(alphas_to_graph, alphas_to_target):
            input_test = {
                self.y: ys_batch,
                self.z: zs_batch,
                self.truncation: trunc
            }
            out_input_test = self.sess.run(self.outputs_orig, input_test)

            target_fn, mask_out = self.get_target_np(out_input_test, at)
            best_im_out = self.apply_alpha(input_test, ag)

            ims_target.append(target_fn)
            ims_transformed.append(best_im_out)
            ims_mask.append(mask_out)

        for ii in range(zs_batch.shape[0]):
            arr_gt = np.stack([x[ii, :, :, :] for x in ims_target], axis=0)
            if wmask:
                arr_transform = np.stack([
                    x[j, :, :, :] * y[j, :, :, :]
                    for x, y in zip(ims_zoomed, ims_mask)
                ],
                                         axis=0)
            else:
                arr_transform = np.stack(
                    [x[ii, :, :, :] for x in ims_transformed], axis=0)
            arr_gt = self.clip_ims(arr_gt)
            arr_transform = self.clip_ims(arr_transform)
            if wgt:
                ims = np.concatenate((arr_gt, arr_transform), axis=0)
            else:
                ims = arr_transform

            filename = filename_base + '_sample{}'.format(ii + batch_start)
            if wgt:
                filename += '_wgt'
            if wmask:
                filename += '_wmask'
            image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)),
                          filename)
Example #2
0
	def vis_multi_image_batch_alphas_combine(self, graph_inputs, filename,
											 alphas_to_graph, alphas_to_target,
											 batch_start,
											 layers=None,
											 name=None, wgt=False, wmask=False,
											 trainEmbed=False, computeL2=False,
											 given_w=None, index_=None):

		zs_batch = graph_inputs['z']  # numpy
		filename_base = filename
		ims_target = []
		ims_transformed = []
		ims_mask = []
		# imgs_new = [[]*len(zs_batch.shape[0])]
		with torch.no_grad():

			for ag1, at1 in zip(alphas_to_graph, alphas_to_target):
				ims_transformed = []
				input_test = {'z': torch.Tensor(zs_batch).cuda()}
				for ag2, at2 in zip(alphas_to_graph, alphas_to_target):
					best_im_out, alpha_org, out_zs = self.apply_alpha_combine(input_test, [ag1, ag2],
																		name=name, layers=layers,
																		trainEmbed=trainEmbed,
																		given_w=given_w, index_=index_)
					best_im_out = best_im_out.detach().cpu().numpy()
					best_im_out = np.uint8(np.clip(((best_im_out + 1) / 2.0) * 255, 0, 255))
					ims_transformed.append(best_im_out)

					# imgs_new[i].append(best_im_out)

				for ii in range(zs_batch.shape[0]):
					arr_transform = np.stack([x[ii, :, :, :] for x in ims_transformed], axis=0)
					ims = arr_transform
					filename = filename_base + '_idx{}_idx{}_{}_sample{}'.format(index_[0],
																		 index_[1],
																		 at1,
																		 ii + batch_start)
					# print('at1 at2', at1, at2, filename)
					if ims.shape[1] == 1 or ims.shape[1] == 3:
						ims = np.transpose(ims, [0, 2, 3, 1])
					image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)), filename)

					temp = filename.split('/')
					import os
					path = '/'.join(temp[:-1]) + '/org_img/' + temp[-1]
					path_dir = '/'.join(temp[:-1]) + '/org_img'
					if not os.path.exists(path_dir):
						print('pt: ', path_dir)
						os.mkdir(path_dir)
					ut.save_image(
						out_zs[ii],
						path+'_org.jpg',
						nrow=1,
						normalize=True,
						range=(-1, 1),
						padding=0)
Example #3
0
	def vis_multi_image_batch_alphas(self, graph_inputs, filename,
									 alphas_to_graph, alphas_to_target,
									 batch_start,
									 layers=None,
									 name=None, wgt=False, wmask=False,
									 trainEmbed=False, computeL2=False,
									 given_w=None, index_=None):

		zs_batch = graph_inputs['z']  # numpy
		filename_base = filename
		ims_target = []
		ims_transformed = []
		ims_mask = []

		for ag, at in zip(alphas_to_graph, alphas_to_target):
			input_test = {'z': torch.Tensor(zs_batch).cuda()}
			best_im_out, alpha_org, out_zs = self.apply_alpha(input_test, ag, name=name, layers=layers,
											trainEmbed=trainEmbed, given_w=given_w, index_=index_)

			best_im_out = best_im_out.detach().cpu().numpy()
			best_im_out = np.uint8(np.clip(((best_im_out + 1) / 2.0) * 255, 0, 255))
			ims_transformed.append(best_im_out)

		img_org = out_zs.detach().cpu().numpy()

		for ii in range(zs_batch.shape[0]):

			if index_!=None and len(self.attrList)>1:
				a = alpha_org[ii, index_].item()
			else:
				a = alpha_org[ii].item()
			if wmask:
				arr_transform = np.stack([x[j, :, :, :] * y[j, :, :, :] for x, y
										  in zip(ims_transformed, ims_mask)], axis=0)
			else:
				ims_transformed_new = ims_transformed
				arr_transform = np.stack([x[ii, :, :, :] for x in ims_transformed_new], axis=0)
				# arr_transform = np.stack([x[ii, :, :, :] for x in
				# 						  ims_transformed], axis=0)
			# arr_gt = self.clip_ims(arr_gt)
			# arr_transform = self.clip_ims(arr_transform)
			ims = arr_transform
			filename = filename_base + '_sample{}'.format(ii + batch_start)
			if wgt:
				filename += '_wgt'
			if wmask:
				filename += '_wmask'
			if ims.shape[1] == 1 or ims.shape[1] == 3:
				# N C W H -> N W H C
				ims = np.transpose(ims, [0, 2, 3, 1])
			# ims = np.squeeze(ims)
			filename = filename +'_%.2f' % a
			print('Save in ', filename)
			image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)), filename)

			"""
Example #4
0
def make_samples(img_tensor,
                 output_dir,
                 epoch,
                 optim_iter,
                 batch_size,
                 pre_path='results',
                 name='test'):
    if img_tensor.is_cuda:
        img_tensor = img_tensor.detach().cpu().numpy()
    img_tensor = np.uint8(np.clip(((img_tensor + 1) / 2.0) * 255, 0, 255))
    if img_tensor.shape[1] == 1 or img_tensor.shape[1] == 3:
        img_tensor = np.transpose(img_tensor, [0, 2, 3, 1])
    image.save_im(
        image.imgrid(img_tensor, cols=int(math.sqrt(batch_size))),
        '{}/{}/{}_{}_{}'.format(output_dir, pre_path, epoch, optim_iter, name))
Example #5
0
    def vis_multi_image_batch_alphas(self,
                                     graph_inputs,
                                     filename,
                                     alphas_to_graph,
                                     alphas_to_target,
                                     batch_start,
                                     layers=None,
                                     name=None,
                                     wgt=False,
                                     wmask=False,
                                     trainEmbed=False,
                                     computeL2=False,
                                     given_w=None,
                                     index_=None):
        # TODO:
        # CHANGE!!
        zs_batch = graph_inputs['z']  # numpy

        filename_base = filename
        ims_target = []
        ims_transformed = []
        ims_mask = []
        L2_loss = {}
        index_ = 0
        for ag, at in zip(alphas_to_graph, alphas_to_target):
            input_test = {'z': torch.Tensor(zs_batch).cuda()}

            best_im_out, alpha_org = self.apply_alpha(input_test,
                                                      ag,
                                                      name=name,
                                                      layers=layers,
                                                      trainEmbed=trainEmbed,
                                                      given_w=given_w,
                                                      index_=index_)
            # best_im_out = F.interpolate(best_im_out, size=256)
            best_im_out = best_im_out.detach().cpu().numpy()
            best_im_out = np.uint8(
                np.clip(((best_im_out + 1) / 2.0) * 255, 0, 255))
            ims_transformed.append(best_im_out)

        for ii in range(zs_batch.shape[0]):
            if wmask:
                arr_transform = np.stack([
                    x[j, :, :, :] * y[j, :, :, :]
                    for x, y in zip(ims_transformed, ims_mask)
                ],
                                         axis=0)
            else:
                arr_transform = np.stack(
                    [x[ii, :, :, :] for x in ims_transformed], axis=0)
            ims = arr_transform
            filename = filename_base + '_sample{}'.format(ii + batch_start)
            if wgt:
                filename += '_wgt'
            if wmask:
                filename += '_wmask'
            # (7, 3, 64, 64)
            if ims.shape[1] == 1 or ims.shape[1] == 3:
                # N C W H -> N W H C
                ims = np.transpose(ims, [0, 2, 3, 1])
            # ims = np.squeeze(ims)

            a_org = alpha_org[ii, ]

            image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)),
                          filename)
Example #6
0
    def vis_image_batch_alphas(self,
                               graph_inputs,
                               filename,
                               alphas_to_graph,
                               alphas_to_target,
                               batch_start,
                               name=None,
                               wgt=False,
                               wmask=False,
                               trainEmbed=False,
                               computeL2=True):

        zs_batch = graph_inputs['z']  # numpy
        filename_base = filename
        ims_target = []
        ims_transformed = []
        ims_mask = []

        # index_ = [i in range(len(alphas_to_graph))]
        L2_loss = {}
        print('len(alphas_to_graph)', len(alphas_to_graph))

        for index_, (ag,
                     at) in enumerator(zip(alphas_to_graph, alphas_to_target)):
            print('Index: ', index)

            input_test = {'z': torch.Tensor(zs_batch).cuda()}
            out_input_test = self.get_logits(input_test)
            out_input_test = out_input_test.detach().cpu().numpy()  # on Cuda
            target_fn, mask_out = self.get_target_np(out_input_test, at)

            best_im_out = self.apply_alpha(input_test, ag, name,
                                           index_).detach().cpu().numpy()

            L2_loss[at] = self.L2_loss(target_fn, best_im_out, mask_out)
            ims_target.append(target_fn)
            ims_transformed.append(best_im_out)
            ims_mask.append(mask_out)
        if computeL2:
            ## Compute L2 loss for drawing the plots
            return L2_loss

        ######### ######### ######### ######### #########
        print('wgt: ', wgt)
        for ii in range(zs_batch.shape[0]):
            arr_gt = np.stack([x[ii, :, :, :] for x in ims_target], axis=0)

            if wmask:
                arr_transform = np.stack([
                    x[j, :, :, :] * y[j, :, :, :]
                    for x, y in zip(ims_transformed, ims_mask)
                ],
                                         axis=0)
            else:
                arr_transform = np.stack(
                    [x[ii, :, :, :] for x in ims_transformed], axis=0)
            arr_gt = self.clip_ims(arr_gt)
            arr_transform = self.clip_ims(arr_transform)
            if wgt:
                ims = np.concatenate((arr_gt, arr_transform), axis=0)
            else:
                ims = arr_transform
            filename = filename_base + '_sample{}'.format(ii + batch_start)
            if wgt:
                filename += '_wgt'
            if wmask:
                filename += '_wmask'
            # (7, 3, 64, 64)
            if ims.shape[1] == self.num_channels:
                # N C W H -> N W H C
                ims = np.transpose(ims, [0, 2, 3, 1])
            # ims = np.squeeze(ims)
            # print('ims.shape: ', ims.shape)
            image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)),
                          filename)
Example #7
0
    def vis_multi_image_batch_alphas(self,
                                     graph_inputs,
                                     filename,
                                     get_target_np,
                                     alphas_to_graph,
                                     alphas_to_target,
                                     batch_start,
                                     name=None,
                                     wgt=False,
                                     wmask=False,
                                     trainEmbed=False,
                                     computeL2=False):
        # TODO:
        # CHANGE!!
        zs_batch = graph_inputs['z']  #numpy

        filename_base = filename
        ims_target = []
        ims_transformed = []
        ims_mask = []
        L2_loss = {}
        index_ = 0
        for ag, at in zip(alphas_to_graph, alphas_to_target):

            input_test = {'z': torch.Tensor(zs_batch).cuda()}
            out_input_test = self.get_logits(input_test)
            output_zs_256 = F.interpolate(out_input_test, size=256)  # T

            out_input_test2 = output_zs_256.detach().cpu().numpy()  # on Cuda
            target_fn, mask_out = get_target_np(out_input_test2, at)
            target_fn = np.uint8(np.clip(((target_fn + 1) / 2.0) * 255, 0,
                                         255))

            best_im_out = self.apply_alpha(input_test,
                                           ag,
                                           name,
                                           trainEmbed=trainEmbed,
                                           index_=index_)
            best_im_out = F.interpolate(best_im_out,
                                        size=256).detach().cpu().numpy()
            best_im_out = np.uint8(
                np.clip(((best_im_out + 1) / 2.0) * 255, 0, 255))

            ims_target.append(target_fn)
            ims_transformed.append(best_im_out)
            ims_mask.append(mask_out)

            if computeL2:
                L2_loss[at] = self.L2_loss(target_fn, best_im_out, mask_out)
            index_ += 1

        if computeL2:
            return L2_loss

        print('wgt: ', wgt)

        for ii in range(zs_batch.shape[0]):
            arr_gt = np.stack([x[ii, :, :, :] for x in ims_target], axis=0)
            if wmask:
                arr_transform = np.stack([
                    x[j, :, :, :] * y[j, :, :, :]
                    for x, y in zip(ims_transformed, ims_mask)
                ],
                                         axis=0)
            else:
                arr_transform = np.stack(
                    [x[ii, :, :, :] for x in ims_transformed], axis=0)
            # arr_gt = self.clip_ims(arr_gt)
            # arr_transform = self.clip_ims(arr_transform)

            if wgt:
                ims = np.concatenate((arr_gt, arr_transform), axis=0)
            else:
                ims = arr_transform
            filename = filename_base + '_sample{}'.format(ii + batch_start)
            if wgt:
                filename += '_wgt'
            if wmask:
                filename += '_wmask'
            # (7, 3, 64, 64)
            if ims.shape[1] == 1 or ims.shape[1] == 3:
                # N C W H -> N W H C
                ims = np.transpose(ims, [0, 2, 3, 1])
                # ims = np.squeeze(ims)
            image.save_im(image.imgrid(ims, cols=len(alphas_to_graph)),
                          filename)