Пример #1
0
    def startStylize(self,triggerData):  
        # check for input data:
        self.checkInputs(triggerData)
        # runs stylizing
        # mostly adapted from Neural Style by Anish Athalye
        sourceImage = self.imageRead(triggerData['source'])
        styleImages = [self.imageRead(triggerData['style'])]
        iterations  = triggerData['quality']
        print ("Number of iterations: ",iterations)

        # the fixed values are adjustments for the style transfer
        # method, NO MAGIC! borrowed from previously mentioned source
        for iteration, image in stylize(
                                network=VGG_PATH,
                                initial=None,
                                content=sourceImage,
                                styles=styleImages,
                                iterations=iterations,
                                content_weight=CONTENT_WEIGHT,
                                style_weight=STYLE_WEIGHT,
                                style_blend_weights=[1],
                                tv_weight=TV_WEIGHT,
                                learning_rate=LEARNING_RATE,
                                print_iterations=True,
                                checkpoint_iterations=True
                                ):
            output_file = None
            output_file = self.destination
            if output_file:
                self.imageSave(output_file, image)
Пример #2
0
def apply_style(options, initial, content, styles, style_weights, iterations,
                network, vgg_weights, vgg_mean_pixel):
    for iteration, image in stylize(
            network=network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content,
            styles=styles,
            preserve_colors=options.preserve_colors,
            iterations=iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            vgg_mean_pixel=vgg_mean_pixel,
            vgg_weights=vgg_weights,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            return combined_rgb
Пример #3
0
def main():
    # lida com os argumentos da linha de comando
    parser = build_parser()
    options = parser.parse_args()

    # verifica se a rede neural utilizada é o modelo correto
    if not os.path.isfile(options.network):
        urllib.request.urlretrieve(
            url=
            'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-vgg-verydeep-19.mat',
            filename=options.network,
            reporthook=_print_download_progress)

    content_image = imread(options.content)  # lê a imagem de conteúdo
    style_images = [imread(style)
                    for style in options.styles]  # lê as imagens de estilo

    # imagem de saída terá mesmo formato da de conteúdo
    target_shape = content_image.shape

    # itera pelas imagens de estilo
    for i in range(len(style_images)):

        # reescalona as imagens de estilo
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]

        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    # define a ponderação para cada estilo passado
    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:  # padrão sendo peso igual para cada estilo
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]

    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]
    combined_rgb = stylize(
        network=options.network,  # path do modelo VGG19
        content=content_image,  # imagem de conteúdo
        styles=style_images,  # imagens de estilo
        iterations=options.iterations,
        content_weight=options.content_weight,
        content_weight_blend=options.content_weight_blend,
        style_weight=options.style_weight,
        style_layer_weight_exp=options.style_layer_weight_exp,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        beta1=options.beta1,
        beta2=options.beta2,
        epsilon=options.epsilon)

    output_file = options.output
    imsave(output_file, combined_rgb)
Пример #4
0
def main():
    args = getArgument()

    if args.subcommand is None:
        print("ERROR: specify either train or eval")
        sys.exit(1)
    if args.cuda and not torch.cuda.is_available():
        print("ERROR: cuda is not available, try running on CPU")
        sys.exit(1)

    train(args) if args.subcommand == "train" else stylize(args)
Пример #5
0
def main():
    parser = build_parser()
    args = parser.parse_args()
    content_img = get_content_image(args)
    style_imgs = get_style_images(args, content_img)
    with tf.Graph().as_default():
        print('\n---- RENDERING SINGLE IMAGE ----\n')
        init_img = get_init_image(args, content_img, style_imgs)
        tick = time.time()
        output_image = stylize(args, content_img, style_imgs, init_img)
        write_image_output(args, output_image, content_img, style_imgs,
                           init_img)
        tock = time.time()
        print('Single image elapsed time: {}'.format(tock - tick))
Пример #6
0
def upload():
    if 'file' in session:
        # S3 Bucket
        bucketName = "styletransferbucket"
        s3 = boto3.resource('s3')
        obj = s3.Object(bucketName, session['file'])
        obj.delete()
        session.clear()
    style = request.args.get('style')
    if (request.method == 'GET'):
        return render_template('upload.html',
                               styleName=style.upper(),
                               stylePath=scrape(style + ".jpg"))
    else:
        if ('file' not in request.files):
            flash('No file part')
            return (redirect(request.url))
        file = request.files['file']
        if (file.filename == ''):
            flash('No selected file')
            return (redirect(request.url))
        if (file and allowed_file(file.filename)):

            # Get files and styled image
            style_strength = float(request.form['styleRange'])
            output_img = 'static/out/' + time.ctime().replace(' ',
                                                              '_') + '.jpg'
            style_image = stylize(file, output_img,
                                  "models/" + style + ".model", style_strength,
                                  0)

            # S3 Bucket
            bucketName = "styletransferbucket"

            # S3 upload image
            s3 = boto3.client('s3')
            s3.put_object(Body=style_image,
                          Bucket=bucketName,
                          Key=output_img,
                          ContentType='image/jpeg')

            session['file'] = output_img
            return (render_template("uploaded.html"))

        flash('Please select right file')
        return (redirect(request.url))
Пример #7
0
def main():
    content_image = imread(content)
    style_images = [imread(style[0]) for style in styles]  #Accepted all style
    style_blend_weights = [style[1] for style in styles]
    target_shape = content_image.shape
    #target size set end

    for i in range(len(style_images)):  #Do this loop for all style_images
        style_scale = STYLE_SCALE  #What this value?
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale
            *  #each style_image should be resized by the style scale of the target_shape by the colums of the pic (Why is not full size but only width)
            target_shape[1] / style_images[i].shape[1])

    total_blend_weight = sum(
        style_blend_weights)  #sum up all the given style_blend_weights
    style_blend_weights = [
        weight /
        total_blend_weight  #Let each the weight be divided by the total_blend_weight
        for weight in style_blend_weights
    ]

    for iteration, image in stylize(  #Get in the stylize
            network=VGG_PATH,
            initial_noiseblend=initial_noiseblend,
            content=content_image,
            styles=style_images,
            iterations=ITERATIONS,
            content_weight=CONTENT_WEIGHT,
            content_weight_blend=CONTENT_WEIGHT_BLEND,
            style_weight=STYLE_WEIGHT,
            style_blend_weights=style_blend_weights,
            tv_weight=1e2,
            learning_rate=LEARNING_RATE,
            beta1=BETA1,
            beta2=BETA2,
            epsilon=EPSILON,
            pooling=POOLING,
            checkpoint_iterations=100):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            output_file = './outputs/ex7output%s.jpg' % iteration
        if output_file:
            imsave(output_file, combined_rgb)  #save the output_file
Пример #8
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (content_image.shape[0] * width // content_image.shape[1],
                     width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i, im in enumerate(style_images):
        style_scale = STYLE_SCALE if options.style_scales is None else options.style_scales[
            i]
        style_images[i] = scipy.misc.imresize(
            im, style_scale * target_shape[1] / im.shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        L = len(style_images)
        style_blend_weights = np.ones(L) / L
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = np.array(
            style_blend_weights) / total_blend_weight

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        elif options.initial_noiseblend < 1.0:
            initial = content_image

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    # try saving a dummy image to the output path to make sure that it's writable
    if os.path.isfile(options.output) and not options.overwrite:
        raise IOError(
            "%s already exists, will not replace it without the '--overwrite' flag"
            % options.output)
    try:
        imsave(options.output, np.zeros((500, 500, 3)))
    except:
        raise IOError(
            '%s is not writable or does not have a valid file extension for an image file'
            % options.output)

    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
Пример #9
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    content_basepath, content_filename = os.path.split(options.content)

    style_basepaths = []
    style_filenames = []
    for style in options.styles:
        basepath, filename = os.path.split(style)
        style_basepaths.append(basepath)
        style_filenames.append(filename)

    if options.output is None:
        # For now, only works with the first style
        style_filename = style_filenames[0]

        out_stylewe = int(options.style_layer_weight_exp * 10)
        out_ashift = int(options.ashift)
        out_contentwe = int(options.content_weight_blend * 10)

        postfix = ""
        if options.out_postfix is not None:
            postfix = "_" + options.out_postfix

        out_sft = ""
        if options.style_feat_type != 'gram':
            out_sft = "_" + options.style_feat_type

        out_preserve = ""
        if options.preserve_colors != 'none':
            if options.preserve_colors == 'before':
                out_preserve = "_bpc"
            else:
                out_preserve = "_pc"

        out_distr_weight = ""
        if options.style_distr_weight != 0.0:
            if options.style_distr_weight > 1e2:
                out_distr_weight = "_sdw%02de2" % (
                    int(options.style_distr_weight) // 100)
            else:
                out_distr_weight = "_sdw%03d" % (int(
                    options.style_distr_weight))

        options.output = "t_%s_%s_%s%04d_h%d_p%s_sw%05d%s_swe%02d_cwe%02d_as%03d_%s%s%s%s.jpg" % (
            content_filename, style_filename, options.optimizer,
            options.iterations, options.max_hierarchy, options.pooling,
            int(options.style_weight), out_distr_weight, out_stylewe,
            out_contentwe, out_ashift, options.network_type, out_sft,
            out_preserve, postfix)

        print("Using auto-generated output filename: %s" % (options.output))

    content_image = comimg.imread(options.content).astype(
        common.get_dtype_np())
    style_images = [
        comimg.imread(style).astype(common.get_dtype_np())
        for style in options.styles
    ]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)

    # TODO: remove this probably, since double doswnscale could affect quality
    #   however, it could save some time if the style image is a lot bigger than content
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    # TODO: change checkpoint naming convention - they should also include hierarchy level
    if options.checkpoint_output and ("%s" not in options.checkpoint_output):
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    print("\n>>> OUTPUT: %s\n" % (options.output))

    total_time = time.time()

    hierarchy_counter = 1

    ITER_DIVIDER_BASE = 1.5
    iter_divider = ITER_DIVIDER_BASE
    iter_hierarchy = [options.iterations]

    dim_first = (content_image.shape[0], content_image.shape[1])
    dim_hierarchy = [dim_first]
    dim_divider = 2
    dim_min = dim_first[0] if dim_first[0] < dim_first[1] else dim_first[1]
    while dim_min > 128 and hierarchy_counter < options.max_hierarchy:
        dim_new = tuple(x // dim_divider for x in dim_first)
        dim_hierarchy.append(dim_new)
        iter_hierarchy.append(int(options.iterations / iter_divider))

        dim_min = dim_new[0] if dim_new[0] < dim_new[1] else dim_new[1]

        dim_divider = dim_divider * 2
        iter_divider = iter_divider * ITER_DIVIDER_BASE
        hierarchy_counter = hierarchy_counter + 1

    num_channels = content_image.shape[2]
    h_initial_guess = content_image

    h_content = content_image

    # If noiseblend is not specified, it should be 0.0
    if options.initial_noiseblend is None:
        options.initial_noiseblend = 0.0

    hierarchy_steps = len(dim_hierarchy)
    for idx in reversed(range(hierarchy_steps)):
        dim = dim_hierarchy[idx]
        iter = iter_hierarchy[idx]

        # There is no point of getting below 25 iterations
        if hierarchy_steps > 1 and iter < 25:
            iter = 25

        is_last_hierarchy_level = (idx == 0)

        # x == dim[1], y == dim[0], meh
        print("Processing: %s / %d" % ((dim[1], dim[0]), iter))

        # If we only do 1 hierarchy step (e.g. no multgrid) - we don't need to resize content/initial
        if options.max_hierarchy > 1:
            h_initial_guess = scipy.misc.imresize(
                h_initial_guess, (dim[0], dim[1], num_channels))
            h_content = scipy.misc.imresize(content_image,
                                            (dim[0], dim[1], num_channels))

        coeff = 0.9
        h_initial_guess = h_initial_guess * coeff + h_content * (1.0 - coeff)

        target_shape = h_content.shape
        h_style_images = []
        for i in range(len(style_images)):
            style_scale = STYLE_SCALE
            if options.style_scales is not None:
                style_scale = options.style_scales[i]
            h_style_images.append(
                scipy.misc.imresize(
                    style_images[i],
                    style_scale * target_shape[1] / style_images[i].shape[1]))

        h_preserve_colors_coeff = 0.0
        if is_last_hierarchy_level:
            if options.preserve_colors == 'all' or options.preserve_colors == 'out':
                h_preserve_colors_coeff = 1.0
        else:
            if options.preserve_colors == 'all' or options.preserve_colors == 'interm':
                #h_preserve_colors_coeff = 0.5
                # we want biggest step to have least color preservation, to get proper style coloring, alpha = 1.0 on biggest step
                # -2 is due to idx starting from 0 and last layer not obeying this scheme
                h_preserve_alpha = (hierarchy_steps - 1 -
                                    idx) / (hierarchy_steps - 2)
                SMALLEST_STEP_PC = 1.0
                BIGGEST_STEP_PC = 0.3
                h_preserve_colors_coeff = BIGGEST_STEP_PC * h_preserve_alpha + SMALLEST_STEP_PC * (
                    1.0 - h_preserve_alpha)

        #print("Preserve colors coeff: %f" % (h_preserve_colors_coeff))

        for iteration, image in stylize(
                network_file=options.network_file,
                network_type=options.network_type,
                initial=h_initial_guess,
                #initial=None,
                initial_noiseblend=options.initial_noiseblend,
                content=h_content,
                #            styles=style_images,
                styles=h_style_images,
                preserve_colors_coeff=h_preserve_colors_coeff,
                preserve_colors_prior=(options.preserve_colors == 'before'),
                iterations=iter,
                content_weight=options.content_weight,
                content_weight_blend=options.content_weight_blend,
                style_weight=options.style_weight,
                style_distr_weight=options.style_distr_weight,
                style_layer_weight_exp=options.style_layer_weight_exp,
                style_blend_weights=style_blend_weights,
                style_feat_type=options.style_feat_type,
                tv_weight=options.tv_weight,
                learning_rate=options.learning_rate,
                beta1=options.beta1,
                beta2=options.beta2,
                epsilon=options.epsilon,
                ashift=options.ashift,
                pooling=options.pooling,
                optimizer=options.optimizer,
                print_iterations=options.print_iterations,
                checkpoint_iterations=options.checkpoint_iterations):
            output_file = None
            combined_rgb = image
            if iteration is not None:
                if options.checkpoint_output:
                    checkpoint_filename = options.checkpoint_output % (
                        "%04dx%04d-%04d" % (dim[0], dim[1], iteration))
                    comimg.imsave(checkpoint_filename, combined_rgb)
            else:
                h_initial_guess = image

        if is_last_hierarchy_level:
            if options.no_collage is None or options.no_collage == False:
                # For now, only works with the first style
                combined_rgb, _ = build_collage.build_collage(
                    np.clip(combined_rgb, 0, 255).astype(np.uint8),
                    np.clip(content_image, 0, 255).astype(np.uint8),
                    np.clip(style_images[0], 0, 255).astype(np.uint8), 'crop')

            # Last hierarchy level, we have the final output
            comimg.imsave(options.output, combined_rgb)
        else:
            # Not the last hierarchy level
            # True to save intermediate hierarchy shots
            if False:
                h_intermediate_name = "h_interm_%04dx%04d.jpg" % (dim[0],
                                                                  dim[1])
                comimg.imsave(h_intermediate_name, h_initial_guess)

        # True to save scaled content images
        if False:
            h_content_name = "h_content_%04dx%04d.jpg" % (dim[0], dim[1])
            comimg.imsave(h_content_name, h_content)

        # True to save scaled style images
        if False:
            for i in range(len(h_style_images)):
                h_style_name = "h_style%d_%04dx%04d.jpg" % (i, dim[0], dim[1])
                comimg.imsave(h_style_name, h_style_images[i])

    #if options.output:
    #    comimg.imsave(options.output, h_initial_guess)

    print("Total time: %fs" % (time.time() - total_time))
Пример #10
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            content=content_image,
            styles=style_images,
            iterations=options.iterations,
            content_weight=options.content_weight,
            style_weight=options.style_weight,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, image)
Пример #11
0
async def style_fusion(
        content_image: UploadFile = File(...),
        style_img: UploadFile = File(...),
        content_weight: int = Body(
            5e0, description="(默认5e0), 内容图片权重,值越大内容图片细节保存越多. "),
        style_weight: int = Body(
            5e2, description="(默认5e2), 样式图片权重, 值越大样式内容细节保存越多."),
        learning_rate: int = Body(1e1, description="(默认1e1), 学习率, 学习图像风格的快慢."),
        content_weight_blend: int = Body(
            1,
            description="(默认值1), 指定内容传输层的系数,数值越小越抽象,该值应在[0.0, 1.0]",
        ),
        tv_weight: int = Body(1e2, description="(默认1e2), 总变化正则化权重"),
        style_layer_weight_exp:
    int = Body(
        1,
        description=
        "(默认值1), 命令行参数可用于调整样式传输的“抽象”程度。较低的值意味着较精细的特征的样式传递将优于较粗糙的特征的样式传递,反之亦然。该值应在[0.0, 1.0]。"
    ),
        beta1: float = Body(0.9, description="(默认0.9), Adam:beta1参数"),
        beta2: float = Body(0.999, description="(默认0.999), Adam:beta2参数"),
        epsilon: float = Body(1e-08, description="(默认1e-08), Adam:epsilon参数"),
        style_scale: float = Body(1.0, description="(默认1.0)"),
        iterations: int = Body(
            1000, description="(默认1000), 迭代次数, 用于计算风格图和内容图像相似的次数"),
        pooling: str = Body(
            'max',
            description="(默认max 可选avg), 最大池化倾向于具有更好的细节样式传输, 但是在低频细节级别上可能会有麻烦"),
        progress_write: bool = Body(
            False, description="(默认Fasle), 将迭代进度数据写入OUTPUT目录"),
        progress_plot: bool = Body(
            False, description="(默认Fasle), 将迭代进度数据绘制到OUTPUT目录"),
        checkpoint_iterations: int = Body(
            100, description="(默认100), 可选None, 和其它整数型频率"),
        width: int = Body(None, description="(默认None), 输出图像宽度"),
        style_scales: int = Body(None, description="(默认None), 一个或多个样式标尺"),
        print_iterations: bool = Body(None, description="(默认None), 统计打印频率"),
        preserve_colors: bool = Body(
            None, description="(默认None 可选True), 仅样式转移, 保留颜色"),
        overwrite: bool = Body(True, description="(默认True, 可选None), 覆盖已保存的文件"),
        style_blend_weights: float = Body(
            None, description="(默认None 一般0.2), 样式混合权重, 该值应在[0.0, 1.0]"),
        initial: bool = Body(None, description="(默认None), 初始图像"),
        initial_noiseblend: float = Body(
            None, description="(默认None), 初始图像与标准化噪声混合的比率(如果未指定初始图像,则使用内容图像)")):
    content_image = await content_image.read()
    style_img = await style_img.read()
    content_image = io.BytesIO(content_image)
    style_img = io.BytesIO(style_img)
    content_image = imread(content_image)
    style_images = [imread(style_img)]
    network = './imagenet-vgg-verydeep-19.mat'  # 网络参数的路径(默认为imagenet-vgg-verydeep-19.mat)
    output = "./result/result.jpg"  # 默认融合风格后结果图像的输出路径.
    checkpoint_output = "./result/output_{:05}.jpg"  # (默认'./result/output_{:05}.jpg)', 可选 None, 用于保存每个阶段的风格迁移图像.
    key = 'TF_CPP_MIN_LOG_LEVEL'
    if key not in os.environ:
        os.environ[key] = '2'
    if not os.path.isfile(network):
        print("Network %s does not exist. (Did you forget to download it?)" %
              network)

    if [checkpoint_iterations, checkpoint_output].count(None) == 1:
        print(
            "use either both of checkpoint_output and checkpoint_iterations or neither"
        )

    if checkpoint_output is not None:
        if re.match(r'^.*(\{.*\}|%.*).*$', checkpoint_output) is None:
            print(
                "To save intermediate images, the checkpoint_output parameter must contain placeholders (e.g. `foo_{}.jpg` or `foo_%d.jpg`)"
            )

    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        if style_scales is not None:
            style_scale = style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if initial_noiseblend is None:
            initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random
        # generated initial guess
        if initial_noiseblend is None:
            initial_noiseblend = 1.0
        if initial_noiseblend < 1.0:
            initial = content_image

    # try saving a dummy image to the output path to make sure that it's writable
    if os.path.isfile(output) and not overwrite:
        raise IOError("%s already exists, will not replace it without "
                      "the '--overwrite' flag" % output)
    try:
        imsave(output, np.zeros((500, 500, 3)))
    except:
        raise IOError('%s is not writable or does not have a valid file '
                      'extension for an image file' % output)

    loss_arrs = None
    for iteration, image, loss_vals in stylize(
            network=str(network),
            initial=initial,
            initial_noiseblend=initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=preserve_colors,
            iterations=iterations,
            content_weight=content_weight,
            content_weight_blend=content_weight_blend,
            style_weight=style_weight,
            style_layer_weight_exp=style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=tv_weight,
            learning_rate=learning_rate,
            beta1=beta1,
            beta2=beta2,
            epsilon=epsilon,
            pooling=pooling,
            print_iterations=print_iterations,
            checkpoint_iterations=checkpoint_iterations,
    ):
        if (image is not None) and (checkpoint_output is not None):
            imsave(fmt_imsave(checkpoint_output, iteration), image)
        if (loss_vals is not None) \
                and (progress_plot or progress_write):
            if loss_arrs is None:
                itr = []
                loss_arrs = OrderedDict((key, []) for key in loss_vals.keys())
            for key, val in loss_vals.items():
                loss_arrs[key].append(val)
            itr.append(iteration)

    imsave(output, image)

    if progress_write:
        fn = "{}/progress.txt".format(os.path.dirname(output))
        tmp = np.empty((len(itr), len(loss_arrs) + 1), dtype=float)
        tmp[:, 0] = np.array(itr)
        for ii, val in enumerate(loss_arrs.values()):
            tmp[:, ii + 1] = np.array(val)
        np.savetxt(fn, tmp, header=' '.join(['itr'] + list(loss_arrs.keys())))

    if progress_plot:
        import matplotlib
        matplotlib.use('Agg')
        from matplotlib import pyplot as plt
        fig, ax = plt.subplots()
        for key, val in loss_arrs.items():
            ax.semilogy(itr, val, label=key)
        ax.legend()
        ax.set_xlabel("iterations")
        ax.set_ylabel("loss")
        fig.savefig("{}/progress.png".format(os.path.dirname(output)))
    return "save img success..."
Пример #12
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    # Loading the content and style image.
    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    # Resize the content image if it is requested in the command line.
    width = options.width
    if width is not None:
        new_shape = (int(math.floor(float(content_image.shape[0]) /
                content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)

    # Resize the style image if it is requested in the command line.
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])

    # Since we can have more than 1 style images, we assign different weight for each style.
    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        style_blend_weights = [1.0/len(style_images) for _ in style_images] # Equal weight for each style.
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight
                               for weight in style_blend_weights]

    # Configure an initial image that we eventually apply the content and style to.
    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # If initial guess is specified but not noiseblend - set noiseblend to 0
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        if options.initial_noiseblend is None:
            # If inital and noiseblend are NOT provided, use random noise
            options.initial_noiseblend = 1.0
        elif options.initial_noiseblend < 1.0:
            # else the content image
            initial = content_image

    # Generate output image at checkpoint as foo*.jpg if requested in the command line.
    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
        network=options.network,
        initial=initial,
        initial_noiseblend=options.initial_noiseblend,
        content=content_image,
        styles=style_images,
        preserve_colors=options.preserve_colors,
        iterations=options.iterations,
        content_weight=options.content_weight,
        content_weight_blend=options.content_weight_blend,
        style_weight=options.style_weight,
        style_layer_weight_exp=options.style_layer_weight_exp,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        beta1=options.beta1,
        beta2=options.beta2,
        epsilon=options.epsilon,
        pooling=options.pooling,
        print_iterations=options.print_iterations,
        checkpoint_iterations=options.checkpoint_iterations
    ):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)
    CONTENT_IMAGES = options.content
    count = 0
    try:
        os.system("mkdir final")
        os.system("mkdir final/" + name_kernel + "")
        os.system("mkdir final/" + name_kernel + "_pickle")
    except:
        print("dirctory stucture already exist")

    for c in CONTENT_IMAGES:
        for s in STYLE_IMAGES:
            for sw in RANGE_SW:
                locat = os.listdir("final/" + name_kernel + "/")
                if (not (sw in locat)):
                    os.system("mkdir final/" + name_kernel + "/" + str(sw))
                    os.system("mkdir final/" + name_kernel + "_pickle/" +
                              str(sw))

                for sig in RANGE_SIGMA:
                    c_image = imread(c)
                    s_image = [imread("examples/style/" + s)]

                    width = None
                    if width is not None:
                        new_shape = (int(
                            math.floor(
                                float(c_image.shape[0]) / c_image.shape[1] *
                                width)), width)
                        c_image = scipy.misc.imresize(c_image, new_shape)
                    target_shape = c_image.shape
                    for i in range(len(s_image)):
                        style_scale = STYLE_SCALE
                        if options.style_scales is not None:
                            style_scale = options.style_scales[i]
                        s_image[i] = scipy.misc.imresize(
                            s_image[i], style_scale * target_shape[1] /
                            s_image[i].shape[1])

                    style_blend_weights = options.style_blend_weights
                    if style_blend_weights is None:
                        # default is equal weights
                        style_blend_weights = [1.0 / 1]
                    else:
                        total_blend_weight = sum(style_blend_weights)
                        style_blend_weights = [
                            weight / total_blend_weight
                            for weight in style_blend_weights
                        ]

                    initial = options.initial
                    if initial is not None:
                        initial = scipy.misc.imresize(imread(initial),
                                                      c_image.shape[:2])
                        # Initial guess is specified, but not noiseblend - no noise should be blended
                        if options.initial_noiseblend is None:
                            options.initial_noiseblend = 0.0
                    else:
                        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
                        if options.initial_noiseblend is None:
                            options.initial_noiseblend = 1.0
                        if options.initial_noiseblend < 1.0:
                            initial = c_image

                    if options.checkpoint_output and "%s" not in options.checkpoint_output:
                        parser.error(
                            "To save intermediate images, the checkpoint output "
                            "parameter must contain `%s` (e.g. `foo%s.jpg`)")

                    count += 1
                    sname = c.split("/")[-1][0:-4] + "_" + s[0:-4] + "_" + str(
                        sig) + "_" + str(sw) + ".jpg"
                    print("loop ==> " + str(count) + "of" + str(
                        len(CONTENT_IMAGES) * len(STYLE_IMAGES) *
                        len(RANGE_SIGMA) * len(RANGE_SW)))

                    # print("--content " + c + " --styles " + s + " --output final/exp/" + sname + " --iterations 1000 --style-weight " + str(sw))
                    files_in_folder = os.listdir("final/" + name_kernel + "/" +
                                                 str(sw) + "/")
                    if (sname in files_in_folder):
                        print("file already exist")
                        continue

                    for iteration, image, dict in stylize(
                            network=options.network,
                            initial=initial,
                            initial_noiseblend=options.initial_noiseblend,
                            content=c_image,
                            styles=s_image,
                            preserve_colors=options.preserve_colors,
                            iterations=ITERATIONS,
                            content_weight=options.content_weight,
                            content_weight_blend=options.content_weight_blend,
                            style_weight=sw,
                            style_layer_weight_exp=options.
                            style_layer_weight_exp,
                            style_blend_weights=style_blend_weights,
                            tv_weight=options.tv_weight,
                            learning_rate=options.learning_rate,
                            beta1=options.beta1,
                            beta2=options.beta2,
                            epsilon=options.epsilon,
                            pooling=options.pooling,
                            print_iterations=PRINT_ITERATIONS,
                            checkpoint_iterations=options.
                            checkpoint_iterations,
                            exp_sigma=500,
                            mat_sigma=1e2,
                            mat_rho=500,
                            gamma_rho=sig,
                            gamma=g,
                            text_to_print=sname,
                            kernel=KERNEL,
                            d=1):
                        print(dict)
                        try:
                            combined_rgb = image
                            imsave(
                                "final/" + name_kernel + "/" + str(sw) + "/" +
                                sname, combined_rgb)
                            with open(
                                    "final/" + name_kernel + "_pickle/" +
                                    str(sw) + "/" + sname[0:-4] + ".pkl",
                                    'wb') as f:
                                pickle.dump(dict, f)

                        except:
                            print("============= ERROR =============")
