def decode_batch(test_func, word_batch): out = test_func([word_batch])[0] ret = [] for j in range(out.shape[0]): out_best = list(np.argmax(out[j, 2:], 1)) out_best = [k for k, g in itertools.groupby(out_best)] outstr = labels_to_text(out_best) ret.append(outstr) return ret
def predict(model: Sequential, tokenizer: Tokenizer, word: str, seq_len: int = 4): encoded_text = r .texts_to_sequences([word])[0] pad_encoded = pad_sequences([encoded_text], maxlen=seq_len, truncating='pre') print(encoded_text, pad_encoded) pred = model.predict(pad_encoded)[0] for _ in range(0, 3): index = np.argmax(pred, axis=0) pred_word = tokenizer.index_word[index] print("Next word suggestion:", pred_word) pred = np.delete(pred, index)
#Εκπαίδευση και αξιολόγηση του μοντέλου batch_size = 32 model_lstm.fit(X1_train,Y1_train,epochs = 25,batch_size=batch_size, verbose = 2, validation_data=(X1_test,Y1_test)) y_pred = model_lstm.predict(X1_test) loss,acc = model_lstm.evaluate(X1_test, Y1_test, verbose = 1, batch_size = batch_size) print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc)) pos_cnt, neg_cnt, pos_correct, neg_correct = 0, 0, 0, 0 for x in range(len(X1_test)): result = model_lstm.predict(X1_test[x].reshape(1, X1_test.shape[1]), batch_size=1, verbose=2)[0] if np.argmax(result) == np.argmax(Y1_test[x]): if np.argmax(Y1_test[x]) == 0: neg_correct += 1 else: pos_correct += 1 if np.argmax(Y1_test[x]) == 0: neg_cnt += 1 else: pos_cnt += 1 print("Ακρίβεια Αποδεκτών Γνωμοδοτήσεων", pos_correct / pos_cnt * 100, "%") print("Ακρίβεια Γνωμοδοτησεων σε εκρεμμότητα", neg_correct / neg_cnt * 100, "%")
from keras.models import model_from_json from pathlib import Path from keras.preprocessing import image from pandas import np classes = [ "Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Boat", "Truck" ] f = Path("model_structure.json") model_structure = f.read_text() model = model_from_json(model_structure) model.load_weights("model_weights.h5") img = image.load_img("tractor-3-1386656.jpg", target_size=(32, 32)) img_arry = image.img_to_array(img) / 255 list_img = np.expand_dims(img_arry, axis=0) res = model.predict(list_img) single = res[0] most_likely = int(np.argmax(single)) class_like = single[most_likely] class_name = classes[most_likely] print("The image is {} with accuracy: {:2f}".format(class_name, class_like))
def _class_maps_to_colored_maps(class_map: np.ndarray) -> np.ndarray: return np.argmax(class_map, axis=3)
cv2.namedWindow('window_frame') while True: _, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_detection.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(gray, (x - int(0.2 * w), y - int(0.3 * h)), (x + int(1.2 * w), y + int(1.2 * h)), (255, 0, 0), 2) face = gray[y - int(0.3 * h): y + int(1.2 * h), x - int(0.2 * w): x + int(1.2 * w)] try: face = cv2.resize(face, (64, 64)) except: continue face = np.expand_dims(face, 0) face = np.expand_dims(face, -1) gender_arg = np.argmax(gender_classifier.predict(face)) gender = gender_labels[gender_arg] gender_window.append(gender) if len(gender_window) >= frame_window: gender_window.pop(0) try: gender_mode = mode(gender_window) except: continue cv2.putText(gray, gender_mode, (x, y - 30), font, .7, (255, 0, 0), 1, cv2.LINE_AA) try: cv2.imshow('window_frame', gray) except: continue
from sklearn.metrics import accuracy_score, confusion_matrix model = Sequential() model.add(Embedding(vocab, 50, input_length=sentence_length)) model.add(Dropout(0.2)) model.add(LSTM(100, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(50)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) x_train, x_test, y_train, y_test = train_test_split(inp_fin, out_fin, test_size=0.2, random_state=42) model.summary() history = model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=64, epochs=10) prediction = np.argmax(model.predict(x_test), axis=-1) print(accuracy_score(y_test, prediction)) model.save('model.h5')