Ejemplo n.º 1
0
 public void StyleModelIsNotNull()
 {
     using (var graph = new EffectGraph())
     {
         Assert.IsNotNull(graph.Style);
     }
 }
Ejemplo n.º 2
0
 public void EffectParamsIsNotNull()
 {
     using (var graph = new EffectGraph())
     {
         Assert.IsNotNull(graph.Params);
     }
 }
Ejemplo n.º 3
0
 public void TransformerModelIsNotNull()
 {
     using (var graph = new EffectGraph())
     {
         Assert.IsNotNull(graph.Transformer);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Apply configuration and set inputs and outputs
        /// </summary>
        /// <param name="newToken">User configuration from dialog</param>
        /// <param name="dstArgs">Destination data (outputs)</param>
        /// <param name="srcArgs">Source data (inputs)</param>
        protected override void OnSetRenderInfo(StyleTransferEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs)
        {
            Contract.Requires(newToken != null && srcArgs != null);
            base.OnSetRenderInfo(newToken, dstArgs, srcArgs);

            // There's a bug in PDN 4.2.8 (possibly earlier versions as well) where
            // OnSetRenderInfo(...) is called *twice* by the caller...
            // This effect is *very* expensive in terms of both memory and CPU-time,
            // so we do what we can to avoid processing it.

            // props.IsValid - denotes whether all required settings are configured
            // this.IsRenderingEnabled - false while the configuration dialog is displayed
            // graph.CanRun - false, if the model cannot be evaluated (e.g. session not started)

            var properties = newToken.Properties;

            if (properties.IsValid && IsRenderingEnabled)
            {
                graph.Params.StyleRatio = properties.StyleRatio;
                if (properties.IsPreset)
                {
                    var tensor = Presets.Instance[properties.PresetName].Style;
                    graph.Params.SetStyleVector(tensor);
                }
                else
                {
                    using (var style = new Bitmap(properties.StyleImage))
                    {
                        var scaling = GetLimitedScalingFactor(style.Size, properties.StyleScale);
                        graph.Params.Style = style.ToTensor(scaling);
                    }
                }

                var tiles = EffectGraph.GetRecommendedTileSize(SrcArgs.Size, MARGIN);
                if (tiles == SrcArgs.Size)
                {
                    graph.Params.Content = srcArgs.Surface.ToTensor();
                    tileSize             = 0;
                }
                else
                {
                    tileSize = tiles.Width;
                }

                modelProvider.StyleType         = properties.StyleModel;
                modelProvider.TransformType     = properties.TransformerModel;
                modelProvider.ComputationDevice = properties.ComputeDevice;

                graph.Params.PostProcess = TransferMethods
                                           .All
                                           .Where(desc => desc.Name == properties.ColorTransfer)
                                           .Select(desc => desc.Interface).FirstOrDefault();

                if (mustCallOnRender)
                {
                    OnRender(Array.Empty <Rectangle>(), 0, 0);
                }
            }
        }
Ejemplo n.º 5
0
 public void EffectParamsIsNotNull()
 {
     using (var provider = new ModelProvider())
     {
         var graph = new EffectGraph(provider);
         Assert.IsNotNull(graph.Params);
     }
 }
Ejemplo n.º 6
0
        // Get scaling factor limited to available memory
        private static float GetUpperScalingLimit(SizeF scaledSize, float scalingFactor)
        {
            // estimate required memory and use 60% as threshold value
            var required  = EffectGraph.GetEstimatedRequiredMemory(scaledSize);
            var threshold = (EffectGraph.AvailableMemory * 100) / 60;

            if (required > threshold)
            {
                // limit scaling factor to fit available memory
                var maxPixels      = EffectGraph.GetMaximumPixelCount(threshold);
                var originalPixels = ((double)scaledSize.Width * scaledSize.Height) / scalingFactor;
                return((float)(maxPixels / originalPixels));
            }

            return(scalingFactor);
        }
        // Once the scaled style image exceed a certain size, warn the user.
        // The models are not GPU-accellerated and might be very slow to
        // calculate if the image size is rather large plus a large amount
        // of RAM is required
        private void WarnAboutImageSize()
        {
            var s       = trackBarSize.Value / 100.0;
            var pixels  = (long?)(pictureBoxStyle.Image?.Width * pictureBoxStyle.Image?.Height * s * s);
            var maximum = EffectGraph.GetMaximumPixelCount((long)(EffectGraph.AvailableMemory * 0.8));

            if (pixels > maximum)
            {
                labelStyleDimensions.ForeColor = labelSizeWarning.ForeColor;
                labelStyleDimensions.Font      = labelSizeWarning.Font;
                labelSizeWarning.Visible       = true;
                panelWarning.Visible           = true;
            }
            else
            {
                labelStyleDimensions.ForeColor = labelStyle.ForeColor;
                labelStyleDimensions.Font      = labelStyle.Font;
                labelSizeWarning.Visible       = false;
                panelWarning.Visible           = false;
            }
        }
Ejemplo n.º 8
0
        public void CanRunCompatibleModel()
        {
            using (var provider = new ModelProvider(load: true))
            {
                var graph    = new EffectGraph(provider);
                var assembly = typeof(EffectGraphTest).Assembly;

                using (var bitmap = new Bitmap(assembly.GetManifestResourceStream(GetType(), "resources.style.png")))
                {
                    graph.Params.Style = bitmap.ToTensor();
                }

                using (var bitmap = new Bitmap(assembly.GetManifestResourceStream(GetType(), "resources.content.png")))
                {
                    graph.Params.Content = bitmap.ToTensor();
                }

                var result = graph.Run();
                Assert.IsNotNull(result);
                Assert.AreEqual(graph.Params.Content.Length, result.Length);
                CollectionAssert.AreEqual(graph.Params.Content.Dimensions.ToArray(),
                                          result.Dimensions.ToArray());
            }
        }
Ejemplo n.º 9
0
        public void CanRunCompatibleModel()
        {
            using (var graph = new EffectGraph())
            {
                var assembly = typeof(EffectGraphTest).Assembly;
                using (var stream = assembly.GetManifestResourceStream(GetType(), "resources.style_mobilenet.onnx"))
                {
                    var model = new byte[stream.Length];
                    stream.Read(model, 0, model.Length);
                    graph.Style.Load(model);
                }

                using (var stream = assembly.GetManifestResourceStream(GetType(), "resources.transformer_separable.onnx"))
                {
                    var model = new byte[stream.Length];
                    stream.Read(model, 0, model.Length);
                    graph.Transformer.Load(model);
                }

                using (var bitmap = new Bitmap(assembly.GetManifestResourceStream(GetType(), "resources.style.png")))
                {
                    graph.Params.Style = bitmap.ToTensor();
                }

                using (var bitmap = new Bitmap(assembly.GetManifestResourceStream(GetType(), "resources.content.png")))
                {
                    graph.Params.Content = bitmap.ToTensor();
                }

                var result = graph.Run();
                Assert.IsNotNull(result);
                Assert.AreEqual(graph.Params.Content.Length, result.Length);
                CollectionAssert.AreEqual(graph.Params.Content.Dimensions.ToArray(),
                                          result.Dimensions.ToArray());
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Return the estimated amount of memory required to process the effect
 /// </summary>
 /// <param name="size">Size of the input image</param>
 /// <param name="scalingFactor">Scaling factor</param>
 /// <returns>Estimated number of bytes required to process the effect</returns>
 public static long GetEstimatedRequiredMemory(Size size, float scalingFactor)
 {
     return(EffectGraph.GetEstimatedRequiredMemory(size.ScaleBy(scalingFactor)));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Initialise the effect
 /// </summary>
 public StyleTransferEffect()
     : base(StringResources.EffectName, Properties.Resources.Icon, SubmenuNames.Artistic, options)
 {
     IsRenderingEnabled = true;
     graph = new EffectGraph(modelProvider);
 }