我正在尝试下载freyface数据集并显示一些示例。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
# configure matplotlib
plt.rcParams['figure.figsize'] = (13.5, 13.5) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
import os
from urllib.request import urlopen, URLError, HTTPError
from scipy.io import loadmat
def fetch_file(url):
"""Downloads a file from a URL.
"""
try:
f = urlopen(url)
print
("Downloading data file " + url + " ...")
# Open our local file for writing
with open(os.path.basename(url), "wb") as local_file:
local_file.write(f.read())
print
("Done.")
# handle errors
except HTTPError as e:
print("HTTP Error:", e.code, url)
except URLError as e:
print
("URL Error:", e.reason, url)
url = "http://www.cs.nyu.edu/~roweis/data/frey_rawface.mat"
data_filename = os.path.basename(url)
if not os.path.exists(data_filename):
fetch_file(url)
else:
print
("Data file %s exists." % data_filename)
# reshape data for later convenience
img_rows, img_cols = 28, 20
ff = loadmat(data_filename, squeeze_me=True, struct_as_record=False)
ff = ff["ff"].T.reshape((-1, img_rows, img_cols))
np.random.seed(42)
n_pixels = img_rows * img_cols
X_train = ff[:1800]
X_val = ff[1800:1900]
X_train = X_train.astype('float32') / 255.
X_val = X_val.astype('float32') / 255.
X_train = X_train.reshape((len(X_train), n_pixels))
X_val = X_val.reshape((len(X_val), n_pixels))
#visualization
def show_examples(data, n=None, n_cols=20, thumbnail_cb=None):
if n is None:
n = len(data)
n_rows = int(np.ceil(n / float(n_cols)))
figure = np.zeros((img_rows * n_rows, img_cols * n_cols))
for k, x in enumerate(data[:n]):
r = k // n_cols
c = k % n_cols
figure[r * img_rows: (r + 1) * img_rows,
c * img_cols: (c + 1) * img_cols] = x
if thumbnail_cb is not None:
thumbnail_cb(locals())
plt.figure(figsize=(12, 10))
plt.imshow(figure)
plt.axis("off")
plt.tight_layout()
show_examples(ff, n=200, n_cols=25)
但我得到了这个奇怪的错误:
TypeError:remove()缺少1个必需的位置参数:' wr' 在以下情况中忽略异常:.remove at 0x000001EB6199D048>
TypeError:remove()缺少1个必需的位置参数:' wr' 在以下情况中忽略异常:.remove at 0x000001EB61992EA0>
TypeError:remove()缺少1个必需的位置参数:' wr' 在以下情况中忽略异常:.remove at 0x000001EB61992F28> TypeError:remove()缺少1个必需的位置参数:' wr'
答案 0 :(得分:1)
我通过这篇文章修改了https://hg.python.org/cpython/rev/2cb530243943
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -106,7 +106,7 @@ class WeakValueDictionary(collections.Mu
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
- def remove(wr, selfref=ref(self)):
+ def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
self = selfref()
if self is not None:
if self._iterating:
@@ -114,7 +114,7 @@ class WeakValueDictionary(collections.Mu
else:
# Atomic removal is necessary since this function
# can be called asynchronously by the GC
- _remove_dead_weakref(d, wr.key)
+ _atomic_removal(d, wr.key)
self._remove = remove
# A list of keys to be removed
self._pending_removals = []