Run Condition GAN model on the CIFAR-10 dataset, unable to clip (np.clip(X_test,-1,1))

51 Views Asked by At

everyone,

I want to load the dataset CIFAR-10, and do some pre-processing on those loaded images, when I run the following codes, my kernel crashes for no reason:

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')

. May I know why? The first "done" got printed out, but the second "done" can not be seen in the output window. I also paste the output here:

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

. I am able to see those example images from the CIFAR-10 dataset. Thank you. The kernel should be able to run the following codes:

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

.

1

There are 1 best solutions below

1
On

everyone,

I guess the program couldn't run because it used too much CPU/memory. After I turned to use Google Colab, the issue got fixed, I didn't see that message "Kernel died" any more.