Example #1
0
        public ResolutionModelDetails GetResolutionModel(int modelId)
        {
            try
            {
                MachineLearningModel machineLearningModel = machineLearningModelDAO.Find(modelId);

                IList <byte> upscaleFactors = GetUpscaleFactors(modelId);
                if (upscaleFactors.Any())
                {
                    ResolutionModelDetails resolutionDetails = new()
                    {
                        Id                      = machineLearningModel.Id,
                        Category                = machineLearningModel.Name,
                        Title                   = $"{machineLearningModel.Architecture} {machineLearningModel.Loss}",
                        Description             = machineLearningModel.LongDescription,
                        UpscaleFactors          = upscaleFactors,
                        RelatedResolutionModels = new List <RelatedResolutionModel>()
                    };

                    foreach (MachineLearningModel model in machineLearningModelDAO.FindByName("Super Resolution"))
                    {
                        if (model.Id != modelId)
                        {
                            resolutionDetails.RelatedResolutionModels.Add(new RelatedResolutionModel()
                            {
                                Id             = model.Id,
                                Title          = $"{model.Architecture} {model.Loss}",
                                UpscaleFactors = GetUpscaleFactors(model.Id)
                            });
                        }
                    }

                    return(resolutionDetails);
                }
                else
                {
                    throw new NotFoundException(modelId);
                }
            }
            catch (InstanceNotFoundException)
            {
                throw new NotFoundException(modelId);
            }
        }
Example #2
0
        public DatasetMetrics GenerateDatasetMetrics(int modelId, byte upscaleFactor, IList <byte[]> dataset)
        {
            try
            {
                if (dataset.Any())
                {
                    DatasetMetrics datasetMetrics = new()
                    {
                        ImageMetrics = new List <ImageMetrics>()
                    };
                    switch (modelId)
                    {
                    case NearestNeighbor:
                        datasetMetrics = new()
                        {
                            Id           = NearestNeighbor,
                            Title        = "Nearest Neighbor",
                            ImageMetrics = new List <ImageMetrics>()
                        };
                        break;

                    case Bilinear:
                        datasetMetrics = new()
                        {
                            Id           = Bilinear,
                            Title        = "Bilinear",
                            ImageMetrics = new List <ImageMetrics>()
                        };
                        break;

                    case Bicubic:
                        datasetMetrics = new()
                        {
                            Id           = Bicubic,
                            Title        = "Bicubic",
                            ImageMetrics = new List <ImageMetrics>()
                        };
                        break;

                    default:
                        MachineLearningModel machineLearningModel = machineLearningModelDAO.Find(modelId);
                        datasetMetrics = new()
                        {
                            Id           = machineLearningModel.Id,
                            Title        = $"{machineLearningModel.Architecture} {machineLearningModel.Loss}",
                            ImageMetrics = new List <ImageMetrics>()
                        };
                        break;
                    }

                    foreach (byte[] image in dataset)
                    {
                        Bitmap reference = Converter.ConvertByteArrayToBitmap(image);
                        Bitmap input     = ResizeBitmap(reference, reference.Width / upscaleFactor, reference.Height / upscaleFactor, InterpolationMode.Bicubic);
                        Bitmap compare   = Resize(modelId, upscaleFactor, input);

                        datasetMetrics.ImageMetrics.Add(
                            new ImageMetrics()
                        {
                            Image = Converter.ConvertBitmapToByteArray(compare),
                            MSE   = Math.Round(Metric.MSE(reference, compare), 4),
                            PSNR  = Math.Round(Metric.PSNR(reference, compare), 4),
                            SSIM  = Math.Round(Metric.SSIM(reference, compare), 4)
                        }
                            );
                    }
                    datasetMetrics.MSE  = Math.Round(datasetMetrics.ImageMetrics.Average(x => x.MSE), 4);
                    datasetMetrics.PSNR = Math.Round(datasetMetrics.ImageMetrics.Average(x => x.PSNR), 4);
                    datasetMetrics.SSIM = Math.Round(datasetMetrics.ImageMetrics.Average(x => x.SSIM), 4);

                    return(datasetMetrics);
                }
                else
                {
                    throw new ArgumentException("Dataset can not be empty");
                }
            }
            catch (InstanceNotFoundException)
            {
                throw new NotFoundException(modelId, upscaleFactor);
            }
            catch (OnnxRuntimeException e)
            {
                throw new InternalErrorException(e);
            }
        }