Esempio n. 1
0
def camera_transfer(checkpoint_dir, screen_shot_dir="output/camera_screen/"):
    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    fps = cap.get(30)
    i = 0
    date = []
    date2 = datetime.datetime.now()
    while (True):
        ret, frame = cap.read()
        i = i + 1
        if i / fps == int(i / fps):
            cv2.imwrite("Assets/image/tmp.png", frame)
            evaluate.ffwd_to_img("Assets/image/tmp.png",
                                 "Assets/image/tmp2.png", checkpoint_dir)
            frame = cv2.imread("Assets/image/tmp2.png")
            cv2.imshow('frame', frame)
            date1 = datetime.datetime.now()
            date.append(date1 - date2)
            date2 = date1
        if cv2.waitKey(1) & 0xFF == ord('s'):
            s = screen_shot_dir + "Screen_shot" + str(
                datetime.datetime.now()).split(" ")[0] + "-" + (str(
                    datetime.datetime.now()).split(" ")[1].split(
                        ".")[0]).replace(":", "-") + '.png'
            cv2.imshow("screen shot", frame)
            cv2.imwrite(s, frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            print("你按了Q键")
            break
    cap.release()
    cv2.destroyAllWindows()
Esempio n. 2
0
def upload_file():
    @after_this_request
    def cleanup(response):
        for f in glob.glob("{}/*".format(INPUT_PATH)):
            os.remove(f)
        for f in glob.glob("{}/*".format(OUTPUT_PATH)):
            os.remove(f)
        return response

    if not request.form.get("style"):
        print('No selected style')
        return BadRequest('No selected style')
    print(request.form.get("style"))
    if 'file' not in request.files:
        print("No file part")
        return BadRequest('No file part')
    file = request.files['file']
    if file.filename == '':
        print('No selected file')
        return BadRequest('No selected file')
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        style = request.form.get("style")
        print("Everything is OK")
        print(file)

        input_filepath = os.path.join(INPUT_PATH, filename)
        output_filepath = os.path.join(OUTPUT_PATH, filename)
        file.save(input_filepath)

        checkpoint = '{}/{}.ckpt'.format(MODELS_PATH, style)
        print(checkpoint)
        ffwd_to_img(input_filepath, output_filepath, checkpoint, '/cpu:0')
        return send_file(output_filepath, mimetype='image/jpg')
Esempio n. 3
0
def main():
    print('ml5.js Style Transfer Training!')
    print('Note: This traning will take a couple of hours.')
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow":options.slow,
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
        "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
        "learning_rate":options.learning_rate,
    }
    
    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,
        style_target,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path
    ]

    print('Training is starting!...')
    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test,preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
    print('Converting model to ml5js')
    dump_checkpoints(kwargs['save_path'], options.model_dir)
    print('Done! Checkpoint saved. Visit https://ml5js.org/docs/StyleTransfer for more information')
Esempio n. 4
0
def main():
    print("Enter main")
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)
    print("Get image target")
    style_target = get_img(options.style)
    if not options.slow:
        with log_time_usage("get images targets"):
            content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "debug": options.debug,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate,
        "tensorboard_dir": options.tensorboard_dir
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    print("Start training")
    with log_time_usage("Training completed in"):
        for preds, losses, i, epoch, time_info in optimize(*args, **kwargs):
            style_loss, content_loss, tv_loss, loss = losses

            print(
                'Epoch %d, Iteration: %d, Loss: %s, AVG batch time: %.2f, total_time: %.2f, ETA (in h): %.2f'
                % (epoch, i, loss, *time_info))
            to_print = (style_loss, content_loss, tv_loss)
            print('style: %s, content:%s, tv: %s' % to_print)
            if options.test:
                assert options.test_dir != False
                preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
                if not options.slow:
                    ckpt_dir = os.path.dirname(options.checkpoint_dir)
                    evaluate.ffwd_to_img(options.test, preds_path,
                                         options.checkpoint_dir)
                else:
                    # TODO: img is not defined
                    # save_img(preds_path, img)
                    pass
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
def main():
    check_version()
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow":options.slow,
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
        "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
        "learning_rate":options.learning_rate,
        "device":options.device,
        "total_iterations":options.total_iterations,
        "base_model_path":options.base_model_path,
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,
        style_target,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        sys.stdout.flush()
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test,preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint-dir %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 6
0
def main(style,
         test=False,
         test_dir='test',
         train_path=TRAIN_PATH,
         slow=False,
         epochs=NUM_EPOCHS,
         checkpoint_iterations=CHECKPOINT_ITERATIONS,
         batch_size=BATCH_SIZE,
         checkpoint_dir=CHECKPOINT_DIR,
         learning_rate=LEARNING_RATE,
         content_weight=CONTENT_WEIGHT,
         style_weight=STYLE_WEIGHT,
         tv_weight=TV_WEIGHT,
         vgg_path=VGG_PATH):
    #parser = build_parser()
    #options = parser.parse_args()
    #check_opts(options)

    style_target = get_img(style)
    if not slow:
        content_targets = _get_files(train_path)
    elif test:
        content_targets = [test]

    kwargs = {
        "slow": slow,
        "epochs": epochs,
        "print_iterations": checkpoint_iterations,
        "batch_size": batch_size,
        "save_path": checkpoint_dir,
        "learning_rate": learning_rate
    }

    if slow:
        if epochs < 10:
            kwargs['epochs'] = 1000
        if learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, content_weight, style_weight, tv_weight,
        vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if test:
            assert test_dir != False
            preds_path = '%s/%s_%s.png' % (test_dir, epoch, i)
            if not slow:
                ckpt_dir = os.path.dirname(checkpoint_dir)
                evaluate.ffwd_to_img(test, preds_path, checkpoint_dir)
            else:
                save_img(preds_path, img)
Esempio n. 7
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate,
        "device_and_number": options.device_and_number
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    import time
    from datetime import datetime
    start_time = time.time()
    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses
        delta_time, start_time = time.time() - start_time, time.time()
        print(
            'Current Time = {}; Time Elapsed = {}; Epoch = {}; Iteration = {}; Loss = {}'
            .format(datetime.now().strftime("%Y %B %d, %H:%M:%S"), delta_time,
                    epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('Loss values: style = %s; content = %s; tv = %s' % to_print)
        sys.stdout.flush()
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:  # if uses GPU, uses RAM that it doesn't have, so it's slow here
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 8
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    if not os.path.exists(options.test_dir):
        os.mkdir(options.test_dir)
    if not os.path.exists(options.checkpoint_dir):
        os.mkdir(options.checkpoint_dir)

    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate,
        "gpu_fraction": options.gpu_fraction
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
def main():
    parser = build_parser()
    options = parser.parse_args()  # 获取命令行参数
    check_opts(options)  # 参数检查

    style_target = get_img(options.style)  # 三通道风格图,大小不固定
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,  # 训练多少轮,所有的训练数据集都训练过一次称为一个epoch,即一轮
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.
        batch_size,  # 批处理。一组多少张,itertions=epoch/batch即迭代一轮的迭代次数, epochs*(epoch/batch)即为总迭代次数
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate
    }  # !!!

    if options.slow:  # !!!
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]  # 待使用参数

    for preds, losses, i, epoch in optimize(*args, **kwargs):  # 迭代!!!
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(
                    preds_path, preds.reshape((512, 512, 3))
                )  ####131 错误save_img(preds_path,img),I found a fix for this. Replace img with preds.reshape((512,512,3))
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 10
0
 def do_start(self):
     style_checkpoint_path = style_path.split(".")[0] + ".ckpt"
     out_path = storage_location_path + content_path.split('\\')[-1]
     if content_is_video:
         evaluate.ffwd_video(content_path, out_path, style_checkpoint_path)
         if show_result.show_video(out_path):
             return True
     else:
         evaluate.ffwd_to_img(content_path, out_path, style_checkpoint_path)
         if show_result.show_image(out_path):
             return True
     # time.sleep(10)
     '''
Esempio n. 11
0
def get_image(file_path):
	url = "https://api.telegram.org/file/bot{}/{}".format(bot_token,file_path)
	#cmd = "curl -o {} {}".format(file_path, url)
	print "getting image:", url
	wget.download(url, file_path)
	#os.system(cmd)
	in_path = file_path
	out_path = 'static/' + in_path
	checkpoint_dir = 'checkpoints/udnie.ckpt'
	print in_path, out_path
	ffwd_to_img(in_path, out_path, random.choice(ckpt_files))
	
	return out_path	
Esempio n. 12
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    #    with tf.Graph().as_default(), tf.device('/cpu:0'), tf.Session() as sess:
    #        init_op = tf.global_variables_initializer()
    #        saver = tf.train.Saver()
    #        saver.restore(sess, options.checkpoint_dir)

    print('options.test: ' + options.test)
    print('options.checkpoint_der: ' + options.checkpoint_dir)

    for index_image in range(0, 20):
        options.test = '../image_style_dataset/' + 'structure' + str(
            index_image + 1).zfill(4) + '.jpg'
        print(options.test)
        preds_path = '%s/' % (options.test_dir) + SAVE_NAME + '%s_%s.png' % (
            999, index_image)
        if not options.slow:
            ckpt_dir = os.path.dirname(options.checkpoint_dir)
            evaluate.ffwd_to_img(options.test, preds_path,
                                 options.checkpoint_dir)
        else:
            save_img(preds_path, img)
def process_file(filename, chk):
    abs_in_filename = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), app.config['UPLOAD_FOLDER'], filename)
    abs_out_filename = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), app.config['PROCESSED_FOLDER'], filename)
    abs_chk_filename = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), app.config['CHECKPOINT_FOLDER'], chk)
    if os.path.exists(abs_out_filename):
        os.remove(abs_out_filename)
    if allowed_file(filename, VIDEO_EXTENSIONS):
        process_video(abs_in_filename, abs_chk_filename, abs_out_filename)
    if allowed_file(filename, IMAGE_EXTENSIONS):
        ffwd_to_img(abs_in_filename, abs_out_filename, abs_chk_filename)
    return redirect('/static/'+filename)
Esempio n. 14
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    # TODO:获取风格图像 style_target 以及内容图像数组 content_targets
    style_target = ___________________
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        #        "slow":options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate,
        "type": options.type,
        "save": options.save
    }
    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses
        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    print("Training complete.")
def stransfer_from_file(fpath):
    assert os.path.isfile(fpath)
    assert fpath.endswith(".jpg") or fpath.endswith(".png")

    fname = os.path.basename(fpath)
    out_path = os.path.join(OUT_PATH, fname)

    # picking random style
    style_id = random.randint(0, NUM_STYLES - 1)
    cpkt_file = os.path.join(CPKT_DIR, CPKT_LIST[style_id])
    style_imgpath = STYLE_PATHS[style_id]

    # running style transfer
    evaluate.ffwd_to_img(fpath, out_path, cpkt_file, device='/cpu:0')
    # return the path to the output image and the path to the style image
    return out_path, style_imgpath
Esempio n. 16
0
def processMessage(data):
    print data
    in_path = data['result']['file_path'].encode(encoding='UTF-8')
    print "in_path:", in_path, type(in_path)
    url = "https://api.telegram.org/file/bot{}/{}".format(bot_token, in_path)
    print "getting image:", url
    wget.download(url, in_path)

    out_path = 'static/' + in_path
    #checkpoint_dir = 'checkpoints/udnie.ckpt'
    checkpoint_dir = random.choice(ckpt_files)

    ffwd_to_img(in_path, out_path, checkpoint_dir)

    print "done image"
    sendPhoto(data['chat_id'], "https://dl2.rohbot.cc/" + out_path)
Esempio n. 17
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)


    content_targets = _get_files(options.train_path)
    clear_content_targets = _get_files(options.train_path_)
    # random.shuffle(content_targets) # 乱序

    kwargs = {
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
        "save_path":os.path.join(options.checkpoint_dir,''),
        "learning_rate":options.learning_rate
    }


    args = [
        content_targets,
        clear_content_targets,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
            ckpt_dir = os.path.dirname(options.checkpoint_dir)
            evaluate.ffwd_to_img(options.test,preds_path,
                                     options.checkpoint_dir)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 18
0
File: app.py Progetto: skai2/Stylize
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = 'in.jpg'
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            filename_out = 'out.jpg'
            ffwd_to_img(filename, filename_out, request.form['checkpoint'])
            return redirect(url_for('uploaded_file',
                                    filename=filename_out))
    return render_template('./index.html')
def style_transfer(path):
    """
    Take the input image and style transfer it
    """
    # check if the post request has the file part
    if 'file' not in request.files:
        return BadRequest("File not present in request")
    file = request.files['file']
    if file.filename == '':
        return BadRequest("File name is not present in request")
    if not allowed_file(file.filename):
        return BadRequest("Invalid file type")
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        input_filepath = os.path.join('./images/', filename)
        output_filepath = os.path.join('/output/', filename)
        file.save(input_filepath)

        # Get checkpoint filename from la_muse
        checkpoint = request.form.get("checkpoint") or "la_muse.ckpt"
        ffwd_to_img(input_filepath, output_filepath, '/input/' + checkpoint, '/gpu:0')
        return send_file(output_filepath, mimetype='image/jpg')
Esempio n. 20
0
def style_transfer(path):
    """
    Take the input image and style transfer it
    """
    # check if the post request has the file part
    if 'file' not in request.files:
        return BadRequest("File not present in request")
    file = request.files['file']
    if file.filename == '':
        return BadRequest("File name is not present in request")
    if not allowed_file(file.filename):
        return BadRequest("Invalid file type")
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        input_filepath = os.path.join('./images/', filename)
        output_filepath = os.path.join('/output/', filename)
        file.save(input_filepath)

        # Get checkpoint filename from la_muse
        checkpoint = request.form.get("checkpoint") or "undine.ckpt"
        ffwd_to_img(input_filepath, output_filepath, '/input/' + checkpoint, '/gpu:0')
        return send_file(output_filepath, mimetype='image/jpg')
Esempio n. 21
0
def main():
    parser = build_parser()  # ArgumentParser()
    options = parser.parse_args()  # call the fucntion then return options
    check_opts(options)

    style_target = get_img(options.style)  # a 3-D numpy array
    if not options.slow:
        # COCO train2014 dataset, roughly 20000 images
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    if options.IN:
        use_IN = True
    else:
        use_IN = False

    print(f"Are we using Instance Normalization? {use_IN}.")

    kwargs = {
        "slow":
        options.slow,
        "epochs":
        options.epochs,
        "print_iterations":
        options.checkpoint_iterations,
        "batch_size":
        options.batch_size,
        "save_path":
        os.path.join(options.checkpoint_dir, 'fast_style_transfer.ckpt'),
        "learning_rate":
        options.learning_rate
    }

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path, use_IN
    ]

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    for generated_image, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, total_loss = losses
        print(f"Epoch: {epoch}, Iteration: {i} ......")
        print(
            f"Total_loss: {total_loss}, Style_loss: {style_loss}, Content_loss: {content_loss}."
        )

        if options.test:
            assert options.test_dir != False
            preds_path = f"{options.test_dir}/{epoch}_{i}.png"
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)

    cmd_text = f"python evaluate.py --checkpoint {options.checkpoint_dir} \
                                    --in-path [in_picture] --out-path [out_picture]"

    print(f"Training complete. For evaluation, type: {cmd_text}")
Esempio n. 22
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    # style_target = get_img(options.style)
    # style_targets = []
    # for path in os.listdir(options.style_dir):
    #     style_targets.append(get_img(os.path.join(options.style_dir, path)))
    style_targets = np.array([get_img(os.path.join(options.style_dir, fname), (256,256,3)) for fname in os.listdir(options.style_dir)])

    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow":options.slow,
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
        "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
        "learning_rate":options.learning_rate,
        "save_checkpoint":options.save_checkpoint,
        "restore_checkpoint_path":os.path.join(options.restore_checkpoint,'fns.ckpt') if options.restore_checkpoint else None
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,
        # style_target,
        style_targets,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)

        if options.test:
            print('Save test image.')
            curr_lambda_style = np.random.randint(1, 100) * 1.0
            # print('curr_lambda_style: {}'.format(curr_lambda_style))
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir,
                                     control_lambda_style=curr_lambda_style,
                                     style_id=np.random.randint(len(style_targets)))
            else:
                save_img(preds_path, img)

    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 23
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.fcrs_weight,
        options.fcon_weight, options.fdir_weight, options.vgg_path
    ]
    cnt = 0
    for preds, losses, i, epoch, lr, gs in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, fcrs_loss, fcon_loss, fdir_loss, loss = losses
        print('cnt: %d' % cnt)
        cnt += 1
        print('Epoch %d, Iteration: %d, Loss: %s, Lr: %s, Gs: %s' %
              (epoch, i, loss, lr, gs))
        to_print = (style_loss, content_loss, tv_loss, fcrs_loss, fcon_loss,
                    fdir_loss)
        print(
            'style: %s, content:%s, tv: %s, fcrs_loss: %s, fcon_loss: %s, fdir_loss: %s'
            % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                #save_img(preds_path, img)
                save_img(preds_path, preds)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    save_img(
        '/home/marshallee/Documents/fast-style-transfer/examples/output/Step-wave-'
        + str(options.fcrs_weight) + '-' + str(options.fcon_weight) + '-' +
        str(options.fdir_weight) + '.jpg', np.squeeze(np.array(preds)))
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
    print(
        str(options.fcrs_weight) + '-' + str(options.fcon_weight) + '-' +
        str(options.fdir_weight))
Esempio n. 24
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    #define a path with weight
    weight_path = '%s/%s_%s_%s_%s' % (options.checkpoint_dir,options.test.split('/')[-1],options.style.split('/')[-1],options.content_weight,options.style_weight)
    #os.mkdir(weight_path)

    kwargs = {
        "slow":options.slow,
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
 #       "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
        "save_path":os.path.join(weight_path,'fns.ckpt'),
        "learning_rate":options.learning_rate
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,
        style_target,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            #calvin - make subfolder for output files - start
            output_path = '%s/%s_%s' % (options.test_dir,options.test.split('/')[-1],options.style.split('/')[-1])
            if not os.path.isdir(output_path):
                os.mkdir(output_path)
            #calin - end

            preds_path = '%s/%s_%s_%s_%s_%s.png' % (output_path,options.content_weight,options.style_weight,epoch,i,style_loss)
            if not options.slow:
                ckpt_dir = os.path.dirname(weight_path)
                evaluate.ffwd_to_img(options.test,preds_path,
                                     weight_path)
              #  ckpt_dir = os.path.dirname(options.checkpoint_dir)   # change the checkpoint folder as dynamic
              #  evaluate.ffwd_to_img(options.test,preds_path,
              #                       options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 25
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]
    style_files = []
    if os.path.isfile(options.style):
        style_files.extend(options.style)
    else:
        style_files = _get_files(options.style)
    for style_file in style_files:
        print("-------------Started to train2014 model for style '%s'" %
              os.path.basename(style_file))
        style_target = get_img(style_file)
        checkpoint_dir = "checkpoint_" + os.path.splitext(
            os.path.basename(style_file))[0]
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)
        test_dir = "test_" + os.path.splitext(os.path.basename(style_file))[0]
        if options.test:
            if not os.path.exists(test_dir):
                os.makedirs(test_dir)
        kwargs = {
            "slow": options.slow,
            "epochs": options.epochs,
            "print_iterations": options.checkpoint_iterations,
            "batch_size": options.batch_size,
            "save_path": checkpoint_dir,
            "learning_rate": options.learning_rate
        }

        if options.slow:
            if options.epochs < 10:
                kwargs['epochs'] = 1000
            if options.learning_rate < 1:
                kwargs['learning_rate'] = 1e1

        args = [
            content_targets, style_target, options.content_weight,
            options.style_weight, options.tv_weight, options.vgg_path
        ]

        for preds, losses, i, epoch in optimize(*args, **kwargs):
            style_loss, content_loss, tv_loss, loss = losses

            print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
            to_print = (style_loss, content_loss, tv_loss)
            print('style: %s, content:%s, tv: %s' % to_print)
            if options.test:
                # assert options.test_dir != False
                preds_path = '%s/%s_%s.png' % (test_dir, epoch, i)
                if not options.slow:
                    ckpt_dir = os.path.dirname(checkpoint_dir)
                    evaluate.ffwd_to_img(options.test, preds_path,
                                         checkpoint_dir)
                else:
                    save_img(preds_path, preds)
        ckpt_dir = checkpoint_dir
        cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
        print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 26
0
def main():
    #実際に実行される関数
    parser = build_parser()
    #引数を解析してparserに格納する
    options = parser.parse_args()
    #引数として読み込まれたものをoptionsに格納する
    check_opts(options)
    #引数が不適切なものではないかどうかを確認する

    style_target = get_img(options.style)
    #スタイル画像をget_img関数を用いてプログラム内に読み込む
    if not options.slow:
        #slowを指定されていなければこちら側に分岐する
        content_targets = _get_files(options.train_path)
        #_get_files関数を使って訓練用の画像のパスを読み込み、content_targets変数に代入する
    elif options.test:
        #引数としてtestが指定されていた場合
        content_targets = [options.test]
        #テスト用画像をcontent_targetsに代入する

    kwargs = {
        #辞書型のオブジェクトkwargsに値を入れていく
        "slow": options.slow,
        #slowが指定されていれば"slow"として格納する
        "epochs": options.epochs,
        #エポック数を"epochs"として格納する
        "print_iterations": options.checkpoint_iterations,
        #チェックポイントに到達するまでの数を"print_iterations"として格納する
        "batch_size": options.batch_size,
        #バッチサイズを"batch_size"として格納する
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        #チェックポイントとして使うファイルのパスをcheckpoint_dirと結合し、"save_path"として格納する
        "learning_rate": options.learning_rate
        #学習率を"learninng_rate"として格納する
    }

    if options.slow:
        #slowが指定されていた場合
        if options.epochs < 10:
            #エポック数が10よりも小さければTrue
            kwargs['epochs'] = 1000
            #エポック数を1000に上書きする
        if options.learning_rate < 1:
            #学習率が1よりも小さい場合
            kwargs['learning_rate'] = 1e1
            #学習率を1x10^1に上書きする

    args = [
        # argsリストに値を代入していく
        content_targets,
        # コンテンツ画像
        style_target,
        # スタイル画像
        options.content_weight,
        # コンテンツの損失の重み
        options.style_weight,
        # スタイルの損失の重み
        options.tv_weight,
        # total variationの損失の重み
        options.vgg_path
        # vggのある場所へのパス
    ]

    # argsは初期値などを格納してある変数
    # kwargsはハイパーパラメータなどが格納してある変数
    for preds, losses, i, epoch in optimize(*args, **kwargs):
        # optimize関数から、マッピング後の入力ノードの値、損失の値、
        # 現在の繰り返し数、現在のエポックを受け取り、
        # その4つの値をそれぞれの変数に格納する
        style_loss, content_loss, tv_loss, loss = losses
        # スタイル損失、コンテンツ損失、tv損失、合計の損失をlosses変数から取得し、
        # その4つの値をそれぞれの変数に格納する

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        # 現在のエポック数、繰り返し数、損失の値をコンソールに表示する
        to_print = (style_loss, content_loss, tv_loss)
        # スタイル損失、コンテンツ損失、tv損失のタプルをto_print変数に格納する
        print('style: %s, content:%s, tv: %s' % to_print)
        # それぞれの損失の値をコンソールに表示する
        if options.test:
            # testモードの場合true
            assert options.test_dir != False
            # テスト用ディレクトリのパスが指定されていなければAssertionError
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            # 新しく生成する画像の名前を、テスト用ディレクトリの名前、
            # 現在のエポック数、繰り返し数から重複がないように生成する
            if not options.slow:
                # slowモードが指定されていない場合True
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                # チェックポイントのディレクトリを内包しているディレクトリの名前を取得する
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
                # テストとして画風変換を行う
                # それぞれ引数testはテスト用画像のディレクトリ、
                # preds_pathは変換後の画像の出力先、checkpoint_dirはチェックポイントのディレクトリ
            else:
                # slowモードが指定されている場合
                save_img(preds_path, img)
                # 画像出力先のディレクトリpreds_pathにimgを保存する
    ckpt_dir = options.checkpoint_dir
    # checkpoint_dirが格納しているパスをckpt_dirに格納しておく
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    # コンソールに出力したい文字列を格納した変数cmd_textを定義する
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 27
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)

    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    # a simpler folder name format
    #   [style image name]_[data format]_[num base channels]_[content weight]_[style weight]_[learning rate]
    style_image_str = os.path.basename(options.style)
    style_image_str = os.path.splitext(style_image_str)[0]
    content_weight_str = np.format_float_scientific(
        options.content_weight).replace('.', '').replace('+', '')
    style_weight_str = np.format_float_scientific(
        options.style_weight).replace('.', '').replace('+', '')

    folder_name = style_image_str + "-" + options.data_format + "_nbc" + str(
        options.num_base_channels) + "_bs" + str(
            options.batch_size
        ) + "_" + content_weight_str + "_" + style_weight_str + "_" + str(
            options.learning_rate)

    # create checkpoint and test image folders
    ckpt_dir_path = os.path.join(options.checkpoint_dir, folder_name)
    test_dir_path = os.path.join(options.test_dir, folder_name)

    print("ckpt_dir_path:{}".format(ckpt_dir_path))
    print("test_dir_path:{}".format(test_dir_path))

    if not os.path.isdir(ckpt_dir_path):
        os.mkdir(ckpt_dir_path)
    if not os.path.isdir(test_dir_path):
        os.mkdir(test_dir_path)

    options.checkpoint_dir = ckpt_dir_path
    options.test_dir = test_dir_path

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        #"save_path":os.path.join(options.checkpoint_dir,folder_name,'fns.ckpt'),
        "learning_rate": options.learning_rate,

        # more cli params
        "data_format": options.data_format,
        "num_base_channels": options.num_base_channels
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,
        style_target,
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.vgg_path,

        # more cli params
        #options.data_format,
        #options.num_base_channels
    ]

    #print ("kwargs:{}".format(kwargs))
    #print ("args:{}".format(*args))

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(
                    options.test,
                    preds_path,
                    options.checkpoint_dir,
                    device=
                    '/gpu:0',  #mcky, force to use GPU, or would have 'Conv2DCustomBackpropInputOp only supports NHWC.' error.
                    data_format=options.data_format,
                    num_base_channels=options.num_base_channels)
            else:
                save_img(preds_path, img)

    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 28
0
def main():
    start_time = time.time()
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = options.style_image
    if 1:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]
    print(len(content_targets))

    kwargs = {
        "epochs":options.epochs,
        "print_iterations":options.checkpoint_iterations,
        "batch_size":options.batch_size,
        "save_path":os.path.join(options.checkpoint_dir,'fns.ckpt'),
        "learning_rate":options.learning_rate,
        "num_examples": options.num_examples,
        "no_gpu":options.no_gpu,
        "logs":options.logs,
        "affine":options.affine,
        "luma":options.luma,
        "contrast":options.contrast
    }

    args = [
        content_targets,
        style_target,
        options.content_weight,
        options.style_weight,
        options.contrast_weight,
        options.tv_weight,
        options.affine_weight,
        options.luma_weight,
        options.vgg_path
    ]



    # save options as json file
    with open(os.path.join(options.test_dir, "options.json"), "w") as f:
        f.write(json.dumps(vars(options), sort_keys=True, indent=4))


    for preds, losses, i, epoch in optimize(*args, **kwargs):

        if options.luma:
            style_loss, content_loss, tv_loss, contrast_loss, affine_loss, luma_loss, loss = losses
            to_print = (style_loss, content_loss, tv_loss, contrast_loss, affine_loss, luma_loss)
        elif options.affine:
            style_loss, content_loss, tv_loss, contrast_loss, affine_loss, loss = losses
            to_print = (style_loss, content_loss, tv_loss, contrast_loss, affine_loss)
        elif options.contrast:
            style_loss, content_loss, tv_loss, contrast_loss, loss = losses
            to_print = (style_loss, content_loss, tv_loss, contrast_loss) 
        else:
            style_loss, content_loss, tv_loss, loss = losses
            to_print = (style_loss, content_loss, tv_loss) 

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        # print('style: %s, content:%s, tv: %s, contrast: %s, gradient: %s' % to_print)

        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir,epoch,i)
            
                # copy ckpt
            src = os.path.join(options.checkpoint_dir, "fns.ckpt.data-00000-of-00001")
            dst = os.path.join(options.checkpoint_dir, "fns.ckpt")
            copyfile(src, dst)

            ckpt_dir = os.path.dirname(options.checkpoint_dir)
            evaluate.ffwd_to_img(options.test,preds_path,
                                 options.checkpoint_dir)

    ckpt_dir = options.checkpoint_dir
    end_time  = time.time()
    elapsed_time = end_time - start_time
    print("Training complete in %s seconds." % (elapsed_time))

    # final cleanup
    os.remove(os.path.join(options.checkpoint_dir, "fns.ckpt.meta"))
Esempio n. 29
0
def main():
    check_version()
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    model_name = os.path.splitext(os.path.basename(options.style))[0]

    kwargs = {
        "slow":
        options.slow,
        "epochs":
        options.epochs,
        "print_iterations":
        options.checkpoint_iterations,
        "batch_size":
        options.batch_size,
        "save_path":
        os.path.join(options.checkpoint_dir, '.'.join([model_name, 'ckpt'])),
        "learning_rate":
        options.learning_rate,
        "device":
        options.device,
        "total_iterations":
        options.total_iterations,
        "base_model_path":
        options.base_model_path,
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        sys.stdout.flush()
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint-dir %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
def _run_model(options):
    check_opts(options)
    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate,
        "device_and_number": options.device
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    start_time = time.time()
    shutdown_time = start_time + options.max_runtime_in_minutes * 60
    if options.log_file:
        log_file_ = open(options.log_file, 'w')
        sys.stdout = log_file_
        sys.stderr = log_file_
    for preds, losses, i, epoch, checkpoint_number in optimize(
            *args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses
        delta_time, start_time = time.time() - start_time, time.time()
        print(
            'Current Time = {}; Time Elapsed = {}; Epoch = {}; Iteration = {}; Loss = {}'
            .format(datetime.now().strftime("%Y %B %d, %H:%M:%S"), delta_time,
                    epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('Loss values: style = %s; content = %s; tv = %s' % to_print)
        sys.stdout.flush()
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:  # if uses GPU, uses RAM that it doesn't have, so it's slow here
                # ckpt_dir = os.path.dirname(options.checkpoint_dir)
                actual_dir = os.path.join(
                    options.checkpoint_dir,
                    'checkpoint_{}'.format(checkpoint_number))
                evaluate.ffwd_to_img(options.test, preds_path, actual_dir)
            else:
                save_img(preds_path, img)
        if time.time() > shutdown_time:  # automatic shut down
            print(
                '\n\nRan for maximum runtime in minutes. Now shutting down time.'
            )
            break
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
    if options.log_file:
        log_file_.close()
Esempio n. 31
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    # Load style and content images
    style_target = get_img(options.style)
    style_seg = get_img(options.style_seg)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets,  # Batch image paths
        style_target,  # Reference style *image*
        style_seg,  # Reference style segmentation map *image*
        options.content_weight,
        options.style_weight,
        options.tv_weight,
        options.photo_weight,
        options.vgg_path,
        options.deeplab_path,
        options.
        resized_dir,  # Batch image resized folder (intermediate from deeplab)
        options.
        seg_dir,  # Batch image segmentation folder (intermediate from deeplab)
        options.matting_dir
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, photo_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss, photo_loss)
        print('style: %s, content:%s, tv: %s, photo: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Esempio n. 32
0
def test_ckpt():
    evaluate.ffwd_to_img('data/chicago.jpg', 'out/test_result.jpg',
                         'ckp_temp/')
    print('EVALUATE FINSHED.')