未验证 提交 49af5ba0 编写于 作者: Y ysbecca 提交者: GitHub

Conformed to Black standards

上级 f7aec996
......@@ -37,6 +37,7 @@ def unpickle(file):
dict = pickle.load(fo, encoding="bytes")
return dict
images, labels = [], []
for batch in data_dir.glob("data_batch_*"):
batch_data = unpickle(batch)
......@@ -66,7 +67,8 @@ hdf5_dir = Path("data/hdf5/")
# Helper functions for timing
class CIFAR_Image():
class CIFAR_Image:
def __init__(self, image, label):
# Dimensions of image for reconstruction - not really necessary for this
# dataset, but some datasets may include images of varying sizes
......@@ -81,6 +83,7 @@ class CIFAR_Image():
image = np.frombuffer(self.image, dtype=np.uint8)
return image.reshape(*self.size, self.channels)
def store_single_disk(image, image_id, label):
""" Stores a single image as a .png file on disk.
Parameters:
......@@ -104,6 +107,7 @@ def store_single_disk(image, image_id, label):
)
writer.writerow([label])
def store_single_lmdb(image, image_id, label):
""" Stores a single image to a LMDB.
Parameters:
......@@ -128,6 +132,7 @@ def store_single_lmdb(image, image_id, label):
txn.put(key.encode("ascii"), pickle.dumps(value))
env.close()
def store_single_hdf5(image, image_id, label):
""" Stores a single image to an HDF5 file.
Parameters:
......@@ -155,6 +160,7 @@ def store_single_hdf5(image, image_id, label):
)
file.close()
_store_single_funcs = dict(
disk=store_single_disk,
lmdb=store_single_lmdb,
......@@ -175,6 +181,7 @@ for method in ("disk", "lmdb", "hdf5"):
store_single_timings[method] = t
print(f"Method: {method}, Time usage: {t}")
def store_many_disk(images, labels):
""" Stores an array of images to disk
Parameters:
......@@ -203,6 +210,7 @@ def store_many_disk(images, labels):
# value per row
writer.writerow([label])
def store_many_lmdb(images, labels):
""" Stores an array of images to LMDB.
Parameters:
......@@ -231,6 +239,7 @@ def store_many_lmdb(images, labels):
)
env.close()
def store_many_hdf5(images, labels):
""" Stores an array of images to HDF5.
Parameters:
......@@ -260,6 +269,7 @@ def store_many_hdf5(images, labels):
)
file.close()
_store_many_funcs = dict(
disk=store_many_disk,
lmdb=store_many_lmdb,
......@@ -268,7 +278,7 @@ _store_many_funcs = dict(
# Run the multiple images experiment now
cutoffs = [10, 100, 1000, 10000, 100000]
cutoffs = [10, 100, 1000, 10000, 100_000]
# Let's double our images so that we have 100,000
images = np.concatenate((images, images), axis=0)
......@@ -294,6 +304,7 @@ for cutoff in cutoffs:
# Let's visualise those results
def plot_with_legend(
x_range,
y_data,
......@@ -334,6 +345,7 @@ def plot_with_legend(
plt.legend(handles=all_plots)
plt.show()
disk_x = store_many_timings["disk"]
lmdb_x = store_many_timings["lmdb"]
hdf5_x = store_many_timings["hdf5"]
......@@ -360,9 +372,9 @@ plot_with_legend(
# Visualise how much memory is used.
# Memory used in KB
disk_mem = [44, 404, 4004, 40032, 400296]
lmdb_mem = [60, 420, 4000, 39000, 393000]
hdf5_mem = [36, 304, 2900, 29000, 293000]
disk_mem = [44, 404, 4004, 40032, 400_296]
lmdb_mem = [60, 420, 4000, 39000, 393_000]
hdf5_mem = [36, 304, 2900, 29000, 293_000]
X = [disk_mem, lmdb_mem, hdf5_mem]
......@@ -384,7 +396,7 @@ for i in range(1, len(cutoffs)):
plt.ylabel("Memory in KB")
plt.title("Disk memory used by method")
plt.xticks(ind, ("PNG", "LMDB", "HDF5"))
plt.yticks(np.arange(0, 400000, 100000))
plt.yticks(np.arange(0, 400_000, 100_000))
plt.legend(
[plot[0] for plot in plots],
......@@ -394,6 +406,7 @@ plt.show()
# Read out a single image.
def read_single_disk(image_id):
""" Stores a single image to disk.
Parameters:
......@@ -420,6 +433,7 @@ def read_single_disk(image_id):
return image, label
def read_single_lmdb(image_id):
""" Stores a single image to LMDB.
Parameters:
......@@ -450,6 +464,7 @@ def read_single_lmdb(image_id):
return image, label
def read_single_hdf5(image_id):
""" Stores a single image to HDF5.
Parameters:
......@@ -470,6 +485,7 @@ def read_single_hdf5(image_id):
return image, label
_read_single_funcs = dict(
disk=read_single_disk,
lmdb=read_single_lmdb,
......@@ -490,6 +506,7 @@ for method in ("disk", "lmdb", "hdf5"):
# Reading in many images
def read_many_disk(num_images):
""" Reads image from disk.
Parameters:
......@@ -524,6 +541,7 @@ def read_many_disk(num_images):
labels.append(int(row[0]))
return images, labels
def read_many_lmdb(num_images):
""" Reads image from LMDB.
Parameters:
......@@ -554,6 +572,7 @@ def read_many_lmdb(num_images):
env.close()
return images, labels
def read_many_hdf5(num_images):
""" Reads image from HDF5.
Parameters:
......@@ -577,6 +596,7 @@ def read_many_hdf5(num_images):
return images, labels
_read_many_funcs = dict(
disk=read_many_disk,
lmdb=read_many_lmdb,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册