Siamese network stuck at 50% accuracy - python

I'm trying to build a Siamese network to do re-identification, the final output of this network should be feature vectors that are compared in order to decide if the input images are from the same class or not. The accuracy of my model is stuck at 50%. I tried multiple variations in the loss function, accuracy and in the layers of the model, but nothing seems to be working and my knowledge in the area stops me from trying another approach. What could possibly resolve this problem?
def cnn(in_dims):
model = Sequential()
model.add(Conv2D(8,(3,3),padding='same',input_shape=(in_dims[0],in_dims[1],in_dims[2]),activation='relu',name='conv1'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool1'))
model.add(Conv2D(16,(3,3),padding='same',activation='relu',name='conv2'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool2'))
model.add(Conv2D(32,(3,3),padding='same',activation='relu',name='conv3'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool3'))
model.add(Conv2D(64,(3,3),padding='same',activation='relu',name='conv4'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool4'))
model.add(Flatten(name='flatten'))
model.add(Dense(64,name='embeddings'))
return model
def double_loss(y_true, y_pred):
total_length = y_pred.shape.as_list()[-1]
image1 = y_pred[:,0:int(total_length*1/2)]
image2 = y_pred[:,int(total_length*1/2):int(total_length*2/2)]
dist = K.sum(K.square(image1 - image2), axis=1)
return dist
def accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
input_1 = Input(shape=(128,64,3,), name='input_1')
input_2 = Input(shape=(128,64,3,), name = 'input_2')
shared = cnn([128,64,3])
encoded_image1 = shared(input_1)
encoded_image2 = shared(input_2)
merged_vector = concatenate([encoded_image1, encoded_image2], axis=-1, name='merged_layer')
model = Model(inputs=[input_1, input_2], outputs=merged_vector)
model.compile(loss=double_loss, optimizer=Adam(lr = 0.0001, beta_1 = 0.9, beta_2 = 0.999), metrics=[accuracy])
history = model.fit([X_a_train, X_b_train], y=y_train,
batch_size=128, epochs=100, validation_data=[[X_a_val, X_b_val], y_val])

Related

Get GradCAM image for Mel-Spectrogram from CNN saved model in Python

After extracting the MFCCs via this code:
def extract_features(file_name):
try:
audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=nmfcc)
pad_width = max_pad_len - mfccs.shape[1]
mfccs = np.pad(mfccs, pad_width=((0, 0), (0, pad_width)), mode='constant')
except Exception as e:
print("Error encountered while parsing file: ", file_name)
return None
return mfccs
I pass the MFCCs through a shallow network (shown below) and then save the model:
# Construct model
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(num_rows, num_columns, num_channels), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
model.add(Dense(num_labels, activation='softmax'))
This is the model summary:
I recall the model via:
def print_prediction(file_name):
prediction_feature = extract_features(file_name)
prediction_feature = prediction_feature.reshape(1, num_rows, num_columns, num_channels)
model_path = os.path.join("ModelPath")
model = load_model(filepath)
predicted_vector = np.argmax(model.predict(prediction_feature), axis=-1)
predicted_class = le.inverse_transform(predicted_vector)
print('Filename: ', os.path.basename(file_name))
print("The predicted class is:", predicted_class[0])
predicted_proba_vector = model.predict(prediction_feature)
predicted_proba = predicted_proba_vector[0]
for i in range(len(predicted_proba)):
category = le.inverse_transform(np.array([i]))
print(category[0], "\t\t : ", format(predicted_proba[i], '.15f'))
print('\n')
return predicted_proba_vector
How can I go about getting a GradCAM image for my audio file that I pass into the CNN model?

Bespoke loss routine for Keras Model

I'm trying to implement a bespoke loss calculation model but keep getting hit with recognition failures. eg: "ValueError: Unknown loss function: root_mean_squared_error_fraction. Please ensure this object is passed to the custom_objects argument."
The function "root_mean_squared_fraction" exists and is functioning, I know this because I call it elsewhere outside of the Keras model and it functions as expected. I'm clearly not understanding something about injecting this into the model definition and would appreciate any advice? Thanks.
from keras.models import load_model
from keras import backend as K
def root_mean_squared_error_fraction(y_true, y_pred):
return K.sqrt(K.mean(K.square((y_pred - y_true)/y_true)))
This is my model routine which does work when the =rmsef in the model.compile is replaced with ='mse':
def ResModel(Svect, Xvect, Yvect, dtrain):
model = Sequential()
model.add(LSTM(64, activation='relu',\
input_shape=(Xvect.shape[1], Xvect.shape[2]),\
return_sequences=True))
model.add(LSTM(32, activation='relu', return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(Yvect.shape[1]))
rmsef = root_mean_squared_error_fraction
model.compile(optimizer = "adam", loss = rmsef )
bmfile = 'bestmodel.h5'
earlystop = EarlyStopping(monitor='val_loss', mode='auto',\
verbose=0, patience=mpat, min_delta=0.005)
chkpoint = ModelCheckpoint(bmfile, monitor = 'val_loss', mode = 'auto',\
save_best_only = True )
history = model.fit(Xvect, Yvect, epochs=mcycl, batch_size=32,\
validation_split=dsplit, verbose=0,\
callbacks = [earlystop, chkpoint] )
saved = load_model('bestmodel.h5')
score = saved.evaluate(Xvect, Yvect, verbose=0)
print("Epoch: %04d yeilded best fit with overall loss of: %0.4f "\
% ((earlystop.stopped_epoch + 1), score ) )
Yvect = descalevector(Svect, saved.predict(Xvect),dtrain )
return Yvect, score

How to reduce the error of tensorflow model?

Who can suggest how to improve the model?
The regular model in sklearn LinearRegression() predicts temperature with an error of 1 and the error of the model built manually on tensorflow won't drop below 5.5, no matter the activation function, the number of layers, or epochs.
The data was both standardized and derived into positive values
def createModelG(inputShape, dropout, initW):
model = Sequential()
model.add(Dense(4096,
kernel_regularizer=keras.regularizers.l2(0.001),
activation = 'elu',
kernel_initializer = initW,
input_dim = inputShape
))
model.add(Dropout(dropout))
#for i in range(3):
# model.add(Dense(512, activation = 'relu'))
# model.add(Dropout(dropout))
model.add(Dense(1024,
kernel_regularizer=keras.regularizers.l2(0.001),
activation = 'elu'
))
model.add(Dropout(dropout))
model.add(Dense(1))
model.compile(
loss = 'mae',
optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0000005),
metrics = ['mse', 'mae']
)
return model
startModelTest = crossValdation(createModelG, trainDataXS, 0.01, 'truncated_normal', 'VancouverT', PrintDot())
modelTest = startModelTest[1]
hist = startModelTest[2]
startModelTest[0]
loss mse mae val_loss val_mse val_mae
0 22.6255 737.889 21.3214 7.32549 55.3201 6.02149
1 21.6446 677.313 20.3387 7.83092 64.0345 6.5251
2 21.1013 646.857 19.7952 7.00224 49.6842 5.69622
3 22.3446 712.008 21.0386 8.07596 68.7968 6.77008
4 24.2565 874.824 22.9531 7.71605 65.3973 6.41274
0 --- --- --- --- --- ---
0 22.3945
link to all code and result of my keras model and ready sklearn models:
https://www.kaggle.com/alihanurumov/weather-prediction-network
def createModelG(inputShape):
model = Sequential()
model.add(Dense(4096, input_dim = inputShape,
kernel_initializer = initializers.glorot_uniform(seed = 1),
kernel_regularizer = keras.regularizers.l2(0.01), activation = "relu"))
model.add(Dense(2048,
kernel_initializer = initializers.glorot_uniform(seed = 1), activation = "relu"))
model.add(Dense(2048,
kernel_initializer = initializers.glorot_uniform(seed = 1), activation = "relu"))
model.add(Dense(1024,
kernel_initializer = initializers.glorot_uniform(seed = 1), activation = "relu"))
model.add(Dense(1024,
kernel_initializer = initializers.glorot_uniform(seed = 1), activation = "relu"))
model.add(layers.Dropout(0.05))
model.add(Dense(1))
optimizer = tf.keras.optimizers.Adam(learning_rate = 0.000001)
model.compile(loss = 'mse', optimizer = optimizer, metrics = ["mse", "mae"])
return model

How can i improve my accuracy of audio data classification

I need to classify if a sound is of a hammer or not.
The dataset contains 8000 .wav audio files(approx 1s each) for training and about 2000 for testing with 2 classes for classification(0 if the sound is not of a hammer or 1 if it is). I tried extracting features(mfcc and padding) and to build my network, but when I fit the model with the extracted data I cannot get over 70.5% accuracy.
What can I change or what else can I consider that would increase my accuracy? Also if my approach is flawed please tell me.
Here is the data extraction part:
def get_traing_features(row):
file_name = os.path.join(os.path.abspath(path_train), str(row[0]))
audio_train, sfreq_train = lb.load(file_name, res_type='kaiser_fast')
mfcc = lb.feature.mfcc(y=audio_train, sr=sfreq_train, n_mfcc=40)
pad_width = max_pad_len - mfcc.shape[1]
mfccs = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
feature = mfccs
label = row[1]
return pd.Series([feature, label])
def get_validation(row):
file_name = os.path.join(os.path.abspath(path_validation), str(row[0]))
audio_val, sfreq_val = lb.load(file_name, res_type='kaiser_fast')
mfcc = lb.feature.mfcc(y=audio_val, sr=sfreq_val, n_mfcc=40)
pad_width = max_pad_len - mfcc.shape[1]
mfccs = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
feature = mfccs
label = row[1]
return pd.Series([feature, label])
train_data= train_data.apply(parser, axis=1)
train_data.columns = ['feature', 'label']x = np.array(temp.feature.tolist())
x = np.array(train_data.feature.tolist())
y = np.array(train_data.label.tolist())
y_binary = to_categorical(y)
x = x.reshape(x.shape[0], numb_rows, numb_cols, num_channels)
test_data = validation.apply(get_validation, axis=1)
test_data.columns = ['feature', 'label']
x_val = np.array(temp_val.feature.tolist())
y_val = np.array(temp_val.label.tolist())
y_binary_val = to_categorical(y_val)
x_val = x_val.reshape(x_val.shape[0], numb_rows, numb_cols, num_channels)
And here is the CNN:
numb_rows = 40
numb_cols = 174
num_channels = 1
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(numb_rows, numb_cols, num_channels), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
checkpointer = ModelCheckpoint(filepath='./saved_models/weights.best.basic_cnn.hdf5', verbose=1, save_best_only=True)
model.fit(x, y_binary, batch_size=10, epochs=200, callbacks=[checkpointer], verbose=1)
y_pred = model.predict_classes(x_val)
acc = accuracy_score(y_val, y_pred)

Code is not executing but is not showing any errors either

I am developing a machine learning algorithm and my code looks like this:
The directories of the images
train_dir = '../input/train_images'
train_labels = pd.read_csv('../input/train.csv')
train_labels['diagnosis'] = train_labels['diagnosis'].astype(str)
train_labels["id_code"]=train_labels["id_code"].apply(lambda x:x+".png")
test_dir = '../input/test_images'
test_labels = '../input/test.csv'
Preprocessing
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,)
train_generator = train_datagen.flow_from_dataframe(
train_labels[:],
directory="../input/train_images",
x_col='id_code', y_col='diagnosis',
target_size=(150, 150),
color_mode='rgb',
class_mode='categorical',
batch_size=32,
shuffle=True,)
The model
def get_model():
model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(150,150,3)))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(5, activation='softmax'))
#Compile your model
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.Adam(),
metrics=['acc'])
return model
Training of the model using k-cross validation
k = 4
num_validation_samples = len(train_generator) // k
np.random.shuffle(train_generator)
validation_scores = []
for fold in range(k):
print('processing fold #:', fold)
validation_data = train_generator[num_validation_samples * fold: num_validation_samples * (fold + 1)]
validation_targets = train_labels[num_validation_samples * fold: num_validation_samples * (fold + 1)]
training_data = np.concatenate([train_generator[:num_validation_samples * fold], train_generator[num_validation_samples * (fold + 1) : ]], axis = 0)
training_targets = np.concatenate([train_labels[:num_validation_samples * fold], train_labels[num_validation_samples * (fold + 1) :]], axis = 0)
model = get_model()
#Run the model
model.fit_generator(
training_data,
training_targets,
steps_per_epoch=30,
epochs=30,
batch_size = 20,
verbose = 0)
#Validate the model
val_loss, val_acc = model.evaluate(validation_data, validation_targets, verbose=0)
validation_scores.append(val_loss)
Every part of this code works except for the training of the model part. It shows the execution symbol, that it is executing, but it never executes. I have waited hours, but nothing happens. I do not understand why. I will appreciate the help.

Categories