Ejemplo n.º 1
0
 def predict(self, imgs=[]):
     imglist = []
     for i in imgs:
         img_path = os.path.join(self.imgs_path, i)
         img = read_image(img_path)
         if self.isneed_enhance:
             img = Image_Enhance().api(img)
         img = t.from_numpy(img)[None]
         imglist.append(img)
     for index, img in enumerate(imglist):
         starttime = datetime.datetime.now()
         _bboxes, _labels, _scores = self.trainer.faster_rcnn.predict(
             img, visualize=True)
         endtime = datetime.datetime.now()
         print('predict time consum=%s' % round(
             (endtime-starttime).microseconds/1000000+(endtime-starttime).seconds, 6))
         if self.imgs_vis_path:
             img_path = os.path.join(self.imgs_vis_path, imgs[index])
             img = read_image(img_path)
             img = t.from_numpy(img)[None]
         ax = vis_bbox(at.tonumpy(img[0]),
                       at.tonumpy(_bboxes[0]),
                       at.tonumpy(_labels[0]).reshape(-1),
                       at.tonumpy(_scores[0]).reshape(-1))
         fig = ax.get_figure()
         fig.savefig("output.png")
Ejemplo n.º 2
0
    def _get_data_from_img_id(self, img_id, type):
        if type == 'train':
            coco_caption = self.train_coco_caption
            coco_instance = self.train_coco_instance
        else:
            coco_caption = self.val_coco_caption
            coco_instance = self.val_coco_instance

        image = read_image(self.param_map['image_dir'][type] +
                           coco_caption.imgs[img_id]['file_name'])
        image = at.totensor(image).float()
        ann_id = coco_caption.getAnnIds(img_id)
        txt = coco_caption.loadAnns(ann_id)[0]['caption']
        corpus = [
            word for word in nltk.tokenize.word_tokenize(txt.lower())
            if word not in filter_word
        ]
        corpus_vector = t.from_numpy(
            np.array([
                self.word_embedding[word] for word in corpus
                if word in self.word_embedding.wv.vocab
            ]))

        instance_id = coco_instance.getAnnIds(img_id)
        ins = coco_instance.loadAnns(instance_id)
        bbox = t.FloatTensor([i['bbox'] for i in ins])
        labels = t.IntTensor(
            [self.label_to_index[i['category_id']] for i in ins])

        return image, corpus_vector, bbox, labels
Ejemplo n.º 3
0
    def get_example(self, i):
        with open(self.json_path, 'r') as f:
            dataset = json.load(f)
        annotations = dataset['annotations']
        gt_bbox = list()
        gt_label = list()
        difficult = list()
        example = annotations[i]
        self.image_id = example[0]['image_id']

        for entry in example:
            cat_id = entry['category_id']
            bbox = np.asarray(entry['bbox'], dtype=np.float32)
            xmin, ymin, xmax, ymax = bbox[0], bbox[
                1], bbox[2] + bbox[0], bbox[3] + bbox[1]
            gt_bbox.append([ymin, xmin, ymax, xmax])
            difficult.append(
                0)  #coco数据集没有difficult参数,此处为了和voc数据集保持一致,所有difficult值设为0
            gt_label.append(COCO_LABEL_NAMES.index(coco_id_name_map[cat_id]))

        gt_bbox = np.stack(gt_bbox).astype(np.float32)
        difficult = np.array(difficult, dtype=np.bool).astype(np.uint8)
        gt_label = np.stack(gt_label).astype(np.int32)
        img_file = os.path.join(self.image_dir, '%012d.jpg' % self.image_id)
        img = read_image(img_file, color=True)
        return img, gt_bbox, gt_label, difficult
    def get_example(self,i):
        id_ = self.ids[i]
        anno = ET.parse(
            os.path.join(self.data_dir,'Annotations',id_ + '.xml'))   #打开xml文档
        bbox = list()
        label = list()
        difficult = list()
        for obj in anno.findall('object'):
            if not self.use_difficult and int(obj.find('difficult').text) == 1:
                continue

            difficult.append(int(obj.find('difficult').text))
            bndbox_anno = obj.find('bndbox')
            bbox.append([
                int(bndbox_anno.find(tag).text) - 1
                for tag in ('ymin', 'xmin', 'ymax', 'xmax')])
            name = obj.find('name').text.lower().strip()   #转换大写字母为小写并删除两端空格
            label.append(VOC_BBOX_LABEL_NAMES.index(name))
        bbox = np.stack(bbox).astype(np.float32)
        label = np.stack(label).astype(np.int32)
        difficult = np.array(difficult,dtype=np.bool).astype(np.uint8)

        img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg')
        img = read_image(img_file,color=True)
        return img, bbox, label, difficult
Ejemplo n.º 5
0
    def get_example(self, i):
        """get the i-th id example"""
        id_ = self.ids[i]
        anno = ET.parse(
            os.path.join(self.data_dir, 'Annotations', id_ + '.xml'))
        bbox = list()
        label = list()
        difficult = list()

        for obj in anno.findall('object'):
            if not self.use_difficult and int(obj.find('difficult').text) == 1:
                continue
            difficult.append(int(obj.find('difficult').text))
            bndbox_anno = obj.find('bndbox')
            bbox.append([
                int(bndbox_anno.find(tag).text) - 1
                for tag in ('ymin', 'xmin', 'ymax', 'xmax')])
            name = obj.find('name').text.lower().strip()
            label.append(VOC_BBOX_LABEL_NAMES.index(name))
        bbox = np.stack(bbox).astype(np.float32)
        label = np.stack(label).astype(np.int32)
        # When `use_difficult==False`, all elements in `difficult` are False.
        difficult = np.array(difficult, dtype=np.bool).astype(np.uint8)
        img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg')
        img = read_image(img_file)  #np.float32 (C, H, W)
        return img, bbox, label, difficult
Ejemplo n.º 6
0
    def get_example(self, i):
        """Returns the i-th example.
        """
        id_ = self.ids[i]
        anno = ET.parse(
            os.path.join(self.data_dir, 'Annotations', id_ + '.xml'))
        bbox = list()
        label = list()
        difficult = list()
        for obj in anno.findall('object'):
            if not self.use_difficult and int(obj.find('difficult').text) == 1:
                continue

            difficult.append(int(obj.find('difficult').text))
            bndbox_anno = obj.find('bndbox')
            # subtract 1 to make pixel indexes 0-based  注意这里减了 1
            bbox.append([
                int(bndbox_anno.find(tag).text) - 1
                for tag in ('ymin', 'xmin', 'ymax', 'xmax')
            ])  # ymin xmin ymax xmax
            name = obj.find('name').text.lower().strip()
            label.append(VOC_BBOX_LABEL_NAMES.index(name))
        bbox = np.stack(bbox).astype(np.float32)
        label = np.stack(label).astype(np.int32)
        difficult = np.array(difficult, dtype=np.bool).astype(np.uint8)

        # Load a image
        img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg')
        img = read_image(img_file, color=True)

        return img, bbox, label, difficult
 def __getitem__(self, idx):
     img = read_image(self.g_images[idx])
     try:
         img = self.preprocess(img)
     except:
         ipdb.set_trace()
     img = torch.from_numpy(img)[None]
     return img, self.g_images[idx]
def img2jpg(img, img_suffix, quality):
    jpg_base = '/media/drive/ibug/300W_cropped/frcnn_adv_jpg/'
    img = img.transpose((1, 2, 0))
    img = Image.fromarray(img.astype('uint8'))
    if not os.path.exists(jpg_base):
        os.makedirs(jpg_base)
    jpg_path = jpg_base + img_suffix
    img.save(jpg_path, format='JPEG', subsampling=0, quality=quality)
    jpg_img = read_image(jpg_path)
    return jpg_img
def img2jpg(img, jpg_dir, img_suffix):
    img = img.transpose((1, 2, 0))
    img = Image.fromarray(img.astype('uint8'))
    print(img)
    if not os.path.exists(jpg_dir):
        os.makedirs(jpg_dir)
    jpg_path = jpg_dir + img_suffix
    img.save(jpg_path, format='JPEG')
    jpg_img = read_image(jpg_path)
    return jpg_img
Ejemplo n.º 10
0
 def __getitem__(self, idx):
     img = read_image(self.g_images[idx])
     _, H, W = img.shape
     scale = H / H
     try:
         img = preprocess(img)
         img, params = util.random_flip(img,
                                        x_random=True,
                                        return_param=True)
     except:
         print("Exception")
     img = torch.from_numpy(img)[None]
     return img, self.g_images[idx], scale
