Getting same result for all predictions in cnn - python

This is my first time training a model in cnn and predicting results but I am getting same value for images I have input. Here is my code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.optimizers import Adam
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D,
GlobalAveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier=Sequential()
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train/=255
X_test/=255
number_of_classes = 10
Y_train = np_utils.to_categorical(y_train, number_of_classes)
Y_test = np_utils.to_categorical(y_test, number_of_classes)
classifier.add(Convolution2D(32,3,3,input_shape=
(28,28,1),activation='relu'))
classifier.add(BatchNormalization(axis=-1))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Convolution2D(32,3,3,activation='relu'))
classifier.add(BatchNormalization(axis=-1))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.add(Dense(output_dim=256,activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dense(output_dim=10,activation='softmax'))
classifier.compile(optimizer='adam',loss='categorical_crossentropy',metrics=
['accuracy'])
gen = ImageDataGenerator(rotation_range=8, width_shift_range=0.08,
shear_range=0.3, height_shift_range=0.08, zoom_range=0.08)
test_gen = ImageDataGenerator()
train_generator = gen.flow(X_train, Y_train, batch_size=64)
test_generator = test_gen.flow(X_test, Y_test, batch_size=64)
classifier.fit_generator(train_generator, steps_per_epoch=60000, epochs=1,
validation_data=test_generator, validation_steps=10000)
import cv2
image = cv2.imread("pitrain.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
ret,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)
#threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
im2,contours, hierarchy =
cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# get contours
# for each contour found, draw a rectangle around it on original image
for contour in contours:
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
# discard areas that are too large
if h>300 and w>300:
continue
# discard areas that are too small
if h<40 or w<40:
continue
# draw rectangle around contour on original image
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
image.shape
image=image[:,:,:1]
newimg = cv2.resize(image,(28,28))
img.shape
img = np.reshape(newimg,[1,28,28,1])
cv2.imshow("screen",img)
classifier.predict(img)
The output I am getting is array of zeros with 1 at third position.
This is where I copied the contour part from https://www.quora.com/How-do-I-extract-a-particular-object-from-images-using-OpenCV
Epoch is equal to 1 because I only wanted to test my model and still I got accuracy of above 99%

Related

Linking my own image dataset stored on google drive with CNN code

for the given code i want to use my own image folder(dataset) stored on google drive instead on mnist dataset. but i am unable to figure it out how to use it.
Any suggestion.
#importing the required libraries
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
#loading data
(X_train,y_train) , (X_test,y_test)=mnist.load_data()
#reshaping data
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1))
#checking the shape after reshaping
print(X_train.shape)
print(X_test.shape)
#normalizing the pixel values
X_train=X_train/255
X_test=X_test/255
I have tried this
path = "/content/drive/My Drive/ANN/seperate_ricepests/seperate_ricepests/database"
a = MasterImage(PATH = path, IMAGE_Size = 200)
#loading data
(X_train,y_train) , (X_test,y_test)=a.load_data()

what does "loss: 0.0000e+00 - acc: 1.0000 - val_loss: 0.0000e+00 - val_acc: 1.0000" mean?

I am trying to classify skin diseases into 4 classes. I tried transfer learning using VGG16. No matter what changes I make, its not classifying. Accuracy is 1 and loss is 0. But I think its not overfitting also, as by confusion matrix I got to know that its classifying everything as single class.
import json
import math
import os
import cv2
from PIL import Image
import numpy as np
from keras import layers
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.models import Model
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import cohen_kappa_score, accuracy_score
import scipy
import tensorflow as tf
from keras import backend as K
import gc
from functools import partial
from tqdm import tqdm
from sklearn import metrics
from collections import Counter
import json
import itertools
from keras.optimizers import Adam
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.layers import Dense, Dropout, Flatten
#loading data and data preparation
def Dataset_loader(DIR, RESIZE, sigmaX=10):
IMG = []
read = lambda imname: np.asarray(Image.open(imname).convert("RGB"))
for IMAGE_NAME in tqdm(os.listdir(DIR)):
PATH = os.path.join(DIR,IMAGE_NAME)
_, ftype = os.path.splitext(PATH)
if ftype == ".jpg":
img = read(PATH)
img = cv2.resize(img, (RESIZE,RESIZE))
IMG.append(np.array(img))
return IMG
eczema_train = np.array(Dataset_loader('/content/medical-image-analysis/train/Eczema Photos', 224))
melanoma_train = np.array(Dataset_loader('/content/medical-image-analysis/train/Melanoma Skin Cancer Nevi and Moles',224))
psoriasis_train = np.array(Dataset_loader('/content/medical-image-analysis/train/Psoriasis pictures Lichen Planus and related diseases',224))
#labelling
eczema_train_label = np.zeros(len(eczema_train))
melonoma_train_label = np.zeros(len(melanoma_train))
psoriasis_train_label = np.zeros(len(psoriasis_train))
X_train = np.concatenate((eczema_train, melanoma_train, psoriasis_train), axis=0,)
Y_train = np.concatenate((eczema_train_label, melonoma_train_label, psoriasis_train_label), axis=0,)
#train and evaluation split
X_train = (X_train-np.mean(X_train))/np.std(X_train)
X_train, X_test, Y_train, Y_test = train_test_split(
X_train, Y_train,
test_size=0.3,
random_state=1
)
X_test, X_val, Y_test, Y_val = train_test_split(
X_test, Y_test,
test_size=0.3,
random_state=1
)
s = np.arange(X_train.shape[0])
np.random.shuffle(s)
X_train = X_train[s]
Y_train = Y_train[s]
pre_trained_model = VGG16(input_shape=(224, 224, 3), include_top=False, weights="imagenet")
for layer in pre_trained_model.layers:
print(layer.name)
layer.trainable = False
print(len(pre_trained_model.layers))
last_layer = pre_trained_model.get_layer('block5_pool')
print('last layer output shape:', last_layer.output_shape)
last_output = last_layer.output
# Flatten the output layer to 1 dimension
x = layers.GlobalMaxPooling2D()(last_output)
# Add a fully connected layer with 512 hidden units and ReLU activation
x = layers.Dense(128, activation='softmax')(x)
# Add a dropout rate of 0.5
x = layers.Dropout(0.5)(x)
# Add a final sigmoid layer for classification
x = layers.Dense(4, activation='softmax')(x)
# Configure and compile the model
model = Model(pre_trained_model.input, x)
optimizer = Adam(lr=0.0001, beta_1=0.9, beta_2=0.9999, epsilon=None, decay=0.0, amsgrad=True)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rotation_range=60,
shear_range=0.2,
zoom_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
train_datagen.fit(X_train)
val_datagen = ImageDataGenerator()
val_datagen.fit(X_val)
batch_size = 64
epochs = 3
history = model.fit(train_datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data = val_datagen.flow(X_val, Y_val),
verbose = 1, steps_per_epoch=(X_train.shape[0] // batch_size),
validation_steps=(X_val.shape[0] // batch_size))
This is the code, can you please help to find out where we went wrong. Thanks in advance:)
#labelling
eczema_train_label = np.zeros(len(eczema_train))
melonoma_train_label = np.zeros(len(melanoma_train))
psoriasis_train_label = np.zeros(len(psoriasis_train))
All of these numpy arrays are filled with zeroes. No matter whether an image represents eczema, melanoma, or psoriasis, you define its ground-truth label as 0 and hence your model "learns" to unconditionally output 0. Make the labels for at least two of the classes nonzero.
Further, you are using a CategoricalCrossentropy, which is only suitable for one-hot label encodings or binary classification. Use a sparse categorical (or sparse softmax) crossentropy instead. (Note that there may be other issues in the code, but this one stands out as a common error which may be seen).

How to implement CAM without visualize_cam in this code?

I want to make Class activation map, so I have write the code
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=30, batch_size=256, shuffle=True, validation_split=0.3)
works fine, so I have imported visualize_cam module
from vis.visualization import visualize_cam
import matplotlib.pyplot as plt
import numpy as np
for i in range(10):
ind = np.where(y_test == i)[0][0]
plt.subplot(141)
plt.imshow(x_test_resized[ind].reshape((28,28)))
for j,modifier in enumerate([None, 'guided', 'relu']):
heat_map = visualize_cam(model, 4, y_test[ind], x_test_resized[ind], backprop_modifier=modifier)
plt.subplot(1,4,j+2)
plt.imshow(heat_map)
plt.show()
but the visualize_cam didn`t work well
I tried many times to fix the module but it doesn`t go well
(it depends on scipy which version is below 1.3. but )
so I have to implement cam without that module
Is there any solution to replace visualize_cam into other option to implement CAM?
Here is a scipy library independent implementation.
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu', name='final_conv')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=1, batch_size=256, shuffle=True, validation_split=0.3)
import numpy as np
import cv2
import io
import requests
from PIL import Image
import matplotlib.pyplot as plt
# Using Keras implementation from tensorflow
from tensorflow.python.keras import applications
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
from tensorflow.python.keras import backend as K
# Get the layer of the last conv layer
fianlconv = model.get_layer('final_conv')
# Get the weights matrix of the last layer
weight_softmax = model.layers[-1].get_weights()[0]
# Function to generate Class Activation Mapping
HEIGHT = 28
WIDTH = 28
def returnCAM(feature_conv, weight_softmax, class_idx):
size_upsample = (WIDTH, HEIGHT)
# Keras default is channels last, hence nc is in last
bz, h, w, nc = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = np.dot(weight_softmax[:, idx], np.transpose(feature_conv.reshape(h*w, nc)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
x = x_test_resized[0,:,:,0]
plt.imshow(x)
plt.show()
classes = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 0:'0'}
probs_extractor = K.function([model.input], [model.output])
features_conv_extractor = K.function([model.input], [fianlconv.output])
probs = probs_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blob = features_conv_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blobs = []
features_blobs.append(features_blob)
idx = np.argsort(probs)
probs = np.sort(probs)
for i in range(-1, -6, -1):
print('{:.3f} -> {}'.format(probs[0, i], classes[idx[0, i]]))
CAMs = returnCAM(features_blobs[0], weight_softmax, [idx[0, -1]])
heatmap = cv2.applyColorMap(cv2.resize(CAMs[0], (28, 28)), cv2.COLORMAP_JET)
result = heatmap[:,:,0] * 0.3 + x * 0.5
print(result.shape)
plt.imshow(result)
plt.show()
N.B: I'm plotting normalized images so the result isn't great, I also trained only for 1 epoch. For better results, you may try training more, change to appropriate color space.

Concatenate layer in Keras makes fitting fail

Whenever I concatenate the outputs of two layers (for example, because I want to use softmax on some outputs and another activation function on the rest), the network always fails to learn.
This is some example code to demonstrate the problem:
from tensorflow.keras.layers import Lambda, Input, Dense, Concatenate, Dropout, Reshape, \
Conv2D, Flatten, MaxPooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.losses import mse, categorical_crossentropy, binary_crossentropy
from tensorflow.keras.utils import plot_model, to_categorical
from tensorflow.keras import backend as K
from tensorflow.keras import optimizers
import numpy as np
import matplotlib.pyplot as plt
import argparse
import os
import pygameVisualise as pyvis
# MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
no_cls = max(y_train)+1
width = 20
extra_dims = True
image_size = x_train.shape[1]
original_dim = image_size * image_size
x_train = np.reshape(x_train, [-1, original_dim])
x_test = np.reshape(x_test, [-1, original_dim])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
y_train = to_categorical(y_train, num_classes=width if extra_dims else no_cls)
y_test = to_categorical(y_test, num_classes=width if extra_dims else no_cls)
hidden_dim = 512
batch_sz = 256
eps = 10
ins = Input(shape=(original_dim,))
x = Dense(hidden_dim)(ins)
cls_pred = Dense(no_cls, activation="softmax")(x)
other = Dense(width-no_cls)(x)
outs = Concatenate()([cls_pred, other])
encoder = Model(ins, outs if extra_dims else cls_pred, name="encoder")
encoder.summary()
def cust_loss_fn(y_true, y_pred):
return categorical_crossentropy(y_true[:no_cls], y_pred[:no_cls])
optimiser = optimizers.SGD(lr=0.003, clipvalue=0.1)
encoder.compile(optimizer=optimiser, loss=cust_loss_fn,
metrics=["accuracy"])
encoder.fit(x_train, y_train,
batch_size=batch_sz,
epochs=eps,
validation_data=(x_test, y_test))
score = encoder.evaluate(x_test, y_test)
print(score)
print(encoder.predict(x_train[0:10]))
With extra_dims = False, i.e. no concatenate layer, the network will consistently reach 88% accuracy in the 10 epochs. When it is True, the network will stay at around 8% accuracy and the loss will not drop at all during training.
Am I doing something wrong?

using keras with cifar10 dataset with vgg16 network model not learning properly?

I seem to have some issue getting proper results with keras, and I am not sure why I am always getting bad result, so I decided today to test it with something for certain should work, being the Cifar10 dataset, and vgg16 network.
#!/usr/bin/python
#
import warnings
from mpl_toolkits.mplot3d import Axes3D
from keras.utils import np_utils
from matplotlib import cm
from keras import metrics
import keras
from keras.layers import GlobalMaxPooling2D
from keras.layers import GlobalAveragePooling2D
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.imagenet_utils import _obtain_input_shape
from keras.engine.topology import get_source_inputs
from keras.models import Sequential
from keras.optimizers import SGD
import scipy
from keras.layers.core import Dense, Activation, Lambda, Reshape,Flatten
from keras.layers import Conv1D,Conv2D,MaxPooling2D, MaxPooling1D, Reshape
#from keras.utils.visualize_util import plot
from keras.utils import np_utils
from keras.layers.advanced_activations import LeakyReLU, PReLU
from keras.layers.advanced_activations import ELU
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import Dropout
from sklearn.preprocessing import MinMaxScaler
from keras import backend as K
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
from keras.layers.merge import Concatenate
from keras.models import load_model
from keras.utils import plot_model
from skimage.util.shape import view_as_blocks
from skimage.util.shape import view_as_windows
from keras.callbacks import ModelCheckpoint
import tensorflow as tf
from keras import backend as K
from keras.layers.local import LocallyConnected1D
from keras.datasets import mnist,cifar10
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 32, 32
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
#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], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
def fws():
#print "Inside"
# Params:
# batch , lr, decay , momentum, epochs
#
#Input shape: (batch_size,40,45,3)
#output shape: (1,15,50)
# number of unit in conv_feature_map = splitd
input = Input(shape=(img_rows,img_cols,3))
zero_padded_section = keras.layers.convolutional.ZeroPadding2D(padding=(20,17), data_format='channels_last')(input)
model = keras.applications.vgg16.VGG16(include_top = False,
weights = 'imagenet',
input_shape = (48,84,3),
pooling = 'max',
classes = 10)
model_output = model(input)
#FC
dense1 = Dense(units = 512, activation = 'relu', name = "dense_1")(model_output)
dense2 = Dense(units = 256, activation = 'relu', name = "dense_2")(dense1)
dense3 = Dense(units = 10 , activation = 'softmax', name = "dense_3")(dense2)
model = Model(inputs = input , outputs = dense3)
#sgd = SGD(lr=0.08,decay=0.025,momentum = 0.99,nesterov = True)
model.compile(loss="categorical_crossentropy", optimizer='adam' , metrics = [metrics.categorical_accuracy])
model.fit(x_train[:500], y_train[:500],
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test[:10], y_test[:10]))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
fws()
I am only using a 500/10 of the dataset, since it would take a while to train it... but based on the result i have now.. nothing has been learning.
7 epochs in and the cat.acc. is 0.1300 and val.acc 0.200 and has been that for 7 epochs now...
What is wrong?
Error being the number of steps_per_epoch not being equal to the full size..
that fixed things.

Categories