代码拉取完成,页面将自动刷新
import random
import math
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, frame_indices):
for i, t in enumerate(self.transforms):
if isinstance(frame_indices[0], list):
next_transforms = Compose(self.transforms[i:])
dst_frame_indices = [
next_transforms(clip_frame_indices)
for clip_frame_indices in frame_indices
]
return dst_frame_indices
else:
frame_indices = t(frame_indices)
return frame_indices
class LoopPadding(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
out = frame_indices
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class TemporalBeginCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
out = frame_indices[:self.size]
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class TemporalCenterCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, frame_indices):
center_index = len(frame_indices) // 2
begin_index = max(0, center_index - (self.size // 2))
end_index = min(begin_index + self.size, len(frame_indices))
out = frame_indices[begin_index:end_index]
for index in out:
if len(out) >= self.size:
break
out.append(index)
return out
class TemporalRandomCrop(object):
def __init__(self, size):
self.size = size
self.loop = LoopPadding(size)
def __call__(self, frame_indices):
rand_end = max(0, len(frame_indices) - self.size - 1)
begin_index = random.randint(0, rand_end)
end_index = min(begin_index + self.size, len(frame_indices))
out = frame_indices[begin_index:end_index]
if len(out) < self.size:
out = self.loop(out)
return out
class TemporalEvenCrop(object):
def __init__(self, size, n_samples=1):
self.size = size
self.n_samples = n_samples
self.loop = LoopPadding(size)
def __call__(self, frame_indices):
n_frames = len(frame_indices)
stride = max(
1, math.ceil((n_frames - 1 - self.size) / (self.n_samples - 1)))
out = []
for begin_index in frame_indices[::stride]:
if len(out) >= self.n_samples:
break
end_index = min(frame_indices[-1] + 1, begin_index + self.size)
sample = list(range(begin_index, end_index))
if len(sample) < self.size:
out.append(self.loop(sample))
break
else:
out.append(sample)
return out
class SlidingWindow(object):
def __init__(self, size, stride=0):
self.size = size
if stride == 0:
self.stride = self.size
else:
self.stride = stride
self.loop = LoopPadding(size)
def __call__(self, frame_indices):
out = []
for begin_index in frame_indices[::self.stride]:
end_index = min(frame_indices[-1] + 1, begin_index + self.size)
sample = list(range(begin_index, end_index))
if len(sample) < self.size:
out.append(self.loop(sample))
break
else:
out.append(sample)
return out
class TemporalSubsampling(object):
def __init__(self, stride):
self.stride = stride
def __call__(self, frame_indices):
return frame_indices[::self.stride]
class Shuffle(object):
def __init__(self, block_size):
self.block_size = block_size
def __call__(self, frame_indices):
frame_indices = [
frame_indices[i:(i + self.block_size)]
for i in range(0, len(frame_indices), self.block_size)
]
random.shuffle(frame_indices)
frame_indices = [t for block in frame_indices for t in block]
return frame_indices
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。