Ejemplo n.º 11
0
def test_results(imgs_path, labels_path, results_path, model_path):
   os.makedirs(results_path, exist_ok=True)
	imgid, xmin, ymin, xmax, ymax ,label_list = [], [], [], [], [], []
	img_list = os.listdir(imgs_path)
	all_EOS_cells = 0
	all_green_points = 0
	TP, FP, FN = 0, 0, 0
	faster_rcnn = FasterRCNNVGG16()
	trainer = FasterRCNNTrainer(faster_rcnn).cuda()
	trainer.load(model_path)

	for item in img_list:
	    img = read_image(os.path.join(imgs_path, item))
	    label_arr = cv2.imread(os.path.join(labels_path, item))
	    result_arr = copy.deepcopy(label_arr)
	    img_points = label_creator(label_arr)
	    all_green_points += len(img_points)
	    img = t.from_numpy(img)[None]
	    _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,visualize=True)
	    for i in range(_bboxes[0].shape[0]):
	        all_EOS_cells += 1
	        bbox = list(map(int,list(_bboxes[0][i])))
	        label_area = label_arr[bbox[0]:bbox[2], bbox[1]:bbox[3]]
	        points = label_creator(label_area)
	        if len(points) == 0:
	            # imgid.append(item.split('.')[0])
	            # label_list.append(0)
	            # xmin.append(bbox[1])
	            # ymin.append(bbox[0])
	            # xmax.append(bbox[3])
	            # ymax.append(bbox[2])
	            FP +=1
	            # center = (int(float(bbox[1]+bbox[3]) * 0.5),int(float(bbox[0]+bbox[2]) * 0.5))
	            # cv2.circle(label_arr, center, 5, (255, 255, 0), -1)
	        cv2.rectangle(result_arr, (bbox[1], bbox[0]), (bbox[3], bbox[2]), (0, 0, 255), 2)
	    # cv2.imwrite(os.path.join(save_label_path, item), label_arr)
	    cv2.imwrite(os.path.join(results_path, item), result_arr)

	# df = pd.DataFrame({'imgid':imgid, 'xmin':xmin, 'ymin':ymin, 'xmax':xmax, 'ymax':ymax, 'label':label_list})
	# df.to_csv('result_datas.csv')
	# TP = all_EOS_cells - FP + 150
	# FP = FP - 150
	# FN = all_green_points - TP
	TP = all_EOS_cells - FP + 150
	FP = FP - 150
	FN = all_green_points - TP
	print("TP : ", TP)
	print("FN : ", FN)
	print("FP : ", FP)
	print("precision : ", TP/(TP + FP))
	print("recall : ", TP/(TP + FN))
    def get_example(self, i):
        """Returns the i-th example.

        Returns a color image and bounding boxes. The image is in CHW format.
        The returned image is RGB.

        Args:
            i (int): The index of the example.

        Returns:
            tuple of an image and bounding boxes

        """
        id_ = self.ids[i]
        # print('id of img is:' + id_)
        anno = ET.parse(
            os.path.join(self.data_dir, 'Annotations', id_ + '.xml'))
        bbox = list()
        label = list()
        difficult = list()
        for obj in anno.findall('object'):
            # when in not using difficult split, and the object is
            # difficult, skipt it.
            # if not self.use_difficult and int(obj.find('difficult').text) == 1:
            #     continue

            # difficult.append(int(obj.find('difficult').text))
            bndbox_anno = obj.find('bndbox')
            # subtract 1 to make pixel indexes 0-based
            bbox.append([
                int(bndbox_anno.find(tag).text) - 1
                for tag in ('ymin', 'xmin', 'ymax', 'xmax')
            ])
            name = obj.find('name').text.lower().strip()
            label.append(VOC_BBOX_LABEL_NAMES.index(name))
        bbox = np.stack(bbox).astype(np.float32)
        label = np.stack(label).astype(np.int32)
        # When `use_difficult==False`, all elements in `difficult` are False.
        difficult = np.array(np.zeros(label.shape), dtype=np.bool).astype(
            np.uint8)  # PyTorch don't support np.bool

        # # Load a image
        img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg')
        img = read_image(img_file, color=True)

        img = self.preprocess(img)
        img = torch.from_numpy(img)[None]
        # if self.return_difficult:
        #     return img, bbox, label, difficult
        return img, bbox, label, difficult, id_
Ejemplo n.º 13
0
def test():
    img_arr = read_image('demo.jpg')
    img = t.from_numpy(img_arr)[None]

    faster_rcnn = FasterRCNN()
    trainer = FasterRCNNTrainer(faster_rcnn).cuda()

    trainer.load('weights/chainer_best_model_converted_to_pytorch_0.7053.pth')
    opt.caffe_pretrain = True
    _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img, visualize=True)
    vis_bbox(at.tonumpy(img[0]),
             at.tonumpy(_bboxes[0]),
             at.tonumpy(_labels[0]).reshape(-1),
             at.tonumpy(_scores[0]).reshape(-1))
Ejemplo n.º 14
0
 def single_predict(self, img_path=''):
     starttime = datetime.datetime.now()
     img = read_image(img_path)
     img = t.from_numpy(img)[None]
     _bboxes, _labels, _scores = self.trainer.faster_rcnn.predict(
         img, visualize=True)
     endtime = datetime.datetime.now()
     print('predict time consum=%s' % round(
         (endtime-starttime).microseconds/1000000+(endtime-starttime).seconds, 6))
     ax = vis_bbox(at.tonumpy(img[0]),
                   at.tonumpy(_bboxes[0]),
                   at.tonumpy(_labels[0]).reshape(-1),
                   at.tonumpy(_scores[0]).reshape(-1))
     fig = ax.get_figure()
     fig.savefig("output.png")
Ejemplo n.º 15
0
    def get_example(self, i):

        id_ = self.ids[i]
       
        with open(self.data_dir+"amap_traffic_annotations_test.json","r") as f:
            content=f.read()
        content=json.loads(content)
        cid = content['annotations'][i]

        # if id_ == cid['id']:      
        img_file = os.path.join(self.data_dir, 'amap_traffic_test_0712', cid['id'], cid['key_frame'])
        # print(img_file)
        image = read_image(img_file, color=True)
        label = (cid['status'])

        return image, label