def test(content_image, result_path, save_path):
    CONTENT_IMAGE = imread(content_image)
    image_shape = (1,) + CONTENT_IMAGE.shape
    input_image = [CONTENT_IMAGE]
    style_images = [imread(style) for style in STYLE_IN]

    target_shape = CONTENT_IMAGE.shape

    for i in range(len(style_images)):
        style_scale = STYLE_SCALE

        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                                              target_shape[1] / style_images[i].shape[1])
    style_blend_weights = STYLE_BLEND_WEIGHTS
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight / total_blend_weight
                               for weight in style_blend_weights]

    with tf.Graph().as_default(), tf.Session() as sess:
        X_content = tf.placeholder(tf.float32, shape=image_shape, name="X_content")

        preds = residual_padding.net(X_content / 255.0)

        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, save_path)

        feed_dict = {
            X_content: input_image
        }

        result = preds.eval(feed_dict = feed_dict)

        initial = result[0]
        initial_noiseblend = 0.0
        for iteration, image in stylize(
                network=VGG_PATH,
                initial=initial,
                initial_noiseblend=initial_noiseblend,
                content=CONTENT_IMAGE,
                styles=style_images,
                preserve_colors=PRESERVE_COLORS,
                iterations=ITERATIONS,
                content_weight=CONTENT_WEIGHT,
                content_weight_blend=CONTENT_WEIGHT_BLEND,
                style_weight=STYLE_WEIGHT,
                style_layer_weight_exp=STYLE_LAYER_WEIGHT_EXP,
                style_blend_weights=style_blend_weights,
                tv_weight=TV_WEIGHT,
                learning_rate=LEARNING_RATE,
                beta1=BETA1,
                beta2=BETA2,
                epsilon=EPSILON,
                pooling=POOLING,
                print_iterations=PRINT_ITERATIONS,
                checkpoint_iterations=CHECK_POINT
        ):
            output_file = None
            combined_rgb = image
            if iteration is not None:
                if CHECK_POINT_PATH:
                    output_file = FINAL_CHECK_POINT_PATH % iteration
            else:
                output_file = OUTPUT
            if output_file:
                imsave(output_file, combined_rgb)
