diff --git a/.gitignore b/.gitignore index 8881c4d..39face7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,8 @@ humbuglog.* /bin/phpunit .php_cs.cache +/data/mnist-svm-model.phpml +/data/train-images-idx3-ubyte +/data/train-labels-idx1-ubyte +/data/t10k-images-idx3-ubyte +/data/t10k-labels-idx1-ubyte diff --git a/README.md b/README.md index c97b17b..f306360 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ php classification/languageDetection.php Classification: * `languageDetection.php` - classifier build for language detection +* `minst.php` - recognize handwritten digits from MNIST dataset (to download dataset use `bin/download-mnist.sh`) Regression: diff --git a/bin/download-mnist.sh b/bin/download-mnist.sh new file mode 100755 index 0000000..02c37c9 --- /dev/null +++ b/bin/download-mnist.sh @@ -0,0 +1,14 @@ +#!/bin/bash +echo "Start download MNIST dataset" +wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz +gzip -d data/train-images-idx3-ubyte.gz + +wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz +gzip -d data/train-labels-idx1-ubyte.gz + +wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz +gzip -d data/t10k-images-idx3-ubyte.gz + +wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz +gzip -d data/t10k-labels-idx1-ubyte.gz +echo "Downloaded MNIST dataset to data/ dir" diff --git a/classification/mnist.php b/classification/mnist.php new file mode 100644 index 0000000..2c7dfbe --- /dev/null +++ b/classification/mnist.php @@ -0,0 +1,41 @@ +train($trainDataset->getSamples(), $trainDataset->getTargets()); + +echo sprintf('Train time: %s'.PHP_EOL, microtime(true) - $trainStart); + +$modelStart = microtime(true); +$modelManager = new ModelManager(); +$modelManager->saveToFile($classifier, __DIR__.'/../data/mnist-svm-model.phpml'); +echo sprintf('Persist time: %s'.PHP_EOL, microtime(true) - $modelStart); + +$predictStart = microtime(true); +$predicted = $classifier->predict($testDataset->getSamples()); +echo sprintf('Predict time: %s'.PHP_EOL, microtime(true) - $predictStart); + +echo sprintf('Accuracy: %s', Accuracy::score($testDataset->getTargets(), $predicted)); +echo sprintf('Total time: %s'.PHP_EOL, microtime(true) - $start); +echo sprintf('Memory: %s', convert(memory_get_peak_usage(true))); diff --git a/composer.json b/composer.json index 46182d7..96482c0 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,6 @@ } ], "require": { - "php-ai/php-ml": "0.6.2" + "php-ai/php-ml": "0.7.0" } } diff --git a/composer.lock b/composer.lock index 1c8a1fb..de62f2c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,32 +4,33 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "5c0b77c2b5992a99ba68384b2caaa5d6", + "content-hash": "51744d449f8402679eea8de2bde1b87b", "packages": [ { "name": "php-ai/php-ml", - "version": "0.6.2", + "version": "0.7.0", "source": { "type": "git", "url": "https://github.com/php-ai/php-ml.git", - "reference": "83f3e8de7021d58297f5357cfeb293a347a2fa39" + "reference": "db95db3e57475df53d602b32c4451438c21e46c3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-ai/php-ml/zipball/83f3e8de7021d58297f5357cfeb293a347a2fa39", - "reference": "83f3e8de7021d58297f5357cfeb293a347a2fa39", + "url": "https://api.github.com/repos/php-ai/php-ml/zipball/db95db3e57475df53d602b32c4451438c21e46c3", + "reference": "db95db3e57475df53d602b32c4451438c21e46c3", "shasum": "" }, "require": { "php": "^7.1" }, "require-dev": { - "phpstan/phpstan-phpunit": "^0.9.4", - "phpstan/phpstan-shim": "^0.9", - "phpstan/phpstan-strict-rules": "^0.9.0", + "phpbench/phpbench": "^0.14.0", + "phpstan/phpstan-phpunit": "^0.10", + "phpstan/phpstan-shim": "^0.10", + "phpstan/phpstan-strict-rules": "^0.10", "phpunit/phpunit": "^7.0.0", - "symplify/coding-standard": "^3.1", - "symplify/easy-coding-standard": "^3.1" + "symplify/coding-standard": "^5.1", + "symplify/easy-coding-standard": "^5.1" }, "type": "library", "autoload": { @@ -58,7 +59,7 @@ "machine learning", "pattern recognition" ], - "time": "2018-02-22T16:06:31+00:00" + "time": "2018-11-07T18:40:31+00:00" } ], "packages-dev": [],