使用 keras 中的 image_dataset_from_directory 创建图像数据集后,如何以 numpy 格式从数据集中获取可以使用 pyplot.imshow 显示的第一个图像?
import tensorflow as tf
import matplotlib.pyplot as plt
test_data = tf.keras.preprocessing.image_dataset_from_directory(
"C:\\Users\\Admin\\Downloads\\kagglecatsanddogs_3367a",
validation_split=.1,
subset='validation',
seed=123)
for e in test_data.as_numpy_iterator():
print(e[1:])
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,它接受哺乳动物列表或动物列表作为参数。
这是我的代码
from typing import Union, List
class Animals():
pass
class Mammels(Animals):
pass
def add_mammel(x : List[Union[Animals, Mammels]]):
x.append(Mammels())
l = [Mammels(), Mammels()]
add_mammel(l)
print(l)
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但是当我用 mypy 检查它时,我得到以下内容
python -m mypy fourth.py
fourth.py:11: error: Argument 1 to "add_mammel" has incompatible type "List[Mammels]"; expected "List[Union[Animals, Mammels]]"
fourth.py:11: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance
fourth.py:11: note: Consider using "Sequence" instead, which is covariant
Run Code Online (Sandbox Code Playgroud)
这个问题与“方差”有关,但我无法弄清楚这真正意味着什么。
在Python中如何将数字四舍五入到最接近的0.5。
例如
100.4 将四舍五入为 100.5
100.6 将四舍五入为 100.5
100.9 将四舍五入为 101