Пример #15
0
def create_sketch(content_img, out_name):
    ooutput = out_name
    if not os.path.isfile(VGG_PATH):
        parser.error(
            "Network does not exist. (Did you forget to download it?)")

    content_image = imread(content_img)
    ostyles = []
    files = os.listdir(SKETCH_DIRECTORY)
    for f in files:
        ostyles.append(SKETCH_DIRECTORY + f)
    style_images = [imread(style) for style in ostyles]

    width = None
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape

    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
    #   if options.style_scales is not None:
    #       style_scale = options.style_scales[i]
    #   style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
    #           target_shape[1] / style_images[i].shape[1])

    style_blend_weights = None
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = None

    try:
        imsave(ooutput, np.zeros((500, 500, 3)))
    except:
        raise IOError(
            'Not writable or does not have a valid file extension for an image file'
        )

    for iteration, image in stylize(
            network=VGG_PATH,
            initial=initial,
            initial_noiseblend=1.0,
            content=content_image,
            styles=style_images,
            preserve_colors=None,
            iterations=ITERATIONS,
            content_weight=CONTENT_WEIGHT,
            content_weight_blend=CONTENT_WEIGHT_BLEND,
            style_weight=STYLE_WEIGHT,
            style_layer_weight_exp=STYLE_LAYER_WEIGHT_EXP,
            style_blend_weights=style_blend_weights,
            tv_weight=TV_WEIGHT,
            learning_rate=LEARNING_RATE,
            beta1=BETA1,
            beta2=BETA2,
            epsilon=EPSILON,
            pooling=POOLING,
            print_iterations=None,
            checkpoint_iterations=None):
        output_file = None
        combined_rgb = image
        if iteration is None:
            output_file = ooutput
        if output_file:
            imsave(output_file, transfer_image(combined_rgb))
