-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
301 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.idea | ||
*.iml | ||
.idea | ||
*.iml | ||
dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright 2017 Tadashi Kishimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright 2017 Tadashi Kishimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
package to.kishimo.minist; | ||
|
||
/** | ||
* 定数. | ||
*/ | ||
public interface Const { | ||
/** | ||
* MNISTの手書き文字データのベースURL. | ||
*/ | ||
public static final String BASE_URL = "http://yann.lecun.com/exdb/mnist/"; | ||
|
||
/** | ||
* MNISTの手書き文字データを保存するベースパス. | ||
*/ | ||
public static final String BASE_PATH = "./dataset/mnist/"; | ||
|
||
/** | ||
* トレーニング用の画像データのファイル名. | ||
*/ | ||
public static final String TRAIN_IMAGE_FILE = "train-images-idx3-ubyte.gz"; | ||
|
||
/** | ||
* トレーニング用のラベルデータのファイル名. | ||
*/ | ||
public static final String TRAIN_LABEL_FILE = "train-labels-idx1-ubyte.gz"; | ||
|
||
/** | ||
* テスト用の画像データのファイル名. | ||
*/ | ||
public static final String TEST_IMAGE_FILE = "t10k-images-idx3-ubyte.gz"; | ||
|
||
/** | ||
* テスト用のラベルデータのファイル名. | ||
*/ | ||
public static final String TEST_LABEL_FILE = "t10k-labels-idx1-ubyte.gz"; | ||
} | ||
package to.kishimo.minist; | ||
|
||
/** | ||
* 定数. | ||
*/ | ||
public interface Const { | ||
/** | ||
* MNISTの手書き文字データのベースURL. | ||
*/ | ||
public static final String BASE_URL = "http://yann.lecun.com/exdb/mnist/"; | ||
|
||
/** | ||
* MNISTの手書き文字データを保存するベースパス. | ||
*/ | ||
public static final String BASE_PATH = "./dataset/mnist/"; | ||
|
||
/** | ||
* トレーニング用の画像データのファイル名. | ||
*/ | ||
public static final String TRAIN_IMAGE_FILE = "train-images-idx3-ubyte.gz"; | ||
|
||
/** | ||
* トレーニング用のラベルデータのファイル名. | ||
*/ | ||
public static final String TRAIN_LABEL_FILE = "train-labels-idx1-ubyte.gz"; | ||
|
||
/** | ||
* テスト用の画像データのファイル名. | ||
*/ | ||
public static final String TEST_IMAGE_FILE = "t10k-images-idx3-ubyte.gz"; | ||
|
||
/** | ||
* テスト用のラベルデータのファイル名. | ||
*/ | ||
public static final String TEST_LABEL_FILE = "t10k-labels-idx1-ubyte.gz"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,97 @@ | ||
package to.kishimo.minist; | ||
|
||
import java.io.*; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* MNISTの手書き文字の画像データを扱うクラス. | ||
*/ | ||
public class ImageDataSet implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
private String fileName = ""; | ||
private int numImages; | ||
private int numDimensions; | ||
private double[][] features; | ||
|
||
/** | ||
* MNISTの手書き文字の画像データファイルを読み込んで、特徴量データに変換する. | ||
* | ||
* @param fileName 画像データのファイル名 | ||
*/ | ||
private ImageDataSet(String fileName) throws IOException, ClassNotFoundException { | ||
this.fileName = fileName; | ||
|
||
File baseDir = new File(Const.BASE_PATH); | ||
if (!baseDir.exists()) { | ||
baseDir.mkdirs(); | ||
} | ||
|
||
Util.download(Const.BASE_URL, Const.BASE_PATH, fileName); | ||
} | ||
|
||
/** | ||
* 画像データセットのインスタンスを作成する. | ||
* | ||
* @param fileName 画像データのファイル名 | ||
* @return 画像データセットのインスタンス | ||
*/ | ||
public static ImageDataSet create(String fileName) throws IOException, ClassNotFoundException { | ||
if (new File(Const.BASE_PATH + fileName + ".ser").exists()) { | ||
System.out.println("Deserializing feature data from " + fileName + ".ser ..."); | ||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Const.BASE_PATH + fileName + ".ser")); | ||
return (ImageDataSet) ois.readObject(); | ||
} else { | ||
System.out.println("Loading feature data from " + fileName + " ..."); | ||
ImageDataSet imageDataSet = new ImageDataSet(fileName); | ||
imageDataSet.loadFeatures(); | ||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Const.BASE_PATH + fileName + ".ser")); | ||
oos.writeObject(imageDataSet); | ||
return imageDataSet; | ||
} | ||
} | ||
|
||
/** | ||
* 画像数を取得する. | ||
* | ||
* @return 画像数 | ||
*/ | ||
|
||
public int getNumImages() { | ||
return numImages; | ||
} | ||
|
||
/** | ||
* 特徴量の次元数を取得する. | ||
* | ||
* @return 特徴量の次元数 | ||
*/ | ||
public int getNumDimensions() { | ||
return numDimensions; | ||
} | ||
|
||
/** | ||
* 特徴量データを取得する. | ||
* | ||
* @return 特徴量データ | ||
*/ | ||
public double[][] getFeatures() { | ||
return features; | ||
} | ||
|
||
/** | ||
* 特徴量データを読み込む. | ||
*/ | ||
private void loadFeatures() throws IOException { | ||
DataInputStream is = new DataInputStream(new GZIPInputStream(new FileInputStream(Const.BASE_PATH + fileName))); | ||
is.readInt(); | ||
numImages = is.readInt(); | ||
numDimensions = is.readInt() * is.readInt(); | ||
|
||
features = new double[numImages][numDimensions]; | ||
for (int i = 0; i < numImages; i++) { | ||
for (int j = 0; j < numDimensions; j++) { | ||
features[i][j] = (double) is.readUnsignedByte() / 255.0; | ||
} | ||
} | ||
} | ||
} | ||
package to.kishimo.minist; | ||
|
||
import java.io.*; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* MNISTの手書き文字の画像データを扱うクラス. | ||
*/ | ||
public class ImageDataSet implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
private String fileName = ""; | ||
private int numImages; | ||
private int numDimensions; | ||
private double[][] features; | ||
|
||
/** | ||
* MNISTの手書き文字の画像データファイルを読み込んで、特徴量データに変換する. | ||
* | ||
* @param fileName 画像データのファイル名 | ||
*/ | ||
private ImageDataSet(String fileName) throws IOException, ClassNotFoundException { | ||
this.fileName = fileName; | ||
|
||
File baseDir = new File(Const.BASE_PATH); | ||
if (!baseDir.exists()) { | ||
baseDir.mkdirs(); | ||
} | ||
|
||
Util.download(Const.BASE_URL, Const.BASE_PATH, fileName); | ||
} | ||
|
||
/** | ||
* 画像データセットのインスタンスを作成する. | ||
* | ||
* @param fileName 画像データのファイル名 | ||
* @return 画像データセットのインスタンス | ||
*/ | ||
public static ImageDataSet create(String fileName) throws IOException, ClassNotFoundException { | ||
if (new File(Const.BASE_PATH + fileName + ".ser").exists()) { | ||
System.out.println("Deserializing feature data from " + fileName + ".ser ..."); | ||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Const.BASE_PATH + fileName + ".ser")); | ||
return (ImageDataSet) ois.readObject(); | ||
} else { | ||
System.out.println("Loading feature data from " + fileName + " ..."); | ||
ImageDataSet imageDataSet = new ImageDataSet(fileName); | ||
imageDataSet.loadFeatures(); | ||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Const.BASE_PATH + fileName + ".ser")); | ||
oos.writeObject(imageDataSet); | ||
return imageDataSet; | ||
} | ||
} | ||
|
||
/** | ||
* 画像数を取得する. | ||
* | ||
* @return 画像数 | ||
*/ | ||
|
||
public int getNumImages() { | ||
return numImages; | ||
} | ||
|
||
/** | ||
* 特徴量の次元数を取得する. | ||
* | ||
* @return 特徴量の次元数 | ||
*/ | ||
public int getNumDimensions() { | ||
return numDimensions; | ||
} | ||
|
||
/** | ||
* 特徴量データを取得する. | ||
* | ||
* @return 特徴量データ | ||
*/ | ||
public double[][] getFeatures() { | ||
return features; | ||
} | ||
|
||
/** | ||
* 特徴量データを読み込む. | ||
*/ | ||
private void loadFeatures() throws IOException { | ||
DataInputStream is = new DataInputStream(new GZIPInputStream(new FileInputStream(Const.BASE_PATH + fileName))); | ||
is.readInt(); | ||
numImages = is.readInt(); | ||
numDimensions = is.readInt() * is.readInt(); | ||
|
||
features = new double[numImages][numDimensions]; | ||
for (int i = 0; i < numImages; i++) { | ||
for (int j = 0; j < numDimensions; j++) { | ||
features[i][j] = (double) is.readUnsignedByte() / 255.0; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.