Last active
February 4, 2019 17:31
-
-
Save dmtrKovalenko/b7bdf95c4c3b69e1873c74fc5f279aca to your computer and use it in GitHub Desktop.
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
const data: any = [] | |
export async function imageToPixels(buffer: Buffer) { | |
const resizedBuffer = await sharp(buffer) | |
.resize(64, 64) | |
.toBuffer() | |
const image = jpeg.decode(resizedBuffer, true) | |
const numChannels = 3; | |
const numPixels = image.width * image.height; | |
const values = [] | |
for (let i = 0; i < numPixels; i++) { | |
for (let channel = 0; channel < numChannels; ++channel) { | |
values[i * numChannels + channel] = image.data[i * 4 + channel]; | |
} | |
} | |
return values.map(value => round10(value / 255)); | |
} | |
async function processDirectory( | |
directoryPath: string, | |
outPutValue: any, | |
) { | |
const directory = await fs.readdir(directoryPath); | |
for (const file of directory) { | |
const fileData = await fs.readFile(path.resolve(directoryPath, file)); | |
const pixels = await imageToPixels(fileData); | |
data.push({ input: pixels, output: [outPutValue]}) | |
} | |
} | |
async function train() { | |
await processDirectory(samoyedDir, 1) | |
await processDirectory(notSamoyedDir, 0) | |
data.forEach((item: any) => console.log(item.output[0])) | |
neuralNetwork.train(data, { errorThresh: 0.5, log: true }) | |
const progressJson = neuralNetwork.toJSON(); | |
await fs.writeFile(progressDir, JSON.stringify(progressJson)); | |
} | |
train().catch(console.log); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is
neuralNetwork
defined, and with what options?