Skip to content

Instantly share code, notes, and snippets.

View yptheangel's full-sized avatar
Working from home

Choo Wilson yptheangel

Working from home
View GitHub Profile
@yptheangel
yptheangel / Plotter.java
Created May 14, 2020 15:30
incomplete learning rate finder plotter
import java.awt.Color;
import java.awt.BasicStroke;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.XYPlot;
@yptheangel
yptheangel / Infer.java
Created April 29, 2020 03:21
Draw Face bounding box and face landmarks
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Scalar;
import java.util.ArrayList;
import java.util.List;
import static org.bytedeco.opencv.global.opencv_highgui.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgproc.circle;
@yptheangel
yptheangel / convert.py
Created April 27, 2020 14:17
xmltotxt
# script referenced from https://github.com/Cartucho/mAP/tree/master/scripts/extra
import sys
import os
import glob
import xml.etree.ElementTree as ET
# make sure that the cwd() in the beginning is the location of the python script (so that every path makes sense)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# change directory to the one with the files to be changed
@yptheangel
yptheangel / QuantizationDL4J.java
Last active October 20, 2020 14:38
Ways to optimize Deeplearning4j model, FP32 to FP16 Quantization
// Tip 1: Do not save the updater if you do not plan to continue training your model
// Set false for saveUpdater flag.
ModelSerializer.writeModel(model, modelFilename, false);
// Results: Model size drop almost 40%.
// Tip 2: Convert FP32 to FP16 floating point precision(Quantization), currently DL4J only supports float. {DOUBLE, FLOAT, HALF}
// Convert the parameters just as below:
model = model.convertDataType(DataType.HALF);
// Results: Model size drop by 50%, half of its original size. Accuracy did not drop.
@yptheangel
yptheangel / CatDogClassifier.java
Created April 16, 2020 09:11
An example for Resnet50 transfer learning in DL4J
import org.datavec.api.io.filters.BalancedPathFilter;
import org.datavec.api.io.labels.ParentPathLabelGenerator;
import org.datavec.api.split.FileSplit;
import org.datavec.api.split.InputSplit;
import org.datavec.image.loader.BaseImageLoader;
import org.datavec.image.recordreader.ImageRecordReader;
import org.deeplearning4j.api.storage.StatsStorage;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
@yptheangel
yptheangel / yolo2image.java
Created April 14, 2020 16:13
an example to run DL4J YOLO2 on an image file
package global.skymind.solution.convolution.objectdetection;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.bytedeco.opencv.opencv_core.Size;
import org.datavec.image.loader.NativeImageLoader;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.layers.objdetect.DetectedObject;
import org.deeplearning4j.nn.layers.objdetect.YoloUtils;
@yptheangel
yptheangel / yolo2customdataset.java
Created April 9, 2020 07:29
an example of dl4j example
/*
*
* * ******************************************************************************
* * * Copyright (c) 2019 Skymind AI Bhd.
* * * Copyright (c) 2020 CertifAI Sdn. Bhd.
* * *
* * * This program and the accompanying materials are made available under the
* * * terms of the Apache License, Version 2.0 which is available at
* * * https://www.apache.org/licenses/LICENSE-2.0.
* * *
@yptheangel
yptheangel / pom.xml
Last active April 25, 2020 07:50
My minimalist pom.xml for using Eclipse Deeplearning4j in a Maven project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.certifai</groupId>
<artifactId>MyDL4JProject</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Computer Vision</name>
@yptheangel
yptheangel / Flatten.java
Last active April 6, 2020 03:18
Flatten in Eclipse Deeplearning4j, For example to flatten Rank 4 tensor to Rank 2 (batch size, nIn) input to the Dense Layer.
// Take note this config is just an example
// Take note that my input is a Rank 4 tensor, a DataSetIterator that uses ImageRecordReader
// Method 1: using .setInputTypes(InputType.convolutional(height,width,channels))
ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder()
.seed(seed)
.updater(new RmsProp(1e-3))
.weightInit(WeightInit.XAVIER)
.l2(1e-4)
.graphBuilder()
.addInputs("input")
@yptheangel
yptheangel / ContinueTrainingFromCheckpoint.java
Created April 5, 2020 08:54
Load DL4J model checkpoint and continue training.
// Train your model and save the model if the training loss is better than the previous. Set saveUpdater boolean as "true" if you plan to continue training. Needed for updaters like Adam.
for (int i = 1; i < epochs + 1; i++) {
tunedModel.fit(trainIterator);
if (tunedModel.score() < lowest) {
lowest = tunedModel.score();
String modelFilename = new File(".").getAbsolutePath() + "/ImageClassifier_loss" + lowest + "_ep" + i + ".zip";
ModelSerializer.writeModel(tunedModel, modelFilename, true);
}
System.out.println(String.format("Completed epoch %d.", i));
}