Skip to content

Commit

Permalink
Refactoring the origin of weights (#199)
Browse files Browse the repository at this point in the history
* Update README to add HTTPS support and change the weights file to CDN service

* refactoring origin of weights
  • Loading branch information
ibelem authored Mar 18, 2024
1 parent dbfab8f commit 994351d
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 116 deletions.
8 changes: 8 additions & 0 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import {numpy} from './libs/numpy.js';
import {addAlert} from './ui.js';

export function weightsOrigin() {
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
return 'https://d3i5xkfad89fac.cloudfront.net';
} else {
return '..';
}
}

export function sizeOfShape(shape) {
return shape.reduce((a, b) => {
return a * b;
Expand Down
8 changes: 3 additions & 5 deletions face_recognition/facenet_nchw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';
const strides = [2, 2];
const autoPad = 'same-upper';

Expand All @@ -12,10 +12,8 @@ export class FaceNetNchw {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/facenet_nchw/weights';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/facenet_nchw/weights';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/facenet_nchw/weights';
this.inputOptions = {
mean: [127.5, 127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5, 127.5],
Expand Down
8 changes: 3 additions & 5 deletions face_recognition/facenet_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';
const strides = [2, 2];
const autoPad = 'same-upper';

Expand All @@ -12,10 +12,8 @@ export class FaceNetNhwc {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/facenet_nhwc/weights';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/facenet_nhwc/weights';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/facenet_nhwc/weights';
this.inputOptions = {
mean: [127.5, 127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5, 127.5],
Expand Down
8 changes: 3 additions & 5 deletions facial_landmark_detection/face_landmark_nchw.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy} from '../common/utils.js';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';

// SimpleCNN model with 'nchw' layout.
export class FaceLandmarkNchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/face_landmark_nchw/weights';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/face_landmark_nchw/weights';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/face_landmark_nchw/weights/';
this.inputOptions = {
inputLayout: 'nchw',
inputDimensions: [1, 3, 128, 128],
Expand Down
8 changes: 3 additions & 5 deletions facial_landmark_detection/face_landmark_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy} from '../common/utils.js';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';

// SimpleCNN model with 'nhwc' layout.
export class FaceLandmarkNhwc {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/face_landmark_nhwc/weights';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/face_landmark_nhwc/weights';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/face_landmark_nhwc/weights/';
this.inputOptions = {
inputLayout: 'nhwc',
inputDimensions: [1, 128, 128, 3],
Expand Down
8 changes: 3 additions & 5 deletions facial_landmark_detection/ssd_mobilenetv2_face_nchw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// SSD MobileNet V2 Face model with 'nchw' layout.
export class SsdMobilenetV2FaceNchw {
Expand All @@ -9,10 +9,8 @@ export class SsdMobilenetV2FaceNchw {
this.deviceType_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/ssd_mobilenetv2_face_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv2_face_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/ssd_mobilenetv2_face_nchw/weights/';
this.inputOptions = {
inputLayout: 'nchw',
margin: [1.2, 1.2, 0.8, 1.1],
Expand Down
8 changes: 3 additions & 5 deletions facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// SSD MobileNet V2 Face model with 'nhwc' layout.
export class SsdMobilenetV2FaceNhwc {
Expand All @@ -9,10 +9,8 @@ export class SsdMobilenetV2FaceNhwc {
this.deviceType_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/ssd_mobilenetv2_face_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv2_face_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/ssd_mobilenetv2_face_nhwc/weights/';
this.inputOptions = {
inputLayout: 'nhwc',
margin: [1.2, 1.2, 0.8, 1.1],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/mobilenet_nchw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy} from '../common/utils.js';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';

// MobileNet V2 model with 'nchw' input layout
export class MobileNetV2Nchw {
Expand All @@ -9,10 +9,8 @@ export class MobileNetV2Nchw {
this.deviceType_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/mobilenetv2_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/mobilenetv2_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/mobilenetv2_nchw/weights/';
this.inputOptions = {
mean: [0.485, 0.456, 0.406],
std: [0.229, 0.224, 0.225],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/mobilenet_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

/* eslint max-len: ["error", {"code": 120}] */

Expand All @@ -11,10 +11,8 @@ export class MobileNetV2Nhwc {
this.deviceType_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/mobilenetv2_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/mobilenetv2_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/mobilenetv2_nhwc/weights/';
this.inputOptions = {
mean: [127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/resnet50v2_nchw.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy} from '../common/utils.js';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';

// ResNet50 V2 model with 'nchw' input layout
export class ResNet50V2Nchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/resnet50v2_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/resnet50v2_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/resnet50v2_nchw/weights/';
this.inputOptions = {
mean: [0.485, 0.456, 0.406],
std: [0.229, 0.224, 0.225],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/resnet50v2_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

const autoPad = 'same-upper';
const strides = [2, 2];
Expand All @@ -12,10 +12,8 @@ export class ResNet50V2Nhwc {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/resnet50v2_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/resnet50v2_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/resnet50v2_nhwc/weights/';
this.inputOptions = {
mean: [127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/squeezenet_nchw.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy} from '../common/utils.js';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';

// SqueezeNet 1.1 model with 'nchw' input layout
export class SqueezeNetNchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/squeezenet1.1_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/squeezenet1.1_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/squeezenet1.1_nchw/weights/';
this.inputOptions = {
mean: [0.485, 0.456, 0.406],
std: [0.229, 0.224, 0.225],
Expand Down
8 changes: 3 additions & 5 deletions image_classification/squeezenet_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// SqueezeNet 1.0 model with 'nhwc' layout
export class SqueezeNetNhwc {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/squeezenet1.0_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/squeezenet1.0_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/squeezenet1.0_nhwc/weights/';
this.inputOptions = {
mean: [127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5],
Expand Down
6 changes: 2 additions & 4 deletions lenet/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ async function main() {
await utils.setBackend(backend, deviceType);
drawNextDigitFromMnist();
const pen = new Pen(visualCanvas);
let weightUrl = '../test-data/models/lenet_nchw/weights/lenet.bin';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
weightUrl = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/lenet_nchw/weights/lenet.bin';
}
const weightUrl = utils.weightsOrigin() +
'/test-data/models/lenet_nchw/weights/lenet.bin';
const lenet = new LeNet(weightUrl);
const [numRuns, powerPreference, numThreads] = utils.getUrlParams();
try {
Expand Down
8 changes: 3 additions & 5 deletions nsnet2/denoiser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NSNet2} from './nsnet2.js';
import * as featurelib from './featurelib.js';
import {sizeOfShape, getUrlParams} from '../common/utils.js';
import {sizeOfShape, getUrlParams, weightsOrigin} from '../common/utils.js';

export class Denoiser {
constructor(batchSize, frames, sampleRate) {
Expand Down Expand Up @@ -29,10 +29,8 @@ export class Denoiser {
return new Promise((resolve, reject) => {
this.log(' - Loading weights... ');
const start = performance.now();
let weightsUrl = '../test-data/models/nsnet2/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
weightsUrl = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/nsnet2/weights/';
}
const weightsUrl = weightsOrigin() +
'/test-data/models/nsnet2/weights/';
const powerPreference = getUrlParams()[1];
const contextOptions = {deviceType};
if (powerPreference) {
Expand Down
14 changes: 5 additions & 9 deletions object_detection/ssd_mobilenetv1_nchw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// SSD MobileNet V1 model with 'nchw' layout, trained on the COCO dataset.
export class SsdMobilenetV1Nchw {
Expand All @@ -10,15 +10,11 @@ export class SsdMobilenetV1Nchw {
this.model_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/ssd_mobilenetv1_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv1_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/ssd_mobilenetv1_nchw/weights/';
// Shares the same bias files with 'nhwc' layout
this.biasUrl_ = '../test-data/models/ssd_mobilenetv1_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.biasUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv1_nhwc/weights/';
}
this.biasUrl_ = weightsOrigin() +
'/test-data/models/ssd_mobilenetv1_nhwc/weights/';
this.inputOptions = {
inputLayout: 'nchw',
labelUrl: './labels/coco_classes.txt',
Expand Down
8 changes: 3 additions & 5 deletions object_detection/ssd_mobilenetv1_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// SSD MobileNet V1 model with 'nhwc' layout, trained on the COCO dataset.
export class SsdMobilenetV1Nhwc {
Expand All @@ -10,10 +10,8 @@ export class SsdMobilenetV1Nhwc {
this.model_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/ssd_mobilenetv1_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv1_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/ssd_mobilenetv1_nhwc/weights/';
this.inputOptions = {
inputLayout: 'nhwc',
labelUrl: './labels/coco_classes.txt',
Expand Down
8 changes: 3 additions & 5 deletions object_detection/tiny_yolov2_nchw.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// Tiny Yolo V2 model with 'nchw' layout, trained on the Pascal VOC dataset.
export class TinyYoloV2Nchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/tiny_yolov2_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/tiny_yolov2_nchw/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/tiny_yolov2_nchw/weights/';
this.inputOptions = {
inputLayout: 'nchw',
labelUrl: './labels/pascal_classes.txt',
Expand Down
8 changes: 3 additions & 5 deletions object_detection/tiny_yolov2_nhwc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js';

// Tiny Yolo V2 model with 'nhwc' layout, trained on the Pascal VOC dataset.
export class TinyYoloV2Nhwc {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/tiny_yolov2_nhwc/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/tiny_yolov2_nhwc/weights/';
}
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/tiny_yolov2_nhwc/weights/';
this.inputOptions = {
inputLayout: 'nhwc',
labelUrl: './labels/pascal_classes.txt',
Expand Down
Loading

0 comments on commit 994351d

Please sign in to comment.