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 package org.openimaj.workinprogress.featlearn;
31
32 import java.io.BufferedInputStream;
33 import java.io.DataInputStream;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Random;
41
42 import org.openimaj.data.dataset.Dataset;
43 import org.openimaj.image.FImage;
44 import org.openimaj.image.Image;
45 import org.openimaj.image.ImageUtilities;
46 import org.openimaj.image.MBFImage;
47 import org.openimaj.image.annotation.evaluation.datasets.Caltech101;
48
49 import com.google.common.collect.Lists;
50
51 public class RandomPatchSampler<IMAGE extends Image<?, IMAGE>> implements Iterable<IMAGE> {
52 long seed = 0;
53 int nsamples;
54 int height;
55 int width;
56 Dataset<IMAGE> ds;
57
58 public RandomPatchSampler(Dataset<IMAGE> data, int width, int height, int nsamples) {
59 this.ds = data;
60 this.width = width;
61 this.height = height;
62 this.nsamples = nsamples;
63 }
64
65 public List<IMAGE> getPatches() {
66 return Lists.newArrayList(this);
67 }
68
69 @Override
70 public Iterator<IMAGE> iterator() {
71 return new Iterator<IMAGE>() {
72 Random rng = new Random(seed);
73 int i = 0;
74
75 @Override
76 public boolean hasNext() {
77 return i < nsamples;
78 }
79
80 @Override
81 public IMAGE next() {
82 i++;
83 return getRandomPatch(ds.getRandomInstance());
84 }
85
86 @Override
87 public void remove() {
88 throw new UnsupportedOperationException();
89 }
90
91 private IMAGE getRandomPatch(IMAGE instance) {
92 final int x = rng.nextInt(instance.getWidth() - width - 1);
93 final int y = rng.nextInt(instance.getHeight() - height - 1);
94
95 return instance.extractROI(x, y, width, height);
96 }
97 };
98 }
99
100 public static List<FImage> loadPatches(File file) throws IOException {
101 final List<FImage> list = new ArrayList<FImage>();
102
103 DataInputStream dis = null;
104 try {
105 dis = new DataInputStream(new BufferedInputStream(new
106 FileInputStream(file)));
107 final int width = dis.readInt();
108 final int height = dis.readInt();
109 final int nsamples = dis.readInt();
110
111 for (int i = 0; i < nsamples; i++) {
112 final FImage image = new FImage(width, height);
113 for (int y = 0; y < height; y++) {
114 for (int x = 0; x < width; x++) {
115 image.pixels[y][x] = dis.readFloat();
116 }
117 }
118 list.add(image);
119 }
120 } finally {
121 if (dis != null) {
122 try {
123 dis.close();
124 } catch (final IOException e1) {
125 }
126 }
127 }
128
129 return list;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public static void main(String[] args) throws IOException {
158 final RandomPatchSampler<MBFImage> ps = new RandomPatchSampler<MBFImage>(
159 Caltech101.getImages(ImageUtilities.MBFIMAGE_READER), 28, 28, 70000);
160
161 final File TRAINING = new File("/Users/jon/Data/caltech101-patches/training/");
162 final File TESTING = new File("/Users/jon/Data/caltech101-patches/testing/");
163
164 TRAINING.mkdirs();
165 TESTING.mkdirs();
166
167 int i = 0;
168 for (final MBFImage patch : ps) {
169 System.out.println(i);
170 if (i < 60000) {
171 ImageUtilities.write(patch, new File(TRAINING, i + ".png"));
172 } else {
173 ImageUtilities.write(patch, new File(TESTING, (i - 60000) + ".png"));
174 }
175 i++;
176 }
177 }
178 }