def load_resource(self): self.player = cv2.imread(os.path.join(self.resource_dir, 'player.png'), 0) # network initization self.net = JumpModel() self.net_fine = JumpModelFine() self.img = tf.placeholder(tf.float32, [None, 640, 720, 3], name='img') self.img_fine = tf.placeholder(tf.float32, [None, 320, 320, 3], name='img_fine') self.label = tf.placeholder(tf.float32, [None, 2], name='label') self.is_training = tf.placeholder(np.bool, name='is_training') self.keep_prob = tf.placeholder(np.float32, name='keep_prob') self.pred = self.net.forward(self.img, self.is_training, self.keep_prob) self.pred_fine = self.net_fine.forward(self.img_fine, self.is_training, self.keep_prob) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) all_vars = tf.all_variables() var_coarse = [k for k in all_vars if k.name.startswith('coarse')] var_fine = [k for k in all_vars if k.name.startswith('fine')] self.saver_coarse = tf.train.Saver(var_coarse) self.saver_fine = tf.train.Saver(var_fine) self.saver_coarse.restore(self.sess, self.ckpt) self.saver_fine.restore(self.sess, self.ckpt_fine) print('==== successfully restored ====')
def inference(path): net = JumpModel() img = tf.placeholder(tf.float32, [None, 640, 720, 3], name='img') label = tf.placeholder(tf.float32, [None, 2], name='label') is_training = tf.placeholder(np.bool, name='is_training') keep_prob = tf.placeholder(np.float32, name='keep_prob') lr = tf.placeholder(np.float32, name='lr') pred = net.forward(img, is_training, keep_prob) saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state('./train_logs') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print('==== successfully restored ====') val_img = get_a_img(path) feed_dict = { img: val_img, is_training: False, keep_prob: 1.0, } pred_out = sess.run(pred, feed_dict=feed_dict) return pred_out
def load_resource(self): self.player = cv2.imread(os.path.join(self.resource_dir, 'player.png'), 0) # network initization self.net = JumpModel() self.img = tf.placeholder(tf.float32, [None, 640, 720, 3], name='img') self.label = tf.placeholder(tf.float32, [None, 2], name='label') self.is_training = tf.placeholder(np.bool, name='is_training') self.keep_prob = tf.placeholder(np.float32, name='keep_prob') self.pred = self.net.forward(self.img, self.is_training, self.keep_prob) self.saver = tf.train.Saver() self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state( os.path.join(self.resource_dir, 'train_logs2')) if ckpt and ckpt.model_checkpoint_path: self.saver.restore(self.sess, ckpt.model_checkpoint_path) print('==== successfully restored ====')
def load_resource(self): self.player = cv2.imread(os.path.join(self.resource_dir, 'player.png'), 0) # network initialization self.net = JumpModel() self.net_fine = JumpModelFine() self.img = tf.placeholder(tf.float32, shape=(None, 640, 720, 3), name='img') self.img_fine = tf.placeholder(tf.float32, shape=(None, 320, 320, 3), name='img_fine') self.label = tf.placeholder(tf.float32, [None, 2], name='label') self.is_training = tf.placeholder(np.bool, name='is_training') self.keep_prob = tf.placeholder( np.float32, name='keep_prob') # get_target_position()用到 # 这里的第三个参数self.keep_prob必须是string类型的 # self.pred = self.net.forward(self.img, self.is_training, self.keep_prob) # self.pred_fine = self.net_fine.forward(self.img_fine, self.is_training, self.keep_prob) # 第三个参数直接写成string类型,两个模型-分别为 coarse 与 fine self.pred = self.net.forward(self.img, self.is_training, "coarse") self.pred_fine = self.net_fine.forward(self.img_fine, self.is_training, "fine") self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) all_vars = tf.global_variables( ) # tf.all_variables() # Please use tf.global_variables instead. var_coarse = [k for k in all_vars if k.name.startswith('coarse')] var_fine = [k for k in all_vars if k.name.startswith('fine')] self.saver_coarse = tf.train.Saver(var_coarse) self.saver_fine = tf.train.Saver(var_fine) self.saver_coarse.restore(self.sess, self.ckpt) self.saver_fine.restore(self.sess, self.ckpt_fine) print('==== successfully restored ====')
6], name[name.index('_w_') + 3:name.index('_w_') + 6] x, y = int(x), int(y) img = cv2.imread(img_name) label = np.array([x, y], dtype=np.float32) return img[np.newaxis, :, :, :], label.reshape((1, label.shape[0])) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-g', '--gpu', default=None, type=int) args = parser.parse_args() if args is not None: os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) net = JumpModel() dataset = JumpData() img = tf.placeholder(tf.float32, [None, 640, 720, 3], name='img') label = tf.placeholder(tf.float32, [None, 2], name='label') is_training = tf.placeholder(np.bool, name='is_training') keep_prob = tf.placeholder(np.float32, name='keep_prob') lr = tf.placeholder(np.float32, name='lr') pred = net.forward(img, is_training, keep_prob, 'coarse') loss = tf.reduce_mean(tf.sqrt(tf.pow(pred - label, 2) + 1e-12)) tf.summary.scalar('loss', loss) optimizer = tf.train.AdamOptimizer(lr) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss) saver = tf.train.Saver()