Some sample images from the Galaxy Zoo
processImage <- function(angle, files)
{
outPath <- file.path(processedImgPath, angle)
parallel::parSapply(cl, files, function(f, angle, path) {
try(imager::load.image(f) %>%
cropGalaxy(0.26) %>%
rotateGalaxy(angle) %>%
resizeGalaxy(c(50, 50)) %>%
imager::save.image(file.path(path, basename(f))))
},
path=outPath, angle=angle, USE.NAMES=FALSE)
}
# use a parallel backend cluster to speed up preprocessing
cl <- parallel::makeCluster(3)
imgFiles <- dir(imgPath, pattern="\\.jpg$")
lapply(c(0, 45, 90), processImage, files=imgFiles)
rxNeuralNet
function from MicrosoftML. This supports an extensive range of network topologies, training algorithms and optimisation hyperparameters. It also supports GPU acceleration via the NVidia CUDA toolkit, which is almost a mandatory requirement when training large networks.
library(MicrosoftML)
# simplified training code
model <- rxNeuralNet(class2 ~ pixels,
data=trainData,
type="multiClass", mlTransformVars="path",
mlTransforms=list(
"BitmapLoaderTransform{col=Image:path}",
"BitmapScalerTransform{col=bitmap:Image width=50 height=50}",
"PixelExtractorTransform{col=pixels:bitmap}"
),
netDefinition=netDefinition,
optimizer=sgd(),
acceleration="gpu",
miniBatchSize=256,
numIterations=100,
normalize="no", initWtsDiameter=0.1, postTransformCache="Disk"
)
rxNeuralNet
uses a language called Net# to define the network topology. While you can write Net# code directly (
it’s fully documented on MSDN
), for convenience, and to avoid having to learn a new language, I’ve used a helper package called
RMLtools
to generate the code.
rxPredict
method to compute predicted values, similar to how most other modelling functions in R have a
predict
method. However, this typically only works within R; to do it from outside requires some additional work. Luckily, Microsoft R Server includes some key features to ease this process of model deployment.
# simplified prediction code
spBasePredictGalaxyClass <- function(model, imgData)
{
# for each image, preprocess the file and store it
path <- sapply(seq_len(nrow(imgData)), function(i) {
inFile <- tempfile(fileext=".jpg")
outFile <- tempfile(fileext=".jpg")
writeBin(imgData$img[[i]], inFile)
imager::load.image(inFile) %>%
cropGalaxy(0.26) %>%
resizeGalaxy(c(50, 50)) %>%
imager::save.image(outFile)
outFile
})
imgData <- data.frame(galclass=" ", path=path)
model <- rxReadObject(as.raw(model))
rxPredict(model, imgData)
}
library(sqlrutils)
spPredictGalaxyClass <- StoredProcedure(spBasePredictGalaxyClass,
spName="predictGalaxyClass",
InputData("imgData",
defaultQuery="select * from galaxyImgData"),
InputParameter("model", "raw",
defaultQuery="select value from galaxyModels where id='model'"),
connectionString=deployDbConnStr
)
registerStoredProcedure(spPredictGalaxyClass, deployDbConnStr)
apiPredictGalaxyClass <- function(id, img)
{
imgData <- data.frame(specobjid=id, img=img, stringsAsFactors=FALSE)
sqlImgData <- RxSqlServerData("galaxyImgData")
rxDataStep(imgData, sqlImgData, overwrite=TRUE)
sqlrutils::executeStoredProcedure(spPredictGalaxyClass)$data
}
library(mrsdeploy)
apiGalaxyModel <- publishService("apiPredictGalaxyClass", apiPredictGalaxyClass, spPredictGalaxyClass,
inputs=list(id="character", img="character"),
outputs=list(pred="data.frame"))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.