-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.html
143 lines (98 loc) · 3.7 KB
/
main.html
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
<html>
<head>
</head>
<body>
<h2> yo </h2>
<label id='top-label'></label>
<!-- <div>
<img src="./examples/cardiomegaly.jpeg" height=224 width=224></img>
</div> -->
<div id='userimg'></div>
<input type="file" id="inputFile" name="inputFileName">
<script src="libs/tf.min.js"></script>
<script src="./preprocess.js"></script>
<script src="./drawTable.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
async function loadMobileNetModel() {
const startTime = performance.now();
//let downloadStatus = setInterval(display_size_data, 100);
const MODEL_PATH = 'https://mlmed.github.io/tools/xray/models/chestxnet2';
let mobilenet = await tf.loadFrozenModel(MODEL_PATH + "/tensorflowjs_model.pb", MODEL_PATH + "/weights_manifest.json");
console.log("First Model loaded " + Math.floor(performance.now() - startTime) + "ms");
return mobilenet;
}
const LABELS = ['Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia'
];
const OP_POINT = [0.45879191, 0.20330566, 0.34361544, 0.30163303, 0.50299263,
0.36888129, 0.29530331, 0.6088959, 0.46361208, 0.17098247,
0.31575406, 0.51793754, 0.49182123, 0.59332716
];
const IMAGE_SIZE = 224;
const RECSCORE_THRESH = 0.5;
const OODSCORE_THRESH = 1000;
let bub = testTf();
d3.select("#top-label")
.text(bub.toString());
fileElement = document.getElementById('inputFile');
fileElement.addEventListener('change', evt => {
let files = evt.target.files; // Display thumbnails & issue call to predict each image.
if (evt.target.files.length < 1) {
return;
}
let fileInfo = files[0];
let filename = fileInfo.name;
if (!fileInfo.type.match('image.*')) {
console.log('error: ' + filename + ' not an image file');
return;
}
console.log("info: reading " + filename);
let reader = new FileReader();
reader.onload = e => {
let img = document.createElement('img');
img.src = e.target.result;
// img.width = IMAGE_SIZE;
// img.height = IMAGE_SIZE;
let userImgDiv = document.getElementById('userimg');
userImgDiv.appendChild(img);
img.onload = () => predict(img, false, filename);
};
reader.readAsDataURL(fileInfo);
});
function status(msg) {
console.log(msg);
}
let batched
let grads
let currentpred
async function predict(imgElement, isInitialRun, name) {
try {
const startTime = performance.now();
await predict_real(imgElement, isInitialRun, name);
const totalTime = performance.now() - startTime;
status(`Done in ${Math.floor(totalTime)}ms`);
} catch (err) {
console.log("error: exception during predict_real");
}
}
async function predict_real(imgElement, isInitialRun, name) {
status('Predicting...');
cropImg = preprocess(imgElement);
batched = cropImg.reshape([1, 1, IMAGE_SIZE, IMAGE_SIZE]).tile([1, 3, 1, 1])
status('Computing Reconstruction...');
let mobilenet = await loadMobileNetModel();
status('Predicting disease...');
// why was this there? Why doesn't it work?
//await sleep(100)
output = tf.tidy(() => {
return mobilenet.execute(batched, ["Sigmoid"])
});
logits = await output.data();
//console.log("Computed logits and grad " + Math.floor(performance.now() - startTime) + "ms");
console.log("logits=" + logits);
buildTable(LABELS, logits, null);
}
</script>
</body>
</html>