修改PythonClass过滤文件
Modify Python Class to filter files
python 的新手。如何修改 class 以使用字符串过滤文件夹中的文件。现在 returns folder_containing_the_content_folder 中的所有文件可能有数百万个项目。以下工作但是我想隔离包含特定字符串的文件,例如,隔离包含 'v_1234_frame':
的所有文件
# Image loader
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.mul(255))
])
image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform=transform)
image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)
有效的 class 需要修改以过滤包含 'v_1234_frame':
的文件名
class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths.
Extends torchvision.datasets.ImageFolder()
Reference: https://discuss.pytorch.org/t/dataloader-filenames-in-each-batch/4212/2
"""
# override the __getitem__ method. this is the method dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
path = self.imgs[index][0]
# make a new tuple that includes original and the path
tuple_with_path = (*original_tuple, path)
return tuple_with_path
我正在学习 python,但似乎无法想出解决方案。希望你能help/suggest改变一下class或调用方法。
构建了我自己的数据加载器,通过 glob 中的通配符模式隔离文件,然后循环遍历这些文件,为每个图像创建一个张量,将其传递给我的模型,这需要将其转换为浮点数。从路径中提取基本名称(图像名称)(例如 img_frame1.jpg)。将结果保存到我的样式文件夹。这种方法让我可以通过通配符完全控制文件。我已经包含了解决方案中使用的其他功能。注意:我没有使用 gpu 来处理这些,所以我可以在标准 python 网络服务器上 运行 它。希望这对将来的人有帮助。简单有时更好:)
# Load image file
# def load_image(path):
# # Images loaded as BGR
# img = cv2.imread(path)
# return img
# def itot(img, max_size=None):
# # Rescale the image
# if (max_size == None):
# itot_t = transforms.Compose([
# # transforms.ToPILImage(),
# transforms.ToTensor(),
# transforms.Lambda(lambda x: x.mul(255))
# ])
# else:
# H, W, C = img.shape
# image_size = tuple([int((float(max_size) / max([H, W])) * x) for x in [H, W]])
# itot_t = transforms.Compose([
# transforms.ToPILImage(),
# transforms.Resize(image_size),
# transforms.ToTensor(),
# transforms.Lambda(lambda x: x.mul(255))
# ])
#
# # Convert image to tensor
# tensor = itot_t(img)
#
# # Add the batch_size dimension
# tensor = tensor.unsqueeze(dim=0)
# return tensor
folder_data = glob.glob(folder_containing_the_content_folder + "content_folder/" + video_token + "_frame*.jpg")
# image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform)
# image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)
# Load Transformer Network
net = transformer.TransformerNetwork()
net.load_state_dict(torch.load(style_path))
net = net.to(device)
with torch.no_grad():
for image_name in folder_data:
img = utils.load_image(image_name)
img = img / 255.0 # standardize the data/transform
img_tensor = utils.itot(img)
# style image tensor
generated_tensor = net(img_tensor.float())
# convert image the model modified tensor back to an image
generated_image = utils.ttoi(generated_tensor)
image_name = os.path.basename(image_name)
# save generated image to folder
utils.saveimg(generated_image, save_folder + image_name)
python 的新手。如何修改 class 以使用字符串过滤文件夹中的文件。现在 returns folder_containing_the_content_folder 中的所有文件可能有数百万个项目。以下工作但是我想隔离包含特定字符串的文件,例如,隔离包含 'v_1234_frame':
的所有文件# Image loader
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.mul(255))
])
image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform=transform)
image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)
有效的 class 需要修改以过滤包含 'v_1234_frame':
的文件名class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths.
Extends torchvision.datasets.ImageFolder()
Reference: https://discuss.pytorch.org/t/dataloader-filenames-in-each-batch/4212/2
"""
# override the __getitem__ method. this is the method dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
path = self.imgs[index][0]
# make a new tuple that includes original and the path
tuple_with_path = (*original_tuple, path)
return tuple_with_path
我正在学习 python,但似乎无法想出解决方案。希望你能help/suggest改变一下class或调用方法。
构建了我自己的数据加载器,通过 glob 中的通配符模式隔离文件,然后循环遍历这些文件,为每个图像创建一个张量,将其传递给我的模型,这需要将其转换为浮点数。从路径中提取基本名称(图像名称)(例如 img_frame1.jpg)。将结果保存到我的样式文件夹。这种方法让我可以通过通配符完全控制文件。我已经包含了解决方案中使用的其他功能。注意:我没有使用 gpu 来处理这些,所以我可以在标准 python 网络服务器上 运行 它。希望这对将来的人有帮助。简单有时更好:)
# Load image file
# def load_image(path):
# # Images loaded as BGR
# img = cv2.imread(path)
# return img
# def itot(img, max_size=None):
# # Rescale the image
# if (max_size == None):
# itot_t = transforms.Compose([
# # transforms.ToPILImage(),
# transforms.ToTensor(),
# transforms.Lambda(lambda x: x.mul(255))
# ])
# else:
# H, W, C = img.shape
# image_size = tuple([int((float(max_size) / max([H, W])) * x) for x in [H, W]])
# itot_t = transforms.Compose([
# transforms.ToPILImage(),
# transforms.Resize(image_size),
# transforms.ToTensor(),
# transforms.Lambda(lambda x: x.mul(255))
# ])
#
# # Convert image to tensor
# tensor = itot_t(img)
#
# # Add the batch_size dimension
# tensor = tensor.unsqueeze(dim=0)
# return tensor
folder_data = glob.glob(folder_containing_the_content_folder + "content_folder/" + video_token + "_frame*.jpg")
# image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform)
# image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)
# Load Transformer Network
net = transformer.TransformerNetwork()
net.load_state_dict(torch.load(style_path))
net = net.to(device)
with torch.no_grad():
for image_name in folder_data:
img = utils.load_image(image_name)
img = img / 255.0 # standardize the data/transform
img_tensor = utils.itot(img)
# style image tensor
generated_tensor = net(img_tensor.float())
# convert image the model modified tensor back to an image
generated_image = utils.ttoi(generated_tensor)
image_name = os.path.basename(image_name)
# save generated image to folder
utils.saveimg(generated_image, save_folder + image_name)