View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 	// public void save(File file) throws IOException {
133 	// DataOutputStream dos = null;
134 	// try {
135 	// dos = new DataOutputStream(new FileOutputStream(file));
136 	// dos.writeInt(this.width);
137 	// dos.writeInt(this.height);
138 	// dos.writeInt(this.nsamples);
139 	//
140 	// for (final FImage image : this) {
141 	// for (int y = 0; y < height; y++) {
142 	// for (int x = 0; x < width; x++) {
143 	// dos.writeFloat(image.pixels[y][x]);
144 	// }
145 	// }
146 	// }
147 	// } finally {
148 	// if (dos != null) {
149 	// try {
150 	// dos.close();
151 	// } catch (final IOException e1) {
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 }