Covid-19 Detection With Images Analysis And Machine Learning – Elemental

/* we have just two outputs positive and negative according to our directories */ int outputNum = 2;int numEpochs = 1;

/*This class downloadData() downloads the datastores the data in java's tmpdir 15MB download compressedIt will take 158MB of space when uncompressedThe data can be downloaded manually here

// Define the File PathsFile trainData = new File(DATA_PATH + "/covid-19/training");File testData = new File(DATA_PATH + "/covid-19/testing");

// Define the FileSplit(PATH, ALLOWED FORMATS,random)FileSplit train = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);FileSplit test = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);

// Extract the parent path as the image labelParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();

ImageRecordReader recordReader = new ImageRecordReader(height, width, channels, labelMaker);

// Initialize the record reader// add a listener, to extract the namerecordReader.initialize(train);//recordReader.setListeners(new LogRecordListener());

// DataSet IteratorDataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);

// Scale pixel values to 0-1DataNormalization scaler = new ImagePreProcessingScaler(0, 1);scaler.fit(dataIter);dataIter.setPreProcessor(scaler);

// Build Our Neural Networklog.info("BUILD MODEL");MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(rngseed).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(new Nesterovs(0.006, 0.9)).l2(1e-4).list().layer(0, new DenseLayer.Builder().nIn(height * width).nOut(100).activation(Activation.RELU).weightInit(WeightInit.XAVIER).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nIn(100).nOut(outputNum).activation(Activation.SOFTMAX).weightInit(WeightInit.XAVIER).build()).setInputType(InputType.convolutional(height, width, channels)).build();

MultiLayerNetwork model = new MultiLayerNetwork(conf);

// The Score iteration Listener will log// output to show how well the network is trainingmodel.setListeners(new ScoreIterationListener(10));

log.info("TRAIN MODEL");for (int i = 0; i < numEpochs; i++) {model.fit(dataIter);}

log.info("EVALUATE MODEL");recordReader.reset();

// The model trained on the training dataset split// now that it has trained we evaluate against the// test data of images the network has not seen

recordReader.initialize(test);DataSetIterator testIter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);scaler.fit(testIter);testIter.setPreProcessor(scaler);

/*log the order of the labels for later useIn previous versions the label order was consistent, but randomIn current verions label order is lexicographicpreserving the RecordReader Labels order is nolonger needed left in for demonstrationpurposes*/log.info(recordReader.getLabels().toString());

// Create Eval object with 10 possible classesEvaluation eval = new Evaluation(outputNum);

// Evaluate the networkwhile (testIter.hasNext()) {DataSet next = testIter.next();INDArray output = model.output(next.getFeatures());// Compare the Feature Matrix from the model// with the labels from the RecordReadereval.eval(next.getLabels(), output);}// show the evaluationlog.info(eval.stats());}

More here:
Covid-19 Detection With Images Analysis And Machine Learning - Elemental

Related Posts

Comments are closed.