Пример #16
0
def stylyze(options, callback):

    parser = build_parser()
    if options is None:
        key = 'TF_CPP_MIN_LOG_LEVEL'
        if key not in os.environ:
            os.environ[key] = '2'

        options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to "
                     "download it?)" % options.network)

    if [options.checkpoint_iterations,
            options.checkpoint_output].count(None) == 1:
        parser.error("use either both of checkpoint_output and "
                     "checkpoint_iterations or neither")

    if options.checkpoint_output is not None:
        if re.match(r'^.*(\{.*\}|%.*).*$', options.checkpoint_output) is None:
            parser.error("To save intermediate images, the checkpoint_output "
                         "parameter must contain placeholders (e.g. "
                         "`foo_{}.jpg` or `foo_%d.jpg`")

    content_image_arr = [imread(i) for i in options.content]
    style_images = [imread(style) for style in options.styles]

    width_arr = options.width
    for i in range(len(content_image_arr)):
        width = width_arr[i]
        content_image = content_image_arr[i]
        if width is not None:
            new_shape = (int(
                math.floor(
                    float(content_image.shape[0]) / content_image.shape[1] *
                    width)), width)
            content_image = scipy.misc.imresize(content_image, new_shape)
            content_image_arr[i] = content_image
        target_shape = content_image.shape
        for j in range(len(style_images)):
            style_scale = STYLE_SCALE
            if options.style_scales is not None:
                style_scale = options.style_scales[j]
            style_images[j] = scipy.misc.imresize(
                style_images[j],
                style_scale * target_shape[1] / style_images[j].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial_arr = content_image_arr

    # try saving a dummy image to the output path to make sure that it's writable
    output_arr = options.output
    for output in output_arr:
        if os.path.isfile(output) and not options.overwrite:
            raise IOError("%s already exists, will not replace it without "
                          "the '--overwrite' flag" % output)
        try:
            imsave(output, np.zeros((500, 500, 3)))
        except:
            raise IOError('%s is not writable or does not have a valid file '
                          'extension for an image file' % output)

    vgg_weights, vgg_mean_pixel = vgg.load_net(options.network)

    style_shapes = [(1, ) + style.shape for style in style_images]
    style_features = [{} for _ in style_images]

    layer_weight = 1.0
    style_layers_weights = {}
    for style_layer in STYLE_LAYERS:
        style_layers_weights[style_layer] = layer_weight
        layer_weight *= options.style_layer_weight_exp

    # normalize style layer weights
    layer_weights_sum = 0
    for style_layer in STYLE_LAYERS:
        layer_weights_sum += style_layers_weights[style_layer]
    for style_layer in STYLE_LAYERS:
        style_layers_weights[style_layer] /= layer_weights_sum

    # compute style features in feedforward mode
    for i in range(len(style_images)):
        g = tf.Graph()
        with g.as_default(), g.device('/cpu:0'), tf.Session() as sess:
            image = tf.placeholder('float', shape=style_shapes[i])
            net = vgg.net_preloaded(vgg_weights, image, options.pooling)
            style_pre = np.array(
                [vgg.preprocess(style_images[i], vgg_mean_pixel)])
            for layer in STYLE_LAYERS:
                features = net[layer].eval(feed_dict={image: style_pre})
                features = np.reshape(features, (-1, features.shape[3]))
                gram = np.matmul(features.T, features) / features.size
                style_features[i][layer] = gram

    initial_content_noise_coeff = 1.0 - options.initial_noiseblend

    for i in range(len(content_image_arr)):
        Data.save_step(Data.get_step() + 1)
        loss_arrs = None
        for iteration, image, loss_vals in stylize(
                initial=initial_arr[i],
                content=content_image_arr[i],
                preserve_colors=options.preserve_colors,
                iterations=options.iterations,
                content_weight=options.content_weight,
                content_weight_blend=options.content_weight_blend,
                tv_weight=options.tv_weight,
                learning_rate=options.learning_rate,
                beta1=options.beta1,
                beta2=options.beta2,
                epsilon=options.epsilon,
                pooling=options.pooling,
                initial_content_noise_coeff=initial_content_noise_coeff,
                style_images=style_images,
                style_layers_weights=style_layers_weights,
                style_weight=options.style_weight,
                style_blend_weights=style_blend_weights,
                vgg_weights=vgg_weights,
                vgg_mean_pixel=vgg_mean_pixel,
                style_features=style_features,
                print_iterations=options.print_iterations,
                checkpoint_iterations=options.checkpoint_iterations,
                callback=callback):
            if (image is not None) and (options.checkpoint_output is not None):
                imsave(fmt_imsave(options.checkpoint_output, iteration), image)
            if (loss_vals is not None) \
                    and (options.progress_plot or options.progress_write):
                if loss_arrs is None:
                    itr = []
                    loss_arrs = OrderedDict(
                        (key, []) for key in loss_vals.keys())
                for key, val in loss_vals.items():
                    loss_arrs[key].append(val)
                itr.append(iteration)

        imsave(options.output[i], image)

        if options.progress_write:
            fn = "{}/progress.txt".format(os.path.dirname(options.output[i]))
            tmp = np.empty((len(itr), len(loss_arrs) + 1), dtype=float)
            tmp[:, 0] = np.array(itr)
            for ii, val in enumerate(loss_arrs.values()):
                tmp[:, ii + 1] = np.array(val)
            np.savetxt(fn,
                       tmp,
                       header=' '.join(['itr'] + list(loss_arrs.keys())))

        if options.progress_plot:
            import matplotlib
            matplotlib.use('Agg')
            from matplotlib import pyplot as plt
            fig, ax = plt.subplots()
            for key, val in loss_arrs.items():
                ax.semilogy(itr, val, label=key)
            ax.legend()
            ax.set_xlabel("iterations")
            ax.set_ylabel("loss")
            fig.savefig("{}/progress.png".format(
                os.path.dirname(options.output[i])))
Пример #17
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("There is no network model")

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in xrange(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save image, parmeter must contain xxx.jpg name")

    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
Пример #18
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    content_image = imread(options.content) # 命令行中参数传进来
    style_images = [imread(style_img) for style_img in options.styles] # 命令行中传进的参数

    width = options.width
    if width is not None: # 假设有width参数传进来, 那么content的大小要重新设置, 即按照比例进行缩放
        # 如height/weight = new_h/new_w, 其中new_w是给定的
        # 则new_h = (height/weight)/new_w
        new_shape = (int(math.floor(float(content_image.shape[0]) / content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image,new_shape)
    target_shape = content_image.shape # 生成图片的大小
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None: # 若style的尺度有指定, 则需要对styles图片集进行缩放
            style_scale = options.style_scales[i] # 每幅图都制定了尺度
        # imresize 传进的size参数
        # size: int, float or tuple
        # *int - Percentage of current size.
        # *float - Fraction of current size.
        # *tuple - Size of the output image(height, width).
        # 那么这里使用的是float, 尺寸: width/height
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale * target_shape[1] / style_images[i].shape[1]) # 每张图按照指定尺度读入

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # 设定默认的相等的weights
        style_blend_weights = [1.0/len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight for weight in style_blend_weights] # 对style_blend_weights进行标准化

    initial = options.initial
    if initial is not None: # 表明生成图像是指定的
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2]) # 生成图像的大小需要进行变换
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else: # 如果没有提供初始图,也没有提供初始的noiseblend,则生成随机噪声图片
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image
    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output parameter must contain '%s' (e.g. `foo%s.jpg`)")

    # 为了保证图像可以写入,设置写入虚拟图像
    if os.path.isfile(options.output) and not options.overwrite:
        raise IOError("%s already exists, will not replace it without the '--overwrite' flag" % options.output)
    try:
        imsave(options.output, np.zeros((500,500,3)))
    except:
        raise IOError('%s is not writable or does not have a valid file extension for an image file' % options.output)

    for iteration, image in stylize.stylize(network = options.network,initial = initial,initial_noiseblend = options.initial_noiseblend,
                                            content = content_image,styles = style_images,preserve_colors = options.preserve_colors,
                                            iterations = options.iterations,content_weight = options.content_weight,
                                            content_weight_blend = options.content_weight_blend,style_weight = options.style_weight,
                                            style_layer_weight_exp = options.style_layer_weight_exp,style_blend_weights = style_blend_weights,
                                            tv_weight = options.tv_weight,learning_rate = options.learning_rate,beta1 = options.beta1,
                                            beta2 = options.beta2,epsilon = options.epsilon, pooling = options.pooling,
                                            print_iterations = options.print_iterations, checkpoint_iterations = options.checkpoint_iterations):
        # stylize 每一次迭代返回一个iteration and image
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file,combined_rgb)
Пример #19
0
def main():

    # https://stackoverflow.com/a/42121886
    key = 'TF_CPP_MIN_LOG_LEVEL'
    if key not in os.environ:
        os.environ[key] = '2'

    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to "
                     "download it?)" % options.network)

    if [options.checkpoint_iterations,
            options.checkpoint_output].count(None) == 1:
        parser.error("use either both of checkpoint_output and "
                     "checkpoint_iterations or neither")

    if options.checkpoint_output is not None:
        if re.match(r'^.*(\{.*\}|%.*).*$', options.checkpoint_output) is None:
            parser.error("To save intermediate images, the checkpoint_output "
                         "parameter must contain placeholders (e.g. "
                         "`foo_{}.jpg` or `foo_%d.jpg`")

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = transform.rescale(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = transform.rescale(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = options.initial
    if initial is not None:
        initial = transform.rescale(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random
        # generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    # try saving a dummy image to the output path to make sure that it's writable
    if os.path.isfile(options.output) and not options.overwrite:
        raise IOError("%s already exists, will not replace it without "
                      "the '--overwrite' flag" % options.output)
    try:
        imsave(options.output, np.zeros((500, 500, 3)))
    except:
        raise IOError('%s is not writable or does not have a valid file '
                      'extension for an image file' % options.output)

    loss_arrs = None
    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            content=content_image,
            styles=style_images,
            iterations=options.iterations,
            content_weight=options.content_weight,
            style_weight=options.style_weight,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, image)
Пример #20
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    # Loading the content and style image.
    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    # Resize the content image if it is requested in the command line.
    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)

    # Resize the style image if it is requested in the command line.
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    # Since we can have more than 1 style images, we assign different weight for each style.
    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        style_blend_weights = [1.0 / len(style_images) for _ in style_images
                               ]  # Equal weight for each style.
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    # Configure an initial image that we eventually apply the content and style to.
    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # If initial guess is specified but not noiseblend - set noiseblend to 0
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        if options.initial_noiseblend is None:
            # If inital and noiseblend are NOT provided, use random noise
            options.initial_noiseblend = 1.0
        elif options.initial_noiseblend < 1.0:
            # else the content image
            initial = content_image

    # Generate output image at checkpoint as foo*.jpg if requested in the command line.
    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
def transform(content_file, style_file, iterations):

    # https://stackoverflow.com/a/42121886
    key = 'TF_CPP_MIN_LOG_LEVEL'
    if key not in os.environ:
        os.environ[key] = '2'

    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(VGG_PATH):
        parser.error("Network %s does not exist. (Did you forget to "
                     "download it?)" % VGG_PATH)

    content_image = content_file
    style_images = [style_file]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random
        # generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    loss_arrs = None
    for iteration, image, loss_vals in stylize(
            network=VGG_PATH,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=options.preserve_colors,
            iterations=iterations,
            content_weight=CONTENT_WEIGHT,
            content_weight_blend=CONTENT_WEIGHT_BLEND,
            style_weight=STYLE_WEIGHT,
            style_layer_weight_exp=STYLE_LAYER_WEIGHT_EXP,
            style_blend_weights=style_blend_weights,
            tv_weight=TV_WEIGHT,
            learning_rate=LEARNING_RATE,
            beta1=BETA1,
            beta2=BETA2,
            epsilon=EPSILON,
            pooling=POOLING,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations,
    ):
        continue

    return image
Пример #22
0
def run():
    # https://stackoverflow.com/a/42121886
    key = 'TF_CPP_MIN_LOG_LEVEL'
    if key not in os.environ:
        os.environ[key] = '2'

    if not os.path.isfile(options.network):
        print os.getcwd()
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = imageread(options.content)
    style_image = imageread(options.styles)

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = spm.imresize(content_image, new_shape)
    target_shape = content_image.shape
    style_scale = default_args.STYLE_SCALE
    style_image = spm.imresize(
        style_image, style_scale * target_shape[1] / style_image.shape[1])

    initial = options.initial
    if initial is not None:
        initial = spm.imresize(imageread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    #print options.overwrite ##overwrite = False? is getting overriden after run() called - unsure why
    # try saving a dummy image to the output path to make sure that it's writable
    if os.path.isfile(options.output) and not options.overwrite:
        raise IOError(
            "%s already exists, will not replace it without the '--overwrite' flag"
            % options.output)
    try:
        imsave(options.output, np.zeros((500, 500, 3)))
    except:
        raise IOError(
            '%s is not writable or does not have a valid file extension for an image file'
            % options.output)

    loss_arrs = None
    for iteration, image, loss_vals in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_image,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations,
    ):
        if (image is not None) and options.checkpoint_output:
            imsave(options.checkpoint_output % iteration, image)
        if (loss_vals is not None) \
                and (options.progress_plot or options.progress_write):
            if loss_arrs is None:
                itr = []
                loss_arrs = OrderedDict((key, []) for key in loss_vals.keys())
            for key, val in loss_vals.items():
                loss_arrs[key].append(val)
            itr.append(iteration)

    print options.output
    imsave(options.output, image)

    if options.progress_write:
        fn = "{}/progress.txt".format(os.path.dirname(options.output))
        tmp = np.empty((len(itr), len(loss_arrs) + 1), dtype=float)
        tmp[:, 0] = np.array(itr)
        for ii, val in enumerate(loss_arrs.values()):
            tmp[:, ii + 1] = np.array(val)
        np.savetxt(fn, tmp, header=' '.join(['itr'] + list(loss_arrs.keys())))

    if options.progress_plot:
        import matplotlib
        matplotlib.use('Agg')
        from matplotlib import pyplot as plt
        fig, ax = plt.subplots()
        for key, val in loss_arrs.items():
            ax.semilogy(itr, val, label=key)
        ax.legend()
        ax.set_xlabel("iterations")
        ax.set_ylabel("loss")
        fig.savefig("{}/progress.png".format(os.path.dirname(options.output)))
Пример #23
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    
    # Reconstruct image
    # Construct Style Templates...
    
    
    # Try a few different stylize settings....
    
    
    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    # grab the content and style images.
    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]
    
    # resize the image by width
    width = options.width
    if width is not None:
        # scale the image shape by the given width (h = H*(w/W)
        new_shape = (int(math.floor(float(content_image.shape[0]) /
                content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    # target is the same shape as the content image
    target_shape = content_image.shape
    
    # set up the array of style images to be applied...
    for i in range(len(style_images)):
        # what is style scale used for?
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        #rescale the style image to be scaled to the content image..
        # this probably helps the texture of the style to size correctly to the new image
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])

    #what do these weights do?
    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0/len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight
                               for weight in style_blend_weights]

    #allow for an initial guess... and allow noise to be added
    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image
    
    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")
    
    for iteration, image in stylize(
        network=options.network,
        initial=initial,
        initial_noiseblend=options.initial_noiseblend,
        content=content_image,
        styles=style_images,
        preserve_colors=options.preserve_colors,
        iterations=options.iterations,
        content_weight=options.content_weight,
        content_weight_blend=options.content_weight_blend,
        style_weight=options.style_weight,
        style_layer_weight_exp=options.style_layer_weight_exp,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        beta1=options.beta1,
        beta2=options.beta2,
        epsilon=options.epsilon,
        pooling=options.pooling,
        print_iterations=options.print_iterations,
        checkpoint_iterations=options.checkpoint_iterations,
        rContent = options.rContent,
        rStyle = options.rStyle,
        label = options.label
        
    ):
        
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
Пример #24
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = None
    if options.content != '':
        print('reading content image %s' % options.content)
        content_image = read_and_resize_images(options.content, options.height,
                                               options.width)
    style_images = read_and_resize_images(
        options.styles, None, None)  # We don't need to resize style images.

    target_shape = (1, int(options.height), int(options.width), 3)

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    output_semantic_mask = None
    style_semantic_masks = None
    if options.use_semantic_masks:
        assert options.output_semantic_mask is not None and options.output_semantic_mask != ''
        assert (len(options.style_semantic_masks) == len(options.styles))
        output_semantic_mask_paths = get_all_image_paths_in_dir(
            options.output_semantic_mask)
        output_semantic_mask = read_and_resize_bw_mask_images(
            output_semantic_mask_paths, options.height, options.width, 1,
            options.semantic_masks_num_layers)
        style_semantic_masks = []
        for style_i, style_semantic_mask_dir in enumerate(
                options.style_semantic_masks):
            style_semantic_mask_paths = get_all_image_paths_in_dir(
                style_semantic_mask_dir)
            style_semantic_masks.append(
                read_and_resize_bw_mask_images(
                    style_semantic_mask_paths, style_images[style_i].shape[0],
                    style_images[style_i].shape[1], 1,
                    options.semantic_masks_num_layers))

    initial = options.initial
    if initial is not None:
        initial = imread(initial, shape=(options.height, options.width))

    content_img_style_weight_mask = None
    if options.content_img_style_weight_mask and options.content_img_style_weight_mask != '':
        content_img_style_weight_mask = (read_and_resize_bw_mask_images(
            [options.content_img_style_weight_mask], options.height,
            options.width, 1, 1))

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    checkpoint_dir = os.path.dirname(options.checkpoint_output)
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    output_dir = os.path.dirname(options.output)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    for iteration, image in stylize(
            network=options.network,
            content=content_image,
            styles=style_images,
            shape=target_shape,
            iterations=options.iterations,
            content_weight=options.content_weight,
            style_weight=options.style_weight,
            tv_weight=options.tv_weight,
            style_blend_weights=style_blend_weights,
            learning_rate=options.learning_rate,
            initial=initial,
            use_mrf=options.use_mrf,
            use_semantic_masks=options.use_semantic_masks,
            output_semantic_mask=output_semantic_mask,
            style_semantic_masks=style_semantic_masks,
            semantic_masks_weight=options.semantic_masks_weight,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations,
            semantic_masks_num_layers=options.semantic_masks_num_layers,
            content_img_style_weight_mask=content_img_style_weight_mask):
        output_file = None
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = str(options.output)
        if output_file:
            imsave(output_file, image)
