I want to apply augmentation on the traning dataset.But i am recieving an error with the system is crushed after it used the available RAM - tensorflow

I am using Resent50 model to classify ultrasound images in to fatty liver and normal liver.while training the model i have received high accuracy on the training set than validation set.The model is suffering from over fitting due to small dateset and imbalanced datasets between normal and abnormal classes.to deal this i want to apply data augmentation but the system doesn't allow me to apply on the training batches.It displays an error "the system is crushed after using all the available memory" .would you pleas help me how i can apply data augmentation with the code I have attached below?
typimport PIL
print('Pillow Version:', PIL.__version__)
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import cv2
from google.colab import drive
drive.mount('/content/drive')
train_dataset='/content/drive/MyDrive/preprocessed us image V2/train'
test_dataset='/content/drive/MyDrive/preprocessed us image V2/test'
validation_dataset='/content/drive/MyDrive/preprocessed us image V2/validation'
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import glob
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
%matplotlib inline
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_dataset, target_size=(224,224), classes=['train_abnormal', 'train_normal'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=validation_dataset, target_size=(224,224), classes=['val_abnormal', 'val_normal'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_dataset, target_size=(224,224), classes=['test_abnormal', 'test_normal'], batch_size=10, shuffle=False)
imgs, labels = next(train_batches)
def plotImages(images_arr):
fig, axes = plt.subplots(1, 10, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
plotImages(imgs)
print(labels)
from keras.applications import ResNet50
from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
IMG_SHAPE = (224,224, 3)
resnet_50 = ResNet50(weights = 'imagenet',
include_top = False,
input_shape = (224,224, 3))
for layer in resnet_50.layers:
layer.trainable = False
print("number of layers:", len(resnet_50.layers))
resnet_50.summary()
#Adding custom Layers
x = resnet_50.output#takes the out put of resnet50 model as input to the archetectur
x = GlobalAveragePooling2D()(x)#applies a 2D a global averaging layer
x = Dense(1024, activation="relu")(x)#densely connected neurones with unit 1024
x = Dropout(0.2)(x)#prevents the model from oerfitting by dropping some neurons/layers
x = Dense(512, activation="relu")(x)#densely connected neurones with unit 512
predictions = Dense(2, activation="softmax")(x)#calculates the probability distribution of the two classses using softmax function
from tensorflow.keras.models import Model
# creating the final model
model_ = Model(inputs=resnet_50.input, outputs=predictions)
# Lock initial layers to do not be trained
#for layer in model_.layers[:52]:
#layer.trainable = False
num_classes = 2
model_.compile(optimizer='adam', loss = 'categorical_crossentropy',metrics = ['accuracy'])
history = model_.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=30,
verbose=2)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0,1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
test_imgs, test_labels = next(test_batches)
plotImages(test_imgs)
print(test_labels)
#one_hot encoding
test_batches.classes#since our test dataset is not shuffeled we want to hae direct mapping between samples and labeles
predictions = model_.predict(x=test_batches,verbose=0) #verbose=0(to get no output whenever we run the predictions)
np.round(predictions)
cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
test_batches.class_indices
cm_plot_labels = ['abnormal','normal']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
plt.ylabel('true label')
plt.xlabel('predicted label')
loss, acc = model_.evaluate(test_batches)
model_.save('models/medical_trial_model.h5')
from tensorflow.keras.models import load_model
new_model = load_model('models/medical_trial_model.h5')
new_model.summary()
new_model.optimizer
from keras import utils as np_utils
datagen = keras.preprocessing.image.ImageDataGenerator(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
validation_split=0.2)
#datagen.fit(train_batches)
#gen = ImageDataGenerator(rotation_range=0.2, horizontal_flip=True)
chosen_image = random.choice(os.listdir('/content/drive/MyDrive/train_dataset/normal'))
image_path = '/content/drive/MyDrive/train_dataset/normal/' + chosen_image
image = tf.expand_dims(plt.imread(image_path),0)
plt.imshow(image[0])
aug_iter = datagen.flow(image)
#datagen.fit(train_batches)
aug_images = [next(aug_iter)[0] for i in range(10)]
plotImages(aug_images)
aug_iter = datagen.flow(image, save_to_dir='/content/drive/MyDrive', save_prefix='aug-image-', save_format='png')
train_batches=aug_iter
history = model.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=10,
verbose=2
)
datagen.fit(train_batches)
x_train, y_train), (x_test, y_test) =
import PIL
print('Pillow Version:', PIL.__version__)
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import cv2
from google.colab import drive
drive.mount('/content/drive')
train_dataset='/content/drive/MyDrive/preprocessed us image V2/train'
test_dataset='/content/drive/MyDrive/preprocessed us image V2/test'
validation_dataset='/content/drive/MyDrive/preprocessed us image V2/validation'
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import glob
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
%matplotlib inline
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_dataset, target_size=(224,224), classes=['train_abnormal', 'train_normal'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=validation_dataset, target_size=(224,224), classes=['val_abnormal', 'val_normal'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_dataset, target_size=(224,224), classes=['test_abnormal', 'test_normal'], batch_size=10, shuffle=False)
imgs, labels = next(train_batches)
def plotImages(images_arr):
fig, axes = plt.subplots(1, 10, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
plotImages(imgs)
print(labels)
from keras import utils as np_utils
datagen = keras.preprocessing.image.ImageDataGenerator(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
validation_split=0.2)
train_generator=datagen.fit(train_batches)
from keras.applications import ResNet50
from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
IMG_SHAPE = (224,224, 3)
resnet_50 = ResNet50(weights = 'imagenet',
include_top = False,
input_shape = (224,224, 3))
for layer in resnet_50.layers:
layer.trainable = False
print("number of layers:", len(resnet_50.layers))
resnet_50.summary()
#Adding custom Layers
x = resnet_50.output#takes the out put of resnet50 model as input to the archetectur
x = GlobalAveragePooling2D()(x)#applies a 2D a global averaging layer
x = Dense(1024, activation="relu")(x)#densely connected neurones with unit 1024
x = Dropout(0.2)(x)#prevents the model from oerfitting by dropping some neurons/layers
x = Dense(512, activation="relu")(x)#densely connected neurones with unit 512
predictions = Dense(2, activation="softmax")(x)
from tensorflow.keras.models import Model
# creating the final model
model_ = Model(inputs=resnet_50.input, outputs=predictions)
# Lock initial layers to do not be trained
#for layer in model_.layers[:52]:
#layer.trainable = False
num_classes = 2
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=10,
verbose=2)
test_imgs, test_labels = next(test_batches)
plotImages(test_imgs)
print(test_labels)
#one_hot encoding
test_batches.classes#since our test dataset is not shuffeled we want to hae direct mapping between samples and labeles
predictions = model.predict(x=test_batches,verbose=0) #verbose=0(to get no output whenever we run the predictions)
np.round(predictions)
cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
test_batches.class_indices
cm_plot_labels = ['abnormal','normal']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
plt.ylabel('true label')
plt.xlabel('predicted label')
loss, acc = model.evaluate(test_batches)
model.save('models/medical_trial_model.h5')
from tensorflow.keras.models import load_model
new_model = load_model('models/medical_trial_model.h5')
new_model.summary()
new_model.optimizer
)
I have tried different techniques like apply augmentation on the single image and then to the whole datasets but it doesn't work.

