Skip to content

Commit ecd7846

Browse files
ppwwyyxxpytorchmergebot
authored andcommitted
Avoid overflow in tensorboard image summary (pytorch#90423)
Fix pytorch#90419 Added some code such that the test will update the expect files when `expecttest.ACCEPT` is True. Pull Request resolved: pytorch#90423 Approved by: https://github.com/soumith
1 parent 1978773 commit ecd7846

7 files changed

+19
-9
lines changed

test/expect/TestTensorBoard.test_image_with_3_channel_batched.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ value {
44
height: 8
55
width: 16
66
colorspace: 3
7-
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\010\010\002\000\000\000\177\024\350\300\000\000\000+IDATx\234cd8\320\360\037\033pww\307*\316\362\343\307\217\037\330$~\374\370\361\037\233\004\013\016\365\377q\211\217H\r\000d\305y\224,\220Z\033\000\000\000\000IEND\256B`\202"
7+
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\010\010\002\000\000\000\177\024\350\300\000\000\000\034IDATx\234cd\370\377\377?\003\t\200\211$\325\014\014\014L$\252\037\231\032\000\355.\004\014i.\207\035\000\000\000\000IEND\256B`\202"
88
}
99
}

test/expect/TestTensorBoard.test_image_with_boxes.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ value {
44
height: 32
55
width: 32
66
colorspace: 3
7-
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000 \000\000\000 \010\002\000\000\000\374\030\355\243\000\000\000sIDATx\234\355\323=\n\300 \014\005\340\027p\250\267p\324\373\332\373\345\020vn\007\367>0\204b\311\233\305/\344G\000\334\236\021Uu\005R\000\377\007\244\224\342\013||\007\2655\330BfP\215\337S`>:{_l\020\335\242\tX6-\000\032r\007G\316\000\2561\226\201\244\252/\005V\357\026\271\003\033\0149\000\232\270\003+\260\301\220\003\240y\000T\221\324V\250_v\320\000\000\000\000IEND\256B`\202"
7+
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000 \000\000\000 \010\002\000\000\000\374\030\355\243\000\000\000CIDATx\234cd\370\377\377?\003\r\001\023MMg```\242\261\371\243\026\214Z@\005\300B@\236\221\221B\013\006\334\007\020@Ai2\364#y\324\202Q\013F-\030\265`\324\202Q\013\206\207\005\0008\302\006@\2475\013\321\000\000\000\000IEND\256B`\202"
88
}
99
}

test/expect/TestTensorBoard.test_image_with_one_channel.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ value {
44
height: 8
55
width: 8
66
colorspace: 3
7-
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\010\000\000\000\010\010\002\000\000\000Km)\334\000\000\000\031IDATx\234cd``\370\217\r0\376\370\361\003\253\004\313\240\224\000\000;\267\273\313%\020=\255\000\000\000\000IEND\256B`\202"
7+
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\010\000\000\000\010\010\002\000\000\000Km)\334\000\000\000\035IDATx\234cd``\370\377\377?\003\006`\302*\312\300\300\300\204Ut\240%\000R\364\006\n\'\250a\364\000\000\000\000IEND\256B`\202"
88
}
99
}

test/expect/TestTensorBoard.test_image_with_one_channel_batched.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ value {
44
height: 8
55
width: 16
66
colorspace: 3
7-
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\010\010\002\000\000\000\177\024\350\300\000\000\000(IDATx\234cd``\370\217\r\034?~\034\2538\313\217\037?~\374\370\201)\201U\020\252\001\253\304\250\006$\000\000\230\346y\315\204l;t\000\000\000\000IEND\256B`\202"
7+
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\010\010\002\000\000\000\177\024\350\300\000\000\000\036IDATx\234cd``\370\377\377?\003\321\200\211$\325\014\014\014L$\251\036\251\032\000\215\270\006\nS2\367\330\000\000\000\000IEND\256B`\202"
88
}
99
}

