Skip to content

Instantly share code, notes, and snippets.

@happiie
Created March 21, 2021 09:29
Show Gist options
  • Save happiie/7dab5c01d9e7ae14f0a489b4e2507f79 to your computer and use it in GitHub Desktop.
Save happiie/7dab5c01d9e7ae14f0a489b4e2507f79 to your computer and use it in GitHub Desktop.
Could not find XML file for image
package ai.certifai.weaponDetection;
import ai.certifai.Helper;
import ai.certifai.solution.object_detection.AvocadoBananaDetector.FruitDataSetIterator;
import org.apache.commons.io.FileUtils;
import org.datavec.api.split.FileSplit;
import org.datavec.api.split.InputSplit;
import org.datavec.image.loader.NativeImageLoader;
import org.datavec.image.recordreader.objdetect.ObjectDetectionRecordReader;
import org.datavec.image.recordreader.objdetect.impl.VocLabelProvider;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.nd4j.common.util.ArchiveUtils;
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
public class weaponIterator {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(weaponIterator.class);
private static final int seed = 123;
private static Random rng = new Random(seed);
private static String dataDir;
private static String downloadLink;
private static Path trainDir, testDir;
private static FileSplit trainData, testData;
private static final int nChannels = 3;
public static final int gridWidth = 13;
public static final int gridHeight = 13;
public static final int yolowidth = 416;
public static final int yoloheight = 416;
private static RecordReaderDataSetIterator makeIterator(InputSplit split, Path dir, int batchSize) throws Exception {
ObjectDetectionRecordReader recordReader = new ObjectDetectionRecordReader(yoloheight, yolowidth, nChannels,
gridHeight, gridWidth, new VocLabelProvider(dir.toString()));
recordReader.initialize(split);
RecordReaderDataSetIterator iter = new RecordReaderDataSetIterator(recordReader, batchSize, 1,1,true);
iter.setPreProcessor(new ImagePreProcessingScaler(0,1));
return iter;
}
public static RecordReaderDataSetIterator trainIterator(int batchSize) throws Exception {
return makeIterator(trainData, trainDir, batchSize);
}
public static RecordReaderDataSetIterator testIterator(int batchSize) throws Exception {
return makeIterator(testData, testDir, batchSize);
}
public static void setup() throws IOException {
log.info("Load data ...");
loadData();
trainDir = Paths.get(dataDir, "weapons", "train");
testDir = Paths.get(dataDir, "weapons", "test");
trainData = new FileSplit(new File(trainDir.toString()), NativeImageLoader.ALLOWED_FORMATS, rng);
testData = new FileSplit(new File(testDir.toString()), NativeImageLoader.ALLOWED_FORMATS, rng);
}
private static void loadData() throws IOException {
dataDir = Paths.get(
System.getProperty("user.home"),
Helper.getPropValues("dl4j_home.data")
).toString();
// downloadLink = Helper.getPropValues("dataset.fruit.url");
// File parentDir = new File(Paths.get(dataDir, "fruits").toString());
// if (!parentDir.exists()) {
// downloadAndUnzip();
// }
}
// private static void downloadAndUnzip() throws IOException {
// String dataPath = new File(dataDir).getAbsolutePath();
// File zipFile = new File(dataPath, "");
//
// log.info("Downloading the dataset from "+downloadLink+ "...");
// FileUtils.copyURLToFile(new URL(downloadLink), zipFile);
//
// if(!Helper.getCheckSum(zipFile.getAbsolutePath())
// .equalsIgnoreCase(Helper.getPropValues("dataset.fruits.hash"))){
// log.info("Downloaded file is incomplete");
// System.exit(0);
// }
//
// log.info("Unzipping "+zipFile.getAbsolutePath());
// ArchiveUtils.unzipFileTo(zipFile.getAbsolutePath(), dataPath);
// }
// private static void downloadAndUnzip() throws IOException {
// String dataPath = new File(dataDir).getAbsolutePath();
// File zipFile = new File(dataPath, "fruits-detection.zip");
//
// if (!zipFile.isFile()) {
// log.info("Downloading the dataset from " + downloadLink + "...");
// FileUtils.copyURLToFile(new URL(downloadLink), zipFile);
// }
// ArchiveUtils.unzipFileTo(zipFile.getAbsolutePath(), dataPath);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment