VGG16 training with colab reutrns WARNING:root:kernel - numpy

im still beginner with DL, here im trying to train VGG16 on a list of images to return 2048 features, but my issue was it returns 4094 features instead of 2048, so what i did to solve the issue is to create sequential model and remove one layer to be 2048 but will during the training on google cloab it returns kernel issue as below :
WARNING:root:kernel d3b89e05-3e27-4035-afcd-79a2c35a319a restarted
i have restarted the runtime and make a factorial restart, but still issue exists maybe because I did something wrong I'm my code, this issue happened only in the last code block
def read_images(folder_path, classlbl):
# load all images into a list
images = []
img_width, img_height = 224, 224
class1=[]
for img in os.listdir(folder_path):
img = os.path.join(folder_path, img)
img = load_img(img, target_size=(img_width, img_height))
class1.append(classlbl)# class one.
images.append(img)
return images, class1
#compute features for each image.
def computefeatures(model,image):
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# get extracted features
features = model.predict(image)
return features
model = Sequential()
# second convolutional block
model.add(Conv2D(128, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(128,(2,2), strides=(1,1), padding="same",activation="relu"))
model.add(MaxPooling2D((2, 2), strides=(2,2)))
# third convolutional block
model.add(Conv2D(256, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(256,(3,3), strides=(1,1), padding="same",activation="relu"))
model.add(Conv2D(256,(3,3), strides=(1,1), padding="same",activation="relu"))
model.add(MaxPooling2D((2, 2), strides=(2,2)))
# third convolutional block
model.add(Conv2D(512, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(512,(3,3), strides=(1,1), padding="same",activation="relu"))
model.add(Conv2D(512,(3,3), strides=(1,1), padding="same",activation="relu"))
model.add(MaxPooling2D((2, 2), strides=(2,2)))
# third convolutional block
model.add(Conv2D(512, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(512,(3,3), strides=(1,1), padding="same",activation="relu"))
model.add(Conv2D(512,(3,3), strides=(1,1), padding="same",activation="relu"))
#DNN Backend
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dense(4096, activation='relu'))
#Output layer for classification (1000 classes)
model.add(Dense(1000, activation='softmax'))
#use the categorical cross entropy loss function for a multi-class classifier.
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# call the image read and
folder_path = '/content/imageDir/101_ObjectCategories/windsor_chair'
classlbl=0
images, class1 =read_images(folder_path, classlbl)
# call the fucntion to compute the features for each image.
list_features1=[]
list_features1 = np.empty((0,4096), float)# create an empty array with 0 row and 4096 columns this number from fature
# extraction from vg16
for img in range(len(images)):
f2=computefeatures(model,images[img]) # compute features forea each image
list_features1 = np.append(list_features1, f2, axis=0)

One of the most popular reasons leads to restart kernel itself is out of memory (CPU RAM). It seems like because of the read_images function, you were trying to read and store too many image files at the same time.
You may want to try ImageDataGenerator API for reading images in small batches.
OR
You can simply change your read_images function as below:
def read_images(folder_path, classlbl):
# load all images into a list
images = []
img_width, img_height = 224, 224
class1=[]
for img in os.listdir(folder_path):
img = os.path.join(folder_path, img)
img = load_img(img, target_size=(img_width, img_height))
yield img, classlbl ## <--- this line
And use it like this
result =read_images(folder_path, classlbl)
# call the fucntion to compute the features for each image.
list_features1=[]
list_features1 = np.empty((0,4096), float)# create an empty array with 0 row and 4096 columns this number from fature
# extraction from vg16
for img, _ in result: # <--- change this line
f2=computefeatures(model, img) # compute features forea each image
list_features1 = np.append(list_features1, f2, axis=0)

Related

How to train image classifications in tensorflow for grayscale images using flow_from_directory?

I am training a CNN model to classify grayscale images into 6 classes. While my code is working well on RGB images, it gives error when I apply it on grayscale images. Here is part of the code:
input_shape=(256, 256,1) # assign "1" to the last channel to account for grayscale.
target_size = (256, 256) # To use it in the flow_from_directory package
model_name='Test1'
model_filename = (model_name+'.hdf5')
optimizer = Adam(learning_rate=1e-3)
loss=['categorical_crossentropy']
metrics = ['accuracy']
## Here is the model:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(6)) # To account for 6 classes
model.add(Activation('softmax'))
model.summary()
train_datagen = ImageDataGenerator(
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
vaidation_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory(
train_path, # points to the folder containing all training images
target_size=target_size,
color_mode='grayscale', # to specify the grayscale
batch_size=batch_size,
shuffle=True,
class_mode='categorical',
interpolation='nearest')
validation_generator = vaidation_datagen.flow_from_directory(
validation_path, # points to the folder containing all validation images
target_size=target_size,
color_mode='grayscale', # to specify the grayscale
batch_size=batch_size,
shuffle=True,
class_mode='categorical',
interpolation='nearest')
model.compile(optimizer, loss , metrics)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint((model_path+model_filename), monitor='loss',verbose=1, save_best_only=True)
model.summary()
history = model.fit(
train_generator,
steps_per_epoch = num_of_train_img_raw//batch_size,
epochs = epochs,
validation_data = validation_generator,
validation_steps = num_of_val_img_raw//batch_size,
callbacks=[model_checkpoint],
use_multiprocessing = False)
Here is the error I receive:
"input depth must be evenly divisible by filter depth: 1 vs 3"
Then the IDE kernel freezes!
Yes it is wasteful/slower but why not just convert the greyscale images into RGB? Unless you need superior performance or really want to update the model both of which will take time to do.
Use grayscale_to_rgb (already built into tensorflow).

Keras - Trying to get 'logits' - one layer before the softmax activation function

I'm trying to get the 'logits' out of my Keras CNN classifier.
I have tried the suggested method here: link.
First I created two models to check the implementation :
create_CNN_MNIST CNN classifier that returns the softmax probabilities.
create_CNN_MNIST_logits CNN with the same layers as in (1) with a little twist in the last layer - changed the activation function to linear to return logits.
Both models were fed with the same Train and Test data of MNIST. Then I applied softmax on the logits, I got a different output from the softmax CNN.
I couldn't find a problem in my code. Maybe you could help advise another method to extract 'logits' from the model?
the code:
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
def create_CNN_MNIST_logits() :
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='linear'))
# compile model
opt = SGD(learning_rate=0.01, momentum=0.9)
def my_sparse_categorical_crossentropy(y_true, y_pred):
return keras.losses.categorical_crossentropy(y_true, y_pred, from_logits=True)
model.compile(optimizer=opt, loss=my_sparse_categorical_crossentropy, metrics=['accuracy'])
return model
def create_CNN_MNIST() :
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='softmax'))
# compile model
opt = SGD(learning_rate=0.01, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model
# load data
X_train = np.load('./data/X_train.npy')
X_test = np.load('./data/X_test.npy')
y_train = np.load('./data/y_train.npy')
y_test = np.load('./data/y_test.npy')
#create models
model_softmax = create_CNN_MNIST()
model_logits = create_CNN_MNIST_logits()
pixels = 28
channels = 1
num_labels = 10
# Reshaping to format which CNN expects (batch, height, width, channels)
trainX_cnn = X_train.reshape(X_train.shape[0], pixels, pixels, channels).astype('float32')
testX_cnn = X_test.reshape(X_test.shape[0], pixels, pixels, channels).astype('float32')
# Normalize images from 0-255 to 0-1
trainX_cnn /= 255
testX_cnn /= 255
train_y_cnn = utils.to_categorical(y_train, num_labels)
test_y_cnn = utils.to_categorical(y_test, num_labels)
#train the models:
model_logits.fit(trainX_cnn, train_y_cnn, validation_split=0.2, epochs=10,
batch_size=32)
model_softmax.fit(trainX_cnn, train_y_cnn, validation_split=0.2, epochs=10,
batch_size=32)
On the evaluation stage, I'll do softmax on the logits to check if its the same as the regular model:
#predict
y_pred_softmax = model_softmax.predict(testX_cnn)
y_pred_logits = model_logits.predict(testX_cnn)
#apply softmax on the logits to get the same result of regular CNN
y_pred_logits_activated = softmax(y_pred_logits)
Now I get different values in both y_pred_logits_activated and y_pred_softmax that lead to different accuracy on the test set.
Your models are probably being trained differently, make sure to set the seed prior to both fit commands so that they're initialised the same weights and have the same train/val split. Also, is the softmax might be incorrect:
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x)
return e_x / e_x.sum(axis=1)
This is numerically equivalent to subtracting the max (https://stackoverflow.com/a/34969389/10475762), and the axis should be 1 if your matrix is of shape [batch, outputs].

How to show latent layer in tensorboard?

I have a trained auto-encoder model which I want to visualize the latent layer in tensor-board.
How can I do it ?
el1 = Conv2D(8, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3))
el2 = MaxPooling2D((2, 2), padding='same')
el3 = Conv2D(8, (3, 3), activation='relu', padding='same')
el4 = MaxPooling2D((2, 2), padding='same')
dl1 = Conv2DTranspose(8, (3, 3), strides=2, activation='relu', padding='same')
dl2 = Conv2DTranspose(8, (3, 3), strides=2, activation='relu', padding='same')
output_layer = Conv2D(3, (3, 3), activation='sigmoid', padding='same')
autoencoder = Sequential()
autoencoder.add(el1)
autoencoder.add(el2)
autoencoder.add(el3)
autoencoder.add(el4)
autoencoder.add(dl1)
autoencoder.add(dl2)
autoencoder.add(output_layer)
autoencoder.compile(optimizer='adam', loss="binary_crossentropy")
logdir = os.path.join("logs/fit/", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
autoencoder.fit(X_train, X_train, epochs=100, batch_size=64, validation_data=(X_test, X_test), verbose=1,
callbacks=[tensorboard_callback])
After the model was fitted, how can I add the latent layer into tensor-board and view it after running tsne or pca ?
You can follow the guide: Visualizing Data using the Embedding Projector in TensorBoard.
I assumed that by "latent layer" you mean "latent space", i.e the representation of the encoded input.
In your case, if you want to represent your latent space, it's first needed to extract the encoder part from your autoencoder. This can be achieved with the functional API of keras:
# After fitting the autoencoder, we create a model that represents the encoder
encoder = tf.keras.Model(autoencoder.input, autoencoder.get_layer(el4.name).output)
Then, it's possible to calculate the latent representation of your test set using the encoder:
latent_test = encoder(X_test)
Then, by following the guide linked above, the latent representation can be saved in a Checkpoint to be visualized with the Tensorboard projector:
# Save the weights we want to analyze as a variable.
# The weights need to have the shape (Number of sample, Total Dimensions)
# Hence why we flatten the Tensor
weights = tf.Variable(tf.reshape(latent_test,(X_test.shape[0],-1)), name="latent_test")
# Create a checkpoint from embedding, the filename and key are the
# name of the tensor.
checkpoint = tf.train.Checkpoint(latent_test=weights)
checkpoint.save(os.path.join(logdir, "embedding.ckpt"))
from tensorboard.plugins import projector
# Set up config.
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
# The name of the tensor will be suffixed by `/.ATTRIBUTES/VARIABLE_VALUE`.
embedding.tensor_name = "latent_test/.ATTRIBUTES/VARIABLE_VALUE"
projector.visualize_embeddings(logdir, config)
Finally, the projector can be accessed by running the Tensorboard:
$ tensorboard --logdir /path/to/logdir
Finally an image of the projector with PCA (here with some random data):

Merging tensors based on a key

I am dealing with a problem in which network design is such that it requires merging output of one part of the network with a tabular input(other input) data based on a key and training the network further with the merged data. It appeared that there is no way two tensors can be merged based on a key. Hence though of converting tensor to numpy to pandas data and them merging. The merged data would be converted back to tensor and used further in the network. Below is the code for it:
def build_convnet(shape=(112, 112, 1)):
from keras.layers import Conv2D, BatchNormalization, MaxPool2D, GlobalMaxPool2D
momentum = .9
model = keras.Sequential()
model.add(Conv2D(64, (3,3), input_shape=shape,
padding='same', activation='relu'))
model.add(Conv2D(64, (3,3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=momentum))
model.add(MaxPool2D())
model.add(Conv2D(128, (3,3), padding='same', activation='relu'))
model.add(Conv2D(128, (3,3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=momentum))
model.add(MaxPool2D())
model.add(Conv2D(256, (3,3), padding='same', activation='relu'))
model.add(Conv2D(256, (3,3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=momentum))
model.add(MaxPool2D())
model.add(Conv2D(512, (3,3), padding='same', activation='relu'))
model.add(Conv2D(512, (3,3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=momentum))
# flatten...
model.add(GlobalMaxPool2D())
return model
def action_model(shape=(3, 112, 112, 1)):
from keras.layers import TimeDistributed, GRU, Dense, Dropout, Concatenate
# Create our convnet with (224, 224, 3) input shape
convnet = build_convnet(shape[1:])
# then create our final model
model = keras.Sequential()
# add the convnet with (5, 224, 224, 3) shape
model.add(TimeDistributed(convnet, input_shape=shape))
# here, you can also use GRU or LSTM
model.add(GRU(64))
# and finally, we make a decision network
model.add(Dense(1024, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(64, activation='relu'))
model.add(Dense(4, activation='relu'))
return model
# create the tab_data and cnn_gru models
tab_dt = keras.Input(shape=(trainX.shape[1],))
cnn_gru = action_model(X_train.shape[1:])
# converting tensor to numpy array and merging with a tabular data on a key(Patient)
cnn_gru_np = cnn_gru.output.eval()
cnn_gru_pd = pd.Dataframe(cnn_gru_np, names = ["V1", "V2", "V3", "V4"])
cnn_gru_pd["Patient"] = train_p
tab_dt_np = tab_dt.eval()
tab_dt_pd = pd.Dataframe(tab_dt_np, names = ["Weeks", "Percent", "Age", "Sex_Male", "SmokingStatus_Ex-smoker", "SmokingStatus_Never smoked"])
tab_dt_pd["Patient"] = train_p.numpy()
combinedInput_pd = pd.merge(tab_dt_pd, cnn_gru_pd, on = ["Patient"], how = "left")
combinedInput_pd.drop(["Patient"], axis = 1, inplace = True)
combinedInput_np = np.array(combinedInput_pd)
combinedInput = tf.convert_to_tensor(combinedInput_np)
# being our regression head
x = Dense(8, activation="relu")(combinedInput)
x = Dense(1, activation="relu")(x)
model = Model(inputs=[tab_dt, cnn_gru.input], outputs=x)
I am getting the below error for eval function in the line "cnn_gru_np = cnn_gru.output.eval()"
ValueError: Cannot evaluate tensor u`enter code here`sing `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`
Please help with suggesting what is going wrong here.
The reason you're getting a ValueError is that the output of a keras model isn't an eager tensor, and thus does not support eval like that.
Just try
some_model = keras.Sequential([keras.layers.Dense(10, input_shape=(5,))])
print(type(some_model.output))
print(type(tf.zeros((2,))))
some_model.output.eval()
# <class 'tensorflow.python.framework.ops.Tensor'>
# <class 'tensorflow.python.framework.ops.EagerTensor'>
# ValueError
However, there is a bigger problem with your approach: there is no connected computation graph from your models inputs to your models outputs because none of the pandas stuff are tensorflow ops. I.E. even if you were able to use eager tensors, you still wouldn't be able to train your model with automatic differentiation.
You're going to have to specify your entire model in tf I'm afraid.
Maybe you could do the data processing before giving it as input to the model? Then you only need split concat ops to put everything together?

tensorflow core dumped after check failed

System information
**I leveraged keras using TensorFlow backend to train a batch of 300*300*3 rgb image.
Linux CentOS 7:
TensorFlow installed from binary:
TensorFlow version 1.4/1.5:
Python version 3.6:
Describe the problem
I used the following code to build CNN model
model = Sequential()
model.add(Convolution2D(32, (5, 5), activation='relu', input_shape=(3, height, width), data_format='channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=36, kernel_size=(5, 5), activation='relu'))
model.add(Conv2D(32, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
batch_steps = get_training_data_batch()
model.fit_generator(training_data_generator(), steps_per_epoch=batch_steps, epochs=15, verbose=2)
model.summary()
x_test, y_test = get_testing_data()
score = model.evaluate(x_test, y_test, verbose=0)
print("score:", score[1])
batch_size = 2000
def training_data_generator():
for benchmark_num in training_benchmark_num_list:
x_data, y_data = get_feature_data(benchmark_num)
samples_totals = x_data.shape[0]
print("samples totals:", samples_totals)
batch = samples_totals / batch_size + 1
for x in range(0, int(batch), 1):
x_sub_data = x_data[x*batch_size:(x+1)*batch_size]
y_sub_data = y_data[x*batch_size:(x+1)*batch_size]
x_train = x_sub_data.reshape(x_sub_data.shape[0], 3, height, width)
x_train = x_train.astype('float32')
x_train /= 255
y_train = np_utils.to_categorical(y_sub_data, 2)
yield (x_train, y_train)
Program shows the error before code dumped:
F tensorflow/core/kernels/maxpooling_op.cc:177]
Check failed: input_backprop_index >= in_start && input_backprop_index < in_end Invalid input backprop index: -1491167680, 2803712000, 2806515712
I traced tensorflow source code, it should be check operation. And backprop index can't be negative. I don't know tensorflow well, why can appear such problem? I think this error is root cause of code dumped. Could you help me solve this problem?