From bd9351ac9a656eaa3160624973a184291c757ffb Mon Sep 17 00:00:00 2001 From: Belem Zhang Date: Mon, 18 Mar 2024 14:59:04 +0800 Subject: [PATCH] refactoring origin of weights --- common/utils.js | 8 ++++++++ face_recognition/facenet_nchw.js | 8 +++----- face_recognition/facenet_nhwc.js | 8 +++----- facial_landmark_detection/face_landmark_nchw.js | 8 +++----- facial_landmark_detection/face_landmark_nhwc.js | 8 +++----- .../ssd_mobilenetv2_face_nchw.js | 8 +++----- .../ssd_mobilenetv2_face_nhwc.js | 8 +++----- image_classification/mobilenet_nchw.js | 8 +++----- image_classification/mobilenet_nhwc.js | 8 +++----- image_classification/resnet50v2_nchw.js | 8 +++----- image_classification/resnet50v2_nhwc.js | 8 +++----- image_classification/squeezenet_nchw.js | 8 +++----- image_classification/squeezenet_nhwc.js | 8 +++----- lenet/main.js | 6 ++---- nsnet2/denoiser.js | 8 +++----- object_detection/ssd_mobilenetv1_nchw.js | 14 +++++--------- object_detection/ssd_mobilenetv1_nhwc.js | 8 +++----- object_detection/tiny_yolov2_nchw.js | 8 +++----- object_detection/tiny_yolov2_nhwc.js | 8 +++----- rnnoise/main.js | 6 ++---- semantic_segmentation/deeplabv3_mnv2_nchw.js | 14 +++++--------- semantic_segmentation/deeplabv3_mnv2_nhwc.js | 8 +++----- style_transfer/fast_style_transfer_net.js | 8 +++----- 23 files changed, 76 insertions(+), 116 deletions(-) diff --git a/common/utils.js b/common/utils.js index b26061ed..135012d1 100644 --- a/common/utils.js +++ b/common/utils.js @@ -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; diff --git a/face_recognition/facenet_nchw.js b/face_recognition/facenet_nchw.js index 726648d5..05086c4f 100644 --- a/face_recognition/facenet_nchw.js +++ b/face_recognition/facenet_nchw.js @@ -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'; @@ -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], diff --git a/face_recognition/facenet_nhwc.js b/face_recognition/facenet_nhwc.js index 8e420f15..4155e645 100644 --- a/face_recognition/facenet_nhwc.js +++ b/face_recognition/facenet_nhwc.js @@ -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'; @@ -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], diff --git a/facial_landmark_detection/face_landmark_nchw.js b/facial_landmark_detection/face_landmark_nchw.js index 5c71acd7..bb347193 100644 --- a/facial_landmark_detection/face_landmark_nchw.js +++ b/facial_landmark_detection/face_landmark_nchw.js @@ -1,6 +1,6 @@ 'use strict'; -import {buildConstantByNpy} from '../common/utils.js'; +import {buildConstantByNpy, weightsOrigin} from '../common/utils.js'; // SimpleCNN model with 'nchw' layout. export class FaceLandmarkNchw { @@ -8,10 +8,8 @@ export class FaceLandmarkNchw { 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], diff --git a/facial_landmark_detection/face_landmark_nhwc.js b/facial_landmark_detection/face_landmark_nhwc.js index 2b076b61..da6575a0 100644 --- a/facial_landmark_detection/face_landmark_nhwc.js +++ b/facial_landmark_detection/face_landmark_nhwc.js @@ -1,6 +1,6 @@ 'use strict'; -import {buildConstantByNpy} from '../common/utils.js'; +import {buildConstantByNpy, weightsOrigin} from '../common/utils.js'; // SimpleCNN model with 'nhwc' layout. export class FaceLandmarkNhwc { @@ -8,10 +8,8 @@ export class FaceLandmarkNhwc { 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], diff --git a/facial_landmark_detection/ssd_mobilenetv2_face_nchw.js b/facial_landmark_detection/ssd_mobilenetv2_face_nchw.js index ed59a43d..c2024686 100644 --- a/facial_landmark_detection/ssd_mobilenetv2_face_nchw.js +++ b/facial_landmark_detection/ssd_mobilenetv2_face_nchw.js @@ -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 { @@ -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], diff --git a/facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js b/facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js index e3f4c990..c625ef29 100644 --- a/facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js +++ b/facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js @@ -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 { @@ -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], diff --git a/image_classification/mobilenet_nchw.js b/image_classification/mobilenet_nchw.js index 0194c195..e9c6aaa6 100644 --- a/image_classification/mobilenet_nchw.js +++ b/image_classification/mobilenet_nchw.js @@ -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 { @@ -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], diff --git a/image_classification/mobilenet_nhwc.js b/image_classification/mobilenet_nhwc.js index 55344c1a..64037ea5 100644 --- a/image_classification/mobilenet_nhwc.js +++ b/image_classification/mobilenet_nhwc.js @@ -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}] */ @@ -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], diff --git a/image_classification/resnet50v2_nchw.js b/image_classification/resnet50v2_nchw.js index 64573be9..1569b41f 100644 --- a/image_classification/resnet50v2_nchw.js +++ b/image_classification/resnet50v2_nchw.js @@ -1,6 +1,6 @@ '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 { @@ -8,10 +8,8 @@ export class ResNet50V2Nchw { 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], diff --git a/image_classification/resnet50v2_nhwc.js b/image_classification/resnet50v2_nhwc.js index 2072981b..2cf264ca 100644 --- a/image_classification/resnet50v2_nhwc.js +++ b/image_classification/resnet50v2_nhwc.js @@ -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]; @@ -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], diff --git a/image_classification/squeezenet_nchw.js b/image_classification/squeezenet_nchw.js index 4a7c0545..b49b14d3 100644 --- a/image_classification/squeezenet_nchw.js +++ b/image_classification/squeezenet_nchw.js @@ -1,6 +1,6 @@ '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 { @@ -8,10 +8,8 @@ export class SqueezeNetNchw { 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], diff --git a/image_classification/squeezenet_nhwc.js b/image_classification/squeezenet_nhwc.js index 4f748ad1..21a8e95d 100644 --- a/image_classification/squeezenet_nhwc.js +++ b/image_classification/squeezenet_nhwc.js @@ -1,6 +1,6 @@ '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 { @@ -8,10 +8,8 @@ export class SqueezeNetNhwc { 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], diff --git a/lenet/main.js b/lenet/main.js index c020ef17..c1fe388b 100644 --- a/lenet/main.js +++ b/lenet/main.js @@ -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 { diff --git a/nsnet2/denoiser.js b/nsnet2/denoiser.js index f3ffcedc..8637f90e 100644 --- a/nsnet2/denoiser.js +++ b/nsnet2/denoiser.js @@ -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) { @@ -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) { diff --git a/object_detection/ssd_mobilenetv1_nchw.js b/object_detection/ssd_mobilenetv1_nchw.js index 35f9bd0c..b9df3753 100644 --- a/object_detection/ssd_mobilenetv1_nchw.js +++ b/object_detection/ssd_mobilenetv1_nchw.js @@ -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 { @@ -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', diff --git a/object_detection/ssd_mobilenetv1_nhwc.js b/object_detection/ssd_mobilenetv1_nhwc.js index d872bf8f..ec32ac38 100644 --- a/object_detection/ssd_mobilenetv1_nhwc.js +++ b/object_detection/ssd_mobilenetv1_nhwc.js @@ -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 { @@ -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', diff --git a/object_detection/tiny_yolov2_nchw.js b/object_detection/tiny_yolov2_nchw.js index ac190227..d8af67cb 100644 --- a/object_detection/tiny_yolov2_nchw.js +++ b/object_detection/tiny_yolov2_nchw.js @@ -1,6 +1,6 @@ '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 { @@ -8,10 +8,8 @@ export class TinyYoloV2Nchw { 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', diff --git a/object_detection/tiny_yolov2_nhwc.js b/object_detection/tiny_yolov2_nhwc.js index 4509d921..6b7a1b94 100644 --- a/object_detection/tiny_yolov2_nhwc.js +++ b/object_detection/tiny_yolov2_nhwc.js @@ -1,6 +1,6 @@ '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 { @@ -8,10 +8,8 @@ export class TinyYoloV2Nhwc { 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', diff --git a/rnnoise/main.js b/rnnoise/main.js index e04cd458..13c62d5c 100644 --- a/rnnoise/main.js +++ b/rnnoise/main.js @@ -7,10 +7,8 @@ const batchSize = 1; const frames = 100; // Frames is fixed at 100 const frameSize = 480; const gainsSize = 22; -let weightsUrl = '../test-data/models/rnnoise/weights/'; -if (location.hostname.toLowerCase().indexOf('github.io') > -1) { - weightsUrl = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/rnnoise/weights/'; -} +const weightsUrl = utils.weightsOrigin() + + '/test-data/models/rnnoise/weights/'; const rnnoise = new RNNoise(weightsUrl, batchSize, frames); $('#backendBtns .btn').on('change', async () => { diff --git a/semantic_segmentation/deeplabv3_mnv2_nchw.js b/semantic_segmentation/deeplabv3_mnv2_nchw.js index 4053c803..9edd1ea3 100644 --- a/semantic_segmentation/deeplabv3_mnv2_nchw.js +++ b/semantic_segmentation/deeplabv3_mnv2_nchw.js @@ -1,6 +1,6 @@ 'use strict'; -import {buildConstantByNpy} from '../common/utils.js'; +import {buildConstantByNpy, weightsOrigin} from '../common/utils.js'; /* eslint max-len: ["error", {"code": 120}] */ @@ -11,15 +11,11 @@ export class DeepLabV3MNV2Nchw { this.deviceType_ = null; this.builder_ = null; this.graph_ = null; - this.weightsUrl_ = '../test-data/models/deeplabv3_mnv2_nchw/weights/'; - if (location.hostname.toLowerCase().indexOf('github.io') > -1) { - this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/deeplabv3_mnv2_nchw/weights/'; - } + this.weightsUrl_ = weightsOrigin() + + '/test-data/models/deeplabv3_mnv2_nchw/weights/'; // Shares the same bias files with 'nhwc' layout - this.biasUrl_ = '../test-data/models/deeplabv3_mnv2_nhwc/weights/'; - if (location.hostname.toLowerCase().indexOf('github.io') > -1) { - this.biasUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/deeplabv3_mnv2_nhwc/weights/'; - } + this.biasUrl_ = weightsOrigin() + + '/test-data/models/deeplabv3_mnv2_nhwc/weights/'; this.inputOptions = { mean: [127.5, 127.5, 127.5], std: [127.5, 127.5, 127.5], diff --git a/semantic_segmentation/deeplabv3_mnv2_nhwc.js b/semantic_segmentation/deeplabv3_mnv2_nhwc.js index ee7f6c3d..96c71bdf 100644 --- a/semantic_segmentation/deeplabv3_mnv2_nhwc.js +++ b/semantic_segmentation/deeplabv3_mnv2_nhwc.js @@ -1,6 +1,6 @@ 'use strict'; -import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js'; +import {buildConstantByNpy, computePadding2DForAutoPad, weightsOrigin} from '../common/utils.js'; // DeepLab V3 MobileNet V2 model with 'nhwc' input layout export class DeepLabV3MNV2Nhwc { @@ -9,10 +9,8 @@ export class DeepLabV3MNV2Nhwc { this.deviceType_ = null; this.builder_ = null; this.graph_ = null; - this.weightsUrl_ = '../test-data/models/deeplabv3_mnv2_nhwc/weights/'; - if (location.hostname.toLowerCase().indexOf('github.io') > -1) { - this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/deeplabv3_mnv2_nhwc/weights/'; - } + this.weightsUrl_ = weightsOrigin() + + '/test-data/models/deeplabv3_mnv2_nhwc/weights/'; this.inputOptions = { mean: [127.5, 127.5, 127.5], std: [127.5, 127.5, 127.5], diff --git a/style_transfer/fast_style_transfer_net.js b/style_transfer/fast_style_transfer_net.js index 532442f7..05e851ac 100644 --- a/style_transfer/fast_style_transfer_net.js +++ b/style_transfer/fast_style_transfer_net.js @@ -1,6 +1,6 @@ 'use strict'; -import {buildConstantByNpy} from '../common/utils.js'; +import {buildConstantByNpy, weightsOrigin} from '../common/utils.js'; /* eslint max-len: ["error", {"code": 130}] */ @@ -12,10 +12,8 @@ export class FastStyleTransferNet { this.graph_ = null; this.constPow_ = null; this.constAdd_ = null; - this.weightsUrl_ = '../test-data/models/fast_style_transfer_nchw/weights/'; - if (location.hostname.toLowerCase().indexOf('github.io') > -1) { - this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/fast_style_transfer_nchw/weights/'; - } + this.weightsUrl_ = weightsOrigin() + + '/test-data/models/fast_style_transfer_nchw/weights/'; this.inputOptions = { inputDimensions: [1, 3, 540, 540], inputLayout: 'nchw',