Пример #1
0
def face_detector_loader(pth_path):
    f_detection_model = SSD(2, MobileNetV1(2), is_training=False)
    state = torch.load(pth_path)
    f_detection_model.load_state_dict(state['model_state_dict'])
    predictor = Predictor(f_detection_model, 300)

    return predictor
Пример #2
0
    def __init__(self, config, data_loader=None):
        ssd_config = toml.load(open(config["network"]["ssd_config"]))

        self._use_gpu = config["test"]["use_gpu"]
        self._saved_model_path = config["test"]["save_model_path"]
        self._saved_model_name = config["test"]["save_model_name"]
        self._n_top = config["test"]["top_number"]
        self._prob_min = config["test"]["prob_min"]
        self._overlap_th = config["test"]["overlap_threshold"]
    
        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name()
        self._image_width, self._image_height, self._images_channel = self._data_loader.get_image_info()

        self._ssd = SSD(config["test"], ssd_config, self._data_loader.get_image_info())
        self._ssd.set_model()

        if self._use_gpu:
            config = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8,
                    allow_growth=True
                )
            )
        else:
            config = tf.compat.v1.ConfigProto(
                device_count = {'GPU': 0}
            )

        self._sess = tf.compat.v1.Session(config=config)
        self._saver = tf.compat.v1.train.Saver()
        self._saver.restore(self._sess, self._saved_model_path + "/" + self._saved_model_name)
Пример #3
0
    def __init__(self, config, data_loader=None):
        ssd_config = toml.load(open(config["network"]))

        self._batch_size = config["batch_size"]
        self._epoch = config["epoch"]
        self._val_step = config["val_step"]
        self._use_gpu = config["use_gpu"]
        self._save_model_path = config["save_model_path"]
        self._save_model_name = config["save_model_name"]

        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()

        self._ssd = SSD(param=config,
                        config=ssd_config,
                        image_info=self._data_loader.get_image_info(),
                        output_dim=len(self._label_name))
        self._ssd.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8, allow_growth=True))
        else:
            config_system = tf.compat.v1.ConfigProto(device_count={'GPU': 0})

        self._sess = tf.compat.v1.Session(config=config_system)
        init = tf.compat.v1.global_variables_initializer()
        self._sess.run(init)
        self._saver = tf.compat.v1.train.Saver()

        self._accuracy = 0.0
        self._loss = Loss()
        self._tensorboard_path = "./logs/" + datetime.today().strftime(
            '%Y-%m-%d-%H-%M-%S')
Пример #4
0
    def post(self):
        ret = []
        nms_threshold = 0.5
        cls_threshold = 0.3
        classes = ['Background', 'Knife', 'Horse', 'Human']
        parser = reqparse.RequestParser()
        parser.add_argument('img', required=True)
        args = parser.parse_args()

        imgurl = args['img']
        response = requests.get(imgurl, stream=True)
        img = Image.open(BytesIO(response.content)).convert("RGB")
        width, height = img.size

        model = SSD()
        checkpoint = torch.load(
            'C:\\Users\\Elina\\Desktop\\IoU-balanced-Loss-SSD\\trained_models\\SSD.pth'
        )
        model.load_state_dict(checkpoint["model_state_dict"])
        if torch.cuda.is_available():
            model.cuda()
        model.eval()
        dboxes = generate_dboxes()
        transformer = SimpleTransformer(dboxes, eval=True)
        img, _, _ = transformer(img, torch.zeros(4), torch.zeros(1))
        encoder = Encoder(dboxes)

        if torch.cuda.is_available():
            img = img.cuda()

        with torch.no_grad():
            ploc, plabel = model(img.unsqueeze(dim=0))
            result = encoder.decode_batch(ploc, plabel, nms_threshold, 20)[0]
            loc, label, prob = [r.cpu().numpy() for r in result]
            best = np.argwhere(prob > cls_threshold).squeeze(axis=1)
            loc = loc[best]
            label = label[best]
            prob = prob[best]

            if len(loc) > 0:
                loc[:, 0::2] *= width
                loc[:, 1::2] *= height
                loc = loc.astype(np.int32)
                for box, lb, pr in zip(loc, label, prob):
                    category = classes[lb]
                    xmin, ymin, xmax, ymax = box
                    xmin, ymin, xmax, ymax = self.ensure_legal(
                        xmin, ymin, xmax, ymax, width, height)
                    ret.append({
                        'label':
                        category,
                        'bbox':
                        [float(xmin),
                         float(ymin),
                         float(xmax),
                         float(ymax)]
                    })

        return json.dumps(ret), 200
