-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManagingImageClassificationDatasets.java
130 lines (113 loc) · 4.61 KB
/
ManagingImageClassificationDatasets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* ManagingImageClassificationDatasets.java
* Copyright (C) 2020-2023 University of Waikato, Hamilton, NZ
*/
package com.github.waikatoufdl.ufdl4j.examples;
import com.github.waikatoufdl.ufdl4j.Client;
import com.github.waikatoufdl.ufdl4j.action.Datasets.Dataset;
import com.github.waikatoufdl.ufdl4j.action.Domains;
import com.github.waikatoufdl.ufdl4j.action.ImageClassificationDatasets;
import com.github.waikatoufdl.ufdl4j.action.Licenses.License;
import com.github.waikatoufdl.ufdl4j.action.Projects.Project;
import com.github.waikatoufdl.ufdl4j.filter.DomainFilter;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Example code for managing image classification datasets.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class ManagingImageClassificationDatasets {
/**
* Expects three arguments:
* 1. backend URL, eg http://127.0.0.1:8000
* 2. backend user, eg admin
* 3. user password, eg admin
* Otherwise the above default values are used.
* Additional arguments are interpreted as files to upload.
*
* @param args the parameters to supply
* @throws Exception if queries fail for some reason
*/
public static void main(String[] args) throws Exception {
System.out.println("--> connecting to backend");
Client client;
if (args.length < 3)
client = new Client("http://127.0.0.1:8000", "admin", "admin");
else
client = new Client(args[0], args[1], args[1]);
// get action
ImageClassificationDatasets action = client.action(ImageClassificationDatasets.class);
// list datasets
System.out.println("--> listing datasets");
for (Dataset dataset: action.list())
System.out.println(dataset);
// grab first available project ID
int project = -1;
List<Project> projects = client.projects().list();
if (projects.size() > 0)
project = projects.get(0).getPK();
// load license
License gpl3 = client.licenses().load("GPL3");
// create dataset
System.out.println("--> creating dataset");
String newName = "dummy-" + System.currentTimeMillis();
Dataset newDataset = action.create(
newName, "image classification dataset", project, gpl3, true, "");
System.out.println(newDataset);
// add files to dataset
if (args.length > 3) {
for (int i = 3; i < args.length; i++) {
File file = new File(args[i]);
System.out.println("--> adding file");
action.addFile(newDataset, file, file.getName());
// add categories
action.addCategories(newDataset, Arrays.asList(file.getName()), Arrays.asList("label1", "label2"));
// remove categories
if (i % 2 == 0)
action.removeCategories(newDataset, Arrays.asList(file.getName()), Arrays.asList("label1"));
else
action.removeCategories(newDataset, Arrays.asList(file.getName()), Arrays.asList("label2"));
// metadata
System.out.println("--> adding metadata");
action.setMetadata(newDataset, file.getName(), "Full file path: " + file.getAbsolutePath());
System.out.println(action.getMetadata(newDataset, file.getName()));
}
// reload dataset
newDataset = action.load(newDataset.getPK());
}
// get categories
System.out.println("--> downloading categories");
Map<String,List<String>> categories = action.getCategories(newDataset);
System.out.println(categories);
// download dataset
System.out.println("--> downloading dataset");
File output = new File(System.getProperty("java.io.tmpdir") + "/" + newName + ".zip");
if (action.download(newDataset, new String[]{"to-subdir-ic"}, output))
System.out.println("--> downloaded dataset to " + output);
// get file from dataset
if (args.length > 3) {
File file = new File(args[3]);
output = new File(System.getProperty("java.io.tmpdir") + "/" + newName + "-" + file.getName());
System.out.println("--> downloading file");
if (action.getFile(newDataset, file.getName(), output))
System.out.println("--> downloaded file: " + output);
}
// list datasets
Domains.Domain domain = client.domains().load("ic");
DomainFilter domainFilter = new DomainFilter(domain);
System.out.println("--> listing datasets for domain " + domain);
for (Dataset dataset: client.datasets().list(domainFilter))
System.out.println(dataset);
// delete file from dataset
if (args.length > 3) {
File file = new File(args[3]);
System.out.println("--> deleting file");
if (action.deleteFile(newDataset, file.getName()))
System.out.println("--> deleted file: " + file.getName());
}
client.close();
}
}