Esempio n. 1
0
LABELMAP = 'cfg/traffic_signs.names'
FROZEN_MODEL = 'frozen_tiny_model_4000.pb'

with tf.gfile.GFile(FROZEN_MODEL, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

graph = tf.Graph()
with graph.as_default():
    tf.import_graph_def(graph_def, name="")
    inputs = graph.get_tensor_by_name('input_images:0')
    predictions = graph.get_tensor_by_name('outputs:0')

with tf.Session(graph=graph) as sess:
    tf.logging.set_verbosity(tf.logging.INFO)
    classes = utils.load_classes(LABELMAP)

    image_list = glob.glob(os.path.join(images_dir, '*.png'))
    image_list += glob.glob(os.path.join(images_dir, '*.jpg'))
    # image_list.sort()
    index = 0
    while index < len(image_list):
        image = cv2.imread(image_list[index])
        if image.shape[0] > image.shape[1]:
            image = imutils.resize(image, width=450)
        else:
            image = imutils.resize(image, width=900)
        original_height = image.shape[0]
        original_width = image.shape[1]
        resized_image = utils.resize_image(image, 416)
        resized_image = np.expand_dims(resized_image, 0)
Esempio n. 2
0
 def __init__(self):
     self.mmtx = load_market_matrix()
     self.trmdict = load_terms_dict()
     self.clstbl = load_classes()
     self.num_docs = len(self.clstbl)
     self.clsdict = {0:"business",1:"entertainment",2:"politics",3:"sport",4:"tech"}
import matplotlib.patches as patches
from PIL import Image

config_path = 'config/yolov3.cfg'
weights_path = 'config/yolov3.weights'
class_path = 'config/coco.names'
img_size = 416
conf_thres = 0.8
nms_thres = 0.4

# Load model and weights
model = Darknet(config_path, img_size=img_size)
model.load_weights(weights_path)
# model.cuda()
model.eval()
classes = utils.load_classes(class_path)
# Tensor = torch.cuda.FloatTensor
Tensor = torch.FloatTensor


def detect_image(img):
    # scale and pad image
    ratio = min(img_size / img.size[0], img_size / img.size[1])
    imw = round(img.size[0] * ratio)
    imh = round(img.size[1] * ratio)
    img_transforms = transforms.Compose([
        transforms.Resize((imh, imw)),
        transforms.Pad((max(int(
            (imh - imw) / 2), 0), max(int(
                (imw - imh) / 2), 0), max(int(
                    (imh - imw) / 2), 0), max(int((imw - imh) / 2), 0)),
Esempio n. 4
0
def detect(
    kitti_weights="../checkpoints/best_weights_kitti.pth",
    config_path="../config/yolov3-kitti.cfg",
    class_path="../data/names.txt",
):
    """
        Script to run inference on sample images. It will store all the inference results in /output directory (relative to repo root)

        Args
            kitti_weights: Path of weights
            config_path: Yolo configuration file path
            class_path: Path of class names txt file

    """
    cuda = torch.cuda.is_available()
    os.makedirs("../output", exist_ok=True)

    # Set up model
    model = Darknet(config_path, img_size=416)
    model.load_weights(kitti_weights)

    if cuda:
        model.cuda()
        print("Cuda available for inference")

    model.eval()  # Set in evaluation mode

    dataloader = DataLoader(
        ImageFolder("../data/samples/", img_size=416),
        batch_size=2,
        shuffle=False,
        num_workers=0,
    )

    classes = load_classes(class_path)  # Extracts class labels from file

    Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

    imgs = []  # Stores image paths
    img_detections = []  # Stores detections for each image index

    print("data size : %d" % len(dataloader))
    print("\nPerforming object detection:")
    prev_time = time.time()
    for batch_i, (img_paths, input_imgs) in enumerate(dataloader):
        # Configure input
        input_imgs = Variable(input_imgs.type(Tensor))

        # Get detections
        with torch.no_grad():
            detections = model(input_imgs)
            detections = non_max_suppression(detections, 80, 0.8, 0.4)
            # print(detections)

        # Log progress
        current_time = time.time()
        inference_time = datetime.timedelta(seconds=current_time - prev_time)
        prev_time = current_time
        print("\t+ Batch %d, Inference Time: %s" % (batch_i, inference_time))

        # Save image and detections
        imgs.extend(img_paths)
        img_detections.extend(detections)

    # Bounding-box colors
    # cmap = plt.get_cmap('tab20b')
    cmap = plt.get_cmap("tab10")
    colors = [cmap(i) for i in np.linspace(0, 1, 20)]

    print("\nSaving images:")
    # Iterate through images and save plot of detections
    for img_i, (path, detections) in enumerate(zip(imgs, img_detections)):

        print("(%d) Image: '%s'" % (img_i, path))

        # Create plot
        img = np.array(Image.open(path))
        plt.figure()
        fig, ax = plt.subplots(1)
        ax.imshow(img)

        kitti_img_size = 416

        # The amount of padding that was added
        pad_x = max(img.shape[0] - img.shape[1],
                    0) * (kitti_img_size / max(img.shape))
        pad_y = max(img.shape[1] - img.shape[0],
                    0) * (kitti_img_size / max(img.shape))
        # Image height and width after padding is removed
        unpad_h = kitti_img_size - pad_y
        unpad_w = kitti_img_size - pad_x

        # Draw bounding boxes and labels of detections
        if detections is not None:
            print(type(detections))
            print(detections.size())
            unique_labels = detections[:, -1].cpu().unique()
            n_cls_preds = len(unique_labels)
            bbox_colors = random.sample(colors, n_cls_preds)
            for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
                print("\t+ Label: %s, Conf: %.5f" %
                      (classes[int(cls_pred)], cls_conf.item()))
                # Rescale coordinates to original dimensions
                box_h = int(((y2 - y1) / unpad_h) * (img.shape[0]))
                box_w = int(((x2 - x1) / unpad_w) * (img.shape[1]))
                y1 = int(((y1 - pad_y // 2) / unpad_h) * (img.shape[0]))
                x1 = int(((x1 - pad_x // 2) / unpad_w) * (img.shape[1]))

                color = bbox_colors[int(
                    np.where(unique_labels == int(cls_pred))[0])]
                # Create a Rectangle patch
                bbox = patches.Rectangle(
                    (x1, y1),
                    box_w,
                    box_h,
                    linewidth=2,
                    edgecolor=color,
                    facecolor="none",
                )
                # Add the bbox to the plot
                ax.add_patch(bbox)
                # Add label
                plt.text(
                    x1,
                    y1 - 30,
                    s=classes[int(cls_pred)] + " " +
                    str("%.4f" % cls_conf.item()),
                    color="white",
                    verticalalignment="top",
                    bbox={
                        "color": color,
                        "pad": 0
                    },
                )

        # Save generated image with detections
        plt.axis("off")
        plt.gca().xaxis.set_major_locator(NullLocator())
        plt.gca().yaxis.set_major_locator(NullLocator())
        plt.savefig("../output/%d.png" % (img_i),
                    bbox_inches="tight",
                    pad_inches=0.0)
        plt.close()
Esempio n. 5
0
parser.add_argument('-w', '--model-weight', default=cfg.WEIGHT_DIR, type=str, metavar='PATH',
                    help='path to latest checkpoint (default: {})'.format(cfg.WEIGHT_DIR))
args = parser.parse_args()
print(args)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

trans = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])

classes_name = load_classes(cfg.CLASSES_NAME_DIR)

# Input
img = Image.open(args.image)
img = trans(img)
img = img.unsqueeze(0)
img = img.to(device)

# Create model
print("=> creating model DFL-CNN...")
model = DFL_VGG16(nclass=cfg.NUM_CLASSES)
model = model.to(device)

# load checkpoint
if args.model_weight:
    if os.path.isfile(args.model_weight):
Esempio n. 6
0
        default="416",
        type=str)

    return parser.parse_args()


args = arg_parse()
images = args.images
batch_size = int(args.bs)
confidence = float(args.confidence)
nms_thesh = float(args.nms_thresh)
start = 0
CUDA = torch.cuda.is_available()

num_classes = 80
classes = load_classes("../data/coco.names")

# Set up the neural network
print("Loading network.....")
model = Darknet(args.cfgfile)
model.load_weights(args.weightsfile)
print("Network successfully loaded")

model.net_info["height"] = args.reso
inp_dim = int(model.net_info["height"])
assert inp_dim % 32 == 0
assert inp_dim > 32

# If there's a GPU availible, put the model on GPU
if CUDA:
    model.cuda()
Esempio n. 7
0
    Args:
        img: image to predict
        model: pytorch model
    Returns:
        class_: class index
        confidence: class confidence 0~1
    """
    # call base prediction function
    output = predict_img(img, model, input_size)
    confidence = output.max(1)[0].item()
    class_idx = output.max(1)[1].item()

    return class_idx, confidence


classes = load_classes('cfg/classes.cfg')


def predict_class_name_and_confidence(img, model, input_size):
    """predict image class and confidence according to trained model

    Args:
        img: image to predict
        model: pytorch model
        input_size: image input size
    Returns:
        class_name: class index
        confidence: class confidence
    """
    class_idx, confidence = predict_class_idx_and_confidence(
        img, model, input_size)