From b9ec25fef7d6191614e49b0dada5c18b6aad085e Mon Sep 17 00:00:00 2001 From: pprp <1115957667@qq.com> Date: Wed, 29 Jan 2020 18:14:59 +0800 Subject: [PATCH] updates --- SmallObjectAugmentation/Helpers.py | 38 ++++ SmallObjectAugmentation/README.md | 11 + SmallObjectAugmentation/aug.py | 157 +++++++++++++ .../background/img00000.txt | 3 + .../background/img00001.txt | 3 + SmallObjectAugmentation/break/img00000.txt | 3 + SmallObjectAugmentation/break/img00001.txt | 3 + SmallObjectAugmentation/createTrain.sh | 2 + SmallObjectAugmentation/demo.py | 29 +++ .../figs/2018-11-03_07_45_09_1_augment.txt | 7 + .../img/2018-11-03_07_45_09_1.txt | 5 + .../save/img00000_augment.txt | 13 ++ .../save/img00001_augment.txt | 13 ++ SmallObjectAugmentation/small.txt | 20 ++ SmallObjectAugmentation/train.txt | 2 + SmallObjectAugmentation/util.py | 213 ++++++++++++++++++ attention/CCNet/ccnet.py | 103 +++++++++ .../.gitattributes | 0 .../LICENSE | 0 .../README.MD | 0 .../data/__init__.py | 0 .../data/dataset.py | 0 .../data/util.py | 0 .../data/voc_dataset.py | 0 .../demo.ipynb | 0 .../misc/convert_caffe_pretrain.py | 0 .../misc/train_fast.py | 0 .../model/__init__.py | 0 .../model/faster_rcnn.py | 0 .../model/faster_rcnn_vgg16.py | 0 .../model/region_proposal_network.py | 0 .../model/roi_module.py | 0 .../model/utils/__init__.py | 0 .../model/utils/bbox_tools.py | 0 .../model/utils/creator_tool.py | 0 .../model/utils/nms/__init__.py | 0 .../model/utils/nms/_nms_gpu_post.pyx | 0 .../model/utils/nms/_nms_gpu_post_py.py | 0 .../model/utils/nms/build.py | 0 .../utils/nms/non_maximum_suppression.py | 0 .../model/utils/roi_cupy.py | 0 .../requirements.txt | 0 .../train.py | 0 .../trainer.py | 0 .../utils/__init__.py | 0 .../utils/array_tool.py | 0 .../utils/config.py | 0 .../utils/eval_tool.py | 0 .../utils/vis_tool.py | 0 49 files changed, 625 insertions(+) create mode 100644 SmallObjectAugmentation/Helpers.py create mode 100644 SmallObjectAugmentation/README.md create mode 100644 SmallObjectAugmentation/aug.py create mode 100644 SmallObjectAugmentation/background/img00000.txt create mode 100644 SmallObjectAugmentation/background/img00001.txt create mode 100644 SmallObjectAugmentation/break/img00000.txt create mode 100644 SmallObjectAugmentation/break/img00001.txt create mode 100644 SmallObjectAugmentation/createTrain.sh create mode 100644 SmallObjectAugmentation/demo.py create mode 100644 SmallObjectAugmentation/figs/2018-11-03_07_45_09_1_augment.txt create mode 100644 SmallObjectAugmentation/img/2018-11-03_07_45_09_1.txt create mode 100644 SmallObjectAugmentation/save/img00000_augment.txt create mode 100644 SmallObjectAugmentation/save/img00001_augment.txt create mode 100644 SmallObjectAugmentation/small.txt create mode 100644 SmallObjectAugmentation/train.txt create mode 100644 SmallObjectAugmentation/util.py create mode 100644 attention/CCNet/ccnet.py rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/.gitattributes (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/LICENSE (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/README.MD (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/data/__init__.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/data/dataset.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/data/util.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/data/voc_dataset.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/demo.ipynb (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/misc/convert_caffe_pretrain.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/misc/train_fast.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/__init__.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/faster_rcnn.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/faster_rcnn_vgg16.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/region_proposal_network.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/roi_module.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/__init__.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/bbox_tools.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/creator_tool.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/nms/__init__.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/nms/_nms_gpu_post.pyx (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/nms/_nms_gpu_post_py.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/nms/build.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/nms/non_maximum_suppression.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/model/utils/roi_cupy.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/requirements.txt (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/train.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/trainer.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/utils/__init__.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/utils/array_tool.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/utils/config.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/utils/eval_tool.py (100%) rename {simple-faster-rcnn-pytorch-master => simple-faster-rcnn-pytorch}/utils/vis_tool.py (100%) diff --git a/SmallObjectAugmentation/Helpers.py b/SmallObjectAugmentation/Helpers.py new file mode 100644 index 00000000..f7ffcf31 --- /dev/null +++ b/SmallObjectAugmentation/Helpers.py @@ -0,0 +1,38 @@ +import glob +import cv2 as cv2 +import numpy as np +import matplotlib.pyplot as plt +# import random +import math +from tqdm import tqdm + + +def load_images(path): + image_list = [] + images = glob.glob(path) + for index in range(len(images)): + image = cv2.cvtColor(cv2.imread(images[index]), cv2.COLOR_BGR2RGB) + image_list.append(image) + # image_list.append(cv2.resize(image,(1280,720))) + + return image_list + + +def read_images(path): + images = glob.glob(path) + return images + + +def load_images_from_path(path): + image_list = [] + for p in tqdm(path): + image = cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB) + image_list.append(image) + return image_list + + +def replace_labels(path): + labelpath = [] + for p in path: + labelpath.append(p.replace('.jpg', '.txt')) + return labelpath diff --git a/SmallObjectAugmentation/README.md b/SmallObjectAugmentation/README.md new file mode 100644 index 00000000..aa307a01 --- /dev/null +++ b/SmallObjectAugmentation/README.md @@ -0,0 +1,11 @@ +Simple python implementation of Augmentation for small object detection, as described in [Augmentation for small object detection +](https://arxiv.org/pdf/1902.07296.pdf). + +![img1](https://github.com/gmayday1997/SmallObjectAugmentation/blob/master/img/2018-11-03_07_45_09_1.jpg) +![img2](https://github.com/gmayday1997/SmallObjectAugmentation/blob/master/figs/2018-11-03_07_45_09_1_augment.jpg) + +## Demo +```shell +cd $ROOT +python demo.py +``` diff --git a/SmallObjectAugmentation/aug.py b/SmallObjectAugmentation/aug.py new file mode 100644 index 00000000..50e18b2f --- /dev/null +++ b/SmallObjectAugmentation/aug.py @@ -0,0 +1,157 @@ +# import glob +import cv2 as cv2 +import numpy as np +# from PIL import Image +import random +import math +from os.path import basename, split, join, dirname +from util import * + + +def find_str(filename): + if 'train' in filename: + return dirname(filename[filename.find('train'):]) + else: + return dirname(filename[filename.find('val'):]) + + +def convert_all_boxes(shape, anno_infos, yolo_label_txt_dir): + height, width, n = shape + label_file = open(yolo_label_txt_dir, 'w') + for anno_info in anno_infos: + target_id, x1, y1, x2, y2 = anno_info + b = (float(x1), float(x2), float(y1), float(y2)) + bb = convert((width, height), b) + label_file.write(str(target_id) + " " + " ".join([str(a) for a in bb]) + '\n') + + +def save_crop_image(save_crop_base_dir, image_dir, idx, roi): + crop_save_dir = join(save_crop_base_dir, find_str(image_dir)) + check_dir(crop_save_dir) + crop_img_save_dir = join(crop_save_dir, basename(image_dir)[:-3] + '_crop_' + str(idx) + '.jpg') + cv2.imwrite(crop_img_save_dir, roi) + + +def copysmallobjects(image_dir, label_dir, save_base_dir, save_crop_base_dir=None, + save_annoation_base_dir=None): + image = cv2.imread(image_dir) + + labels = read_label_txt(label_dir) + if len(labels) == 0: return + rescale_labels = rescale_yolo_labels(labels, image.shape) # 转换坐标表示 + all_boxes = [] + + for idx, rescale_label in enumerate(rescale_labels): + + all_boxes.append(rescale_label) + # 目标的长宽 + rescale_label_height, rescale_label_width = rescale_label[4] - rescale_label[2], rescale_label[3] - \ + rescale_label[1] + + if (issmallobject((rescale_label_height, rescale_label_width), thresh=64 * 64) and rescale_label[0] == '1'): + roi = image[rescale_label[2]:rescale_label[4], rescale_label[1]:rescale_label[3]] + + new_bboxes = random_add_patches(rescale_label, rescale_labels, image.shape, paste_number=2, iou_thresh=0.2) + count = 0 + + # 将新生成的位置加入到label,并在相应位置画出物体 + for new_bbox in new_bboxes: + count += 1 + all_boxes.append(new_bbox) + cl, bbox_left, bbox_top, bbox_right, bbox_bottom = new_bbox[0], new_bbox[1], new_bbox[2], new_bbox[3], \ + new_bbox[4] + try: + if (count > 1): + roi = flip_bbox(roi) + image[bbox_top:bbox_bottom, bbox_left:bbox_right] = roi + except ValueError: + continue + + dir_name = find_str(image_dir) + save_dir = join(save_base_dir, dir_name) + check_dir(save_dir) + yolo_txt_dir = join(save_dir, basename(image_dir.replace('.jpg', '_augment.txt'))) + cv2.imwrite(join(save_dir, basename(image_dir).replace('.jpg', '_augment.jpg')), image) + convert_all_boxes(image.shape, all_boxes, yolo_txt_dir) + + +def GaussianBlurImg(image): + # 高斯模糊 + ran = random.randint(0, 9) + if ran % 2 == 1: + image = cv2.GaussianBlur(image, ksize=(ran, ran), sigmaX=0, sigmaY=0) + else: + pass + return image + + +def suo_fang(image, area_max=2000, area_min=1000): + # 改变图片大小 + height, width, channels = image.shape + + while (height*width) > area_max: + image = cv2.resize(image, (int(width * 0.9),int(height * 0.9))) + height, width, channels = image.shape + height,width = int(height*0.9),int(width*0.9) + + while (height*width) < area_min: + image = cv2.resize(image, (int(width * 1.1),int(height * 1.1))) + height, width, channels = image.shape + height,width = int(height*1.1),int(width*1.1) + + return image + + +def copysmallobjects2(image_dir, label_dir, save_base_dir, small_img_dir): + image = cv2.imread(image_dir) + labels = read_label_txt(label_dir) + if len(labels) == 0: + return + rescale_labels = rescale_yolo_labels(labels, image.shape) # 转换坐标表示 + all_boxes = [] + for _, rescale_label in enumerate(rescale_labels): + all_boxes.append(rescale_label) + + for small_img_dirs in small_img_dir: + image_bbox = cv2.imread(small_img_dirs) + #roi = image_bbox + roi = suo_fang(image_bbox,area_max=3000,area_min=1500) + + new_bboxes = random_add_patches2(roi.shape, rescale_labels, image.shape, paste_number=1, iou_thresh=0) + count = 0 + for new_bbox in new_bboxes: + count += 1 + + cl, bbox_left, bbox_top, bbox_right, bbox_bottom = new_bbox[0], new_bbox[1], new_bbox[2], new_bbox[3], \ + new_bbox[4] + #roi = GaussianBlurImg(roi) # 高斯模糊 + height, width, channels = roi.shape + center = (int(width / 2),int(height / 2)) + #ran_point = (int((bbox_top+bbox_bottom)/2),int((bbox_left+bbox_right)/2)) + mask = 255 * np.ones(roi.shape, roi.dtype) + + try: + if count > 1: + roi = flip_bbox(roi) + #image[bbox_top:bbox_bottom, bbox_left:bbox_right] = roi + #image[bbox_top:bbox_bottom, bbox_left:bbox_right] = cv2.addWeighted(image[bbox_top:bbox_bottom, bbox_left:bbox_right], + # 0.5,roi,0.5,0) #图片融合 + + # 泊松融合 + #image = cv2.seamlessClone(roi, image, mask, ran_point, cv2.NORMAL_CLONE) + #print(str(bbox_bottom-bbox_top) + "|" + str(bbox_right-bbox_left)) + #print(roi.shape) + #print(mask.shape) + image[bbox_top:bbox_bottom, bbox_left:bbox_right] = cv2.seamlessClone(roi, image[bbox_top:bbox_bottom, bbox_left:bbox_right], + mask, center, cv2.NORMAL_CLONE) + all_boxes.append(new_bbox) + rescale_labels.append(new_bbox) + except ValueError: + print("---") + continue + dir_name = find_str(image_dir) + save_dir = join(save_base_dir, dir_name) + check_dir(save_dir) + yolo_txt_dir = join(save_dir, basename(image_dir.replace('.jpg', '_augment.txt'))) + cv2.imwrite(join(save_dir, basename(image_dir).replace('.jpg', '_augment.jpg')), image) + convert_all_boxes(image.shape, all_boxes, yolo_txt_dir) diff --git a/SmallObjectAugmentation/background/img00000.txt b/SmallObjectAugmentation/background/img00000.txt new file mode 100644 index 00000000..66f5ab29 --- /dev/null +++ b/SmallObjectAugmentation/background/img00000.txt @@ -0,0 +1,3 @@ +0 0.265364583333 0.139130434783 0.0151041666667 0.0369565217391 +0 0.284114583333 0.151086956522 0.0119791666667 0.0173913043478 +0 0.3515625 0.179347826087 0.015625 0.0239130434783 diff --git a/SmallObjectAugmentation/background/img00001.txt b/SmallObjectAugmentation/background/img00001.txt new file mode 100644 index 00000000..61129b4a --- /dev/null +++ b/SmallObjectAugmentation/background/img00001.txt @@ -0,0 +1,3 @@ +0 0.2671875 0.139130434783 0.021875 0.0434782608696 +0 0.284114583333 0.154891304348 0.0119791666667 0.025 +0 0.350520833333 0.179347826087 0.0177083333333 0.0239130434783 diff --git a/SmallObjectAugmentation/break/img00000.txt b/SmallObjectAugmentation/break/img00000.txt new file mode 100644 index 00000000..66f5ab29 --- /dev/null +++ b/SmallObjectAugmentation/break/img00000.txt @@ -0,0 +1,3 @@ +0 0.265364583333 0.139130434783 0.0151041666667 0.0369565217391 +0 0.284114583333 0.151086956522 0.0119791666667 0.0173913043478 +0 0.3515625 0.179347826087 0.015625 0.0239130434783 diff --git a/SmallObjectAugmentation/break/img00001.txt b/SmallObjectAugmentation/break/img00001.txt new file mode 100644 index 00000000..61129b4a --- /dev/null +++ b/SmallObjectAugmentation/break/img00001.txt @@ -0,0 +1,3 @@ +0 0.2671875 0.139130434783 0.021875 0.0434782608696 +0 0.284114583333 0.154891304348 0.0119791666667 0.025 +0 0.350520833333 0.179347826087 0.0177083333333 0.0239130434783 diff --git a/SmallObjectAugmentation/createTrain.sh b/SmallObjectAugmentation/createTrain.sh new file mode 100644 index 00000000..07ea0ac1 --- /dev/null +++ b/SmallObjectAugmentation/createTrain.sh @@ -0,0 +1,2 @@ +find ./background -maxdepth 4 -name "*.jpg" | sort > train.txt +find ./crops -maxdepth 4 -name "*.jpg" | sort > small.txt diff --git a/SmallObjectAugmentation/demo.py b/SmallObjectAugmentation/demo.py new file mode 100644 index 00000000..c1b91ce1 --- /dev/null +++ b/SmallObjectAugmentation/demo.py @@ -0,0 +1,29 @@ +import aug as am +import Helpers as hp +from util import * +import os +from os.path import join +from tqdm import tqdm +import random + +base_dir = os.getcwd() + +save_base_dir = join(base_dir, 'save') + +check_dir(save_base_dir) + +imgs_dir = [f.strip() for f in open(join(base_dir, 'train.txt')).readlines()] +labels_dir = hp.replace_labels(imgs_dir) + +small_imgs_dir = [f.strip() for f in open(join(base_dir, 'small.txt')).readlines()] +random.shuffle(small_imgs_dir) + +for image_dir, label_dir in tqdm(zip(imgs_dir, labels_dir)): + small_img = [] + for x in range(8): + if small_imgs_dir == []: + #exit() + small_imgs_dir = [f.strip() for f in open(join(base_dir,'small.txt')).readlines()] + random.shuffle(small_imgs_dir) + small_img.append(small_imgs_dir.pop()) + am.copysmallobjects2(image_dir, label_dir, save_base_dir,small_img) diff --git a/SmallObjectAugmentation/figs/2018-11-03_07_45_09_1_augment.txt b/SmallObjectAugmentation/figs/2018-11-03_07_45_09_1_augment.txt new file mode 100644 index 00000000..e713d7d8 --- /dev/null +++ b/SmallObjectAugmentation/figs/2018-11-03_07_45_09_1_augment.txt @@ -0,0 +1,7 @@ +1 0.6 0.5898148148148148 0.0125 0.061111111111111116 +1 0.346875 0.5916666666666667 0.0125 0.061111111111111116 +1 0.2708333333333333 0.5750000000000001 0.0125 0.061111111111111116 +0 0.3046875 0.44537037037037036 0.065625 0.1388888888888889 +0 0.10625 0.8481481481481482 0.19375 0.3 +0 0.6140625 0.40740740740740744 0.026041666666666668 0.037037037037037035 +0 0.6359375 0.375 0.036458333333333336 0.07222222222222223 diff --git a/SmallObjectAugmentation/img/2018-11-03_07_45_09_1.txt b/SmallObjectAugmentation/img/2018-11-03_07_45_09_1.txt new file mode 100644 index 00000000..c6b73d95 --- /dev/null +++ b/SmallObjectAugmentation/img/2018-11-03_07_45_09_1.txt @@ -0,0 +1,5 @@ +1 0.602083333333 0.591666666667 0.0125 0.0611111111111 +0 0.306770833333 0.449074074074 0.065625 0.138888888889 +0 0.108333333333 0.85 0.19375 0.3 +0 0.616145833333 0.411111111111 0.0260416666667 0.037037037037 +0 0.636979166667 0.376851851852 0.0364583333333 0.0722222222222 diff --git a/SmallObjectAugmentation/save/img00000_augment.txt b/SmallObjectAugmentation/save/img00000_augment.txt new file mode 100644 index 00000000..3a1a3e41 --- /dev/null +++ b/SmallObjectAugmentation/save/img00000_augment.txt @@ -0,0 +1,13 @@ +0 0.2643229166666667 0.13804347826086957 0.015104166666666667 0.03695652173913044 +0 0.2830729166666667 0.15 0.011979166666666666 0.017391304347826087 +0 0.35104166666666664 0.1782608695652174 0.015625 0.02391304347826087 +1 0.8265625 0.6293478260869565 0.011458333333333333 0.06739130434782609 +1 0.5752604166666666 0.7494565217391305 0.021354166666666667 0.07934782608695652 +1 0.6140625 0.8445652173913044 0.014583333333333334 0.09565217391304348 +1 0.72890625 0.6347826086956522 0.015104166666666667 0.09130434782608696 +1 0.43515624999999997 0.7663043478260869 0.022395833333333334 0.0782608695652174 +1 0.7671875 0.5396739130434782 0.017708333333333333 0.0923913043478261 +1 0.5122395833333333 0.9032608695652175 0.0203125 0.08695652173913043 +1 0.53046875 0.5679347826086957 0.018229166666666668 0.08586956521739131 +1 0.6596354166666667 0.7711956521739131 0.021354166666666667 0.07282608695652174 +1 0.490625 0.8190217391304347 0.01875 0.08804347826086957 diff --git a/SmallObjectAugmentation/save/img00001_augment.txt b/SmallObjectAugmentation/save/img00001_augment.txt new file mode 100644 index 00000000..92105a49 --- /dev/null +++ b/SmallObjectAugmentation/save/img00001_augment.txt @@ -0,0 +1,13 @@ +0 0.26666666666666666 0.13804347826086957 0.021875 0.043478260869565216 +0 0.2830729166666667 0.15380434782608696 0.011979166666666666 0.025 +0 0.3494791666666667 0.1782608695652174 0.017708333333333333 0.02391304347826087 +1 0.94296875 0.6923913043478261 0.013020833333333334 0.07608695652173914 +1 0.47031249999999997 0.8630434782608696 0.015625 0.07173913043478261 +1 0.62265625 0.5782608695652174 0.024479166666666666 0.08478260869565217 +1 0.29921875 0.5173913043478261 0.0203125 0.07173913043478261 +1 0.7361979166666667 0.5119565217391304 0.013020833333333334 0.06304347826086956 +1 0.3963541666666667 0.6402173913043478 0.016666666666666666 0.1 +1 0.4236979166666667 0.7717391304347826 0.0171875 0.08260869565217391 +1 0.9356770833333333 0.8913043478260869 0.0203125 0.09782608695652174 +1 0.8783854166666667 0.5608695652173913 0.015104166666666667 0.09130434782608696 +1 0.8260416666666667 0.5233695652173913 0.019791666666666666 0.08369565217391305 diff --git a/SmallObjectAugmentation/small.txt b/SmallObjectAugmentation/small.txt new file mode 100644 index 00000000..d716e5fd --- /dev/null +++ b/SmallObjectAugmentation/small.txt @@ -0,0 +1,20 @@ +./crops/0000003_crop_39.jpg +./crops/0000003_crop_60.jpg +./crops/0000003_crop_73.jpg +./crops/0000003_crop_74.jpg +./crops/0000003_crop_87.jpg +./crops/0000004_crop_28.jpg +./crops/0000004_crop_30.jpg +./crops/0000004_crop_5.jpg +./crops/0000004_crop_6.jpg +./crops/0000004_crop_7.jpg +./crops/0000004_crop_8.jpg +./crops/0000005_crop_5.jpg +./crops/0000014_crop_15.jpg +./crops/0000024_crop_5.jpg +./crops/0000188_crop_44.jpg +./crops/0000189_crop_76.jpg +./crops/0000189_crop_77.jpg +./crops/0000190_crop_28.jpg +./crops/0000239_crop_23.jpg +./crops/0000242_crop_15.jpg diff --git a/SmallObjectAugmentation/train.txt b/SmallObjectAugmentation/train.txt new file mode 100644 index 00000000..29426696 --- /dev/null +++ b/SmallObjectAugmentation/train.txt @@ -0,0 +1,2 @@ +./background/img00000.jpg +./background/img00001.jpg diff --git a/SmallObjectAugmentation/util.py b/SmallObjectAugmentation/util.py new file mode 100644 index 00000000..6512df59 --- /dev/null +++ b/SmallObjectAugmentation/util.py @@ -0,0 +1,213 @@ +import os +import cv2 +import numpy as np +from os.path import join, split +import random + + +def convert(size, box): + dw = 1. / (size[0]) + dh = 1. / (size[1]) + x = (box[0] + box[1]) / 2.0 - 1 + y = (box[2] + box[3]) / 2.0 - 1 + w = box[1] - box[0] + h = box[3] - box[2] + x = x * dw + w = w * dw + y = y * dh + h = h * dh + return (x, y, w, h) + + +def issmallobject(bbox, thresh): + if bbox[0] * bbox[1] <= thresh: + return True + else: + return False + + +def read_label_txt(label_dir): + labels = [] + with open(label_dir) as fp: + for f in fp.readlines(): + labels.append(f.strip().split(' ')) + return labels + + +def load_txt_label(label_dir): + return np.loadtxt(label_dir, dtype=str) + + +def load_txt_labels(label_dir): + labels = [] + for l in label_dir: + la = load_txt_label(l) + labels.append(la) + return labels + + +def check_dir(dir): + if not os.path.exists(dir): + os.makedirs(dir) + + +def rescale_yolo_labels(labels, img_shape): + height, width, nchannel = img_shape + rescale_boxes = [] + for box in list(labels): + x_c = float(box[1]) * width + y_c = float(box[2]) * height + w = float(box[3]) * width + h = float(box[4]) * height + x_left = x_c - w * .5 + y_left = y_c - h * .5 + x_right = x_c + w * .5 + y_right = y_c + h * .5 + rescale_boxes.append([box[0], int(x_left), int(y_left), int(x_right), int(y_right)]) + return rescale_boxes + + +def draw_annotation_to_image(img, annotation, save_img_dir): + for anno in annotation: + cl, x1, y1, x2, y2 = anno + cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), color=(255, 0, 0)) + font = cv2.FONT_HERSHEY_SIMPLEX + cv2.putText(img, cl, (int((x1 + x2) / 2), y1 - 5), font, fontScale=0.8, color=(0, 0, 255)) + cv2.imwrite(save_img_dir, img) + + +def bbox_iou(box1, box2): + cl, b1_x1, b1_y1, b1_x2, b1_y2 = box1 + cl, b2_x1, b2_y1, b2_x2, b2_y2 = box2 + # get the corrdinates of the intersection rectangle + inter_rect_x1 = max(b1_x1, b2_x1) + inter_rect_y1 = max(b1_y1, b2_y1) + inter_rect_x2 = min(b1_x2, b2_x2) + inter_rect_y2 = min(b1_y2, b2_y2) + # Intersection area + inter_width = inter_rect_x2 - inter_rect_x1 + 1 + inter_height = inter_rect_y2 - inter_rect_y1 + 1 + if inter_width > 0 and inter_height > 0: # strong condition + inter_area = inter_width * inter_height + # Union Area + b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1) + b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1) + iou = inter_area / (b1_area + b2_area - inter_area) + else: + iou = 0 + return iou + + +def swap(x1, x2): + if (x1 > x2): + temp = x1 + x1 = x2 + x2 = temp + return x1, x2 + + +def norm_sampling(search_space): + # 随机生成点 + search_x_left, search_y_left, search_x_right, search_y_right = search_space + new_bbox_x_center = random.randint(search_x_left, search_x_right) + new_bbox_y_center = random.randint(search_y_left, search_y_right) + return [new_bbox_x_center, new_bbox_y_center] + + +def flip_bbox(roi): + roi = roi[:, ::-1, :] + return roi + + +def sampling_new_bbox_center_point(img_shape, bbox): + #### sampling space #### + height, width, nc = img_shape + cl, x_left, y_left, x_right, y_right = bbox + bbox_w, bbox_h = x_right - x_left, y_right - y_left + ### left top ### + if x_left <= width / 2: + search_x_left, search_y_left, search_x_right, search_y_right = width * 0.6, height / 2, width * 0.75, height * 0.75 + if x_left > width / 2: + search_x_left, search_y_left, search_x_right, search_y_right = width * 0.25, height / 2, width * 0.5, height * 0.75 + return [search_x_left, search_y_left, search_x_right, search_y_right] + + +def random_add_patches(bbox, rescale_boxes, shape, paste_number, iou_thresh): + temp = [] + for rescale_bbox in rescale_boxes: + temp.append(rescale_bbox) + cl, x_left, y_left, x_right, y_right = bbox + bbox_w, bbox_h = x_right - x_left, y_right - y_left + center_search_space = sampling_new_bbox_center_point(shape, bbox) + success_num = 0 + new_bboxes = [] + while success_num < paste_number: + new_bbox_x_center, new_bbox_y_center = norm_sampling(center_search_space) + print(norm_sampling(center_search_space)) + new_bbox_x_left, new_bbox_y_left, new_bbox_x_right, new_bbox_y_right = new_bbox_x_center - 0.5 * bbox_w, \ + new_bbox_y_center - 0.5 * bbox_h, \ + new_bbox_x_center + 0.5 * bbox_w, \ + new_bbox_y_center + 0.5 * bbox_h + new_bbox = [cl, int(new_bbox_x_left), int(new_bbox_y_left), int(new_bbox_x_right), int(new_bbox_y_right)] + ious = [bbox_iou(new_bbox, bbox_t) for bbox_t in rescale_boxes] + if max(ious) <= iou_thresh: + # for bbox_t in rescale_boxes: + # iou = bbox_iou(new_bbox[1:],bbox_t[1:]) + # if(iou <= iou_thresh): + success_num += 1 + temp.append(new_bbox) + new_bboxes.append(new_bbox) + else: + continue + return new_bboxes + + +def sampling_new_bbox_center_point2(img_shape, bbox): + #### sampling space #### + height, width, nc = img_shape + bbox_h, bbox_w, bbox_c = bbox + ### left top ### + ''' + search_x_left, search_y_left, search_x_right, search_y_right = width * 0.55 , height * 0.5 , \ + width * 0.9 , height * 0.95 + ''' + search_x_left, search_y_left, search_x_right, search_y_right = width * 0.35 , height * 0.6 , \ + width * 1 , height * 0.95 + + return [search_x_left, search_y_left, search_x_right, search_y_right] + + +def random_add_patches2(bbox_img, rescale_boxes, shape, paste_number, iou_thresh): + temp = [] + for rescale_bbox in rescale_boxes: + temp.append(rescale_bbox) + bbox_h, bbox_w, bbox_c = bbox_img + img_h,img_w,img_c = shape + center_search_space = sampling_new_bbox_center_point2(shape, bbox_img) # 选取生成随机点区域 + success_num = 0 + new_bboxes = [] + cl = 1 + while success_num < paste_number: + new_bbox_x_center, new_bbox_y_center = norm_sampling(center_search_space) # 随机生成点坐标 + if new_bbox_x_center-0.5*bbox_w < 0 or new_bbox_x_center+0.5*bbox_w > img_w: + continue + if new_bbox_y_center-0.5*bbox_h < 0 or new_bbox_y_center+0.5*bbox_h > img_h: + continue + new_bbox_x_left, new_bbox_y_left, new_bbox_x_right, new_bbox_y_right = new_bbox_x_center - 0.5 * bbox_w, \ + new_bbox_y_center - 0.5 * bbox_h, \ + new_bbox_x_center + 0.5 * bbox_w, \ + new_bbox_y_center + 0.5 * bbox_h + new_bbox = [cl, int(new_bbox_x_left), int(new_bbox_y_left), int(new_bbox_x_right), int(new_bbox_y_right)] + + ious = [bbox_iou(new_bbox, bbox_t) for bbox_t in rescale_boxes] + ious2 = [bbox_iou(new_bbox,bbox_t1) for bbox_t1 in new_bboxes] + if ious2 == []: + ious2.append(0) + if max(ious) <= iou_thresh and max(ious2) <= iou_thresh: + success_num += 1 + temp.append(new_bbox) + new_bboxes.append(new_bbox) + else: + continue + + return new_bboxes diff --git a/attention/CCNet/ccnet.py b/attention/CCNet/ccnet.py new file mode 100644 index 00000000..410c3777 --- /dev/null +++ b/attention/CCNet/ccnet.py @@ -0,0 +1,103 @@ +import functools +import time + +import torch +import torch.autograd as autograd +import torch.cuda.comm as comm +import torch.nn as nn +import torch.nn.functional as F +from torch.autograd.function import once_differentiable + + +class CA_Weight(autograd.Function): + @staticmethod + def forward(ctx, t, f): + # Save context + n, c, h, w = t.size() + size = (n, h + w - 1, h, w) + weight = torch.zeros(size, + dtype=t.dtype, + layout=t.layout, + device=t.device) + + _ext.ca_forward_cuda(t, f, weight) + + # Output + ctx.save_for_backward(t, f) + + return weight + + @staticmethod + @once_differentiable + def backward(ctx, dw): + t, f = ctx.saved_tensors + + dt = torch.zeros_like(t) + df = torch.zeros_like(f) + + _ext.ca_backward_cuda(dw.contiguous(), t, f, dt, df) + + _check_contiguous(dt, df) + + return dt, df + + +class CA_Map(autograd.Function): + @staticmethod + def forward(ctx, weight, g): + # Save context + out = torch.zeros_like(g) + _ext.ca_map_forward_cuda(weight, g, out) + + # Output + ctx.save_for_backward(weight, g) + + return out + + @staticmethod + @once_differentiable + def backward(ctx, dout): + weight, g = ctx.saved_tensors + + dw = torch.zeros_like(weight) + dg = torch.zeros_like(g) + + _ext.ca_map_backward_cuda(dout.contiguous(), weight, g, dw, dg) + + _check_contiguous(dw, dg) + + return dw, dg + + +ca_weight = CA_Weight.apply +ca_map = CA_Map.apply + + +class CrissCrossAttention(nn.Module): + """ Criss-Cross Attention Module""" + def __init__(self, in_dim): + super(CrissCrossAttention, self).__init__() + self.chanel_in = in_dim + + self.query_conv = nn.Conv2d(in_channels=in_dim, + out_channels=in_dim // 8, + kernel_size=1) + self.key_conv = nn.Conv2d(in_channels=in_dim, + out_channels=in_dim // 8, + kernel_size=1) + self.value_conv = nn.Conv2d(in_channels=in_dim, + out_channels=in_dim, + kernel_size=1) + self.gamma = nn.Parameter(torch.zeros(1)) + + def forward(self, x): + proj_query = self.query_conv(x) + proj_key = self.key_conv(x) + proj_value = self.value_conv(x) + + energy = ca_weight(proj_query, proj_key) + attention = F.softmax(energy, 1) + out = ca_map(attention, proj_value) + out = self.gamma * out + x + + return out diff --git a/simple-faster-rcnn-pytorch-master/.gitattributes b/simple-faster-rcnn-pytorch/.gitattributes similarity index 100% rename from simple-faster-rcnn-pytorch-master/.gitattributes rename to simple-faster-rcnn-pytorch/.gitattributes diff --git a/simple-faster-rcnn-pytorch-master/LICENSE b/simple-faster-rcnn-pytorch/LICENSE similarity index 100% rename from simple-faster-rcnn-pytorch-master/LICENSE rename to simple-faster-rcnn-pytorch/LICENSE diff --git a/simple-faster-rcnn-pytorch-master/README.MD b/simple-faster-rcnn-pytorch/README.MD similarity index 100% rename from simple-faster-rcnn-pytorch-master/README.MD rename to simple-faster-rcnn-pytorch/README.MD diff --git a/simple-faster-rcnn-pytorch-master/data/__init__.py b/simple-faster-rcnn-pytorch/data/__init__.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/data/__init__.py rename to simple-faster-rcnn-pytorch/data/__init__.py diff --git a/simple-faster-rcnn-pytorch-master/data/dataset.py b/simple-faster-rcnn-pytorch/data/dataset.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/data/dataset.py rename to simple-faster-rcnn-pytorch/data/dataset.py diff --git a/simple-faster-rcnn-pytorch-master/data/util.py b/simple-faster-rcnn-pytorch/data/util.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/data/util.py rename to simple-faster-rcnn-pytorch/data/util.py diff --git a/simple-faster-rcnn-pytorch-master/data/voc_dataset.py b/simple-faster-rcnn-pytorch/data/voc_dataset.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/data/voc_dataset.py rename to simple-faster-rcnn-pytorch/data/voc_dataset.py diff --git a/simple-faster-rcnn-pytorch-master/demo.ipynb b/simple-faster-rcnn-pytorch/demo.ipynb similarity index 100% rename from simple-faster-rcnn-pytorch-master/demo.ipynb rename to simple-faster-rcnn-pytorch/demo.ipynb diff --git a/simple-faster-rcnn-pytorch-master/misc/convert_caffe_pretrain.py b/simple-faster-rcnn-pytorch/misc/convert_caffe_pretrain.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/misc/convert_caffe_pretrain.py rename to simple-faster-rcnn-pytorch/misc/convert_caffe_pretrain.py diff --git a/simple-faster-rcnn-pytorch-master/misc/train_fast.py b/simple-faster-rcnn-pytorch/misc/train_fast.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/misc/train_fast.py rename to simple-faster-rcnn-pytorch/misc/train_fast.py diff --git a/simple-faster-rcnn-pytorch-master/model/__init__.py b/simple-faster-rcnn-pytorch/model/__init__.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/__init__.py rename to simple-faster-rcnn-pytorch/model/__init__.py diff --git a/simple-faster-rcnn-pytorch-master/model/faster_rcnn.py b/simple-faster-rcnn-pytorch/model/faster_rcnn.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/faster_rcnn.py rename to simple-faster-rcnn-pytorch/model/faster_rcnn.py diff --git a/simple-faster-rcnn-pytorch-master/model/faster_rcnn_vgg16.py b/simple-faster-rcnn-pytorch/model/faster_rcnn_vgg16.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/faster_rcnn_vgg16.py rename to simple-faster-rcnn-pytorch/model/faster_rcnn_vgg16.py diff --git a/simple-faster-rcnn-pytorch-master/model/region_proposal_network.py b/simple-faster-rcnn-pytorch/model/region_proposal_network.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/region_proposal_network.py rename to simple-faster-rcnn-pytorch/model/region_proposal_network.py diff --git a/simple-faster-rcnn-pytorch-master/model/roi_module.py b/simple-faster-rcnn-pytorch/model/roi_module.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/roi_module.py rename to simple-faster-rcnn-pytorch/model/roi_module.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/__init__.py b/simple-faster-rcnn-pytorch/model/utils/__init__.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/__init__.py rename to simple-faster-rcnn-pytorch/model/utils/__init__.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/bbox_tools.py b/simple-faster-rcnn-pytorch/model/utils/bbox_tools.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/bbox_tools.py rename to simple-faster-rcnn-pytorch/model/utils/bbox_tools.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/creator_tool.py b/simple-faster-rcnn-pytorch/model/utils/creator_tool.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/creator_tool.py rename to simple-faster-rcnn-pytorch/model/utils/creator_tool.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/nms/__init__.py b/simple-faster-rcnn-pytorch/model/utils/nms/__init__.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/nms/__init__.py rename to simple-faster-rcnn-pytorch/model/utils/nms/__init__.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/nms/_nms_gpu_post.pyx b/simple-faster-rcnn-pytorch/model/utils/nms/_nms_gpu_post.pyx similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/nms/_nms_gpu_post.pyx rename to simple-faster-rcnn-pytorch/model/utils/nms/_nms_gpu_post.pyx diff --git a/simple-faster-rcnn-pytorch-master/model/utils/nms/_nms_gpu_post_py.py b/simple-faster-rcnn-pytorch/model/utils/nms/_nms_gpu_post_py.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/nms/_nms_gpu_post_py.py rename to simple-faster-rcnn-pytorch/model/utils/nms/_nms_gpu_post_py.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/nms/build.py b/simple-faster-rcnn-pytorch/model/utils/nms/build.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/nms/build.py rename to simple-faster-rcnn-pytorch/model/utils/nms/build.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/nms/non_maximum_suppression.py b/simple-faster-rcnn-pytorch/model/utils/nms/non_maximum_suppression.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/nms/non_maximum_suppression.py rename to simple-faster-rcnn-pytorch/model/utils/nms/non_maximum_suppression.py diff --git a/simple-faster-rcnn-pytorch-master/model/utils/roi_cupy.py b/simple-faster-rcnn-pytorch/model/utils/roi_cupy.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/model/utils/roi_cupy.py rename to simple-faster-rcnn-pytorch/model/utils/roi_cupy.py diff --git a/simple-faster-rcnn-pytorch-master/requirements.txt b/simple-faster-rcnn-pytorch/requirements.txt similarity index 100% rename from simple-faster-rcnn-pytorch-master/requirements.txt rename to simple-faster-rcnn-pytorch/requirements.txt diff --git a/simple-faster-rcnn-pytorch-master/train.py b/simple-faster-rcnn-pytorch/train.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/train.py rename to simple-faster-rcnn-pytorch/train.py diff --git a/simple-faster-rcnn-pytorch-master/trainer.py b/simple-faster-rcnn-pytorch/trainer.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/trainer.py rename to simple-faster-rcnn-pytorch/trainer.py diff --git a/simple-faster-rcnn-pytorch-master/utils/__init__.py b/simple-faster-rcnn-pytorch/utils/__init__.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/utils/__init__.py rename to simple-faster-rcnn-pytorch/utils/__init__.py diff --git a/simple-faster-rcnn-pytorch-master/utils/array_tool.py b/simple-faster-rcnn-pytorch/utils/array_tool.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/utils/array_tool.py rename to simple-faster-rcnn-pytorch/utils/array_tool.py diff --git a/simple-faster-rcnn-pytorch-master/utils/config.py b/simple-faster-rcnn-pytorch/utils/config.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/utils/config.py rename to simple-faster-rcnn-pytorch/utils/config.py diff --git a/simple-faster-rcnn-pytorch-master/utils/eval_tool.py b/simple-faster-rcnn-pytorch/utils/eval_tool.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/utils/eval_tool.py rename to simple-faster-rcnn-pytorch/utils/eval_tool.py diff --git a/simple-faster-rcnn-pytorch-master/utils/vis_tool.py b/simple-faster-rcnn-pytorch/utils/vis_tool.py similarity index 100% rename from simple-faster-rcnn-pytorch-master/utils/vis_tool.py rename to simple-faster-rcnn-pytorch/utils/vis_tool.py