def get_classes(self): try: label_map_string = '' print("getting " + self.label_file + ' from ' + self.bucket) obj = self.s3.get_object(Bucket=self.bucket, Key=self.label_file) label_map_string = obj['Body'].read().decode('utf-8') print("got " + self.label_file) self.label_map = {} self.label_map = labelmap.StringIntLabelMap() self.labelmap_dict = {} text_format.Merge(label_map_string, self.label_map) self.categories = [] for item in self.label_map.item: self.labelmap_dict[item.id] = item.name self.categories.append({'id': item.id, 'name': item.name}) self.category_index = label_map_util.create_category_index( self.categories) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "NoSuchKey": self.labelmap_dict = {} print(self.label_file + ' was not found') else: print(e) raise ValueError('Error getting ' + self.label_file + ' in ' + self.bucket)
def _save_label_map_dict(label_map_dict, label_map_path): label_map = string_int_label_map_pb2.StringIntLabelMap() items = sorted(label_map_dict.items(), key=lambda x: int(x[1])) for name, id_ in items: item = label_map.item.add() item.id = id_ item.name = name _save_label_map(label_map, label_map_path)
def load_labelmap(path): with open(path, "r") as labels_file: label_map_string = labels_file.read() label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(label_map_string, label_map) except text_format.ParseError: label_map.ParseFromString(label_map_string) return label_map
def load_labelmap(path): with tf.gfile.GFile(path, 'r') as fid: label_map_string = fid.read() label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(label_map_string, label_map) except text_format.ParseError: label_map.ParseFromString(label_map_string) _validate_label_map(label_map) return label_map
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) run_num = 20 image, org = decode_image_opencv(FLAGS.image) image = image.astype(np.uint8) request_array = [] # batch_size_array = [1, 1, 4, 4, 1, 4] batch_size_array = [1, 2, 4, 8, 16, 32, 64, 128] # batch_size_array = [32, 16, 8, 4, 2, 1] # batch_size_array = [10, 9, 8, 7, 6, 5] for batch_size in batch_size_array: inputs = image for i in range(batch_size - 1): inputs = np.append(inputs, image, axis = 0) request = predict_pb2.PredictRequest() request.model_spec.name = model_name request.model_spec.signature_name = 'serving_default' request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto(inputs, shape=inputs.shape)) request_array.append(request) print("Done with input preparation") raw_input("Press Enter to continue...\n") for i in range(len(batch_size_array)): batch_size = batch_size_array[i] request = request_array[i] durationSum = 0.0 for j in range(run_num): start = time.time() result = stub.Predict(request, 10.0) end = time.time() duration = end - start print("duration = %f" % duration) if (j != 0): durationSum += duration print("average duration (warm-up excluded) for batch size of %d = %f" % (batch_size, durationSum / (run_num - 1)))
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # durationSum = 0.0 thread_num = 1 run_num = 100 batch_size = 1 image, org = decode_image_opencv(FLAGS.image) image = image.astype(np.uint8) inputs = image for i in range(batch_size - 1): inputs = np.append(inputs, image, axis = 0) # request = predict_pb2.PredictRequest() # request.model_spec.name = model_name # request.model_spec.signature_name = 'serving_default' # request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto(inputs, shape=inputs.shape)) # # warmup # warmup_num = 3 # for i in range(warmup_num): # start = time.time() # result = stub.Predict(request, 10.0) # end = time.time() # duration = end - start # print("warmup duration = %f" % duration) # # time.sleep(2.0) raw_input("Press Enter to continue...") start = time.time() jobs = [] for i in range(thread_num): p = multiprocessing.Process(target = send_request, args = (inputs, run_num, batch_size, i,)) jobs.append(p) p.start() for p in jobs: p.join() end = time.time() print("overall time = %f" % (end - start))
def load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ with tf.gfile.GFile(path, 'r') as fid: label_map_string = fid.read() label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(label_map_string, label_map) except text_format.ParseError: label_map.ParseFromString(label_map_string) return label_map
def main(_): if not FLAGS.img_path: print('Please specify img_path -img_path=...') return if not FLAGS.num_tests: print('Please specify num_tests -num_tests=n') return if not FLAGS.server: print('please specify server -server host:port') return print("Number of test=", FLAGS.num_tests) s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) do_inference(FLAGS.server, FLAGS.batch_size, FLAGS.num_tests, FLAGS.img_path)
def cats_to_pbtxt(cats_json, pbtxt): cats, _ = load_cats(cats_json) cats[0] = 'none_of_the_above' #insert bg category import sys sys.path.append('/home/splendor/win/learn/models/object_detection/protos') import string_int_label_map_pb2 labels = string_int_label_map_pb2.StringIntLabelMap() for k, v in cats.items(): label = labels.item.add() label.id = k label.name = v # print(labels) with open(pbtxt, 'w') as f: f.write(str(labels))
def _load_label_map(label_map_path): """Loads label map proto. Args: label_map_path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ with tf.gfile.GFile(label_map_path, 'r') as fid: label_map_string = fid.read() label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(label_map_string, label_map) except text_format.ParseError: label_map.ParseFromString(label_map_string) # validate label map for item in label_map.item: if item.id < 1: raise ValueError('Label map ids should be >= 1.') return label_map
def main(_): label_map = labelmap.StringIntLabelMap() with open(FLAGS.label_file, 'r') as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) categories = [] for item in label_map.item: labelmap_dict[item.name] = item.id categories.append({'id': item.id, 'name': item.name}) print(categories) print(labelmap_dict) writer = tf.python_io.TFRecordWriter(FLAGS.output_path) path = os.path.join(os.getcwd(), FLAGS.image_dir) examples = pd.read_csv(FLAGS.csv_input) grouped = split(examples, 'filename') for group in grouped: tf_example = create_tf_example(group, path) writer.write(tf_example.SerializeToString()) writer.close() output_path = os.path.join(os.getcwd(), FLAGS.output_path) print('Successfully created the TFRecords: {}'.format(output_path))
def load_pbtxt_file(path): """Read .pbtxt file. Args: path: Path to StringIntLabelMap proto text file (.pbtxt file). Returns: A StringIntLabelMapProto. Raises: ValueError: If path is not exist. """ if not tf.gfile.Exists(path): raise ValueError('`path` is not exist.') with tf.gfile.GFile(path, 'r') as fid: pbtxt_string = fid.read() pbtxt = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(pbtxt_string, pbtxt) except text_format.ParseError: pbtxt.ParseFromString(pbtxt_string) return pbtxt
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) image, org = decode_image_opencv(FLAGS.image) image = image.astype(np.uint8) model_1 = 'ssd_inception_v2_coco' model_2 = 'ssd_mobilenet' # thread_pool = [] t1 = threading.Thread(target=send_request, args=( stub, image, model_1, 10, )) t2 = threading.Thread(target=send_request, args=( stub, image, model_2, 1, )) t1.start() t2.start() t1.join() t2.join()
def Setup(): s = open('%s/modules_traffic/mscoco_complete_label_map.pbtxt' % os.environ['TRAFFIC_JAMMER_PATH'], 'r').read() mymap = labelmap.StringIntLabelMap() TrafficInception._label_map = text_format.Parse(s, mymap)
def main(_): if (sys.argv[1] == "mobilenet"): model_name = "ssd_mobilenet_v1_coco" elif (sys.argv[1] == "inception"): model_name = "ssd_inception_v2_coco" else: model_name = "ssd_resnet50_v1_fpn" s = open('/home/yitao/Documents/fun-project/tensorflow-related/traffic-jammer/single_dnn_client/obj_det/mscoco_complete_label_map.pbtxt','r').read() mymap =labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s,mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) run_num = 10 batch_size = 1 for i in range(run_num): start = time.time() image, org = decode_image_opencv(FLAGS.image) _draw = org.copy() image = image.astype(np.uint8) inputs = image for i in range(batch_size - 1): inputs = np.append(inputs, image, axis = 0) request = predict_pb2.PredictRequest() request.model_spec.name = model_name request.model_spec.signature_name = 'serving_default' request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto (inputs, shape=inputs.shape)) result = stub.Predict(request, 10.0) # print(result) boxes = result.outputs['detection_boxes'] scores = result.outputs['detection_scores'] labels = result.outputs['detection_classes'] num_detections= result.outputs['num_detections'] # print("???") # print(boxes) boxes= tf.make_ndarray(boxes) scores= tf.make_ndarray(scores) labels= tf.make_ndarray(labels) num_detections= tf.make_ndarray(num_detections) # print("boxes output",(boxes).shape) # print("scores output",(scores).shape) # print("labels output",(labels).shape) # print('num_detections',num_detections[0]) # # visualize detections hints from # # # https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb # for box, score, label in zip(boxes[0], scores[0], labels[0]): # # scores are sorted so we can break # if score < 0.3: # break # #dim = image.shape[0:2] # dim = _draw.shape # #print("Label-raw",labels_to_names[label-1]," at ",box," Score ",score) # box = box_normal_to_pixel(box, dim) # b = box.astype(int) # class_label = get_label(int(label)) # print("Label",class_label ," at ",b," Score ",score) # # draw the image and write out # cv2.rectangle(_draw,(b[0],b[1]),(b[2],b[3]),(0,0,255),1) # cv2.putText(_draw,class_label + "-"+str(round(score,2)), (b[0]+2,b[1]+8),\ # cv2.FONT_HERSHEY_SIMPLEX, .45, (0,0,255)) # cv2.imshow("test", _draw) # cv2.waitKey(0) end = time.time() duration = end - start print("duration = %s sec" % str(duration))
def generate_elements(self, url): train_writer = tf.python_io.TFRecordWriter(dir_path + 'train.record') test_writer = tf.python_io.TFRecordWriter(dir_path + 'test.record') try: if os.path.isfile(labelmap_file) == True: label_file = util_obj.open_file(labelmap_file, "rb") class_map = string_int_label_map_pb2.StringIntLabelMap() class_map.ParseFromString(label_file.read()) label_file.close() else: class_map = string_int_label_map_pb2.StringIntLabelMap() self.driver.get(url) #all_visible_elements = self.driver.find_elements_by_xpath("//*[not(contains(@style,'display:none'))]") all_visible_elements = self.driver.find_elements_by_xpath("//a[not(contains(@style,'display:none')) and not(contains(@class,'story'))] | //h2[not(contains(@style,'display:none')) and not(contains(@class,'story'))] | //button[not(contains(@style,'display:none')) and not(contains(@class,'story'))] | //span[not(contains(@style,'display:none')) and not(contains(@class,'story'))] | //input[not(contains(@style,'display:none')) and not(contains(@class,'story'))]") for element in all_visible_elements: elements_list = [] location = element.location_once_scrolled_into_view location = element.location size = element.size x1_orig = location['x'] * 2 y1_orig = location['y'] * 2 x2_orig = x1_orig + size['width'] * 2 y2_orig = y1_orig + size['height'] * 2 words = element.text.split() if x2_orig != x1_orig and y2_orig != y1_orig and len(words) < 4: png = self.driver.get_screenshot_as_png() pil_image = Image.open(BytesIO(png)) image_width, image_height = pil_image.size img, x1,y1,x2,y2 = self.crop_image(pil_image,x1_orig,y1_orig,x2_orig,y2_orig,1024,600) #pil_image = pil_image.resize((1024,600)) img = np.asarray(img) suffix = self.get_suffix_from_element(element) #x1 = int((x1_orig/image_width)*1024) #x2 = int((x2_orig/image_width)*1024) #y1 = int((y1_orig/image_height)*600) #y2 = int((y2_orig/image_height)*600) if not suffix: continue class_name = element.tag_name + '_' + suffix original_image_path = dir_path + "images/train/" + class_name + '.png' #pil_image.save(original_image_path) self.image_with_bounding_box(samples_dir,class_name,img,x1,y1,x2,y2) # writing class to label_map.pbtxt class_id = util_obj.add_class_to_label_map(class_name, class_map) print("class id", class_id) # creating an tf.example #width = size['width'] * 2 #height = size['height'] * 2 elements_list.append([img,x1,y1,x2,y2,image_width,image_height,class_name,class_id]) train_list,test_list = util_obj.create_augmented_images(elements_list,80) self.serialize_image_list(train_writer,train_list) self.serialize_image_list(test_writer,test_list) except Exception: #print(class_name) traceback.print_exc() label_map_file = util_obj.open_file(labelmap_file, "wb") label_map_file.write(class_map.SerializeToString()) label_map_file.close() train_writer.close() test_writer.close()
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) thread_num_array = [1, 2, 4] batch_size_2d_array = [[1, 2, 4, 8, 16, 32, 64, 128], [1, 2, 4, 8, 16, 32, 64], [1, 2, 4, 8, 16, 32]] # thread_num_array = [1] # batch_size_2d_array = [[4, 8]] for i in range(len(thread_num_array)): thread_num = thread_num_array[i] batch_size_array = batch_size_2d_array[i] for batch_size in batch_size_array: time.sleep(2.0) run_num = 1024 / (thread_num * batch_size) # print("[INFO] thread_num = %d, batch_size = %d, run_num = %d" % (thread_num, batch_size, run_num)) t0 = time.time() inputs = prepareInputs(batch_size) t1 = time.time() # print("[Debug] it took %.3f sec to prepare image of shape %s" % (t1 - t0, inputs.shape)) # warmup warmup_num = 3 send_request(stub, inputs, batch_size, warmup_num, 0) # raw_input("Press Enter to continue...") time.sleep(2.0) t0 = time.time() thread_pool = [] for thread_id in range(thread_num): t = threading.Thread(target=send_request, args=( stub, inputs, batch_size, run_num, thread_id, )) thread_pool.append(t) t.start() for t in thread_pool: t.join() t1 = time.time() print( "[Summary] thread_num = %d, batch_size = %d, run_num = %d, total duration = %.3f" % (thread_num, batch_size, run_num, t1 - t0))
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # durationSum = 0.0 thread_num = int(sys.argv[1]) # run_num = 10 batch_size = int(sys.argv[2]) run_num = 1024 / (thread_num * batch_size) print("[INFO] thread_num = %d, batch_size = %d, run_num = %d" % (thread_num, batch_size, run_num)) image, org = decode_image_opencv(FLAGS.image) image = image.astype(np.uint8) t0 = time.time() inputs = image for i in range(batch_size - 1): inputs = np.append(inputs, image, axis=0) t1 = time.time() print("\n[INFO] preparing input data takes %.3f sec\n" % (t1 - t0)) request = predict_pb2.PredictRequest() request.model_spec.name = model_name request.model_spec.signature_name = 'serving_default' request.inputs['inputs'].CopyFrom( tf.contrib.util.make_tensor_proto(inputs, shape=inputs.shape)) # warmup warmup_num = 3 for i in range(warmup_num): start = time.time() result = stub.Predict(request, 10.0) end = time.time() duration = end - start print("warmup duration = %f" % duration) # time.sleep(2.0) raw_input("Press Enter to continue...") start = time.time() thread_pool = [] for i in range(thread_num): t = threading.Thread(target=send_request, args=( stub, inputs, run_num, batch_size, i, )) thread_pool.append(t) t.start() for t in thread_pool: t.join() end = time.time() print("overall time = %f" % (end - start))
def main(_): s = open('mscoco_complete_label_map.pbtxt', 'r').read() mymap = labelmap.StringIntLabelMap() global _label_map _label_map = text_format.Parse(s, mymap) channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) durationSum = 0.0 run_num = 10 batch_size = 64 image, org = decode_image_opencv(FLAGS.image) # _draw = org.copy() image = image.astype(np.uint8) inputs = image for i in range(batch_size - 1): inputs = np.append(inputs, image, axis=0) request = predict_pb2.PredictRequest() request.model_spec.name = model_name request.model_spec.signature_name = 'serving_default' request.inputs['inputs'].CopyFrom( tf.contrib.util.make_tensor_proto(inputs, shape=inputs.shape)) for i in range(run_num): start = time.time() result = stub.Predict(request, 10.0) end = time.time() if (i != 0): duration = end - start durationSum += duration print("duration = %f" % duration) # print(result) # boxes = result.outputs['detection_boxes'] # scores = result.outputs['detection_scores'] # labels = result.outputs['detection_classes'] # num_detections= result.outputs['num_detections'] # boxes= tf.make_ndarray(boxes) # scores= tf.make_ndarray(scores) # labels= tf.make_ndarray(labels) # num_detections= tf.make_ndarray(num_detections) # print("boxes output",(boxes).shape) # print("scores output",(scores).shape) # print("labels output",(labels).shape) # print('num_detections',num_detections[0]) # # visualize detections hints from # # # https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb # for box, score, label in zip(boxes[0], scores[0], labels[0]): # # scores are sorted so we can break # if score < 0.3: # break # #dim = image.shape[0:2] # dim = _draw.shape # #print("Label-raw",labels_to_names[label-1]," at ",box," Score ",score) # box = box_normal_to_pixel(box, dim) # b = box.astype(int) # class_label = get_label(int(label)) # print("Label",class_label ," at ",b," Score ",score) # # draw the image and write out # cv2.rectangle(_draw,(b[0],b[1]),(b[2],b[3]),(0,0,255),1) # cv2.putText(_draw,class_label + "-"+str(round(score,2)), (b[0]+2,b[1]+8),\ # cv2.FONT_HERSHEY_SIMPLEX, .45, (0,0,255)) # cv2.imshow("test", _draw) # cv2.waitKey(0) print("average duration for batch size of %d = %f" % (batch_size, durationSum / (run_num - 1)))