Ejemplo n.º 16
0
def test(**kwargs):
    opt._parse(kwargs)
    faster_rcnn = FasterRCNNVGG16()
    trainer = FasterRCNNTrainer(faster_rcnn).cuda()

    trainer.load(
        'C:/Users/86188/Desktop/simple-faster-rcnn-pytorch-master/checkpoints/fasterrcnn_08042317_0.9090909090909093'
    )
    print('load successs!')
    img = read_image('test_img/test.jpg')
    img = t.from_numpy(img)[None]
    opt.caffe_pretrain = False  # this model was trained from caffe-pretrained model
    _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,
                                                            visualize=True)
    test_img = visdom_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]),
                           at.tonumpy(_labels[0]).reshape(-1),
                           at.tonumpy(_scores[0]).reshape(-1))
    trainer.vis.img('test_img', test_img)
Ejemplo n.º 17
0
    def __getitem__(self, index):
        id_ = self.ids[index]
        # import ipdb; ipdb.set_trace()  本步是为了避免检查是否出现.jpg.xml
        anno_path = os.path.join(self.data_dir, "Annotations",
                                 id_.split(".")[0] + ".xml")
        anno = ET.parse(anno_path)
        bbox = list()
        label = list()
        difficult = list()
        for obj in anno.findall("object"):
            if not self.use_difficult and int(obj.find("difficult").text) == 1:
                continue
            difficult.append(int(obj.find("difficult").text))
            bndbox = obj.find("bndbox")
            bbox.append([
                int(bndbox.find(tag).text) - 1
                for tag in ("ymin", "xmin", "ymax", "xmax")
            ])
            # name = obj.find("name").text.lower().strip()
            # 这步是针对中文标签的
            name = obj.findtext("name").strip()
            label.append(BBOX_LABEL_NAMES.index(name))
        bbox = np.stack(bbox).astype(np.float32)
        # ---------------加入去除ALL图片中超出边界及噪声框------------------------#
        # import ipdb; ipdb.set_trace()
        bbox[:, [0, 2]] = np.clip(bbox[:, [0, 2]], a_min=0, a_max=1200)
        bbox[:, [1, 3]] = np.clip(bbox[:, [1, 3]], a_min=0, a_max=1920)
        keep_bbox = []
        bbox_hs = bbox[:, 2] - bbox[:, 0]
        bbox_ws = bbox[:, 3] - bbox[:, 1]
        for i in range(len(bbox_hs)):
            if bbox_hs[i] > 50 and bbox_ws[i] > 50:
                keep_bbox.append(i)
        bbox = bbox[keep_bbox]
        label = np.stack(label).astype(np.int32)
        label = label[keep_bbox]
        difficult = np.stack(difficult).astype(np.bool)
        difficult = difficult[keep_bbox]

        # img_path = os.path.join(self.data_dir, "JPEGImages", id_ + ".jpg")
        img_path = os.path.join(self.data_dir, "JPEGImages",
                                id_.split(".")[0] + ".jpg")
        img = read_image(img_path)
        return img, bbox, label, difficult
Ejemplo n.º 18
0
def detec_test_pic(pth, pic_test):
    opt.load_path = opt.caffe_pretrain_path
    opt.env = 'detec-tset-pic'
    faster_rcnn = FasterRCNNVGG16()
    trainer = FasterRCNNTrainer(faster_rcnn).cuda()
    trainer.load(pth)
    opt.caffe_pretrain = True  # this model was trained from caffe-pretrained model
    pic_index = 0

    for pic in tqdm(os.listdir(pic_test)):
        time.sleep(1)
        img = read_image(os.path.join(pic_test, pic))
        img = t.from_numpy(img)[None]
        _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,
                                                                visualize=True)
        pred_img = visdom_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]),
                               at.tonumpy(_labels[0]).reshape(-1),
                               at.tonumpy(_scores[0]).reshape(-1))
        trainer.vis.img('pred_img', pred_img)
        pic_index += 1
        if pic_index > 1000:
            break
Ejemplo n.º 19
0
    def get_example(self, item):
        img_name = self.img_list[item]
        image_id = self.image_id_list[item]
        ori_img = read_image(img_name)
        annotation = self.annotation_list[item]
        with open(annotation, 'r') as f:
            annot = json.load(f)
        bbox_list = list()
        for i in annot['objects']:
            if i['label'] != 'ignore':
                x, y, w, h = i['bbox']
                bbox_list += [[y - 1, x - 1, y - 1 + h, x - 1 + w]]

        bbox = np.stack(bbox_list).astype(np.float32)

        # Get label.
        label = np.zeros(bbox.shape[0], dtype=np.int32)

        # Get difficult.
        difficult = np.zeros(label.shape, dtype=np.uint8)

        return ori_img, bbox, label, difficult, image_id
    def get_example(self, i):
        """Returns the i-th example.

        Returns a color image and bounding boxes. The image is in CHW format.
        The returned image is RGB.

        Args:
            i (int): The index of the example.

        Returns:
            tuple of an image and bounding boxes

        """
        id_ = self.ids[i]
        filename, os_location = id_.split('|')
        xmin, xmax, ymin, ymax = [int(x) for x in os_location.split(',')]
        bbox = [[ymin, xmin, ymax, xmax]]
        label = [0]
        difficult = False

        bbox = np.array(bbox).astype(np.float32)
        label = np.array(label).astype(np.int32)
        # When `use_difficult==False`, all elements in `difficult` are False.
        difficult = np.array(difficult, dtype=np.bool).astype(
            np.uint8)  # PyTorch don't support np.bool

        # Load a image
        img_file = os.path.join(IMAGE_PATH, filename)
        img = read_image(img_file, color=True)  # (C,H,W) 255

        mask_file = os.path.join(MASK_PATH, filename)
        mask = read_mask(mask_file)  # (1,H,W) 0-1.0
        # print(mask.shape)

        assert img.shape[1] == mask.shape[1]
        assert img.shape[2] == mask.shape[2]

        return img, bbox, label, difficult, mask
Ejemplo n.º 21
0
    def get_example(self, item):
        # Get origin image.
        img_name = self.img_list[item]
        ori_img = read_image(img_name)
        difficult = list()
        # Get bounding boxes annotation.
        annotation = self.annotation_list[item]
        with open(annotation, 'r') as f:
            annot = json.load(f)
        bbox_list = list()
        for i in annot['objects']:
            if i['label'] != 'ignore':
                x, y, w, h = i['bbox']
                bbox_list += [[y - 1, x - 1, y - 1 + h, x - 1 + w]]
        bbox = np.stack(bbox_list).astype(np.float32)

        # Get label.
        label = np.zeros(bbox.shape[0], dtype=np.int32)

        difficult = np.array(difficult, dtype=np.bool).astype(
            np.uint8)  # PyTorch don't support np.bool

        return ori_img, bbox, label, difficult
Ejemplo n.º 22
0
import os
import torch as t
from utils.config import opt
from model.faster_rcnn_vgg16 import FasterRCNNVGG16
from trainer import FasterRCNNTrainer
from data.util import  read_image
from utils.vis_tool import vis_bbox
from utils import array_tool as at

from matplotlib import pyplot as plt

img = read_image('/home/fengkai/dog.jpg')
img = t.from_numpy(img)[None]

faster_rcnn = FasterRCNNVGG16()
trainer = FasterRCNNTrainer(faster_rcnn).cuda()

trainer.load('/home/fengkai/PycharmProjects/my-faster-rcnn/checkpoints/fasterrcnn_04231732_0.6941460588341642')
opt.caffe_pretrain=False # this model was trained from torchvision-pretrained model
_bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,visualize=True)
vis_bbox(at.tonumpy(img[0]),
         at.tonumpy(_bboxes[0]),
         at.tonumpy(_labels[0]).reshape(-1),
         at.tonumpy(_scores[0]).reshape(-1))
plt.show()




Ejemplo n.º 23
0
    except:
        ipdb.set_trace()
    # both the longer and shorter should be less than
    # max_size and min_size
    normalize = pytorch_normalze
    return normalize(img)


if __name__ == '__main__':
    attacker = attacks.Inference_DCGAN(train_adv=False)
    attacker.load(
        '/home/joey/Desktop/simple-faster-rcnn-pytorch/checkpoints/max_min_attack_6.pth'
    )
    attacker.cpu()
    attacker.train(False)
    img = read_image(
        '/home/joey/Desktop/simple-faster-rcnn-pytorch/akira_img.jpeg')
    img = preprocess(img)
    img = torch.from_numpy(img)[None]
    img = Variable(img.float())
    adv_img = attacker(img, epsilon=1)
    # Export ONNX model
    torch.onnx.export(attacker,
                      img,
                      "attacker.proto",
                      export_params=True,
                      verbose=True)
    # Load ONNX model
    model = onnx.load("attacker.proto")
    graph = model.graph
    # Check Formation
    onnx.checker.check_model(model)
