## Precomputing the targets for content and style # Load content and style images content_image = preprocess_image_scale(args.content_image_path, img_size=args.img_size) style_images = [ preprocess_image_scale(img, img_size=args.style_img_size) for img in args.style_image_path ] nb_styles = len(style_images) model = vgg19.VGG19(weights='imagenet', include_top=False) outputs_dict = dict([(layer.name, layer.output) for layer in model.layers]) content_features = get_content_features(outputs_dict, args.content_layers) style_features = get_style_features(outputs_dict, args.style_layers, norm_by_channels=args.norm_by_channels) get_content_fun = K.function([model.input], content_features) get_style_fun = K.function([model.input], style_features) content_targets = get_content_fun([content_image]) # List of list of features style_targets_list = [get_style_fun([img]) for img in style_images] # List of batched features style_targets = [] for l in range(len(args.style_layers)): batched_features = [] for i in range(nb_styles): batched_features.append(style_targets_list[i][l][None])
parser.add_argument('--style_imgs', type=str, default=None, nargs='+', help='Style image file names.') parser.add_argument('--style_img_size', type=int, default=[None], nargs='+', help='Largest size of the style images') parser.add_argument('--style_layers', type=str, nargs='+', default=def_sl) parser.add_argument('--gpu', type=str, default='') parser.add_argument('--allow_growth', default=False, action='store_true') args = parser.parse_args() config_gpu(args.gpu, args.allow_growth) loss_net = vgg16.VGG16(weights='imagenet', include_top=False) targets_dict = dict([(layer.name, layer.output) for layer in loss_net.layers]) s_targets = get_style_features(targets_dict, args.style_layers) get_style_target = K.function([loss_net.input], s_targets) gm_lists = [[] for l in args.style_layers] img_list = [] img_size_list = [] # Get style image names or get all images in the directory if args.style_imgs is None: args.style_imgs = os.listdir(args.style_dir) # Check the image sizes args.style_img_size = std_input_list(args.style_img_size, len(args.style_imgs), 'Image size') for img_name, img_size in zip(args.style_imgs, args.style_img_size): try: