image recognition using hybrid cnn+svm model - python-3.x

To recognize the image I am using cnn+svm hybrid model.
model:
model.add(Conv2D(20, (5, 5), padding="same",input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(500))
model.add(Activation('relu'))
model.add(Dense((classes), kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('linear'))
compiling model:
opt = Adam(lr=lr, decay=lr / epochs)
model.compile(loss="categorical_hinge", optimizer=opt, metrics=["accuracy"])
Is this correct? When the training process, the accuracy not improving ....
what is the wrong with this code...

Related

Is there any way to obtain features of any layer from autoencoder in Keras? [duplicate]

This question already has answers here:
Obtaining output of an Intermediate layer in TensorFlow/Keras
(2 answers)
Closed 1 year ago.
Hi Guys I am working with autoencoders, I am trying to get the features from a specific layer of autoencoder ( I am not interested in the latent space). I am using the following code:
#Define autoencoder
import keras
input_shape = (1, 512, 512, 1)
SIZE = 512
encoder = keras.models.Sequential()
encoder.add(keras.layers.Conv2D(32, (9, 9), activation='elu', padding='same', input_shape=(SIZE, SIZE, 1)))
encoder.add(keras.layers.BatchNormalization())
encoder.add(keras.layers.Conv2D(64, (7, 7), activation='elu', padding='same'))
encoder.add(keras.layers.BatchNormalization())
encoder.add(keras.layers.Conv2D(32, (5, 5), activation='elu', padding='same'))
encoder.add(keras.layers.MaxPooling2D((2, 2), padding='same'))
encoder.add(keras.layers.BatchNormalization())
encoder.add(keras.layers.Conv2D(32, (3, 3), activation='elu', padding='same'))
encoder.add(keras.layers.MaxPooling2D((2, 2), padding='same'))
encoder.add(keras.layers.BatchNormalization())
#Decoder
decoder = keras.models.Sequential()
decoder.add(keras.layers.Conv2D(32, (3, 3), activation='elu', padding='same'))
decoder.add(keras.layers.UpSampling2D((2, 2)))
decoder.add(keras.layers.BatchNormalization())
decoder.add(keras.layers.Conv2D(32, (5, 5), activation='elu', padding='same'))
decoder.add(keras.layers.UpSampling2D((2, 2)))
decoder.add(keras.layers.BatchNormalization())
decoder.add(keras.layers.Conv2D(64, (7, 7), activation='elu', padding='same'))
#decoder.add(keras.layers.UpSampling2D((2, 2)))
decoder.add(keras.layers.BatchNormalization())
decoder.add(keras.layers.Conv2D(32, (9,9), activation='elu', padding='same'))
#decoder.add(keras.layers.UpSampling2D((2, 2)))
decoder.add(keras.layers.BatchNormalization())
#decoder.add(keras.layers.Conv2D(64, (11,11), activation='elu', padding='same'))
#decoder.add(keras.layers.UpSampling2D((2, 2)))
#decoder.add(keras.layers.BatchNormalization())
decoder.add(keras.layers.Conv2D(1, (3, 3), activation='elu', padding='same'))
autoencoder = keras.models.Sequential([encoder,decoder])
autoencoder.compile(loss='mean_squared_error', optimizer = "adam")
autoencoder.summary()
Finally, I train the autoencoder:
model_train = autoencoder.fit(X_train_noise, X_train,
epochs=5000,
shuffle=True)
Now, I need to enter a new image but I only want the outputs from the third layer from the encoder part of the defined CNN. Any ideas?
Thanks!!!
In Sequential model you can get any layer output by the model.layers[index] or model.get_layer(layer_name).
For example for the third layer output:
features_for_third_layer = encoder.layers[2].output
or
features_for_third_layer = autoencoder.layers[0].layers[2].output
After training autoencoder, if you want just the output of any arbitrary layer of encoder, and you want to feed an image and get the output, one easy way is to define another model like this:
new_model = keras.models.Model(inputs=encoder.input, outputs=encoder.layers[2].output)
Then, you can get the output like this:
feature_vector = new_model.predict([image])

Concatenate in Sequential KERAS U-NET

I want set up a Keras Sequential Deep UNET, but I dont know how to concatenate
specific layers.
from keras import models
from keras import layers
from keras.layers.convolutional import Conv2D, Conv2DTranspose
model = models.Sequential()
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same', input_shape=(512, 512, 4)))
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(32,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(32,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(Conv2DTranspose(8, (2, 2), strides=(2, 2), padding='same'))
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(classes,(3,3), activation="relu", padding='same'))
model = model.compile(optimizer=optimizer,loss=loss, metrics=['accuracy'])
model.summary()
In a non Sequential Model it would be something like that
u6 = Conv2DTranspose(n_filters * 8, (3, 3), strides = (2, 2), padding = 'same')(c5)
u6 = concatenate([u6, c4])
u6 = Dropout(dropout)(u6)
c6 = conv2d_block(u6, n_filters * 8, kernel_size = 3, batchnorm = batchnorm)

Error when checking input: expected conv2d_19_input to have 4 dimensions, but got array with shape (274, 1) deep learning

this is my code with error
def createModel():
model = Sequential()
# first set of CONV => RELU => MAX POOL layers
model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=inputShape))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))
# returns our fully constructed deep learning + Keras image classifier
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
# use binary_crossentropy if there are two classes
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
return model
ValueError: Error when checking input: expected conv2d_19_input to have 4 dimensions, but got array with shape (274, 1)