Ejemplo n.º 24
0
def train(**kwargs):
    opt._parse(kwargs)

    dataset = Dataset(opt)
    print('load data')
    dataloader = data_.DataLoader(dataset,
                                  batch_size=1,
                                  shuffle=True,
                                  # pin_memory=True,
                                  num_workers=opt.num_workers)
    testset = TestDataset(opt)
    test_dataloader = data_.DataLoader(testset,
                                       batch_size=1,
                                       num_workers=opt.test_num_workers,
                                       shuffle=False,
                                       pin_memory=True
                                       )
    testset_all = TestDataset_all(opt, 'test2')
    test_all_dataloader = data_.DataLoader(testset_all,
                                           batch_size=1,
                                           num_workers=opt.test_num_workers,
                                           shuffle=False,
                                           pin_memory=True
                                           )

    tsf = Transform(opt.min_size, opt.max_size)
    faster_rcnn = FasterRCNNVGG16()
    trainer = FasterRCNNTrainer(faster_rcnn).cuda()
    print('model construct completed')

    # 加载训练过的模型,在config配置路径就可以了
    if opt.load_path:
        trainer.load(opt.load_path)
        print('load pretrained model from %s' % opt.load_path)

    #提取蒸馏知识所需要的软标签
    if opt.is_distillation == True:
        opt.predict_socre = 0.3
        for ii, (imgs, sizes, gt_bboxes_, gt_labels_, scale, id_) in tqdm(enumerate(dataloader)):
            if len(gt_bboxes_) == 0:
                continue
            sizes = [sizes[0][0].item(), sizes[1][0].item()]
            pred_bboxes_, pred_labels_, pred_scores_, features_ = trainer.faster_rcnn.predict(imgs, [
                sizes])

            img_file = os.path.join(
                opt.voc_data_dir, 'JPEGImages', id_[0] + '.jpg')
            ori_img = read_image(img_file, color=True)
            img, pred_bboxes_, pred_labels_, scale_ = tsf(
                (ori_img, pred_bboxes_[0], pred_labels_[0]))

            #去除软标签和真值标签重叠过多的部分,去除错误的软标签
            pred_bboxes_, pred_labels_, pred_scores_ = py_cpu_nms(
                gt_bboxes_[0], gt_labels_[0], pred_bboxes_, pred_labels_, pred_scores_[0])

            #存储软标签,这样存储不会使得GPU占用过多
            np.save('label/' + str(id_[0]) + '.npy', pred_labels_)
            np.save('bbox/' + str(id_[0]) + '.npy', pred_bboxes_)
            np.save('feature/' + str(id_[0]) + '.npy', features_)
            np.save('score/' + str(id_[0]) + '.npy', pred_scores_)

        opt.predict_socre = 0.05
    t.cuda.empty_cache()

    # visdom 显示所有类别标签名
    trainer.vis.text(dataset.db.label_names, win='labels')
    best_map = 0
    lr_ = opt.lr

    for epoch in range(opt.epoch):
        print('epoch=%d' % epoch)

        # 重置混淆矩阵
        trainer.reset_meters()
        # tqdm可以在长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator),
        # 是一个快速、扩展性强
        for ii, (img, sizes, bbox_, label_, scale, id_) in tqdm(enumerate(dataloader)):
            if len(bbox_) == 0:
                continue
            scale = at.scalar(scale)
            img, bbox, label = img.cuda().float(), bbox_.cuda(), label_.cuda()
            # 训练的就这一步 下面的都是打印的信息
            # 转化成pytorch能够计算的格式,转tensor格式
            if opt.is_distillation == True:
                #读取软标签
                teacher_pred_labels = np.load(
                    'label/' + str(id_[0]) + '.npy')
                teacher_pred_bboxes = np.load(
                    'bbox/' + str(id_[0]) + '.npy')
                teacher_pred_features_ = np.load(
                    'feature/' + str(id_[0]) + '.npy')
                teacher_pred_scores = np.load(
                    'score/' + str(id_[0]) + '.npy')
                #格式转换
                teacher_pred_bboxes = teacher_pred_bboxes.astype(np.float32)
                teacher_pred_labels = teacher_pred_labels.astype(np.int32)
                teacher_pred_scores = teacher_pred_scores.astype(np.float32)
                #转成pytorch格式
                teacher_pred_bboxes_ = at.totensor(teacher_pred_bboxes)
                teacher_pred_labels_ = at.totensor(teacher_pred_labels)
                teacher_pred_scores_ = at.totensor(teacher_pred_scores)
                teacher_pred_features_ = at.totensor(teacher_pred_features_)
                #使用GPU
                teacher_pred_bboxes_ = teacher_pred_bboxes_.cuda()
                teacher_pred_labels_ = teacher_pred_labels_.cuda()
                teacher_pred_scores_ = teacher_pred_scores_.cuda()
                teacher_pred_features_ = teacher_pred_features_.cuda()

                # 如果dataset.py 中的Transform 设置了图像翻转,就要使用这个判读软标签是否一起翻转
                if(teacher_pred_bboxes_[0][1] != bbox[0][0][1]):
                    _, o_C, o_H, o_W = img.shape
                    teacher_pred_bboxes_ = flip_bbox(
                        teacher_pred_bboxes_, (o_H, o_W), x_flip=True)

                losses = trainer.train_step(img, bbox, label, scale, epoch,
                                            teacher_pred_bboxes_, teacher_pred_labels_, teacher_pred_features_, teacher_pred_scores)
            else:
                trainer.train_step(img, bbox, label, scale, epoch)

            # visdom显示的信息
            if (ii + 1) % opt.plot_every == 0:
                if os.path.exists(opt.debug_file):
                    ipdb.set_trace()

                # plot loss
                trainer.vis.plot_many(trainer.get_meter_data())

                # plot groud truth bboxes
                ori_img_ = inverse_normalize(at.tonumpy(img[0]))
                gt_img = visdom_bbox(ori_img_,
                                     at.tonumpy(bbox_[0]),
                                     at.tonumpy(label_[0]))
                trainer.vis.img('gt_img', gt_img)

                gt_img = visdom_bbox(ori_img_,
                                     at.tonumpy(teacher_pred_bboxes_),
                                     at.tonumpy(teacher_pred_labels_),
                                     at.tonumpy(teacher_pred_scores_))
                trainer.vis.img('gt_img_all', gt_img)

                # plot predicti bboxes
                _bboxes, _labels, _scores, _ = trainer.faster_rcnn.predict(
                    [ori_img_], visualize=True)
                pred_img = visdom_bbox(ori_img_,
                                       at.tonumpy(_bboxes[0]),
                                       at.tonumpy(_labels[0]).reshape(-1),
                                       at.tonumpy(_scores[0]))
                trainer.vis.img('pred_img', pred_img)

                # 混淆矩阵
                # rpn confusion matrix(meter)
                trainer.vis.text(
                    str(trainer.rpn_cm.value().tolist()), win='rpn_cm')
                # roi confusion matrix
                trainer.vis.text(
                    str(trainer.roi_cm.value().tolist()), win='roi_cm')
                # trainer.vis.img('roi_cm', at.totensor(
                # trainer.roi_cm.value(), False).float())

        eval_result = eval(test_dataloader, faster_rcnn, test_num=opt.test_num)
        trainer.vis.plot('test_map', eval_result['map'])
        lr_ = trainer.faster_rcnn.optimizer.param_groups[0]['lr']
        log_info = 'lr:{},ap:{}, map:{},loss:{}'.format(str(lr_),
                                                        str(eval_result['ap']),
                                                        str(eval_result['map']),
                                                        str(trainer.get_meter_data()))
        trainer.vis.log(log_info)

        # 保存最好结果并记住路径
        if eval_result['map'] > best_map:
            best_map = eval_result['map']
            best_path = trainer.save(best_map=best_map)

        if epoch == 20:
            trainer.save(best_map='20')
            result = eval(test_all_dataloader,
                          trainer.faster_rcnn, test_num=5000)
            print('20result={}'.format(str(result)))
            # trainer.load(best_path)
            # result=eval(test_all_dataloader,trainer.faster_rcnn,test_num=5000)
            # print('bestmapresult={}'.format(str(result)))
            break

        # 每10轮加载前面最好权重,并且减少学习率
        if epoch % 20 == 15:
            trainer.load(best_path)
            trainer.faster_rcnn.scale_lr(opt.lr_decay)
            lr_ = lr_ * opt.lr_decay
