-
Notifications
You must be signed in to change notification settings - Fork 74
/
gans.ts
79 lines (64 loc) · 1.81 KB
/
gans.ts
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
import type { Tensor, Tensor4D, } from '@tensorflow/tfjs-core';
import type { ModelDefinition, ModelDefinitionFn, TF, } from '@upscalerjs/core';
import { NAME, VERSION, } from './constants.generated';
const modelDefinition: ModelDefinitionFn = (tf: TF) => {
const Layer = tf.layers.Layer;
const SCALE = 4;
const BETA = 0.2;
type Inputs = Tensor4D | Tensor4D[];
const isTensorArray = (inputs: Inputs): inputs is Tensor4D[] => {
return Array.isArray(inputs);
};
const getInput = (inputs: Inputs): Tensor4D => {
if (isTensorArray(inputs)) {
return inputs[0];
}
return inputs;
};
class MultiplyBeta extends Layer {
beta: number;
constructor() {
super({});
this.beta = BETA;
}
call(inputs: Inputs) {
return tf.mul(getInput(inputs), this.beta);
}
static className = 'MultiplyBeta';
}
class PixelShuffle extends Layer {
scale: number;
constructor() {
super({});
this.scale = SCALE;
}
computeOutputShape(inputShape: number[]) {
return [inputShape[0], inputShape[1], inputShape[2], 3,];
}
call(inputs: Inputs) {
return tf.depthToSpace(getInput(inputs), this.scale, 'NHWC');
}
static className = 'PixelShuffle';
}
const modelDefinition: ModelDefinition = {
scale: SCALE,
channels: 3,
path: 'models/gans/model.json',
packageInformation: {
name: NAME,
version: VERSION,
},
meta: {
dataset: 'div2k',
},
preprocess: (image: Tensor) => tf.mul(image, 1 / 255),
postprocess: (output: Tensor) => tf.tidy(() => {
const clippedValue = (output).clipByValue(0, 1);
output.dispose();
return tf.mul(clippedValue, 255);
}),
customLayers: [MultiplyBeta, PixelShuffle,],
};
return modelDefinition;
};
export default modelDefinition;