001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.workinprogress.featlearn;
031
032import java.io.BufferedInputStream;
033import java.io.DataInputStream;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.IOException;
037import java.util.ArrayList;
038import java.util.Iterator;
039import java.util.List;
040import java.util.Random;
041
042import org.openimaj.data.dataset.Dataset;
043import org.openimaj.image.FImage;
044import org.openimaj.image.Image;
045import org.openimaj.image.ImageUtilities;
046import org.openimaj.image.MBFImage;
047import org.openimaj.image.annotation.evaluation.datasets.Caltech101;
048
049import com.google.common.collect.Lists;
050
051public class RandomPatchSampler<IMAGE extends Image<?, IMAGE>> implements Iterable<IMAGE> {
052        long seed = 0;
053        int nsamples;
054        int height;
055        int width;
056        Dataset<IMAGE> ds;
057
058        public RandomPatchSampler(Dataset<IMAGE> data, int width, int height, int nsamples) {
059                this.ds = data;
060                this.width = width;
061                this.height = height;
062                this.nsamples = nsamples;
063        }
064
065        public List<IMAGE> getPatches() {
066                return Lists.newArrayList(this);
067        }
068
069        @Override
070        public Iterator<IMAGE> iterator() {
071                return new Iterator<IMAGE>() {
072                        Random rng = new Random(seed);
073                        int i = 0;
074
075                        @Override
076                        public boolean hasNext() {
077                                return i < nsamples;
078                        }
079
080                        @Override
081                        public IMAGE next() {
082                                i++;
083                                return getRandomPatch(ds.getRandomInstance());
084                        }
085
086                        @Override
087                        public void remove() {
088                                throw new UnsupportedOperationException();
089                        }
090
091                        private IMAGE getRandomPatch(IMAGE instance) {
092                                final int x = rng.nextInt(instance.getWidth() - width - 1);
093                                final int y = rng.nextInt(instance.getHeight() - height - 1);
094
095                                return instance.extractROI(x, y, width, height);
096                        }
097                };
098        }
099
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}