def lambda_dump_input(obj):
     current_iter = obj['current_iter']
     input_data = obj['input_data']
     if current_iter % 25 == 0 and args.print_inter_img == True:
         save_image(output_dir + '/' + filename_array[0] + suffix +
                    + '_' + str(current_iter.zfill(5)) + '.' +
                    filename_array[1],
                    input_data[0],
                    deprocess_type='vgg19')
Exemple #2
0
        lc_weight = 1.
        ls_weight = 1.
        print("lc_weight: %f, ls_weight: %f" % (lc_weight, ls_weight))

        print('Compiling VGG headless 5 for content ' + lc_name +
              ' and style ' + ls_name)
        ls = alpha * loss_style / lc_weight
        lc = loss_content / ls_weight
        loss = ls + lc
        grads = K.gradients(loss, input_layer)
        if optimizer == 'adam':
            grads = norm_l2(grads)
        iterate = K.function([input_layer], [loss, grads, lc, ls])

        print('Training the image')
        config = {'learning_rate': 5e-1}
        best_input_data, losses = train_input(input_data,
                                              iterate,
                                              optimizer,
                                              config,
                                              max_iter=args.max_iter)
        prefix = str(current_iter).zfill(4)
        suffix = "_lc_weight%f_ls_weight%f" % (lc_weight, ls_weight)
        fullOutPath = resultsDir + '/' + prefix + '_style' + ls_name + '_content' + lc_name + suffix + ".png"
        # dump_as_hdf5(resultsDir + '/' + prefix + '_style' + ls_name + '_content' + lc_name + suffix + ".hdf5", best_input_data[0])
        save_image(fullOutPath, best_input_data[0], deprocess_type='vgg19')
        plot_losses(losses, resultsDir, prefix, suffix)

        current_iter += 1
Exemple #3
0
                    should_convert=False, custom_objects=custom_objects)

    print('Predicting')
    start = time.clock()
    results = st_model.predict(X_test) # Equivalent to predict([X_test, False])
    results_overfit = st_model.predict(X_overfit) # Equivalent to predict([X_test, False])
    end = time.clock()
    duration_batch = (end-start)/(X_test.shape[0] * X_overfit.shape[0])

    print('Dumping results')
    tmp_output_dir = output_dir + '/' + absolute_model_dir.split('/')[-2] + '/' + absolute_model_dir.split('/')[-1]
    if not os.path.isdir(tmp_output_dir): 
        os.makedirs(tmp_output_dir)
    for idx, im in enumerate(results):
        prefix = str(current_iter).zfill(4)
        fullOutPath = tmp_output_dir + '/' + prefix + "_" + str(idx) + ".png"
        save_image(fullOutPath, im, deprocess_type='st')
        fullOriPath = tmp_output_dir + '/' + prefix + "_" + str(idx) + "_ori.png"
        save_image(fullOriPath, X_test[idx], deprocess_type='st')

        current_iter += 1

    for idx, im in enumerate(results_overfit):
        prefix = str(current_iter).zfill(4)
        fullOutPath = tmp_output_dir + '/' + prefix + str(idx) + "_overfit.png"
        save_image(fullOutPath, im, deprocess_type='st')
        fullOriPath = tmp_output_dir + '/' + prefix + str(idx) + "_overfit_ori.png"
        save_image(fullOriPath, X_overfit[idx], deprocess_type='st')

        current_iter += 1
            suffix = "_content%s_alpha%f_beta%f_gamma%f" % (lc_name, alpha,
                                                            beta, gamma)
            out_filename = filename_array[0] + suffix + '.' + filename_array[1]

            def lambda_dump_input(obj):
                current_iter = obj['current_iter']
                input_data = obj['input_data']
                if current_iter % 25 == 0 and args.print_inter_img == True:
                    save_image(output_dir + '/' + filename_array[0] + suffix +
                               + '_' + str(current_iter.zfill(5)) + '.' +
                               filename_array[1],
                               input_data[0],
                               deprocess_type='vgg19')

            best_input_data, losses = train_input(
                input_data,
                train_iteratee,
                optimizer, {'learning_rate': config['learning_rate']},
                max_iter=args.max_iter,
                callbacks=[lambda_dump_input])

            print('Dumping data')
            save_image(output_dir + '/' + out_filename,
                       best_input_data[0],
                       deprocess_type='vgg19')
            if args.no_dump_losses == False:
                plot_losses(losses, output_dir, filename_array[0], suffix)

            current_iter += 1