gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as tf from '../../dist/tfjs.esm';
 
import { ConvParams, extractWeightsFactory, ExtractWeightsFunction, ParamMapping } from '../common/index';
import { isFloat } from '../utils/index';
import { ConvLayerParams, NetParams, ResidualLayerParams, ScaleLayerParams } from './types';
 
function extractorsFactory(extractWeights: ExtractWeightsFunction, paramMappings: ParamMapping[]) {
  function extractFilterValues(numFilterValues: number, numFilters: number, filterSize: number): tf.Tensor4D {
    const weights = extractWeights(numFilterValues);
    const depth = weights.length / (numFilters * filterSize * filterSize);
 
    if (isFloat(depth)) {
      throw new Error(`depth has to be an integer: ${depth}, weights.length: ${weights.length}, numFilters: ${numFilters}, filterSize: ${filterSize}`);
    }
 
    return tf.tidy(
      () => tf.transpose(
        tf.tensor4d(weights, [numFilters, depth, filterSize, filterSize]),
        [2, 3, 1, 0],
      ),
    );
  }
 
  function extractConvParams(
    numFilterValues: number,
    numFilters: number,
    filterSize: number,
    mappedPrefix: string,
  ): ConvParams {
    const filters = extractFilterValues(numFilterValues, numFilters, filterSize);
    const bias = tf.tensor1d(extractWeights(numFilters));
 
    paramMappings.push(
      { paramPath: `${mappedPrefix}/filters` },
      { paramPath: `${mappedPrefix}/bias` },
    );
 
    return { filters, bias };
  }
 
  function extractScaleLayerParams(numWeights: number, mappedPrefix: string): ScaleLayerParams {
    const weights = tf.tensor1d(extractWeights(numWeights));
    const biases = tf.tensor1d(extractWeights(numWeights));
 
    paramMappings.push(
      { paramPath: `${mappedPrefix}/weights` },
      { paramPath: `${mappedPrefix}/biases` },
    );
 
    return {
      weights,
      biases,
    };
  }
 
  function extractConvLayerParams(
    numFilterValues: number,
    numFilters: number,
    filterSize: number,
    mappedPrefix: string,
  ): ConvLayerParams {
    const conv = extractConvParams(numFilterValues, numFilters, filterSize, `${mappedPrefix}/conv`);
    const scale = extractScaleLayerParams(numFilters, `${mappedPrefix}/scale`);
 
    return { conv, scale };
  }
 
  function extractResidualLayerParams(
    numFilterValues: number,
    numFilters: number,
    filterSize: number,
    mappedPrefix: string,
    isDown = false,
  ): ResidualLayerParams {
    const conv1 = extractConvLayerParams((isDown ? 0.5 : 1) * numFilterValues, numFilters, filterSize, `${mappedPrefix}/conv1`);
    const conv2 = extractConvLayerParams(numFilterValues, numFilters, filterSize, `${mappedPrefix}/conv2`);
 
    return { conv1, conv2 };
  }
 
  return {
    extractConvLayerParams,
    extractResidualLayerParams,
  };
}
 
export function extractParams(weights: Float32Array): { params: NetParams, paramMappings: ParamMapping[] } {
  const {
    extractWeights,
    getRemainingWeights,
  } = extractWeightsFactory(weights);
 
  const paramMappings: ParamMapping[] = [];
 
  const {
    extractConvLayerParams,
    extractResidualLayerParams,
  } = extractorsFactory(extractWeights, paramMappings);
 
  const conv32_down = extractConvLayerParams(4704, 32, 7, 'conv32_down');
  const conv32_1 = extractResidualLayerParams(9216, 32, 3, 'conv32_1');
  const conv32_2 = extractResidualLayerParams(9216, 32, 3, 'conv32_2');
  const conv32_3 = extractResidualLayerParams(9216, 32, 3, 'conv32_3');
 
  const conv64_down = extractResidualLayerParams(36864, 64, 3, 'conv64_down', true);
  const conv64_1 = extractResidualLayerParams(36864, 64, 3, 'conv64_1');
  const conv64_2 = extractResidualLayerParams(36864, 64, 3, 'conv64_2');
  const conv64_3 = extractResidualLayerParams(36864, 64, 3, 'conv64_3');
 
  const conv128_down = extractResidualLayerParams(147456, 128, 3, 'conv128_down', true);
  const conv128_1 = extractResidualLayerParams(147456, 128, 3, 'conv128_1');
  const conv128_2 = extractResidualLayerParams(147456, 128, 3, 'conv128_2');
 
  const conv256_down = extractResidualLayerParams(589824, 256, 3, 'conv256_down', true);
  const conv256_1 = extractResidualLayerParams(589824, 256, 3, 'conv256_1');
  const conv256_2 = extractResidualLayerParams(589824, 256, 3, 'conv256_2');
  const conv256_down_out = extractResidualLayerParams(589824, 256, 3, 'conv256_down_out');
 
  const fc = tf.tidy(
    () => tf.transpose(tf.tensor2d(extractWeights(256 * 128), [128, 256]), [1, 0]),
  );
  paramMappings.push({ paramPath: 'fc' });
 
  if (getRemainingWeights().length !== 0) {
    throw new Error(`weights remaing after extract: ${getRemainingWeights().length}`);
  }
 
  const params = {
    conv32_down,
    conv32_1,
    conv32_2,
    conv32_3,
    conv64_down,
    conv64_1,
    conv64_2,
    conv64_3,
    conv128_down,
    conv128_1,
    conv128_2,
    conv256_down,
    conv256_1,
    conv256_2,
    conv256_down_out,
    fc,
  };
 
  return { params, paramMappings };
}