Spaces:
Running
on
Zero
Running
on
Zero
File size: 27,584 Bytes
2d9a728 |
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
import os
import io
import cv2
import numpy as np
import torch
from torchvision import transforms
import warnings
from decord import VideoReader, cpu
from torch.utils.data import Dataset
from .random_erasing import RandomErasing
from .video_transforms import (
Compose, Resize, CenterCrop, Normalize,
create_random_augment, random_short_side_scale_jitter,
random_crop, random_resized_crop_with_shift, random_resized_crop,
horizontal_flip, random_short_side_scale_jitter, uniform_crop,
)
from .volume_transforms import ClipToTensor
try:
from petrel_client.client import Client
has_client = True
except ImportError:
has_client = False
class SSRawFrameClsDataset(Dataset):
"""Load your own raw frame classification dataset."""
def __init__(self, anno_path, prefix='', split=' ', mode='train', clip_len=8,
crop_size=224, short_side_size=256, new_height=256, new_width=340,
keep_aspect_ratio=True, num_segment=1, num_crop=1, test_num_segment=10,
test_num_crop=3, filename_tmpl='img_{:05}.jpg', args=None):
self.anno_path = anno_path
self.prefix = prefix
self.split = split
self.mode = mode
self.clip_len = clip_len
self.crop_size = crop_size
self.short_side_size = short_side_size
self.new_height = new_height
self.new_width = new_width
self.keep_aspect_ratio = keep_aspect_ratio
self.num_segment = num_segment
self.test_num_segment = test_num_segment
self.num_crop = num_crop
self.test_num_crop = test_num_crop
self.filename_tmpl = filename_tmpl
self.args = args
self.aug = False
self.rand_erase = False
self.client = None
if has_client:
self.client = Client('~/petreloss.conf')
if self.mode in ['train']:
self.aug = True
if self.args.reprob > 0:
self.rand_erase = True
if VideoReader is None:
raise ImportError(
"Unable to import `decord` which is required to read videos.")
import pandas as pd
cleaned = pd.read_csv(self.anno_path, header=None, delimiter=self.split)
self.dataset_samples = list(cleaned.values[:, 0].astype('str'))
self.total_frames = list(cleaned.values[:, 1])
self.label_array = list(cleaned.values[:, -1])
if (mode == 'train'):
pass
elif (mode == 'validation'):
self.data_transform = Compose([
Resize(self.short_side_size,
interpolation='bilinear'),
CenterCrop(size=(self.crop_size,
self.crop_size)),
ClipToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
elif mode == 'test':
self.data_resize = Compose([
Resize(size=(short_side_size),
interpolation='bilinear')
])
self.data_transform = Compose([
ClipToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
self.test_seg = []
self.test_dataset = []
self.test_total_frames = []
self.test_label_array = []
for ck in range(self.test_num_segment):
for cp in range(self.test_num_crop):
for idx in range(len(self.label_array)):
self.test_seg.append((ck, cp))
self.test_dataset.append(self.dataset_samples[idx])
self.test_total_frames.append(self.total_frames[idx])
self.test_label_array.append(self.label_array[idx])
def __getitem__(self, index):
if self.mode == 'train':
args = self.args
scale_t = 1
sample = self.dataset_samples[index]
total_frame = self.total_frames[index]
buffer = self.load_frame(sample,
total_frame,
sample_rate_scale=scale_t) # T H W C
if len(buffer) == 0:
while len(buffer) == 0:
warnings.warn(
"video {} not correctly loaded during training".format(
sample))
index = np.random.randint(self.__len__())
sample = self.dataset_samples[index]
total_frame = self.total_frames[index]
buffer = self.load_frame(sample,
total_frame,
sample_rate_scale=scale_t)
if args.num_sample > 1:
frame_list = []
label_list = []
index_list = []
for _ in range(args.num_sample):
new_frames = self._aug_frame(buffer, args)
label = self.label_array[index]
frame_list.append(new_frames)
label_list.append(label)
index_list.append(index)
return frame_list, label_list, index_list, {}
else:
buffer = self._aug_frame(buffer, args)
return buffer, self.label_array[index], index, {}
elif self.mode == 'validation':
sample = self.dataset_samples[index]
total_frame = self.total_frames[index]
buffer = self.load_frame(sample, total_frame)
if len(buffer) == 0:
while len(buffer) == 0:
warnings.warn(
"video {} not correctly loaded during validation".
format(sample))
index = np.random.randint(self.__len__())
sample = self.dataset_samples[index]
buffer = self.load_frame(sample, total_frame)
buffer = self.data_transform(buffer)
return buffer, self.label_array[index], sample.split(
"/")[-1].split(".")[0]
elif self.mode == 'test':
sample = self.test_dataset[index]
total_frame = self.test_total_frames[index]
chunk_nb, split_nb = self.test_seg[index]
buffer = self.load_frame(sample, total_frame)
while len(buffer) == 0:
warnings.warn("video {}, temporal {}, spatial {} not found during testing".format(\
str(self.test_dataset[index]), chunk_nb, split_nb))
index = np.random.randint(self.__len__())
sample = self.test_dataset[index]
total_frame = self.test_total_frames[index]
chunk_nb, split_nb = self.test_seg[index]
buffer = self.load_frame(sample, total_frame)
buffer = self.data_resize(buffer)
if isinstance(buffer, list):
buffer = np.stack(buffer, 0)
spatial_step = 1.0 * (max(buffer.shape[1], buffer.shape[2]) - self.short_side_size) \
/ (self.test_num_crop - 1)
temporal_start = chunk_nb
spatial_start = int(split_nb * spatial_step)
if buffer.shape[1] >= buffer.shape[2]:
buffer = buffer[temporal_start::self.test_num_segment, \
spatial_start:spatial_start + self.short_side_size, :, :]
else:
buffer = buffer[temporal_start::self.test_num_segment, \
:, spatial_start:spatial_start + self.short_side_size, :]
buffer = self.data_transform(buffer)
return buffer, self.test_label_array[index], sample.split("/")[-1].split(".")[0], \
chunk_nb, split_nb
else:
raise NameError('mode {} unkown'.format(self.mode))
def _aug_frame(
self,
buffer,
args,
):
aug_transform = create_random_augment(
input_size=(self.crop_size, self.crop_size),
auto_augment=args.aa,
interpolation=args.train_interpolation,
)
buffer = [transforms.ToPILImage()(frame) for frame in buffer]
buffer = aug_transform(buffer)
buffer = [transforms.ToTensor()(img) for img in buffer]
buffer = torch.stack(buffer) # T C H W
buffer = buffer.permute(0, 2, 3, 1) # T H W C
# T H W C
buffer = tensor_normalize(buffer, [0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
# T H W C -> C T H W.
buffer = buffer.permute(3, 0, 1, 2)
# Perform data augmentation.
scl, asp = (
[0.08, 1.0],
[0.75, 1.3333],
)
buffer = spatial_sampling(
buffer,
spatial_idx=-1,
min_scale=256,
max_scale=320,
crop_size=self.crop_size,
random_horizontal_flip=False if args.data_set == 'SSV2' else True,
inverse_uniform_sampling=False,
aspect_ratio=asp,
scale=scl,
motion_shift=False)
if self.rand_erase:
erase_transform = RandomErasing(
args.reprob,
mode=args.remode,
max_count=args.recount,
num_splits=args.recount,
device="cpu",
)
buffer = buffer.permute(1, 0, 2, 3)
buffer = erase_transform(buffer)
buffer = buffer.permute(1, 0, 2, 3)
return buffer
def load_frame(self, sample, num_frames, sample_rate_scale=1):
"""Load video content using Decord"""
fname = sample
fname = os.path.join(self.prefix, fname)
if self.mode == 'test':
tick = num_frames / float(self.num_segment)
all_index = []
for t_seg in range(self.test_num_segment):
tmp_index = [
int(t_seg * tick / self.test_num_segment + tick * x)
for x in range(self.num_segment)
]
all_index.extend(tmp_index)
all_index = list(np.sort(np.array(all_index)))
imgs = []
for idx in all_index:
frame_fname = os.path.join(fname, self.filename_tmpl.format(idx + 1))
if "s3://" in fname:
img_bytes = self.client.get(frame_fname)
else:
with open(frame_fname, 'rb') as f:
img_bytes = f.read()
img_np = np.frombuffer(img_bytes, np.uint8)
img = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
imgs.append(img)
buffer = np.array(imgs)
return buffer
# handle temporal segments
average_duration = num_frames // self.num_segment
all_index = []
if average_duration > 0:
if self.mode == 'validation':
all_index = list(
np.multiply(list(range(self.num_segment)),
average_duration) +
np.ones(self.num_segment, dtype=int) *
(average_duration // 2))
else:
all_index = list(
np.multiply(list(range(self.num_segment)),
average_duration) +
np.random.randint(average_duration, size=self.num_segment))
elif num_frames > self.num_segment:
if self.mode == 'validation':
all_index = list(range(self.num_segment))
else:
all_index = list(
np.sort(
np.random.randint(num_frames, size=self.num_segment)))
else:
all_index = [0] * (self.num_segment - num_frames) + list(
range(num_frames))
all_index = list(np.array(all_index))
imgs = []
for idx in all_index:
frame_fname = os.path.join(fname, self.filename_tmpl.format(idx + 1))
if "s3://" in fname:
img_bytes = self.client.get(frame_fname)
else:
with open(frame_fname, 'rb') as f:
img_bytes = f.read()
img_np = np.frombuffer(img_bytes, np.uint8)
img = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
imgs.append(img)
buffer = np.array(imgs)
return buffer
def __len__(self):
if self.mode != 'test':
return len(self.dataset_samples)
else:
return len(self.test_dataset)
class SSVideoClsDataset(Dataset):
"""Load your own video classification dataset."""
def __init__(self, anno_path, prefix='', split=' ', mode='train', clip_len=8,
crop_size=224, short_side_size=256, new_height=256,
new_width=340, keep_aspect_ratio=True, num_segment=1,
num_crop=1, test_num_segment=10, test_num_crop=3, filename_tmpl=None, args=None):
self.anno_path = anno_path
self.prefix = prefix
self.split = split
self.mode = mode
self.clip_len = clip_len
self.crop_size = crop_size
self.short_side_size = short_side_size
self.new_height = new_height
self.new_width = new_width
self.keep_aspect_ratio = keep_aspect_ratio
self.num_segment = num_segment
self.test_num_segment = test_num_segment
self.num_crop = num_crop
self.test_num_crop = test_num_crop
self.args = args
self.aug = False
self.rand_erase = False
self.client = None
if has_client:
self.client = Client('~/petreloss.conf')
if self.mode in ['train']:
self.aug = True
if self.args.reprob > 0:
self.rand_erase = True
if VideoReader is None:
raise ImportError("Unable to import `decord` which is required to read videos.")
import pandas as pd
cleaned = pd.read_csv(self.anno_path, header=None, delimiter=self.split)
self.dataset_samples = list(cleaned.values[:, 0])
self.label_array = list(cleaned.values[:, 1])
if (mode == 'train'):
pass
elif (mode == 'validation'):
self.data_transform = Compose([
Resize(self.short_side_size, interpolation='bilinear'),
CenterCrop(size=(self.crop_size, self.crop_size)),
ClipToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
elif mode == 'test':
self.data_resize = Compose([
Resize(size=(short_side_size), interpolation='bilinear')
])
self.data_transform = Compose([
ClipToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
self.test_seg = []
self.test_dataset = []
self.test_label_array = []
for ck in range(self.test_num_segment):
for cp in range(self.test_num_crop):
for idx in range(len(self.label_array)):
sample_label = self.label_array[idx]
self.test_label_array.append(sample_label)
self.test_dataset.append(self.dataset_samples[idx])
self.test_seg.append((ck, cp))
def __getitem__(self, index):
if self.mode == 'train':
args = self.args
scale_t = 1
sample = self.dataset_samples[index]
buffer = self.loadvideo_decord(sample, sample_rate_scale=scale_t) # T H W C
if len(buffer) == 0:
while len(buffer) == 0:
warnings.warn("video {} not correctly loaded during training".format(sample))
index = np.random.randint(self.__len__())
sample = self.dataset_samples[index]
buffer = self.loadvideo_decord(sample, sample_rate_scale=scale_t)
if args.num_sample > 1:
frame_list = []
label_list = []
index_list = []
for _ in range(args.num_sample):
new_frames = self._aug_frame(buffer, args)
label = self.label_array[index]
frame_list.append(new_frames)
label_list.append(label)
index_list.append(index)
return frame_list, label_list, index_list, {}
else:
buffer = self._aug_frame(buffer, args)
return buffer, self.label_array[index], index, {}
elif self.mode == 'validation':
sample = self.dataset_samples[index]
buffer = self.loadvideo_decord(sample)
if len(buffer) == 0:
while len(buffer) == 0:
warnings.warn("video {} not correctly loaded during validation".format(sample))
index = np.random.randint(self.__len__())
sample = self.dataset_samples[index]
buffer = self.loadvideo_decord(sample)
buffer = self.data_transform(buffer)
return buffer, self.label_array[index], sample.split("/")[-1].split(".")[0]
elif self.mode == 'test':
sample = self.test_dataset[index]
chunk_nb, split_nb = self.test_seg[index]
buffer = self.loadvideo_decord(sample)
while len(buffer) == 0:
warnings.warn("video {}, temporal {}, spatial {} not found during testing".format(\
str(self.test_dataset[index]), chunk_nb, split_nb))
index = np.random.randint(self.__len__())
sample = self.test_dataset[index]
chunk_nb, split_nb = self.test_seg[index]
buffer = self.loadvideo_decord(sample)
buffer = self.data_resize(buffer)
if isinstance(buffer, list):
buffer = np.stack(buffer, 0)
spatial_step = 1.0 * (max(buffer.shape[1], buffer.shape[2]) - self.short_side_size) \
/ (self.test_num_crop - 1)
temporal_start = chunk_nb # 0/1
spatial_start = int(split_nb * spatial_step)
if buffer.shape[1] >= buffer.shape[2]:
buffer = buffer[temporal_start::2, \
spatial_start:spatial_start + self.short_side_size, :, :]
else:
buffer = buffer[temporal_start::2, \
:, spatial_start:spatial_start + self.short_side_size, :]
buffer = self.data_transform(buffer)
return buffer, self.test_label_array[index], sample.split("/")[-1].split(".")[0], \
chunk_nb, split_nb
else:
raise NameError('mode {} unkown'.format(self.mode))
def _aug_frame(
self,
buffer,
args,
):
aug_transform = create_random_augment(
input_size=(self.crop_size, self.crop_size),
auto_augment=args.aa,
interpolation=args.train_interpolation,
)
buffer = [
transforms.ToPILImage()(frame) for frame in buffer
]
buffer = aug_transform(buffer)
buffer = [transforms.ToTensor()(img) for img in buffer]
buffer = torch.stack(buffer) # T C H W
buffer = buffer.permute(0, 2, 3, 1) # T H W C
# T H W C
buffer = tensor_normalize(
buffer, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
)
# T H W C -> C T H W.
buffer = buffer.permute(3, 0, 1, 2)
# Perform data augmentation.
scl, asp = (
[0.08, 1.0],
[0.75, 1.3333],
)
buffer = spatial_sampling(
buffer,
spatial_idx=-1,
min_scale=256,
max_scale=320,
crop_size=self.crop_size,
random_horizontal_flip=False if args.data_set == 'SSV2' else True,
inverse_uniform_sampling=False,
aspect_ratio=asp,
scale=scl,
motion_shift=False
)
if self.rand_erase:
erase_transform = RandomErasing(
args.reprob,
mode=args.remode,
max_count=args.recount,
num_splits=args.recount,
device="cpu",
)
buffer = buffer.permute(1, 0, 2, 3)
buffer = erase_transform(buffer)
buffer = buffer.permute(1, 0, 2, 3)
return buffer
def loadvideo_decord(self, sample, sample_rate_scale=1):
"""Load video content using Decord"""
fname = sample
fname = os.path.join(self.prefix, fname)
try:
if self.keep_aspect_ratio:
if "s3://" in fname:
video_bytes = self.client.get(fname)
vr = VideoReader(io.BytesIO(video_bytes),
num_threads=1,
ctx=cpu(0))
else:
vr = VideoReader(fname, num_threads=1, ctx=cpu(0))
else:
if "s3://" in fname:
video_bytes = self.client.get(fname)
vr = VideoReader(io.BytesIO(video_bytes),
width=self.new_width,
height=self.new_height,
num_threads=1,
ctx=cpu(0))
else:
vr = VideoReader(fname, width=self.new_width, height=self.new_height,
num_threads=1, ctx=cpu(0))
except:
print("video cannot be loaded by decord: ", fname)
return []
if self.mode == 'test':
tick = len(vr) / float(self.num_segment)
all_index = list(np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segment)] +
[int(tick * x) for x in range(self.num_segment)]))
while len(all_index) < (self.num_segment * self.test_num_segment):
all_index.append(all_index[-1])
all_index = np.sort(np.array(all_index))
vr.seek(0)
buffer = vr.get_batch(all_index).asnumpy()
return buffer
elif self.mode == 'validation':
tick = len(vr) / float(self.num_segment)
all_index = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segment)])
vr.seek(0)
buffer = vr.get_batch(all_index).asnumpy()
return buffer
# handle temporal segments
average_duration = len(vr) // self.num_segment
if average_duration > 0:
all_index = list(np.multiply(list(range(self.num_segment)), average_duration) + np.random.randint(average_duration,
size=self.num_segment))
elif len(vr) > self.num_segment:
all_index = list(np.sort(np.random.randint(len(vr), size=self.num_segment)))
else:
all_index = list(np.zeros((self.num_segment,)))
vr.seek(0)
buffer = vr.get_batch(all_index).asnumpy()
return buffer
def __len__(self):
if self.mode != 'test':
return len(self.dataset_samples)
else:
return len(self.test_dataset)
def spatial_sampling(
frames,
spatial_idx=-1,
min_scale=256,
max_scale=320,
crop_size=224,
random_horizontal_flip=True,
inverse_uniform_sampling=False,
aspect_ratio=None,
scale=None,
motion_shift=False,
):
"""
Perform spatial sampling on the given video frames. If spatial_idx is
-1, perform random scale, random crop, and random flip on the given
frames. If spatial_idx is 0, 1, or 2, perform spatial uniform sampling
with the given spatial_idx.
Args:
frames (tensor): frames of images sampled from the video. The
dimension is `num frames` x `height` x `width` x `channel`.
spatial_idx (int): if -1, perform random spatial sampling. If 0, 1,
or 2, perform left, center, right crop if width is larger than
height, and perform top, center, buttom crop if height is larger
than width.
min_scale (int): the minimal size of scaling.
max_scale (int): the maximal size of scaling.
crop_size (int): the size of height and width used to crop the
frames.
inverse_uniform_sampling (bool): if True, sample uniformly in
[1 / max_scale, 1 / min_scale] and take a reciprocal to get the
scale. If False, take a uniform sample from [min_scale,
max_scale].
aspect_ratio (list): Aspect ratio range for resizing.
scale (list): Scale range for resizing.
motion_shift (bool): Whether to apply motion shift for resizing.
Returns:
frames (tensor): spatially sampled frames.
"""
assert spatial_idx in [-1, 0, 1, 2]
if spatial_idx == -1:
if aspect_ratio is None and scale is None:
frames, _ = random_short_side_scale_jitter(
images=frames,
min_size=min_scale,
max_size=max_scale,
inverse_uniform_sampling=inverse_uniform_sampling,
)
frames, _ = random_crop(frames, crop_size)
else:
transform_func = (
random_resized_crop_with_shift
if motion_shift
else random_resized_crop
)
frames = transform_func(
images=frames,
target_height=crop_size,
target_width=crop_size,
scale=scale,
ratio=aspect_ratio,
)
if random_horizontal_flip:
frames, _ = horizontal_flip(0.5, frames)
else:
# The testing is deterministic and no jitter should be performed.
# min_scale, max_scale, and crop_size are expect to be the same.
assert len({min_scale, max_scale, crop_size}) == 1
frames, _ = random_short_side_scale_jitter(
frames, min_scale, max_scale
)
frames, _ = uniform_crop(frames, crop_size, spatial_idx)
return frames
def tensor_normalize(tensor, mean, std):
"""
Normalize a given tensor by subtracting the mean and dividing the std.
Args:
tensor (tensor): tensor to normalize.
mean (tensor or list): mean value to subtract.
std (tensor or list): std to divide.
"""
if tensor.dtype == torch.uint8:
tensor = tensor.float()
tensor = tensor / 255.0
if type(mean) == list:
mean = torch.tensor(mean)
if type(std) == list:
std = torch.tensor(std)
tensor = tensor - mean
tensor = tensor / std
return tensor
|