Пример #5
0
def test_one(path):
    model = SSD()
    checkpoint = torch.load(model_path)
    model.load_state_dict(checkpoint["model_state_dict"])
    if torch.cuda.is_available():
        model.cuda()
    model.eval()
    dboxes = generate_dboxes()
    transformer = SimpleTransformer(dboxes, eval=True)
    img = Image.open(path).convert("RGB")
    img, _, _ = transformer(img, torch.zeros(4), torch.zeros(1))
    encoder = Encoder(dboxes)

    if torch.cuda.is_available():
        img = img.cuda()
    with torch.no_grad():
        ploc, plabel = model(img.unsqueeze(dim=0))
        result = encoder.decode_batch(ploc, plabel, nms_threshold, 20)[0]
        loc, label, prob = [r.cpu().numpy() for r in result]
        best = np.argwhere(prob > cls_threshold).squeeze(axis=1)
        loc = loc[best]
        label = label[best]
        prob = prob[best]
        output_img = cv2.imread(path)
        if len(loc) > 0:
            height, width, _ = output_img.shape
            loc[:, 0::2] *= width
            loc[:, 1::2] *= height
            loc = loc.astype(np.int32)
            for box, lb, pr in zip(loc, label, prob):
                category = classes[lb]
                color = colors[lb]
                xmin, ymin, xmax, ymax = box
                xmin, ymin, xmax, ymax = ensure_legal(xmin, ymin, xmax, ymax,
                                                      width, height)
                cv2.rectangle(output_img, (xmin, ymin), (xmax, ymax), color, 2)
                text_size = cv2.getTextSize(category + " : %.2f" % pr,
                                            cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
                cv2.rectangle(
                    output_img, (xmin, ymin),
                    (xmin + text_size[0] + 3, ymin + text_size[1] + 4), color,
                    -1)
                cv2.putText(output_img, category + " : %.2f" % pr,
                            (xmin, ymin + text_size[1] + 4),
                            cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
        output = output_path + "{}_prediction.jpg".format(
            os.path.splitext(os.path.split(path)[1])[0])
        cv2.imwrite(output, output_img)
Пример #6
0
class Trainer(object):
    def __init__(self, config, data_loader=None):
        ssd_config = toml.load(open(config["network"]))

        self._batch_size = config["batch_size"]
        self._epoch = config["epoch"]
        self._val_step = config["val_step"]
        self._use_gpu = config["use_gpu"]
        self._save_model_path = config["save_model_path"]
        self._save_model_name = config["save_model_name"]

        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()

        self._ssd = SSD(param=config,
                        config=ssd_config,
                        image_info=self._data_loader.get_image_info(),
                        output_dim=len(self._label_name))
        self._ssd.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8, allow_growth=True))
        else:
            config_system = tf.compat.v1.ConfigProto(device_count={'GPU': 0})

        self._sess = tf.compat.v1.Session(config=config_system)
        init = tf.compat.v1.global_variables_initializer()
        self._sess.run(init)
        self._saver = tf.compat.v1.train.Saver()

        self._accuracy = 0.0
        self._loss = Loss()
        self._tensorboard_path = "./logs/" + datetime.today().strftime(
            '%Y-%m-%d-%H-%M-%S')

    def _save_model(self):
        os.makedirs(self._save_model_path, exist_ok=True)
        self._saver.save(self._sess,
                         self._save_model_path + "/" + self._save_model_name)

    def _save_tensorboard(self, loss):
        with tf.name_scope('log'):
            tf.compat.v1.summary.scalar('loss', loss)
            merged = tf.compat.v1.summary.merge_all()
            writer = tf.compat.v1.summary.FileWriter(self._tensorboard_path,
                                                     self._sess.graph)

    def train(self):
        with tqdm(range(self._epoch)) as pbar:
            for i, ch in enumerate(pbar):  #train
                input_images, input_labels = self._data_loader.get_train_data(
                    self._batch_size)

                _, loss, loss_conf, loss_loc = self._ssd.train(
                    self._sess, input_images, input_labels)
                pbar.set_postfix(
                    OrderedDict(loss=loss,
                                loss_conf=loss_conf,
                                loss_loc=loss_loc))

                #self._save_tensorboard(loss)
                self._loss.append(loss, loss_conf, loss_loc)

                if i % self._val_step == 0:  #test
                    self._save_model()
                    self._loss.save_log()
