public static void Serialize(ProxySerialize serialize, LossMulticlassLogPerPixel net)
        {
            if (serialize == null)
            {
                throw new ArgumentNullException(nameof(serialize));
            }
            if (net == null)
            {
                throw new ArgumentNullException(nameof(net));
            }

            net.ThrowIfDisposed();

            var error = NativeMethods.LossMulticlassLogPerPixel_serialize_proxy(net.NetworkType,
                                                                                serialize.NativePtr,
                                                                                net.NativePtr,
                                                                                out var errorMessage);

            switch (error)
            {
            case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                throw new NotSupportNetworkTypeException(net.NetworkType);

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }
        }
        public static void Serialize(this IDictionary <string, LossMulticlassLogPerPixel> maps,
                                     ProxySerialize serialize,
                                     int networkType = 0)
        {
            if (serialize == null)
            {
                throw new ArgumentNullException(nameof(serialize));
            }

            serialize.ThrowIfDisposed();

            StdString[] keys = null;

            try
            {
                keys = new StdString[maps.Count];
                var values = new IntPtr[maps.Count];

                var index = 0;
                foreach (var kvp in maps)
                {
                    keys[index]   = new StdString(kvp.Key);
                    values[index] = kvp.Value.NativePtr;
                    index++;
                }

                var keysArray = keys.Select(s => s.NativePtr).ToArray();
                var error     = NativeMethods.LossMulticlassLogPerPixel_serialize_proxy_map(networkType,
                                                                                            serialize.NativePtr,
                                                                                            keysArray,
                                                                                            values,
                                                                                            maps.Count,
                                                                                            out var errorMessage);
                Dnn.Cuda.ThrowCudaException(error);
                switch (error)
                {
                case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                    throw new NotSupportNetworkTypeException(networkType);

                case NativeMethods.ErrorType.GeneralSerialization:
                    throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
                }
            }
            finally
            {
                keys?.DisposeElement();
            }
        }
Ejemplo n.º 3
0
        private static int Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("To run this program you need a copy of the PASCAL VOC2012 dataset.");
                Console.WriteLine();
                Console.WriteLine("You call this program like this: ");
                Console.WriteLine("./dnn_instance_segmentation_train_ex /path/to/VOC2012 [det-minibatch-size] [seg-minibatch-size] [class-1] [class-2] [class-3] ...");
                return(1);
            }

            try
            {
                Console.WriteLine("\nSCANNING PASCAL VOC2012 DATASET");
                Console.WriteLine();

                var listing = PascalVOC2012.GetPascalVoc2012TrainListing(args[0]).ToArray();
                Console.WriteLine($"images in entire dataset: {listing.Length}");
                if (listing.Length == 0)
                {
                    Console.WriteLine("Didn't find the VOC2012 dataset. ");
                    return(1);
                }

                // mini-batches smaller than the default can be used with GPUs having less memory
                var argc             = args.Length;
                var detMiniBatchSize = argc >= 2 ? int.Parse(args[1]) : 35;
                var segMiniBatchSize = argc >= 3 ? int.Parse(args[2]) : 100;
                Console.WriteLine($"det mini-batch size: {detMiniBatchSize}");
                Console.WriteLine($"seg mini-batch size: {segMiniBatchSize}");

                var desiredClassLabels = new List <string>();
                for (var arg = 3; arg < argc; ++arg)
                {
                    desiredClassLabels.Add(args[arg]);
                }

                if (!desiredClassLabels.Any())
                {
                    desiredClassLabels.Add("bicycle");
                    desiredClassLabels.Add("car");
                    desiredClassLabels.Add("cat");
                }

                Console.Write("desired classlabels:");
                foreach (var desiredClassLabel in desiredClassLabels)
                {
                    Console.Write($" {desiredClassLabel}");
                }
                Console.WriteLine();

                // extract the MMOD rects
                Console.Write("\nExtracting all truth instances...");
                var truthInstances = LoadAllTruthInstances(listing);
                Console.WriteLine(" Done!");
                Console.WriteLine();


                if (listing.Length != truthInstances.Count)
                {
                    throw new ApplicationException();
                }

                var originalTruthImages = new List <TruthImage>();
                for (int i = 0, end = listing.Length; i < end; ++i)
                {
                    originalTruthImages.Add(new TruthImage
                    {
                        Info           = listing[i],
                        TruthInstances = truthInstances[i]
                    });
                }


                var truthImagesFilteredByClass = FilterBasedOnClassLabel(originalTruthImages, desiredClassLabels);

                Console.WriteLine($"images in dataset filtered by class: {truthImagesFilteredByClass.Count}");

                IgnoreSomeTruthBoxes(truthImagesFilteredByClass);
                var truthImages = FilterImagesWithNoTruth(truthImagesFilteredByClass);

                Console.WriteLine($"images in dataset after ignoring some truth boxes: {truthImages.Count}");

                // First train an object detector network (loss_mmod).
                Console.WriteLine("\nTraining detector network:");
                var detNet = TrainDetectionNetwork(truthImages, (uint)detMiniBatchSize);

                // Then train mask predictors (segmentation).
                var segNetsByClass = new Dictionary <string, LossMulticlassLogPerPixel>();

                // This flag controls if a separate mask predictor is trained for each class.
                // Note that it would also be possible to train a separate mask predictor for
                // class groups, each containing somehow similar classes -- for example, one
                // mask predictor for cars and buses, another for cats and dogs, and so on.
                const bool separateSegNetForEachClass = true;


                if (separateSegNetForEachClass)
                {
                    foreach (var classLabel in desiredClassLabels)
                    {
                        // Consider only the truth images belonging to this class.
                        var classImages = FilterBasedOnClassLabel(truthImages, new[] { classLabel });

                        Console.WriteLine($"\nTraining segmentation network for class {classLabel}:");
                        segNetsByClass[classLabel] = TrainSegmentationNetwork(classImages, (uint)segMiniBatchSize, classLabel);
                    }
                }
                else
                {
                    Console.WriteLine("Training a single segmentation network:");
                    segNetsByClass[""] = TrainSegmentationNetwork(truthImages, (uint)segMiniBatchSize, "");
                }

                Console.WriteLine("Saving networks");
                using (var proxy = new ProxySerialize(InstanceSegmentationNetFilename))
                {
                    LossMmod.Serialize(proxy, detNet);
                    segNetsByClass.Serialize(proxy, 4);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(0);
        }