Пример #25
0
content = 'examples/1-content.jpg'  # 此处为内容图片路径,可修改
styles = ['examples/1-style.jpg']  # 此处为风格图片路径,可修改

content_image = imread(content)  # 读取content图片
style_images = [imread(style) for style in styles]  # 读取style图片,可以有多个
initial_noiseblend = 1.0
initial = content_image
style_blend_weights = [1.0 / len(style_images) for _ in style_images]
for iteration, image in stylize(network=VGG_PATH,
                                initial=initial,
                                initial_noiseblend=initial_noiseblend,
                                content=content_image,
                                styles=style_images,
                                preserve_colors=None,
                                iterations=ITERATIONS,
                                content_weight=CONTENT_WEIGHT,
                                content_weight_blend=CONTENT_WEIGHT_BLEND,
                                style_weight=STYLE_WEIGHT,
                                style_layer_weight_exp=STYLE_LAYER_WEIGHT_EXP,
                                style_blend_weights=style_blend_weights,
                                tv_weight=TV_WEIGHT,
                                learning_rate=LEARNING_RATE,
                                beta1=BETA1,
                                beta2=BETA2,
                                epsilon=EPSILON,
                                pooling=POOLING,
                                print_iterations=None,
                                checkpoint_iterations=None):
    print(iteration)
Пример #26
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    # Check VGG path
    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    # Load Content and styles images
    #  And resize the style to the content size
    content = utils.imread(options.content)
    if options.semantic_transfer:
        masks = utils.maskread(options.masks)
        sem_styles = [utils.imread(s) for s in options.semantic_styles]
        for idx, img in enumerate(sem_styles):
            sem_styles[idx] = scipy.misc.imresize(img, content.shape)
        style = None
    else:
        style = utils.imread(options.style)
        style = scipy.misc.imresize(style, content.shape)
        masks = None
        sem_styles = None

    # Image initialisation: noise or content image?
    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(utils.imread(initial), content.shape,
                                      'bilinear')
    else:
        initial = None

    # Checkpoint format
    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    # Begin optimization
    for iteration, image in stylize(
            network=options.network,
            semantic_transfer=options.semantic_transfer,
            initial=initial,
            content=content,
            style=style,
            mask=masks,
            sem_style_images=sem_styles,
            gradient_capping=options.gradient_capping,
            capped_objs=options.capped_objs,
            auto_tuning=options.auto_tuning,
            erosion=options.erosion,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            style_weight=options.style_weight,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations):
        output_file = None
        combined_rgb = image

        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            utils.imsave(output_file, combined_rgb)
