Я хочу создать веб-сервис с флягой, где несколько моделей глубокого обучения будут применяться к определенным типам данных, чтобы получить результат. В настоящее время я хочу загрузить их локально в main () один раз при запуске, передать их init , чтобы просто инициализировать их один раз при запуске выполнения скрипта, а затем вызывать его каждый раз, когда это необходимо для выполнения вперед пройти, чтобы вернуть что-то. Пока что это то, что я сделал с остальными, но я не знаю, как справиться с чистой инициализацией модели тензорного потока. Приведенный ниже код работает нормально. Любые предложения, изменения приветствуются:
def evaluate_sample(numpy_array, no_of_frames):
_IMAGE_SIZE = 224
_SAMPLE_VIDEO_FRAMES = no_of_frames
_CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
NUM_CLASSES = 400
flow_input = tf.placeholder(
tf.float32,
shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
flow_variable_map = {}
for variable in tf.global_variables():
if variable.name.split('/')[0] == 'Flow':
flow_variable_map[variable.name.replace(':0', '')] = variable
flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
model_logits = flow_logits
model_predictions = tf.nn.softmax(model_logits)
with tf.Session() as sess:
feed_dict = {}
flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
flow_sample = numpy_array
feed_dict[flow_input] = flow_sample
out_logits, out_predictions = sess.run(
[model_logits, model_predictions],
feed_dict=feed_dict)
logits2=np.asarray(out_logits)
return logits2
def get_flow_features(video_path):
.....
aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
.....
class GetOutOfContext:
def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
self.keras_model = keras_model
self.pytorch_model = pytorch_model
self.word2vec_model = word2vec_model
self.max_pooling = max_pooling
#self.kineticsi3d = kineticsi3d
print("Similarity Between Video and Text Service Initialized...")
def get(self):
dirpath = tempfile.mkdtemp()+"/"
video_path = download_video(url,dirpath)
aggregated_audio = get_audio_features(video_path)
aggregated_flow = get_flow_features(video_path)
aggregated_video = get_visual_features(video_path, dirpath)
aggregated_text = get_word_features(text)
.......
if __name__ == "__main__":
"""Loading Prediction Model"""
video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
the_model = Net(video_modality_dim, 300, audio_cluster=16)
the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
the_model.eval()
"""Loading Image Feature Extraction Model"""
model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
"""Loading Word2Vec Model"""
model2 = api.load("word2vec-google-news-300")
maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
word_vectors = model2.wv
nltk.download('stopwords')
x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
y = x.get()