This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !pip3 install --quiet "tensorflow>=1.7" | |
| !pip3 install --quiet tensorflow-hub | |
| !pip3 install seaborn | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import os | |
| import pandas as pd | |
| import re |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ['HUM', 'ENTY', 'LOC', 'ABBR', 'DESC', 'NUM'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| new_text = ["current us president?", "what compose my pc?", 'where is USA', 'what U.S.A mean?', | |
| 'tell me about how economy work', 'the number of PI?'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| df_train = get_dataframe('test_data.txt') | |
| category_counts = len(df_train.label.cat.categories) | |
| print(category_counts) # 6 | |
| sns.barplot(x.index,x) | |
| plt.gca().set_ylabel('samples') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def UniversalEmbedding(x): | |
| return embed(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["default"] | |
| input_text = layers.Input(shape=(1,), dtype=tf.string) | |
| embedding = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))(input_text) | |
| dense = layers.Dense(256, activation='relu')(embedding) | |
| pred = layers.Dense(category_counts, activation='softmax')(dense) | |
| model = Model(inputs=[input_text], outputs=pred) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train_text = np.array(df_train['text'].tolist(), dtype=object)[:, np.newaxis] | |
| train_label = np.asarray(pd.get_dummies(df_train.label), dtype = np.int8) | |
| print(train_text.shape) | |
| print(train_label.shape) | |
| print(train_label[:3]) | |
| # also handel the test set | |
| df_test = get_dataframe('test_data.txt') | |
| test_text = np.array(df_test['text'].tolist(), dtype=object)[:, np.newaxis] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with tf.Session() as session: | |
| K.set_session(session) | |
| session.run(tf.global_variables_initializer()) | |
| session.run(tf.tables_initializer()) | |
| history = model.fit(train_text, train_label, | |
| validation_data=(test_text, test_label), | |
| epochs=10, | |
| batch_size=32) | |
| model.save_weights('./model.h5') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def run_pred(text_list): | |
| with tf.Session() as session: | |
| K.set_session(session) | |
| session.run(tf.global_variables_initializer()) | |
| session.run(tf.tables_initializer()) | |
| model.load_weights('./model.h5') | |
| predicts = model.predict(np.array(text_list, dtype=object)[:, np.newaxis], batch_size=32) | |
| categories = df_train.label.cat.categories.tolist() | |
| predict_labels = [categories[logit] for logit in predicts.argmax(axis=1)] | |
| return predict_labels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # code origin: https://www.jianshu.com/p/e1d95c4e1697 | |
| class Entity: | |
| def __init__(self, size, x, y): | |
| """ init x, y when init the instance""" | |
| self.x, self.y = x, y | |
| self.size = size | |
| def __call__(self, x, y): | |
| '''you can change x, y after''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # code origin: https://www.kaggle.com/timesler/facial-recognition-model-in-pytorch | |
| class DetectionPipeline: | |
| """Pipeline class for detecting faces in the frames of a video file.""" | |
| def __init__(self, detector, n_frames=None, batch_size=60, resize=None): | |
| """Constructor for DetectionPipeline class. | |
| Keyword Arguments: | |
| n_frames {int} -- Total number of frames to load. These will be evenly spaced |