Пример #27
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    _, _, fileList = walk_dir("/home/dnn/Project/gitproject/neural-style/content")

    if len(fileList) == 0:
        print "there are no pictures preparing for processing"
        return


    for file in fileList:
        content_image = imread(os.path.join("content", file))
        style_images = imread("style/1-style.jpg")

        width = options.width
        if width is not None:
            new_shape = (int(math.floor(float(content_image.shape[0]) /
                                        content_image.shape[1] * width)), width)
            content_image = scipy.misc.imresize(content_image, new_shape)
        target_shape = content_image.shape
        for i in range(len(style_images)):
            style_scale = STYLE_SCALE
            if options.style_scales is not None:
                style_scale = options.style_scales[i]
            style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                                                  target_shape[1] / style_images[i].shape[1])

        style_blend_weights = options.style_blend_weights
        if style_blend_weights is None:
            # default is equal weights
            style_blend_weights = [1.0 / len(style_images) for _ in style_images]
        else:
            total_blend_weight = sum(style_blend_weights)
            style_blend_weights = [weight / total_blend_weight
                                   for weight in style_blend_weights]

        initial = options.initial
        if initial is not None:
            initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])

        if options.checkpoint_output and "%s" not in options.checkpoint_output:
            parser.error("To save intermediate images, the checkpoint output "
                         "parameter must contain `%s` (e.g. `foo%s.jpg`)")

        for iteration, image in stylize(
            network=options.network,
            initial=initial,
            content=content_image,
            styles=style_images,
            iterations=options.iterations,
            content_weight=options.content_weight,
            style_weight=options.style_weight,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations
        ):
            output_file = None
            if iteration is not None:
                if options.checkpoint_output:
                    output_file = options.checkpoint_output % iteration
            else:
                output_file = os.path.join("output", file)
            if output_file:
                imsave(output_file, image)
                imremove(os.path.join("content", file))
Пример #28
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(math.floor(float(content_image.shape[0]) /
                content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0/len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight
                               for weight in style_blend_weights]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    # try saving a dummy image to the output path to make sure that it's writable
    try:
        imsave(options.output, np.zeros((500, 500, 3)))
    except:
        raise IOError('%s is not writable or does not have a valid file extension for an image file' % options.output)

    for iteration, image in stylize(
        network=options.network,
        initial=initial,
        initial_noiseblend=options.initial_noiseblend,
        content=content_image,
        styles=style_images,
        preserve_colors=options.preserve_colors,
        iterations=options.iterations,
        content_weight=options.content_weight,
        content_weight_blend=options.content_weight_blend,
        style_weight=options.style_weight,
        style_layer_weight_exp=options.style_layer_weight_exp,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        beta1=options.beta1,
        beta2=options.beta2,
        epsilon=options.epsilon,
        pooling=options.pooling,
        print_iterations=options.print_iterations,
        checkpoint_iterations=options.checkpoint_iterations
    ):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(math.floor(float(content_image.shape[0]) /
                content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0/len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight
                               for weight in style_blend_weights]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
        network=options.network,
        initial=initial,
        content=content_image,
        styles=style_images,
        iterations=options.iterations,
        content_weight=options.content_weight,
        style_weight=options.style_weight,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        print_iterations=options.print_iterations,
        checkpoint_iterations=options.checkpoint_iterations
    ):
        output_file = None
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, image)
Пример #30
0
def main():
    # This will print all array values in full
    np.set_printoptions(threshold=np.nan)

    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    # Load the vgg weights in advance
    vgg_weights, vgg_mean_pixel = vgg.load_net(options.network)
    content_image = imread(options.content)

    # Jacob: moved this here since the same image features will be used for each style image
    content_features = {}
    g = tf.Graph()
    shape = (1, ) + content_image.shape
    with g.as_default(), g.device('/cpu:0'), tf.Session() as sess:
        image = tf.placeholder('float', shape=shape)
        net = vgg.net_preloaded(vgg_weights, image, options.pooling)
        content_pre = np.array([vgg.preprocess(content_image, vgg_mean_pixel)])
        for layer in CONTENT_LAYERS:
            content_features[layer] = net[layer].eval(
                feed_dict={image: content_pre})

    print("READY")
    sys.stdout.flush(
    )  # Make sure Java can sense this output before Python blocks waiting for input
    count = 0
    #for style in style_images: # loop through separate style inputs individually
    for line in sys.stdin:
        # Assumes a single line of input will be a json for one image
        style = jsonimread(line)

        width = options.width
        if width is not None:
            new_shape = (int(
                math.floor(
                    float(content_image.shape[0]) / content_image.shape[1] *
                    width)), width)
            content_image = scipy.misc.imresize(content_image, new_shape)
        target_shape = content_image.shape
        # This batch of code was in a loop for each style input before
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style = scipy.misc.imresize(
            style, style_scale * target_shape[1] / style.shape[1])

        # Removed code for blanding between multiple styles
        style_blend_weights = [1.0]

        initial = options.initial
        if initial is not None:
            initial = scipy.misc.imresize(imread(initial),
                                          content_image.shape[:2])
            # Initial guess is specified, but not noiseblend - no noise should be blended
            if options.initial_noiseblend is None:
                options.initial_noiseblend = 0.0
        else:
            # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
            if options.initial_noiseblend is None:
                options.initial_noiseblend = 1.0
            if options.initial_noiseblend < 1.0:
                initial = content_image

        if options.checkpoint_output and "%s" not in options.checkpoint_output:
            parser.error("To save intermediate images, the checkpoint output "
                         "parameter must contain `%s` (e.g. `foo%s.jpg`)")

        for iteration, image in stylize(
                network=options.network,
                initial=initial,
                initial_noiseblend=options.initial_noiseblend,
                content=content_image,
                styles=[style
                        ],  # Changed this to be a list of only one style image
                preserve_colors=options.preserve_colors,
                iterations=options.iterations,
                content_weight=options.content_weight,
                content_weight_blend=options.content_weight_blend,
                style_weight=options.style_weight,
                style_layer_weight_exp=options.style_layer_weight_exp,
                style_blend_weights=style_blend_weights,
                tv_weight=options.tv_weight,
                learning_rate=options.learning_rate,
                beta1=options.beta1,
                beta2=options.beta2,
                epsilon=options.epsilon,
                pooling=options.pooling,
                print_iterations=options.print_iterations,
                checkpoint_iterations=options.checkpoint_iterations,
                # These vgg settings are now loaded only once
                vgg_weights=vgg_weights,
                vgg_mean_pixel=vgg_mean_pixel,
                content_features=content_features):
            output_file = None
            combined_rgb = image
            if iteration is not None:
                if options.checkpoint_output:
                    output_file = options.checkpoint_output % iteration
            else:
                # Change final output files to simply be numbered
                output_file = "%d.JPG" % count
                count = count + 1
            if output_file:
                # No longer save image to file
                #imsave(output_file, combined_rgb)
                # Output json String
                print(json.dumps(combined_rgb.tolist()))
                sys.stdout.flush(
                )  # Make sure Java can sense this output before Python blocks waiting for input
    print("DONE")
Пример #31
0
def main():

    # https://stackoverflow.com/a/42121886
    key = 'TF_CPP_MIN_LOG_LEVEL'
    if key not in os.environ:
        os.environ[key] = '2'

    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to "
                     "download it?)" % options.network)

    if [options.checkpoint_iterations,
            options.checkpoint_output].count(None) == 1:
        parser.error("use either both of checkpoint_output and "
                     "checkpoint_iterations or neither")

    if options.checkpoint_output is not None:
        if re.match(r'^.*(\{.*\}|%.*).*$', options.checkpoint_output) is None:
            parser.error("To save intermediate images, the checkpoint_output "
                         "parameter must contain placeholders (e.g. "
                         "`foo_{}.jpg` or `foo_%d.jpg`")

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        # im = Image.fromarray(style_images[i])
        im = Image.fromarray((style_images[i] * 255).astype(np.uint8))
        size = tuple((np.array(im.size) * style_scale * target_shape[1] /
                      style_images[i].shape[1]).astype(int))
        new_image = np.array(im.resize(size, Image.BICUBIC))
        style_images[i] = new_image
        # style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
        # target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random
        # generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    # try saving a dummy image to the output path to make sure that it's writable
    if os.path.isfile(options.output) and not options.overwrite:
        raise IOError("%s already exists, will not replace it without "
                      "the '--overwrite' flag" % options.output)
    try:
        imsave(options.output, np.zeros((500, 500, 3)))
    except:
        raise IOError('%s is not writable or does not have a valid file '
                      'extension for an image file' % options.output)

    loss_arrs = None
    for iteration, image, loss_vals in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations,
    ):
        if (image is not None) and (options.checkpoint_output is not None):
            imsave(fmt_imsave(options.checkpoint_output, iteration), image)
        if (loss_vals is not None) \
                and (options.progress_plot or options.progress_write):
            if loss_arrs is None:
                itr = []
                loss_arrs = OrderedDict((key, []) for key in loss_vals.keys())
            for key, val in loss_vals.items():
                loss_arrs[key].append(val)
            itr.append(iteration)

    imsave(options.output, image)

    if options.progress_write:
        fn = "{}/progress.txt".format(os.path.dirname(options.output))
        tmp = np.empty((len(itr), len(loss_arrs) + 1), dtype=float)
        tmp[:, 0] = np.array(itr)
        for ii, val in enumerate(loss_arrs.values()):
            tmp[:, ii + 1] = np.array(val)
        np.savetxt(fn, tmp, header=' '.join(['itr'] + list(loss_arrs.keys())))

    if options.progress_plot:
        import matplotlib
        matplotlib.use('Agg')
        from matplotlib import pyplot as plt
        fig, ax = plt.subplots()
        for key, val in loss_arrs.items():
            ax.semilogy(itr, val, label=key)
        ax.legend()
        ax.set_xlabel("iterations")
        ax.set_ylabel("loss")
        fig.savefig("{}/progress.png".format(os.path.dirname(options.output)))