How can I use dropout in keras

An error occurred while cnn modeling.
When using dropout, the following error message occurs.
this is error message
UnboundLocalError: local variable 'a' referenced before assignment
model
def getModel(input_shape,filter_size=32,pool_size=(2,2),dropout=0.2):
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=input_shape, activation='elu', kernel_initializer="he_normal", padding='same', kernel_regularizer=regularizers.l2(0.01)))
I want to use dropout after maxpooling
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(16, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
this is flatten area
model.add(Flatten())
model.add(Dense(126, kernel_initializer="glorot_normal" ,kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('tanh'))
model.add(Dense(classes))
model.add(Activation('sigmoid'))
complile
model.compile(loss='categorical_crossentropy',
optimizer='adadelta', #SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
metrics=['accuracy'])
return model
model fit
np.random.seed(42)
hist = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, verbose = 1, validation_split = .2)
I couldn't figure out what is 'a' here and hence the error,but I think following code should help:
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal",padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

how to build a model for 1 dimension data (data after pca/zca process) in keras?

I'm trying to make a binary classification model for color images,
which contains to classes : a.intact b. damaged
I build a model like below:
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=X_train.shape[1:]
))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.35))
model.add(Conv2D(32, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, name = 'output')) #output
model.add(Activation('softmax'))
Since I didn't get expected result.
I tried saliency map and it shown that the model didn't
detect the defects at all.
After different image process like grayscale or histgram equalization
and random augmentation method.
It still not working at all.
Now I want to give a try on PCA/ZCA process to my data.
this is PCA/ZCA code I found from internet:
from sklearn.decomposition import PCA
pca = PCA(n_components=150, whiten=True, random_state=42)
n_samples = len(X_train)
X_train = X_train.reshape((n_samples, -1))
X_train_pca = pca.fit_transform(X_train)
n_samples = len(X_test)
X_test = X_test.reshape((n_samples, -1))
X_test_pca = pca.transform(X_test)
pca = PCA(whiten=True)
transformed = pca.fit_transform(X_train)
pca.whiten = False
zca = pca.inverse_transform(transformed)
Since it transform my data into a 1 dimension array,
I got this ERROR message:
ValueError: Input 0 is incompatible with layer conv1: expected ndim=4, found ndim=2
Any idea to build appropriate model or any way to modify the original one to fit
my data in ?
Any suggestion will be appreciated.

Resources