# predict in batches n_batch = int(math.ceil(len(all_inps) / batch)) for j in range(n_batch): from_idx = j * batch to_idx = min(from_idx + batch, len(all_inps)) # collect images input in the batch this_batch = all_inps[from_idx:to_idx] inp_feed = my_pool.map( lambda inp: (np.expand_dims( tfnet.framework.preprocess(os.path.join(inp[0], inp[1])), 0)), this_batch) # Feed to the net feed_dict = {tfnet.inp: np.concatenate(inp_feed, 0)} tfnet.say('Forwarding {} inputs ...'.format(len(inp_feed))) start = time.time() out = tfnet.sess.run([tfnet.out, tfnet.my_out], feed_dict) my_out = out[1] out = out[0] stop = time.time() last = stop - start tfnet.say('Total time = {}s / {} inps = {} ips'.format( last, len(inp_feed), len(inp_feed) / last)) # Post processing tfnet.say('Post processing {} inputs ...'.format(len(inp_feed))) start = time.time() my_pool.map( lambda p: (lambda i, prediction: my_postprocess(
requiredDirectories = [FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir, 'out')] if FLAGS.summary: requiredDirectories.append(FLAGS.summary) _get_dir(requiredDirectories) tfnet = TFNet(FLAGS) model = masknet.create_model() model.summary() model.load_weights("weights.hdf5") elapsed = int() start = timer() tfnet.say('Press [ESC] to quit demo') preprocessed = tfnet.framework.preprocess(frame) buffer_inp.append(frame) buffer_pre.append(preprocessed) # Only process and imshow when queue is full if elapsed % FLAGS.queue == 0: feed_dict = {tfnet.inp: buffer_pre} net_out = tfnet.sess.run( [tfnet.out, tfnet.my_c2, tfnet.my_c3, tfnet.my_c4, tfnet.my_c5], feed_dict) my_c2 = net_out[1] my_c3 = net_out[2] my_c4 = net_out[3] my_c5 = net_out[4] net_out = net_out[0]
to_idx = min(from_idx + batch, len(all_inps)) # collect images input in the batch this_batch = all_inps[from_idx:to_idx] # 对batch中每一个图片文件进行多进程操作: # 在非training的predict中resize,BGR->RGB,使用cv转化为numpy tensor inp_feed = pool.map( lambda inp: (np.expand_dims( tfnet.framework.preprocess(os.path.join(inp_path, inp)), 0)), this_batch) # Feed to the net # 将一个batch内的所有图片concat起来成一个batch送入网络中 feed_dict = {tfnet.inp: np.concatenate(inp_feed, 0)} tfnet.say('Forwarding {} inputs ...'.format(len(inp_feed))) start = time.time() out = tfnet.sess.run(tfnet.out, feed_dict) stop = time.time() last = stop - start tfnet.say('Total time = {}s / {} inps = {} ips'.format( last, len(inp_feed), len(inp_feed) / last)) # Post processing # 将tf的out进行后处理,给图片画框并存储 tfnet.say('Post processing {} inputs ...'.format(len(inp_feed))) start = time.time() pool.map( lambda p: (lambda i, prediction: postprocess( tfnet, prediction, os.path.join(inp_path, this_batch[i])))(*p),