numpy 在CIFAR-10数据集上运行Condition GAN模型,无法裁剪(np.clip(X_test,-1,1))

iklwldmw  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(110)

各位,
我想加载数据集CIFAR-10,并对加载的图像进行一些预处理,当我运行以下代码时,我的内核无故崩溃:

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
#import packages
from keras.datasets import cifar10
from keras.models import Sequential, Model
from keras.layers import Dense, LeakyReLU, BatchNormalization
from keras.layers import Conv2D, Conv2DTranspose, Reshape, Flatten
from keras.layers import Input, Flatten, Embedding, multiply, Dropout
from keras.layers import Concatenate, GaussianNoise,Activation
from keras.optimizers import Adam
from keras.utils import np_utils, to_categorical
from keras import initializers
from keras import backend as K
from keras import *
#load dataset
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
#explore visual data
num_classes = len(np.unique(y_train))
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
fig = plt.figure(figsize=(8,3))
for i in range(num_classes):
ax = plt.subplot(2, 5, 1 + i, xticks=[], yticks=[])
idx = np.where(y_train[:]==i)[0]
features_idx = X_train[idx,::]
img_num = np.random.randint(features_idx.shape[0])
img = features_idx[img_num,::]
ax.set_title(class_names[i])
plt.imshow(img)
plt.tight_layout()
plt.show()
#reshaping and normalizing the inputs
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
if K.image_data_format() == 'channels_first':
X_train = X_train.reshape(X_train.shape[0], 3, 32, 32)
X_test = X_test.reshape(X_test.shape[0], 3, 32, 32)
input_shape = (3, 32, 32)
else:
X_train = X_train.reshape(X_train.shape[0], 32, 32, 3)
X_test = X_test.reshape(X_test.shape[0], 32, 32, 3)
input_shape = (32, 32, 3)    
#convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, num_classes)
Y_test = np_utils.to_categorical(y_test, num_classes)
#the generator is using tanh activation, for which we need to preprocess 
#the image data into the range between -1 and 1.
X_train = np.float32(X_train)
X_train = (X_train / 255 - 0.5) * 2
X_train = np.clip(X_train, -1, 1)
X_test = np.float32(X_test)
X_test = (X_test / 255 - 0.5) * 2
print('done')
X_test = np.clip(X_test, -1, 1)
print('done')

.我可以知道为什么吗?第一个“done”被打印出来了,但是第二个“done”在输出窗口中看不到。我也将输出粘贴在这里:

X_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
done

.我能够看到CIFAR-10数据集的示例图像。谢谢。内核应该能够运行以下代码:

X_test = np.clip(X_test, -1, 1)

.

ny6fqffe

ny6fqffe1#

各位,
我猜程序无法运行是因为它占用了太多的CPU/内存。在我使用Google Colab之后,这个问题得到了解决,我再也没有看到“内核死亡”的消息。

相关问题