All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.simiacryptus.mindseye.texture_generation.HiDef Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Copyright (c) 2019 by Andrew Charneski.
 *
 * The author licenses this file to you under the
 * Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance
 * with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.simiacryptus.mindseye.texture_generation;

import com.simiacryptus.mindseye.ImageScript;
import com.simiacryptus.mindseye.applications.ArtistryUtil;
import com.simiacryptus.mindseye.applications.ColorTransfer;
import com.simiacryptus.mindseye.applications.ImageArtUtil;
import com.simiacryptus.mindseye.applications.TextureGeneration;
import com.simiacryptus.mindseye.lang.Tensor;
import com.simiacryptus.mindseye.lang.cudnn.Precision;
import com.simiacryptus.mindseye.models.CVPipe_VGG19;
import com.simiacryptus.mindseye.test.TestUtil;
import com.simiacryptus.notebook.NotebookOutput;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * The type Hi def.
 */
public abstract class HiDef extends ImageScript {

  public final double coeff_style_mean;
  /**
   * The Coeff style bandCovariance.
   */
  public final double coeff_style_cov;
  /**
   * The Style sources.
   */
  public final String[] styleSources;
  /**
   * The Resolution.
   */
  public final int resolution;
  /**
   * The Dream coeff.
   */
  public final double dreamCoeff;

  public HiDef(
      final double coeff_style_mean,
      final double coeff_style_cov,
      final double dreamCoeff,
      final int resolution,
      final String... styleSources
  ) {
    this.coeff_style_mean = coeff_style_mean;
    this.coeff_style_cov = coeff_style_cov;
    this.dreamCoeff = dreamCoeff;
    this.resolution = resolution;
    this.styleSources = styleSources;
  }

  public void accept(@Nonnull NotebookOutput log) {

    TextureGeneration.VGG19 textureGeneration = new TextureGeneration.VGG19();
    Precision precision = Precision.Float;
    textureGeneration.parallelLossFunctions = true;
    textureGeneration.setTiling(3);
    log.p("Style Source:");
    for (final CharSequence styleSource : styleSources) {
      log.p(log.png(ArtistryUtil.load(styleSource, resolution), "Style Image"));
    }
    final Map, TextureGeneration.StyleCoefficients> styles = TestUtil.buildMap(x -> {
      TextureGeneration.StyleCoefficients styleCoefficients = new TextureGeneration.StyleCoefficients<>(
          TextureGeneration.CenteringMode.Origin);
      for (final CVPipe_VGG19.Layer layer : getLayers()) {
        styleCoefficients.set(
            layer,
            coeff_style_mean,
            coeff_style_cov,
            dreamCoeff
        );
      }
      x.put(
          Arrays.asList(styleSources),
          styleCoefficients
      );
    });

    final AtomicReference canvas = new AtomicReference<>(ArtistryUtil.paint_Plasma(3, 1000.0, 1.1, resolution));

    canvas.set(log.subreport("Color_Space_Analog", sublog -> {
      ColorTransfer contentColorTransform = new ColorTransfer.VGG19() {
      }.setOrtho(false);
      //colorSyncContentCoeffMap.set(CVPipe_VGG19.Layer.Layer_1a, 1e-1);
      int colorSyncResolution = 600;
      Tensor resizedCanvas = Tensor.fromRGB(TestUtil.resize(canvas.get().toImage(), colorSyncResolution));
      final ColorTransfer.StyleSetup styleSetup = ImageArtUtil.getColorAnalogSetup(
          Arrays.asList(styleSources),
          precision,
          resizedCanvas,
          ImageArtUtil.getStyleImages(
              colorSyncResolution, styleSources
          ),
          CVPipe_VGG19.Layer.Layer_0
      );
      contentColorTransform.transfer(
          sublog,
          resizedCanvas,
          styleSetup,
          getTrainingMinutes(),
          contentColorTransform.measureStyle(styleSetup),
          getMaxIterations(),
          isVerbose()
      );
      return contentColorTransform.forwardTransform(canvas.get());
    }));

    TextureGeneration.StyleSetup styleSetup = new TextureGeneration.StyleSetup<>(
        precision,
        TestUtil.buildMap(y -> y.putAll(
            styles.keySet().stream().flatMap(
                x -> x.stream())
                .collect(Collectors.toMap(
                    x -> x,
                    file -> ArtistryUtil.load(
                        file,
                        resolution
                    )
                )))),
        styles
    );
    log.p("Input Parameters:");
    log.eval(() -> {
      return ArtistryUtil.toJson(styleSetup);
    });
    textureGeneration.optimize(
        log,
        textureGeneration.measureStyle(styleSetup), canvas.get(),
        getTrainingMinutes(),
        getMaxIterations(), isVerbose(), styleSetup.precision
    );
  }

  @Nonnull
  public abstract List getLayers();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy