Beispiel #1
0
        private static double Learn(ImageBatch imb, LabelBatch lab, int x, Net net)
        {
            int    correctNum = 0;
            double costSum    = 0;

            for (int i = x * MINIBATCH_SIZE; i < x * MINIBATCH_SIZE + MINIBATCH_SIZE; i++)
            {
                formChanger.ChangeImage(imb[i].GetBitmap(), lab[i] + "     (" + (i + 1) + "/" + imb.Count() + ")");
                net.LoadSource(imb[i], lab[i]);               //载入样本
                var isCorrect = net.BeginReason(out var say); //正推
                var cost      = net.Evaluation();             //计算cost
                costSum += cost;
                formChanger.AddNeuronNote(net, lab[i], say, cost);
                net.Recall();//反向传播
                if (isCorrect)
                {
                    correctNum++;
                }
                PrintInForm(isCorrect, say, cost);
            }

            var averageCost = costSum / MINIBATCH_SIZE;

            formChanger.AddCost(averageCost, Convert.ToDouble(correctNum) / MINIBATCH_SIZE);
            net.Update(0.1, Net.UpdateOptimizer.SGD);
            ResultWriter.WriteResult(net);
            PrintInConsole(averageCost, correctNum);
            return(averageCost);
        }
        public HttpResponseMessage CreateBatch(ImageBatch imageBatch)
        {
            // Our response object
            ImageBatchResponse batchResult = new ImageBatchResponse();

            // Validate the api key
            ValidateApi validateApi    = new ValidateApi();
            Validation  validateApiKey = validateApi.ValidateApiKey(imageBatch.ApiKey);

            // Api key is invalid.
            if (!validateApiKey.IsValid)
            {
                return(BuildErrorResponse(validateApiKey));
            }

            // Loop through the values
            List <ImageResponse> imageList = new List <ImageResponse>();

            foreach (ImageDetails detail in imageBatch.ImageDetails)
            {
                // Validate the values first
                ValidateInput validation      = new ValidateInput();
                Validation    validationInput = validation.ValidateBarcodeValue(detail.Type, detail.Value);

                // Result is Valid
                ImageResponse imageResponse = new ImageResponse();
                if (validationInput.IsValid)
                {
                    imageResponse.ImageUrl = string.Format(
                        "http://www.codegenerate.me/Code/Barcode?type={0}&value={1}", detail.Type, detail.Value);
                    imageResponse.Result = "Success";
                    batchResult.SuccessfulCount++;
                }
                else
                {
                    imageResponse.Result = validationInput.ErrorMessage;
                }

                imageList.Add(imageResponse);
            }

            // Add the images
            batchResult.Images = imageList.ToArray();

            // Build the object to return
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(batchResult.ToJSON())
            };

            return(result);
        }
Beispiel #3
0
        private static void BeginLearning(int startPos)
        {
            var imageBatch = new ImageBatch(DataReader.ReadTrainImage());
            var labelBatch = new LabelBatch(DataReader.ReadTrainLabel());
            var net        = ResultWriter.ReadResult();

            //net.InitMomentumLists();
            for (int x = startPos / MINIBATCH_SIZE; x < (60000 / MINIBATCH_SIZE); x++)
            {
                ResultWriter.WriteLog("start:" + x * MINIBATCH_SIZE + " to " + (x * MINIBATCH_SIZE + MINIBATCH_SIZE) + "\n");
                for (; ;)
                {
                    var averageCost = Learn(imageBatch, labelBatch, x, net);//学习minibatch的一份
                    if (averageCost < 0.01)
                    {
                        ResultWriter.WriteLog("cost:" + averageCost + "\n");
                        break;
                    }
                }
            }
        }
        public HttpResponseMessage CreateBatch(ImageBatch imageBatch)
        {
            // Our response object
            ImageBatchResponse batchResult = new ImageBatchResponse();

            // Validate the api key
            ValidateApi validateApi = new ValidateApi();
            Validation validateApiKey = validateApi.ValidateApiKey(imageBatch.ApiKey);

            // Api key is invalid.
            if (!validateApiKey.IsValid)
            {
                return BuildErrorResponse(validateApiKey);
            }

            // Loop through the values
            List<ImageResponse> imageList = new List<ImageResponse>();
            foreach (ImageDetails detail in imageBatch.ImageDetails)
            {
                // Validate the values first
                ValidateInput validation = new ValidateInput();
                Validation validationInput = validation.ValidateBarcodeValue(detail.Type, detail.Value);

                // Result is Valid
                ImageResponse imageResponse = new ImageResponse();
                if (validationInput.IsValid)
                {
                    imageResponse.ImageUrl = string.Format(
                        "http://www.codegenerate.me/Code/Barcode?type={0}&value={1}", detail.Type, detail.Value);
                    imageResponse.Result = "Success";
                    batchResult.SuccessfulCount++;
                }
                else
                {
                    imageResponse.Result = validationInput.ErrorMessage;
                }

                imageList.Add(imageResponse);
            }

            // Add the images
            batchResult.Images = imageList.ToArray();

            // Build the object to return
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
                                             {Content = new StringContent(batchResult.ToJSON())};

            return result;
        }