(detector): import and export modelData as bin
parent
f20c6209bf
commit
00c8ce6836
Binary file not shown.
|
|
@ -16,10 +16,30 @@ public class Main {
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
String path = "data/labels.csv";
|
String path = "data/labels.csv";
|
||||||
// User Dialog
|
// User Dialog
|
||||||
String userInput = IO.readln("Using " + path + " as standard path for training data. Continue? (ENTER)");
|
|
||||||
if(!userInput.isEmpty()){
|
String modelPath = "data/modelData.bin";
|
||||||
path = IO.readln("Enter your path: ");
|
File modelFile = new File(modelPath);
|
||||||
|
if(modelFile.exists()){
|
||||||
|
String userInput = IO.readln("Found a model in your data directory, continue with it? (y/n)");
|
||||||
|
if(userInput.toLowerCase().contains("y")){
|
||||||
|
useImportedModel("data");
|
||||||
|
} else{
|
||||||
|
useNewModel();
|
||||||
}
|
}
|
||||||
|
} else{
|
||||||
|
useNewModel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void useImportedModel(String modelPath){
|
||||||
|
IO.println("Using " + modelPath);
|
||||||
|
String csvPath = "data/labels.csv";
|
||||||
|
Detector genreDetector = new Detector(csvPath, modelPath);
|
||||||
|
runModel(genreDetector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void useNewModel(){
|
||||||
|
String path = "data/labels.csv";
|
||||||
|
|
||||||
IO.println("Starting training...");
|
IO.println("Starting training...");
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
@ -30,17 +50,26 @@ public class Main {
|
||||||
long stop = System.currentTimeMillis();
|
long stop = System.currentTimeMillis();
|
||||||
IO.println("Training finished! (" + (int) (stop-start) +"ms)");
|
IO.println("Training finished! (" + (int) (stop-start) +"ms)");
|
||||||
|
|
||||||
// User input text
|
runModel(genreDetector);
|
||||||
// Predict genre
|
|
||||||
path = "input.txt";
|
|
||||||
userInput = IO.readln("Using input.txt as source? (y/n)");
|
|
||||||
if(!userInput.toLowerCase().contains("y")){
|
|
||||||
path = IO.readln("Enter your path");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Scanner scanner = new Scanner(new File(path), "UTF-8");
|
public static void runModel(Detector genreDetector){
|
||||||
Map<String, Double> result = genreDetector.askModel(scanner);
|
// User input text
|
||||||
|
// Predict genre
|
||||||
|
String path = "input.txt";
|
||||||
|
IO.println("Using input.txt as source");
|
||||||
|
Scanner scanner = null;
|
||||||
|
try {
|
||||||
|
scanner = new Scanner(new File(path), "UTF-8");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
IO.println("Couldn't find input file: " + path+ " - " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Double> result = genreDetector.askModel(scanner);
|
||||||
|
printResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printResult(Map<String, Double> result){
|
||||||
IO.println("");
|
IO.println("");
|
||||||
|
|
||||||
for (Map.Entry<String, Double> entry : result.entrySet()) {
|
for (Map.Entry<String, Double> entry : result.entrySet()) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package de.nicode3141.genredct;
|
package de.nicode3141.genredct;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Detector {
|
public class Detector {
|
||||||
|
|
@ -8,9 +9,10 @@ public class Detector {
|
||||||
private static final int EPOCH_SIZE = 5;
|
private static final int EPOCH_SIZE = 5;
|
||||||
private static final double SCALE = 1000.0;
|
private static final double SCALE = 1000.0;
|
||||||
private static final int SKIP_TOP = 50;
|
private static final int SKIP_TOP = 50;
|
||||||
|
private static final String modelOutputFile = "modelData.bin";
|
||||||
private int genreCount;
|
private int genreCount;
|
||||||
public Map<String, double[]> modelData;
|
public Map<String, double[]> modelData;
|
||||||
public final Set<String> vocabulary;
|
public Set<String> vocabulary;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the training with provided data
|
* Starts the training with provided data
|
||||||
|
|
@ -23,7 +25,7 @@ public class Detector {
|
||||||
// trainModel with labels and WordCount
|
// trainModel with labels and WordCount
|
||||||
// returns weights
|
// returns weights
|
||||||
|
|
||||||
Map<String, List<String>> genreData = GenreData.loadGenres("data/labels.csv");
|
Map<String, List<String>> genreData = GenreData.loadGenres(csvPath);
|
||||||
assert genreData != null;
|
assert genreData != null;
|
||||||
List<String> allPaths = genreData.values()
|
List<String> allPaths = genreData.values()
|
||||||
.stream()
|
.stream()
|
||||||
|
|
@ -42,23 +44,67 @@ public class Detector {
|
||||||
modelData.put(genres.getKey(), currWeights);
|
modelData.put(genres.getKey(), currWeights);
|
||||||
genreCount++;
|
genreCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exportModel("data");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the training with already trained data
|
||||||
|
* @param csvPath path of the csv containing all texts
|
||||||
|
* @param modelPath path of the .bin file containing training data
|
||||||
|
* @return created weights for each genre + bias
|
||||||
|
*/
|
||||||
|
public Detector(String csvPath, String modelPath) {
|
||||||
|
// Build common vocabulary
|
||||||
|
// Get labels and WordCounts from countTrainData()
|
||||||
|
// trainModel with labels and WordCount
|
||||||
|
// returns weights
|
||||||
|
importModel(modelPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores the training data as a .bin file
|
* Stores the training data as a .bin file
|
||||||
* @param pathTo where to store .bin
|
* @param pathTo folder where to store .bin
|
||||||
*/
|
*/
|
||||||
public void exportModel(String pathTo){
|
public void exportModel(String pathTo){
|
||||||
// TODO: use BufferedOutputStream
|
// TODO: use BufferedOutputStream
|
||||||
// For each element in the map, write Key, write weights, write bias
|
// For each element in the map, write Key, write weights, write bias
|
||||||
|
String filePath = pathTo + "/" + modelOutputFile;
|
||||||
|
try{
|
||||||
|
// "Java isn't a bad language..." Then look right here ;-)
|
||||||
|
|
||||||
|
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||||
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
|
||||||
|
ObjectOutputStream outputStream = new ObjectOutputStream(bufferedOutputStream);
|
||||||
|
|
||||||
|
outputStream.writeObject(modelData);
|
||||||
|
outputStream.writeObject(vocabulary);
|
||||||
|
outputStream.close();
|
||||||
|
IO.println("Written Model Data to " + filePath);
|
||||||
|
} catch(IOException err){
|
||||||
|
IO.println("Error whilst storing model data: " + err.getLocalizedMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports the training data from a .bin file
|
* Imports the training data from a .bin file
|
||||||
* @param pathTo where .bin is located
|
* @param pathTo folder where .bin is located
|
||||||
*/
|
*/
|
||||||
public void importModel(String pathTo){
|
public void importModel(String pathTo){
|
||||||
// TODO: use BufferedInputStream
|
// TODO: use BufferedInputStream
|
||||||
|
String filePath = pathTo + "/" + modelOutputFile;
|
||||||
|
try {
|
||||||
|
// again... Worst design decisions ever made
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||||
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||||
|
ObjectInputStream inputStream = new ObjectInputStream(bufferedInputStream);
|
||||||
|
|
||||||
|
this.modelData = (Map<String, double[]>) inputStream.readObject();
|
||||||
|
this.vocabulary = (Set<String>) inputStream.readObject();
|
||||||
|
inputStream.close();
|
||||||
|
} catch (IOException | ClassNotFoundException err) {
|
||||||
|
IO.println("Error whilst reading model data: " + err.getLocalizedMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -136,6 +182,8 @@ public class Detector {
|
||||||
Random random = new Random(2);
|
Random random = new Random(2);
|
||||||
int totalRuns = 0;
|
int totalRuns = 0;
|
||||||
|
|
||||||
|
// Epochs help to improve the result
|
||||||
|
// At the start all weights are 0, so the model is dumb
|
||||||
for(int j = 0; j < EPOCH_SIZE; j++) {
|
for(int j = 0; j < EPOCH_SIZE; j++) {
|
||||||
|
|
||||||
// Shuffled ordered index (currently ordered by genre)
|
// Shuffled ordered index (currently ordered by genre)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue