Python matplotlib子地块非常慢
下面的代码Python matplotlib子地块非常慢,python,matplotlib,pytorch,data-visualization,Python,Matplotlib,Pytorch,Data Visualization,下面的代码 from torch.utils.data import DataLoader from defect_segmentation.data_loading.DatasetSingleImage import dataset_single_image_default import numpy as np if __name__ == "__main__": import matplotlib.pyplot as plt def main():
from torch.utils.data import DataLoader
from defect_segmentation.data_loading.DatasetSingleImage import dataset_single_image_default
import numpy as np
if __name__ == "__main__":
import matplotlib.pyplot as plt
def main():
dataset = dataset_single_image_default()
batch_size = 16
shuffle = True
num_workers = 0
loader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
for i_batch, sample_batched in enumerate(loader):
fig, axs = plt.subplots(int(np.sqrt(batch_size)), batch_size // int(np.sqrt(batch_size)))
fig.suptitle(f"i_batch = {i_batch}")
for i_sample, ax in zip(range(sample_batched.shape[0]), axs.flat):
ax.set_title(f"Sample #{i_sample}")
ax.axis("off")
ax.imshow(sample_batched[i_sample, :, :])
plt.pause(0.001)
main()
工作和输出数据如下
这很好
问题是到了第10个数字时,填充这些数字变得非常缓慢。我不知道是什么原因造成的
为完整起见,以下是创建数据集(跨越单个图像)的代码:
是什么导致绘图速度变慢,以及如何修复它?是否在每批之后关闭图形?如果所有数据仍处于“活动”状态-这可能会显著降低您的机器速度。尝试将图形写入磁盘(使用例如),并在每批之后关闭它们。这会影响速度吗?@Shai就是这样。你能解释一下为什么会这样吗?是否有一个使图保持打开的解决方法?打开的图(窗口)需要一些系统资源。如果您有许多打开的窗口,这相当于大量的系统资源@Gulzar@Shai这对我来说毫无意义,因为Windows可以轻松处理20多个窗口,但速度大约为10位数。不仅仅是窗口,还有所有图形元素:轴、标题、图例等。
from torch.utils.data import Dataset
from Utils.ConfigProvider import ConfigProvider
import cv2
import os
from overrides import overrides # pip install overrides
class DatasetSingleImage(Dataset):
def __init__(self, image_path: str, sample_shape: tuple, strides: tuple):
self._path = image_path
assert os.path.isfile(self._path)
self._im = cv2.imread(self._path)
self._shape = self._im.shape
self._rows, self._cols = self._shape[0], self._shape[1]
self._sample_shape = sample_shape
self._sample_rows, self._sample_cols = self._sample_shape[0], self._sample_shape[1]
self._strides = strides
self._stride_rows, self._stride_cols = self._strides[0], self._strides[1]
# self._rows_start_range = range(0, self._rows, self._stride_rows)
# self._cols_start_range = range(0, self._cols, self._stride_cols)
self._rows_tuples_range = \
[(c, min(c + self._sample_rows, self._rows)) for c in range(0, self._rows - self._sample_rows, self._stride_rows)]
self._cols_tuples_range = \
[(r, min(r + self._sample_cols, self._cols)) for r in range(0, self._cols - self._sample_cols, self._stride_cols)]
self._n_strides_rows = len(self._rows_tuples_range)
self._n_strides_cols = len(self._cols_tuples_range)
self._total_strides = self._n_strides_rows * self._n_strides_cols
def __len__(self):
return self._total_strides
@overrides # pip install overrides
def __getitem__(self, ind):
row_ind = ind // self._n_strides_cols
col_ind = ind % self._n_strides_cols
sample_x = self._rows_tuples_range[row_ind]
sample_y = self._cols_tuples_range[col_ind]
sample = self._im[sample_x[0]:sample_x[1], sample_y[0]:sample_y[1]]
assert sample.shape[:2] == self._sample_shape
return sample
def dataset_single_image_default():
path = ConfigProvider.config().data.defective_inspected_path1
sample_shape = (50, 50)
strides = (25, 25)
dataset = DatasetSingleImage(path, sample_shape, strides)
return dataset