Пример #7
0
def main(opt):
    if torch.cuda.is_available():
        print('Will compute using CUDA')
        # torch.distributed.init_process_group(backend='nccl', init_method='env://')
        # num_gpus = torch.distributed.get_world_size()
        num_gpus = 1
        torch.cuda.manual_seed(123)
    else:
        torch.manual_seed(123)
        num_gpus = 1

    train_params = {
        "batch_size": opt.batch_size * num_gpus,
        "shuffle": True,
        "drop_last": False,
        "num_workers": opt.num_workers,
        "collate_fn": collate_fn
    }

    test_params = {
        "batch_size": opt.batch_size * num_gpus,
        "shuffle": True,
        "drop_last": False,
        "num_workers": opt.num_workers,
        "collate_fn": collate_fn
    }

    dboxes = generate_dboxes()
    model = SSD()
    train_set = OIDataset(SimpleTransformer(dboxes))
    train_loader = DataLoader(train_set, **train_params)
    test_set = OIDataset(SimpleTransformer(dboxes, eval=True), train=False)
    test_loader = DataLoader(test_set, **test_params)

    encoder = Encoder(dboxes)

    opt.lr = opt.lr * num_gpus * (opt.batch_size / 32)
    criterion = Loss(dboxes)

    optimizer = torch.optim.SGD(model.parameters(),
                                lr=opt.lr,
                                momentum=opt.momentum,
                                weight_decay=opt.weight_decay,
                                nesterov=True)
    scheduler = MultiStepLR(optimizer=optimizer,
                            milestones=opt.multistep,
                            gamma=0.1)

    if torch.cuda.is_available():
        model.cuda()
        criterion.cuda()

    model = torch.nn.DataParallel(model)

    if os.path.isdir(opt.log_path):
        shutil.rmtree(opt.log_path)
    os.makedirs(opt.log_path)

    if not os.path.isdir(opt.save_folder):
        os.makedirs(opt.save_folder)
    checkpoint_path = os.path.join(opt.save_folder, "SSD.pth")

    writer = SummaryWriter(opt.log_path)

    if os.path.isfile(checkpoint_path):
        checkpoint = torch.load(checkpoint_path)
        first_epoch = checkpoint["epoch"] + 1
        model.module.load_state_dict(checkpoint["model_state_dict"])
        scheduler.load_state_dict(checkpoint["scheduler"])
        optimizer.load_state_dict(checkpoint["optimizer"])
        # evaluate(model, test_loader, encoder, opt.nms_threshold)
    else:
        first_epoch = 0

    for epoch in range(first_epoch, opt.epochs):
        train(model, train_loader, epoch, writer, criterion, optimizer,
              scheduler)
        evaluate(model, test_loader, encoder, opt.nms_threshold)

        checkpoint = {
            "epoch": epoch,
            "model_state_dict": model.module.state_dict(),
            "optimizer": optimizer.state_dict(),
            "scheduler": scheduler.state_dict()
        }
        torch.save(checkpoint, checkpoint_path)