Related

the model that am trying to classify ultrasound images is getting a climax 100% accuracy for both traning and validation with out increasing gradually

I am using an ultrasound images datasets to classify normal liver an fatty liver.I have a total of 550 images.every time i train this code i got an accuracy of 100 % for both my training and validation at first iteration of the epoch.I do have 333 images for class abnormal and 162 images for class normal which i use it for training and validation.the rest 55 images (18 normal and 37 abnormal) for testing.here is th link for the dataset https://github.com/humedeg/amid
below i have attached the code for the classification of two classes.
import PIL
print('Pillow Version:', PIL.__version__)
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import cv2
from google.colab import drive
drive.mount('/content/drive')
train_dataset='/content/drive/MyDrive/prepocessed_US_image/train_N vs F'
test_dataset='/content/drive/MyDrive/prepocessed_US_image/test N vs F'
validation_dataset='/content/drive/MyDrive/prepocessed_US_image/val N vs F'
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import glob
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
%matplotlib inline
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_dataset, target_size=(224,224), classes=['train_abnormal', 'train_normal'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=validation_dataset, target_size=(224,224), classes=['val_abnormal', 'val_normal'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_dataset, target_size=(224,224), classes=['test_abnormal', 'test_normal'], batch_size=10, shuffle=False)
imgs, labels = next(train_batches)
def plotImages(images_arr):
fig, axes = plt.subplots(1, 10, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
plotImages(imgs)
print(labels)
from keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten, Activation
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras import backend as K
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
datagen.fit(train_batches)
import keras.utils as image
train_img = train_X[10]
img = image.img_to_array(train_img) # convert image to numpy arry
img = img.reshape((1,) + img.shape) # reshape image
i = 0
for batch in datagen.flow(img, save_prefix='train', save_format='png'): # this loops runs forever until we break, saving images to current directory with specified prefix
plt.figure(i)
plot = plt.imshow(image.img_to_array(batch[0]))
i += 1
if i > 4: # show 4 images
break
plt.show()
from keras.applications import InceptionV3
from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
IMG_SHAPE = (299,299, 3)
inc_model = InceptionV3(weights = 'imagenet',
include_top = False,
input_shape = (299,299, 3))
for layer in inc_model.layers:
layer.trainable = False
print("number of layers:", len(inc_model.layers))
inc_model.summary()
# Here we freeze the last 4 layers
# Layers are set to trainable as True by default
#Adding custom Layers
x = inc_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.2)(x)
x = Dense(512, activation="relu")(x)
predictions = Dense(2, activation="softmax")(x)
print(x)
from tensorflow.keras.models import Model
# creating the final model
model_ = Model(inputs=inc_model.input, outputs=predictions)
# Lock initial layers to do not be trained
#for layer in model_.layers[:52]:
#layer.trainable = False
num_classes = 2
model_.compile(optimizer='adam', loss = 'categorical_crossentropy',metrics = ['accuracy'])
history = model_.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=10,
verbose=2)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0,1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
test_dataset='/content/drive/MyDrive/prepocessed_US_image/test N vs F'
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_dataset, target_size=(224,224), classes=['test_abnormal', 'test_normal'], batch_size=10, shuffle=False)
def plotImages(images_arr):
fig, axes = plt.subplots(1, 10, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
test_imgs, test_labels = next(test_batches)
plotImages(test_imgs)
print(test_labels)
test_batches.classes
predictions = model_.predict(x=test_batches,verbose=0)
np.round(predictions)
from sklearn.metrics import confusion_matrix
import itertools
cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
test_batches.class_indices
cm_plot_labels = ['abnormal','normal']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
plt.ylabel('true label')
plt.xlabel('predicted label')
[result of the traning](https://i.stack.imgur.com/23lhN.png)
`

Keras CNN predicts 2 classes out of 4

I have a problem about my CNN model made using tensorflow. The goal is to predict the classes of satellite images, corresponding to the type of clouds (data extracted from the kaggle competition "Planet: Understanding the Amazon from Space"). There are 4 classes : clear, cloudy, partly cloudy and haze.
Everything works fine until I try to test the model on individual images. Then, it always predicts 2 classes and nothing else. I noticed that if I run the model again, it may predict 2 other classes among the 4. The model was trained for 10 epochs, which gave an accuracy of 0.8717.
Here is my code :
import numpy as np
import pandas as pd
import cv2
from tqdm import tqdm
import h5py
import os
os.listdir("/kaggle/input/")
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense,MaxPooling2D,Conv2D,Flatten,Dropout,Activation
from tensorflow.keras.layers import BatchNormalization
from sklearn import svm
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from oauth2client.client import GoogleCredentials
import csv
#from keras.optimizers import RMSprop
from tensorflow.keras import Input, Model
batch_size = 128
img_width = 256
img_height = 256
train_data = ImageDataGenerator(
rescale = 1./255,
validation_split = 0.25)
train_generator = train_data.flow_from_directory(
'../input/clouds',
target_size=(img_height, img_width),
color_mode='rgb',
batch_size=batch_size,
shuffle = True,
class_mode="categorical",
subset = 'training'
)
valid_generator = train_data.flow_from_directory(
'../input/clouds',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
subset = 'validation'
)
num_classes = 4
model = Sequential([
Input(shape = [img_width, img_height, 3]),
Conv2D(128,4,activation = 'relu'),
MaxPooling2D(),
Conv2D(64,4,activation = 'relu'),
MaxPooling2D(),
Conv2D(32,4, activation = 'relu'),
MaxPooling2D(),
Conv2D(16,4,activation = 'relu'),
MaxPooling2D(),
Flatten(),
Dense(64, activation = 'relu'),
Dense(num_classes, activation = 'softmax')
])
model.compile(optimizer = "adam",
loss = 'categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_generator, validation_data = valid_generator, epochs = 10)
img_to_predict = cv2.imread('/kaggle/input/clouds-test/clouds_test/test_3877_6013089.jpg') #an augmented image from original dataset
img_to_predict = cv2.cvtColor(img_to_predict, cv2.COLOR_BGR2RGB)
img_to_predict = np.expand_dims(cv2.resize(img_to_predict, (256,256)), axis = 0)
res = model.predict(img_to_predict)
label_map = (train_generator.class_indices)
print(label_map)
print(list(label_map)[np.argmax(res, axis = -1)[0]])
Thank you for you help.

CNN Model Predicting Only First Class

I am working on a fine-grained classification to classify car models. So I have used transfer learning ResNet50. As per my knowledge it is performing fine while training. But when I try new images it is always predicting a single class. Below is my code.
For training:
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
import numpy as np
from glob import glob
IMAGE_SIZE = [224, 224]
train_path = 'Datasets/train'
valid_path = 'Datasets/test'
resnet = ResNet50(input_shape = IMAGE_SIZE + [3], weights='imagenet', include_top = False)
for layer in resnet.layers:
layer.trainable = False
folders = glob('Datasets/train/*') #training folders
x = Flatten()(resnet.output)
prediction = Dense(len(folders), activation='softmax') (x)
model = Model(inputs = resnet.input, outputs = prediction)
model.compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy']
)
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('Datasets/train',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('Datasets/test',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=200,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
from tensorflow.keras.models import load_model
model.save('model_updateV1.h5')
y_pred = model.predict(test_set)
import numpy as np
y_pred = np.argmax(y_pred, axis=1)
For Trying New Images:
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from tensorflow.keras.applications.resnet50 import preprocess_input
model = load_model('model_updateV1.h5')
img = image.load_img('Datasets/test/mercedes/45.jpg', target_size=(224,224))
x = image.img_to_array(img)
x = x/255.
x = np.expand_dims(x, axis = 0)
img_data = preprocess_input(x)
img_data.shape
model.predict(img_data)
a = np.argmax(model.predict(img_data), axis=1)
a
I think your problem is that you are rescaling the images twice. You have code
x=x/255
then you expand the dimensions which is fine. However you then have code
img_data = preprocess_input(x)
The preprocess_input functon I believe rescales the pixel values between -1 and +1 with the code
x=x/127.5-1.
So now your pixel value have been scaled down twice. So just delete the code
x=x/255

InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Matrix size-incompatible: In[0]: [665,64], In[1]: [42560,1]

[[[{{node dense_22/MatMul}}]] [[dense_23/Sigmoid/_329]] (1) Invalid argument: Matrix size-incompatible: In[0]: [665,64], In[1]: [42560,1]
This all belongs to:y_score = model.predict(X_test, verbose = 1, batch_size = 256)
from warnings import warn
from abc import ABCMeta, abstractmethod
from tensorflow import keras, reshape
np.random.seed(1337) # for reproducibility
import keras
from keras.optimizers import RMSprop, SGD
from keras.models import Sequential, model_from_yaml
from keras.layers.core import Dense, Dropout, Activation, Flatten
import keras.layers.core as core
from keras.layers import Dense, Dropout, Embedding, LSTM, Input, merge, multiply, Reshape
from keras.layers.convolutional import Convolution1D, MaxPooling1D
from keras.layers.wrappers import Bidirectional
from keras.constraints import maxnorm
from keras.layers.recurrent import LSTM, GRU
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.layers import Embedding
from sklearn.metrics import fbeta_score, roc_curve, auc, roc_auc_score, average_precision_score
import matplotlib.pyplot as plt
from keras.regularizers import l2, l1, l1_l2
# from keras.models import Model
from tensorflow.keras.models import Model
# from keras import backend as K
import tensorflow.keras.backend as K
from keras.engine.topology import Layer
from keras import activations, initializers, regularizers, constraints
from keras.engine import InputSpec
from keras.layers import ActivityRegularization
class Attention(Layer):
def __init__(self,hidden,init='glorot_uniform',activation='linear',W_regularizer=None,b_regularizer=None,W_constraint=None,**kwargs):
self.init = initializers.get(init)
self.activation = activations.get(activation)
self.W_regularizer = regularizers.get(W_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.W_constraint = constraints.get(W_constraint)
self.hidden=hidden
super(Attention, self).__init__(**kwargs)
def build(self, input_shape):
input_dim = input_shape[-1]
self.input_length = input_shape[1]
self.W0 = self.add_weight(name ='{}_W1'.format(self.name), shape = (input_dim, self.hidden), initializer = 'glorot_uniform', trainable=True) # Keras 2 API
self.W = self.add_weight( name ='{}_W'.format(self.name), shape = (self.hidden, 1), initializer = 'glorot_uniform', trainable=True)
self.b0 = K.zeros((self.hidden,), name='{}_b0'.format(self.name))
self.b = K.zeros((1,), name='{}_b'.format(self.name))
print (modil.summary())
return modil
#for Testing only
def test(n_estimators = 16):
model = set_up_model_up()
X_test = np.load('/content/drive/MyDrive/X_test.npy', mmap_mode='r')
y_test = np.load('/content/drive/MyDrive/y_test.npy', mmap_mode='r')
ensemble = np.zeros(len(X_test))
for i in range(n_estimators):
print ('testing', i, 'model')
print ('model shape is', model.summary)
model.load_weights('/content/drive/MyDrive/model/bestmodel_split_chr_GD_'+ str(i) + '.hdf5')
print ('model shape after loading is', model.summary)
#Once the model is created, you can config the model with losses and metrics with model.compile(), train the model with model.fit(), or use the model to do prediction with model.predict().
print ('Predicting...')
# X_test=X_test.Reshape(665,-1)
print ('testing',X_test.shape)
print (len(model.layers))
# y_score = model.predict(np.expand_dims(np.array(X_test, dtype=np.float32), 0), verbose = 1, batch_size = 256)
formatmul= np.empty((3,2000,4), dtype=object)
for x in range(0, 2):
for y in range(0, 1999):
for z in range(0, 3):
formatmul[x][y][z]=X_test[x][y][z]
y_score = model.predict(X_test).reshape(665,-1), verbose = 1, batch_size = 256)
print("model.output_shape",model.output_shape)
print("model.input_shape",model.input_shape)
y_score = model.predict(formatmul, batch_size=42560)
y_score = model.predict(np.array(formatmul, dtype=np.float32), batch_size =665)
y_pred = []
for item in y_score:
y_pred.append(item[0])
y_pred = np.array(y_pred)
ensemble += y_pred
ensemble /= n_estimators
np.save('/content/drive/MyDrive/test_result/y_test', y_test)
np.save('/content/drive/MyDrive/test_result/y_pred', ensemble)
auroc = roc_auc_score(y_test, ensemble)
aupr = average_precision_score(y_test, ensemble)
print ('auroc', auroc)
print ('aupr' , aupr)
test(n_estimators = 16)

Reproduce same results on each run - Keras, Google Colab

I run the following code in Google Colab(with GPU):
import random
random.seed(1)
import numpy as np
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
import pandas as pd
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Flatten, Dense, Lambda, SimpleRNN
from keras.optimizers import *
from keras.utils import np_utils
from keras.initializers import *
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, roc_auc_score, auc, precision_recall_curve
from sklearn.metrics import confusion_matrix
from keras.callbacks import EarlyStopping
from keras import backend as K
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
##Loading dataset train and validation files, the files are same for every run
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)
print("***********************************************************************************************")
def make_model():
model = Sequential()
model.add(Conv2D(10,(5,5), kernel_initializer=glorot_uniform(seed=1), input_shape = (22,10,1), use_bias = True, activation = "relu", strides = 1, padding = "valid"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(20, kernel_initializer=glorot_uniform(seed=1), activation = "relu"))
model.add(Lambda(lambda x: tf.expand_dims(x, axis=1)))
model.add(SimpleRNN(20, kernel_initializer=glorot_uniform(seed=1), activation="relu",return_sequences=False))
model.add(Dense(1, kernel_initializer=glorot_uniform(seed=1), activation="sigmoid"))
opti = SGD(lr = 0.01)
model.compile(loss = "binary_crossentropy", optimizer = opti, metrics = ["accuracy"])
return model
model = make_model()
model.fit(x_train, y_train, validation_data = (x_validation,y_validation), epochs = 50, batch_size = 20, verbose = 2, callbacks=[es])
Despite setting all seed values, my prediction results of the model are different on subsequent runs. The training and testing of the model happens in the same Colab cell.
You are dealing with floating point numbers that are multiplied and added on different threads and can therefore happen in different order. Floating point additions and multiplications are not commutative. See What Every Computer Scientist Should Know About Floating-Point Arithmetic.