Skip to content

Commit 9ea738d

Browse files
committed
Pixel encoder changed to raw pixel encoder
1 parent d7786ea commit 9ea738d

File tree

5 files changed

+32
-46
lines changed

5 files changed

+32
-46
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- Unreleased
1+
- 0.0.4-alpha
22
- Added Dropout hidden layer
33
- Added K-d Neighbors classifier and regressor
44
- Added Extra Tree Regressor
@@ -14,6 +14,8 @@
1414
- Added min dimensionality estimation on random projectors
1515
- Added Gaussian Random Projector
1616
- Removed Ellipsoidal distance kernel
17+
- Added Thresholded ReLU activation function
18+
- Changed API of Raw Pixel Encoder
1719

1820
- 0.0.3-alpha
1921
- Added Extra Tree classifier

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ MIT
3737
- [Unlabeled](#unlabeled)
3838
- [Feature Extractors](#feature-extractors)
3939
- [Word Count Vectorizer](#word-count-vectorizer)
40-
- [Pixel Encoder](#pixel-encoder)
40+
- [Raw Pixel Encoder](#raw-pixel-encoder)
4141
- [Estimators](#estimators)
4242
- [Anomaly Detectors](#anomaly-detectors)
4343
- [Isolation Forest](#isolation-forest)
@@ -765,16 +765,15 @@ use Rubix\ML\Extractors\Tokenizers\Word;
765765
$extractor = new WordCountVectorizer(5000, ['the', 'what', 'has'], true, new Word());
766766
```
767767

768-
### Pixel Encoder
769-
Images must first be converted to color channel values in order to be passed to an Estimator. The Pixel Encoder takes an array of images (as [PHP Resources](http://php.net/manual/en/language.types.resource.php)) and converts them to a flat vector of color channel data. Image scaling and cropping is handled automatically by [Intervention Image](http://image.intervention.io/). The GD extension is required to use this feature.
768+
### Raw Pixel Encoder
769+
The Raw Pixel Encoder takes an array of images (as [PHP Resources](http://php.net/manual/en/language.types.resource.php)) and converts them to a flat vector of raw color channel data. Scaling and cropping is handled automatically by [Intervention Image](http://image.intervention.io/) for PHP. Note that the GD extension is required to use this feature.
770770

771771
##### Parameters:
772772
| # | Param | Default | Type | Description |
773773
|--|--|--|--|--|
774774
| 1 | size | [32, 32] | array | A tuple of width and height values denoting the resolution of the encoding. |
775775
| 2 | rgb | true | bool | True to use RGB color channel data and False to use Greyscale. |
776-
| 3 | sharpen | 0 | int | A value between 0 and 100 indicating the amount of sharpness to add to each sample. |
777-
| 4 | driver | 'gd' | string | The PHP extension to use for image processing ('gd' *or* 'imagick'). |
776+
| 3 | driver | 'gd' | string | The PHP extension to use for image processing ('gd' *or* 'imagick'). |
778777

779778
##### Additional Methods:
780779
This Extractor does not have any additional methods.
@@ -783,7 +782,7 @@ This Extractor does not have any additional methods.
783782
```php
784783
use Rubix\ML\Extractors\PixelEncoder;
785784

786-
$extractor = new PixelEncoder([28, 28], false, 'imagick');
785+
$extractor = new PixelEncoder([28, 28], true, 'gd');
787786
```
788787

789788
---

programs/MushroomClassifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
$hidden = [
4848
new Dense(20, new ThresholdedReLU(0.0)),
49-
new Dense(20, new ThresholdedReLU(0.0)),
49+
new Dense(25, new ThresholdedReLU(0.0)),
5050
new Dense(20, new ThresholdedReLU(0.0)),
5151
];
5252

src/Extractors/PixelEncoder.php renamed to src/Extractors/RawPixelEncoder.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@
88
use RuntimeException;
99

1010
/**
11-
* Pixel Encoder
11+
* Raw Pixel Encoder
1212
*
13-
* Images must first be converted to color channel values in order to be passed
14-
* to an Estimator. The Pixel Encoder takes an array of images (as PHP Resources)
15-
* and converts them to a flat vector of color channel data. Image scaling and
16-
* cropping is handled automatically by Intervention Image. The GD extension is
17-
* required to use this feature.
13+
* The Raw Pixel Encoder takes an array of images (as PHP Resources)
14+
* and converts them into a flat vector of raw color channel data. Scaling and
15+
* cropping is handled automatically by Intervention Image for PHP. Note that
16+
* the GD extension is required to use this feature.
1817
*
1918
* @category Machine Learning
2019
* @package Rubix/ML
2120
* @author Andrew DalPino
2221
*/
23-
class PixelEncoder implements Extractor
22+
class RawPixelEncoder implements Extractor
2423
{
2524
/**
2625
* The image will be scaled and cropped according to the setting of this
@@ -31,21 +30,12 @@ class PixelEncoder implements Extractor
3130
protected $size;
3231

3332
/**
34-
* The number of channels to encode. Each channel requires width x height
35-
* number of features.
33+
* The number of color channels to encode.
3634
*
3735
* @var int
3836
*/
3937
protected $channels;
4038

41-
/**
42-
* The amount of sharpness to apply to the image before vectorization.
43-
* 0 - 100.
44-
*
45-
* @var int
46-
*/
47-
protected $sharpen;
48-
4939
/**
5040
* The Intervention image manager instance.
5141
*
@@ -60,29 +50,25 @@ class PixelEncoder implements Extractor
6050
* @throws \InvalidArgumentException
6151
* @return void
6252
*/
63-
public function __construct(array $size = [32, 32], bool $rgb = true,
64-
int $sharpen = 0, string $driver = 'gd')
53+
public function __construct(array $size = [32, 32], bool $rgb = true, string $driver = 'gd')
6554
{
6655
if (count($size) !== 2) {
6756
throw new InvalidArgumentException('Size must have a width and a'
6857
. ' height.');
6958
}
7059

71-
foreach ($size as $dimension) {
72-
if ($dimension < 1) {
73-
throw new InvalidArgumentException('Width and height must be'
74-
. ' greater than 1 pixel.');
75-
}
60+
if (!is_int($size[0]) and !is_int($size[1])) {
61+
throw new InvalidArgumentException('Width and height must be'
62+
. ' integers.');
7663
}
7764

78-
if ($sharpen < 0 or $sharpen > 100) {
79-
throw new InvalidArgumentException('Sharpness factor must be'
80-
. ' between 0 and 100');
65+
if ($size[0] < 1 or $size[1] < 1) {
66+
throw new InvalidArgumentException('Width and height must be'
67+
. ' greater than 1 pixel.');
8168
}
8269

8370
$this->size = $size;
8471
$this->channels = $rgb ? 3 : 1;
85-
$this->sharpen = $sharpen;
8672
$this->intervention = new ImageManager(['driver' => $driver]);
8773
}
8874

@@ -115,8 +101,7 @@ public function extract(array $samples) : array
115101
$image = $image->greyscale();
116102
}
117103

118-
$image->fit(...$this->size)
119-
->sharpen($this->sharpen);
104+
$image->fit(...$this->size);
120105

121106
$vectors[] = $this->vectorize($image);
122107
}
@@ -139,11 +124,11 @@ public function vectorize(Image $image) : array
139124

140125
for ($x = 0; $x < $this->size[0]; $x++) {
141126
for ($y = 0; $y < $this->size[1]; $y++) {
142-
$rgba = imagecolorsforindex($image,
143-
imagecolorat($image, $x, $y));
127+
$rgba = imagecolorsforindex($image, imagecolorat($image, $x, $y));
144128

145-
$vector = array_merge($vector,
146-
array_values(array_slice($rgba, 0, $this->channels)));
129+
foreach (array_slice($rgba, 0, $this->channels) as $value) {
130+
$vector[] = $value;
131+
};
147132
}
148133
}
149134

tests/Extractors/PixelEncoderTest.php renamed to tests/Extractors/RawPixelEncoderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Rubix\Tests\Extractors;
44

55
use Rubix\ML\Extractors\Extractor;
6-
use Rubix\ML\Extractors\PixelEncoder;
6+
use Rubix\ML\Extractors\RawPixelEncoder;
77
use PHPUnit\Framework\TestCase;
88

9-
class PixelEncoderTest extends TestCase
9+
class RawPixelEncoderTest extends TestCase
1010
{
1111
protected $extractor;
1212

@@ -18,12 +18,12 @@ public function setUp()
1818
imagecreatefromjpeg(__DIR__ . '/space.jpg'),
1919
];
2020

21-
$this->extractor = new PixelEncoder([3, 3], true, 0, 'gd');
21+
$this->extractor = new RawPixelEncoder([3, 3], true, 'gd');
2222
}
2323

2424
public function test_build_count_vectorizer()
2525
{
26-
$this->assertInstanceOf(PixelEncoder::class, $this->extractor);
26+
$this->assertInstanceOf(RawPixelEncoder::class, $this->extractor);
2727
$this->assertInstanceOf(Extractor::class, $this->extractor);
2828
}
2929

0 commit comments

Comments
 (0)