test/expect/TestTensorBoard.test_image_without_channel.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ value {
44
height: 8
55
width: 8
66
colorspace: 3
7-
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\010\000\000\000\010\010\002\000\000\000Km)\334\000\000\000\031IDATx\234cd``\370\217\r0\376\370\361\003\253\004\313\240\224\000\000;\267\273\313%\020=\255\000\000\000\000IEND\256B`\202"
7+
encoded_image_string: "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\010\000\000\000\010\010\002\000\000\000Km)\334\000\000\000\035IDATx\234cd``\370\377\377?\003\006`\302*\312\300\300\300\204Ut\240%\000R\364\006\n\'\250a\364\000\000\000\000IEND\256B`\202"
88
}
99
}

test/test_tensorboard.py

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import unittest
99
import uuid
10+
import expecttest
1011

1112
TEST_TENSORBOARD = True
1213
try:
@@ -520,6 +521,11 @@ def read_expected_content(function_ptr):
520521
return f.read()
521522

522523
def compare_image_proto(actual_proto, function_ptr):
524+
if expecttest.ACCEPT:
525+
expected_file = get_expected_file(function_ptr)
526+
with open(expected_file, 'w') as f:
527+
f.write(text_format.MessageToString(actual_proto))
528+
return True
523529
expected_str = read_expected_content(function_ptr)
524530
expected_proto = Summary()
525531
text_format.Parse(expected_str, expected_proto)
@@ -537,6 +543,9 @@ def compare_image_proto(actual_proto, function_ptr):
537543
)
538544

539545
def compare_proto(str_to_compare, function_ptr):
546+
if expecttest.ACCEPT:
547+
write_proto(str_to_compare, function_ptr)
548+
return True
540549
expected = read_expected_content(function_ptr)
541550
str_to_compare = str(str_to_compare)
542551
return remove_whitespace(str_to_compare) == remove_whitespace(expected)

torch/utils/tensorboard/summary.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ def image(tag, tensor, rescale=1, dataformats="NCHW"):
433433
channels]` where `channels` is 1, 3, or 4.
434434
'tensor' can either have values in [0, 1] (float32) or [0, 255] (uint8).
435435
The image() function will scale the image values to [0, 255] by applying
436-
a scale factor of either 1 (uint8) or 255 (float32).
436+
a scale factor of either 1 (uint8) or 255 (float32). Out-of-range values
437+
will be clipped.
437438
Returns:
438439
A scalar `Tensor` of type `string`. The serialized `Summary` protocol
439440
buffer.
@@ -443,7 +444,7 @@ def image(tag, tensor, rescale=1, dataformats="NCHW"):
443444
# Do not assume that user passes in values in [0, 255], use data type to detect
444445
scale_factor = _calc_scale_factor(tensor)
445446
tensor = tensor.astype(np.float32)
446-
tensor = (tensor * scale_factor).astype(np.uint8)
447+
tensor = (tensor * scale_factor).clip(0, 255).astype(np.uint8)
447448
image = make_image(tensor, rescale=rescale)
448449
return Summary(value=[Summary.Value(tag=tag, image=image)])
449450

@@ -457,7 +458,7 @@ def image_boxes(
457458
tensor_boxes = make_np(tensor_boxes)
458459
tensor_image = tensor_image.astype(np.float32) * _calc_scale_factor(tensor_image)
459460
image = make_image(
460-
tensor_image.astype(np.uint8), rescale=rescale, rois=tensor_boxes, labels=labels
461+
tensor_image.clip(0, 255).astype(np.uint8), rescale=rescale, rois=tensor_boxes, labels=labels
461462
)
462463
return Summary(value=[Summary.Value(tag=tag, image=image)])
463464

@@ -513,7 +514,7 @@ def video(tag, tensor, fps=4):
513514
# If user passes in uint8, then we don't need to rescale by 255
514515
scale_factor = _calc_scale_factor(tensor)
515516
tensor = tensor.astype(np.float32)
516-
tensor = (tensor * scale_factor).astype(np.uint8)
517+
tensor = (tensor * scale_factor).clip(0, 255).astype(np.uint8)
517518
video = make_video(tensor, fps)
518519
return Summary(value=[Summary.Value(tag=tag, image=video)])
519520

0 commit comments

Comments
 (0)