Ejemplo n.º 25
0
if not os.path.exists("../outputs/"):
    os.mkdir("../outputs/")

trainer.load(
    '/home/sar/AnnotatedNetworkModelGit-master/fasterrcnn/simple-faster-rcnn/fasterrcnn_03241017'
)

opt.caffe_pretrain = False  # this model was trained from caffe-pretrained model

filenames = glob.glob('../test_data/*.jpg')

results_file = open("../submit/larger%s.csv" % str(0.5), "w")
iii = 1
# import matplotlib.pyplot as plt
for filename in filenames:
    img = read_image(filename)
    img = t.from_numpy(img)[None]
    _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,
                                                            visualize=True)
    # print(_bboxes)
    bboxes = _bboxes[0]
    labels = _labels[0]
    scores = _scores[0]

    image = cv2.imread(filename)
    # fig, ax = plt.subplots(1,2,figsize=(5,10))
    for i, box in (enumerate(bboxes)):
        cv2.rectangle(image, (int(box[1]), int(box[0])),
                      (int(box[3]), int(box[2])),
                      color=(0, 0, 255),
                      thickness=2)
Ejemplo n.º 26
0
 attacker = attacks.DCGAN(train_adv=False)
 attacker.load('/home/joey/Desktop/simple-faster-rcnn-pytorch/checkpoints/max_min_attack_6.pth')
 trainer = VictimFasterRCNNTrainer(faster_rcnn,attacker).cuda()
 trainer.load('/home/joey/Desktop/simple-faster-rcnn-pytorch/checkpoints/fasterrcnn_full_03172016_10')
 quality_list = [100,90,80,70,60,50,40,30,20]
 threshold = [0.7]
 adv_det_list = []
 for quality in threshold:
     detected = 0
     jpg_detected = 0
     adv_detected = 0
     trainer.faster_rcnn.score_thresh = quality
     for i in range(len(_data)):
         img,im_path = _data[i]
         ipdb.set_trace()
         img = read_image('/home/joey/Desktop/simple-faster-rcnn-pytorch/reporter.jpg')
         img = preprocess(img)
         img = torch.from_numpy(img)[None]
         im_path = '/home/joey/Desktop/simple-faster-rcnn-pytorch/reporter.jpg'
         img = Variable(img.float().cuda())
         im_path_clone = b = '%s' % im_path
         save_path = _data.save_dir + 'frcnn' + im_path.split('/')[-1]
         save_path_adv = _data.save_dir_adv + 'frcnn_adv_' + im_path_clone.split('/')[-1]
         save_path_combined = _data.save_dir_comb + 'frcnn_comb_' + im_path_clone.split('/')[-1]
         save_path_perturb = _data.save_dir_perturb + 'frcnn_perturb_' + im_path_clone.split('/')[-1]
         if not os.path.exists(_data.save_dir):
             os.makedirs(_data.save_dir)
         if not os.path.exists(_data.save_dir_adv):
             os.makedirs(_data.save_dir_adv)
         if not os.path.exists(_data.save_dir_comb):
             os.makedirs(_data.save_dir_comb)
Ejemplo n.º 27
0
import os
import torch as t
from utils.config import opt
from model import FasterRCNNVGG16
from trainer import FasterRCNNTrainer
from data.util import read_image
from utils.vis_tool import vis_bbox
from utils import array_tool as at

img = read_image('misc/demo.jpg')
img = t.from_numpy(img)[None]

faster_rcnn = FasterRCNNVGG16()
trainer = FasterRCNNTrainer(faster_rcnn).cuda()

trainer.load('./fasterrcnn_12222105_0.712649824453_caffe_pretrain.pth')
opt.caffe_pretrain = False  # this model was trained from torchvision-pretrained model
_bboxes, _labels, _scores = trainer.faster_rcnn.predict(img, visualize=True)
vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]),
         at.tonumpy(_labels[0]).reshape(-1),
         at.tonumpy(_scores[0]).reshape(-1))
Ejemplo n.º 28
0
               'Belt')
label_names = list(LABEL_NAMES) + ['bg']
path = 'DataSets/image_test/'
files = os.listdir(path)
faster_rcnn = FasterRCNNVGG16()
trainer = FasterRCNNTrainer(faster_rcnn).cuda()

checkpoint = torch.load('model.pkl')
trainer.faster_rcnn.load_state_dict(checkpoint['model_state'])
print(checkpoint['epoch'])
print(checkpoint['optimizer_state']['param_groups'])
opt.caffe_pretrain = False
for f in files:
    print(f)
    name_list = list()
    img = read_image(path + f)
    #img = preprocess(img)
    img = torch.from_numpy(img)[None]
    #print(img,img.shape)
    _bboxes, _labels, _scores = faster_rcnn.predict(img, visualize=True)
    #if len(_labels) != 0:
    #    for label in _labels:
    #        name_list.append(label_names[label])
    print(_bboxes)
    print(_labels)
    print(_scores)
    ax = vis_bbox(at.tonumpy(img[0]), at.tonumpy(_bboxes[0]),
                  at.tonumpy(_labels[0]).reshape(-1),
                  at.tonumpy(_scores[0]).reshape(-1))
"""
image_folder_path = 'DataSets/images/'
Ejemplo n.º 29
0
name, pred = [], []
id_list_file = '/home/csce/czwhhh/competition/rebar/VOC_GJ/ImageSets/Main/test.txt'

ids = [id_.strip() for id_ in open(id_list_file)]

faster_rcnn = FasterRCNNVGG16()
trainer = FasterRCNNTrainer(faster_rcnn).cuda()

trainer.load('./checkpoints/fasterrcnn_01151508_0')
opt.caffe_pretrain = True

for id in ids:
    print(id)
    img = read_image(
        '/home/csce/czwhhh/competition/rebar/VOC_GJ/JPEGImages/{0}.jpg'.format(
            id))
    img = t.from_numpy(img)[None]
    _bboxes, _labels, _scores = trainer.faster_rcnn.predict(img,
                                                            visualize=True)
    all_box = at.tonumpy(_bboxes[0])

    for i in range(all_box.shape[0]):
        bbox = []
        bbox.append(str(int(round(all_box[i][1]))))
        bbox.append(str(int(round(all_box[i][0]))))
        bbox.append(str(int(round(all_box[i][3]))))
        bbox.append(str(int(round(all_box[i][2]))))
        name.append('{0}.jpg'.format(id))
        pred.append(' '.join(bbox))
Ejemplo n.º 30
0
import os
import torch as t
import torch.nn as nn
from utils.config import opt
from model import FasterRCNNVGG16
from trainer import FasterRCNNTrainer
from data.util import read_image
from utils.vis_tool import vis_bbox
from utils import array_tool as at
from utils.average import AverageVal
import matplotlib.pyplot as plt

img = read_image("misc/demo.jpg")
img = t.from_numpy(img)[None]

logger = AverageVal()
faster_rcnn = FasterRCNNVGG16()

trainer = FasterRCNNTrainer(faster_rcnn, logger).cuda()

trainer.load("model/chainer_best_model_converted_to_pytorch_0.7053.pth")
opt.caffe_pretrain = True  # this model was trained from caffe-pretrained model

print(img.shape)

h = trainer.faster_rcnn.getFeatureMap(img)
print(h)