Spaces:
Running
Running
File size: 13,490 Bytes
1f2f6bf 6864261 1f2f6bf bc03848 1f2f6bf bc03848 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf e9d96d4 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf bc03848 1f2f6bf bc03848 1f2f6bf 6864261 1f2f6bf 6864261 1f2f6bf bc03848 1f2f6bf e9d96d4 1f2f6bf | 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | import { useMemo, useState } from "react";
import type { RunInfo } from "./train.ts";
import { Button, Card } from "@elvis/ui";
type NumericArray = ArrayLike<number>;
type Shape4D = [number, number, number, number];
interface InputLayer {
type: "input";
output: NumericArray;
shape: Shape4D;
}
interface Conv2dLayer {
type: "conv2d";
output: NumericArray;
kernels: NumericArray;
outputShape: Shape4D;
kernelShape: [number, number, number, number];
stride: number;
padding: number | "same" | "valid";
activationType?: string;
}
interface MaxPoolLayer {
type: "maxpool";
output: NumericArray;
shape: Shape4D;
size: number;
stride: number;
}
interface FlattenLayer {
type: "flatten";
}
interface DenseLayer {
type: "dense";
inputUnits: number;
outputUnits: number;
activationType?: string;
}
interface OutputLayer {
type: "output";
output: NumericArray;
}
type LayerInfo =
| InputLayer
| Conv2dLayer
| MaxPoolLayer
| FlattenLayer
| DenseLayer
| OutputLayer;
interface InfoViewerProps {
info?: RunInfo[];
onSampleIndexChange: () => void;
}
interface InputViewerProps {
output: NumericArray;
shape: Shape4D;
onSampleIndexChange: () => void;
}
interface Conv2dLayerViewerProps {
layerIdx: number;
stride: number;
padding: number | "same" | "valid";
activationType?: string;
kernels: NumericArray;
output: NumericArray;
kernelShape: [number, number, number, number];
outputShape: Shape4D;
}
interface MaxPoolLayerViewerProps {
layerIdx: number;
stride: number;
size: number;
output: NumericArray;
shape: Shape4D;
}
interface OutputLayerViewerProps {
probs: NumericArray;
}
interface DenseLayerViewerProps {
inputUnits: number;
outputUnits: number;
activationType?: string;
}
function asLayerInfo(layer: RunInfo): LayerInfo | null {
if (typeof layer.type !== "string") {
return null;
}
switch (layer.type) {
case "input":
case "conv2d":
case "maxpool":
case "flatten":
case "dense":
case "output":
return layer as unknown as LayerInfo;
default:
return null;
}
}
function extractImage(output: NumericArray, h: number, w: number, cCount: number): ImageData {
const buffer = new Uint8ClampedArray(h * w * 4);
for (let i = 0; i < h; ++i) {
for (let j = 0; j < w; ++j) {
for (let c = 0; c < cCount; ++c) {
const val = output[i * w * cCount + j * cCount + c];
buffer[(i * w + j) * 4 + c] = val * 255;
}
for (let c = cCount; c < 3; ++c) {
buffer[(i * w + j) * 4 + c] = buffer[(i * w + j) * 4 + (cCount - 1)];
}
buffer[(i * w + j) * 4 + 3] = 255;
}
}
return new ImageData(buffer, w, h);
}
function extractKernels(
kernels: NumericArray,
selectedOutputChannel: number,
kh: number,
kw: number,
inC: number,
outC: number,
): ImageData[] {
let minVal = Infinity;
let maxVal = -Infinity;
for (let i = 0; i < kernels.length; ++i) {
const val = kernels[i];
if (val < minVal) minVal = val;
if (val > maxVal) maxVal = val;
}
const kernelImgDatas: ImageData[] = [];
for (let ic = 0; ic < inC; ++ic) {
const buffer = new Uint8ClampedArray(kh * kw * 4);
for (let i = 0; i < kh; ++i) {
for (let j = 0; j < kw; ++j) {
const val = kernels[i * kw * inC * outC + j * inC * outC + ic * outC + selectedOutputChannel];
const normVal = (val - minVal) / (maxVal - minVal + 1e-8);
const pixel = Math.round(normVal * 255);
buffer[(i * kw + j) * 4 + 0] = pixel;
buffer[(i * kw + j) * 4 + 1] = pixel;
buffer[(i * kw + j) * 4 + 2] = pixel;
buffer[(i * kw + j) * 4 + 3] = 255;
}
}
kernelImgDatas.push(new ImageData(buffer, kw, kh));
}
return kernelImgDatas;
}
function extractActivationMaps(activations: NumericArray, h: number, w: number, cCount: number): ImageData[] {
let minVal = Infinity;
let maxVal = -Infinity;
for (let i = 0; i < activations.length; ++i) {
const val = activations[i];
if (val < minVal) minVal = val;
if (val > maxVal) maxVal = val;
}
const activationImgDatas: ImageData[] = [];
for (let c = 0; c < cCount; ++c) {
const buffer = new Uint8ClampedArray(h * w * 4);
for (let i = 0; i < h; ++i) {
for (let j = 0; j < w; ++j) {
const val = activations[i * w * cCount + j * cCount + c];
const normVal = (val - minVal) / (maxVal - minVal + 1e-8);
const pixel = Math.round(normVal * 255);
buffer[(i * w + j) * 4 + 0] = pixel;
buffer[(i * w + j) * 4 + 1] = pixel;
buffer[(i * w + j) * 4 + 2] = pixel;
buffer[(i * w + j) * 4 + 3] = 255;
}
}
activationImgDatas.push(new ImageData(buffer, w, h));
}
return activationImgDatas;
}
function imgDataToSrc(imgData: ImageData): string {
const canvas = document.createElement("canvas");
canvas.width = imgData.width;
canvas.height = imgData.height;
const ctx = canvas.getContext("2d");
if (!ctx) return "";
ctx.putImageData(imgData, 0, 0);
return canvas.toDataURL();
}
function InputViewer({ output, shape, onSampleIndexChange }: InputViewerProps) {
const [, h, w, cCount] = shape;
const imgData = useMemo(() => extractImage(output, h, w, cCount), [output, h, w, cCount]);
const imgSrc = useMemo(() => imgDataToSrc(imgData), [imgData]);
return (
<Card>
<h3 className="text-lg font-semibold text-slate-900">Input Layer</h3>
<div className="mt-2 text-sm text-slate-700">
<strong>Input size:</strong> {imgData.width} x {imgData.height}
</div>
<h4 className="mt-3 text-sm font-medium text-slate-800">Sample input</h4>
<div className="mt-2 flex flex-wrap items-center gap-3">
{imgSrc && (
<img
src={imgSrc}
alt="Input sample"
className="h-24 w-24 rounded border border-slate-200 object-contain"
/>
)}
<Button label="New Sample" onClick={onSampleIndexChange} />
</div>
</Card>
);
}
function Conv2dLayerViewer({
layerIdx,
stride,
padding,
activationType,
kernels,
output,
kernelShape,
outputShape,
}: Conv2dLayerViewerProps) {
const [selectedChannel, setSelectedChannel] = useState<number | null>(null);
const [kh, kw, inC, outC] = kernelShape;
const [, h, w, cCount] = outputShape;
const kernelImgDatas = useMemo(() => {
if (selectedChannel == null) return null;
return extractKernels(kernels, selectedChannel, kh, kw, inC, outC);
}, [kernels, selectedChannel, kh, kw, inC, outC]);
const kernelSrcs = useMemo(() => kernelImgDatas?.map(imgDataToSrc) ?? null, [kernelImgDatas]);
const activations = useMemo(
() => extractActivationMaps(output, h, w, cCount),
[output, h, w, cCount],
);
const activationSrcs = useMemo(() => activations.map(imgDataToSrc), [activations]);
return (
<Card>
<h3 className="text-lg font-semibold text-slate-900">Convolution Layer</h3>
<div className="mt-2 grid grid-cols-2 gap-2 text-sm text-slate-700">
<div>
<strong>Kernel Size:</strong> {kh} x {kw}
</div>
<div>
<strong>Stride:</strong> {stride}
</div>
<div>
<strong>Padding:</strong> {padding}
</div>
<div>
<strong>Activation:</strong> {activationType ?? "none"}
</div>
<div>
<strong>Output channels:</strong> {cCount}
</div>
</div>
{selectedChannel != null && kernelSrcs && (
<>
<h4 className="mt-3 text-sm font-medium text-slate-800">
Kernel for output {selectedChannel} (min-max normalized)
</h4>
<div className="mt-2 grid grid-cols-4 gap-2 sm:grid-cols-6 md:grid-cols-8">
{kernelSrcs.map((src, idx) => (
<img
key={`${layerIdx}-${idx}-kernel`}
src={src}
alt={`Kernel ${idx}`}
className="h-24 w-24 rounded object-contain"
/>
))}
</div>
</>
)}
<h4 className="mt-3 text-sm font-medium text-slate-800">Activation Maps (min-max normalized)</h4>
<div className="mt-2 grid grid-cols-4 gap-2 sm:grid-cols-6 md:grid-cols-8">
{activationSrcs.map((src, idx) => (
<img
key={`${layerIdx}-${idx}-activation`}
src={src}
alt={`Activation Map ${idx}`}
onClick={() => setSelectedChannel(selectedChannel === idx ? null : idx)}
className={`h-24 w-24 cursor-pointer rounded border object-contain ${
selectedChannel === idx ? "border-lime-500 ring-2 ring-lime-300" : "border-slate-200"
}`}
/>
))}
</div>
</Card>
);
}
function MaxPoolLayerViewer({ layerIdx, stride, size, output, shape }: MaxPoolLayerViewerProps) {
const [, h, w, cCount] = shape;
const activations = useMemo(() => extractActivationMaps(output, h, w, cCount), [output, h, w, cCount]);
const activationSrcs = useMemo(() => activations.map(imgDataToSrc), [activations]);
return (
<Card>
<h3 className="text-lg font-semibold text-slate-900">MaxPool Layer</h3>
<div className="mt-2 grid grid-cols-2 gap-2 text-sm text-slate-700">
<div>
<strong>Pool Size:</strong> {size} x {size}
</div>
<div>
<strong>Stride:</strong> {stride}
</div>
</div>
<h4 className="mt-3 text-sm font-medium text-slate-800">Outputs (min-max normalized)</h4>
<div className="mt-2 grid grid-cols-4 gap-2 sm:grid-cols-6 md:grid-cols-8">
{activationSrcs.map((src, idx) => (
<img
key={`${layerIdx}-${idx}-output`}
src={src}
alt={`Output ${idx}`}
className="h-24 w-24 rounded border border-slate-200 object-contain"
/>
))}
</div>
</Card>
);
}
function OutputLayerViewer({ probs }: OutputLayerViewerProps) {
const numClasses = probs.length;
return (
<Card>
<h3 className="text-lg font-semibold text-slate-900">Output Layer (softmax)</h3>
<div className="mt-2 text-sm text-slate-700">
<strong>Number of Classes:</strong> {numClasses}
</div>
<h4 className="mt-3 text-sm font-medium text-slate-800">Class Probabilities</h4>
<div className="mt-2 grid grid-cols-2 gap-2 text-sm sm:grid-cols-3">
{Array.from({ length: numClasses }).map((_, i) => (
<div key={i} className="rounded border border-slate-200 bg-slate-50 px-2 py-1">
<strong>Class {i}:</strong> {Number(probs[i]).toFixed(2)}
</div>
))}
</div>
</Card>
);
}
function DenseLayerViewer({ inputUnits, outputUnits, activationType }: DenseLayerViewerProps) {
return (
<Card>
<h3 className="text-lg font-semibold text-slate-900">Dense Layer</h3>
<div className="mt-2 grid grid-cols-2 gap-2 text-sm text-slate-700">
<div>
<strong>Input Units:</strong> {inputUnits}
</div>
<div>
<strong>Output Units:</strong> {outputUnits}
</div>
<div>
<strong>Activation:</strong> {activationType ?? "none"}
</div>
</div>
</Card>
);
}
export default function InfoViewer({ info, onSampleIndexChange }: InfoViewerProps) {
const layers = useMemo(() => (info ?? []).map(asLayerInfo).filter((v): v is LayerInfo => v !== null), [info]);
function renderLayer(layer: LayerInfo, idx: number) {
switch (layer.type) {
case "input":
return (
<InputViewer
key={idx}
output={layer.output}
shape={layer.shape}
onSampleIndexChange={onSampleIndexChange}
/>
);
case "conv2d":
return (
<Conv2dLayerViewer
key={idx}
layerIdx={idx}
stride={layer.stride}
padding={layer.padding}
activationType={layer.activationType}
kernels={layer.kernels}
output={layer.output}
kernelShape={layer.kernelShape}
outputShape={layer.outputShape}
/>
);
case "maxpool":
return (
<MaxPoolLayerViewer
key={idx}
layerIdx={idx}
stride={layer.stride}
size={layer.size}
output={layer.output}
shape={layer.shape}
/>
);
case "flatten":
return null;
case "dense":
return (
<DenseLayerViewer
key={idx}
inputUnits={layer.inputUnits}
outputUnits={layer.outputUnits}
activationType={layer.activationType}
/>
);
case "output":
return null;
default:
return (
<Card key={idx} className="p-4 text-sm text-slate-700 shadow-sm">
Unknown Layer Type
</Card>
);
}
}
const lastLayer = layers.length > 0 ? layers[layers.length - 1] : null;
const outputLayer = lastLayer?.type === "output" ? lastLayer : null;
// the second last layer in layers is the dense layer for the output - don't show it.
const bodyLayers = outputLayer ? layers.slice(0, -2) : layers;
return (
<div className="flex flex-col gap-4 min-h-0">
{bodyLayers.map((layer, idx) => renderLayer(layer, idx))}
{outputLayer && <OutputLayerViewer probs={outputLayer.output} />}
</div>
);
}
|