如何在jupyter中输出数据集里图片
时间: 2024-11-22 07:46:41 浏览: 25
在 Jupyter Notebook 中展示数据集中的图片,你可以使用 matplotlib 或者 IPython 的 Image 标准魔法函数 `%matplotlib inline` 和 `Image` 对象。以下是一个简单的步骤:
1. 首先,确保已安装 matplotlib,如果没有安装,可以使用 pip 安装:
```bash
pip install matplotlib
```
2. 导入必要的库:
```python
import matplotlib.pyplot as plt
from PIL import Image
```
3. 使用 `%matplotlib inline` 设置图像在 notebook 内部显示:
```python
%matplotlib inline
```
4. 加载图片到变量中,假设你的图片文件名为 "image.jpg" 并位于当前目录下:
```python
image_path = 'image.jpg'
with open(image_path, 'rb') as img_file:
image_data = img_file.read()
```
5. 创建并显示图片:
```python
plt.imshow(Image.open(BytesIO(image_data)))
plt.show()
```
这里的 `BytesIO` 是一个内存缓存,用于在内存中打开图像。
相关问题
利用LeNet-5解决手写体数字识别,在测试集上输出精度。 2. 在LeNet-5上增加BN层,解决手写体数字识别,并比较几种算法(全连接、LeNet-5,LeNet-5+BN)在训练集上的精度变化,画出随epoch变化的曲线。 3. 分别搭建VGG13和ResNet18网络,解决CIFAR10数据集上的图片分类,画出这两种网络模型在该数据集上的随epoch的精度变化曲线。请给出Tensorflow2.0版本的epoch=30的且jupyter notebook 内核不会挂掉的解决代码
很高兴为您解答这些问题。以下是使用Tensorflow 2.0版本的代码解决这些问题:
1. 利用LeNet-5解决手写体数字识别,在测试集上输出精度。
```python
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255.
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255.
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
# 构建LeNet-5模型
model = Sequential([
Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(units=120, activation='relu'),
Dense(units=84, activation='relu'),
Dense(units=10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
model.fit(x_train, y_train, batch_size=128, epochs=10, validation_data=(x_test, y_test))
# 在测试集上输出精度
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
```
2. 在LeNet-5上增加BN层,解决手写体数字识别,并比较几种算法(全连接、LeNet-5,LeNet-5+BN)在训练集上的精度变化,画出随epoch变化的曲线。
```python
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization
# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255.
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255.
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
# 构建全连接模型
fc_model = Sequential([
Flatten(input_shape=(28, 28, 1)),
Dense(units=128, activation='relu'),
Dense(units=64, activation='relu'),
Dense(units=10, activation='softmax')
])
# 构建LeNet-5模型
lenet5_model = Sequential([
Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(units=120, activation='relu'),
Dense(units=84, activation='relu'),
Dense(units=10, activation='softmax')
])
# 构建增加BN层的LeNet-5模型
lenet5_bn_model = Sequential([
Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(units=120, activation='relu'),
BatchNormalization(),
Dense(units=84, activation='relu'),
BatchNormalization(),
Dense(units=10, activation='softmax')
])
# 编译模型
fc_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
lenet5_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
lenet5_bn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
fc_history = fc_model.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test))
lenet5_history = lenet5_model.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test))
lenet5_bn_history = lenet5_bn_model.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test))
# 画出随epoch变化的曲线
import matplotlib.pyplot as plt
plt.plot(fc_history.history['accuracy'])
plt.plot(lenet5_history.history['accuracy'])
plt.plot(lenet5_bn_history.history['accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['FC', 'LeNet-5', 'LeNet-5+BN'], loc='lower right')
plt.show()
```
3. 分别搭建VGG13和ResNet18网络,解决CIFAR10数据集上的图片分类,画出这两种网络模型在该数据集上的随epoch的精度变化曲线。
```python
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Activation, Dropout
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras import regularizers
import numpy as np
# 加载CIFAR10数据集
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 数据预处理
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
# 学习率衰减函数
def lr_schedule(epoch):
lr = 1e-3
if epoch > 75:
lr *= 0.5e-3
elif epoch > 50:
lr *= 1e-3
elif epoch > 25:
lr *= 1e-2
print('Learning rate:', lr)
return lr
# 构建VGG13模型
def vgg13_model():
model = Sequential([
Conv2D(64, (3, 3), padding='same', input_shape=x_train.shape[1:], activation='relu'),
BatchNormalization(),
Conv2D(64, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Dropout(0.2),
Conv2D(128, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
Conv2D(128, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Dropout(0.3),
Conv2D(256, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
Conv2D(256, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
Conv2D(256, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Dropout(0.4),
Conv2D(512, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
Conv2D(512, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
Conv2D(512, (3, 3), padding='same', activation='relu'),
BatchNormalization(),
MaxPooling2D(pool_size=(2, 2)),
Dropout(0.5),
Flatten(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(10, activation='softmax')
])
return model
# 构建ResNet18模型
def resnet18_model():
def residual_block(inputs, filters, strides=1, projection=False):
identity = inputs
x = Conv2D(filters=filters, kernel_size=(3, 3), strides=strides, padding='same')(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters=filters, kernel_size=(3, 3), strides=1, padding='same')(x)
x = BatchNormalization()(x)
if projection:
identity = Conv2D(filters=filters, kernel_size=(1, 1), strides=strides, padding='same')(inputs)
identity = BatchNormalization()(identity)
x = tf.keras.layers.add([x, identity])
x = Activation('relu')(x)
return x
inputs = tf.keras.Input(shape=x_train.shape[1:])
x = Conv2D(filters=64, kernel_size=(7, 7), strides=2, padding='same')(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')(x)
x = residual_block(x, filters=64)
x = residual_block(x, filters=64)
x = residual_block(x, filters=128, strides=2, projection=True)
x = residual_block(x, filters=128)
x = residual_block(x, filters=256, strides=2, projection=True)
x = residual_block(x, filters=256)
x = residual_block(x, filters=512, strides=2, projection=True)
x = residual_block(x, filters=512)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = Dense(units=10, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=x)
return model
# 编译模型
vgg13_model = vgg13_model()
resnet18_model = resnet18_model()
sgd = SGD(lr=lr_schedule(0), momentum=0.9)
vgg13_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
resnet18_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
vgg13_history = vgg13_model.fit(x_train, y_train, batch_size=128, epochs=30, validation_data=(x_test, y_test), callbacks=[LearningRateScheduler(lr_schedule)])
resnet18_history = resnet18_model.fit(x_train, y_train, batch_size=128, epochs=30, validation_data=(x_test, y_test), callbacks=[LearningRateScheduler(lr_schedule)])
# 画出随epoch变化的曲线
import matplotlib.pyplot as plt
plt.plot(vgg13_history.history['accuracy'])
plt.plot(resnet18_history.history['accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['VGG13', 'ResNet18'], loc='lower right')
plt.show()
```
cifar10数据集图片输出黑色
### 关于CIFAR-10数据集图像显示为黑色的问题
当遇到CIFAR-10数据集中图像显示为黑色的情况时,这通常是由几个常见原因引起的。以下是可能的原因以及相应的解决方案。
#### 可能原因一:图像加载方式不正确
如果使用了错误的方式读取或处理图像,则可能导致图像无法正常显示。例如,在某些情况下,直接从字节流转换到图像可能会丢失颜色通道的信息,从而使得图像看起来是全黑的[^1]。
```python
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
def show_image(img_array):
img = Image.fromarray(np.uint8((img_array + 1) * 127.5)) # 将标准化后的图片还原并转成PIL.Image对象
plt.imshow(img)
plt.axis('off')
plt.show()
```
这段代码展示了如何将经过预处理(如减均值除方差)的数据恢复回原始范围再展示出来的方法。注意这里假设输入`img_array`已经被标准化到了[-1, 1]区间内;如果不是这种情况,请调整对应的数值变换逻辑以适应实际场景。
#### 可能原因二:数据归一化设置不当
另一个常见的问题是由于数据已经进行了某种形式的归一化操作,但在可视化之前没有将其逆向变换成原来的尺度。对于CIFAR-10这样的小型彩色图库来说,默认情况下每个像素的颜色分量会被缩放到[0, 1]之间或者通过其他方法做了线性映射。因此,在尝试查看这些图像前,应该先确认它们是否处于合适的表示范围内,并据此做出相应调整[^3]。
```python
mean = (0.4914, 0.4822, 0.4465)
std = (0.2023, 0.1994, 0.2010)
unnormalize = transforms.Normalize(
mean=[-m/s for m, s in zip(mean, std)],
std=[1/s for s in std])
transformed_img_tensor = unnormalize(transformed_img_tensor.clone()) # 假设 transformed_img_tensor 是 PyTorch Tensor 类型
show_image(transformed_img_tensor.permute(1, 2, 0).numpy())
```
此部分代码片段说明了怎样利用PyTorch中的transforms模块来执行反向规范化过程,以便可以更直观地观察到未经任何修改的真实色彩效果。
#### 可能原因三:图形界面配置问题
有时即使上述两方面都没有出现问题,仍然会因为绘图环境本身存在缺陷而导致图像呈现异常现象。比如Matplotlib默认保存的文件格式可能是灰度模式而非真彩模式,或者是Jupyter Notebook内的输出窗口未能及时刷新等原因造成的视觉误差。针对这类情况建议检查当前使用的绘图工具及其参数设定是否有误,必要时可更换不同的前端框架来进行对比验证[^2]。
阅读全文
相关推荐
















