| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | from __future__ import print_function, absolute_import |
| |
|
| | import argparse |
| | import numpy as np |
| | import os |
| |
|
| | import joblib |
| | from sklearn import svm |
| |
|
| |
|
| | def preprocess_mnist(raw, withlabel, ndim, scale, image_dtype, label_dtype, rgb_format): |
| | images = raw["x"] |
| | if ndim == 2: |
| | images = images.reshape(-1, 28, 28) |
| | elif ndim == 3: |
| | images = images.reshape(-1, 1, 28, 28) |
| | if rgb_format: |
| | images = np.broadcast_to(images, (len(images), 3) + images.shape[2:]) |
| |
|
| | elif ndim != 1: |
| | raise ValueError("invalid ndim for MNIST dataset") |
| | images = images.astype(image_dtype) |
| | images *= scale / 255.0 |
| |
|
| | if withlabel: |
| | labels = raw["y"].astype(label_dtype) |
| | return images, labels |
| | return images |
| |
|
| |
|
| | if __name__ == "__main__": |
| | parser = argparse.ArgumentParser() |
| |
|
| | |
| | parser.add_argument("--epochs", type=int, default=-1) |
| | parser.add_argument("--output-data-dir", type=str, default=os.environ["SM_OUTPUT_DATA_DIR"]) |
| | parser.add_argument("--model-dir", type=str, default=os.environ["SM_MODEL_DIR"]) |
| | parser.add_argument("--train", type=str, default=os.environ["SM_CHANNEL_TRAIN"]) |
| | parser.add_argument("--test", type=str, default=os.environ["SM_CHANNEL_TEST"]) |
| |
|
| | args = parser.parse_args() |
| |
|
| | train_file = np.load(os.path.join(args.train, "train.npz")) |
| | test_file = np.load(os.path.join(args.test, "test.npz")) |
| |
|
| | preprocess_mnist_options = { |
| | "withlabel": True, |
| | "ndim": 1, |
| | "scale": 1.0, |
| | "image_dtype": np.float32, |
| | "label_dtype": np.int32, |
| | "rgb_format": False, |
| | } |
| |
|
| | |
| | train_images, train_labels = preprocess_mnist(train_file, **preprocess_mnist_options) |
| | test_images, test_labels = preprocess_mnist(test_file, **preprocess_mnist_options) |
| |
|
| | |
| | clf = svm.SVC(gamma=0.001, C=100.0, max_iter=args.epochs) |
| |
|
| | |
| | clf.fit(train_images, train_labels) |
| |
|
| | |
| | joblib.dump(clf, os.path.join(args.model_dir, "model.joblib")) |
| |
|
| |
|
| | def model_fn(model_dir): |
| | clf = joblib.load(os.path.join(model_dir, "model.joblib")) |
| | return clf |
| |
|