-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Fix bugs in loading/saving items 2. Add image buffer for efficient storage of pictures 3. replace old MNIST dataset with new one that we generates
- Loading branch information
Showing
11 changed files
with
215 additions
and
122 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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Tensor } from 'backend/tensor.js'; | ||
import { assert } from 'util/assert.js'; | ||
import { clip_pixel, scale_shift } from 'backend/ops.js'; | ||
|
||
/** | ||
* This class provides a more uniform and general way to store and save images with same shape | ||
*/ | ||
class ImageBuffer { | ||
constructor() {} | ||
|
||
/** | ||
* @param { Array } tsArray - Array of Tensor | ||
*/ | ||
static fromTensors(tsArray) { | ||
assert(tsArray.length > 0, 'empty array'); | ||
// we assume that all images have the same shape | ||
let ib = new ImageBuffer(); | ||
ib.shape = tsArray[0].shape; | ||
ib.images = []; | ||
let size = tsArray[0].size; | ||
for (let i of tsArray) { | ||
let x = i.clone(); | ||
clip_pixel(x); | ||
let ba = new Uint8ClampedArray(size); | ||
for (let j = 0; j < size; j++) { | ||
ba[j] = x.w[j]; | ||
} | ||
ib.images.push(ba); | ||
} | ||
return ib; | ||
} | ||
|
||
get tensors() { | ||
return this.images.map(b => { | ||
let t = new Tensor(this.shape); | ||
let N = t.size; | ||
for (let i = 0; i < N; i++) { | ||
t.w[i] = (b[i] / 255.0) * 2.0 - 1.0; | ||
} | ||
return t; | ||
}); | ||
} | ||
|
||
/** | ||
* Load images from buffer | ||
* @param { object } t - Object that contains info about tensor we want to get | ||
* @param { BufferReader } buf - The ArrayBuffer contains data | ||
* @return { ImageBuffer } - the loaded images | ||
*/ | ||
static load(map, buf) { | ||
let ib = new ImageBuffer(); | ||
ib.name = map.name; | ||
ib.shape = map.shape; | ||
ib.images = []; | ||
|
||
let size = ib.shape[0] * ib.shape[1] * ib.shape[2]; // FIXME: may a more general way? | ||
// OK, load values from buffer | ||
for (let i = 0; i < map.count; i++) { | ||
ib.images.push(buf.read(size, 'Uint8ClampedArray')); | ||
} | ||
return ib; | ||
} | ||
|
||
save(buf) { | ||
for (let i of this.images) buf.write(i); | ||
return {name: this.name, shape: this.shape, count: this.images.length, type: 'images'}; | ||
} | ||
} | ||
|
||
export { ImageBuffer }; |
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
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
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"name":"mnist/","nodes":[{"name":"x","shape":[28,28,1],"count":2000,"type":"images"},{"type":"Int32Array","name":"y","length":2000}]}] |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.