content_image = load_image(args.content_image) style_image = load_image(args.style_image) # use existing if available if (load_segmentation): print("Load segmentation from files.") content_segmentation_image = cv2.imread(content_segmentation_filename) style_segmentation_image = cv2.imread(style_segmentation_filename) content_segmentation_masks = extract_segmentation_masks(content_segmentation_image) style_segmentation_masks = extract_segmentation_masks(style_segmentation_image) # otherwise compute it else: print("Create segmentation.") content_segmentation, style_segmentation = compute_segmentation(args.content_image, args.style_image) cv2.imwrite(change_filename(args.seg_dir, args.content_image, '_seg_raw', '.png'), content_segmentation) cv2.imwrite(change_filename(args.seg_dir, args.style_image, '_seg_raw', '.png'), style_segmentation) content_segmentation_masks, style_segmentation_masks = merge_segments(content_segmentation, style_segmentation, args.semantic_thresh, args.similarity_metric) cv2.imwrite(change_filename(args.seg_dir, args.content_image, '_seg', '.png'), reduce_dict(content_segmentation_masks, content_image)) cv2.imwrite(change_filename(args.seg_dir, args.style_image, '_seg', '.png'), reduce_dict(style_segmentation_masks, style_image)) if args.init == "noise": random_noise_scaling_factor = 0.0001 random_noise = np.random.randn(*content_image.shape).astype(np.float32)
if path is None or not os.path.isfile(path): print("File {} does not exist.".format(path)) exit(0) content_image = load_image(args.content_image) # use existing if available if (load_segmentation): print("Load segmentation from files.") content_segmentation_image = cv2.imread( content_segmentation_filename) content_segmentation_masks = extract_segmentation_masks( content_segmentation_image) else: print("Create segmentation.") content_segmentation = compute_segmentation(args.content_image) cv2.imwrite( change_filename(result_dir, args.content_image, '_seg_raw', '.png'), content_segmentation) content_segmentation_masks, color_to_gram_dict, anp_results = merge_anps( content_segmentation, style_text, args.adjective_threshold, args.noun_threshold, result_dir) cv2.imwrite( change_filename(result_dir, args.content_image, '_seg', '.png'), reduce_dict(content_segmentation_masks, content_image)) if args.init == "noise": random_noise_scaling_factor = 0.0001 random_noise = np.random.randn(*content_image.shape).astype(
from components.segmentation import compute_segmentation from components.semantic_merge import extract_segmentation_masks import itertools as it from operator import itemgetter import networkx as nx import nltk import numpy as np import tensorflow as tf from os.path import join from components.PSPNet.model import load_color_label_dict from components.path import WEIGHTS_DIR content_segmentation, style_segmentation = compute_segmentation( "/home/skhan22/PoeticStyleTransfer/data2/summer_lake/2108832-1366x768-[DesktopNexus.com].jpg", "/home/skhan22/PoeticStyleTransfer/data2/summer_lake/2504-1271617411m0fL.jpg" ) print(content_segmentation.shape) print(style_segmentation.shape) # load color - label mapping color_label_dict = load_color_label_dict() label_color_dict = { label: color for color, labels in color_label_dict.items() for label in labels } colors = color_label_dict.keys() # Extract the boolean mask for every color content_masks = extract_segmentation_masks(content_segmentation, colors)
def get_gram_from_generator(anp, color, save_dir, results, vgg19): #fake generator implementation #just randomly picks an image from the ANP dataset and gets the gram matrices for it image_names = [] for image_name in os.listdir(os.path.join(dataset_location, anp)): if(image_name.endswith('.jpg')): image_names.append(image_name) rand_indices = list(range(len(image_names))) random.shuffle(rand_indices) selected_image = os.path.join(dataset_location.rstrip(), anp.rstrip(), image_names[rand_indices[0]].rstrip()) print('loading '+selected_image) image_segmentation = compute_segmentation(selected_image) image_masks = extract_segmentation_masks(image_segmentation) i = 1 while not color in image_masks.keys(): selected_image = os.path.join(dataset_location, anp, image_names[rand_indices[i]].rstrip()) print('previous image didnt work loading' + selected_image) image_segmentation = compute_segmentation(selected_image) image_masks = extract_segmentation_masks(image_segmentation) i += 1 # global first # global first_selected_image # if(first): # first_selected_image = cv2.imread(selected_image) # first = False # colored_image = color_transfer(first_selected_image, cv2.imread(selected_image)) # cv2.imwrite(os.path.join(save_dir, anp+'.jpg'), colored_image) # selected_image = os.path.join(save_dir, anp+'.jpg') required_mask = mask_for_tf_with_color(image_masks, color) results+=anp+' image: '+selected_image+'\n' style_anp_image = load_image(selected_image) #cv2.imwrite(os.path.join(save_dir, anp+'_seg_raw.png'), image_segmentation) style_anp_image = vgg.preprocess(style_anp_image) anp_gram_matrices = [] global weight_restorer with tf.compat.v1.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) weight_restorer.init(sess) style_convs = sess.run( fetches=[vgg19.conv1_1, vgg19.conv2_1, vgg19.conv3_1, vgg19.conv4_1, vgg19.conv5_1], feed_dict={image_placeholder: style_anp_image}) for style_conv in style_convs: anp_gram_matrices.append(tf.compat.v1.identity(calculate_gram_matrix_with_mask(style_conv, required_mask))) return anp_gram_matrices, results