Пример #32
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):  #Test Existence du réseau
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = imread(options.content)
    style_images = [imread(style)
                    for style in options.styles]  #Si plusieurs images de style
    print("content_image.shape ", content_image.shape)
    i = 0
    for style in options.styles:
        print("style_image n.", i + 1, " ", style_images[i].shape)
        i = i + 1

    width = options.width
    print("width", width)
    if width is not None:  #Modifie la taille de l'image de base
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        print("newshape", new_shape)
        content_image = scipy.misc.imresize(
            content_image, new_shape
        )  #Remplace les 2 1ers éléments de content_image.shape par ceux de new_shape
    target_shape = content_image.shape
    print("targetshape", target_shape)

    for i in range(len(style_images)
                   ):  #pour chaque image de style, change format de l'array
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        print("style_scale", style_scale)
        print("style_images[i].shape[1]", style_images[i].shape[1])
        print("coeff",
              style_scale * target_shape[1] / style_images[i].shape[1])
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1]
        )  #Multiplie les 2 1ers éléments de style_images[i].shape par coeff
        print("style_images[", i, "].shape", style_images[i].shape)

    style_blend_weights = options.style_blend_weights  #liste
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [
            1.0 / len(style_images) for _ in style_images
        ]  #liste à n élts (n=nb d'images de style) = part de chaque image de style dans l'image finale
        print("(None) style_blend_weights", style_blend_weights)
    else:
        print("style_blend_weights", style_blend_weights)
        total_blend_weight = sum(style_blend_weights)
        print("total_blend_weight", total_blend_weight)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]  #=>sum(style_blend_weights)=1
        print("style_blend_weights", style_blend_weights)

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        print("initial.shape", initial.shape)
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image
    print("options.initial_noiseblend", options.initial_noiseblend)
    print("\n")

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
            network=options.network,
            initial=initial,
            initial_noiseblend=options.initial_noiseblend,
            content=content_image,
            styles=style_images,
            matte=options.matte,
            preserve_colors=options.preserve_colors,
            iterations=options.iterations,
            content_weight=options.content_weight,
            content_weight_blend=options.content_weight_blend,
            style_weight=options.style_weight,
            style_layer_weight_exp=options.style_layer_weight_exp,
            style_blend_weights=style_blend_weights,
            tv_weight=options.tv_weight,
            matte_weight=options.matte_weight,
            learning_rate=options.learning_rate,
            beta1=options.beta1,
            beta2=options.beta2,
            epsilon=options.epsilon,
            pooling=options.pooling,
            output=options.output,
            dest_txt=options.dest_txt,
            dest_fig=options.dest_fig,  #ajout personnel
            print_iterations=options.print_iterations,
            checkpoint_iterations=options.checkpoint_iterations,
    ):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:  #enregistre des images intermédiaires
                output_file = options.checkpoint_output % iteration  #nom des images intermédiaires

        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
Пример #33
0
def main():
    parser = build_parser()
    options = parser.parse_args()

    if not os.path.isfile(options.network):
        parser.error("Network %s does not exist. (Did you forget to download it?)" % options.network)

    content_image = imread(options.content)
    style_images = [imread(style) for style in options.styles]

    width = options.width
    if width is not None:
        new_shape = (int(math.floor(float(content_image.shape[0]) /
                content_image.shape[1] * width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape
    for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])

    style_blend_weights = options.style_blend_weights
    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0/len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [weight/total_blend_weight
                               for weight in style_blend_weights]

    initial = options.initial
    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])
        # Initial guess is specified, but not noiseblend - no noise should be blended
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 0.0
    else:
        # Neither inital, nor noiseblend is provided, falling back to random generated initial guess
        if options.initial_noiseblend is None:
            options.initial_noiseblend = 1.0
        if options.initial_noiseblend < 1.0:
            initial = content_image

    if options.checkpoint_output and "%s" not in options.checkpoint_output:
        parser.error("To save intermediate images, the checkpoint output "
                     "parameter must contain `%s` (e.g. `foo%s.jpg`)")

    for iteration, image in stylize(
        network=options.network,
        initial=initial,
        initial_noiseblend=options.initial_noiseblend,
        content=content_image,
        styles=style_images,
        preserve_colors=options.preserve_colors,
        iterations=options.iterations,
        content_weight=options.content_weight,
        content_weight_blend=options.content_weight_blend,
        style_weight=options.style_weight,
        style_layer_weight_exp=options.style_layer_weight_exp,
        style_blend_weights=style_blend_weights,
        tv_weight=options.tv_weight,
        learning_rate=options.learning_rate,
        beta1=options.beta1,
        beta2=options.beta2,
        epsilon=options.epsilon,
        pooling=options.pooling,
        print_iterations=options.print_iterations,
        checkpoint_iterations=options.checkpoint_iterations
    ):
        output_file = None
        combined_rgb = image
        if iteration is not None:
            if options.checkpoint_output:
                output_file = options.checkpoint_output % iteration
        else:
            output_file = options.output
        if output_file:
            imsave(output_file, combined_rgb)
Пример #34
0
def main():

    a = time.time()
    # 默认参数
    content_weight = 5e0
    style_weight = 1e2
    tv_weight = 1e2
    learning_rate = 1e1
    style_scale = 1.0
    iterations = 1000
    VGG_PATH = 'imagenet-vgg-verydeep-19.mat'

    # 必选参数
    content_image_path = "content.jpg"
    four_layer_style_path = [
        "relu1-1.jpg", "relu2-1.jpg", "relu3-1.jpg", "relu4-1.jpg",
        "relu5-1.jpg"
    ]  # 这里的输入替换成对于4个层都不同的输入,不采用混合style
    output_path = None

    # 可选

    print_iterations = None
    ckpt_output = "output{}.jpg"
    ckpt_iterations = iterations / 20
    width = None
    style_scales = None
    initial = None
    style_blend_weights = [1.0, 1.0, 1.0, 1.0, 1.0]

    assert len(four_layer_style_path) == 5
    assert len(style_blend_weights) == 5

    if not os.path.isfile(VGG_PATH):
        parser.error(
            "Network %s does not exist. (Did you forget to download it?)" %
            options.network)

    content_image = imread(content_image_path)
    style_images = [imread(style) for style in four_layer_style_path]

    if width is not None:
        new_shape = (int(
            math.floor(
                float(content_image.shape[0]) / content_image.shape[1] *
                width)), width)
        content_image = scipy.misc.imresize(content_image, new_shape)
    target_shape = content_image.shape

    for i in range(len(style_images)):
        if style_scales is not None:
            style_scale = style_scales[i]
        style_images[i] = scipy.misc.imresize(
            style_images[i],
            style_scale * target_shape[1] / style_images[i].shape[1])

    if style_blend_weights is None:
        # default is equal weights
        style_blend_weights = [1.0 / len(style_images) for _ in style_images]
    else:
        total_blend_weight = sum(style_blend_weights)
        style_blend_weights = [
            weight / total_blend_weight for weight in style_blend_weights
        ]

    if initial is not None:
        initial = scipy.misc.imresize(imread(initial), content_image.shape[:2])

    assert ckpt_output and "{}" in ckpt_output

    for iteration, image in stylize(network=VGG_PATH,
                                    initial=initial,
                                    content=content_image,
                                    styles=style_images,
                                    iterations=iterations,
                                    content_weight=content_weight,
                                    style_weight=style_weight,
                                    style_blend_weights=style_blend_weights,
                                    tv_weight=tv_weight,
                                    learning_rate=learning_rate,
                                    print_iterations=print_iterations,
                                    checkpoint_iterations=ckpt_iterations):
        output_file = None
        if iteration is not None:
            if ckpt_output:
                output_file = ckpt_output.format(iteration)
        else:
            output_file = output_path
        if output_file:
            imsave(output_file, image)
            print(time.time() - a)