void OutputDataProcessingCPU(ImageManipulationModels imageManipulationModels, float[] outputData, Color32[] pixelBuffer, Color32[] srcBuffer = null) { switch (imageManipulationModels) { case ImageManipulationModels.Colorization: // FIXME: stretch to original size for (int i = 0; i < pixelBuffer.Length; i++) { var lab = AiliaColorConv.Color2Lab(srcBuffer[i]); var nlab = new AiliaColorConv.LAB( lab.l, outputData[i + 0 * pixelBuffer.Length], outputData[i + 1 * pixelBuffer.Length] ); pixelBuffer[i] = AiliaColorConv.Lab2Color(nlab); } break; default: for (int i = 0; i < pixelBuffer.Length; i++) { pixelBuffer[i].r = (byte)Mathf.Clamp(outputData[i + 0 * pixelBuffer.Length] * 255, 0, 255); pixelBuffer[i].g = (byte)Mathf.Clamp(outputData[i + 1 * pixelBuffer.Length] * 255, 0, 255); pixelBuffer[i].b = (byte)Mathf.Clamp(outputData[i + 2 * pixelBuffer.Length] * 255, 0, 255); pixelBuffer[i].a = 255; } break; } }
void InputDataPocessing(ImageManipulationModels imageSegmentaionModels, Texture inputImage, float[] processedInputBuffer, bool upsideDown = false) { float weight = 1; float bias = 0; bool rgbRepeats = false; switch (imageSegmentaionModels) { case ImageManipulationModels.IlluminationCorrection: weight = 2; bias = -1; break; default: break; } if (inputCBuffer == null || inputCBuffer.count != processedInputBuffer.Length) { if (inputCBuffer != null) { inputCBuffer.Release(); } inputCBuffer = new ComputeBuffer(processedInputBuffer.Length, sizeof(float)); } int kernelIndex; if (rgbRepeats) { if (upsideDown) { kernelIndex = channelLastUpsideDownKernel; } else { kernelIndex = channelLastKernel; } } else { if (upsideDown) { kernelIndex = channelFirstUpsideDownKernel; } else { kernelIndex = channelFirstKernel; } } inputDataProcessingShader.SetFloat(computeShaderWeightId, weight); inputDataProcessingShader.SetFloat(computeShaderBiasId, bias); inputDataProcessingShader.SetInt(computeShaderWidthId, inputImage.width); inputDataProcessingShader.SetInt(computeShaderHeightId, inputImage.height); inputDataProcessingShader.SetTexture(kernelIndex, computeShaderTextureId, inputImage); inputDataProcessingShader.SetBuffer(kernelIndex, computeShaderResultId, inputCBuffer); inputDataProcessingShader.Dispatch(kernelIndex, inputImage.width / 32 + 1, inputImage.height / 32 + 1, 1); inputCBuffer.GetData(processedInputBuffer); }
void LoadImage(ImageManipulationModels imageManipulationModels, AiliaImageSource ailiaImageSource) { switch (imageManipulationModels) { case ImageManipulationModels.SRResNet: ailiaImageSource.CreateSource("file://" + Application.dataPath + "/AXIP/AILIA-MODELS/ImageManipulation/SampleImage/lenna.png"); break; case ImageManipulationModels.Noise2Noise: ailiaImageSource.CreateSource("file://" + Application.dataPath + "/AXIP/AILIA-MODELS/ImageManipulation/SampleImage/monarch-gaussian-noisy.png"); break; case ImageManipulationModels.IlluminationCorrection: ailiaImageSource.CreateSource("file://" + Application.dataPath + "/AXIP/AILIA-MODELS/ImageManipulation/SampleImage/illumination_correction_test.png"); break; case ImageManipulationModels.Colorization: ailiaImageSource.CreateSource("file://" + Application.dataPath + "/AXIP/AILIA-MODELS/ImageManipulation/SampleImage/ansel_adams1.png"); break; } }
void InputDataPocessingCPU(ImageManipulationModels imageManipulationModels, Color32[] inputImage, float[] processedInputBuffer) { float weight = 1f / 255f; float bias = 0; bool rgbRepeats = false; switch (imageManipulationModels) { case ImageManipulationModels.IlluminationCorrection: weight = 1f / 127.5f; bias = -1; break; case ImageManipulationModels.Colorization: if (rgbRepeats) { for (int i = 0; i < inputImage.Length; i++) { var r = (inputImage[i].r) * weight + bias; var g = (inputImage[i].g) * weight + bias; var b = (inputImage[i].b) * weight + bias; var lab = AiliaColorConv.Color2Lab(new Color(r, g, b)); processedInputBuffer[i * 3 + 0] = (float)lab.L; processedInputBuffer[i * 3 + 1] = (float)lab.A; processedInputBuffer[i * 3 + 2] = (float)lab.B; } } else { for (int i = 0; i < inputImage.Length; i++) { var r = (inputImage[i].r) * weight + bias; var g = (inputImage[i].g) * weight + bias; var b = (inputImage[i].b) * weight + bias; var lab = AiliaColorConv.Color2Lab(new Color(r, g, b)); processedInputBuffer[i + inputImage.Length * 0] = (float)lab.L; } } return; default: break; } // flatten input data if (rgbRepeats) { for (int i = 0; i < inputImage.Length; i++) { // rgbrgbrgb... processedInputBuffer[i * 3 + 0] = (inputImage[i].r) * weight + bias; processedInputBuffer[i * 3 + 1] = (inputImage[i].g) * weight + bias; processedInputBuffer[i * 3 + 2] = (inputImage[i].b) * weight + bias; } } else { for (int i = 0; i < inputImage.Length; i++) { processedInputBuffer[i + inputImage.Length * 0] = (inputImage[i].r) * weight + bias; processedInputBuffer[i + inputImage.Length * 1] = (inputImage[i].g) * weight + bias; processedInputBuffer[i + inputImage.Length * 2] = (inputImage[i].b) * weight + bias; } } }
void SetShape(ImageManipulationModels imageSegmentaionModels) { Ailia.AILIAShape shape = null; switch (imageSegmentaionModels) { case ImageManipulationModels.SRResNet: case ImageManipulationModels.Noise2Noise: shape = ailiaModel.GetInputShape(); InputWidth = (int)shape.x; InputHeight = (int)shape.y; InputChannel = (int)shape.z; shape = ailiaModel.GetOutputShape(); OutputWidth = (int)shape.x; OutputHeight = (int)shape.y; OutputChannel = (int)shape.z; break; case ImageManipulationModels.IlluminationCorrection: shape = new Ailia.AILIAShape(); shape.x = (uint)AiliaImageSource.Width; shape.y = (uint)AiliaImageSource.Height; shape.z = 3; shape.w = 1; shape.dim = 4; ailiaModel.SetInputShape(shape); InputWidth = AiliaImageSource.Width; InputHeight = AiliaImageSource.Height; InputChannel = 3; OutputWidth = AiliaImageSource.Width; OutputHeight = AiliaImageSource.Height; OutputChannel = 3; break; #if false case ImageManipulationModels.Colorization: shape = new Ailia.AILIAShape(); shape.x = (uint)AiliaImageSource.Width; shape.y = (uint)AiliaImageSource.Height; shape.z = 1; shape.w = 1; shape.dim = 4; ailiaModel.SetInputShape(shape); InputWidth = AiliaImageSource.Width; InputHeight = AiliaImageSource.Height; InputChannel = 1; OutputWidth = AiliaImageSource.Width; OutputHeight = AiliaImageSource.Height; OutputChannel = 2; break; #else // 256x256に縮小してみる case ImageManipulationModels.Colorization: shape = ailiaModel.GetInputShape(); shape.z = 1; shape.w = 1; shape.dim = 4; ailiaModel.SetInputShape(shape); InputWidth = (int)shape.x; InputHeight = (int)shape.y; InputChannel = 1; OutputWidth = InputWidth; // AiliaImageSource.Width; OutputHeight = InputHeight; // AiliaImageSource.Height; OutputChannel = 2; break; #endif // ~true } }
// Download models and Create ailiaModel AiliaModel CreateAiliaNet(ImageManipulationModels modelType, bool gpu_mode = true) { string asset_path = Application.temporaryCachePath; string serverFolderName = ""; string prototxtName = ""; string onnxName = ""; switch (modelType) { case ImageManipulationModels.SRResNet: serverFolderName = "srresnet"; prototxtName = "srresnet.opt.onnx.prototxt"; onnxName = "srresnet.opt.onnx"; break; case ImageManipulationModels.Noise2Noise: serverFolderName = "noise2noise"; prototxtName = "noise2noise_gaussian.onnx.prototxt"; onnxName = "noise2noise_gaussian.onnx"; break; case ImageManipulationModels.IlluminationCorrection: serverFolderName = "illnet"; prototxtName = "illnet.onnx.prototxt"; onnxName = "illnet.onnx"; break; case ImageManipulationModels.Colorization: serverFolderName = "colorization"; prototxtName = "colorizer.onnx.prototxt"; onnxName = "colorizer.onnx"; break; } ailiaModel = new AiliaModel(); if (gpu_mode) { // call before OpenFile ailiaModel.Environment(Ailia.AILIA_ENVIRONMENT_TYPE_GPU); } AiliaDownload ailia_download = new AiliaDownload(); ailia_download.DownloaderProgressPanel = UICanvas.transform.Find("DownloaderProgressPanel").gameObject; var urlList = new List <ModelDownloadURL>(); urlList.Add(new ModelDownloadURL() { folder_path = serverFolderName, file_name = prototxtName }); urlList.Add(new ModelDownloadURL() { folder_path = serverFolderName, file_name = onnxName }); StartCoroutine(ailia_download.DownloadWithProgressFromURL(urlList, () => { modelPrepared = ailiaModel.OpenFile(asset_path + "/" + prototxtName, asset_path + "/" + onnxName); })); return(ailiaModel); }