djl icon indicating copy to clipboard operation
djl copied to clipboard

DOnt getting Any BoundingBox AND result same classes always?

Open nikkisingh111333 opened this issue 4 years ago • 4 comments

hey i m following pikachu detection for creating my own dataset ...Dogs Breed Detection + claasification heres my Data prepearation code: **//OBJECT DETECTION DATASET

public class GestureDataset extends ObjectDetectionDataset {

private static final String VERSION = "1.0";
private static final String ARTIFACT_ID = "gesture";

private Usage usage;
private List<Path> imagePaths;
private PairList<Long, Rectangle> labels;

private Resource resource;
private boolean prepared;

protected GestureDataset(Builder builder) {
    super(builder);
    usage = builder.usage;
    imagePaths = new ArrayList<Path>();
    labels = new PairList<Long, Rectangle>();
 //   MRL mrl = MRL.dataset(CV.ANY, builder.groupId, builder.artifactId);
 //   resource = new Resource(builder.repository, mrl, VERSION);
}

/**
 * Creates a new builder to build a {@link PikachuDetection}.
 *
 * @return a new builder
 */
public static Builder builder() {
    return new Builder();
}

/** {@inheritDoc} */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }

    
   
    String images="//content//drive//MyDrive//Colab Notebooks//DOGS";
    Path root = Paths.get("//content//drive//MyDrive//Colab Notebooks//DOGS//");
    File imgs=new File(images);
    
    Path usagePath;
    switch (usage) {
        case TRAIN:
            usagePath = Paths.get("//DOGS//");
            break;
        case TEST:
            usagePath = Paths.get("//Validated//");
            break;
        case VALIDATION:
        default:
            throw new UnsupportedOperationException("Validation data not available.");
    }
    usagePath = root.resolve(usagePath);
    String Annopath="//content//drive//MyDrive//Colab Notebooks//DOGS_ANNO";
    File indexFiles = new File(Annopath);
    List <File>folds=Arrays.asList(indexFiles.listFiles());
		  Collections.sort(folds);
    long classs=1;
    
    for(File dogs:folds){
       try{
        System.out.println(dogs.getName());
        File dogname=new File(Annopath+"//"+dogs.getName());

        for(File images_anno:dogname.listFiles()){
              
        
               BufferedReader reader = Files.newBufferedReader(Paths.get(Annopath+"//"+dogs.getName()+"//"+images_anno.getName()) );
                String lines=reader.lines().map(line -> line + "\n").collect(Collectors.joining());
                
                 Type mapType = new TypeToken<Map<String, List<Float>>>() {}.getType();
                     JSONObject json = XML.toJSONObject(lines);
                     System.out.println(json);
                  System.out.println("Wiooooooo:"+images_anno.getName());
              GsonBuilder builder = new GsonBuilder(); 
              builder.setPrettyPrinting(); 
  
                Gson gson = builder.create(); 
                   Anno anno=gson.fromJson(json.toString(),Anno.class);
                 String imgName=anno.getAnnotation().getObject().getName();
                 System.out.println(imgName+"------"+root.resolve(dogs.getName()+"//"+images_anno.getName()));
     
            imagePaths.add(root.resolve(dogs.getName()+"//"+images_anno.getName()+".jpg"));

            long objectClass = classs;
            System.out.println("looki");
            System.out.println("Londiiii"+anno.getAnnotation().getObject().getBndbox().getXmin());
            Rectangle objectLocation =
                    new Rectangle(
                            new Point(anno.getAnnotation().getObject().getBndbox().getXmin(), anno.getAnnotation().getObject().getBndbox().getYmin()), anno.getAnnotation().getObject().getBndbox().getXmax(),anno.getAnnotation().getObject().getBndbox().getYmax());
            labels.add(objectClass, objectLocation);
            System.out.println("AllSet");

        
    
          }
    }
    catch(Exception ex){
        System.out.println("Error:"+ex.getMessage());
    }   
       
        
       classs+=1;
       System.out.println("Classes:"+classs+"-Images-"+imagePaths.size()+"--Labels-"+labels.size());
    }
    
    
    System.out.println(labels);
    prepared = true;
}

@Override
public PairList<Long, Rectangle> getObjects(long index) {
    return labels;
}

/** {@inheritDoc} */
@Override
protected long availableSize() {
    return imagePaths.size();
}

@Override
protected Image getImage(long index) throws IOException {
    int idx = Math.toIntExact(index);
    return ImageFactory.getInstance().fromFile(imagePaths.get(idx));
}

@Override
public Optional<Integer> getImageWidth() {
    return Optional.empty();
}

@Override
public Optional<Integer> getImageHeight() {
    return Optional.empty();
}

/** A builder for a {@link PikachuDetection}. */
public static final class Builder extends ImageDataset.BaseBuilder<Builder> {

    Repository repository;
    String groupId;
    String artifactId;
    Usage usage;

    /** Constructs a new builder. */
    Builder() {
        repository = BasicDatasets.REPOSITORY;
        groupId = BasicDatasets.GROUP_ID;
        artifactId = ARTIFACT_ID;
        usage = Usage.TRAIN;
    }

    /** {@inheritDoc} */
    @Override
    public Builder self() {
        return this;
    }

    /**
     * Sets the optional usage.
     *
     * @param usage the usage
     * @return this builder
     */
    public Builder optUsage(Usage usage) {
        this.usage = usage;
        return self();
    }

    /**
     * Sets the optional repository.
     *
     * @param repository the repository
     * @return this builder
     */
    public Builder optRepository(Repository repository) {
        this.repository = repository;
        return self();
    }

    /**
     * Sets optional groupId.
     *
     * @param groupId the groupId}
     * @return this builder
     */
    public Builder optGroupId(String groupId) {
        this.groupId = groupId;
        return this;
    }

    /**
     * Sets the optional artifactId.
     *
     * @param artifactId the artifactId
     * @return this builder
     */
    public Builder optArtifactId(String artifactId) {
        if (artifactId.contains(":")) {
            String[] tokens = artifactId.split(":");
            groupId = tokens[0];
            this.artifactId = tokens[1];
        } else {
            this.artifactId = artifactId;
        }
        return this;
    }

    /**
     * Builds the {@link PikachuDetection}.
     *
     * @return the {@link PikachuDetection}
     */


    public GestureDataset build() {
        if (pipeline == null) {
            pipeline = new Pipeline(new ToTensor());
        }
        return new GestureDataset(this);
    }
}

}**

All the model building is same as picachu detection:

`var usage=Dataset.Usage.TRAIN; var validate=Dataset.Usage.TEST; Pipeline pipeline = new Pipeline(new Resize(150,150)); pipeline.add(new ToTensor()); ObjectDetectionDataset dogsData = GestureDataset.builder() .optUsage(usage) .optLimit(1000)

                    .optPipeline(pipeline)
                    .setSampling(128,false)
                    .build();
    dogsData.prepare(new ProgressBar());
    dogsData.size();
   `

at last my training code:

int[] numFilters = {16, 32, 64}; SequentialBlock baseBlock = new SequentialBlock(); for (int numFilter : numFilters) { baseBlock.add(SingleShotDetection.getDownSamplingBlock(numFilter)); } List<List<Float>> sizes = new ArrayList<>(); List<List<Float>> ratios = new ArrayList<>(); for (int i = 0; i < 5; i++) { ratios.add(Arrays.asList(1f, 2f, 0.5f)); } sizes.add(Arrays.asList(0.2f, 0.272f)); sizes.add(Arrays.asList(0.37f, 0.447f)); sizes.add(Arrays.asList(0.54f, 0.619f)); sizes.add(Arrays.asList(0.71f, 0.79f)); sizes.add(Arrays.asList(0.88f, 0.961f)); //TRAIN BLOCK var ssd= SingleShotDetection.builder() .setNumClasses(12) .setNumFeatures(3) .optGlobalPool(true) .setRatios(ratios) .setSizes(sizes) .setBaseNetwork(baseBlock) .build(); SequentialBlock ssdPredict = new SequentialBlock(); ssdPredict.add(ssd); ssdPredict.add(new LambdaBlock( output -> { NDArray anchors = output.get(0); System.out.println(anchors); NDArray classPredictions = output.get(1).softmax(-1).transpose(0, 2, 1); NDArray boundingBoxPredictions = output.get(2); MultiBoxDetection multiBoxDetection = MultiBoxDetection.builder().build(); NDList detections = multiBoxDetection.detection( new NDList( classPredictions, boundingBoxPredictions, anchors)); return detections.singletonOrThrow().split(new long[] {1, 2}, 2); })); //ADDING OUTPUT BLOCK
TRAINING START FROM HERE

        ` Model model = Model.newInstance("GestureDetector");
        float detectionThreshold = 0.2f;
        // load parameters back to original training block
        model.setBlock(ssd);
        // append prediction logic at end of training block with parameter loaded
        Block ssdTrain = model.getBlock();
       model.setBlock(ssdPredict);
        model.setBlock(ssdTrain);
          var epoch=50;
var train_config= new DefaultTrainingConfig(new SingleShotDetectionLoss())
            .addEvaluator(new SingleShotDetectionAccuracy("classAccuracy"))
            .addEvaluator(new BoundingBoxError("boundingBoxError"))
            .optDevices(new Device[]{Device.gpu(0)})
            .addTrainingListeners(TrainingListener.Defaults.logging());
            Trainer trainer = model.newTrainer(train_config);

SingleShotDetectionTranslator translator =SingleShotDetectionTranslator.builder() .addTransform(new Resize(150,150)) .addTransform(new ToTensor())
.optThreshold(detectionThreshold) .optSynset(Arrays.asList("Japanese","Shih-tzu","bluetick","English_fox","Irish_wolf","Saluki","Staffordshire","American_terrier","Border_terrier","Brittany_spaniel","Rottweiler","German_shephard")) .build(); trainer.setMetrics(new Metrics()); System.out.println("whaaat"); trainer.initialize(new Shape(128,3,150, 150));
for(int epochs=0;epochs<epoch;epochs++){ for(Batch b:trainer.iterateDataset(dogsData)){ System.out.println("loo"); EasyTrain.trainBatch(trainer,b); trainer.step(); } } // reset training and validation evaluators at end of epoch trainer.notifyListeners(listener -> listener.onEpoch(trainer));`