Пример #8
0
class Inferencer(object):
    def __init__(self, config, data_loader=None):
        ssd_config = toml.load(open(config["network"]["ssd_config"]))

        self._use_gpu = config["test"]["use_gpu"]
        self._saved_model_path = config["test"]["save_model_path"]
        self._saved_model_name = config["test"]["save_model_name"]
        self._n_top = config["test"]["top_number"]
        self._prob_min = config["test"]["prob_min"]
        self._overlap_th = config["test"]["overlap_threshold"]
    
        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name()
        self._image_width, self._image_height, self._images_channel = self._data_loader.get_image_info()

        self._ssd = SSD(config["test"], ssd_config, self._data_loader.get_image_info())
        self._ssd.set_model()

        if self._use_gpu:
            config = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8,
                    allow_growth=True
                )
            )
        else:
            config = tf.compat.v1.ConfigProto(
                device_count = {'GPU': 0}
            )

        self._sess = tf.compat.v1.Session(config=config)
        self._saver = tf.compat.v1.train.Saver()
        self._saver.restore(self._sess, self._saved_model_path + "/" + self._saved_model_name)


    def _save_result(self, image, locs, labels, num):
        image *= 255.
        image = np.clip(image, 0, 255).astype('uint8')
        image = cv2.resize(image, (self._image_width, self._image_height))
        if len(labels) and len(locs):
            for label, loc in zip(labels, locs):
                if 1<(loc[2]-loc[0])*(loc[2]-loc[0]) and label!=20:
                    loc = np.array([loc[0], loc[1], loc[2], loc[3]])

                    cv2.rectangle(image, 
                                (int(loc[0]), int(loc[1])), 
                                (int(loc[2]), int(loc[3])), 
                                (0, 0, 255), 1)

                    cv2.putText(image, 
                            str(self._label_name[int(label)]), 
                            (int(loc[0]), int(loc[1])), 
                            cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 0, 255), 1)
        cv2.imwrite( "./result/test_{}.jpg".format(num), image )
        

    def inference(self):

        num = 0
        #input_images, input_labels = self._data_loader.get_test_data()
        input_images, input_labels = self._data_loader.get_test_data()
        input_images = input_images[:10]
        input_labels = input_labels[:10]
        for image, label in zip(input_images, input_labels):
            pred_confs, pred_locs = self._ssd.inference(self._sess, image)
            locs, labels = self._ssd.detect_objects(pred_confs, pred_locs, self._n_top, self._prob_min, self._overlap_th)
            #print("pred_locs:{}".format(pred_locs))
            #print("locs:{}, labels{}".format(locs, labels))

            self._save_result(image, locs, labels, num)
            num += 1
            if num==1:
                return 0
Пример #9
0
#import pdb; pdb.set_trace()
train_loader = DataLoader(train_set,
                          batch_size=32,
                          shuffle=True,
                          collate_fn=train_set.collate_fn)

test_set = VOCLoader(root='./datasets_raid1/voc/VOC2007',
                     image_set='test',
                     transform=transform)
#import pdb; pdb.set_trace()
test_loader = DataLoader(test_set,
                         batch_size=32,
                         shuffle=False,
                         collate_fn=train_set.collate_fn)

model = SSD(21).to(device)
optimizer = torch.optim.SGD(model.parameters(),
                            lr=1e-3,
                            momentum=0.9,
                            weight_decay=0.0005)
criterion = Loss().to(device)

#import pdb; pdb.set_trace()

epochs = 1
for epoch in range(epochs):
    print("%d/%d" % (epoch, epochs))
    model.train()
    for i, (images, categories, boxes) in enumerate(test_loader):

        #import pdb; pdb.set_trace()