sklearn.exceptions.NotFittedError: This LabelEncoder instance is not fitted yet - python

I'm trying to run a voice recognition code from Github HERE that analyzes voice. There is an example in final_results_gender_test.ipynb that illustrates the steps both on the training and inference. So I copied and adjusted the inference part and came up with the following code that uses the trained model for just inference. But I'm not sure why I get this error, complaining This LabelEncoder instance is not fitted yet.
How to fix the problem? I'm just doing inference and why do I need the fit?
Traceback (most recent call last):
File "C:\Users\myname\Documents\Speech-Emotion-Analyzer-master\audio.py", line 53, in <module>
livepredictions = (lb.inverse_transform((liveabc)))
File "C:\Users\myname\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 272, in inverse_transform
check_is_fitted(self, 'classes_')
File "C:\Users\myname\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 914, in check_is_fitted
raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This LabelEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
Here is my copied/adjusted code from the notebook:
import os
from keras import regularizers
import keras
from keras.callbacks import ModelCheckpoint
from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D, Dense, Embedding, Input, Flatten, Dropout, Activation, LSTM
from keras.models import Model, Sequential, model_from_json
from keras.preprocessing import sequence
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
import librosa
import librosa.display
from matplotlib.pyplot import specgram
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
opt = keras.optimizers.rmsprop(lr=0.00001, decay=1e-6)
lb = LabelEncoder()
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("saved_models/Emotion_Voice_Detection_Model.h5")
print("Loaded model from disk")
X, sample_rate = librosa.load('h04.wav', res_type='kaiser_fast',duration=2.5,sr=22050*2,offset=0.5)
sample_rate = np.array(sample_rate)
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=13),axis=0)
featurelive = mfccs
livedf2 = featurelive
livedf2= pd.DataFrame(data=livedf2)
livedf2 = livedf2.stack().to_frame().T
twodim= np.expand_dims(livedf2, axis=2)
livepreds = loaded_model.predict(twodim, batch_size=32, verbose=1)
livepreds1=livepreds.argmax(axis=1)
liveabc = livepreds1.astype(int).flatten()
livepredictions = (lb.inverse_transform((liveabc)))
print(livepredictions)

Related

UnimplementedError: Graph execution error, in google Collab with keras

Hi guys im trying to do some AI text classification with Keras and is giving me this error. Probably my layers are bad or something like that but dont really know the "Unimplemented" error.
This is my code:
history = model.fit(X_train, y_train,
epochs=100,
verbose=True,
validation_data=(X_test, y_test),
batch_size=10)
The error is:
`
UnimplementedError: Graph execution error:
Detected at node 'binary_crossentropy/Cast' defined at (most recent call last)
`
DonĀ“t know why is this happening.
Rest of the code:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import RandomizedSearchCV
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
import os
# print(os.listdir("../input"))
plt.style.use('ggplot')
filepath_dict = {'films': 'reviews_filmaffinity.scv'}
df_list = []
for source, filepath in filepath_dict.items():
df = pd.read_table('reviews_filmaffinity.csv', sep='\|\|', header=0, engine='python')
df['source'] = source
df_list.append(df)
df = pd.concat(df_list)
df_films = df[df['source'] == 'films']
df_films['texto'] = df_films['review_title'] + ' ' + df_films['review_text']
sentences = df_films['texto'].values
df_films['polaridad'] = df['review_rate'].apply(lambda x: 'positivo' if x > 6
else ('negativo' if x < 4
else 'neutro'))
y = df_films['polaridad'].values
sentences_train, sentences_test, y_train, y_test = train_test_split(sentences, y, test_size=0.2, random_state=0)
vectorizer = CountVectorizer()
vectorizer.fit(sentences_train)
X_train = vectorizer.transform(sentences_train)
X_test = vectorizer.transform(sentences_test)
X_train
classifier = LogisticRegression()
classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)
print("Accuracy:", score)
input_dim = X_train.shape[1] # Number of features
model = Sequential()
model.add(layers.Dense(10, input_dim=input_dim, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
I searched online but i dont figured out how to fix that... its driving me crazy

How to import images from a folder and set part of them as testing data and part of them as training data?

I am trying to import all 60,000 images (50,000 for training and 10,000 for testing) inside a directory (the directory location is known) in Python for image classification using TensorFlow. I want to import all images and let them
path = /home/user/mydirectory
I try code:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from keras.utils import np_utils
from PIL import Image
import glob
image_list = []
for filename in glob.glob(r'/home/user/mydirectory*.gif'): #assuming gif
im=Image.open(filename)
image_list.append(im)
(x_train, y_train)=image_list()
(x_test, y_test)=image_list()
However, the error is TypeError: 'list' object is not callable...
In your code, you are trying to call the list as a function which is throwing the error.
I think the below code can help you to get two subsets for your data.
import numpy as np
import random
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from keras.utils import np_utils
from PIL import Image
import glob
image_list = []
for filename in glob.glob(r'/home/user/mydirectory*.gif'): #assuming gif
im=Image.open(filename)
image_list.append(im)
random.shuffle(image_list) # This line now has shuffled your list(inplace operation)
n = len(image_list)
train_data_len = int(n*0.83) # Roughly 50k images
train_data = images_list[:train_data_len] # getting images upto 50k index
test_data = images[train_data_len:] # getting rest of the images
There are other ways to process the data. Found this an easy one to begin with.
You should have
... = image_list
in the last 2 lines above instead of
... = image_list()
as it is a list, not a callable function.
You are calling list image_list() in the last and second last line. You are trying to split into train and label, but you don't have a category added to image_list.
You can create a folder with a category name; for example, suppose you have
two classes, dog and cat, then you can create training directory like:
home/usr/mydirectory/train/cat
home/usr/mydirectory/train/dog
and use Image Data Generator:
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set =train_datagen.flow_from_directory(
'home/usr/mydirectory/train/',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
Similarly, for test data, you can create a directory with test and store images with the category as the folder name.

How to test a neural network model without reloading it again and again

i have written a neural network code in jupyter notebook and once i run the program i can just test in another cell without needing to run the whole model again.Now how can i do the same if i write the code in some other text editors.
from __future__ import division, print_function
import pandas as pd
import numpy as np
import re
import nltk
from nltk.corpus import stopwords
from numpy import array
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers.core import Activation, Dropout, Dense
from keras.layers import Flatten
from keras.layers import GlobalMaxPooling1D
from keras.layers.embeddings import Embedding
from sklearn.model_selection import train_test_split
from keras.preprocessing.text import Tokenizer
import re
import string
data = pd.read_csv('totaldt.csv',encoding='UTF-8')
data.columns = ['Text', 'Label']
data.head()
data.Label.value_counts()
def remove_punct(text):
text_nopunct = ''
text_nopunct = re.sub('['+ string.punctuation +']', '', text)
return text_nopunct
data['Text_Clean'] = data['Text'].apply(lambda x: remove_punct(x))
from nltk import word_tokenize
tokens = [word_tokenize(sen) for sen in data.Text_Clean]
def lower_token(tokens):
return [w.lower() for w in tokens]
lower_tokens = [lower_token(token) for token in tokens]
from gensim import models
from keras.callbacks import ModelCheckpoint
from keras.layers import Dense, Dropout, Reshape, Flatten, concatenate, Input, Embedding
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from sklearn.model_selection import train_test_split
from gensim.models.wrappers import FastText
import numpy as np
import pandas as pd
import os
import collections
import re
import string
from gensim.models import Word2Vec
from gensim.models.wrappers import FastText
model = FastText.load_fasttext_format('cc.te.300.bin')
def get_average_word2vec(tokens_list, vector, generate_missing=False, k=300):
if len(tokens_list)<1:
return np.zeros(k)
if generate_missing:
vectorized = [vector[word] if word in vector else np.random.rand(k) for word in tokens_list]
else:
vectorized = [vector[word] if word in vector else np.zeros(k) for word in tokens_list]
length = len(vectorized)
summed = np.sum(vectorized, axis=0)
averaged = np.divide(summed, length)
return averaged
x = Dense(128, activation='relu')(lstm)
x = Dropout(0.2)(x)
preds = Dense(labels_index, activation='sigmoid')(x)
model = Model(sequence_input, preds)
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['acc'])
model.summary()
return model
model = recurrent_nn(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index)+1, EMBEDDING_DIM,
len(list(label_names)))
num_epochs = 4
batch_size = 34
hist = model.fit(x_train, y_tr, epochs=num_epochs, validation_split=0.1, shuffle=True, batch_size=batch_size)
#predictions = model.predict(test_cnn_data, batch_size=10, verbose=1)
#labels = ["pos", "neg"]
#prediction_labels=[]
#for p in predictions:
# prediction_labels.append(labels[np.argmax(p)])
#sum(data_test.Label==prediction_labels)/len(prediction_labels)
This is the testing part and i need to enter as many as sentences and test the model without re running again
s = list(input("enter sentence"))
test_sequence = tokenizer.texts_to_sequences(s)
test_cnn_dat = pad_sequences(test_sequence, maxlen=MAX_SEQUENCE_LENGTH)
prediction= model.predict(test_cnn_dat, batch_size=1 , verbose=1)
print(prediction)
Why not just add a while True: before your prediction portion of the code? This allows it to continuously loop and asks you for the next input sentence.

Converting TensorFlow models to TensorFlow lite to deploy on Android devices

I am working on a Machine Learning project which I need to run on an Android device. I am a complete newbie to both Machine Learning and TensorFlow and as a result I am having a really hard time since the past few weeks. From what I have learnt so far, I have to
Create a checkpoint file
Freeze the model
Convert to tflite
I have went through numerous tutorials (all of them work with image datasets) without any luck.
Here is my code
from __future__ import absolute_import, division, print_function, unicode_literals
import glob
import os
from keras.models import Sequential, load_model
import numpy as np
import pandas as pd
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
import matplotlib.pyplot as plt
import keras as k
try:
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow import keras
#from tensorflow.contrib import lite
#from tensorflow.python.compiler.tensorrt import trt_convert as lite
df = pd.read_csv("kidney2.csv")
df.head()
df.shape
columns_to_retain = ["sg", "al", "sc", "hemo", "pcv", "wbcc", "rbcc", "htn", "classification"]
df = df.drop([col for col in df.columns if not col in columns_to_retain], axis=1)
df = df.dropna(axis=0)
for column in df.columns:
if df[column].dtype == np.number:
continue
df[column] = LabelEncoder().fit_transform(df[column])
df.head()
X = df.drop(["classification"], axis=1)
y = df["classification"]
x_scaler = MinMaxScaler()
x_scaler.fit(X)
column_names = X.columns
X[column_names] = x_scaler.transform(X)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size= 0.2, shuffle=True)
def create_model():
model = Sequential()
model.add(Dense(256, input_dim=len(X.columns),
kernel_initializer=k.initializers.random_normal(seed=13), activation="relu"))
model.add(Dense(1, activation="hard_sigmoid"))
model.compile(loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy'])
return model
model=create_model()
model.summary()
#checkpoint_path='training_1/cp.ckp'
#checkpoint_dir=os.path.dirname(checkpoint_path)
#Create checkpoint callback
#cp_callback=tf.keras.callbacks.ModelCheckpoint(checkpoint_path,save_weights_only=True,verbose=1)
checkpoint_directory = "training_1"
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
#checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model)
#status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_directory))
#train_op = optimizer.minimize( ... )
#status.assert_consumed() # Optional sanity checks.
#with tf.compat.v1.Session() as session:
# Use the Session to restore variables, or initialize them if
# tf.train.latest_checkpoint returned None.
# status.initialize_or_restore(session)
# for _ in range(num_training_steps):
# session.run(train_op)
# checkpoint.save(file_prefix=checkpoint_prefix)
model=create_model()
history = model.fit(X_train, y_train, epochs=2000, batch_size=X_train.shape[0])
model.save("kidney_model.model")
model=create_model()
model.load_weights(checkpoint_directory)
I get the following error
OSError: Unable to open file (unable to open file: name = 'training_1', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)
I also tried to convert to tflite directly by using
saved_model_dir='kidney_model.model'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
But I get an error saying
OSError: SavedModel file does not exist at: kidney_model.model/{saved_model.pbtxt|saved_model.pb}
Is there any way I can convert this model to tflite?
I am completely lost. Any help is really appreciated.

How to view network weights and bias during training

I have the below code.
I would like to see how the weights and bias changes during training.
Ideally I would like to see it in tensorboard.
Would someone be able to show me how to do this.
from time import time
import numpy as np
import matplotlib.pyplot as plt
import keras
import tensorflow as tf
from keras.callbacks import TensorBoard
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
x = scaler.fit_transform(np.array([[1965.0], [1980.0]])).reshape(-1,1)
y = scaler.fit_transform(np.array([[320.0], [345.0]])).reshape(-1,1)
tensorboard = TensorBoard(log_dir='logs/{}'.format(time()), write_grads=True)
model = keras.Sequential([keras.layers.Dense(1, activation='linear')])
model.compile(optimizer='sgd',
loss="mean_squared_error")
model.fit(x=x, y=y, epochs=1000, callbacks=[tensorboard])
yHat = model.predict(x)
Based on the Keras documentation, all you need to do maybe is just run the command line:
tensorboard --logdir=logs
Notice that the logdir setting is pointing to the root of your log directory.

Categories

Resources