コード例 #1
0
def guess_intent(text):
    tokens = preprocess_input(text, 16)
    ids = get_id_vector(tokens, token_2_id)
    X = np.array([ids])
    y = model.predict(X)[0]
    intent_id = np.argmax(y)
    return intent_texts[intent_id]
コード例 #2
0
async def get_response(message):
    content = message.content
    tokenized_content = preprocess_input(content, 16)
    if (len(tokenized_content) > 16):
        await message.channel.send(random.choice(CONFUSED))
        return
    intent = guess_intent(message.content)
    if (intent == 'greet'):
        await message.channel.send(random.choice(GREETINGS))
        return
    if (intent == 'bye'):
        await message.channel.send(random.choice(BYES))
        return
    if (intent == 'affirmative' or intent == 'negative'):
        await message.channel.send(random.choice(CONFUSED))
        return
    slots = guess_slot(message.content)
    if (intent == 'definitionQuestion'):
        if (len(slots) != 1):
            await message.channel.send(random.choice(CONFUSED))
            return
        (subject, _) = slots[0]
        await answer_definition_question(message, subject)
    if (intent == 'propertyQuestion'):
        if (len(slots) != 2):
            await message.channel.send(random.choice(CONFUSED))
            return
        (subject, t1) = slots[0]
        (prop, t2) = slots[1]
        if (t1 == 'property'):
            subject, prop = prop, subject
        await answer_property_question(message, subject, prop)
コード例 #3
0
ファイル: mAP.py プロジェクト: zhiding/supreme-winner
def preprocess(img_path, crop=False, region=None):
    img = Image.open(img_path)
    if crop:
        img = img.crop(region)
    img = img.resize((IMAGE_WIDTH, IMAGE_HEIGHT))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    return img
コード例 #4
0
    def get_example(self, i):
        item = self.dataset_list[i]
        file_content = item.source_data[0].get_content()
        if isinstance(item.attributes['classification'], list):
            label = item.attributes['classification'][0]['label']
        else:
            label = item.attributes['classification']['label']
        file_like_object = io.BytesIO(file_content)
        img = self.read_image_as_array(file_like_object,
                                       target_size=(img_rows, img_cols))
        if img is None:
            return None
        img = preprocess_input(img)

        if self.train:
            img = img[:, :, ::-1]

        return img, int(label) - 1
コード例 #5
0
ファイル: model.py プロジェクト: manangoel99/EmotionDetector
    def predictFace(self, gray_image, face):
        emotion_target_size = self.emotion_classifier.input_shape[1:3]

        x1, x2, y1, y2 = apply_offsets(face_utils.rect_to_bb(face),
                                       self.emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]

        try:
            gray_face = cv2.resize(gray_face, (emotion_target_size))
        except:
            return None
        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        emotion_prediction = self.emotion_classifier.predict(gray_face)
        emotion_probability = np.max(emotion_prediction)
        emotion_label_arg = np.argmax(emotion_prediction)
        emotion_text = self.labels[emotion_label_arg]
        return emotion_text
コード例 #6
0
def handler(_iter, ctx):
    for img in _iter:
        img = Image.fromarray(img)
        img = img.resize((img_rows, img_cols))

        x = np.asarray(img)
        x = np.asarray(x, dtype=np.float32)
        x = np.transpose(x, (2, 0, 1))
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)

        with chainer.using_config('train', False):
            with chainer.using_config('enable_backprop', False):
                predict = F.softmax(model(x))
                result = predict[0].data
                if USE_GPU >= 0:
                    result = to_cpu(result)

        sorted_result = decode_predictions(result.tolist())
        yield {"result": sorted_result}
コード例 #7
0
def guess_slot(text):
    tokens = preprocess_input(text, 16)
    ids = get_id_vector(tokens, token_2_id)
    X = np.array([ids])
    y = model.predict(X)[0]
    slots = np.argmax(y, axis=1)
    result = []
    last_token = ''
    last_type = 0
    for i in range(16):
        token = tokens[i]
        slot_type = slots[i]
        if (token != null_token and slot_type != 0):
            if (slot_type == last_type):
                last_token += ' ' + token
            else:
                if (last_type != 0):
                    result.append((last_token, slot_texts[last_type]))
                last_token = token
                last_type = slot_type
        if (i == 15 and last_type != 0):
            result.append((last_token, slot_texts[last_type]))
    return result
コード例 #8
0
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()

early_stop = EarlyStopping('val_loss', patience=patience)
reduce_lr = ReduceLROnPlateau('val_loss',
                              factor=0.1,
                              patience=int(patience / 4),
                              verbose=1)
trained_models_path = base_path + '_mini_XCEPTION'
model_names = trained_models_path + '.{epoch:02d}-{val_accuracy:.2f}.hdf5'
model_checkpoint = ModelCheckpoint(model_names,
                                   'val_loss',
                                   verbose=1,
                                   save_best_only=True)
callbacks = [model_checkpoint, early_stop, reduce_lr]

faces, emotions = load_data()
faces = preprocess_input(faces)
num_samples, num_classes = emotions.shape
xtrain, xtest, ytrain, ytest = train_test_split(faces,
                                                emotions,
                                                test_size=0.2,
                                                shuffle=True)
model.fit_generator(data_generator.flow(xtrain, ytrain, batch_size),
                    steps_per_epoch=len(xtrain) / batch_size,
                    epochs=num_epochs,
                    verbose=1,
                    callbacks=callbacks,
                    validation_data=(xtest, ytest))
コード例 #9
0
from keras.preprocessing.sequence import pad_sequences
import pickle
import numpy as np

training_path = 'dataset/default_dataset_training.json'
testing_path = 'dataset/default_dataset_testing.json'

sentences, tokenized_sentences, intents, intent_texts, slots, slot_texts = load_data(
    training_path)
sentences_test, tokenized_sentences_test, intents_test, _, slots_test, __ = load_data(
    testing_path)

sentence_max_length = 16
token2id_path = 'token2id.pkl'
processed_sents = list(
    map(lambda x: preprocess_input(x, sentence_max_length), sentences))
processed_sents_test = list(
    map(lambda x: preprocess_input(x, sentence_max_length), sentences_test))
token_2_id = None
if (path.exists(token2id_path)):
    with open(token2id_path, 'rb') as file:
        token_2_id = pickle.load(file)
else:
    token_2_id = get_token_to_id_dict(processed_sents)
    with open(token2id_path, 'wb') as file:
        pickle.dump(token_2_id, file)
sent_ids = list(
    map(lambda sent: get_id_vector(sent, token_2_id), processed_sents))
sent_ids_test = list(
    map(lambda sent: get_id_vector(sent, token_2_id), processed_sents_test))
コード例 #10
0
def clicked():
        
        label = tk.Label(root)
        #load the file path
        image_path = askopenfilename()
        print image_path
        #open the image
        image_path1 = Image.open(image_path)
        image_path1 = image_path1.resize((200, 200), Image.ANTIALIAS)
        label.image_path1 = ImageTk.PhotoImage(image_path1)
        label['image'] = label.image_path1
        label.pack()

        detection_model_path = '/home/akanksha/code/trained_models/detection_models/haarcascade_frontalface_default.xml'
	emotion_model_path = '/home/akanksha/code/trained_models/emotion_modelsfer2013_mini_XCEPTION.86-0.43.hdf5'
	emotion_labels = get_labels('fer2013')

	#loading models
	face_detection = cv2.CascadeClassifier(detection_model_path)
	emotion_classifier = load_model(emotion_model_path, compile=False)

	# getting input model shapes for inference
	emotion_target_size = emotion_classifier.input_shape[1:3]



	# loading images
	pil_image = image.load_img(image_path, grayscale=True)
	gray_image = image.img_to_array(pil_image)
	gray_image = np.squeeze(gray_image)
	gray_image = gray_image.astype('uint8')

	faces = face_detection.detectMultiScale(gray_image, 1.3, 5)
	if (len(faces)==0):
		print("Please enter a valid image!")
		z = Label(root, text='Please enter a valid image!',background="white")
	        z.pack()
	
	var = 0
	for (x, y, w, h) in faces:
		gray_face = gray_image[y:y+w , x:x+h]
		gray_face = cv2.resize(gray_face, (emotion_target_size))
		gray_face = preprocess_input(gray_face)
		gray_face = np.expand_dims(gray_face, 0)
		gray_face = np.expand_dims(gray_face, -1)
		emotion_proba = emotion_classifier.predict(gray_face)
		print("------------------------------------------------------------------------")
		print("Probabilities of each class: ")
		print(" 0:angry , 1:disgust , 2:fear , 3:happy , 4:sad , 5:surprise , 6:neutral ")
		print(emotion_proba)
		emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
		emotion_text = emotion_labels[emotion_label_arg]
		print("-------------------------------------------------------------------------")
		print("Emotion class: ")	
		print(emotion_text)
		print("--------------------------------------------------------------------------")
		var = np.amax(emotion_proba)
		print("Maximum probability emotion: ")
		print(var)
		c = Label(root, text='MAXIMUM PROBABILITY EMOTION: ',background="white")
	        c.pack()
		d = Label(root, text=var,background="white")
	        d.pack()
	        f = Label(root, text="EMOTION LABEL: ",background="white")
	        f.pack()
	        e = Label(root, text=emotion_text,background="white")
	        e.pack()
		
		
		var = var * 100
		if(emotion_text == "anger"):
			S = 2.36*(math.log(0.33*var + 1.00))

		elif(emotion_text == "disguist"):
			S = 7.27*(math.log(0.01*var + 1.02))

		elif(emotion_text == "fear"):
			S = 1.76*(math.log(1.36*var + 1.00))

		elif(emotion_text == "happy"):
			S = -7.56*(math.log(-0.003*var + 1.01))

		elif(emotion_text == "sad"):
			S = 2.85*(math.log(0.13*var + 1.01))

		elif(emotion_text == "surprise"):
			S = 2.45*(math.log(0.29*var + 1.00))
			
		elif(emotion_text == "neutral"):
			S = 5.05*(math.log(0.015*var + 1.016))

		print("Stress Value:")
	   	print(S)
		a = Label(root, text='STRESS VALUE(range:0-9): ',background="white")
		a.pack()
		b = Label(root, text=S,background="white")
		b.pack()
コード例 #11
0
import preprocess
import pickle
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
import sys

token2id_path = sys.argv[2]  # 'token2id.pkl'
model_path = sys.argv[3]  # 'model.h5'

with open(token2id_path, 'rb') as f:
    token2id = pickle.load(f)
model = load_model(model_path)
model.summary()

while True:
    text = input("Input your comment: ")
    processed_tokens = preprocess.preprocess_input(text)
    id_vector = preprocess.get_id_vector(processed_tokens, token2id)
    X = pad_sequences([id_vector], 500)
    result = model.predict(X)[0]
    print("Prediction: " + str(result))
コード例 #12
0
import load_data
import preprocess
import train
from os import path
import pickle
import sys

data_path = sys.argv[1]
token2id_path = sys.argv[2]  # 'token2id.pkl'
model_path = sys.argv[3]  # 'model.h5'

(comments, sentinents) = load_data.load_dataset(data_path, 50000)
processed_comments = [preprocess.preprocess_input(item) for item in comments]

token2id = None
if (path.exists(token2id_path)):
    with open(token2id_path, 'rb') as f:
        token2id = pickle.load(f)
else:
    token2id = preprocess.get_token_to_id_dict(processed_comments)
    with open(token2id_path, 'wb') as f:
        pickle.dump(token2id, f)

X = [preprocess.get_id_vector(item, token2id) for item in processed_comments]
y = [1 if item == 'positive' else 0 for item in sentinents]

model = train.train_model(X, y, model_path, 500,
                          len(token2id) + 1, 128, 0.5, 256)