def main(argv): flags = parser( description="freeze yolov3 graph from checkpoint file").parse_args() classes = utils.read_coco_names("./data/coco.names") num_classes = len(classes) SIZE = flags.image_size print("=> the input image size is [%d, %d]" % (SIZE, SIZE)) model = yolov3.yolov3(num_classes) with tf.Graph().as_default() as graph: sess = tf.Session(graph=graph) inputs = tf.placeholder( tf.float32, [1, SIZE, SIZE, 3]) # placeholder for detector inputs with tf.variable_scope('yolov3'): feature_map = model.forward(inputs, is_training=False) boxes, confs, probs = model.predict(feature_map) scores = confs * probs print("=>", boxes, scores) boxes, scores, labels = utils.gpu_nms( boxes, scores, num_classes, score_thresh=flags.score_threshold, iou_thresh=flags.iou_threshold) print("=>", boxes, scores, labels) feature_map_1, feature_map_2, feature_map_3 = feature_map print("=>", feature_map_1, feature_map_2, feature_map_3) saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3')) if flags.convert: if not os.path.exists(flags.weights_path): url = 'https://github.com/YunYang1994/tensorflow-yolov3/releases/download/v1.0/yolov3.weights' for i in range(3): time.sleep(1) print("=> %s does not exists ! " % flags.weights_path) print("=> It will take a while to download it from %s" % url) print('=> Downloading yolov3 weights ... ') wget.download(url, flags.weights_path) load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), flags.weights_path) sess.run(load_ops) save_path = saver.save(sess, save_path=flags.ckpt_file) print('=> model saved in path: {}'.format(save_path)) if flags.freeze: saver.restore(sess, flags.ckpt_file) print('=> checkpoint file restored from ', flags.ckpt_file) utils.freeze_graph(sess, './checkpoint/yolov3_cpu_nms.pb', ["concat_9", "mul_6"]) utils.freeze_graph(sess, './checkpoint/yolov3_gpu_nms.pb', ["concat_10", "concat_11", "concat_12"]) utils.freeze_graph(sess, './checkpoint/yolov3_feature.pb', [ "yolov3/yolo-v3/feature_map_1", "yolov3/yolo-v3/feature_map_2", "yolov3/yolo-v3/feature_map_3", ])
def __init__(self): ''' Called when class object is created. ''' self.IMAGE_H, self.IMAGE_W = imgsize, imgsize self.classes = utils.read_coco_names('./data/coco.names') self.num_classes = len(self.classes) self.colors = self.color_generator() self.windowName = 'Inference' cv2.namedWindow(self.windowName, cv2.WINDOW_NORMAL)
def __init__(self): ''' Called when class object is created. ''' self.img_size = imgsize self.max_batch_size = batchsize self.num_classes = len(utils.read_coco_names('./data/coco.names')) self.input_tensor, self.output_tensors = utils.read_pb_return_tensors( tf.compat.v1.get_default_graph(), "./checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) self.config = tf.compat.v1.ConfigProto() # self.config.gpu_options.per_process_gpu_memory_fraction = 0.4 self.sess = tf.compat.v1.Session(config=self.config) _ = self.sess.run( self.output_tensors, feed_dict={self.input_tensor: self.createRandomSample()})
def __init__(self): self.SIZE = [416, 416] self.classes = utils.read_coco_names('./data/coco.names') self.num_classes = len(self.classes) self.input_tensor, self.output_tensors = utils.read_pb_return_tensors( tf.get_default_graph(), "./checkpoint/yolov3_gpu_nms.pb", ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"]) self.sess = tf.Session() self.LOWER_RED_RANGE = np.array([17, 15, 100]) self.UPPER_RED_RANGE = np.array([50, 56, 200]) self.pitch_rate = 0 self.yaw_rate = 0 self.vertical_rate = 0 self.TARGET = 0 self.drone_centroid = (int(856 / 2), int(480 * (0.4)) ) # drone_centroid self.result = None
def __init__(self): self.IMAGE_H = 416 self.IMAGE_W = 416 self.classes = utils.read_coco_names('./data/coco.names') self.num_classes = len(self.classes) self.gpu_nms_graph = tf.Graph() self.input_tensor, self.output_tensors = utils.read_pb_return_tensors( self.gpu_nms_graph, "./checkpoint/yolov3_gpu_nms.pb", ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"]) self.sess = tf.Session(graph=self.gpu_nms_graph) self.last_PIL_image = None # 原图 self.last_boxes = None self.last_scores = None self.last_labels = None self.colors = [[254.0, 254.0, 254], [248.92, 228.6, 127], [243.84, 203.20000000000002, 0], [238.76, 177.79999999999998, -127], [233.68, 152.4, -254], [228.6, 127.0, 254], [223.52, 101.60000000000001, 127], [218.44, 76.20000000000002, 0], [213.35999999999999, 50.79999999999999, -127], [208.28000000000003, 25.399999999999995, -254], [203.20000000000002, 0.0, 254], [198.12, -25.400000000000023, 127], [193.04, -50.79999999999999, 0], [187.96, -76.20000000000002, -127], [182.88, -101.59999999999998, -254], [177.79999999999998, -127.0, 254], [172.71999999999997, -152.40000000000003, 127], [167.64, -177.79999999999998, 0], [162.56, -203.20000000000002, -127], [157.48, -228.59999999999997, -254], [152.4, -254.0, 254], [147.32000000000002, -279.40000000000003, 127], [142.24, -304.80000000000007, 0], [137.16, -330.19999999999993, -127], [132.08, -355.59999999999997, -254], [127.0, 254.0, 254], [121.92, 228.6, 127], [116.83999999999999, 203.20000000000002, 0], [111.75999999999999, 177.79999999999998, -127], [106.68, 152.4, -254], [101.60000000000001, 127.0, 254], [96.52, 101.60000000000001, 127], [91.44, 76.20000000000002, 0], [86.35999999999999, 50.79999999999999, -127], [81.27999999999999, 25.399999999999995, -254], [76.20000000000002, 0.0, 254], [71.12, -25.400000000000023, 127], [66.04, -50.79999999999999, 0], [60.96, -76.20000000000002, -127], [55.879999999999995, -101.59999999999998, -254], [50.79999999999999, -127.0, 254], [45.72000000000001, -152.40000000000003, 127], [40.64000000000001, -177.79999999999998, 0], [35.56, -203.20000000000002, -127], [30.48, -228.59999999999997, -254], [25.399999999999995, -254.0, 254], [20.31999999999999, -279.40000000000003, 127], [15.240000000000013, -304.80000000000007, 0], [10.160000000000009, -330.19999999999993, -127], [5.0800000000000045, -355.59999999999997, -254], [0.0, 254.0, 254], [-5.0800000000000045, 228.6, 127], [-10.160000000000009, 203.20000000000002, 0], [-15.240000000000013, 177.79999999999998, -127], [-20.320000000000018, 152.4, -254], [-25.400000000000023, 127.0, 254], [-30.480000000000025, 101.60000000000001, 127], [-35.559999999999974, 76.20000000000002, 0], [-40.63999999999998, 50.79999999999999, -127], [-45.719999999999985, 25.399999999999995, -254], [-50.79999999999999, 0.0, 254], [-55.879999999999995, -25.400000000000023, 127], [-60.96, -50.79999999999999, 0], [-66.04, -76.20000000000002, -127], [-71.12, -101.59999999999998, -254], [-76.20000000000002, -127.0, 254], [-81.28000000000002, -152.40000000000003, 127], [-86.36000000000001, -177.79999999999998, 0], [-91.44000000000003, -203.20000000000002, -127], [-96.51999999999997, -228.59999999999997, -254], [-101.59999999999998, -254.0, 254], [-106.67999999999998, -279.40000000000003, 127], [-111.75999999999999, -304.80000000000007, 0], [-116.83999999999999, -330.19999999999993, -127], [-121.92, -355.59999999999997, -254], [-127.0, 254.0, 254], [-132.08, 228.6, 127], [-137.16, 203.20000000000002, 0], [-142.24, 177.79999999999998, -127], [-147.32000000000002, 152.4, -254]] self.label_name = ['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
# Created date: 2018-11-30 15:47:45 # Description : # #================================================================ import tensorflow as tf from core import utils, yolov3 from core.dataset import dataset, Parser sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 8 EPOCHS = 2000 * 1000 LR = 0.0001 SHUFFLE_SIZE = 1000 CLASSES = utils.read_coco_names('./data/voc.names') ANCHORS = utils.get_anchors('./data/voc_anchors.txt') NUM_CLASSES = len(CLASSES) train_tfrecord = "../VOC/train/voc_train*.tfrecords" test_tfrecord = "../VOC/test/voc_test*.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) testset = dataset(parser, test_tfrecord, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = tf.cond(is_training, lambda: trainset.get_next(), lambda: testset.get_next()) images, *y_true = example
import sys import numpy as np import tensorflow as tf from tqdm import tqdm from PIL import Image from core import utils, yolov3 from core.dataset import dataset, Parser import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = str(1) sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 CLASSES = utils.read_coco_names('./sixray.names') # todo NUM_CLASSES = len(CLASSES) ANCHORS = utils.get_anchors('./sixray_anchors.txt', IMAGE_H, IMAGE_W) # todo CKPT_FILE = "./checkpoint/yolov3_sixray.ckpt-2500" # todo IOU_THRESH = 0.5 SCORE_THRESH = 0.3 all_detections = [] all_annotations = [] all_aver_precs = {CLASSES[i]: 0. for i in range(NUM_CLASSES)} test_tfrecord = "./sixray_*.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) testset = dataset(parser, test_tfrecord, batch_size=1,
import cv2 import numpy as np import tensorflow as tf from core import utils from PIL import Image from core.dataset import Parser, dataset sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 1 SHUFFLE_SIZE = 1 train_tfrecord = "./apple_dataset/apple_*.tfrecords" anchors = utils.get_anchors('./data/apple_anchors.txt', IMAGE_H, IMAGE_W) classes = utils.read_coco_names('./data/apple.names') num_classes = len(classes) parser = Parser(IMAGE_H, IMAGE_W, anchors, num_classes, debug=True) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) is_training = tf.placeholder(tf.bool) example = trainset.get_next() for l in range(20): image, boxes = sess.run(example) image, boxes = image[0], boxes[0] n_box = len(boxes) for i in range(n_box): image = cv2.rectangle(image,(int(float(boxes[i][0])),
config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) IMAGE_H, IMAGE_W = 416, 416 #输入网络模型图像尺寸 BATCH_SIZE = 8 # batch size 每次读取多少张图像 STEPS = 315200 #训练迭代的最大次数 #STEPS = 335000 #训练迭代的最大次数 LR = 0.0001 # if Nan, set 0.0005, 0.0001 初始学习率设置为0.0001 DECAY_STEPS = 100 #训练多少步之后对学习率进行一次调整 DECAY_RATE = 0.999 #学习率调整的比例 SHUFFLE_SIZE = 1000 #打散的个数 CLASSES = utils.read_coco_names('./data/mask.names') #类别数据 ANCHORS = utils.get_anchors('./data/mask_anchors.txt', IMAGE_H, IMAGE_W) #归一化锚框 NUM_CLASSES = len(CLASSES) #类别数 EVAL_INTERNAL = 1000 #每100步评估一次 recall和precision SAVE_INTERNAL = 1000 #每500步保存一次模型 train_tfrecord = "./train_data_tfrecord/train.tfrecords" test_tfrecord = "./train_data_tfrecord/test.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) testset = dataset(parser, test_tfrecord, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = tf.cond(is_training, lambda: trainset.get_next(),
# Author : YunYang1994 # Created date: 2019-01-23 10:21:50 # Description : # #================================================================ import numpy as np import tensorflow as tf from PIL import Image from core import utils import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = str(0) IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('./sixray.names') # todo num_classes = len(classes) image_path = "/media/kent/DISK2/SBRI_Project/dataset_sixray/SIXray-master/ks3util-1.1.1-upload/JPEGImage/P08852.jpg" # 181, img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors( cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes,
import numpy as np import tensorflow as tf from PIL import Image from core import utils import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" config = tf.ConfigProto() config.gpu_options.allow_growth = True IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('./data/raccoon.names') num_classes = len(classes) image_path = "data/hide_specific_target_dataset/000000336584.jpg" # 181, img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors( cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.3, iou_thresh=0.5)
p1x = box[0] * xDist p1y = box[1] * yDist p2x = box[2] * xDist p2y = box[3] * yDist return int(p1x), int(p1y), int(p2x), int(p2y) classesPath = "/home/benoit/Documents/Stage2A/tensorflow-yolov3/data/coco.names" modelPath = "/home/benoit/Documents/Stage2A/tensorflow-yolov3/checkpoint/yolov3_cpu_nms.pb" destPath = "/home/benoit/Documents/Stage2A/resources/results/detection" IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names(classesPath) num_classes = len(classes) input_tensor, output_tensors = utils.read_pb_return_tensors( tf.get_default_graph(), modelPath, ["Placeholder:0", "concat_9:0", "mul_6:0"]) videosPaths = { "CP": "/home/benoit/Documents/Stage2A/resources/CP_dataset/data/P2L_S5_C3.1/P2L_S5_C3.1.mp4", # "PCDS": "/home/benoit/Documents/Stage2A/resources/PCDS_dataset/25_20160407_back/normal/crowd/2016_04_07_18_24_54BackColor.avi", # "MIVIA": "/home/benoit/Documents/Stage2A/resources/MIVIA_dataset/Dataset People Counting MIVIA/DBc/VIDEOS/RGB/C_I_S_1.mkv", # "MOT": "/home/benoit/Documents/Stage2A/resources/MOT_dataset/2DMOT2015/train/PETS09-S2L1/img1/PETS09-S2L1.mp4" } with tf.Session() as sess: for name, videoPath in videosPaths.items():
import cv2 import numpy as np import tensorflow as tf from core import utils from PIL import Image from core.dataset import Parser, dataset sess = tf.Session() BATCH_SIZE = 1 SHUFFLE_SIZE = 1 CLASSES = utils.read_coco_names('./data/class.names') ANCHORS = utils.get_anchors('./data/anchors.txt') NUM_CLASSES = len(CLASSES) TRAIN_TFRECORD = "./data/train_data/val.tfrecords" TEST_TFRECORD = "./data/val_data/val.tfrecords" parser_train = Parser(ANCHORS, NUM_CLASSES) parser_test = Parser(ANCHORS, NUM_CLASSES) trainset = dataset(parser_train, TRAIN_TFRECORD, BATCH_SIZE, shuffle=SHUFFLE_SIZE, multi_image_size=False) testset = dataset(parser_test, TEST_TFRECORD, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = testset.get_next() for l in range(1): res = sess.run(example) image = res[0][0] * 255 y_true = res[1:] boxes = utils.decode_gtbox(y_true) n_box = len(boxes) for i in range(n_box): image = cv2.rectangle(image,(int(float(boxes[i][0])), int(float(boxes[i][1]))),
import cv2 import numpy as np import tensorflow as tf from core import utils from PIL import Image from core.dataset import Parser, dataset sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 1 SHUFFLE_SIZE = 1 train_tfrecord = "../data/voc_data/images_train.tfrecords" anchors = utils.get_anchors('../data/objects.txt', IMAGE_H, IMAGE_W) classes = utils.read_coco_names('../data/objects.names') # print(classes) num_classes = len(classes) # 识别的种类 parser = Parser(IMAGE_H, IMAGE_W, anchors, num_classes, debug=True) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) is_training = tf.placeholder(tf.bool) example = trainset.get_next() for l in range(1): image, boxes = sess.run(example) # print(image) # print(sess.run(example)) image, boxes = image[0], boxes[0]
#! /usr/bin/env python # coding=utf-8 import numpy as np import tensorflow as tf from PIL import Image from core import utils IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('data/SVHN/SVHN.names') num_classes = len(classes) image_path = "data/SVHN/PaddingTest/1.png" # 181, img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors( cpu_nms_graph, "checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.3, iou_thresh=0.5) image = utils.draw_boxes(img, boxes, scores,
# Editor : VIM # File name : f**k.py # Author : YunYang1994 # Created date: 2019-01-23 10:21:50 # Description : # #================================================================ import numpy as np import tensorflow as tf from PIL import Image from core import utils IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('bdd100k/bdd100k.names') num_classes = len(classes) image_path = "D:/self-driving/tensorflow-yolov3-master/bdd100k/data/val/b4b99a3e-21dfc344.jpg" # 181, img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. config = tf.ConfigProto() config.gpu_options.allow_growth = True cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors(cpu_nms_graph, "./checkpoint/yolov3_cpu_nms_bdd.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(config=config,graph=cpu_nms_graph) as sess:
# # Editor : VIM # File name : f**k.py # Author : YunYang1994 # Created date: 2019-01-23 10:21:50 # Description : # #================================================================ import numpy as np import tensorflow as tf from PIL import Image from core import utils IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('./constructionsite_dataset/constructionsite.names') num_classes = len(classes) image_path = "./constructionsite_dataset/VOC2007/train/JPEGImages/23.jpg" img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors(cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run(output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.3, iou_thresh=0.5) image = utils.draw_boxes(img, boxes, scores, labels, classes, [IMAGE_H, IMAGE_W], show=True)
import cfg import numpy as np import tensorflow as tf from PIL import Image from core import utils from core.detect_function import draw_boxes, detection_detail from core.yolov3 import YOLO_V3 classes = utils.read_coco_names(cfg.classes_names_file) # with tf.variable_scope('yolov3'): model = YOLO_V3() sess = tf.Session() for i, k in enumerate(tf.global_variables("yolov3"), start=1): print(i, k.name) sess.run(tf.global_variables_initializer()) # Loading pre_trained weights print("Loading weights ...") sess.run(utils.load_weights(tf.global_variables(scope='yolov3'), cfg.weights_file)) while True: print("=============================================") flag = 0 while flag == 0: img_path = input("input the image path:") try: img = Image.open(img_path) # input RGB format
import numpy as np import tensorflow as tf from core import utils from PIL import Image from core.dataset import Parser, dataset sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 1 SHUFFLE_SIZE = 1 #train_tfrecord = "./raccoon_dataset/raccoon_*.tfrecords" train_tfrecord = "data/tfdata/voc_*.tfrecords" anchors = utils.get_anchors('data/voc_anchors.txt', IMAGE_H, IMAGE_W) classes = utils.read_coco_names('data/voc.names') num_classes = len(classes) parser = Parser(IMAGE_H, IMAGE_W, anchors, num_classes, debug=True) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) is_training = tf.placeholder(tf.bool) example = trainset.get_next() for l in range(10): image, boxes = sess.run(example) image, boxes = image[0], boxes[0] n_box = len(boxes) for i in range(n_box): image = cv2.rectangle(
# Description : # #================================================================ import tensorflow as tf from core import utils, yolov3 INPUT_SIZE = 416 BATCH_SIZE = 1 EPOCHS = 20 LR = 0.001 SHUFFLE_SIZE = 1 weights_path = "/home/yang/test/yolov3.weights" sess = tf.Session() classes = utils.read_coco_names('./data/coco.names') num_classes = len(classes) file_pattern = "./data/train_data/quick_train_data/tfrecords/quick_train_data*.tfrecords" anchors = utils.get_anchors('./data/yolo_anchors.txt') dataset = tf.data.TFRecordDataset(filenames=tf.gfile.Glob(file_pattern)) dataset = dataset.map(utils.parser(anchors, num_classes).parser_example, num_parallel_calls=10) dataset = dataset.repeat().shuffle(SHUFFLE_SIZE).batch(BATCH_SIZE).prefetch( BATCH_SIZE) iterator = dataset.make_one_shot_iterator() example = iterator.get_next() images, *y_true = example model = yolov3.yolov3(num_classes) with tf.variable_scope('yolov3'):
# coding=utf-8 #================================================================ # Copyright (C) 2019 * Ltd. All rights reserved. # # Editor : Vscode # File name : Config.py # Author : # Created date: # Description : # #================================================================ from core import utils IMAGE_H = 416 IMAGE_W = 416 CLASSES = utils.read_coco_names('./data/wider/wider.names') NUM_CLASSES = len(CLASSES) ANCHORS = utils.get_anchors('./data/wider/wider_anchors.txt', IMAGE_H, IMAGE_W) #Training BATCH_SIZE = 16 STEPS = 40000 Learning_Rate = 0.002 # beginning laerining DECAY_STEPS = 5000 # DECAY_RATE = 0.9 # every decay_steps,new_lr=current_lr *dacay_rate SHUFFLE_SIZE = 200 # for data shuffle MAX_TO_KEEP = 2 # training checkpoint SAVE_INTERNAL = 400 # every each steps,save weights to checkpoint EVAL_INTERNAL = 100 # every each steps, evaluate training result Train_tfrecord = "./data/wider/train.tfrecords" Valid_tfrecord = './data/wider/valid.tfrecords'
# Created date: 2018-12-20 11:58:21 # Description : compute mAP # #================================================================ import sys import numpy as np import tensorflow as tf from tqdm import tqdm from PIL import Image from core import utils, yolov3 from core.dataset import dataset, Parser sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 CLASSES = utils.read_coco_names( './constructionsite_dataset/constructionsite.names') NUM_CLASSES = len(CLASSES) ANCHORS = utils.get_anchors( './constructionsite_dataset/constructionsite_anchors.txt', IMAGE_H, IMAGE_W) CKPT_FILE = "./checkpoint/yolov3.ckpt-2500" IOU_THRESH = 0.5 SCORE_THRESH = 0.3 all_detections = [] all_annotations = [] all_aver_precs = {CLASSES[i]: 0. for i in range(NUM_CLASSES)} test_tfrecord = "./tfrecords/voc_*.tfrecords" # test_tfrecord = "./raccoon_dataset/raccoon_*.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES)
import tensorflow as tf from core import utils, yolov3 import cv2 from PIL import Image import numpy as np input_image = "../data/raccoon_data/images/raccoon-4.jpg" image = Image.open(input_image) # image = cv2.imread(input_image) # image = Image.fromarray(image) image_resize = cv2.resize(np.array(image) / 255., (416, 416)) image_place = tf.placeholder(dtype=tf.float32, shape=(None, 416, 416, 3)) CLASSES = utils.read_coco_names('../data/objects.names') ANCHORE = utils.get_anchors("../data/objects.txt", 416, 416) model = yolov3.yolov3(len(CLASSES), ANCHORE) with tf.variable_scope('yolov3'): pred_feature_map = model.forward(image_place, is_training=False) pred = model.predict(pred_feature_map) sess = tf.Session() saver = tf.train.Saver() model_dir = tf.train.latest_checkpoint("../data/raccoon_data/model/") saver.restore(sess, model_dir) boxes, confs, prods = sess.run(pred, feed_dict={image_place: np.expand_dims(image_resize, 0)}) boxes, confs, prods = utils.cpu_nms(boxes, confs * prods, len(CLASSES)) utils.draw_boxes(image, boxes, confs, prods, CLASSES, (416, 416), "../data/font/HuaWenXinWei-1.ttf") print(boxes, confs, prods)
#! /usr/bin/env python # coding=utf-8 import sys import numpy as np import tensorflow as tf from tqdm import tqdm from PIL import Image from core import utils, yolov3 from core.dataset import dataset, Parser sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 # CLASSES = utils.read_coco_names('./data/raccoon.names') CLASSES = utils.read_coco_names('./data/apple.names') NUM_CLASSES = len(CLASSES) # ANCHORS = utils.get_anchors('./data/raccoon_anchors.txt', IMAGE_H, IMAGE_W) ANCHORS = utils.get_anchors('./data/apple_anchors.txt', IMAGE_H, IMAGE_W) CKPT_FILE = "./checkpoint2/yolov3.ckpt-10000" IOU_THRESH = 0.5 SCORE_THRESH = 0.3 all_detections = [] all_annotations = [] all_aver_precs = {CLASSES[i]:0. for i in range(NUM_CLASSES)} # test_tfrecord = "./raccoon_dataset/raccoon_*.tfrecords" # test_tfrecord = "./apple_dataset/apple_*.tfrecords" test_tfrecord = "./apple_dataset/apple_test.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES)
import tensorflow as tf from core import utils, yolov3 from core.dataset import dataset, Parser #此部分为迁移学习进行训练 sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 8 STEPS = 2500 LR = 0.001 # if Nan, set 0.0005, 0.0001 DECAY_STEPS = 100 DECAY_RATE = 0.9 SHUFFLE_SIZE = 200 CLASSES = utils.read_coco_names('../data/raccoon.names') ANCHORS = utils.get_anchors('../data/raccoon_anchors.txt', IMAGE_H, IMAGE_W) NUM_CLASSES = len(CLASSES) EVAL_INTERNAL = 100 SAVE_INTERNAL = 500 train_tfrecord = "../data/train_dome_data/images_train.tfrecords" test_tfrecord = "../data/train_dome_data/images_test.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) testset = dataset(parser, test_tfrecord, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) #占位符 方便session传递参数 example = tf.cond(is_training, lambda: trainset.get_next(), lambda: testset.get_next()) # y_true = [feature_map_1 , feature_map_2 , feature_map_3]
from PIL import Image from core import utils, yolov3 from core.dataset import dataset, Parser coco_ids = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90 ] sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 CLASSES = utils.read_coco_names('./data/coco.names') NUM_CLASSES = len(CLASSES) ANCHORS = utils.get_anchors('./data/coco_anchors.txt', IMAGE_H, IMAGE_W) CKPT_FILE = "/home/common/pretrained_models/checkpoint/yolov3.ckpt" IOU_THRESH = 0.5 SCORE_THRESH = 0.001 all_detections = [] all_annotations = [] all_aver_precs = {CLASSES[i]: 0. for i in range(NUM_CLASSES)} test_tfrecord = "/home/common/datasets/tfrecords/5k.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) testset = dataset(parser, test_tfrecord, batch_size=1,
def main(): global config OutDir = './data/HideSpecificTarget_result/1/' Img_path = "data/coco_test_data/2.jpg" attack_index = 0 # 攻击第几个框 from 0 continuous_round = 20 mark = 0 if os.path.exists(OutDir): shutil.rmtree(OutDir) os.makedirs(OutDir) pNumber = 3 MaxEpoch = 20000 # 迭代上限 Phi1 = 0.4 Phi2 = 1.2 Phi3 = 4 # Phi4 = 1/20 # Phi5 = 1/70 IMAGE_H = 416 IMAGE_W = 416 with tf.Session(config=config) as sess: classes = utils.read_coco_names('./data/coco.names') ###########对原图进行剪切获取SImg############### yolo = YoloTest() source_img = Image.open(Img_path) TImg = np.array(source_img.resize((IMAGE_H, IMAGE_W)), dtype=np.float32)/255. yolo.predict_from_array(TImg) yolo.last_PIL_image = Image.fromarray((255. * TImg).astype('uint8')).convert('RGB') yolo.save_result(os.path.join(OutDir, 'yuanshitupian.jpg' )) original_boxes, scores, labels = yolo.predict_from_array(TImg) assert len(labels) > attack_index # attack first label #attack_index = random.randint(0, len(labels) - 1) attack_index = random.choice(range(len(labels))) print("attack random label: {} ".format(classes[labels[attack_index]])) attcked_label = labels[attack_index] x_min = original_boxes[attack_index][0] y_min = original_boxes[attack_index][1] x_max = original_boxes[attack_index][2] y_max = original_boxes[attack_index][3] center_point = ((x_min+x_max)/2, (y_max+y_min)/2) Top_One_SM = np.zeros(shape=(pNumber), dtype=float) pbestFitness = np.zeros(shape=(pNumber), dtype=float) gbest = np.zeros(shape=(IMAGE_H, IMAGE_W, 3), dtype=float) gbestFitness = -1e8 xOldOld = np.repeat(np.expand_dims(TImg, axis=0), pNumber, axis=0) xOld = np.repeat(np.expand_dims(TImg, axis=0), pNumber, axis=0) pbest = np.zeros(shape=(pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) x = np.zeros(shape=(pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) L2D = np.zeros(shape=(pNumber), dtype=float) Top_One_SM_pbest = np.zeros(shape=(pNumber), dtype=float) L2D_pbest = np.zeros(shape=(pNumber), dtype=float) Top_One_SM_gbest = 0.0 L2D_gbest = 0.0 sign_gbest = 0 # 标记是否有新的gbest生成 Top_One_SM_SUM_round = np.ones(shape=(continuous_round), dtype=float) # 记录每一轮的Top_One_SM_SUM x_round = np.zeros(shape=(continuous_round, pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) # 记录每一轮的x xOld_round = np.zeros(shape=(continuous_round, pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) # 记录每一轮的xOld xOldOld_round = np.zeros(shape=(continuous_round, pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) # 记录每一轮的xOldOld gbest_round = np.zeros(shape=(continuous_round, pNumber, IMAGE_H, IMAGE_W, 3), dtype=float) # 记录每一轮的gbest for i in range(MaxEpoch): print("[Epoch {}]################################################################################################################".format(i)) if i==0: # initialization print( "initialization") for l in range(pNumber): x[l] = TImg + 2 * np.random.randn(IMAGE_H, IMAGE_W, 3) x[l] = np.clip(x[l], 0.0, 1.0) L2D[l] = np.linalg.norm(TImg - x[l]) xOld = np.copy(x) xOldOld = np.copy(xOld) x_round[i] = np.copy(x) xOld_round[i] = np.copy(xOld) xOldOld_round[i] = np.copy(xOldOld) ##########计算 标签得分############## for j in range(pNumber): boxes, scores, labels = yolo.predict_from_array(x[j]) Top_One_SM[j] = 50 for m, label in enumerate(labels): if label == attcked_label: # 如果原框还在,依据得分惩罚它 if is_point_in_box(center_point, boxes[m]): Top_One_SM[j] = 0 break Top_One_SM_SUM = 0 for b in range(pNumber): Top_One_SM_SUM = Top_One_SM_SUM + Top_One_SM[b] Top_One_SM_SUM_round[i] = Top_One_SM_SUM fitness = Top_One_SM - L2D pbestFitness = np.copy(fitness) pbest = np.copy(x) Top_One_SM_pbest = np.copy(Top_One_SM) L2D_pbest = np.copy(L2D) for m in range(pNumber): if pbestFitness[m] > gbestFitness: gbestFitness = pbestFitness[m] gbest = np.copy(pbest[m]) Top_One_SM_gbest = Top_One_SM[m] L2D_gbest = L2D[m] gbest_round[i] = np.copy(gbest) yolo.predict_from_array(gbest) yolo.last_PIL_image = Image.fromarray((255. * gbest).astype('uint8')).convert('RGB') yolo.save_result(os.path.join(OutDir, 'aaaaagbest_query_%d_TopOneSm_%.4f_L2_%.1f.jpg' %(i, Top_One_SM_gbest, L2D_gbest))) print("l2distance:", L2D ) print("Label Score", Top_One_SM) else: if mark == 0: Top_One_SM_SUM_round_Sum = 0 # 求 Top_One_SM_SUM_round中第1到最后一个元素的和 for d in range(1, continuous_round): Top_One_SM_SUM_round_Sum += Top_One_SM_SUM_round[d] if bool(1 - (Top_One_SM_SUM_round[0] != 0 and Top_One_SM_SUM_round_Sum == 0)): pv = Phi1 * ((xOld - xOldOld) / 2 + Phi2 * np.random.rand() * (pbest - x) + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / 20 + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 70) x = x + pv x = np.clip(x, 0.0, 1.0) print('Now in the Rapid iteration###################################################################################') if i <= continuous_round - 1: x_round[i] = np.copy(x) else: x_round[:continuous_round - 1] = np.copy(x_round[1:]) x_round[continuous_round - 1] = np.copy(x) else: x = x_round[0] xOld = xOld_round[0] xOldOld = xOldOld_round[0] gbest = gbest_round[0] mark = 1 else: if i < 30: guanxing = Phi1 * (xOld - xOldOld) / 2 pbest_x = Phi1 * Phi2 * np.random.rand() * (pbest - x) gbest_x = Phi1 * Phi3 * np.random.rand() * (gbest - x) SImg_x = Phi1 * (TImg - x) / 100 rand_num_list = Phi1 * np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 100 pv = guanxing + pbest_x + gbest_x + SImg_x + rand_num_list x = x + pv x = np.clip(x, 0.0, 1.0) print('x:', x[0][0][0]) print('guanxing:', guanxing[0][0][0]) print('pbest_x:', pbest_x[0][0][0]) print('gbest_x:', gbest_x[0][0][0]) print('SImg_x:', SImg_x[0][0][0]) print('rand_num_list:', rand_num_list[0][0][0]) print('pv:', pv[0][0][0]) elif i < 100: pv = Phi1 * ((xOld - xOldOld) / 2 + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / 200 + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 70) x = x + pv x = np.clip(x, 0.0, 1.0) elif i < 600: pv = Phi1 * ((xOld - xOldOld) / 4 + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / (0.65 * i + 170) + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 70) x = x + pv x = np.clip(x, 0.0, 1.0) elif i < 1000: pv = Phi1 * ((xOld - xOldOld) / 5 + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / 400 + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 100) # 此处pv并没有加约束,v使用的是标准正态分布,可以更改 x = x + pv x = np.clip(x, 0.0, 1.0) elif i < 1500: pv = Phi1 * ((xOld - xOldOld) / 5 + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / 600 + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 200) # 此处pv并没有加约束,v使用的是标准正态分布,可以更改 x = x + pv x = np.clip(x, 0.0, 1.0) else: pv = Phi1 * ((xOld - xOldOld) / 5 + Phi3 * np.random.rand() * (gbest - x) + (TImg - x) / 800 + np.random.randn(pNumber, IMAGE_H, IMAGE_W, 3) / 300) # 此处pv并没有加约束,v使用的是标准正态分布,可以更改 x = x + pv x = np.clip(x, 0.0, 1.0) xOldOld = np.copy(xOld) xOld = np.copy(x) for j in range(pNumber): L2D[j] = np.linalg.norm(TImg - x[j]) boxes, scores, labels = yolo.predict_from_array(x[j]) Top_One_SM[j] = 50 for m, label in enumerate(labels): if label == attcked_label: # 如果原框还在,依据得分惩罚它 if is_point_in_box(center_point, boxes[m]): Top_One_SM[j] = 0 break fitness = Top_One_SM - L2D if mark == 0: Top_One_SM_SUM = 0 for b in range(pNumber): Top_One_SM_SUM = Top_One_SM_SUM + Top_One_SM[b] if i <= continuous_round - 1: xOld_round[i] = np.copy(xOld) xOldOld_round[i] = np.copy(xOldOld) Top_One_SM_SUM_round[i] = Top_One_SM_SUM else: xOld_round[:continuous_round - 1] = np.copy(xOld_round[1:]) xOld_round[continuous_round - 1] = np.copy(xOld) xOldOld_round[:continuous_round - 1] = np.copy(xOldOld_round[1:]) xOldOld_round[continuous_round - 1] = np.copy(xOldOld) Top_One_SM_SUM_round[:continuous_round - 1] = Top_One_SM_SUM_round[1:] Top_One_SM_SUM_round[continuous_round - 1] = Top_One_SM_SUM for e in range(pNumber): ##########更新pbest和pbestFitness############### if fitness[e] > pbestFitness[e]: pbestFitness[e] = fitness[e] pbest[e] = np.copy(x[e]) Top_One_SM_pbest[e] = Top_One_SM[e] L2D_pbest[e] = L2D[e] for e in range(pNumber): ##########更新gbest和gbestFitness############### if pbestFitness[e] > gbestFitness: sign_gbest = 1 gbestFitness = pbestFitness[e] gbest = np.copy(pbest[e]) Top_One_SM_gbest = Top_One_SM_pbest[e] L2D_gbest = L2D_pbest[e] if mark == 0: if i <= continuous_round - 1: gbest_round[i] = np.copy(gbest) else: gbest_round[:continuous_round - 1] = np.copy(gbest_round[1:]) gbest_round[continuous_round - 1] = np.copy(gbest) if sign_gbest == 1: yolo.predict_from_array(gbest) yolo.last_PIL_image = Image.fromarray((255. * gbest).astype('uint8')).convert('RGB') yolo.save_result(os.path.join(OutDir, 'aaaaagbest_query_%d_TopOneSm_%.4f_L2_%.1f.jpg' % (i, Top_One_SM_gbest, L2D_gbest))) sign_gbest = 0 # print() print("l2distance:", L2D) print("Label Score:", Top_One_SM)
#! /usr/bin/env python # coding=utf-8 import tensorflow as tf from core import utils, yolov3 from core.dataset import dataset, Parser from PIL import Image sess = tf.Session() IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 1 SHUFFLE_SIZE = 200 CLASSES = utils.read_coco_names("data/SVHN/SVHN.names") ANCHORS = utils.get_anchors("data/SVHN/SVHN_anchors.txt", IMAGE_H, IMAGE_W) NUM_CLASSES = len(CLASSES) test_tfrecord = "data/SVHN/tfrecords/quick_test_data.tfrecords" parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) testset = dataset(parser, test_tfrecord, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = testset.get_next() images, *y_true = example model = yolov3.yolov3(NUM_CLASSES, ANCHORS) with tf.variable_scope('yolov3'): pred_feature_map = model.forward(images, is_training=is_training) loss = model.compute_loss(pred_feature_map, y_true) y_pred = model.predict(pred_feature_map)
class_name_path = "bdd100k/bdd100k.names" train_tfrecord = "bdd100k/train_tf.tfrecords" test_tfrecord = "bdd100k/val_tf.tfrecords" dataname="bdd100k" IMAGE_H, IMAGE_W = 416, 416 BATCH_SIZE = 8 STEPS = 25000 LR = 0.001 # if Nan, set 0.0005, 0.0001 DECAY_STEPS = 100 DECAY_RATE = 0.9 SHUFFLE_SIZE = 200 CLASSES = utils.read_coco_names(class_name_path) ANCHORS = utils.get_anchors('data/voc_anchors.txt', IMAGE_H, IMAGE_W) NUM_CLASSES = len(CLASSES) EVAL_INTERNAL = 100 SAVE_INTERNAL = 500 parser = Parser(IMAGE_H, IMAGE_W, ANCHORS, NUM_CLASSES) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) testset = dataset(parser, test_tfrecord , BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = tf.cond(is_training, lambda: trainset.get_next(), lambda: testset.get_next()) images, *y_true = example
import cv2 import numpy as np import tensorflow as tf from core import utils from PIL import Image from core.dataset import Parser, dataset sess = tf.Session() INPUT_SIZE = 416 BATCH_SIZE = 1 SHUFFLE_SIZE = 1 train_tfrecord = "/home/yang/test/highwaydata/tfrecords/highwaydata*.tfrecords" test_tfrecord = "/home/yang/test/highwaydata/tfrecords/highwaydata*.tfrecords" anchors = utils.get_anchors('./data/coco_anchors.txt') classes = utils.read_coco_names('./data/vehicle.names') num_classes = len(classes) parser = Parser(416, 416, anchors, num_classes, debug=True) trainset = dataset(parser, train_tfrecord, BATCH_SIZE, shuffle=SHUFFLE_SIZE) testset = dataset(parser, test_tfrecord, BATCH_SIZE, shuffle=None) is_training = tf.placeholder(tf.bool) example = tf.cond(is_training, lambda: trainset.get_next(), lambda: testset.get_next()) for l in range(20): image, boxes = sess.run(example, feed_dict={is_training: False}) image, boxes = image[0], boxes[0] n_box = len(boxes)