HERES my result :

Model mn=trainer.getModel(); mn.setBlock(ssdPredict); Predictor<Image, DetectedObjects> predictor = mn.newPredictor(translator); Image image = ImageFactory.getInstance().fromFile(Paths.get("//content//drive//MyDrive//Colab Notebooks//DOGS//n02088632-bluetick//n02088632_1047.jpg"));
DetectedObjects detectedObjects = predictor.predict(image); image.drawBoundingBoxes(detectedObjects); // return number of pikachu detected detectedObjects.getNumberOfObjects(); Map<String, List<Rectangle>> ret = new ConcurrentHashMap<>(); int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); Map<String,Float> prlist=new ConcurrentHashMap<>(); List<DetectedObjects.DetectedObject> detections = detectedObjects.items(); int n=1; for (DetectedObjects.DetectedObject detection : detections) { String className = detection.getClassName(); float probability = (float) detection.getProbability(); Rectangle rect = detection.getBoundingBox().getBounds(); double x = rect.getX() * imageWidth; double y = rect.getY() * imageHeight; double w = rect.getWidth() * imageWidth; double h = rect.getHeight() * imageHeight; System.out.println(x+"---"+y+"----"+w+"-----"+h); if(probability>0.99){ prlist.put(className+"_"+n,probability); n++; } ret.compute( className, (k, list) -> { if (list == null) { list = new ArrayList<>(); } //list.add(new BoundingBox( x, y, w, h)); return list; }); } System.out.println(prlist); image.getWrappedImage()

Screenshot (61)

no mater which dog image i use results are all same it gives me only this four result array with NO boundingboxes and sorry for too much code....i dont know what to do please suggest me

nikkisingh111333 avatar Jun 29 '21 20:06 nikkisingh111333

I see one problem with your getObjects function. The function return value is a list of objects because it is possible for there to be multiple objects in a single image. If you only have one object to detect, you should return a singleton list. However, you are returning the same value regardless of which image.

Consider changing the type of your labels instance variable to List<PairList<Long, Rectangle>>.

zachgk avatar Jul 01 '21 22:07 zachgk

i m not getting what you are saying...means you are saying that i should change getObjects return type from pairList to List<PairList> ....and i just want my model to detect one dog only ..single object...can you share a small sample it would make me clear...

Update: when i change labels instance from Pair to List its gives me cant @Override error which is obivous that i m changing return type.....i dont understand...can you please share me A CodePiece for this...??

nikkisingh111333 avatar Jul 02 '21 11:07 nikkisingh111333

@Override
public PairList<Long, Rectangle> getObjects(long index) {
    return new PairList(Collections.singletonList(labels.get(index));
}

zachgk avatar Jul 06 '21 21:07 zachgk

i m getting Null pointer exception after doing this now what should i do?

nikkisingh111333 avatar Jul 07 '21 18:07 nikkisingh111333