def run_model(self): data, vocabulary = prepare_test_data(self.config) self.model.test(sess, data, vocabulary)
def main(argv): start_time = time.time() config = Config() config.phase = FLAGS.phase config.train_cnn = FLAGS.train_cnn config.beam_size = FLAGS.beam_size config.distributed = FLAGS.distributed config.test_image_dir = os.path.join(FLAGS.node_root, 'images') config.test_result_dir = os.path.join(FLAGS.node_root, 'results') config.test_result_file = os.path.join(FLAGS.node_root, 'results.cvs') config.replicas = len(FLAGS.worker_hosts.split(",")) config.task_index = FLAGS.task_index if FLAGS.phase == 'train': # training phase if FLAGS.distributed: config.train_image_dir = FLAGS.input_path print config.train_image_dir ps_hosts = FLAGS.ps_hosts.split(",") worker_hosts = FLAGS.worker_hosts.split(",") # Create a cluster from the parameter server and worker hosts. cluster = tf.train.ClusterSpec({ "ps": ps_hosts, "worker": worker_hosts }) # Create and start a server for the local task. server = tf.train.Server(cluster, job_name=FLAGS.job_name, task_index=FLAGS.task_index) #with tf.device(tf.train.replica_device_setter(cluster=cluster)): # global_step = tf.Variable(0) #with tf.device("/job:ps/task:0"): # global_step = tf.Variable(0, name="global_step") if FLAGS.job_name == "ps": server.join() elif FLAGS.job_name == "worker": with tf.device( tf.train.replica_device_setter( worker_device="/job:worker/task:%d" % FLAGS.task_index, cluster=cluster)): tf.reset_default_graph() global_step = tf.contrib.framework.get_or_create_global_step( ) # global_step = tf.get_variable('global_step', [], # initializer = tf.constant_initializer(0), # trainable = False, # dtype = tf.int32) data = prepare_train_data(config) model = CaptionGenerator(config) init_op = tf.initialize_all_variables() is_chief = (FLAGS.task_index == 0) # Create a "supervisor", which oversees the training process. sv = tf.train.Supervisor( is_chief=is_chief, logdir="/home/mauro.emc/image_captioning/tmp/logs", init_op=init_op, global_step=global_step, save_model_secs=600) with sv.prepare_or_wait_for_session(server.target) as sess: if is_chief: sv.start_queue_runners(sess, [chief_queue_runner]) # Insert initial tokens to the queue. sess.run(init_token_op) sess.run(tf.global_variables_initializer()) model.train(sess, data) sv.stop() else: with tf.Session() as sess: data = prepare_train_data(config) model = CaptionGenerator(config) sess.run(tf.global_variables_initializer()) if FLAGS.load: model.load(sess, FLAGS.model_file) if FLAGS.load_cnn: model.load_cnn(sess, FLAGS.cnn_model_file) tf.get_default_graph().finalize() model.train(sess, data) elif FLAGS.phase == 'eval': with tf.Session() as sess: # evaluation phase coco, data, vocabulary = prepare_eval_data(config) model = CaptionGenerator(config) model.load(sess, FLAGS.model_file) tf.get_default_graph().finalize() model.eval(sess, coco, data, vocabulary) else: with tf.Session() as sess: # testing phase data, vocabulary = prepare_test_data(config) model = CaptionGenerator(config) model.load(sess, FLAGS.model_file) tf.get_default_graph().finalize() model.test(sess, data, vocabulary) print 'Total time in seconds : ' + str(time.time() - start_time)
def main_handler(event, context): photoList = mysql.getPhotoInforPrediction() if photoList: json_data = photoList[0] photo_dict = photoList[1] for eve_file in ["289999.json", "289999.npy", "289999.txt"]: cosClient.download2disk("others/" + eve_file, '/tmp/' + eve_file) temp_path = "/tmp/images/" if not os.path.exists(temp_path + "temp/"): os.makedirs(temp_path + "temp/") if not os.path.exists(temp_path + "prediction/"): os.makedirs(temp_path + "prediction/") print("Save File To System") for eve_pic_infor in json_data: if eve_pic_infor["type"] == "png": cosClient.download2disk( eve_pic_infor["pic"], '/tmp/images/temp/' + eve_pic_infor["name"]) PngPath = temp_path + "temp/%s" % (eve_pic_infor["name"]) PNG_JPG(PngPath, os.path.splitext(PngPath)[0] + ".jpg") else: cosClient.download2disk( eve_pic_infor["pic"], "/tmp/images/prediction/" + eve_pic_infor["name"]) tf.reset_default_graph() with tf.Session() as sess: data, vocabulary = prepare_test_data(config) model = CaptionGenerator(config) model.load(sess, "/tmp/289999.npy") tf.get_default_graph().finalize() file_list, caption = model.prediction(sess, data, vocabulary) caption_data = [{ "file": file_list[i], "caption": caption[i] } for i in range(0, len(file_list))] try: for eve in caption_data: english = eve['caption'] try: chinese = json.loads(getChinese(english))["TargetText"] time.sleep(0.2) except: chinese = None remark = chinese if chinese else english tags = FromJieba(chinese, "textrank", 3) if chinese else "-" filename = eve['file'].split("/", )[-1] mysql.saveToPhotoDB(tags, remark, photo_dict[filename]) for eve in tags: mysql.saveToTagsDB(eve) tag = mysql.getTags(eve) if tag: mysql.saveToPhotoTagsDB(tag, photo_dict[filename]) except Exception as e: print(e)
def image_caption(self, image_path): # this is what it should do data, vocabulary = prepare_test_data(self.config, image_path=image_path) caption, score = self.model.caption(self.sess, data, vocabulary) print(f"caption '{caption}' wiht score {score} for image {image_path} ") return caption, score
def main(argv): config = Config() config.phase = FLAGS.phase config.train_cnn = FLAGS.train_cnn config.beam_size = FLAGS.beam_size config.trainable_variable = FLAGS.train_cnn np.random.seed(config.seed) tf.random.set_random_seed(config.seed) if FLAGS.model == 'SeqGAN': model = SeqGAN(config) elif FLAGS.model == 'Show&Tell': model = CaptionGenerator(config) else: model = SeqGAN(config) with tf.Session() as sess: print("FLAG: " + FLAGS.phase) if FLAGS.phase == 'train': # training phase #image_path = 'D:/download/COCO/train/images/COCO_train2014_000000318556.jpg' #image_loader = ImageLoader('./utils/ilsvrc_2012_mean.npy') #data = prepare_train_data(config) sess.run(tf.global_variables_initializer()) if FLAGS.load: model.load(sess, FLAGS.model_file) #load the cnn file #if FLAGS.load_cnn: #model.load_cnn(sess, FLAGS.cnn_model_file) #model.load_vgg() #tf.get_default_graph().finalize() model.train(sess, data) elif FLAGS.phase == 'eval': print("EVALUATING") # evaluation phase eval_data = prepare_eval_data(config) #model.load(sess, FLAGS.model_file) #sess.run(tf.global_variables_initializer()) #tf.get_default_graph().finalize() #saver = tf.train.Saver(tf.global_variables()) #saver.restore(sess,FlAGS.model_file) model.eval(sess, eval_data) elif FLAGS.phase == 'test_loaded_cnn': # testing only cnn sess.run(tf.global_variables_initializer()) imgs = tf.placeholder(tf.float32, [None, 224, 224, 3]) probs = model.test_cnn(imgs) model.load_cnn(sess, FLAGS.cnn_model_file) img1 = imread(FLAGS.image_file, mode='RGB') img1 = imresize(img1, (224, 224)) prob = sess.run(probs, feed_dict={imgs: [img1]})[0] preds = (np.argsort(prob)[::-1])[0:5] for p in preds: print(class_names[p], prob[p]) else: # testing phase test_data = prepare_test_data(config) model.load(sess, FLAGS.model_file) #tf.get_default_graph().finalize() model.test(sess, test_data)