├── .gitmodules ├── __init__.py ├── source ├── __init__.py ├── bg │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ └── bgutils.cpython-36.pyc │ └── bgutils.py ├── test │ ├── __init__.py │ ├── demo.py │ └── test.py ├── tools │ ├── __init__.py │ ├── convert_img.py │ ├── marklane.py │ └── cropvideo.py ├── tracking │ ├── __init__.py │ └── lkflow.py └── __pycache__ │ └── __init__.cpython-36.pyc ├── mainmodels ├── models │ ├── autoencoder │ │ ├── readme.md │ │ ├── __init__.py │ │ ├── utils.py │ │ ├── model.py │ │ ├── data_input.py │ │ └── trainer.py │ ├── tradition │ │ ├── control │ │ │ ├── readme.md │ │ │ ├── __init__.py │ │ │ └── model_cnn_server.py │ │ ├── cnn │ │ │ ├── __init__.py │ │ │ ├── __init.py │ │ │ ├── trainer.py │ │ │ └── model_eval.py │ │ ├── __init__.py │ │ ├── tools │ │ │ └── __init__.py │ │ ├── multicnn │ │ │ ├── __init__.py │ │ │ ├── trainer.py │ │ │ ├── layers.py │ │ │ └── model_eval.py │ │ └── config.py │ ├── ssd │ │ ├── train.run.sh │ │ ├── __init__.py │ │ ├── tools │ │ │ ├── __init__.py │ │ │ ├── __init.py │ │ │ ├── NMS.py │ │ │ └── NMS_vis.py │ │ ├── visualization │ │ │ ├── __init__.py │ │ │ └── vis_model.py │ │ ├── featuremodel │ │ │ ├── __init__.py │ │ │ ├── alexnet.py │ │ │ ├── resalexnet.py │ │ │ └── nwpunet.py │ │ ├── __init.py │ │ ├── settings.py │ │ └── inference.py │ ├── __init__.py │ ├── resnet │ │ ├── __init__.py │ │ ├── settings.py │ │ ├── trainer.py │ │ └── evaluate.py │ └── rpnplus │ │ ├── __init__.py │ │ ├── tools │ │ ├── __init__.py │ │ ├── showlabel.py │ │ └── image_pylib.py │ │ ├── config.py │ │ ├── readme.md │ │ └── NMS.py ├── dataset │ ├── LSD10 │ │ ├── readme.txt │ │ ├── __init__.py │ │ ├── lsd_config.py │ │ ├── tmp.py │ │ ├── labelmap_lsd10.prototxt │ │ ├── 产生标准文件.py │ │ ├── map.txt │ │ ├── 显示结果图.py │ │ ├── lsd_ssd_input.py │ │ ├── tools.py │ │ └── evaluate_lsd10.py │ ├── __init__.py │ ├── JLVideo │ │ ├── __init__.py │ │ ├── 吉林一号测试集评价.py │ │ ├── 显示结果图.py │ │ ├── 图像切割成标准输入.py │ │ └── 吉林一号视频信息.py │ ├── TT100K │ │ ├── __init__.py │ │ ├── tt100k_sign.txt │ │ └── rdssd_mAP.txt │ ├── VEDAI │ │ ├── __init__.py │ │ ├── vedia_config.py │ │ ├── 数据可视化.py │ │ ├── vedia_ssd_input.py │ │ └── tools.py │ ├── NWPUVHR10 │ │ ├── __init__.py │ │ ├── nwpu_config.py │ │ └── tools.py │ ├── __init.py │ ├── usa_traffic_sign_input.py │ ├── show_target.py │ ├── VOC格式转换.py │ ├── ssd.score.log │ ├── cifar_input.py │ └── airplane_yolo_input.py ├── __init__.py └── scripts │ ├── create_data.sh │ ├── create_list.sh │ └── 组织模型数据.py ├── attached ├── SAEModel.png ├── airplane.png ├── drawLine.png └── CIFAR10train.png ├── .gitignore └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /source/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /source/bg/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /source/test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /source/tools/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /source/tracking/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/readme.md: -------------------------------------------------------------------------------- 1 | ## 主要是进行相关的测试部分 2 | -------------------------------------------------------------------------------- /source/test/demo.py: -------------------------------------------------------------------------------- 1 | for i in range(10, 0, -1): 2 | print(i) 3 | -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/readme.txt: -------------------------------------------------------------------------------- 1 | - 将NWPUVHR10和VEDIA的数据进行整合,做尺度差异较大的数据集 2 | -------------------------------------------------------------------------------- /mainmodels/models/tradition/control/readme.md: -------------------------------------------------------------------------------- 1 | ### 该模块用来对图像的结果进行处理 2 | 就是核心的控制模块 -------------------------------------------------------------------------------- /attached/SAEModel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/attached/SAEModel.png -------------------------------------------------------------------------------- /attached/airplane.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/attached/airplane.png -------------------------------------------------------------------------------- /attached/drawLine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/attached/drawLine.png -------------------------------------------------------------------------------- /mainmodels/models/ssd/train.run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | python trainer.py > nohup.log 2>& 1 & -------------------------------------------------------------------------------- /attached/CIFAR10train.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/attached/CIFAR10train.png -------------------------------------------------------------------------------- /source/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/source/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /source/bg/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/source/bg/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /source/bg/__pycache__/bgutils.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiyang882/TinyObject/HEAD/source/bg/__pycache__/bgutils.cpython-36.pyc -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/tradition/cnn/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/ssd/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/JLVideo/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/11 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/TT100K/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/6 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/VEDAI/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/resnet/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/26 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/ssd/tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/tradition/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/1 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/dataset/NWPUVHR10/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/4 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/tradition/tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/1 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/ssd/visualization/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/9/1 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/tradition/control/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/3 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/tradition/multicnn/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/5 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function -------------------------------------------------------------------------------- /mainmodels/models/ssd/featuremodel/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | """ 12 | 该package主要是进行多种模型的尝试, 13 | 利用不同的模型提取出feature然后进行RPN和ROI的操作 14 | """ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | output/ 3 | *.gz 4 | *.zip 5 | *.rar 6 | *.tar.gz 7 | *.tar 8 | *.ckpt* 9 | *.pkl 10 | .DS_Store 11 | *.[ao~] 12 | *.jpg 13 | *.png 14 | *.npy 15 | *.pyc 16 | /data/ 17 | /pretrain/ 18 | /mainmodels/log/ 19 | /attached/airplane.png 20 | /attached/drawLine.png 21 | /attached/CIFAR10train.png 22 | /attached/SAEModel.png 23 | !/attached/SAEModel.png 24 | !/attached/CIFAR10train.png 25 | !/attached/airplane.png 26 | !/attached/drawLine.png 27 | trainmodel/ 28 | mainmodels/models/ssd/visualization/tensorboard_out/ 29 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/__init.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/23 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import sys 13 | 14 | proj_dir = "/".join(os.path.abspath(__file__).split("/")[:-4]) 15 | print(proj_dir) 16 | mainmodels_path = "/".join([proj_dir, "mainmodels"]) 17 | sys.path.append(proj_dir) 18 | sys.path.append(mainmodels_path) -------------------------------------------------------------------------------- /mainmodels/dataset/__init.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/18 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import sys 13 | 14 | proj_root = "/".join(os.path.abspath(__file__).split("/")[0:-3]) 15 | main_models = "/".join([proj_root, "mainmodels"]) 16 | models = "/".join([proj_root, "models"]) 17 | print(proj_root) 18 | 19 | sys.path.extend([proj_root, main_models, models]) 20 | print(sys.path) -------------------------------------------------------------------------------- /mainmodels/models/ssd/tools/__init.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/18 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import sys 13 | 14 | proj_root = "/".join(os.path.abspath(__file__).split("/")[0:-5]) 15 | main_models = "/".join([proj_root, "mainmodels"]) 16 | models = "/".join([proj_root, "models"]) 17 | print(proj_root) 18 | 19 | sys.path.extend([proj_root, main_models, models]) 20 | print(sys.path) -------------------------------------------------------------------------------- /mainmodels/models/tradition/cnn/__init.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/18 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import sys 13 | 14 | proj_root = "/".join(os.path.abspath(__file__).split("/")[0:-5]) 15 | main_models = "/".join([proj_root, "mainmodels"]) 16 | models = "/".join([proj_root, "models"]) 17 | print(proj_root) 18 | 19 | sys.path.extend([proj_root, main_models, models]) 20 | print(sys.path) -------------------------------------------------------------------------------- /mainmodels/dataset/usa_traffic_sign_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | """美国交通数据的组织""" 12 | 13 | """ 14 | 1. python extractAnnotations.py copy allAnnotations.csv 15 | 2. python mergeAnnotationFiles.py frame mergedAnnotations.csv 16 | 3. python create_pickle.py 17 | 18 | train_data in the path: 19 | /Volumes/projects/TrafficSign/SSD/signDatabasePublicFramesOnly 20 | /data_raw_400x260.p 21 | """ -------------------------------------------------------------------------------- /mainmodels/models/ssd/visualization/vis_model.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/9/1 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | 13 | from mainmodels.models.ssd.ssdmodel import SSDModel 14 | 15 | 16 | with tf.Graph().as_default(), tf.Session() as sess: 17 | # This includes the entire graph, e.g. loss function, optimizer, etc. 18 | _ = SSDModel() 19 | 20 | tf.summary.merge_all() 21 | writer = tf.summary.FileWriter('./tensorboard_out', sess.graph) 22 | tf.global_variables_initializer().run() -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function 9 | 10 | from enum import Enum, unique 11 | 12 | @unique 13 | class InputType(Enum): 14 | """Enum to specify the data type requested""" 15 | validation = 'validation' 16 | train = 'train' 17 | test = 'test' 18 | 19 | def __str__(self): 20 | """Return the string representation of the enum""" 21 | return self.value 22 | 23 | @staticmethod 24 | def check(input_type): 25 | """Check if input_type is an element of this Enum""" 26 | if not isinstance(input_type, InputType): 27 | raise ValueError("Invalid input_type, required a valid InputType") -------------------------------------------------------------------------------- /source/test/test.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/7/21 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import sys 12 | 13 | import math 14 | 15 | K = input() 16 | need = [] 17 | for i in range(K): 18 | t = input() 19 | need = map(int, t.strip().split(",")) 20 | matrix = [] 21 | for i in range(K): 22 | tmp = input() 23 | matrix.append(map(int, tmp.strip().split(","))) 24 | 25 | res = 0 26 | lastX, lastY = K-1, -1 27 | for i in range(K): 28 | for j in range(len(matrix[i])): 29 | if sum(need) > 0 and matrix[i][j] and need[i]: 30 | res += (math.fabs(i-lastX) + math.fabs(j-lastY)) 31 | need[i] -= 1 32 | lastX, lastY = i, j 33 | print(res) -------------------------------------------------------------------------------- /mainmodels/scripts/create_data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cur_dir=$(cd $( dirname ${BASH_SOURCE[0]} ) && pwd ) 4 | root_dir=$cur_dir/../.. 5 | 6 | cd $root_dir 7 | 8 | redo=1 9 | data_root_dir="/root/caffe/data/CSUVOCDATA/" 10 | dataset_name="CSUVIDEO" 11 | mapfile="$root_dir/data/$dataset_name/labelmap_csuvideo.prototxt" 12 | anno_type="detection" 13 | db="lmdb" 14 | min_dim=0 15 | max_dim=0 16 | width=0 17 | height=0 18 | 19 | extra_cmd="--encode-type=jpg --encoded" 20 | if [ $redo ] 21 | then 22 | extra_cmd="$extra_cmd --redo" 23 | fi 24 | for subset in test trainval 25 | do 26 | python $root_dir/scripts/create_annoset.py --anno-type=$anno_type --label-map-file=$mapfile --min-dim=$min_dim --max-dim=$max_dim --resize-width=$width --resize-height=$height --check-label $extra_cmd $data_root_dir $root_dir/data/$dataset_name/$subset.txt $data_root_dir/$dataset_name/$db/$dataset_name"_"$subset"_"$db examples/$dataset_name 27 | done -------------------------------------------------------------------------------- /mainmodels/dataset/VEDAI/vedia_config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | # 1-airplane, 2-ship, 3-storage tank, 4-baseball diamond, 5-tennis court, 12 | # 6-basketball court, 7-ground track field, 8-harbor, 9-bridge, 10-vehicle 13 | sign_idx_dict = { 14 | "car": 1, 15 | "truck": 2, 16 | "tractor": 4, 17 | "campingcar": 5, 18 | "van": 9, 19 | "vehicle": 10, 20 | "PickUp": 11, 21 | "ship": 23, 22 | "airplane": 31, 23 | } 24 | 25 | remove_label_ids = [7, 8] 26 | 27 | idx_sign_dict = { 28 | 1: "car", 29 | 2: "truck", 30 | 4: "tractor", 31 | 5: "campingcar", 32 | 9: "van", 33 | 10: "vehicle", 34 | 11: "pickup", 35 | 23: "ship", 36 | 31: "airplane" 37 | } 38 | -------------------------------------------------------------------------------- /mainmodels/models/resnet/settings.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | proj_dir = "/".join(os.path.abspath(__file__).split("/")[:-4]) 13 | print(proj_dir) 14 | 15 | class ResNetConfig(object): 16 | dataset = "cifar10" 17 | mode = "train" 18 | train_data_path = "/Volumes/projects/TrainData/CIFAR/cifar-10-batches-bin" 19 | eval_data_path = "/Volumes/projects/TrainData/CIFAR/cifar-10-batches-bin" 20 | image_size = 32 21 | log_base_dir = "/".join([proj_dir, "mainmodels/log/resnet"]) 22 | 23 | train_log_dir = "/".join([log_base_dir, "train"]) 24 | eval_log_dir = "/".join([log_base_dir, "eval"]) 25 | num_gpus = 0 26 | eval_once = False 27 | eval_batch_count = 50 28 | 29 | g_ResNetConfig = ResNetConfig() -------------------------------------------------------------------------------- /mainmodels/dataset/NWPUVHR10/nwpu_config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | # 1-airplane, 2-ship, 3-storage tank, 4-baseball diamond, 5-tennis court, 12 | # 6-basketball court, 7-ground track field, 8-harbor, 9-bridge, 10-vehicle 13 | sign_idx_dict = { 14 | "airplane": 1, 15 | "ship": 2, 16 | "storagetank": 3, 17 | "baseballdiamond": 4, 18 | "tenniscourt": 5, 19 | "basketballcourt": 6, 20 | "groundtrackfield": 7, 21 | "harbor": 8, 22 | "bridge": 9, 23 | "vehicle": 10 24 | } 25 | 26 | idx_sign_dict = { 27 | 1: "airplane", 28 | 2: "ship", 29 | 3: "storagetank", 30 | 4: "baseballdiamond", 31 | 5: "tenniscourt", 32 | 6: "basketballcourt", 33 | 7: "groundtrackfield", 34 | 8: "harbor", 35 | 9: "bridge", 36 | 10: "vehicle" 37 | } 38 | -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/lsd_config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | sign_idx_dict = { 12 | "airplane": 1, 13 | "ship": 2, 14 | "storagetank": 3, 15 | "baseballdiamond": 4, 16 | "tenniscourt": 5, 17 | "basketballcourt": 6, 18 | "groundtrackfield": 7, 19 | "harbor": 8, 20 | "bridge": 9, 21 | "vehicle": 10, 22 | "car": 10, 23 | "vehiclecar": 10, 24 | "campingcar": 11, 25 | "van": 11, 26 | "pickup": 12, 27 | "truck": 12, 28 | "tractor": 12, 29 | } 30 | 31 | idx_sign_dict = { 32 | 1: "airplane", 33 | 2: "ship", 34 | 3: "storagetank", 35 | 4: "baseballdiamond", 36 | 5: "tenniscourt", 37 | 6: "basketballcourt", 38 | 7: "groundtrackfield", 39 | 8: "harbor", 40 | 9: "bridge", 41 | 10: "vehicle", 42 | 11: "van", 43 | 12: "truck" 44 | } -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/tmp.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | 13 | dir_prefix = "/root/caffe/data/LSD10/" 14 | 15 | img_voc_dir = dir_prefix + "JPEGImages/" 16 | annotation_voc_dir = dir_prefix + "Annotations/" 17 | 18 | 19 | def organizing_model_data(from_file_path, save_path): 20 | if os.path.exists(save_path): 21 | os.remove(save_path) 22 | writer = open(save_path, "w") 23 | with open(from_file_path, "r") as reader: 24 | for line in reader: 25 | img_name = line.strip() 26 | abs_img_path = img_voc_dir + img_name 27 | abs_anno_path = annotation_voc_dir + img_name.replace("jpg", "xml") 28 | writer.write("{} {}\n".format(abs_img_path, abs_anno_path)) 29 | 30 | 31 | 32 | 33 | if __name__ == '__main__': 34 | from_file_path = "/root/caffe/data/LSD10/all_train.txt" 35 | save_path = "/root/caffe/data/LSD10/train.txt" 36 | organizing_model_data(from_file_path, save_path) -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/labelmap_lsd10.prototxt: -------------------------------------------------------------------------------- 1 | item { 2 | name: "none_of_the_above" 3 | label: 0 4 | display_name: "background" 5 | } 6 | item { 7 | name: "airplane" 8 | label: 1 9 | display_name: "airplane" 10 | } 11 | item { 12 | name: "ship" 13 | label: 2 14 | display_name: "ship" 15 | } 16 | item { 17 | name: "storagetank" 18 | label: 3 19 | display_name: "storagetank" 20 | } 21 | item { 22 | name: "baseballdiamond" 23 | label: 4 24 | display_name: "baseballdiamond" 25 | } 26 | item { 27 | name: "tenniscourt" 28 | label: 5 29 | display_name: "tenniscourt" 30 | } 31 | item { 32 | name: "basketballcourt" 33 | label: 6 34 | display_name: "basketballcourt" 35 | } 36 | item { 37 | name: "groundtrackfield" 38 | label: 7 39 | display_name: "groundtrackfield" 40 | } 41 | item { 42 | name: "harbor" 43 | label: 8 44 | display_name: "harbor" 45 | } 46 | item { 47 | name: "bridge" 48 | label: 9 49 | display_name: "bridge" 50 | } 51 | item { 52 | name: "vehicle" 53 | label: 10 54 | display_name: "vehicle" 55 | } 56 | item { 57 | name: "van" 58 | label: 11 59 | display_name: "van" 60 | } 61 | item { 62 | name: "truck" 63 | label: 12 64 | display_name: "truck" 65 | } -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/4 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | 13 | proj_dir = "/".join(os.path.abspath(__file__).split("/")[:-4]) 14 | 15 | class RpnPlusModelConfig(object): 16 | vgg16_model_path = "/".join([proj_dir, "model", "vgg16.npy"]) 17 | rpnplus_model_path = "/".join([proj_dir, "model", "rpnplus_model.npy"]) 18 | 19 | save_rpnplus_model_dir = "/".join([proj_dir, "model"]) 20 | 21 | image_input_dir = "/".join([proj_dir, "trainmodel", "data", "images"]) 22 | image_save_dir = "/".join([proj_dir, "output"]) 23 | 24 | # 主要是用来存储图像和对应的标签的位置的文件 25 | train_samples_index_path = \ 26 | "/Users/liuguiyang/Downloads/AirplaneSamples/Positive/train/index.txt" 27 | test_samples_index_path = \ 28 | "/Users/liuguiyang/Downloads/AirplaneSamples/Positive/test/index.txt" 29 | 30 | image_height = 720 31 | image_width = 960 32 | image_depth = 3 33 | 34 | feature_ratio = 8 35 | 36 | batch_size = 256 37 | 38 | g_RpnPlus_Config = RpnPlusModelConfig() 39 | -------------------------------------------------------------------------------- /mainmodels/dataset/TT100K/tt100k_sign.txt: -------------------------------------------------------------------------------- 1 | tt100k_sign = { 2 | "i10": 1, 3 | "i2": 2, 4 | "i4": 3, 5 | "i5": 4, 6 | "il100": 5, 7 | "il60": 6, 8 | "il80": 7, 9 | "il90": 8, 10 | "io": 9, 11 | "ip": 10, 12 | 13 | "w13": 11, 14 | "w21": 12, 15 | "w22": 13, 16 | "w30": 14, 17 | "w32": 15, 18 | "w55": 16, 19 | "w57": 17, 20 | "w58": 18, 21 | "w59": 19, 22 | "w63": 32, 23 | "wo": 20, 24 | 25 | "p1": 33, 26 | "p3": 39, 27 | "p5": 40, 28 | "p6": 41, 29 | "p9": 42, 30 | "p10": 21, 31 | "p11": 22, 32 | "p12": 34, 33 | "p19": 35, 34 | "p23": 36, 35 | "p26": 37, 36 | "p27": 38, 37 | 38 | "pa14": 23, 39 | "pb": 43, 40 | "pg": 44, 41 | "ph4": 24, 42 | "ph4.5": 45, 43 | "ph5": 46, 44 | 45 | "pl5": 52, 46 | "pl15": 49, 47 | "pl20": 25, 48 | "pl30": 50, 49 | "pl40": 51, 50 | "pl50": 53, 51 | "pl60": 54, 52 | "pl70": 26, 53 | "pl80": 55, 54 | "pl90": 27, 55 | "pl100": 47, 56 | "pl120": 48, 57 | 58 | "pm20": 28, 59 | "pm30": 56, 60 | "pm55": 57, 61 | 62 | "pn": 29, 63 | "pne": 58, 64 | 65 | "po": 30, 66 | "pr40": 59, 67 | "pr60": 60, 68 | "ps": 31 69 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TinyObject 2 | 主要是用来解决小目标的检测和跟踪问题 3 | 4 | ## 数据来源 5 | - Skybox Imaging Satellite Video Dataset 6 | - 斯坦福无人机航拍数据 7 | - 吉林一号视频数据 8 | 9 | --- 10 | 11 | ## 工程的主要目录结构 12 | ### 工程根目录 13 | ```bash 14 | . 15 | ├── README.md 16 | ├── mainmodels 17 | │   ├── dataset 18 | │   └── models 19 | └── source 20 | ├── bg 21 | ├── test 22 | ├── tools 23 | └── tracking 24 | ``` 25 | 26 | ### 模型目录 27 | ```bash 28 | . 29 | ├── dataset 30 | │   ├── NWPU_dataset_input.py 31 | │   ├── TT100k_traffic_sign_input.py 32 | │   ├── airplane_input.py 33 | │   ├── cifar_input.py 34 | │   ├── show_target.py 35 | │   ├── tools.py 36 | │   └── usa_traffic_sign_input.py 37 | └── models 38 | ├── autoencoder 39 | ├── resnet 40 | ├── rpnplus 41 | ├── ssd 42 | └── tradition 43 | ``` 44 | 45 | --- 46 | 47 | ## TODOLIST 48 | - [x] 对JS1号卫星图像数据进行裁剪,并将相应的目标的位置进行坐标变换 49 | - [x] 使用C++编写SSD网络中不同feature_map中对应的anchor的标签数据,这其中问题较多 50 | - [x] 在构造anchor boxes时,需要将inner的box剔除出去 51 | - [x] 对于随机采样出来的图像,在生成SSD训练样本时,却发现仅仅由大约 30% 的图像能在不同的feature map中找到对应的候选框 52 | - [x] 将物体覆盖的率的评价函数由IOU改成overlap-ratio 53 | - [x] 使用Python将anchor在没有获得候选框的图像的结果可视化出来,分析在不同目标尺度上的差异 54 | - [x] 对于上述结果进行可视化分析,找到适合的阈值,使得SSD模型能覆盖大部分目标 55 | - [ ] 现在训练SSD网络,base网络是自己定义的普通的CNN网络 56 | - [ ] 紧接着将ResNet作为base网络,对比模型结果 57 | -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/tools/showlabel.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/4 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import shutil 13 | 14 | from mainmodels.models.rpnplus.tools.image_pylib import IMGLIB 15 | 16 | if __name__ == '__main__': 17 | imglib = IMGLIB() 18 | 19 | imageDir = 'image/' 20 | 21 | anoDir = 'ano/' 22 | 23 | saveImgDir = 'saveDir/' 24 | thr = 0.99 25 | imageNames = [] 26 | 27 | if os.path.isdir(saveImgDir): 28 | shutil.rmtree(saveImgDir) 29 | os.mkdir(saveImgDir) 30 | 31 | for file in os.listdir(imageDir): 32 | file_path = os.path.join(imageDir, file) 33 | if os.path.isfile(file_path) and os.path.splitext(file_path)[ 34 | 1] == '.jpg': 35 | nameNow = os.path.splitext(file)[0] 36 | imageName = imageDir + '/' + nameNow + '.jpg' 37 | anoName = anoDir + '/' + nameNow + '.txt' 38 | saveImgName = saveImgDir + '/' + nameNow + '.jpg' 39 | imglib.read_img(imageName) 40 | imglib.read_ano(anoName) 41 | imglib.drawBox(thr, False) 42 | imglib.save_img(saveImgName) 43 | print(imageName) 44 | 45 | -------------------------------------------------------------------------------- /mainmodels/scripts/create_list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | root_dir=/root/caffe/data/CSUVOCDATA/ 4 | #sub_dir=ImageSets/Main 5 | bash_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 6 | dst_file="/root/caffe/data/CSUVIDEO/test.txt" 7 | for dataset in trainval test 8 | do 9 | # dst_file=$bash_dir/$dataset.txt 10 | # if [ -f $dst_file ] 11 | # then 12 | # rm -f $dst_file 13 | # fi 14 | # 15 | # echo "Create list for $dataset..." 16 | # dataset_file=$root_dir/$sub_dir/$dataset.txt 17 | # 18 | # img_file=$bash_dir/$dataset"_img.txt" 19 | # cp $dataset_file $img_file 20 | # sed -i "s/^/\/JPEGImages\//g" $img_file 21 | # sed -i "s/$/.jpg/g" $img_file 22 | # 23 | # label_file=$bash_dir/$dataset"_label.txt" 24 | # cp $dataset_file $label_file 25 | # sed -i "s/^/\/Annotations\//g" $label_file 26 | # sed -i "s/$/.xml/g" $label_file 27 | # 28 | # paste -d' ' $img_file $label_file >> $dst_file 29 | # 30 | # rm -f $label_file 31 | # rm -f $img_file 32 | 33 | # Generate image name and size infomation. 34 | if [ $dataset == "test" ] 35 | then 36 | $bash_dir/../../build/tools/get_image_size $root_dir $dst_file $bash_dir/$dataset"_name_size.txt" 37 | fi 38 | 39 | # Shuffle trainval file. 40 | # if [ $dataset == "trainval" ] 41 | # then 42 | # rand_file=$dst_file.random 43 | # cat $dst_file | perl -MList::Util=shuffle -e 'print shuffle();' > $rand_file 44 | # mv $rand_file $dst_file 45 | # fi 46 | done -------------------------------------------------------------------------------- /mainmodels/scripts/组织模型数据.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/7 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | 13 | dir_prefix = "/root/repos/CSUVOCFormat/" 14 | SSD_IMG_W, SSD_IMG_H = 512, 512 15 | 16 | tpl_img_voc_dir = dir_prefix + "{}x{}/{}/JPEGImages/" 17 | tpl_annotation_voc_dir = dir_prefix + "{}x{}/{}/Annotations/" 18 | 19 | 20 | def organizing_model_data(video_names, save_path): 21 | if os.path.exists(save_path): 22 | os.remove(save_path) 23 | for video_name in video_names: 24 | img_voc_dir = tpl_img_voc_dir.format(SSD_IMG_W, SSD_IMG_H, video_name) 25 | annotation_voc_dir = tpl_annotation_voc_dir.format( 26 | SSD_IMG_W, SSD_IMG_H, video_name) 27 | with open(save_path, "a") as writer: 28 | for img_name in os.listdir(img_voc_dir): 29 | if not os.path.exists( 30 | annotation_voc_dir+img_name.replace("jpg", "xml")): 31 | continue 32 | writer.write("{} {}\n".format( 33 | img_voc_dir+img_name,annotation_voc_dir+img_name.replace("jpg", "xml"))) 34 | 35 | 36 | if __name__ == '__main__': 37 | video_names = ["large_000013363_total", "large_000014631_total", "large_minneapolis_1_total"] 38 | save_path = "/root/caffe/data/CSUVIDEO/trainval.txt" 39 | organizing_model_data(video_names, save_path) -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/readme.md: -------------------------------------------------------------------------------- 1 | ## RPN Model 2 | 3 | ### Model Architecture 4 | ```bash 5 | content_rpn/conv1_1/filter:0 [5, 5, 1, 64] 0.078693 6 | content_rpn/conv1_1/biases:0 [64] 0.0 7 | content_rpn/conv1_2/filter:0 [5, 5, 64, 64] 5.1143 8 | content_rpn/conv1_2/biases:0 [64] 0.0 9 | content_rpn/conv2_1/filter:0 [5, 5, 64, 128] 10.2417 10 | content_rpn/conv2_1/biases:0 [128] 0.0 11 | content_rpn/conv2_2/filter:0 [5, 5, 128, 128] 20.4513 12 | content_rpn/conv2_2/biases:0 [128] 0.0 13 | content_rpn/conv3_1/filter:0 [3, 3, 128, 64] 3.66532 14 | content_rpn/conv3_1/biases:0 [64] 0.0 15 | content_rpn/conv3_2/filter:0 [3, 3, 64, 64] 1.84733 16 | content_rpn/conv3_2/biases:0 [64] 0.0 17 | content_rpn/conv3_3/filter:0 [3, 3, 64, 64] 1.85867 18 | content_rpn/conv3_3/biases:0 [64] 0.0 19 | content_rpn/conv4_1/filter:0 [3, 3, 64, 64] 1.83939 20 | content_rpn/conv4_1/biases:0 [64] 0.0 21 | content_rpn/conv4_2/filter:0 [3, 3, 64, 64] 1.85312 22 | content_rpn/conv4_2/biases:0 [64] 0.0 23 | content_rpn/conv4_3/filter:0 [3, 3, 64, 64] 1.85742 24 | content_rpn/conv4_3/biases:0 [64] 0.0 25 | content_rpn/gamma3:0 [] 1.0 26 | content_rpn/gamma4:0 [] 0.5 27 | content_rpn/conv_proposal_2/filter:0 [5, 2, 128, 128] 8.17913 28 | content_rpn/conv_proposal_2/biases:0 [128] 0.0 29 | content_rpn/conv_proposal_3/filter:0 [5, 2, 64, 256] 8.19745 30 | content_rpn/conv_proposal_3/biases:0 [256] 0.0 31 | content_rpn/conv_proposal_4/filter:0 [5, 2, 64, 256] 8.20255 32 | content_rpn/conv_proposal_4/biases:0 [256] 0.0 33 | content_rpn/conv_cls_score/filter:0 [1, 1, 640, 18] 0.576218 34 | content_rpn/conv_cls_score/biases:0 [18] 0.0 35 | content_rpn/conv_bbox_pred/filter:0 [1, 1, 640, 36] 1.16785 36 | content_rpn/conv_bbox_pred/biases:0 [36] 0.0 37 | ``` -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/产生标准文件.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/6 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | from mainmodels.dataset.LSD10.tools import extract_target_from_xml 12 | from mainmodels.dataset.LSD10.lsd_config import sign_idx_dict, idx_sign_dict 13 | 14 | 15 | def get_true_target_name(input_name): 16 | return idx_sign_dict[sign_idx_dict[input_name]] 17 | 18 | dir_prefix = "/Volumes/projects/repos/RSI/LSD10/" 19 | anno_dir = dir_prefix + "Annotations/" 20 | save_dir = dir_prefix + "std_results/" 21 | 22 | all_test_filepath = dir_prefix + "all_test.txt" 23 | tpl_std_name = "comp4_det_std_{}.txt" 24 | 25 | 26 | # "target": { "filename": [[pos01], [pos02]]} 27 | all_details = {} 28 | with open(all_test_filepath, "r") as test_reader: 29 | for line in test_reader: 30 | img_name = ".".join(line.strip().split(".")[:-1]) 31 | anno_filepath = anno_dir + img_name + ".xml" 32 | anno_details = extract_target_from_xml(anno_filepath) 33 | for item in anno_details: 34 | item[-1] = get_true_target_name(item[-1]) 35 | for item in anno_details: 36 | target_name = item[-1] 37 | if target_name not in all_details: 38 | all_details[target_name] = dict() 39 | if img_name not in all_details[target_name]: 40 | all_details[target_name][img_name] = list() 41 | all_details[target_name][img_name].append(item[:4]) 42 | # 数据组织完成,现在需要分类进行存储 43 | for key, val in all_details.items(): 44 | with open(save_dir+tpl_std_name.format(key), "w") as f: 45 | for file_name, target_annos in val.items(): 46 | for annos in target_annos: 47 | f.write("{} {} {} {} {} {}\n".format( 48 | file_name, 1.0, annos[0], annos[1], annos[2], annos[3])) -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/map.txt: -------------------------------------------------------------------------------- 1 | iou == 0.625 and prob == 0.625 2 | airplane 0.625 0.625 0.9545454545454546 0.9665271966527197 3 | ship 0.625 0.625 0.8955223880597015 0.8450704225352113 4 | storagetank 0.625 0.625 0.9660493827160493 0.7904040404040404 5 | baseballdiamond 0.625 0.625 0.9305555555555556 0.9852941176470589 6 | tenniscourt 0.625 0.625 0.9549180327868853 0.8825757575757576 7 | basketballcourt 0.625 0.625 0.9493670886075949 0.974025974025974 8 | groundtrackfield 0.625 0.625 1.0 1.0 9 | harbor 0.625 0.625 0.9605263157894737 0.948051948051948 10 | bridge 0.625 0.625 0.8823529411764706 0.9 11 | vehicle 0.625 0.625 0.8067226890756303 0.5663716814159292 12 | van 0.625 0.625 0.8372093023255814 0.6666666666666666 13 | truck 0.625 0.625 0.7441860465116279 0.44755244755244755 14 | 15 | mAP = (0.9545454545454546 + 0.8955223880597015 + 0.9660493827160493 + 0 16 | .9305555555555556 + 0.9549180327868853 + 0.9493670886075949 + 1.0 + 0 17 | .9605263157894737 + 0.8823529411764706 + 0.8067226890756303 + 0 18 | .8372093023255814 + 0.7441860465116279) / 12 19 | mAP = 0.906829599762502 20 | 21 | 22 | iou == 0.625 and prob == 0.75 23 | airplane 0.75 0.625 0.9613733905579399 0.9613733905579399 24 | ship 0.75 0.625 0.908256880733945 0.7279411764705882 25 | storagetank 0.75 0.625 1.0 0.42209631728045327 26 | baseballdiamond 0.75 0.625 0.9386792452830188 0.9754901960784313 27 | tenniscourt 0.75 0.625 0.9647577092511013 0.8423076923076923 28 | basketballcourt 0.75 0.625 0.9493670886075949 0.974025974025974 29 | groundtrackfield 0.75 0.625 1.0 1.0 30 | harbor 0.75 0.625 0.9857142857142858 0.8961038961038961 31 | bridge 0.75 0.625 0.8823529411764706 0.9 32 | vehicle 0.75 0.625 0.8973384030418251 0.4729458917835671 33 | van 0.75 0.625 1.0 0.7 34 | truck 0.75 0.625 0.7586206896551724 0.4888888888888889 35 | 36 | mAP = (0.9613733905579399 + 0.908256880733945 + 1.0 + 0.9386792452830188 + 0.9647577092511013 + 0.9493670886075949 + 1.0 + 0.9857142857142858 + 0.8823529411764706 + 0.8973384030418251 + 1.0 + 0.7586206896551724) 37 | mAP = 0.9372050528351128 -------------------------------------------------------------------------------- /source/tools/convert_img.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | """ 3 | @contact: liuguiyang15@mails.ucas.edu.cn 4 | @file: convert_img.py.py 5 | @time: 2018/5/23 23:22 6 | """ 7 | 8 | import cv2 9 | from matplotlib import pyplot as plt 10 | 11 | 12 | img_path = "/Users/liuguiyang/Desktop/000011_612_204_1124_716_7.jpg" 13 | img_name = "000011_612_204_1124_716_7.jpg" 14 | 15 | image = cv2.imread(img_path) 16 | 17 | xImg = cv2.flip(image, 1, dst=None) 18 | xImg1 = cv2.flip(image, 0, dst=None) 19 | xImg2 = cv2.flip(image, -1, dst=None) 20 | 21 | cv2.imwrite("{}_{}".format("flip1", img_name), xImg) 22 | cv2.imwrite("{}_{}".format("flip0", img_name), xImg1) 23 | cv2.imwrite("{}_{}".format("flip-1", img_name), xImg2) 24 | 25 | 26 | h, w = image.shape[:2] 27 | crop_h, crop_w = h - 50, w - 50 28 | import random 29 | 30 | for i in range(3): 31 | x = random.randint(0, 50) 32 | y = random.randint(0, 50) 33 | crop_image = image[y:y+crop_h, x:x+crop_w] 34 | crop_image = cv2.resize(crop_image, (w, h)) 35 | cv2.imwrite("{}_{}_{}".format(x, y, img_name), crop_image) 36 | 37 | 38 | 39 | # 定义旋转rotate函数 40 | def rotate(image, angle, center=None, scale=1.0): 41 | # 获取图像尺寸 42 | (h, w) = image.shape[:2] 43 | 44 | # 若未指定旋转中心,则将图像中心设为旋转中心 45 | if center is None: 46 | center = (w / 2, h / 2) 47 | 48 | # 执行旋转 49 | M = cv2.getRotationMatrix2D(center, angle, scale) 50 | rotated = cv2.warpAffine(image, M, (w, h)) 51 | 52 | # 返回旋转后的图像 53 | return rotated 54 | 55 | # 将原图旋转不同角度 56 | rotated = rotate(image, 45) 57 | cv2.imshow("Rotated by 45 Degrees", rotated) 58 | cv2.imwrite("{}_{}".format("r45", img_name), rotated) 59 | rotated = rotate(image, -45) 60 | cv2.imshow("Rotated by -45 Degrees", rotated) 61 | cv2.imwrite("{}_{}".format("r-45", img_name), rotated) 62 | rotated = rotate(image, 90) 63 | cv2.imshow("Rotated by 90 Degrees", rotated) 64 | cv2.imwrite("{}_{}".format("r90", img_name), rotated) 65 | rotated = rotate(image, -90) 66 | cv2.imshow("Rotated by -90 Degrees", rotated) 67 | cv2.imwrite("{}_{}".format("r-90", img_name), rotated) 68 | cv2.waitKey(0) -------------------------------------------------------------------------------- /mainmodels/dataset/VEDAI/数据可视化.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import cv2 13 | 14 | from mainmodels.dataset.VEDAI.tools import fetch_targets_info 15 | 16 | 17 | base_dir = "/Volumes/projects/repos/RSI/VEDAI/" 18 | img_512_dir = base_dir + "512/Vehicules512/" 19 | anno_512_dir = base_dir + "512/Annotations512/" 20 | 21 | 22 | # 标注出给定的坐标下的目标区域 23 | def annotate_targets(src_image, anno_targets, line_width=1): 24 | for info in anno_targets: 25 | if len(info) == 9: 26 | pre_x, pre_y = info[0], info[1] 27 | for i in range(2, 8, 2): 28 | x, y = info[i], info[i+1] 29 | cv2.line(src_image, (pre_x, pre_y), (x, y), (0, 0, 255), line_width) 30 | pre_x, pre_y = x, y 31 | cv2.line(src_image, (pre_x, pre_y), (info[0], info[1]), (0, 0, 255), line_width) 32 | elif len(info) == 5: 33 | xmin, ymin, xmax, ymax = info[:4] 34 | cv2.rectangle(src_image, (xmin, ymin), (xmax, ymax), (255, 0, 0), line_width) 35 | return src_image 36 | 37 | def show_targets(img_dir, anno_dir): 38 | for img_name in os.listdir(img_dir): 39 | if img_name.startswith("._"): 40 | continue 41 | img_type = img_name.split(".")[0].split("_")[1] 42 | if img_type == "ir": 43 | continue 44 | img_prefix = img_name.split("_")[0] 45 | abs_img_path = img_dir + img_name 46 | abs_anno_path = anno_dir + img_prefix + ".txt" 47 | anno_targets, normal_anno_targets = fetch_targets_info(abs_anno_path) 48 | src_img = cv2.imread(abs_img_path) 49 | src_img = annotate_targets(src_img, anno_targets) 50 | src_img = annotate_targets(src_img, normal_anno_targets) 51 | cv2.imshow("src", src_img) 52 | print(img_name) 53 | cv2.waitKey() 54 | 55 | 56 | if __name__ == '__main__': 57 | show_targets(img_512_dir, anno_512_dir) -------------------------------------------------------------------------------- /mainmodels/dataset/VEDAI/vedia_ssd_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import cv2 13 | 14 | from mainmodels.dataset.VEDAI.tools import fetch_targets_info 15 | from mainmodels.dataset.VEDAI.tools import fetch_xml_format 16 | 17 | 18 | vedia_voc_dir = "/Volumes/projects/repos/RSI/VEDAI/VEDIAVOCFORAMT/" 19 | vedia_voc_image_dir = vedia_voc_dir + "JPEGImages/" 20 | vedia_voc_anno_dir = vedia_voc_dir + "Annotations/" 21 | 22 | base_dir = "/Volumes/projects/repos/RSI/VEDAI/" 23 | img_512_dir = base_dir + "512/Vehicules512/" 24 | anno_512_dir = base_dir + "512/Annotations512/" 25 | 26 | 27 | # 将vedia的原始数据转换成ssd模型voc格式的数据输入 28 | def create_ssd_samples(img_dir, anno_dir): 29 | if not os.path.exists(vedia_voc_image_dir): 30 | os.makedirs(vedia_voc_image_dir) 31 | if not os.path.exists(vedia_voc_anno_dir): 32 | os.makedirs(vedia_voc_anno_dir) 33 | 34 | for img_name in os.listdir(img_dir): 35 | if img_name.startswith("._"): 36 | continue 37 | img_type = img_name.split(".")[0].split("_")[1] 38 | if img_type == "ir": 39 | continue 40 | img_prefix = img_name.split("_")[0] 41 | abs_img_path = img_dir + img_name 42 | abs_anno_path = anno_dir + img_prefix + ".txt" 43 | anno_targets, normal_anno_targets = fetch_targets_info(abs_anno_path) 44 | src_img = cv2.imread(abs_img_path) 45 | xml_obj = fetch_xml_format(src_img, img_name.replace("png", "jpg"), 46 | normal_anno_targets) 47 | # 将图像和标注文件拷贝到指定的位置 48 | with open(vedia_voc_anno_dir+img_name.replace("png", "xml"), "w") as \ 49 | anno_writer: 50 | anno_writer.write(xml_obj) 51 | cv2.imwrite(vedia_voc_image_dir+img_name.replace("png", "jpg"), 52 | src_img, [int( cv2.IMWRITE_JPEG_QUALITY), 100]) 53 | print(img_name) 54 | 55 | 56 | if __name__ == '__main__': 57 | create_ssd_samples(img_512_dir, anno_512_dir) -------------------------------------------------------------------------------- /source/tools/marklane.py: -------------------------------------------------------------------------------- 1 | #! coding : utf-8 2 | 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | from matplotlib import image as mpimg 6 | import os, json 7 | 8 | class DrawLine(object): 9 | def __init__(self): 10 | self.filepath = "/Users/liuguiyang/Documents/TrackingDataSet/JL1st/src/0001.jpg" 11 | self.writepath = self.filepath.replace(".jpg", ".txt") 12 | 13 | self.filepath.split('/')[-1].split('.') 14 | self.fig = plt.figure(1) 15 | self.ax = self.fig.add_subplot(111) 16 | self.saveJson = {} 17 | self.startUp() 18 | 19 | def startUp(self): 20 | self.points = [] 21 | self.alreadyDrawPos = [] 22 | self.lineInd = 0 23 | self.img = mpimg.imread(self.filepath) 24 | 25 | self.cid = self.fig.canvas.mpl_connect('button_press_event', self.click) 26 | self.kid = self.fig.canvas.mpl_connect('key_press_event', self.on_key_press) 27 | plt.imshow(self.img) 28 | 29 | def click(self, event): # the function to run everytime click the plane 30 | self.points.append([event.xdata, event.ydata]) 31 | pointsarray = np.asarray(self.points) 32 | drawMark = self.ax.plot(pointsarray[:, 0], pointsarray[:, 1], 'ro') 33 | self.alreadyDrawPos.append(drawMark[0]) 34 | self.ax.figure.canvas.draw() 35 | 36 | def on_key_press(self, event): 37 | ''' 38 | key w: save the line_th 39 | //key n: start the new line 40 | //key c: clear the cur line data, which have labled 41 | key q: close the figure window 42 | ''' 43 | if event.key in 'w': 44 | # writer the point into the file 45 | if len(self.points) <= 0: 46 | return 47 | self.saveJson[self.lineInd] = self.points 48 | self.points = [] 49 | self.lineInd += 1 50 | 51 | if event.key in 'q': 52 | if len(self.saveJson) == 0: 53 | return 54 | with open(self.writepath, 'w') as handle: 55 | json.dump(self.saveJson, handle) 56 | plt.close(self.fig) 57 | 58 | 59 | if __name__ == "__main__": 60 | draw = DrawLine() 61 | plt.show() 62 | -------------------------------------------------------------------------------- /mainmodels/models/tradition/config.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/3 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | 13 | proj_root = "/".join(os.path.abspath(__file__).split("/")[:-4]) 14 | 15 | class DataSet: 16 | positive_nums = 10 17 | negative_nums = 10 18 | class_nums = 2 19 | class_labels = [0, 1] 20 | 21 | class BaseConfig(object): 22 | train_samplesp_path = "/Volumes/projects/第三方数据下载/JL1ST/train.tfrecords" 23 | test_samples_path = "/Volumes/projects/第三方数据下载/JL1ST/test.tfrecords" 24 | 25 | # image info 26 | image_width = 56 27 | image_height = 56 28 | image_depth = 1 29 | 30 | sample_ratio = { 31 | "train": 0.7, 32 | "test": 0.2, 33 | "valid": 0.1 34 | } 35 | batch_size = 96 36 | NUM_CLASSES = 2 37 | NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 8471 38 | NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 747 39 | labels = { 40 | "background": 0, 41 | "airplane": 1, 42 | # "ship": 2, 43 | # "storage_tank": 3, 44 | # "harbor": 4, 45 | # "bridge": 5 46 | } 47 | 48 | # Constants describing the training process. 49 | MOVING_AVERAGE_DECAY = 0.9999 # The decay to use for the moving average. 50 | NUM_EPOCHS_PER_DECAY = 350.0 # Epochs after which learning rate decays. 51 | LEARNING_RATE_DECAY_FACTOR = 0.1 # Learning rate decay factor. 52 | INITIAL_LEARNING_RATE = 0.1 # Initial learning rate. 53 | 54 | use_fp16 = False 55 | 56 | dataset = DataSet() 57 | 58 | class CNNConfig(BaseConfig): 59 | # train_log_dir = "/".join([proj_root, "trainmodel", "log", "cnn", "train"]) 60 | train_log_dir = "/Volumes/projects/第三方数据下载/train_log/cnn/train/" 61 | eval_log_dir = "/Volumes/projects/第三方数据下载/train_log/cnn/valid/" 62 | 63 | class MultiCNNConfig(BaseConfig): 64 | train_log_dir = "/".join([proj_root, "trainmodel", "log", "multicnn", 65 | "train"]) 66 | eval_log_dir = "/".join([proj_root, "trainmodel", "log", "multicnn", 67 | "eval"]) 68 | 69 | g_CNNConfig = CNNConfig() 70 | g_MultiCNNConfig = MultiCNNConfig() 71 | -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/model.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function 9 | 10 | import tensorflow as tf 11 | 12 | class SingleLayerAE(): 13 | 14 | def _pad(self, input_x, filter_side): 15 | amount = filter_side - 1 16 | paddings = [[0, 0], [amount, amount], [amount, amount], [0, 0]] 17 | return tf.pad(input_x, paddings=paddings) 18 | 19 | def get(self, images, train_phase=False, l2_penalty=0.0): 20 | initializer = tf.contrib.layers.variance_scaling_initializer( 21 | factor=2.0, 22 | mode='FAN_IN', 23 | uniform=False, 24 | seed=69, 25 | dtype=tf.float32) 26 | filter_side = 3 27 | filters_number = 32 28 | 29 | with tf.variable_scope("prepare"): 30 | input_x = self._pad(images, filter_side) 31 | 32 | with tf.variable_scope("encode"): 33 | encoding = conv( 34 | input_x=input_x, 35 | shape=[filter_side, filter_side, input_x.get_shape()[3].value, filters_number], 36 | stride=1, 37 | padding="VALID", 38 | train_phase=train_phase, 39 | activation=tf.nn.tanh, 40 | wd=l2_penalty, 41 | initializer=initializer 42 | ) 43 | # encode layer info (32, 34, 34, 32) 44 | print("encode layer info", encoding.get_shape()) 45 | with tf.variable_scope("decode"): 46 | decoding = conv( 47 | input_x=encoding, 48 | shape=[filter_side, filter_side, filters_number, input_x.get_shape()[3].value], 49 | stride=1, 50 | padding="VALID", 51 | train_phase=train_phase, 52 | activation=tf.nn.tanh, 53 | initializer=initializer 54 | ) 55 | 56 | is_training_ = tf.placeholder_with_default( 57 | False, shape=(), name="is_training_" 58 | ) 59 | return is_training_, decoding 60 | 61 | def loss(self, predictions, real_values): 62 | with tf.variable_scope("loss"): 63 | mse = tf.divide( 64 | tf.reduce_mean( 65 | tf.square(tf.subtract(predictions, real_values)) 66 | ), 2.0, name="mse") 67 | return mse -------------------------------------------------------------------------------- /mainmodels/dataset/show_target.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/8/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os, codecs, json 12 | import cv2 13 | 14 | from mainmodels.models.ssd.settings import g_SSDConfig 15 | 16 | 17 | dir_prefix = g_SSDConfig.DATASET_BASE_DIR 18 | if not dir_prefix.endswith("/"): 19 | dir_prefix += "/" 20 | 21 | 22 | def show_src_dir(): 23 | for img_file_name in os.listdir(dir_prefix + "src/"): 24 | if img_file_name.startswith("._"): 25 | continue 26 | print(img_file_name) 27 | anno_file = ".".join(img_file_name.split(".")[:-1] + ["json"]) 28 | print(anno_file) 29 | src_img = cv2.imread(dir_prefix+"src/"+img_file_name) 30 | with codecs.open(dir_prefix+"annotation/"+anno_file, "r") as handle: 31 | anno_targets = json.load(handle) 32 | for area in anno_targets: 33 | print(area) 34 | cv2.rectangle(src_img, (area[0], area[1]), (area[2], area[3]), 35 | (0, 255, 0), 2) 36 | cv2.imshow("dsfdsf", src_img) 37 | cv2.waitKey() 38 | 39 | 40 | # 展示原始的标注信息 41 | def show_annotation_image_file(file_name): 42 | anno_file = ".".join(file_name.split(".")[:-1] + ["json"]) 43 | print(anno_file) 44 | abs_image_path = dir_prefix + "total_src/" + file_name 45 | print(abs_image_path) 46 | src_img = cv2.imread(dir_prefix + "src/" + file_name) 47 | with codecs.open(dir_prefix + "annotation/" + anno_file, "r") as handle: 48 | anno_targets = json.load(handle) 49 | for area in anno_targets: 50 | cv2.rectangle(src_img, (area[0], area[1]), (area[2], area[3]), 51 | (0, 0, 255), 2) 52 | cv2.imshow("GT", src_img) 53 | cv2.waitKey(0) 54 | 55 | 56 | # 展示SSD构造训练样本时产生的候选框的目标 57 | def show_ssd_prepare_boxes(file_name): 58 | ssd_box_name = ".".join(file_name.strip().split(".")[:-1]) + "_annotation.txt" 59 | ssd_box_path = dir_prefix + "ssd_label_box_src/" + ssd_box_name 60 | src_img = cv2.imread(dir_prefix + "src/" + file_name) 61 | with codecs.open(ssd_box_path, "r", "utf8") as handler: 62 | for line in handler: 63 | line = line.strip().split(',') 64 | box_pos = [int(float(i)) for i in line] 65 | cv2.rectangle(src_img, tuple(box_pos[:2]), tuple(box_pos[2:]), 66 | (0, 255, 0), 1) 67 | cv2.imshow("SSD_BOX", src_img) 68 | cv2.waitKey(0) 69 | 70 | 71 | if __name__ == '__main__': 72 | pass -------------------------------------------------------------------------------- /source/bg/bgutils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from skimage.feature import hog 4 | from skimage import exposure 5 | 6 | def centroid_histogram(clt): 7 | # grab the number of different clusters and create a histogram 8 | # based on the number of pixels assigned to each cluster 9 | numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1) 10 | (hist, _) = np.histogram(clt.labels_, bins = numLabels) 11 | 12 | # normalize the histogram, such that it sums to one 13 | hist = hist.astype("float") 14 | hist /= hist.sum() 15 | 16 | # return the histogram 17 | return hist 18 | 19 | def plot_colors(hist, centroids): 20 | # initialize the bar chart representing the relative frequency 21 | # of each of the colors 22 | bar = np.zeros((50, 300, 3), dtype="uint8") 23 | startX = 0 24 | 25 | # loop over the percentage of each cluster and the color of 26 | # each cluster 27 | for (percent, color) in zip(hist, centroids): 28 | # plot the relative percentage of each cluster 29 | endX = startX + (percent * 300) 30 | cv2.rectangle(bar, (int(startX), 0), (int(endX), 50), 31 | color.astype("uint8").tolist(), -1) 32 | startX = endX 33 | print(color) 34 | # return the bar chart 35 | return bar 36 | 37 | def getAngle(curPos, nextPos): 38 | c_x, c_y = int(curPos[0]), int(curPos[1]) 39 | n_x, n_y = int(nextPos[0]), int(nextPos[1]) 40 | if n_y - n_x == 0: 41 | return np.pi / 2 42 | return np.arctan(1.0 * (n_y - c_y) / 1.0 * (n_x - c_x)) 43 | 44 | def split_lables(lables_pred): 45 | c_lables = np.unique(lables_pred) 46 | res = [] 47 | for k in c_lables: 48 | tmp = lables_pred.copy() 49 | tmp = tmp.astype(np.uint8) 50 | for i in range(lables_pred.shape[0]): 51 | for j in range(lables_pred.shape[1]): 52 | if lables_pred[i][j] == k: 53 | tmp[i][j] = 1 54 | else: 55 | tmp[i][j] = 0 56 | res.append(tmp) 57 | return res 58 | 59 | def convert2Dto3D(lables_array, lables_centers): 60 | h, w = lables_array.shape 61 | bar = np.zeros((h, w, 3), dtype="uint8") 62 | colors = np.unique(lables_array) 63 | for i in range(h): 64 | for j in range(w): 65 | ind = lables_array[i][j] 66 | bar[i][j] = lables_centers[ind].astype(np.uint8) 67 | return bar 68 | 69 | def hogFeature(image): 70 | if len(image.shape) == 3: 71 | image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) 72 | 73 | fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), 74 | cells_per_block=(1, 1), visualise=True) 75 | # Rescale histogram for better display 76 | hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) 77 | return fd, hog_image 78 | 79 | def getIntPoint(fPos): 80 | return (int(fPos[0]), int(fPos[1])) -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/显示结果图.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/6 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import cv2 13 | 14 | from mainmodels.dataset.LSD10.tools import extract_target_from_xml 15 | from mainmodels.dataset.LSD10.lsd_config import sign_idx_dict, idx_sign_dict 16 | 17 | dir_prefix = "/Volumes/projects/repos/RSI/LSD10/" 18 | anno_dir = dir_prefix + "Annotations/" 19 | image_dir = dir_prefix + "JPEGImages/" 20 | model_res_dir = dir_prefix + "results/" 21 | all_test_filepath = dir_prefix + "all_test.txt" 22 | 23 | def merge_model_results(prob=0.5): 24 | # {"f_name": [[prob,area,target],]} 25 | all_model_results = dict() 26 | for filename in os.listdir(model_res_dir): 27 | target_name = filename.split(".")[0].split("_")[-1] 28 | model_res_path = model_res_dir + filename 29 | with open(model_res_path, "r") as res_reader: 30 | for line in res_reader: 31 | line = line.strip().split(" ") 32 | if float(line[1]) < prob: 33 | continue 34 | f_name, t_prob = line[0]+".jpg", float(line[1]) 35 | t_area = [int(float(a)) for a in line[2:]] 36 | if f_name not in all_model_results: 37 | all_model_results[f_name] = list() 38 | all_model_results[f_name].append([t_prob]+t_area+[target_name]) 39 | return all_model_results 40 | 41 | all_model_results = merge_model_results() 42 | 43 | def get_true_target_name(input_name): 44 | return idx_sign_dict[sign_idx_dict[input_name]] 45 | 46 | with open(all_test_filepath, "r") as test_reader: 47 | for line in test_reader: 48 | filename = image_dir + line.strip() 49 | gt_anno_path = anno_dir + ".".join( 50 | line.strip().split(".")[:-1] + ["xml"]) 51 | anno_details = extract_target_from_xml(gt_anno_path) 52 | for item in anno_details: 53 | item[-1] = get_true_target_name(item[-1]) 54 | # print(anno_details) 55 | src_img = cv2.imread(filename) 56 | for area in anno_details: 57 | cv2.rectangle(src_img, 58 | (area[0], area[1]), 59 | (area[2], area[3]), 60 | (0, 255, 0), 2) 61 | # 绘制模型检测结果的目标位置 62 | # print(all_model_results[line.strip()]) 63 | for items in all_model_results[line.strip()]: 64 | cv2.rectangle(src_img, 65 | (items[1], items[2]), 66 | (items[3], items[4]), 67 | (0, 0, 255), 2) 68 | cv2.imshow(line.strip(), src_img) 69 | cv2.waitKey() 70 | -------------------------------------------------------------------------------- /source/tools/cropvideo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | import importlib 5 | 6 | import os 7 | import sys 8 | sys.path.append("/usr/local/Cellar/opencv3/3.2.0/lib/python3.6/site-packages") 9 | importlib.reload(sys) 10 | 11 | import cv2 12 | import matplotlib.pylab as plt 13 | 14 | class VideoTracking(): 15 | def __init__(self): 16 | self.video_dir = "/Volumes/projects/第三方数据下载/长光卫星高清视频第二批/处理的视频" 17 | self.video_name = \ 18 | "20170602-美国-明尼阿波利斯01.mp4" 19 | self.cap, self.prev, self.cur = None, None, None 20 | self.isFirst = True 21 | self.track_len = 10 22 | self.detect_interval = 5 23 | self.tracks = [] 24 | self.cam = cv2.VideoCapture("/".join([self.video_dir, self.video_name])) 25 | self.writer = None 26 | fps = int(self.cam.get(cv2.CAP_PROP_FPS)) 27 | size = (int(self.cam.get(cv2.CAP_PROP_FRAME_WIDTH)), 28 | int(self.cam.get(cv2.CAP_PROP_FRAME_HEIGHT))) 29 | print(fps, size) 30 | self.frameInd = 1 31 | 32 | def splitImages(self, frame, blockNums = 4): 33 | width, height = frame.shape[0], frame.shape[1] 34 | c_x, c_y = width // 2, height // 2 35 | 36 | subImgs = [] 37 | subImgs.append([0, c_x, 0, c_y, frame[0:c_x, 0:c_y]]) 38 | subImgs.append([c_x, width, 0, c_y, frame[c_x:width, 0:c_y]]) 39 | subImgs.append([0, c_x, c_y, height, frame[0:c_x, c_y:height]]) 40 | subImgs.append([c_x, width, c_y, height, frame[c_x:width, c_y:height]]) 41 | return subImgs 42 | 43 | def run(self) -> object: 44 | # 获得视频的格式 45 | while self.cam.isOpened(): 46 | ret, frame = self.cam.read() 47 | if ret == False: 48 | break 49 | subImages = self.splitImages(frame, 4) 50 | if False: 51 | for i in range(4): 52 | plt.subplot(2, 2, i+1) 53 | plt.imshow(subImages[i]) 54 | plt.show() 55 | if True: 56 | save_prefix = "/".join([self.video_dir, 57 | "SRC_"+self.video_name.split(".")[0]]) 58 | if not os.path.isdir(save_prefix): 59 | os.makedirs(save_prefix) 60 | 61 | for i in range(4): 62 | filename = "%d_%d_%d_%d_%04d.png" % (subImages[i][0], 63 | subImages[i][1], 64 | subImages[i][2], 65 | subImages[i][3], 66 | self.frameInd) 67 | filepath = "/".join([save_prefix, filename]) 68 | cv2.imwrite(filepath, subImages[i][4]) 69 | print(filepath) 70 | self.frameInd += 1 71 | self.cam.release() 72 | 73 | if __name__ == "__main__": 74 | obj = VideoTracking() 75 | obj.run() 76 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/featuremodel/alexnet.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | import tensorflow.contrib.slim as slim 13 | 14 | from mainmodels.models.ssd.settings import g_SSDConfig 15 | from mainmodels.models.ssd import ssdmodel 16 | 17 | def AlexNet(): 18 | # Image batch tensor and dropout keep prob placeholders 19 | x = tf.placeholder(tf.float32, 20 | [None, g_SSDConfig.IMG_H, g_SSDConfig.IMG_W, 21 | g_SSDConfig.NUM_CHANNELS], name='x') 22 | is_training = tf.placeholder(tf.bool, name='is_training') 23 | 24 | # Classification and localization predictions 25 | preds_conf = [] # conf -> classification b/c confidence loss -> classification loss 26 | preds_loc = [] 27 | 28 | # Use batch normalization for all convolution layers 29 | with slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm, 30 | normalizer_params={'is_training': True}, 31 | weights_regularizer=slim.l2_regularizer( 32 | scale=g_SSDConfig.REG_SCALE)): 33 | net = slim.conv2d(x, 64, [11, 11], 4, padding='VALID', scope='conv1') 34 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool1') 35 | net = slim.conv2d(net, 192, [5, 5], scope='conv2') 36 | 37 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv2') 38 | preds_conf.append(net_conf) 39 | preds_loc.append(net_loc) 40 | 41 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool2') 42 | net = slim.conv2d(net, 384, [3, 3], scope='conv3') 43 | net = slim.conv2d(net, 384, [3, 3], scope='conv4') 44 | net = slim.conv2d(net, 256, [3, 3], scope='conv5') 45 | 46 | # The following layers added for SSD 47 | net = slim.conv2d(net, 1024, [3, 3], scope='conv6') 48 | net = slim.conv2d(net, 1024, [1, 1], scope='conv7') 49 | 50 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv7') 51 | preds_conf.append(net_conf) 52 | preds_loc.append(net_loc) 53 | 54 | net = slim.conv2d(net, 256, [1, 1], scope='conv8') 55 | net = slim.conv2d(net, 512, [3, 3], 2, scope='conv8_2') 56 | 57 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv8_2') 58 | preds_conf.append(net_conf) 59 | preds_loc.append(net_loc) 60 | 61 | net = slim.conv2d(net, 128, [1, 1], scope='conv9') 62 | net = slim.conv2d(net, 256, [3, 3], 2, scope='conv9_2') 63 | 64 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv9_2') 65 | preds_conf.append(net_conf) 66 | preds_loc.append(net_loc) 67 | 68 | # Concatenate all preds together into 1 vector, for both classification and localization predictions 69 | final_pred_conf = tf.concat(preds_conf, 1) 70 | final_pred_loc = tf.concat(preds_loc, 1) 71 | 72 | # Return a dictionary of {tensor_name: tensor_reference} 73 | ret_dict = { 74 | 'x': x, 75 | 'y_pred_conf': final_pred_conf, 76 | 'y_pred_loc': final_pred_loc, 77 | 'is_training': is_training, 78 | } 79 | return ret_dict -------------------------------------------------------------------------------- /mainmodels/models/tradition/multicnn/trainer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/5 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import time 13 | from datetime import datetime 14 | 15 | import tensorflow as tf 16 | from mainmodels.models.tradition.multicnn import model 17 | from mainmodels.models.tradition.config import g_MultiCNNConfig 18 | 19 | absdirpath = os.path.dirname(os.path.abspath(__file__)) 20 | 21 | FLAGS = tf.app.flags.FLAGS 22 | tf.app.flags.DEFINE_string('train_dir', g_MultiCNNConfig.train_log_dir, 23 | """Directory where to write event logs """ 24 | """and checkpoint.""") 25 | tf.app.flags.DEFINE_integer('max_steps', 20000, 26 | """Number of batches to run.""") 27 | tf.app.flags.DEFINE_boolean('log_device_placement', False, 28 | """Whether to log device placement.""") 29 | 30 | def train(): 31 | with tf.Graph().as_default(): 32 | global_step = tf.contrib.framework.get_or_create_global_step() 33 | images, labels = model.distorted_inputs() 34 | 35 | # Build a Graph that computes the logits predictions from the 36 | # inference model. 37 | logits = model.inference(images) 38 | 39 | # Calculate loss. 40 | loss = model.loss(logits, labels) 41 | 42 | # Build a Graph that trains the model with one batch of examples and 43 | # updates the model parameters. 44 | train_op = model.train(loss, global_step) 45 | 46 | class _LoggerHook(tf.train.SessionRunHook): 47 | """Logs loss and runtime.""" 48 | 49 | def begin(self): 50 | self._step = -1 51 | 52 | def before_run(self, run_context): 53 | self._step += 1 54 | self._start_time = time.time() 55 | return tf.train.SessionRunArgs(loss) # Asks for loss value. 56 | 57 | def after_run(self, run_context, run_values): 58 | duration = time.time() - self._start_time 59 | loss_value = run_values.results 60 | if self._step % 50 == 0: 61 | num_examples_per_step = g_MultiCNNConfig.batch_size 62 | examples_per_sec = num_examples_per_step / duration 63 | sec_per_batch = float(duration) 64 | 65 | format_str = ('%s: step %d, loss = %.4f (%.1f examples/sec; %.3f ' 66 | 'sec/batch)') 67 | print(format_str % (datetime.now(), self._step, loss_value, 68 | examples_per_sec, sec_per_batch)) 69 | 70 | with tf.train.MonitoredTrainingSession( 71 | checkpoint_dir=FLAGS.train_dir, 72 | hooks=[tf.train.StopAtStepHook(last_step=FLAGS.max_steps), 73 | tf.train.NanTensorHook(loss), 74 | _LoggerHook()], 75 | config=tf.ConfigProto( 76 | log_device_placement=FLAGS.log_device_placement)) as mon_sess: 77 | while not mon_sess.should_stop(): 78 | mon_sess.run(train_op) 79 | 80 | def main(argv=None): 81 | train() 82 | 83 | if __name__ == '__main__': 84 | tf.app.run() 85 | -------------------------------------------------------------------------------- /mainmodels/models/tradition/cnn/trainer.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | import os 6 | import time 7 | from datetime import datetime 8 | 9 | import tensorflow as tf 10 | 11 | # import __init 12 | from mainmodels.models.tradition.cnn import model 13 | from mainmodels.models.tradition.config import g_CNNConfig 14 | 15 | absdirpath = os.path.dirname(os.path.abspath(__file__)) 16 | 17 | FLAGS = tf.app.flags.FLAGS 18 | tf.app.flags.DEFINE_string('train_dir', g_CNNConfig.train_log_dir, 19 | """Directory where to write event logs """ 20 | """and checkpoint.""") 21 | tf.app.flags.DEFINE_integer('max_steps', 30000, 22 | """Number of batches to run.""") 23 | tf.app.flags.DEFINE_boolean('log_device_placement', False, 24 | """Whether to log device placement.""") 25 | 26 | 27 | def train(): 28 | """Train CIFAR-10 for a number of steps.""" 29 | with tf.Graph().as_default(): 30 | global_step = tf.contrib.framework.get_or_create_global_step() 31 | 32 | # Get images and labels for CIFAR-10. 33 | images, labels = model.distorted_inputs() 34 | 35 | # Build a Graph that computes the logits predictions from the 36 | # inference model. 37 | logits = model.inference(images) 38 | 39 | # Calculate loss. 40 | loss = model.loss(logits, labels) 41 | 42 | # Build a Graph that trains the model with one batch of examples and 43 | # updates the model parameters. 44 | train_op = model.train(loss, global_step) 45 | 46 | class _LoggerHook(tf.train.SessionRunHook): 47 | """Logs loss and runtime.""" 48 | 49 | def begin(self): 50 | self._step = -1 51 | 52 | def before_run(self, run_context): 53 | self._step += 1 54 | self._start_time = time.time() 55 | return tf.train.SessionRunArgs(loss) # Asks for loss value. 56 | 57 | def after_run(self, run_context, run_values): 58 | duration = time.time() - self._start_time 59 | loss_value = run_values.results 60 | if self._step % 50 == 0: 61 | num_examples_per_step = g_CNNConfig.batch_size 62 | examples_per_sec = num_examples_per_step / duration 63 | sec_per_batch = float(duration) 64 | 65 | format_str = ( 66 | '%s: step %d, loss = %.4f (%.1f examples/sec; %.3f ' 67 | 'sec/batch)') 68 | print(format_str % (datetime.now(), self._step, loss_value, 69 | examples_per_sec, sec_per_batch)) 70 | 71 | with tf.train.MonitoredTrainingSession( 72 | checkpoint_dir=FLAGS.train_dir, 73 | hooks=[tf.train.StopAtStepHook(last_step=FLAGS.max_steps), 74 | tf.train.NanTensorHook(loss), 75 | _LoggerHook()], 76 | config=tf.ConfigProto( 77 | log_device_placement=FLAGS.log_device_placement)) as mon_sess: 78 | while not mon_sess.should_stop(): 79 | mon_sess.run(train_op) 80 | 81 | 82 | def main(argv=None): 83 | train() 84 | 85 | if __name__ == '__main__': 86 | tf.app.run() 87 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/featuremodel/resalexnet.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | import tensorflow.contrib.slim as slim 13 | 14 | from mainmodels.models.ssd.settings import g_SSDConfig 15 | from mainmodels.models.ssd import ssdmodel 16 | 17 | def ResAlexNet(): 18 | # Image batch tensor and dropout keep prob placeholders 19 | x = tf.placeholder(tf.float32, 20 | [None, g_SSDConfig.IMG_H, g_SSDConfig.IMG_W, 21 | g_SSDConfig.NUM_CHANNELS], name='x') 22 | is_training = tf.placeholder(tf.bool, name='is_training') 23 | 24 | # Classification and localization predictions 25 | preds_conf = [] # conf -> classification b/c confidence loss -> classification loss 26 | preds_loc = [] 27 | 28 | # Use batch normalization for all convolution layers 29 | with slim.arg_scope([slim.conv2d], 30 | normalizer_fn=slim.batch_norm, 31 | normalizer_params={'is_training': True}, 32 | weights_regularizer=slim.l2_regularizer(scale=g_SSDConfig.REG_SCALE)): 33 | net = slim.conv2d(x, 64, [11, 11], 4, padding='SAME', scope='conv1') 34 | pool1 = slim.max_pool2d(net, [3, 3], 2, padding='SAME', scope='pool1') 35 | net = slim.conv2d(net, 192, [5, 5], 2, scope='conv2') 36 | pool2 = slim.max_pool2d(net, [3, 3], 2, padding='SAME', scope='pool2') 37 | 38 | normalization_factor = tf.sqrt(tf.reduce_mean(tf.square(net))) 39 | l2_pool1 = pool1 / ( 40 | tf.sqrt(tf.reduce_mean( 41 | tf.square(pool1))) / normalization_factor) 42 | l2_pool2 = pool2 / ( 43 | tf.sqrt(tf.reduce_mean( 44 | tf.square(pool2))) / normalization_factor) 45 | 46 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv2') 47 | preds_conf.append(net_conf) 48 | preds_loc.append(net_loc) 49 | 50 | net = tf.concat([l2_pool1, net], 3) 51 | net = slim.conv2d(net, 384, [3, 3], 2, scope='conv3') 52 | net = tf.concat([l2_pool2, net], 3) 53 | net = slim.conv2d(net, 512, [3, 3], scope='conv4') 54 | net = slim.conv2d(net, 1024, [1, 1], scope='conv5') 55 | 56 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv5') 57 | preds_conf.append(net_conf) 58 | preds_loc.append(net_loc) 59 | 60 | net = slim.conv2d(net, 512, [3, 3], 2, scope='conv6') 61 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv6') 62 | preds_conf.append(net_conf) 63 | preds_loc.append(net_loc) 64 | 65 | net = slim.conv2d(net, 256, [3, 3], 2, scope='conv7') 66 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv7') 67 | preds_conf.append(net_conf) 68 | preds_loc.append(net_loc) 69 | 70 | # Concatenate all preds together into 1 vector, for both classification and localization predictions 71 | final_pred_conf = tf.concat(preds_conf, 1) 72 | final_pred_loc = tf.concat(preds_loc, 1) 73 | 74 | # Return a dictionary of {tensor_name: tensor_reference} 75 | ret_dict = { 76 | 'x': x, 77 | 'y_pred_conf': final_pred_conf, 78 | 'y_pred_loc': final_pred_loc, 79 | 'is_training': is_training, 80 | } 81 | return ret_dict 82 | 83 | 84 | if __name__ == '__main__': 85 | ResAlexNet() -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/data_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function 9 | 10 | import os 11 | 12 | import tensorflow as tf 13 | from mainmodels.models.autoencoder.builders import build_batch, scale_image 14 | 15 | from mainmodels.models.autoencoder.utils import InputType 16 | 17 | filepath = "../../output/0001_252004_32_32.bin" 18 | 19 | if not os.path.exists(filepath): 20 | raise IOError(filepath + " not found !") 21 | 22 | class WordData(): 23 | 24 | def __init__(self): 25 | self._name = "WordData" 26 | self._num_examples_per_epoch_for_train = 180780 27 | self._num_examples_per_epoch_for_eval = 45196 28 | self._num_examples_per_epoch_for_test = 45196 29 | self._num_classes = 10 30 | 31 | def num_examples(self, input_type): 32 | """Returns the number of examples per the specified input_type 33 | 34 | Args: 35 | input_type: InputType enum 36 | """ 37 | InputType.check(input_type) 38 | 39 | if input_type == InputType.train: 40 | return self._num_examples_per_epoch_for_train 41 | elif input_type == InputType.test: 42 | return self._num_examples_per_epoch_for_test 43 | return self._num_examples_per_epoch_for_eval 44 | 45 | def inputs(self, input_type, batch_size): 46 | # InputType.check(input_type=input_type) 47 | if input_type == InputType.train: 48 | num_examples_per_epoch = self._num_examples_per_epoch_for_train 49 | filenames = [filepath] 50 | elif input_type == InputType.test: 51 | num_examples_per_epoch = self._num_examples_per_epoch_for_test 52 | filenames = [filepath] 53 | else: 54 | num_examples_per_epoch = self._num_examples_per_epoch_for_eval 55 | filenames = [filepath] 56 | 57 | with tf.variable_scope("{}_input".format(input_type)): 58 | filename_queue = tf.train.string_input_producer(filenames) 59 | read_input = self._read(filename_queue) 60 | min_fraction_of_examples_in_queue = 0.4 61 | min_queue_examples = int(num_examples_per_epoch * 62 | min_fraction_of_examples_in_queue) 63 | 64 | return build_batch(image=read_input["feature"], 65 | label=read_input["feature"], 66 | min_queue_examples=min_queue_examples, 67 | batch_size=batch_size, 68 | shuffle=(input_type == InputType.train)) 69 | 70 | def _read(self, filename_queue): 71 | result = { 72 | "label_len": None, 73 | "label": None, 74 | "feature_len": None, 75 | "feature": None 76 | } 77 | image_depth = 1 78 | image_bytes = 32*32 79 | reader = tf.FixedLengthRecordReader(record_bytes=image_bytes) 80 | key, value = reader.read(filename_queue) 81 | record_byes = tf.decode_raw(value, tf.uint8) 82 | image = tf.reshape( 83 | tf.slice(record_byes, [0], [image_bytes]), 84 | [image_depth, 32, 32] 85 | ) 86 | result["feature"] = tf.cast( 87 | tf.transpose(image, [1, 2, 0]), 88 | tf.float32 89 | ) 90 | image = tf.divide(result["feature"], 255.0) 91 | result["feature"] = scale_image(image) 92 | return result -------------------------------------------------------------------------------- /mainmodels/models/ssd/featuremodel/nwpunet.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/7/8 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | import tensorflow.contrib.slim as slim 13 | 14 | from mainmodels.models.ssd.settings import g_SSDConfig 15 | from mainmodels.models.ssd import ssdmodel 16 | 17 | def NWPUNet(): 18 | # Image batch tensor and dropout keep prob placeholders 19 | x = tf.placeholder(tf.float32, 20 | [None, g_SSDConfig.IMG_H, g_SSDConfig.IMG_W, 21 | g_SSDConfig.NUM_CHANNELS], name='x') 22 | is_training = tf.placeholder(tf.bool, name='is_training') 23 | 24 | # Classification and localization predictions 25 | preds_conf = [] # conf -> classification b/c confidence loss -> classification loss 26 | preds_loc = [] 27 | 28 | # Use batch normalization for all convolution layers 29 | with slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm, 30 | normalizer_params={'is_training': True}, 31 | weights_regularizer=slim.l2_regularizer( 32 | scale=g_SSDConfig.REG_SCALE)): 33 | net = slim.conv2d(x, 64, [3, 3], 1, padding='SAME', scope='conv1_1') 34 | net = slim.conv2d(net, 64, [3, 3], 1, padding='SAME', scope='conv1_2') 35 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') 36 | 37 | net = slim.conv2d(net, 128, [3, 3], 2, scope='conv2_1') 38 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') 39 | net = slim.conv2d(net, 128, [2, 2], 1, scope='conv2_2') 40 | 41 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv2_2') 42 | preds_conf.append(net_conf) 43 | preds_loc.append(net_loc) 44 | 45 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') 46 | net = slim.conv2d(net, 128, [3, 3], scope='conv3') 47 | net = slim.conv2d(net, 128, [3, 3], scope='conv4') 48 | net = slim.conv2d(net, 128, [3, 3], scope='conv5') 49 | 50 | # The following layers added for SSD 51 | net = slim.conv2d(net, 256, [3, 3], scope='conv6') 52 | net = slim.conv2d(net, 256, [1, 1], scope='conv7') 53 | 54 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv7') 55 | preds_conf.append(net_conf) 56 | preds_loc.append(net_loc) 57 | 58 | net = slim.conv2d(net, 128, [1, 1], scope='conv8_1') 59 | net = slim.conv2d(net, 256, [3, 3], 2, scope='conv8_2') 60 | 61 | net_conf, net_loc = ssdmodel.SSDHook(net, 'conv8_2') 62 | preds_conf.append(net_conf) 63 | preds_loc.append(net_loc) 64 | 65 | # net = slim.conv2d(net, 128, [1, 1], scope='conv9') 66 | # net = slim.conv2d(net, 256, [3, 3], 2, scope='conv9_2') 67 | # 68 | # net_conf, net_loc = ssdmodel.SSDHook(net, 'conv9_2') 69 | # preds_conf.append(net_conf) 70 | # preds_loc.append(net_loc) 71 | 72 | # Concatenate all preds together into 1 vector, for both classification and localization predictions 73 | final_pred_conf = tf.concat(preds_conf, 1) 74 | final_pred_loc = tf.concat(preds_loc, 1) 75 | 76 | # Return a dictionary of {tensor_name: tensor_reference} 77 | ret_dict = { 78 | 'x': x, 79 | 'y_pred_conf': final_pred_conf, 80 | 'y_pred_loc': final_pred_loc, 81 | 'is_training': is_training, 82 | } 83 | return ret_dict 84 | 85 | if __name__ == '__main__': 86 | NWPUNet() -------------------------------------------------------------------------------- /mainmodels/models/autoencoder/trainer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/5/22 6 | 7 | from __future__ import absolute_import 8 | from __future__ import print_function 9 | 10 | import time 11 | 12 | import tensorflow as tf 13 | from mainmodels.models.autoencoder.data_input import WordData 14 | from mainmodels.models.autoencoder.builders import build_optimizer 15 | from mainmodels.models.autoencoder.builders import build_train_savers 16 | from mainmodels.models.autoencoder.model import SingleLayerAE 17 | from mainmodels.models.autoencoder.utils import InputType 18 | 19 | 20 | class WordAETrainer(): 21 | 22 | def __init__(self, model): 23 | self._model = model 24 | 25 | def train(self, dataset, args): 26 | with tf.Graph().as_default(): 27 | global_step = tf.Variable(0, trainable=False, name='global_step') 28 | 29 | with tf.device("/cpu:0"): 30 | print(type(InputType.train)) 31 | images, labels = dataset.inputs( 32 | input_type=InputType.train, 33 | batch_size=args["batch_size"] 34 | ) 35 | with tf.device("/cpu:0"): 36 | is_training_, reconstructions = self._model.get( 37 | images=images, 38 | train_phase=True, 39 | l2_penalty=args["l2"] 40 | ) 41 | 42 | loss = self._model.loss(reconstructions, labels) 43 | 44 | optimizer = build_optimizer(args=args, 45 | global_step=global_step) 46 | train_op = optimizer.minimize( 47 | loss, 48 | global_step=global_step, 49 | ) 50 | 51 | init = [ 52 | tf.variables_initializer(tf.global_variables() + tf.local_variables()), 53 | tf.tables_initializer() 54 | ] 55 | 56 | with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: 57 | sess.run(init) 58 | 59 | coord = tf.train.Coordinator() 60 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 61 | 62 | # Create the Savers 63 | train_saver, best_saver = build_train_savers() 64 | old_gs = sess.run([global_step]) 65 | 66 | # Restart from where we were 67 | for step in range(old_gs, args["epochs"]+1): 68 | start_time = time.time() 69 | _, loss_value = sess.run( 70 | [train_op, loss], feed_dict={is_training_: True} 71 | ) 72 | duration = time.time() - start_time 73 | print(duration, loss_value) 74 | 75 | coord.request_stop() 76 | coord.join(threads) 77 | 78 | if __name__ == '__main__': 79 | args = { 80 | "epochs": 10, 81 | "batch_size": 32, 82 | "l2": 1e-5, 83 | "lr": 1E-3, 84 | "decay_steps": 100000, 85 | "decay_rate": 0.96, 86 | "optimizer": tf.train.AdamOptimizer, 87 | "paths": { 88 | "basedir": "/Users/liuguiyang/Documents/CodeProj/PyProj/TinyObjectDetection/trainmodel/log/autoencoder/", 89 | "best": "best/", 90 | "train": "train/", 91 | "valid": "valid/" 92 | } 93 | } 94 | slae = SingleLayerAE() 95 | obj = WordAETrainer(slae) 96 | dataset = WordData() 97 | obj.train(dataset=dataset, args=args) -------------------------------------------------------------------------------- /mainmodels/dataset/JLVideo/吉林一号测试集评价.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/9 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import cv2 13 | 14 | from mainmodels.dataset import tools 15 | 16 | score_csuvideo_path = "/Volumes/projects/repos/RSI/CSUVOCFormat/data/CSUVIDEO" \ 17 | "/results/LDSSD_512x512_score/Main/comp4_det_test_plane.txt" 18 | test_img_csuvideo_dir = "/Volumes/projects/repos/RSI/CSUVOCFormat/512x512" \ 19 | "/large_tunisia_total/JPEGImages/" 20 | test_anno_csuvideo_dir = "/Volumes/projects/repos/RSI/CSUVOCFormat/512x512" \ 21 | "/large_tunisia_total/Annotations/" 22 | 23 | 24 | # 将标准的标签数据转换成和VOC一致的txt数据格式进行对比 25 | def convert_std_annotation(video_name): 26 | data_dir = "/Volumes/projects/repos/RSI/CSUVOCFormat/" 27 | tpl_save_dir = "/Volumes/projects/repos/RSI/CSUVOCFormat/data/CSUVIDEO" \ 28 | "/results/LDSSD_512x512_score/Main/comp4_det_std_{" \ 29 | "}_plane.txt" 30 | tpl_anno_dir = data_dir + "{}x{}/{}/Annotations/" 31 | anno_dir = tpl_anno_dir.format(512, 512, video_name) 32 | std_writer = open(tpl_save_dir.format(video_name), "w") 33 | for xml_name in os.listdir(anno_dir): 34 | abs_anno_path = anno_dir + xml_name 35 | anno_targets = tools.extract_airplane_posinfo(abs_anno_path) 36 | for area in anno_targets: 37 | std_writer.write("{} 1 {} {} {} {}\n".format( 38 | ".".join(xml_name.split(".")[:-1]), 39 | area[0], area[1], area[2], area[3])) 40 | std_writer.close() 41 | 42 | 43 | def show_res(): 44 | with open(score_csuvideo_path, "r") as score_reader: 45 | imgs_info = dict() 46 | for line in score_reader: 47 | line = line.strip().split(" ") 48 | img_name, prob, xmin, ymin, xmax, ymax = line[:] 49 | if img_name not in imgs_info: 50 | imgs_info[img_name] = list() 51 | imgs_info[img_name].append([float(prob), int(xmin), int(ymin), 52 | int(xmax), int(ymax)]) 53 | print(len(imgs_info)) 54 | for img_name, target_annos in imgs_info.items(): 55 | abs_img_path = test_img_csuvideo_dir + img_name + ".jpg" 56 | print(abs_img_path) 57 | if not os.path.exists(abs_img_path): 58 | print(abs_img_path) 59 | continue 60 | 61 | abs_anno_path = test_anno_csuvideo_dir + img_name + ".xml" 62 | gt_list = tools.extract_airplane_posinfo(abs_anno_path) 63 | src_img = cv2.imread(abs_img_path) 64 | w, h = src_img.shape[:2] 65 | if w != 512 or h != 512: 66 | continue 67 | for prob_area in target_annos: 68 | prob = prob_area[0] 69 | # print(prob) 70 | if prob < 0.40: 71 | continue 72 | area = prob_area[1:] 73 | cv2.rectangle(src_img, (area[0], area[1]), (area[2], area[3]), 74 | (0, 255, 0), 2) 75 | for area in gt_list: 76 | cv2.rectangle(src_img, (area[0], area[1]), (area[2], area[3]), 77 | (0, 0, 255), 2) 78 | cv2.imshow("src_img", src_img) 79 | cv2.waitKey() 80 | 81 | 82 | # convert_std_annotation("large_000013363_total") 83 | # convert_std_annotation("large_000014631_total") 84 | # convert_std_annotation("large_minneapolis_1_total") 85 | # convert_std_annotation("large_tunisia_total") 86 | show_res() 87 | -------------------------------------------------------------------------------- /source/tracking/lkflow.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | import cv2 5 | 6 | class VideoTracking(): 7 | def __init__(self): 8 | #video_dir = "/Volumes/projects/TrainData/Stanford_Drone_Dataset/stanford_campus_dataset/videos/bookstore/video0/" 9 | self.video_dir = "/Users/liuguiyang/Documents/TrackingDataSet/Iris/" 10 | self.cap, self.prev, self.cur = None, None, None 11 | self.isFirst = True 12 | self.track_len = 10 13 | self.detect_interval = 5 14 | self.tracks = [] 15 | self.cam = cv2.VideoCapture(self.video_dir + "Iris_Dubai_Crop.avi") 16 | self.frame_idx = 0 17 | self.lk_params = dict(winSize=(7, 7), 18 | maxLevel=2, 19 | criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 20 | self.feature_params = dict(maxCorners=500, 21 | qualityLevel=0.3, 22 | minDistance=5, 23 | blockSize=5) 24 | 25 | def run(self) -> object: 26 | # 获得视频的格式 27 | while self.cam.isOpened(): 28 | ret, frame = self.cam.read() 29 | if ret == False: 30 | break 31 | width, height = frame.shape[0], frame.shape[1] 32 | if self.isFirst: 33 | self.prev = cv2.cvtColor(frame[0:width, 0:height], cv2.COLOR_RGB2GRAY) 34 | self.isFirst = False 35 | continue 36 | 37 | cur_rgb = frame[0:width, 0:height] 38 | self.cur = cv2.cvtColor(cur_rgb, cv2.COLOR_RGB2GRAY) 39 | 40 | if len(self.tracks) > 0: # 检测到角点后进行光流跟踪 41 | img0, img1 = self.prev, self.cur 42 | p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2) 43 | p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, 44 | **self.lk_params) # 前一帧的角点和当前帧的图像作为输入来得到角点在当前帧的位置 45 | p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, 46 | **self.lk_params) # 当前帧跟踪到的角点及图像和前一帧的图像作为输入来找到前一帧的角点位置 47 | d = abs(p0 - p0r).reshape(-1, 2).max(-1) # 得到角点回溯与前一帧实际角点的位置变化关系 48 | good = d < 1 # 判断d内的值是否小于1,大于1跟踪被认为是错误的跟踪点 49 | new_tracks = [] 50 | for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): # 将跟踪正确的点列入成功跟踪点 51 | if not good_flag: 52 | continue 53 | tr.append((x, y)) 54 | if len(tr) > self.track_len: 55 | del tr[0] 56 | new_tracks.append(tr) 57 | cv2.circle(cur_rgb, (x, y), 2, (0, 255, 0), -1) 58 | self.tracks = new_tracks 59 | cv2.polylines(cur_rgb, [np.int32(tr) for tr in self.tracks], False, 60 | (0, 255, 0)) # 以上一振角点为初始点,当前帧跟踪到的点为终点划线 61 | 62 | if self.frame_idx % self.detect_interval == 0: # 每5帧检测一次特征点 63 | mask = np.zeros_like(self.cur) # 初始化和视频大小相同的图像 64 | mask[:] = 255 # 将mask赋值255也就是算全部图像的角点 65 | for x, y in [np.int32(tr[-1]) for tr in self.tracks]: # 跟踪的角点画圆 66 | cv2.circle(mask, (x, y), 5, 0, -1) 67 | p = cv2.goodFeaturesToTrack(self.cur, mask=mask, **self.feature_params) # 像素级别角点检测 68 | if p is not None: 69 | for x, y in np.float32(p).reshape(-1, 2): 70 | self.tracks.append([(x, y)]) # 将检测到的角点放在待跟踪序列中 71 | 72 | self.frame_idx += 1 73 | self.prev = self.cur 74 | cv2.imshow('lk_track', cur_rgb) 75 | ch = cv2.waitKey() 76 | if ch == ord('q'): 77 | break 78 | 79 | self.cam.release() 80 | cv2.destroyAllWindows() 81 | 82 | if __name__ == "__main__": 83 | obj = VideoTracking() 84 | obj.run() 85 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/tools/NMS.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import json 13 | 14 | import numpy as np 15 | 16 | from mainmodels.models.ssd.settings import g_SSDConfig 17 | from mainmodels.models.ssd.tools.data_prep import calc_iou 18 | 19 | IMG_W, IMG_H = g_SSDConfig.IMG_W, g_SSDConfig.IMG_H 20 | 21 | def check_box_legal(box_coords, src_rect=[IMG_W, IMG_H, IMG_W, IMG_H]): 22 | for i in range(0, len(box_coords)): 23 | if box_coords[i] < 0: 24 | return False 25 | if box_coords[i] > src_rect[i]: 26 | return False 27 | return True 28 | 29 | def nms(y_pred_conf, y_pred_loc, prob): 30 | print("len of prob is ", len(prob)) 31 | print("max of prob is ", np.max(prob), "min of prob is ", np.min(prob)) 32 | print("mean of prob is ", np.mean(prob)) 33 | class_boxes = {} 34 | with open(g_SSDConfig.label_sign_path, "r") as handle: 35 | traffic_label_dict = json.load(handle) 36 | for key, val in traffic_label_dict.items(): 37 | class_boxes[val] = list() 38 | 39 | class_boxes[0] = list() 40 | y_idx = 0 41 | for fm_size in g_SSDConfig.FM_SIZES: 42 | fm_h, fm_w = fm_size # feature map height and width 43 | for row in range(fm_h): 44 | for col in range(fm_w): 45 | for db in g_SSDConfig.DEFAULT_BOXES: 46 | if prob[y_idx] > g_SSDConfig.CONF_THRESH and y_pred_conf[y_idx] > 0.: 47 | xc, yc = row+0.5, col+0.5 48 | center_coords = np.array([xc, yc, xc, yc]) 49 | # predictions are offsets to center of fm cell 50 | abs_box_coords = center_coords + y_pred_loc[y_idx * 4: y_idx * 4 + 4] 51 | # Calculate predicted box coordinates in actual image 52 | scale = np.array( 53 | [IMG_W / fm_w, IMG_H / fm_h, IMG_W / fm_w, 54 | IMG_H / fm_h]) 55 | box_coords = abs_box_coords * scale 56 | box_coords = [int(round(x)) for x in box_coords] 57 | if check_box_legal(box_coords): 58 | cls = y_pred_conf[y_idx] 59 | cls_prob = prob[y_idx] 60 | box = (*box_coords, cls, cls_prob) 61 | if len(class_boxes[cls]) == 0: 62 | class_boxes[cls].append(box) 63 | else: 64 | suppressed = False # did this box suppress other box(es)? 65 | overlapped = False # did this box overlap with other box(es)? 66 | for other_box in class_boxes[cls]: 67 | iou = calc_iou(box[:4], other_box[:4]) 68 | if iou > g_SSDConfig.NMS_IOU_THRESH: 69 | overlapped = True 70 | # If current box has higher confidence than other box 71 | if box[5] > other_box[5]: 72 | class_boxes[cls].remove(other_box) 73 | suppressed = True 74 | if suppressed or not overlapped: 75 | class_boxes[cls].append(box) 76 | 77 | y_idx += 1 78 | print("y_idx is ", y_idx) 79 | # Gather all the pruned boxes and return them 80 | boxes = [] 81 | for cls in class_boxes.keys(): 82 | for class_box in class_boxes[cls]: 83 | boxes.append(class_box) 84 | boxes = np.array(boxes) 85 | 86 | return boxes 87 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/tools/NMS_vis.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import json 13 | 14 | import numpy as np 15 | 16 | from mainmodels.models.ssd.settings import g_SSDConfig 17 | from mainmodels.models.ssd.tools.data_prep import calc_iou 18 | 19 | IMG_W, IMG_H = g_SSDConfig.IMG_W, g_SSDConfig.IMG_H 20 | 21 | def check_box_legal(box_coords, src_rect=[IMG_W, IMG_H, IMG_W, IMG_H]): 22 | for i in range(0, len(box_coords)): 23 | if box_coords[i] < 0: 24 | return False 25 | if box_coords[i] > src_rect[i]: 26 | return False 27 | return True 28 | 29 | def nms_vis(y_pred_conf, y_pred_loc, prob): 30 | print("len of prob is ", len(prob)) 31 | print("max of prob is ", np.max(prob), "min of prob is ", np.min(prob)) 32 | print("mean of prob is ", np.mean(prob)) 33 | class_boxes = {} 34 | with open(g_SSDConfig.label_sign_path, "r") as handle: 35 | traffic_label_dict = json.load(handle) 36 | for key, val in traffic_label_dict.items(): 37 | class_boxes[val] = list() 38 | 39 | class_boxes[0] = list() 40 | y_idx = 0 41 | for fm_size in g_SSDConfig.FM_SIZES: 42 | fm_h, fm_w = fm_size # feature map height and width 43 | for row in range(fm_h): 44 | for col in range(fm_w): 45 | for db in g_SSDConfig.DEFAULT_BOXES: 46 | if prob[y_idx] > g_SSDConfig.CONF_THRESH and y_pred_conf[y_idx] > 0.: 47 | xc, yc = row+0.5, col+0.5 48 | center_coords = np.array([xc, yc, xc, yc]) 49 | # predictions are offsets to center of fm cell 50 | abs_box_coords = center_coords + y_pred_loc[y_idx * 4: y_idx * 4 + 4] 51 | # Calculate predicted box coordinates in actual image 52 | scale = np.array( 53 | [IMG_W / fm_w, IMG_H / fm_h, IMG_W / fm_w, 54 | IMG_H / fm_h]) 55 | box_coords = abs_box_coords * scale 56 | box_coords = [int(round(x)) for x in box_coords] 57 | if check_box_legal(box_coords): 58 | cls = y_pred_conf[y_idx] 59 | cls_prob = prob[y_idx] 60 | box = (*box_coords, cls, cls_prob) 61 | if len(class_boxes[cls]) == 0: 62 | class_boxes[cls].append(box) 63 | else: 64 | suppressed = False # did this box suppress other box(es)? 65 | overlapped = False # did this box overlap with other box(es)? 66 | for other_box in class_boxes[cls]: 67 | iou = calc_iou(box[:4], other_box[:4]) 68 | if iou > g_SSDConfig.NMS_IOU_THRESH: 69 | overlapped = True 70 | # If current box has higher confidence than other box 71 | if box[5] > other_box[5]: 72 | class_boxes[cls].remove(other_box) 73 | suppressed = True 74 | if suppressed or not overlapped: 75 | class_boxes[cls].append(box) 76 | 77 | y_idx += 1 78 | print("y_idx is ", y_idx) 79 | # Gather all the pruned boxes and return them 80 | boxes = [] 81 | for cls in class_boxes.keys(): 82 | for class_box in class_boxes[cls]: 83 | boxes.append(class_box) 84 | boxes = np.array(boxes) 85 | 86 | return boxes 87 | -------------------------------------------------------------------------------- /mainmodels/models/tradition/control/model_cnn_server.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/3 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | """本文件就是将训练完毕的模型提供稳定的服务""" 12 | 13 | import numpy as np 14 | import tensorflow as tf 15 | 16 | from mainmodels.models.tradition.cnn import model as CNNModel 17 | from mainmodels.models.tradition.multicnn import model as MultiCNNModel 18 | from mainmodels.models.tradition.config import g_CNNConfig 19 | from mainmodels.models.tradition.config import g_MultiCNNConfig 20 | 21 | 22 | class ModelServer(object): 23 | 24 | def __init__(self, use_model_flag=0): 25 | 26 | self._use_model_flag = use_model_flag 27 | self._images = None 28 | self._logits = None 29 | self._sess = None 30 | if use_model_flag == 0: 31 | self._train_log_dir = g_CNNConfig.train_log_dir 32 | self._image_height = g_CNNConfig.image_height 33 | self._image_width = g_CNNConfig.image_width 34 | self._image_depth = g_CNNConfig.image_depth 35 | elif use_model_flag == 1: 36 | self._train_log_dir = g_MultiCNNConfig.train_log_dir 37 | self._image_height = g_MultiCNNConfig.image_height 38 | self._image_width = g_MultiCNNConfig.image_width 39 | self._image_depth = g_MultiCNNConfig.image_depth 40 | else: 41 | raise IOError("There is not this model in the system !") 42 | 43 | if not tf.gfile.Exists(self._train_log_dir): 44 | raise IOError(self._train_log_dir + " model ckpt file not found !") 45 | 46 | def build_model(self): 47 | self._images = tf.placeholder(dtype=tf.float32, 48 | shape=(None, self._image_height, 49 | self._image_width, 50 | self._image_depth)) 51 | 52 | if self._use_model_flag == 0: 53 | softmax_linear = CNNModel.inference(self._images) 54 | elif self._use_model_flag == 1: 55 | softmax_linear = MultiCNNModel.inference(self._images) 56 | else: 57 | raise IOError("There is not this model in the system !") 58 | return softmax_linear 59 | 60 | def load_model(self): 61 | with tf.Graph().as_default(): 62 | self._logits = self.build_model() 63 | saver = tf.train.Saver() 64 | 65 | self._sess = tf.Session() 66 | ckpt = tf.train.get_checkpoint_state(self._train_log_dir) 67 | if ckpt and ckpt.model_checkpoint_path: 68 | saver.restore(self._sess, ckpt.model_checkpoint_path) 69 | # for node in tf.get_default_graph().as_graph_def().node: 70 | # if "Assign" in node.name or "restore_all" in node.name or \ 71 | # "report" in node.name or "save" in node.name or \ 72 | # "GradientDescent" in node.name or "gradients" in \ 73 | # node.name: 74 | # continue 75 | # print(node.name) 76 | graph = tf.get_default_graph() 77 | self._logits = graph.get_tensor_by_name( 78 | "softmax_linear/softmax_linear:0") 79 | print(self._logits) 80 | else: 81 | raise IOError("No checkpoint file found !") 82 | 83 | def predict(self, images): 84 | if self._sess is None: 85 | raise IOError("TF Model not loaded !") 86 | # 输入的images是一个[Batch, height, width, channel] Numpy for [0-255] 87 | # np.float32 88 | images = np.divide(images, 255.0) 89 | images = np.subtract(images, 0.5) 90 | images = np.multiply(images, 2) 91 | pred = self._sess.run(self._logits, 92 | feed_dict={self._images: images}) 93 | return pred 94 | -------------------------------------------------------------------------------- /mainmodels/dataset/VOC格式转换.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/9/25 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | # 将其他数据格式的数据转换成VOC标准格式 12 | 13 | import os 14 | import json 15 | from lxml.etree import Element, SubElement, tostring 16 | import cv2 17 | 18 | 19 | def fetch_label(label_map_path): 20 | with open(label_map_path, "r") as handler: 21 | label2id_dict = json.load(handler) 22 | id2label_dict = dict() 23 | for key, val in label2id_dict.items(): 24 | id2label_dict[val] = key 25 | return id2label_dict 26 | 27 | id2label_dict = fetch_label("/home/ai-i-liuguiyang/repos_ssd/Tencent-Tsinghua/sub_traffic_sign.json") 28 | 29 | 30 | def write_xml_format(abs_img_path, abs_anno_path, save_dir, img_format="jpg"): 31 | src_img_data = cv2.imread(abs_img_path) 32 | img_height, img_width, img_channle = src_img_data.shape 33 | 34 | node_root = Element('annotation') 35 | node_folder = SubElement(node_root, 'folder') 36 | node_folder.text = 'TT100K' 37 | node_filename = SubElement(node_root, 'filename') 38 | if abs_img_path.split(".")[-1] != img_format: 39 | abs_img_path = ".".join(abs_img_path.split(".")[:-1] + [img_format]) 40 | node_filename.text = abs_img_path.split("/")[-1] 41 | 42 | node_size = SubElement(node_root, 'size') 43 | node_width = SubElement(node_size, 'width') 44 | node_width.text = str(img_width) 45 | node_height = SubElement(node_size, 'height') 46 | node_height.text = str(img_height) 47 | node_depth = SubElement(node_size, 'depth') 48 | node_depth.text = str(img_channle) 49 | 50 | with open(abs_anno_path, "r") as handler: 51 | anno_list = json.load(handler) 52 | for anno_target in anno_list: 53 | node_object = SubElement(node_root, 'object') 54 | node_name = SubElement(node_object, 'name') 55 | node_name.text = id2label_dict[anno_target[-1]] 56 | node_difficult = SubElement(node_object, 'difficult') 57 | node_difficult.text = '0' 58 | node_bndbox = SubElement(node_object, 'bndbox') 59 | node_xmin = SubElement(node_bndbox, 'xmin') 60 | node_xmin.text = str(anno_target[0]) 61 | node_ymin = SubElement(node_bndbox, 'ymin') 62 | node_ymin.text = str(anno_target[1]) 63 | node_xmax = SubElement(node_bndbox, 'xmax') 64 | node_xmax.text = str(anno_target[2]) 65 | node_ymax = SubElement(node_bndbox, 'ymax') 66 | node_ymax.text = str(anno_target[3]) 67 | xml_obj = tostring(node_root, pretty_print=True) 68 | xml_obj = xml_obj.decode("utf8") 69 | tmp = abs_anno_path.split("/")[-1] 70 | tmp = ".".join(tmp.split(".")[:-1] + ["xml"]) 71 | print(save_dir + "Annotations/" + tmp) 72 | with open(save_dir + "Annotations/" + tmp, "w") as handler: 73 | handler.write(xml_obj) 74 | tmp = abs_img_path.split("/")[:-1] 75 | cv2.imwrite(save_dir + "JPEGImages/" + tmp, src_img_data) 76 | 77 | 78 | def convert2xml(img_postfix="png", anno_postfix="json"): 79 | img_dir = "/home/ai-i-liuguiyang/repos_ssd/Tencent-Tsinghua/VOCFORMAT/JPEGImages/" 80 | annotation_label_dir = "/home/ai-i-liuguiyang/repos_ssd/Tencent-Tsinghua/VOCFORMAT/Annotations/" 81 | save_dir = "/home/ai-i-liuguiyang/repos_ssd/Tencent-Tsinghua/standard_data/TT100K/" 82 | annotation_lists = os.listdir(annotation_label_dir) 83 | for anno_name in annotation_lists: 84 | if anno_name.startswith("._") or anno_name.startswith("."): 85 | continue 86 | if not anno_name.endswith(anno_postfix): 87 | continue 88 | img_name = ".".join(anno_name.split(".")[:-1] + [img_postfix]) 89 | img_path = img_dir + img_name 90 | if not os.path.exists(img_path): 91 | continue 92 | write_xml_format(img_path, annotation_label_dir + anno_name, save_dir) 93 | break 94 | 95 | 96 | if __name__ == '__main__': 97 | convert2xml() -------------------------------------------------------------------------------- /mainmodels/models/resnet/trainer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import tensorflow as tf 12 | 13 | from mainmodels.dataset import cifar_input 14 | from mainmodels.models.resnet import model as resnet_model 15 | from mainmodels.models.resnet.settings import g_ResNetConfig 16 | 17 | 18 | def train(hps): 19 | images, labels = cifar_input.build_input( 20 | g_ResNetConfig.dataset, g_ResNetConfig.train_data_path, 21 | hps.batch_size, g_ResNetConfig.mode) 22 | model = resnet_model.ResNet(hps, images, labels, g_ResNetConfig.mode) 23 | model.build_graph() 24 | 25 | truth = tf.argmax(model.labels, axis=1) 26 | predictions = tf.argmax(model.predictions, axis=1) 27 | precision = tf.reduce_mean(tf.to_float(tf.equal(predictions, truth))) 28 | 29 | summary_hook = tf.train.SummarySaverHook( 30 | save_steps=100, 31 | output_dir=g_ResNetConfig.train_dir, 32 | summary_op=tf.summary.merge([model.summaries, 33 | tf.summary.scalar('Precision', precision)])) 34 | 35 | logging_hook = tf.train.LoggingTensorHook( 36 | tensors={'step': model.global_step, 37 | 'loss': model.cost, 38 | 'precision': precision}, 39 | every_n_iter=10) 40 | 41 | class _LearningRateSetterHook(tf.train.SessionRunHook): 42 | """Sets learning_rate based on global step.""" 43 | 44 | def begin(self): 45 | self._lrn_rate = 0.1 46 | 47 | def before_run(self, run_context): 48 | return tf.train.SessionRunArgs( 49 | model.global_step, # Asks for global step value. 50 | feed_dict={model.lrn_rate: self._lrn_rate}) # Sets learning rate 51 | 52 | def after_run(self, run_context, run_values): 53 | train_step = run_values.results 54 | if train_step < 40000: 55 | self._lrn_rate = 0.1 56 | elif train_step < 60000: 57 | self._lrn_rate = 0.01 58 | elif train_step < 80000: 59 | self._lrn_rate = 0.001 60 | else: 61 | self._lrn_rate = 0.0001 62 | 63 | with tf.train.MonitoredTrainingSession( 64 | checkpoint_dir=g_ResNetConfig.log_root, 65 | hooks=[logging_hook, _LearningRateSetterHook()], 66 | chief_only_hooks=[summary_hook], 67 | # Since we provide a SummarySaverHook, we need to disable default 68 | # SummarySaverHook. To do that we set save_summaries_steps to 0. 69 | save_summaries_steps=0, 70 | config=tf.ConfigProto(allow_soft_placement=True)) as mon_sess: 71 | while not mon_sess.should_stop(): 72 | mon_sess.run(model.train_op) 73 | 74 | def main(_): 75 | if g_ResNetConfig.num_gpus == 0: 76 | dev = '/cpu:0' 77 | elif g_ResNetConfig.num_gpus == 1: 78 | dev = '/gpu:0' 79 | else: 80 | raise ValueError('Only support 0 or 1 gpu.') 81 | 82 | if g_ResNetConfig.mode == 'train': 83 | batch_size = 32 84 | elif g_ResNetConfig.mode == 'eval': 85 | batch_size = 100 86 | 87 | if g_ResNetConfig.dataset == 'cifar10': 88 | num_classes = 10 89 | elif g_ResNetConfig.dataset == 'cifar100': 90 | num_classes = 100 91 | 92 | hps = resnet_model.HParams(batch_size=batch_size, 93 | num_classes=num_classes, 94 | min_lrn_rate=0.0001, 95 | lrn_rate=0.1, 96 | num_residual_units=2, 97 | use_bottleneck=False, 98 | weight_decay_rate=0.0002, 99 | relu_leakiness=0.1, 100 | optimizer='mom') 101 | 102 | with tf.device(dev): 103 | if g_ResNetConfig.mode == 'train': 104 | train(hps) 105 | 106 | 107 | if __name__ == '__main__': 108 | tf.logging.set_verbosity(tf.logging.DEBUG) 109 | tf.app.run() -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/lsd_ssd_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/15 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import random 13 | import cv2 14 | 15 | from mainmodels.dataset.LSD10 import tools 16 | from mainmodels.dataset.LSD10 import lsd_config as config 17 | 18 | 19 | nwpu_voc_dir = "/Volumes/projects/repos/RSI/NWPUVHR10/NWPUVOCFORMAT/" 20 | nwpu_voc_image_dir = nwpu_voc_dir + "JPEGImages/" 21 | nwpu_voc_anno_dir = nwpu_voc_dir + "Annotations/" 22 | 23 | vedia_voc_dir = "/Volumes/projects/repos/RSI/VEDAI/VEDIAVOCFORAMT/" 24 | vedia_voc_image_dir = vedia_voc_dir + "JPEGImages/" 25 | vedia_voc_anno_dir = vedia_voc_dir + "Annotations/" 26 | 27 | lsd_voc_dir = "/Volumes/projects/repos/RSI/LSD10/" 28 | lsd_voc_image_dir = lsd_voc_dir + "JPEGImages/" 29 | lsd_voc_anno_dir = lsd_voc_dir + "Annotations/" 30 | 31 | # 采用蓄水池采样算法对序列进行采样 32 | def rand_selected_file(file_list, K_ratio=2/7): 33 | K = int(len(file_list) * K_ratio) 34 | res = list() 35 | for i in range(0, len(file_list)): 36 | if i < K: 37 | res.append(file_list[i]) 38 | else: 39 | M = random.randint(0, i) 40 | if M < K: 41 | res[M] = file_list[i] 42 | return res 43 | 44 | # 先确定每个原始数据集中的训练集和测试集 45 | def split_dataset(): 46 | nwpu_img_list = os.listdir(nwpu_voc_image_dir) 47 | vedia_img_list = os.listdir(vedia_voc_image_dir) 48 | test_nwpu_img_list = rand_selected_file(nwpu_img_list) 49 | test_vedia_img_list = rand_selected_file(vedia_img_list) 50 | with open(nwpu_voc_dir+"test.txt", "w") as test_nwpu_writer: 51 | for item in test_nwpu_img_list: 52 | test_nwpu_writer.write("{}\n".format(item)) 53 | with open(nwpu_voc_dir+"train.txt", "w") as train_nwpu_writer: 54 | for item in nwpu_img_list: 55 | if item not in test_nwpu_img_list: 56 | train_nwpu_writer.write("{}\n".format(item)) 57 | with open(vedia_voc_dir+"test.txt", "w") as test_vedia_writer: 58 | for item in test_vedia_img_list: 59 | test_vedia_writer.write("{}\n".format(item)) 60 | with open(vedia_voc_dir+"train.txt", "w") as train_vedia_writer: 61 | for item in vedia_img_list: 62 | if item not in test_vedia_img_list: 63 | train_vedia_writer.write("{}\n".format(item)) 64 | 65 | 66 | # 更新数据集中的label信息 67 | def flush_dataset(): 68 | for anno_name in os.listdir(lsd_voc_anno_dir): 69 | abs_anno_path = lsd_voc_anno_dir + anno_name 70 | print(abs_anno_path) 71 | anno_targets = tools.extract_target_from_xml(abs_anno_path) 72 | new_anno_targets = list() 73 | for anno_info in anno_targets: 74 | label_name = anno_info[-1] 75 | label_id = config.sign_idx_dict[label_name] 76 | label_name = config.idx_sign_dict[label_id] 77 | new_anno_info = anno_info[:-1] + [label_name] 78 | new_anno_targets.append(new_anno_info) 79 | src_image = cv2.imread( 80 | lsd_voc_image_dir+anno_name.replace("xml", "jpg")) 81 | xml_obj = tools.fetch_xml_format( 82 | src_image, anno_name.replace("xml", "jpg"), new_anno_targets) 83 | with open(lsd_voc_anno_dir+anno_name, "w") as writer: 84 | writer.write(xml_obj) 85 | 86 | #1,$s/vehiclecar/vehicle/g 87 | 88 | import numpy as np 89 | def calc_rgb_mean(): 90 | r_list, g_list, b_list = list(), list(), list() 91 | with open("/Volumes/projects/repos/RSI/LSD10/total.txt", "r") as reader: 92 | for line in reader.readlines(): 93 | line = line.strip() 94 | src_img = cv2.imread(line) 95 | b, g, r = cv2.split(src_img) 96 | b_list.append(np.mean(b)) 97 | g_list.append(np.mean(g)) 98 | r_list.append(np.mean(r)) 99 | print(np.mean(r_list)) 100 | print(np.mean(g_list)) 101 | print(np.mean(b_list)) 102 | """ 103 | 104.480289006 104 | 107.307103097 105 | 95.8043901467 106 | """ 107 | 108 | if __name__ == '__main__': 109 | # split_dataset() 110 | # calc_rgb_mean() 111 | flush_dataset() 112 | pass -------------------------------------------------------------------------------- /mainmodels/dataset/TT100K/rdssd_mAP.txt: -------------------------------------------------------------------------------- 1 | 20893 I1206 12:56:12.914737 17389 solver.cpp:540] class1: 0.744612 2 | 20894 I1206 12:56:12.918388 17389 solver.cpp:540] class2: 0.668199 3 | 20895 I1206 12:56:12.922925 17389 solver.cpp:540] class3: 0.706051 4 | 20896 I1206 12:56:12.928609 17389 solver.cpp:540] class4: 0.834819 5 | 20897 I1206 12:56:12.929757 17389 solver.cpp:540] class5: 0.834605 6 | 20898 I1206 12:56:12.931900 17389 solver.cpp:540] class6: 0.774596 7 | 20899 I1206 12:56:12.933171 17389 solver.cpp:540] class7: 0.702296 8 | 20900 I1206 12:56:12.934283 17389 solver.cpp:540] class8: 0.882255 9 | 20901 I1206 12:56:12.941704 17389 solver.cpp:540] class9: 0.573351 10 | 20902 I1206 12:56:12.943495 17389 solver.cpp:540] class10: 0.730888 11 | 20903 I1206 12:56:12.947707 17389 solver.cpp:540] class11: 0.428351 12 | 20904 I1206 12:56:12.950464 17389 solver.cpp:540] class12: 0.470619 13 | 20905 I1206 12:56:12.953248 17389 solver.cpp:540] class13: 0.721793 14 | 20906 I1206 12:56:12.955550 17389 solver.cpp:540] class14: 0.51455 15 | 20907 I1206 12:56:12.957865 17389 solver.cpp:540] class15: 0.832101 16 | 20908 I1206 12:56:12.960610 17389 solver.cpp:540] class16: 0.642705 17 | 20909 I1206 12:56:12.964496 17389 solver.cpp:540] class17: 0.50057 18 | 20910 I1206 12:56:12.973161 17389 solver.cpp:540] class18: 0.544715 19 | 20911 I1206 12:56:12.976976 17389 solver.cpp:540] class19: 0.350183 20 | 20912 I1206 12:56:12.984083 17389 solver.cpp:540] class20: 0.198378 21 | 20913 I1206 12:56:12.985172 17389 solver.cpp:540] class21: 0.543038 22 | 20914 I1206 12:56:12.992384 17389 solver.cpp:540] class22: 0.729431 23 | 20915 I1206 12:56:12.994004 17389 solver.cpp:540] class23: 0.91736 24 | 20916 I1206 12:56:12.996317 17389 solver.cpp:540] class24: 0.825614 25 | 20917 I1206 12:56:12.999795 17389 solver.cpp:540] class25: 0.802736 26 | 20918 I1206 12:56:13.001153 17389 solver.cpp:540] class26: 0.616763 27 | 20919 I1206 12:56:13.001926 17389 solver.cpp:540] class27: 0.566915 28 | 20920 I1206 12:56:13.003231 17389 solver.cpp:540] class28: 0.830133 29 | 20921 I1206 12:56:13.015952 17389 solver.cpp:540] class29: 0.764905 30 | 20922 I1206 12:56:13.023739 17389 solver.cpp:540] class30: 0.472181 31 | 20923 I1206 12:56:13.024466 17389 solver.cpp:540] class31: 0.410196 32 | 20924 I1206 12:56:13.028928 17389 solver.cpp:540] class32: 0.657326 33 | 20925 I1206 12:56:13.030810 17389 solver.cpp:540] class33: 0.82213 34 | 20926 I1206 12:56:13.034413 17389 solver.cpp:540] class34: 0.775802 35 | 20927 I1206 12:56:13.036054 17389 solver.cpp:540] class35: 0.881279 36 | 20928 I1206 12:56:13.036885 17389 solver.cpp:540] class36: 0.700655 37 | 20929 I1206 12:56:13.039355 17389 solver.cpp:540] class37: 0.668637 38 | 20930 I1206 12:56:13.040638 17389 solver.cpp:540] class38: 0.879858 39 | 20931 I1206 12:56:13.041898 17389 solver.cpp:540] class39: 0.842976 40 | 20932 I1206 12:56:13.043666 17389 solver.cpp:540] class40: 0.731516 41 | 20933 I1206 12:56:13.046531 17389 solver.cpp:540] class41: 0.846965 42 | 20934 I1206 12:56:13.048041 17389 solver.cpp:540] class42: 0.850808 43 | 20935 I1206 12:56:13.049077 17389 solver.cpp:540] class43: 0.795867 44 | 20936 I1206 12:56:13.050935 17389 solver.cpp:540] class44: 0.865259 45 | 20937 I1206 12:56:13.052279 17389 solver.cpp:540] class45: 0.870021 46 | 20938 I1206 12:56:13.057042 17389 solver.cpp:540] class46: 0.713011 47 | 20939 I1206 12:56:13.058337 17389 solver.cpp:540] class47: 0.808095 48 | 20940 I1206 12:56:13.059557 17389 solver.cpp:540] class48: 0.774517 49 | 20941 I1206 12:56:13.060524 17389 solver.cpp:540] class49: 0.850753 50 | 20942 I1206 12:56:13.062283 17389 solver.cpp:540] class50: 0.760629 51 | 20943 I1206 12:56:13.066892 17389 solver.cpp:540] class51: 0.77872 52 | 20944 I1206 12:56:13.072407 17389 solver.cpp:540] class52: 0.791629 53 | 20945 I1206 12:56:13.076395 17389 solver.cpp:540] class53: 0.595845 54 | 20946 I1206 12:56:13.078758 17389 solver.cpp:540] class54: 0.723524 55 | 20947 I1206 12:56:13.081642 17389 solver.cpp:540] class55: 0.698423 56 | 20948 I1206 12:56:13.082468 17389 solver.cpp:540] class56: 0.894011 57 | 20949 I1206 12:56:13.084090 17389 solver.cpp:540] class57: 0.812597 58 | 20950 I1206 12:56:13.091696 17389 solver.cpp:540] class58: 0.716771 59 | 20951 I1206 12:56:13.093933 17389 solver.cpp:540] class59: 0.801273 60 | 20952 I1206 12:56:13.094475 17389 solver.cpp:540] class60: 0.864687 61 | 20953 I1206 12:56:13.094485 17389 solver.cpp:546] Test net output #0: detection_eval = 0.715142 -------------------------------------------------------------------------------- /mainmodels/dataset/ssd.score.log: -------------------------------------------------------------------------------- 1 | I1018 07:46:51.310816 536 caffe.cpp:155] Finetuning from models/VGGNet/TT100K/SSD_300x300/VGG_TT100K_SSD_300x300_iter_400000.caffemodel 2 | I1018 07:46:51.603868 536 caffe.cpp:251] Starting Optimization 3 | I1018 07:46:51.603902 536 solver.cpp:294] Solving VGG_TT100K_SSD_300x300_train 4 | I1018 07:46:51.603924 536 solver.cpp:295] Learning Rate Policy: multistep 5 | I1018 07:46:51.810986 536 solver.cpp:332] Iteration 0, loss = 1.99847 6 | I1018 07:46:51.811106 536 solver.cpp:433] Iteration 0, Testing net (#0) 7 | I1018 07:56:12.144702 536 solver.cpp:540] class1: 0.890144 8 | I1018 07:56:12.146682 536 solver.cpp:540] class2: 0.8738 9 | I1018 07:56:12.149351 536 solver.cpp:540] class3: 0.848247 10 | I1018 07:56:12.152518 536 solver.cpp:540] class4: 0.885535 11 | I1018 07:56:12.152899 536 solver.cpp:540] class5: 0.969982 12 | I1018 07:56:12.153772 536 solver.cpp:540] class6: 0.877935 13 | I1018 07:56:12.154384 536 solver.cpp:540] class7: 0.855057 14 | I1018 07:56:12.154742 536 solver.cpp:540] class8: 0.970202 15 | I1018 07:56:12.156975 536 solver.cpp:540] class9: 0.800012 16 | I1018 07:56:12.157838 536 solver.cpp:540] class10: 0.851737 17 | I1018 07:56:12.160275 536 solver.cpp:540] class11: 0.623431 18 | I1018 07:56:12.161499 536 solver.cpp:540] class12: 0.541473 19 | I1018 07:56:12.163192 536 solver.cpp:540] class13: 0.696826 20 | I1018 07:56:12.164328 536 solver.cpp:540] class14: 0.735949 21 | I1018 07:56:12.164801 536 solver.cpp:540] class15: 0.878022 22 | I1018 07:56:12.166062 536 solver.cpp:540] class16: 0.761616 23 | I1018 07:56:12.169116 536 solver.cpp:540] class17: 0.657266 24 | I1018 07:56:12.171540 536 solver.cpp:540] class18: 0.746919 25 | I1018 07:56:12.173094 536 solver.cpp:540] class19: 0.661506 26 | I1018 07:56:12.174386 536 solver.cpp:540] class20: 0.476255 27 | I1018 07:56:12.175020 536 solver.cpp:540] class21: 0.74672 28 | I1018 07:56:12.180550 536 solver.cpp:540] class22: 0.84061 29 | I1018 07:56:12.181102 536 solver.cpp:540] class23: 0.963833 30 | I1018 07:56:12.181850 536 solver.cpp:540] class24: 0.851511 31 | I1018 07:56:12.184188 536 solver.cpp:540] class25: 0.9027 32 | I1018 07:56:12.184834 536 solver.cpp:540] class26: 0.789371 33 | I1018 07:56:12.185058 536 solver.cpp:540] class27: 0.612192 34 | I1018 07:56:12.185997 536 solver.cpp:540] class28: 0.882052 35 | I1018 07:56:12.192613 536 solver.cpp:540] class29: 0.834686 36 | I1018 07:56:12.196696 536 solver.cpp:540] class30: 0.691101 37 | I1018 07:56:12.196943 536 solver.cpp:540] class31: 0.788985 38 | I1018 07:56:12.198205 536 solver.cpp:540] class32: 0.634827 39 | I1018 07:56:12.199414 536 solver.cpp:540] class33: 0.769948 40 | I1018 07:56:12.200563 536 solver.cpp:540] class34: 0.876362 41 | I1018 07:56:12.201292 536 solver.cpp:540] class35: 0.961648 42 | I1018 07:56:12.201548 536 solver.cpp:540] class36: 0.823155 43 | I1018 07:56:12.202535 536 solver.cpp:540] class37: 0.834112 44 | I1018 07:56:12.202913 536 solver.cpp:540] class38: 0.961223 45 | I1018 07:56:12.203526 536 solver.cpp:540] class39: 0.897547 46 | I1018 07:56:12.204175 536 solver.cpp:540] class40: 0.840413 47 | I1018 07:56:12.204860 536 solver.cpp:540] class41: 0.885045 48 | I1018 07:56:12.205315 536 solver.cpp:540] class42: 0.896603 49 | I1018 07:56:12.205574 536 solver.cpp:540] class43: 0.814996 50 | I1018 07:56:12.206746 536 solver.cpp:540] class44: 0.878344 51 | I1018 07:56:12.207309 536 solver.cpp:540] class45: 0.887312 52 | I1018 07:56:12.209983 536 solver.cpp:540] class46: 0.767225 53 | I1018 07:56:12.211320 536 solver.cpp:540] class47: 0.839921 54 | I1018 07:56:12.211887 536 solver.cpp:540] class48: 0.879194 55 | I1018 07:56:12.212354 536 solver.cpp:540] class49: 0.892206 56 | I1018 07:56:12.213207 536 solver.cpp:540] class50: 0.883329 57 | I1018 07:56:12.215230 536 solver.cpp:540] class51: 0.825485 58 | I1018 07:56:12.223831 536 solver.cpp:540] class52: 0.840184 59 | I1018 07:56:12.227646 536 solver.cpp:540] class53: 0.766099 60 | I1018 07:56:12.229473 536 solver.cpp:540] class54: 0.855347 61 | I1018 07:56:12.231015 536 solver.cpp:540] class55: 0.866471 62 | I1018 07:56:12.231374 536 solver.cpp:540] class56: 0.903151 63 | I1018 07:56:12.231724 536 solver.cpp:540] class57: 0.88488 64 | I1018 07:56:12.235144 536 solver.cpp:540] class58: 0.868218 65 | I1018 07:56:12.236022 536 solver.cpp:540] class59: 0.969913 66 | I1018 07:56:12.236500 536 solver.cpp:540] class60: 0.905403 67 | I1018 07:56:12.236554 536 solver.cpp:546] Test net output #0: detection_eval = 0.82357 68 | -------------------------------------------------------------------------------- /mainmodels/dataset/cifar_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | """CIFAR dataset input module. 12 | """ 13 | 14 | import tensorflow as tf 15 | 16 | def build_input(dataset, data_path, batch_size, mode): 17 | """Build CIFAR image and labels. 18 | 19 | Args: 20 | dataset: Either 'cifar10' or 'cifar100'. 21 | data_path: Filename for data. 22 | batch_size: Input batch size. 23 | mode: Either 'train' or 'eval'. 24 | Returns: 25 | images: Batches of images. [batch_size, image_size, image_size, 3] 26 | labels: Batches of labels. [batch_size, num_classes] 27 | Raises: 28 | ValueError: when the specified dataset is not supported. 29 | """ 30 | image_size = 32 31 | if dataset == 'cifar10': 32 | label_bytes = 1 33 | label_offset = 0 34 | num_classes = 10 35 | elif dataset == 'cifar100': 36 | label_bytes = 1 37 | label_offset = 1 38 | num_classes = 100 39 | else: 40 | raise ValueError('Not supported dataset %s', dataset) 41 | 42 | depth = 3 43 | image_bytes = image_size * image_size * depth 44 | record_bytes = label_bytes + label_offset + image_bytes 45 | 46 | data_files = tf.gfile.Glob(data_path) 47 | file_queue = tf.train.string_input_producer(data_files, shuffle=True) 48 | # Read examples from files in the filename queue. 49 | reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) 50 | _, value = reader.read(file_queue) 51 | 52 | # Convert these examples to dense labels and processed images. 53 | record = tf.reshape(tf.decode_raw(value, tf.uint8), [record_bytes]) 54 | label = tf.cast(tf.slice(record, [label_offset], [label_bytes]), tf.int32) 55 | # Convert from string to [depth * height * width] to [depth, height, width]. 56 | depth_major = tf.reshape(tf.slice(record, [label_bytes], [image_bytes]), 57 | [depth, image_size, image_size]) 58 | # Convert from [depth, height, width] to [height, width, depth]. 59 | image = tf.cast(tf.transpose(depth_major, [1, 2, 0]), tf.float32) 60 | 61 | if mode == 'train': 62 | image = tf.image.resize_image_with_crop_or_pad( 63 | image, image_size+4, image_size+4) 64 | image = tf.random_crop(image, [image_size, image_size, 3]) 65 | image = tf.image.random_flip_left_right(image) 66 | # Brightness/saturation/constrast provides small gains .2%~.5% on cifar. 67 | # image = tf.image.random_brightness(image, max_delta=63. / 255.) 68 | # image = tf.image.random_saturation(image, lower=0.5, upper=1.5) 69 | # image = tf.image.random_contrast(image, lower=0.2, upper=1.8) 70 | image = tf.image.per_image_standardization(image) 71 | 72 | example_queue = tf.RandomShuffleQueue( 73 | capacity=16 * batch_size, 74 | min_after_dequeue=8 * batch_size, 75 | dtypes=[tf.float32, tf.int32], 76 | shapes=[[image_size, image_size, depth], [1]]) 77 | num_threads = 16 78 | else: 79 | image = tf.image.resize_image_with_crop_or_pad( 80 | image, image_size, image_size) 81 | image = tf.image.per_image_standardization(image) 82 | 83 | example_queue = tf.FIFOQueue( 84 | 3 * batch_size, 85 | dtypes=[tf.float32, tf.int32], 86 | shapes=[[image_size, image_size, depth], [1]]) 87 | num_threads = 1 88 | 89 | example_enqueue_op = example_queue.enqueue([image, label]) 90 | tf.train.add_queue_runner(tf.train.queue_runner.QueueRunner( 91 | example_queue, [example_enqueue_op] * num_threads)) 92 | 93 | # Read 'batch' labels + images from the example queue. 94 | images, labels = example_queue.dequeue_many(batch_size) 95 | labels = tf.reshape(labels, [batch_size, 1]) 96 | indices = tf.reshape(tf.range(0, batch_size, 1), [batch_size, 1]) 97 | labels = tf.sparse_to_dense( 98 | tf.concat(values=[indices, labels], axis=1), 99 | [batch_size, num_classes], 1.0, 0.0) 100 | 101 | assert len(images.get_shape()) == 4 102 | assert images.get_shape()[0] == batch_size 103 | assert images.get_shape()[-1] == 3 104 | assert len(labels.get_shape()) == 2 105 | assert labels.get_shape()[0] == batch_size 106 | assert labels.get_shape()[1] == num_classes 107 | 108 | # Display the training images in the visualizer. 109 | tf.summary.image('images', images) 110 | return images, labels -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/tools.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import re 13 | import cv2 14 | 15 | import xml.dom.minidom 16 | from lxml.etree import Element, SubElement, tostring 17 | from mainmodels.dataset.NWPUVHR10 import nwpu_config as config 18 | 19 | # 提取图像对应的标注的数据 20 | def fetch_anno_targets_info(abs_anno_path, is_label_text=False): 21 | if not os.path.exists(abs_anno_path): 22 | raise IOError("No Such annotation file !") 23 | with open(abs_anno_path, "r") as anno_reader: 24 | total_annos = list() 25 | for line in anno_reader: 26 | line = line.strip() 27 | sub_anno = re.split("\(|\,|\)", line) 28 | a = [int(item) for item in sub_anno if len(item)] 29 | if len(a) == 5: 30 | if is_label_text: 31 | total_annos.append(a[:4]+[config.idx_sign_dict[a[-1]]]) 32 | else: 33 | total_annos.append(a) 34 | return total_annos 35 | 36 | def fetch_xml_format(src_img_data, f_name, anno_list): 37 | img_height, img_width, img_channle = src_img_data.shape 38 | 39 | node_root = Element('annotation') 40 | node_folder = SubElement(node_root, 'folder') 41 | node_folder.text = 'LSD10' 42 | node_filename = SubElement(node_root, 'filename') 43 | node_filename.text = f_name 44 | 45 | node_size = SubElement(node_root, 'size') 46 | node_width = SubElement(node_size, 'width') 47 | node_width.text = str(img_width) 48 | node_height = SubElement(node_size, 'height') 49 | node_height.text = str(img_height) 50 | node_depth = SubElement(node_size, 'depth') 51 | node_depth.text = str(img_channle) 52 | 53 | for anno_target in anno_list: 54 | node_object = SubElement(node_root, 'object') 55 | node_name = SubElement(node_object, 'name') 56 | node_name.text = anno_target[-1] 57 | node_difficult = SubElement(node_object, 'difficult') 58 | node_difficult.text = '0' 59 | node_bndbox = SubElement(node_object, 'bndbox') 60 | node_xmin = SubElement(node_bndbox, 'xmin') 61 | node_xmin.text = str(1 if anno_target[0]<0 else anno_target[0]) 62 | node_ymin = SubElement(node_bndbox, 'ymin') 63 | node_ymin.text = str(1 if anno_target[1]<0 else anno_target[1]) 64 | node_xmax = SubElement(node_bndbox, 'xmax') 65 | node_xmax.text = str(img_width-1 if anno_target[2]>=img_width else anno_target[2]) 66 | node_ymax = SubElement(node_bndbox, 'ymax') 67 | node_ymax.text = str(img_height-1 if anno_target[3]>=img_height else anno_target[3]) 68 | xml_obj = tostring(node_root, pretty_print=True) 69 | xml_obj = xml_obj.decode("utf8") 70 | return xml_obj 71 | 72 | # 给定一个标记文件,找到对应的目标的位置信息 73 | def extract_target_from_xml(filename): 74 | if not os.path.exists(filename): 75 | raise IOError(filename + " not exists !") 76 | # 使用minidom解析器打开 XML 文档 77 | DOMTree = xml.dom.minidom.parse(filename) 78 | collection = DOMTree.documentElement 79 | # 获取集合中所有的目标 80 | targets = collection.getElementsByTagName("object") 81 | res = [] 82 | for target in targets: 83 | target_name = target.getElementsByTagName('name')[0].childNodes[0].data 84 | bndbox = target.getElementsByTagName("bndbox")[0] 85 | xmin = bndbox.getElementsByTagName("xmin")[0].childNodes[0].data 86 | ymin = bndbox.getElementsByTagName("ymin")[0].childNodes[0].data 87 | xmax = bndbox.getElementsByTagName("xmax")[0].childNodes[0].data 88 | ymax = bndbox.getElementsByTagName("ymax")[0].childNodes[0].data 89 | res.append([int(xmin), int(ymin), int(xmax), int(ymax), target_name]) 90 | return res 91 | 92 | # 原始数据中多目标的显示 93 | def show_targets(img_dir, anno_dir): 94 | for img_name in os.listdir(img_dir): 95 | if img_name.startswith("._"): 96 | continue 97 | abs_img_path = img_dir+img_name 98 | abs_anno_path = anno_dir+img_name.replace("jpg", "xml") 99 | target_annos = extract_target_from_xml(abs_anno_path) 100 | image = cv2.imread(abs_img_path) 101 | for target_info in target_annos: 102 | xmin, ymin, xmax, ymax = target_info[:4] 103 | cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2) 104 | cv2.imshow("src", image) 105 | cv2.waitKey() 106 | 107 | 108 | if __name__ == '__main__': 109 | a = fetch_anno_targets_info( 110 | "/Volumes/projects/repos/RSI/NWPUVHR10/sub_annotation/001.txt") 111 | print(a) -------------------------------------------------------------------------------- /mainmodels/models/tradition/multicnn/layers.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/5 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import re 12 | 13 | import tensorflow as tf 14 | from mainmodels.models.tradition.config import g_MultiCNNConfig 15 | 16 | # If a model is trained with multiple GPUs, prefix all Op names with tower_name 17 | # to differentiate the operations. Note that this prefix is removed from the 18 | # names of the summaries when visualizing a model. 19 | TOWER_NAME = 'tower' 20 | 21 | def _activation_summary(x): 22 | # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training 23 | # session. This helps the clarity of presentation on tensorboard. 24 | tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name) 25 | tf.summary.histogram(tensor_name + '/activations', x) 26 | tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x)) 27 | 28 | 29 | def _variable_on_cpu(name, shape, initializer): 30 | with tf.device('/cpu:0'): 31 | dtype = tf.float16 if g_MultiCNNConfig.use_fp16 else tf.float32 32 | var = tf.get_variable(name, shape, initializer=initializer, dtype=dtype) 33 | return var 34 | 35 | 36 | def _variable_with_weight_decay(name, shape, stddev, wd): 37 | dtype = tf.float16 if g_MultiCNNConfig.use_fp16 else tf.float32 38 | var = _variable_on_cpu(name, shape, 39 | tf.truncated_normal_initializer(stddev=stddev, dtype=dtype)) 40 | if wd is not None: 41 | weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss') 42 | tf.add_to_collection('losses', weight_decay) 43 | return var 44 | 45 | 46 | def _add_loss_summaries(total_loss): 47 | # Compute the moving average of all individual losses and the total loss. 48 | loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') 49 | losses = tf.get_collection('losses') 50 | loss_averages_op = loss_averages.apply(losses + [total_loss]) 51 | 52 | # Attach a scalar summary to all individual losses and the total loss; do the 53 | # same for the averaged version of the losses. 54 | for l in losses + [total_loss]: 55 | # Name each loss as '(raw)' and name the moving average version of the loss 56 | # as the original loss name. 57 | tf.summary.scalar(l.op.name + ' (raw)', l) 58 | tf.summary.scalar(l.op.name, loss_averages.average(l)) 59 | 60 | return loss_averages_op 61 | 62 | def create_conv_layer(tf_input, k_shape, k_stride, out_nums, layer_name, 63 | stddev, wd=0.0): 64 | with tf.variable_scope(layer_name) as scope: 65 | chanel_input = tf_input.shape[-1].value 66 | k_shape = k_shape + [chanel_input, out_nums] 67 | kernel = _variable_with_weight_decay('weights', 68 | shape=k_shape, 69 | stddev=stddev, 70 | wd=wd) 71 | conv = tf.nn.conv2d(tf_input, kernel, k_stride, padding="SAME") 72 | biases = _variable_on_cpu('biases', [out_nums], 73 | tf.constant_initializer(0.1)) 74 | pre_activation = tf.nn.bias_add(conv, biases) 75 | conv_layer = tf.nn.relu(pre_activation, name=scope.name) 76 | _activation_summary(conv_layer) 77 | return conv_layer 78 | 79 | def create_fc_layer(tf_input, out_nums, layer_name, stddev, wd): 80 | with tf.variable_scope(layer_name) as scope: 81 | input_dim_len = len(tf_input.shape) 82 | dim = 1 83 | for i in range(1, input_dim_len): 84 | dim *= tf_input.shape[i].value 85 | reshape = tf.reshape(tf_input, [-1, dim]) 86 | weights = _variable_with_weight_decay('weights', shape=[dim, out_nums], 87 | stddev=stddev, wd=wd) 88 | biases = _variable_on_cpu('biases', [out_nums], tf.constant_initializer(0.1)) 89 | fc_layer = tf.nn.relu(tf.matmul(reshape, weights) + biases, 90 | name=scope.name) 91 | _activation_summary(fc_layer) 92 | return fc_layer 93 | 94 | def create_softmax_layer(tf_input, out_nums, layer_name, stddev=0.005, wd=0.0): 95 | with tf.variable_scope(layer_name) as scope: 96 | input_dim = tf_input.shape[-1].value 97 | weights = _variable_with_weight_decay('weights', 98 | [input_dim, out_nums], 99 | stddev=1/input_dim, wd=wd) 100 | biases = _variable_on_cpu('biases', [out_nums], 101 | tf.constant_initializer(0.0)) 102 | softmax_linear = tf.add(tf.matmul(tf_input, weights), biases, 103 | name=scope.name) 104 | _activation_summary(softmax_linear) 105 | return softmax_linear -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/NMS.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/4 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import numpy as np 12 | 13 | # Felzenszwalb et al. 14 | def non_max_suppression_slow(boxes, overlapThresh): 15 | # if there are no boxes, return an empty list 16 | if len(boxes) == 0: 17 | return boxes 18 | 19 | # initialize the list of picked indexes 20 | pick = [] 21 | 22 | # grab the coordinates of the bounding boxes 23 | x1 = boxes[:, 0] 24 | y1 = boxes[:, 1] 25 | xw = boxes[:, 2] 26 | yh = boxes[:, 3] 27 | x2 = x1 + xw 28 | y2 = y1 + yh 29 | 30 | # compute the area of the bounding boxes and sort the bounding 31 | # boxes by the bottom-right y-coordinate of the bounding box 32 | area = xw * yh 33 | idxs = np.argsort(boxes[:, 4]) 34 | 35 | # keep looping while some indexes still remain in the indexes 36 | # list 37 | while len(idxs) > 0: 38 | # grab the last index in the indexes list, add the index 39 | # value to the list of picked indexes, then initialize 40 | # the suppression list (i.e. indexes that will be deleted) 41 | # using the last index 42 | last = len(idxs) - 1 43 | i = idxs[last] 44 | pick.append(i) 45 | suppress = [last] 46 | 47 | # loop over all indexes in the indexes list 48 | for pos in xrange(0, last): 49 | # grab the current index 50 | j = idxs[pos] 51 | 52 | # find the largest (x, y) coordinates for the start of 53 | # the bounding box and the smallest (x, y) coordinates 54 | # for the end of the bounding box 55 | xx1 = max(x1[i], x1[j]) 56 | yy1 = max(y1[i], y1[j]) 57 | xx2 = min(x2[i], x2[j]) 58 | yy2 = min(y2[i], y2[j]) 59 | 60 | # compute the width and height of the bounding box 61 | w = max(0, xx2 - xx1) 62 | h = max(0, yy2 - yy1) 63 | 64 | # compute the ratio of overlap between the computed 65 | # bounding box and the bounding box in the area list 66 | overlap = float(w * h) / min(area[j], area[i]) 67 | 68 | # if there is sufficient overlap, suppress the 69 | # current bounding box 70 | if overlap > overlapThresh: 71 | suppress.append(pos) 72 | 73 | # delete all indexes from the index list that are in the 74 | # suppression list 75 | idxs = np.delete(idxs, suppress) 76 | 77 | # return only the bounding boxes that were picked 78 | return boxes[pick] 79 | 80 | 81 | def non_max_suppression_fast(boxes, overlapThresh): 82 | # if there are no boxes, return an empty list 83 | if len(boxes) == 0: 84 | return boxes 85 | 86 | # initialize the list of picked indexes 87 | pick = [] 88 | 89 | # grab the coordinates of the bounding boxes 90 | x1 = boxes[:, 0] 91 | y1 = boxes[:, 1] 92 | xw = boxes[:, 2] 93 | yh = boxes[:, 3] 94 | x2 = x1 + xw 95 | y2 = y1 + yh 96 | 97 | # compute the area of the bounding boxes and sort the bounding 98 | # boxes by the bottom-right y-coordinate of the bounding box 99 | area = xw * yh 100 | idxs = np.argsort(boxes[:, 4]) 101 | 102 | # keep looping while some indexes still remain in the indexes 103 | # list 104 | while len(idxs) > 0: 105 | # grab the last index in the indexes list and add the 106 | # index value to the list of picked indexes 107 | last = len(idxs) - 1 108 | i = idxs[last] 109 | pick.append(i) 110 | 111 | # find the largest (x, y) coordinates for the start of 112 | # the bounding box and the smallest (x, y) coordinates 113 | # for the end of the bounding box 114 | xx1 = np.maximum(x1[i], x1[idxs[:last]]) 115 | yy1 = np.maximum(y1[i], y1[idxs[:last]]) 116 | xx2 = np.minimum(x2[i], x2[idxs[:last]]) 117 | yy2 = np.minimum(y2[i], y2[idxs[:last]]) 118 | 119 | # compute the width and height of the bounding box 120 | w = np.maximum(0, xx2 - xx1) 121 | h = np.maximum(0, yy2 - yy1) 122 | 123 | # compute the ratio of overlap 124 | overlap = (w * h) / np.minimum(area[idxs[:last]], area[i]) 125 | 126 | # delete all indexes from the index list that have 127 | idxs = np.delete(idxs, np.concatenate( 128 | ([last], np.where(overlap > overlapThresh)[0]))) 129 | return boxes[pick] 130 | 131 | 132 | def filter_bbox(bbox): 133 | xrng = [0.1, 959.9] 134 | yrng = [0.1, 719.9] 135 | # bbox[:, :4] = bbox[:, :4] / 1.5 136 | x1 = bbox[:, 0] 137 | y1 = bbox[:, 1] 138 | x2 = bbox[:, 0] + bbox[:, 2] 139 | y2 = bbox[:, 1] + bbox[:, 3] 140 | keep = \ 141 | np.where((x1 > xrng[0]) & (x2 < xrng[1]) & (y1 > yrng[0]) & (y2 < yrng[1]))[ 142 | 0] 143 | return bbox[keep, :] -------------------------------------------------------------------------------- /mainmodels/models/tradition/multicnn/model_eval.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/5 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import math 12 | import time 13 | from datetime import datetime 14 | 15 | import numpy as np 16 | import tensorflow as tf 17 | from mainmodels.models.tradition.multicnn import model 18 | from mainmodels.models.tradition.config import g_MultiCNNConfig 19 | 20 | FLAGS = tf.app.flags.FLAGS 21 | 22 | tf.app.flags.DEFINE_string('eval_dir', g_MultiCNNConfig.eval_log_dir, 23 | """Directory where to write event logs.""") 24 | tf.app.flags.DEFINE_string('eval_data', 'test', 25 | """Either 'test' or 'train_eval'.""") 26 | tf.app.flags.DEFINE_string('checkpoint_dir', g_MultiCNNConfig.train_log_dir, 27 | """Directory where to read model checkpoints.""") 28 | tf.app.flags.DEFINE_integer('eval_interval_secs', 30 * 1, 29 | """How often to run the eval.""") 30 | tf.app.flags.DEFINE_integer('num_examples', 31 | g_MultiCNNConfig.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL, 32 | """Number of examples to run.""") 33 | tf.app.flags.DEFINE_integer('batch_size', g_MultiCNNConfig.batch_size, 34 | """Number of examples to run.""") 35 | tf.app.flags.DEFINE_boolean('run_once', False, 36 | """Whether to run eval only once.""") 37 | 38 | 39 | def eval_once(saver, summary_writer, top_k_op, summary_op): 40 | """Run Eval once. 41 | 42 | Args: 43 | saver: Saver. 44 | summary_writer: Summary writer. 45 | top_k_op: Top K op. 46 | summary_op: Summary op. 47 | """ 48 | with tf.Session() as sess: 49 | ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) 50 | if ckpt and ckpt.model_checkpoint_path: 51 | # Restores from checkpoint 52 | saver.restore(sess, ckpt.model_checkpoint_path) 53 | # Assuming model_checkpoint_path looks something like: 54 | # /my-favorite-path/cifar10_train/model.ckpt-0, 55 | # extract global_step from it. 56 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 57 | else: 58 | print('No checkpoint file found') 59 | return 60 | 61 | # Start the queue runners. 62 | coord = tf.train.Coordinator() 63 | try: 64 | threads = [] 65 | for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): 66 | threads.extend(qr.create_threads(sess, coord=coord, daemon=True, 67 | start=True)) 68 | 69 | num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size)) 70 | true_count = 0 # Counts the number of correct predictions. 71 | total_sample_count = num_iter * FLAGS.batch_size 72 | step = 0 73 | while step < num_iter and not coord.should_stop(): 74 | predictions = sess.run([top_k_op]) 75 | true_count += np.sum(predictions) 76 | step += 1 77 | 78 | # Compute precision @ 1. 79 | precision = true_count / total_sample_count 80 | print('%s: precision @ 1 = %.3f' % (datetime.now(), precision)) 81 | 82 | summary = tf.Summary() 83 | summary.ParseFromString(sess.run(summary_op)) 84 | summary.value.add(tag='Precision @ 1', simple_value=precision) 85 | summary_writer.add_summary(summary, global_step) 86 | except Exception as e: # pylint: disable=broad-except 87 | coord.request_stop(e) 88 | 89 | coord.request_stop() 90 | coord.join(threads, stop_grace_period_secs=10) 91 | 92 | 93 | def evaluate(): 94 | with tf.Graph().as_default() as g: 95 | images, labels = model.inputs(g_MultiCNNConfig.test_samples_path) 96 | 97 | # Build a Graph that computes the logits predictions from the 98 | # inference model. 99 | logits = model.inference(images) 100 | 101 | # Calculate predictions. 102 | top_k_op = tf.nn.in_top_k(logits, labels, 1) 103 | 104 | # Restore the moving average version of the learned variables for eval. 105 | variable_averages = tf.train.ExponentialMovingAverage( 106 | g_MultiCNNConfig.MOVING_AVERAGE_DECAY) 107 | variables_to_restore = variable_averages.variables_to_restore() 108 | saver = tf.train.Saver(variables_to_restore) 109 | 110 | # Build the summary operation based on the TF collection of Summaries. 111 | summary_op = tf.summary.merge_all() 112 | 113 | summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) 114 | 115 | while True: 116 | eval_once(saver, summary_writer, top_k_op, summary_op) 117 | if FLAGS.run_once: 118 | break 119 | time.sleep(FLAGS.eval_interval_secs) 120 | 121 | 122 | def main(argv=None): 123 | if tf.gfile.Exists(FLAGS.eval_dir): 124 | tf.gfile.DeleteRecursively(FLAGS.eval_dir) 125 | tf.gfile.MakeDirs(FLAGS.eval_dir) 126 | evaluate() 127 | 128 | 129 | if __name__ == '__main__': 130 | tf.app.run() -------------------------------------------------------------------------------- /mainmodels/dataset/JLVideo/显示结果图.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/6 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import cv2 13 | 14 | from mainmodels.dataset import tools 15 | from mainmodels.dataset.LSD10.tools import extract_target_from_xml 16 | 17 | dir_prefix = "/Volumes/projects/repos/RSI/CSUVOCFormat/608x608/large_000013363_total/" 18 | anno_dir = dir_prefix + "Annotations/" 19 | image_dir = dir_prefix + "JPEGImages/" 20 | model_res_dir = dir_prefix + "results/" 21 | all_test_filepath = dir_prefix + "all_test.txt" 22 | 23 | all_dir_prefix = "/Volumes/projects/repos/RSI/CSUVOCFormat/source/large_000013363_total/" 24 | all_anno_dir = all_dir_prefix + "Annotations/" 25 | all_image_dir = all_dir_prefix + "JPEGImages/" 26 | 27 | res_save_dir = "/Volumes/projects/repos/RSI/CSUVOCFormat/608x608" \ 28 | "/large_000013363_total/images2video_res/" 29 | 30 | def merge_model_results(prob=0.50): 31 | # {"f_name": [[prob,area,target],]} 32 | all_model_results = dict() 33 | for filename in os.listdir(model_res_dir): 34 | target_name = filename.split(".")[0].split("_")[-1] 35 | model_res_path = model_res_dir + filename 36 | with open(model_res_path, "r") as res_reader: 37 | for line in res_reader: 38 | line = line.strip().split(" ") 39 | if float(line[1]) < prob: 40 | continue 41 | f_name, t_prob = line[0]+".jpg", float(line[1]) 42 | t_area = [int(float(a)) for a in line[2:]] 43 | if f_name not in all_model_results: 44 | all_model_results[f_name] = list() 45 | all_model_results[f_name].append([t_prob]+t_area+[target_name]) 46 | return all_model_results 47 | 48 | all_model_results = merge_model_results() 49 | 50 | # cv2.namedWindow("000013363", cv2.WINDOW_NORMAL) 51 | # for idx in range(1, len(os.listdir(all_anno_dir)), 5): 52 | # anno_name = "%06d.xml" % idx 53 | # src_img_name = "%06d.jpg" % idx 54 | # anno_path = all_anno_dir + anno_name 55 | # img_path = all_image_dir + src_img_name 56 | # src_image = cv2.imread(img_path) 57 | # targets_info = tools.extract_airplane_posinfo(anno_path) 58 | # if len(targets_info) == 0: 59 | # continue 60 | # 61 | # src_img = cv2.imread(img_path) 62 | # for area in targets_info: 63 | # cv2.rectangle(src_img, 64 | # (area[0], area[1]), 65 | # (area[2], area[3]), 66 | # (0, 255, 0), 2) 67 | # 68 | # for sub_img_name in all_model_results.keys(): 69 | # if sub_img_name[:6] == src_img_name[:6]: 70 | # x0, y0, x1, y1 = [int(a) for a in sub_img_name[7:-4].split("_")][:4] 71 | # for items in all_model_results[sub_img_name]: 72 | # cv2.rectangle(src_img, 73 | # (items[1]+x0, items[2]+y0), 74 | # (items[3]+x0, items[4]+y0), 75 | # (0, 0, 255), 2) 76 | # cv2.imwrite(res_save_dir+src_img_name, src_img, 77 | # [int(cv2.IMWRITE_JPEG_QUALITY), 100]) 78 | # cv2.imshow("000013363", src_img) 79 | # cv2.waitKey() 80 | 81 | 82 | # with open(all_test_filepath, "r") as test_reader: 83 | # for line in test_reader: 84 | # filename = image_dir + line.strip() 85 | # gt_anno_path = anno_dir + ".".join( 86 | # line.strip().split(".")[:-1] + ["xml"]) 87 | # anno_details = extract_target_from_xml(gt_anno_path) 88 | # # print(anno_details) 89 | # src_img = cv2.imread(filename) 90 | # for area in anno_details: 91 | # cv2.rectangle(src_img, 92 | # (area[0], area[1]), 93 | # (area[2], area[3]), 94 | # (0, 255, 0), 2) 95 | # # 绘制模型检测结果的目标位置 96 | # # print(all_model_results[line.strip()]) 97 | # for items in all_model_results[line.strip()]: 98 | # cv2.rectangle(src_img, 99 | # (items[1], items[2]), 100 | # (items[3], items[4]), 101 | # (0, 0, 255), 2) 102 | # cv2.imshow(line.strip(), src_img) 103 | # cv2.waitKey() 104 | 105 | img_root = '/Volumes/projects/repos/RSI/CSUVOCFormat/608x608' \ 106 | '/large_000013363_total/images2video_res/' 107 | fps = 5 #保存视频的FPS,可以适当调整 108 | 109 | #可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg 110 | fourcc = cv2.VideoWriter_fourcc(*'MJPG') 111 | videoWriter = cv2.VideoWriter('saveVideo.avi', 112 | fourcc, fps, (4096, 3072))#最后一个是保存图片的尺寸 113 | 114 | for i in range(1, 673, 5): 115 | img_path = img_root + "%06d.jpg" % i 116 | if not os.path.exists(img_path): 117 | continue 118 | print(img_path) 119 | frame = cv2.imread(img_path) 120 | videoWriter.write(frame) 121 | videoWriter.release() -------------------------------------------------------------------------------- /mainmodels/models/tradition/cnn/model_eval.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/3 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import math 12 | import time 13 | from datetime import datetime 14 | 15 | import numpy as np 16 | import tensorflow as tf 17 | from mainmodels.models.tradition.cnn import model 18 | from mainmodels.models.tradition.config import g_CNNConfig 19 | 20 | FLAGS = tf.app.flags.FLAGS 21 | 22 | tf.app.flags.DEFINE_string('eval_dir', g_CNNConfig.eval_log_dir, 23 | """Directory where to write event logs.""") 24 | tf.app.flags.DEFINE_string('eval_data', 'test', 25 | """Either 'test' or 'train_eval'.""") 26 | tf.app.flags.DEFINE_string('checkpoint_dir', g_CNNConfig.train_log_dir, 27 | """Directory where to read model checkpoints.""") 28 | tf.app.flags.DEFINE_integer('eval_interval_secs', 30 * 1, 29 | """How often to run the eval.""") 30 | tf.app.flags.DEFINE_integer('num_examples', 31 | g_CNNConfig.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL, 32 | """Number of examples to run.""") 33 | tf.app.flags.DEFINE_integer('batch_size', g_CNNConfig.batch_size, 34 | """Number of examples to run.""") 35 | tf.app.flags.DEFINE_boolean('run_once', False, 36 | """Whether to run eval only once.""") 37 | 38 | 39 | def eval_once(saver, summary_writer, top_k_op, summary_op): 40 | """Run Eval once. 41 | 42 | Args: 43 | saver: Saver. 44 | summary_writer: Summary writer. 45 | top_k_op: Top K op. 46 | summary_op: Summary op. 47 | """ 48 | with tf.Session() as sess: 49 | ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) 50 | if ckpt and ckpt.model_checkpoint_path: 51 | # Restores from checkpoint 52 | saver.restore(sess, ckpt.model_checkpoint_path) 53 | # Assuming model_checkpoint_path looks something like: 54 | # /my-favorite-path/cifar10_train/model.ckpt-0, 55 | # extract global_step from it. 56 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 57 | else: 58 | print('No checkpoint file found') 59 | return 60 | 61 | # Start the queue runners. 62 | coord = tf.train.Coordinator() 63 | try: 64 | threads = [] 65 | for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): 66 | threads.extend(qr.create_threads(sess, coord=coord, daemon=True, 67 | start=True)) 68 | 69 | num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size)) 70 | true_count = 0 # Counts the number of correct predictions. 71 | total_sample_count = num_iter * FLAGS.batch_size 72 | step = 0 73 | while step < num_iter and not coord.should_stop(): 74 | predictions = sess.run([top_k_op]) 75 | true_count += np.sum(predictions) 76 | step += 1 77 | 78 | # Compute precision @ 1. 79 | precision = true_count / total_sample_count 80 | print('%s: precision @ 1 = %.3f' % (datetime.now(), precision)) 81 | 82 | summary = tf.Summary() 83 | summary.ParseFromString(sess.run(summary_op)) 84 | summary.value.add(tag='Precision @ 1', simple_value=precision) 85 | summary_writer.add_summary(summary, global_step) 86 | except Exception as e: # pylint: disable=broad-except 87 | coord.request_stop(e) 88 | 89 | coord.request_stop() 90 | coord.join(threads, stop_grace_period_secs=10) 91 | 92 | 93 | def evaluate(): 94 | """Eval CIFAR-10 for a number of steps.""" 95 | with tf.Graph().as_default() as g: 96 | # Get images and labels for CIFAR-10. 97 | images, labels = model.inputs(g_CNNConfig.test_samples_path) 98 | 99 | # Build a Graph that computes the logits predictions from the 100 | # inference model. 101 | logits = model.inference(images) 102 | 103 | # Calculate predictions. 104 | top_k_op = tf.nn.in_top_k(logits, labels, 1) 105 | 106 | # Restore the moving average version of the learned variables for eval. 107 | variable_averages = tf.train.ExponentialMovingAverage( 108 | g_CNNConfig.MOVING_AVERAGE_DECAY) 109 | variables_to_restore = variable_averages.variables_to_restore() 110 | saver = tf.train.Saver(variables_to_restore) 111 | 112 | # Build the summary operation based on the TF collection of Summaries. 113 | summary_op = tf.summary.merge_all() 114 | 115 | summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) 116 | 117 | while True: 118 | eval_once(saver, summary_writer, top_k_op, summary_op) 119 | if FLAGS.run_once: 120 | break 121 | time.sleep(FLAGS.eval_interval_secs) 122 | 123 | 124 | def main(argv=None): # pylint: disable=unused-argument 125 | if tf.gfile.Exists(FLAGS.eval_dir): 126 | tf.gfile.DeleteRecursively(FLAGS.eval_dir) 127 | tf.gfile.MakeDirs(FLAGS.eval_dir) 128 | evaluate() 129 | 130 | 131 | if __name__ == '__main__': 132 | tf.app.run() -------------------------------------------------------------------------------- /mainmodels/dataset/JLVideo/图像切割成标准输入.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/12/11 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | # 把吉林一号视频卫星数据切割成指定大小的数据进行批量的检测 12 | import os 13 | import json 14 | import codecs 15 | import random 16 | import cv2 17 | import time 18 | from lxml.etree import Element, SubElement, tostring 19 | from mainmodels.dataset import tools 20 | 21 | 22 | dir_prefix = "/Volumes/projects/repos/RSI/CSUVOCFormat/" 23 | tpl_src_annotation_dir = dir_prefix + "source/{}/Annotations/" 24 | tpl_src_images_dir = dir_prefix + "source/{}/JPEGImages/" 25 | 26 | 27 | SSD_IMG_W, SSD_IMG_H = 608, 608 28 | 29 | tpl_img_voc_dir = dir_prefix + "{}x{}/{}/JPEGImages/" 30 | tpl_annotation_voc_dir = dir_prefix + "{}x{}/{}/Annotations/" 31 | tpl_src_png_dir = dir_prefix + "{}x{}/{}/PNGImages/" 32 | 33 | sign_label = { 34 | "plane": 1 35 | } 36 | 37 | id2sign_dict = { 38 | 1: "plane" 39 | } 40 | 41 | def load_annotation(video_name): 42 | src_annotation_dir = tpl_src_annotation_dir.format(video_name) 43 | src_images_dir = tpl_src_images_dir.format(video_name) 44 | for idx in range(1, len(os.listdir(src_annotation_dir))+1, 5): 45 | anno_name = "%06d.xml" % idx 46 | src_img_name = "%06d.jpg" % idx 47 | anno_path = src_annotation_dir + anno_name 48 | img_path = src_images_dir + src_img_name 49 | src_image = cv2.imread(img_path) 50 | targets_info = tools.extract_airplane_posinfo(anno_path) 51 | if len(targets_info) == 0: 52 | continue 53 | create_ssd_training_samples(video_name, src_image, targets_info, src_img_name) 54 | 55 | # 从样本中随机裁剪出制定的大小的候选样本,这其中必须要包含相应的目标 56 | def create_ssd_training_samples( 57 | video_name, src_image, anno_targets, image_name): 58 | # 保存裁剪出来的结果 59 | def _crop_valid(area, anno_targets): 60 | anno_res = [] 61 | for info in anno_targets: 62 | if ((info[0] >= area[0] and info[1] >= area[1]) and 63 | (info[2] <= area[2] and info[3] <= area[3])): 64 | anno_res.append( 65 | [info[0] - area[0], info[1] - area[1], 66 | info[2] - area[0], info[3] - area[1], 67 | info[-1]]) 68 | if (info[0] >= area[0] and info[1] >= area[1] and 69 | info[0] < area[2] and info[1] < area[3] and 70 | (not (info[2] <= area[2] and info[3] <= area[3]))): 71 | base = (info[2] - info[0]) * (info[3] - info[1]) 72 | x_max_min = min(info[2], area[2]) 73 | y_max_min = min(info[3], area[3]) 74 | new_square = (x_max_min - info[0]) * (y_max_min - info[1]) 75 | if new_square / base >= 0.75: 76 | anno_res.append( 77 | [info[0] - area[0], info[1] - area[1], 78 | x_max_min - area[0], y_max_min - area[1], 79 | info[-1]]) 80 | return anno_res 81 | 82 | def _align_crop_for_target(): 83 | h, w = src_image.shape[:2] 84 | crop_list, anno_list = [], [] 85 | for lx in range(0, w, int(SSD_IMG_W//1)): 86 | for ly in range(0, h, int(SSD_IMG_H//1)): 87 | u_x, u_y = lx, ly 88 | if lx + SSD_IMG_W > w: 89 | u_x = w - SSD_IMG_W 90 | if ly + SSD_IMG_H > h: 91 | u_y = h - SSD_IMG_H 92 | area = [u_x, u_y, u_x + SSD_IMG_W, u_y + SSD_IMG_H] 93 | trans_targets = _crop_valid(area, anno_targets) 94 | if trans_targets: 95 | crop_list.append(area) 96 | anno_list.append(trans_targets) 97 | return crop_list, anno_list 98 | 99 | crop_list, anno_list = _align_crop_for_target() 100 | target_img_voc_dir = tpl_img_voc_dir.format( 101 | SSD_IMG_W, SSD_IMG_H, video_name) 102 | target_annotation_voc_dir = tpl_annotation_voc_dir.format( 103 | SSD_IMG_W, SSD_IMG_H, video_name) 104 | if not os.path.exists(target_img_voc_dir): 105 | os.makedirs(target_img_voc_dir) 106 | if not os.path.exists(target_annotation_voc_dir): 107 | os.makedirs(target_annotation_voc_dir) 108 | 109 | image_name = ".".join(image_name.split(".")[:-1]) 110 | for i in range(len(crop_list)): 111 | x0, y0, x1, y1 = crop_list[i] 112 | # roi = im[y1:y2, x1:x2] opencv中类似NUMPY的裁剪 113 | sub_img = src_image[y0:y1, x0:x1] 114 | f_name = image_name + "_%d_%d_%d_%d_%d.jpg" % (x0, y0, x1, y1, i) 115 | cv2.imwrite(target_img_voc_dir + f_name, sub_img) 116 | xml_obj = tools.fetch_xml_format(sub_img, f_name, anno_list[i]) 117 | f_name = image_name + "_%d_%d_%d_%d_%d.xml" % (x0, y0, x1, y1, i) 118 | with codecs.open(target_annotation_voc_dir + f_name, "w") as handle: 119 | handle.write(xml_obj) 120 | print(target_annotation_voc_dir + f_name) 121 | 122 | 123 | if __name__ == '__main__': 124 | load_annotation("large_000013363_total") -------------------------------------------------------------------------------- /mainmodels/models/rpnplus/tools/image_pylib.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/4 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | from PIL import Image, ImageDraw, ImageFont 12 | import random 13 | 14 | def safeInt(ss): 15 | return int(float(ss)) 16 | 17 | class BBX: 18 | def __init__(self): 19 | pass 20 | 21 | def str2bbx(self, str): 22 | chrs = str.split(' ') 23 | 24 | self.name = chrs[0] 25 | 26 | self.x = safeInt(chrs[1]) 27 | self.y = safeInt(chrs[2]) 28 | self.w = safeInt(chrs[3]) 29 | self.h = safeInt(chrs[4]) 30 | self.score = float(chrs[5]) 31 | 32 | def resize(self, scale, x_d, y_d): 33 | self.x = safeInt(self.x * scale) + x_d 34 | self.y = safeInt(self.y * scale) + y_d 35 | self.w = safeInt(self.w * scale) 36 | self.h = safeInt(self.h * scale) 37 | 38 | class IMGLIB: 39 | def __init__(self): 40 | pass 41 | 42 | def setBBXs(self, bboxs, name0): 43 | self.bbxs = [] 44 | for bbox in bboxs: 45 | bbx = BBX() 46 | bbx.name = name0 47 | bbx.x = safeInt(bbox[0]) 48 | bbx.y = safeInt(bbox[1]) 49 | bbx.w = safeInt(bbox[2]) 50 | bbx.h = safeInt(bbox[3]) 51 | bbx.score = bbox[4] 52 | self.bbxs.append(bbx) 53 | 54 | def showBBXs(self): 55 | self.drawBox() 56 | self.img.show() 57 | 58 | def saveBBXs(self, fileName): 59 | f = open(fileName, 'w') 60 | f.write('% bbGt version=3\n') 61 | for bbx in self.bbxs: 62 | f.write('%s %d %d %d %d %f 0 0 0 0 0 0\n' % (bbx.name, bbx.x, bbx.y, bbx.w, bbx.h, bbx.score)) 63 | f.close() 64 | 65 | def drawOneBox(self, bbx, thr=-1.0,showName = False): 66 | 67 | if bbx.score >= thr: 68 | x = bbx.x 69 | y = bbx.y 70 | w = bbx.w 71 | h = bbx.h 72 | line1 = ((x, y), (x + w, y), (x + w, y + h), (x, y + h), (x, y)) 73 | 74 | self.draw.line(line1, fill=(255, 0, 0)) 75 | if showName: 76 | font = ImageFont.truetype("OpenSans-Regular.ttf", 20) 77 | self.draw.text((x, y - 25), str(bbx.score), fill=(255, 0, 0), font=font) 78 | 79 | def drawBox(self, thr=-1.0, showName = False): 80 | self.draw = ImageDraw.Draw(self.img) 81 | for bbx in self.bbxs: 82 | self.drawOneBox(bbx, thr,showName) 83 | 84 | def read_img(self, fileName): 85 | self.img = Image.open(fileName) 86 | 87 | def read_ano(self, fileName): 88 | 89 | f = open(fileName, 'r') 90 | lines = f.readlines() 91 | self.bbxs = [] 92 | for line in lines[1:]: 93 | nbbx = BBX() 94 | nbbx.str2bbx(line) 95 | self.bbxs.append(nbbx) 96 | 97 | # self.img.show() 98 | 99 | def resizeBBXs(self, r, x_d, y_d): 100 | for bbx in self.bbxs: 101 | bbx.resize(r, x_d, y_d) 102 | 103 | def resize(self, width, height, scale=1.0): 104 | o_width, o_height = self.img.size 105 | t_width = safeInt(width * scale) 106 | t_height = safeInt(height * scale) 107 | 108 | o_ratio = o_width / float(o_height) 109 | n_ratio = width / float(height) 110 | 111 | if o_ratio > n_ratio: 112 | re_ration = t_width / float(o_width) 113 | a_height = safeInt(re_ration * o_height) 114 | a_width = t_width 115 | self.img = self.img.resize((a_width, a_height), Image.ANTIALIAS) 116 | else: 117 | re_ration = t_height / float(o_height) 118 | a_width = safeInt(re_ration * o_width) 119 | a_height = t_height 120 | self.img = self.img.resize((a_width, a_height), Image.ANTIALIAS) 121 | 122 | self.x_d = random.randint(0, abs(a_width - width)) 123 | self.y_d = random.randint(0, abs(a_height - height)) 124 | imgNew = Image.new("RGB", (width, height), "black") 125 | 126 | box = (0, 0, a_width, a_height) 127 | region = self.img.crop(box) 128 | 129 | imgNew.paste(region, (self.x_d, self.y_d)) 130 | self.img = imgNew 131 | self.resizeBBXs(re_ration, self.x_d, self.y_d) 132 | # self.drawBox() 133 | 134 | def cleanAno(self,w0, h0): 135 | newBBXS = [] 136 | for bbox in self.bbxs: 137 | if bbox.x >= 0 and bbox.x <= w0 and bbox.y >= 0 and bbox.y <= h0 and bbox.w >= 20 and bbox.w <= w0 and bbox.h >= 30 and bbox.h <= h0: 138 | bbx = BBX() 139 | bbx.name = bbox.name 140 | bbx.x = bbox.x 141 | bbx.y = bbox.y 142 | bbx.w = bbox.w 143 | bbx.h = bbox.h 144 | bbx.score = bbox.score 145 | newBBXS.append(bbx) 146 | self.bbxs = newBBXS 147 | 148 | def save_img(self, imgName): 149 | self.img.save(imgName) 150 | 151 | def pureResize(self,width, height): 152 | self.img = self.img.resize((width, height), Image.ANTIALIAS) 153 | 154 | if __name__ == '__main__': 155 | pass 156 | 157 | -------------------------------------------------------------------------------- /mainmodels/dataset/NWPUVHR10/tools.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import re 13 | import cv2 14 | 15 | import xml.dom.minidom 16 | from lxml.etree import Element, SubElement, tostring 17 | from mainmodels.dataset.NWPUVHR10 import nwpu_config as config 18 | 19 | # 提取图像对应的标注的数据 20 | def fetch_anno_targets_info(abs_anno_path, is_label_text=False): 21 | if not os.path.exists(abs_anno_path): 22 | raise IOError("No Such annotation file !") 23 | with open(abs_anno_path, "r") as anno_reader: 24 | total_annos = list() 25 | for line in anno_reader: 26 | line = line.strip() 27 | sub_anno = re.split("\(|\,|\)", line) 28 | a = [int(item) for item in sub_anno if len(item)] 29 | if len(a) == 5: 30 | if is_label_text: 31 | total_annos.append(a[:4]+[config.idx_sign_dict[a[-1]]]) 32 | else: 33 | total_annos.append(a) 34 | return total_annos 35 | 36 | def fetch_xml_format(src_img_data, f_name, anno_list): 37 | img_height, img_width, img_channle = src_img_data.shape 38 | 39 | node_root = Element('annotation') 40 | node_folder = SubElement(node_root, 'folder') 41 | node_folder.text = 'NWPU' 42 | node_filename = SubElement(node_root, 'filename') 43 | node_filename.text = f_name 44 | 45 | node_size = SubElement(node_root, 'size') 46 | node_width = SubElement(node_size, 'width') 47 | node_width.text = str(img_width) 48 | node_height = SubElement(node_size, 'height') 49 | node_height.text = str(img_height) 50 | node_depth = SubElement(node_size, 'depth') 51 | node_depth.text = str(img_channle) 52 | 53 | for anno_target in anno_list: 54 | node_object = SubElement(node_root, 'object') 55 | node_name = SubElement(node_object, 'name') 56 | node_name.text = anno_target[-1] 57 | node_difficult = SubElement(node_object, 'difficult') 58 | node_difficult.text = '0' 59 | node_bndbox = SubElement(node_object, 'bndbox') 60 | node_xmin = SubElement(node_bndbox, 'xmin') 61 | node_xmin.text = str(anno_target[0]) 62 | node_ymin = SubElement(node_bndbox, 'ymin') 63 | node_ymin.text = str(anno_target[1]) 64 | node_xmax = SubElement(node_bndbox, 'xmax') 65 | node_xmax.text = str(anno_target[2]) 66 | node_ymax = SubElement(node_bndbox, 'ymax') 67 | node_ymax.text = str(anno_target[3]) 68 | xml_obj = tostring(node_root, pretty_print=True) 69 | xml_obj = xml_obj.decode("utf8") 70 | return xml_obj 71 | 72 | # 给定一个标记文件,找到对应的目标的位置信息 73 | def extract_target_from_xml(filename): 74 | if not os.path.exists(filename): 75 | raise IOError(filename + " not exists !") 76 | # 使用minidom解析器打开 XML 文档 77 | DOMTree = xml.dom.minidom.parse(filename) 78 | collection = DOMTree.documentElement 79 | # 获取集合中所有的目标 80 | targets = collection.getElementsByTagName("object") 81 | res = [] 82 | for target in targets: 83 | target_name = target.getElementsByTagName('name')[0].childNodes[0].data 84 | bndbox = target.getElementsByTagName("bndbox")[0] 85 | xmin = bndbox.getElementsByTagName("xmin")[0].childNodes[0].data 86 | ymin = bndbox.getElementsByTagName("ymin")[0].childNodes[0].data 87 | xmax = bndbox.getElementsByTagName("xmax")[0].childNodes[0].data 88 | ymax = bndbox.getElementsByTagName("ymax")[0].childNodes[0].data 89 | res.append([int(xmin), int(ymin), int(xmax), int(ymax), target_name]) 90 | return res 91 | 92 | # 原始数据中多目标的显示 93 | def show_targets(img_dir, anno_dir): 94 | for img_name in os.listdir(img_dir): 95 | if img_name.startswith("._"): 96 | continue 97 | abs_img_path = img_dir+img_name 98 | abs_anno_path = anno_dir+img_name.replace("jpg", "xml") 99 | target_annos = extract_target_from_xml(abs_anno_path) 100 | image = cv2.imread(abs_img_path) 101 | for target_info in target_annos: 102 | xmin, ymin, xmax, ymax = target_info[:4] 103 | cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2) 104 | cv2.imshow("src", image) 105 | cv2.waitKey() 106 | 107 | 108 | def show_nwpu_vhr_annotation(img_names): 109 | img_dir = "/Volumes/projects/DataSets/NWPU-VHR-10/positive_image_set/" 110 | anno_dir = "/Volumes/projects/DataSets/NWPU-VHR-10/ground_truth/" 111 | for name in img_names: 112 | img_path = img_dir + name + ".jpg" 113 | image = cv2.imread(img_path) 114 | 115 | anno_path = anno_dir + name + ".txt" 116 | with open(anno_path, "r") as reader: 117 | for line in reader: 118 | a = re.split(r'\(|\)|,|\s', line.strip()) 119 | print(a) 120 | c = [] 121 | for b in a: 122 | if len(b): 123 | c.append(int(b)) 124 | x1, y1, x2, y2, label = c 125 | cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) 126 | # break 127 | # break 128 | 129 | cv2.imshow("src", image) 130 | cv2.imwrite(name+".jpg", image) 131 | cv2.waitKey() 132 | pass 133 | 134 | 135 | if __name__ == '__main__': 136 | # a = fetch_anno_targets_info( 137 | # "/Volumes/projects/repos/RSI/NWPUVHR10/sub_annotation/001.txt") 138 | # print(a) 139 | show_nwpu_vhr_annotation(["160"]) -------------------------------------------------------------------------------- /mainmodels/dataset/VEDAI/tools.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/10 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import re 13 | import cv2 14 | 15 | import xml.dom.minidom 16 | from lxml.etree import Element, SubElement, tostring 17 | from mainmodels.dataset.VEDAI import vedia_config as config 18 | 19 | # 提取图像对应的标注的数据 20 | def fetch_anno_targets_info(abs_anno_path, is_label_text=False): 21 | if not os.path.exists(abs_anno_path): 22 | raise IOError("No Such annotation file !") 23 | with open(abs_anno_path, "r") as anno_reader: 24 | total_annos = list() 25 | for line in anno_reader: 26 | line = line.strip() 27 | sub_anno = re.split("\(|\,|\)", line) 28 | a = [int(item) for item in sub_anno if len(item)] 29 | if len(a) == 5: 30 | if is_label_text: 31 | total_annos.append(a[:4]+[config.idx_sign_dict[a[-1]]]) 32 | else: 33 | total_annos.append(a) 34 | return total_annos 35 | 36 | def fetch_xml_format(src_img_data, f_name, anno_list): 37 | img_height, img_width, img_channle = src_img_data.shape 38 | 39 | node_root = Element('annotation') 40 | node_folder = SubElement(node_root, 'folder') 41 | node_folder.text = 'VEDAI' 42 | node_filename = SubElement(node_root, 'filename') 43 | node_filename.text = f_name 44 | 45 | node_size = SubElement(node_root, 'size') 46 | node_width = SubElement(node_size, 'width') 47 | node_width.text = str(img_width) 48 | node_height = SubElement(node_size, 'height') 49 | node_height.text = str(img_height) 50 | node_depth = SubElement(node_size, 'depth') 51 | node_depth.text = str(img_channle) 52 | 53 | for anno_target in anno_list: 54 | node_object = SubElement(node_root, 'object') 55 | node_name = SubElement(node_object, 'name') 56 | if isinstance(anno_target[-1], int) and anno_target[-1] in config.remove_label_ids: 57 | continue 58 | if isinstance(anno_target[-1], int): 59 | node_name.text = config.idx_sign_dict[anno_target[-1]] 60 | else: 61 | node_name.text = anno_target[-1] 62 | node_difficult = SubElement(node_object, 'difficult') 63 | node_difficult.text = '0' 64 | node_bndbox = SubElement(node_object, 'bndbox') 65 | node_xmin = SubElement(node_bndbox, 'xmin') 66 | node_xmin.text = str(anno_target[0]) 67 | node_ymin = SubElement(node_bndbox, 'ymin') 68 | node_ymin.text = str(anno_target[1]) 69 | node_xmax = SubElement(node_bndbox, 'xmax') 70 | node_xmax.text = str(anno_target[2]) 71 | node_ymax = SubElement(node_bndbox, 'ymax') 72 | node_ymax.text = str(anno_target[3]) 73 | xml_obj = tostring(node_root, pretty_print=True) 74 | xml_obj = xml_obj.decode("utf8") 75 | return xml_obj 76 | 77 | # 给定一个标记文件,找到对应的目标的位置信息 78 | def extract_target_from_xml(filename): 79 | if not os.path.exists(filename): 80 | raise IOError(filename + " not exists !") 81 | # 使用minidom解析器打开 XML 文档 82 | DOMTree = xml.dom.minidom.parse(filename) 83 | collection = DOMTree.documentElement 84 | # 获取集合中所有的目标 85 | targets = collection.getElementsByTagName("object") 86 | res = [] 87 | for target in targets: 88 | target_name = target.getElementsByTagName('name')[0].childNodes[0].data 89 | bndbox = target.getElementsByTagName("bndbox")[0] 90 | xmin = bndbox.getElementsByTagName("xmin")[0].childNodes[0].data 91 | ymin = bndbox.getElementsByTagName("ymin")[0].childNodes[0].data 92 | xmax = bndbox.getElementsByTagName("xmax")[0].childNodes[0].data 93 | ymax = bndbox.getElementsByTagName("ymax")[0].childNodes[0].data 94 | res.append([int(xmin), int(ymin), int(xmax), int(ymax), target_name]) 95 | return res 96 | 97 | # 原始数据中多目标的显示 98 | def show_targets(img_dir, anno_dir): 99 | for img_name in os.listdir(img_dir): 100 | if img_name.startswith("._"): 101 | continue 102 | abs_img_path = img_dir+img_name 103 | abs_anno_path = anno_dir+img_name.replace("jpg", "xml") 104 | target_annos = extract_target_from_xml(abs_anno_path) 105 | image = cv2.imread(abs_img_path) 106 | for target_info in target_annos: 107 | xmin, ymin, xmax, ymax = target_info[:4] 108 | cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2) 109 | cv2.imshow("src", image) 110 | cv2.waitKey() 111 | 112 | def fetch_targets_info(abs_anno_path): 113 | anno_targets = list() 114 | normal_anno_targets = list() 115 | with open(abs_anno_path, "r") as anno_reader: 116 | for line in anno_reader: 117 | line = line.strip().split(" ") 118 | if len(line) != 14: 119 | continue 120 | label_id = int(line[3]) 121 | # x1,x2,x3,x4,y1,y2,y3,y4 122 | t = [int(a) for a in line[-8:]] 123 | coordinate_pos = [t[0], t[4], t[1], t[5], t[2], t[6], t[3], t[7]] 124 | normal_pos = [min(t[:4]), min(t[4:8]), max(t[:4]), max(t[4:8])] 125 | anno_targets.append(coordinate_pos + [label_id]) 126 | normal_anno_targets.append(normal_pos + [label_id]) 127 | return anno_targets, normal_anno_targets 128 | 129 | 130 | if __name__ == '__main__': 131 | a = fetch_anno_targets_info( 132 | "/Volumes/projects/repos/RSI/NWPUVHR10/sub_annotation/001.txt") 133 | print(a) -------------------------------------------------------------------------------- /mainmodels/dataset/JLVideo/吉林一号视频信息.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/11/2 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | ''' 12 | 主要是统计吉林一号四段视频数据中的飞机信息 13 | ''' 14 | 15 | import os 16 | 17 | import cv2 18 | 19 | from mainmodels.dataset import tools 20 | 21 | base_dir = "/Volumes/projects/repos/RSI/" 22 | video_name = "large_000013363_total" 23 | video_name = "large_000014631_total" 24 | video_name = "large_minneapolis_1_total" 25 | video_name = "large_tunisia_total" 26 | sub_annotation_dir = base_dir + video_name + "/sub_annotation/" 27 | annotation_dir = base_dir + video_name + "/annotation/" 28 | if not os.path.exists(annotation_dir): 29 | os.makedirs(annotation_dir) 30 | frame_dir = base_dir + video_name + "/large_src/" 31 | images_prefix = list() 32 | 33 | 34 | # 统计出一张大图像中每个目标的数量 35 | # zero_padding: 文件名字的后缀是按照几个零补齐占位的 36 | def count_large_target_info(zero_padding=4): 37 | max_idx = 0 38 | for item in os.listdir(sub_annotation_dir): 39 | max_idx = max(max_idx, int(item.strip().split(".")[0].split("_")[-1])) 40 | tmp_prefix = "_".join(item.strip().split(".")[0].split("_")[:4]) + "_" 41 | if tmp_prefix not in images_prefix: 42 | images_prefix.append(tmp_prefix) 43 | print(images_prefix) 44 | 45 | targets_list = list() 46 | for idx in range(1, max_idx+1): 47 | total_target_info = dict() 48 | for prefix in images_prefix: 49 | anno_path_tpl = sub_annotation_dir + prefix + "%0"+str(zero_padding)+"d.xml" 50 | anno_path = anno_path_tpl % idx 51 | if not os.path.exists(anno_path): 52 | continue 53 | sub_target_info = tools.extract_airplane_posinfo(anno_path) 54 | total_target_info[prefix] = sub_target_info 55 | tmp = 0 56 | for key, val in total_target_info.items(): 57 | # print(key, len(val)) 58 | tmp += len(val) 59 | if tmp: 60 | targets_list.append(tmp) 61 | print(targets_list) 62 | 63 | # 将部分图像的标注数据显示在原始的图像中 64 | def show_annotation_src(anno_zero_padding=4, frame_zero_padding=6): 65 | 66 | def _prefix2position(name_prefix): 67 | ''' 68 | width, height = frame.shape[0], frame.shape[1] 69 | c_x, c_y = width // 2, height // 2 70 | [0:c_x, 0:c_y] 71 | ''' 72 | position = [int(a) for a in name_prefix.strip('_').split('_')] 73 | return position[0], position[2] 74 | 75 | max_idx = 0 76 | for item in os.listdir(sub_annotation_dir): 77 | max_idx = max(max_idx, int(item.strip().split(".")[0].split("_")[-1])) 78 | tmp_prefix = "_".join(item.strip().split(".")[0].split("_")[:4]) + "_" 79 | if tmp_prefix not in images_prefix: 80 | images_prefix.append(tmp_prefix) 81 | 82 | for idx in range(1, max_idx + 1): 83 | total_target_info = dict() 84 | for prefix in images_prefix: 85 | anno_path_tpl = sub_annotation_dir + prefix + "%0" + str( 86 | anno_zero_padding) + "d.xml" 87 | anno_path = anno_path_tpl % idx 88 | if not os.path.exists(anno_path): 89 | continue 90 | # [xmin, ymin, xmax, ymax] 91 | sub_target_info = tools.extract_airplane_posinfo(anno_path) 92 | total_target_info[prefix] = sub_target_info 93 | frame_path_tpl = frame_dir + "%0" + str(frame_zero_padding) + "d.jpg" 94 | frame_path = frame_path_tpl % idx 95 | if not os.path.exists(frame_path): 96 | continue 97 | src_image = cv2.imread(frame_path) 98 | anno_list = list() 99 | for key, val in total_target_info.items(): 100 | height_shift, width_shift = _prefix2position(key) 101 | for pos in val: 102 | new_pos = [pos[0]+width_shift, pos[1]+height_shift, 103 | pos[2]+width_shift, pos[3]+height_shift] 104 | anno_list.append(new_pos + ["plane"]) 105 | cv2.rectangle(src_image, 106 | (new_pos[0], new_pos[1]), (new_pos[2], new_pos[3]), 107 | (0, 255, 0), 2) 108 | # cv2.imwrite(base_dir+"tmp/%06d.jpg" % idx, src_image) 109 | abs_anno_path = annotation_dir + "%06d.xml" % idx 110 | print(abs_anno_path) 111 | tools.write_xml_format(frame_path, abs_anno_path, anno_list) 112 | # break 113 | 114 | import shutil 115 | 116 | # 将转换好的标准数据转换成VOC格式的数据集 117 | def copy2voc(save_prefix): 118 | if not os.path.exists(save_prefix + video_name): 119 | os.makedirs(save_prefix + video_name) 120 | if not os.path.exists(save_prefix + video_name + "/JPEGImages"): 121 | os.makedirs(save_prefix + video_name + "/JPEGImages") 122 | if not os.path.exists(save_prefix + video_name + "/Annotations"): 123 | os.makedirs(save_prefix + video_name + "/Annotations") 124 | # 根据当前的video_name来拷贝对应的图像和xml文件 125 | for anno_name in os.listdir(annotation_dir): 126 | if anno_name.startswith("._"): 127 | continue 128 | shutil.copyfile(frame_dir+anno_name.replace("xml", "jpg"), 129 | save_prefix+video_name+"/JPEGImages/"+anno_name 130 | .replace("xml", "jpg")) 131 | shutil.copyfile(annotation_dir+anno_name, 132 | save_prefix+video_name+"/Annotations/"+anno_name) 133 | print(anno_name) 134 | 135 | if __name__ == '__main__': 136 | # show_annotation_src() 137 | copy2voc("/Volumes/projects/repos/RSI/VOCFormat/") 138 | -------------------------------------------------------------------------------- /mainmodels/models/resnet/evaluate.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import time 12 | import six 13 | import sys 14 | 15 | import tensorflow as tf 16 | import numpy as np 17 | 18 | from mainmodels.dataset import cifar_input 19 | from mainmodels.models.resnet import model as resnet_model 20 | 21 | 22 | FLAGS = tf.app.flags.FLAGS 23 | tf.app.flags.DEFINE_string('dataset', 'cifar10', 'cifar10 or cifar100.') 24 | tf.app.flags.DEFINE_string('mode', 'train', 'train or eval.') 25 | tf.app.flags.DEFINE_string('train_data_path', '/Volumes/projects/TrainData/CIFAR/cifar-10-batches-bin', 26 | 'Filepattern for training data.') 27 | tf.app.flags.DEFINE_string('eval_data_path', '', 28 | 'Filepattern for eval data') 29 | tf.app.flags.DEFINE_integer('image_size', 32, 'Image side length.') 30 | tf.app.flags.DEFINE_string('train_dir', 'train', 31 | 'Directory to keep training outputs.') 32 | tf.app.flags.DEFINE_string('eval_dir', 'eval', 33 | 'Directory to keep eval outputs.') 34 | tf.app.flags.DEFINE_integer('eval_batch_count', 50, 35 | 'Number of batches to eval.') 36 | tf.app.flags.DEFINE_bool('eval_once', False, 37 | 'Whether evaluate the model only once.') 38 | tf.app.flags.DEFINE_string('log_root', '/Users/liuguiyang/Documents/CodeProj/PyProj/TinyObject/mainmodels/log/resnet', 39 | 'Directory to keep the checkpoints. Should be a ' 40 | 'parent directory of FLAGS.train_dir/eval_dir.') 41 | tf.app.flags.DEFINE_integer('num_gpus', 0, 42 | 'Number of gpus used for training. (0 or 1)') 43 | 44 | 45 | def evaluate(hps): 46 | images, labels = cifar_input.build_input( 47 | FLAGS.dataset, FLAGS.eval_data_path, hps.batch_size, FLAGS.mode) 48 | model = resnet_model.ResNet(hps, images, labels, FLAGS.mode) 49 | model.build_graph() 50 | saver = tf.train.Saver() 51 | summary_writer = tf.summary.FileWriter(FLAGS.eval_dir) 52 | 53 | sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) 54 | tf.train.start_queue_runners(sess) 55 | 56 | best_precision = 0.0 57 | while True: 58 | try: 59 | ckpt_state = tf.train.get_checkpoint_state(FLAGS.log_root) 60 | except tf.errors.OutOfRangeError as e: 61 | tf.logging.error('Cannot restore checkpoint: %s', e) 62 | continue 63 | if not (ckpt_state and ckpt_state.model_checkpoint_path): 64 | tf.logging.info('No model to eval yet at %s', FLAGS.log_root) 65 | continue 66 | tf.logging.info('Loading checkpoint %s', ckpt_state.model_checkpoint_path) 67 | saver.restore(sess, ckpt_state.model_checkpoint_path) 68 | 69 | total_prediction, correct_prediction = 0, 0 70 | for _ in range(FLAGS.eval_batch_count): 71 | (summaries, loss, predictions, truth, train_step) = sess.run( 72 | [model.summaries, model.cost, model.predictions, 73 | model.labels, model.global_step]) 74 | 75 | truth = np.argmax(truth, axis=1) 76 | predictions = np.argmax(predictions, axis=1) 77 | correct_prediction += np.sum(truth == predictions) 78 | total_prediction += predictions.shape[0] 79 | 80 | precision = 1.0 * correct_prediction / total_prediction 81 | best_precision = max(precision, best_precision) 82 | 83 | precision_summ = tf.Summary() 84 | precision_summ.value.add( 85 | tag='Precision', simple_value=precision) 86 | summary_writer.add_summary(precision_summ, train_step) 87 | best_precision_summ = tf.Summary() 88 | best_precision_summ.value.add( 89 | tag='Best Precision', simple_value=best_precision) 90 | summary_writer.add_summary(best_precision_summ, train_step) 91 | summary_writer.add_summary(summaries, train_step) 92 | tf.logging.info('loss: %.3f, precision: %.3f, best precision: %.3f' % 93 | (loss, precision, best_precision)) 94 | summary_writer.flush() 95 | 96 | if FLAGS.eval_once: 97 | break 98 | 99 | time.sleep(60) 100 | 101 | 102 | def main(_): 103 | if FLAGS.num_gpus == 0: 104 | dev = '/cpu:0' 105 | elif FLAGS.num_gpus == 1: 106 | dev = '/gpu:0' 107 | else: 108 | raise ValueError('Only support 0 or 1 gpu.') 109 | 110 | if FLAGS.mode == 'train': 111 | batch_size = 128 112 | elif FLAGS.mode == 'eval': 113 | batch_size = 100 114 | 115 | if FLAGS.dataset == 'cifar10': 116 | num_classes = 10 117 | elif FLAGS.dataset == 'cifar100': 118 | num_classes = 100 119 | 120 | hps = resnet_model.HParams(batch_size=batch_size, 121 | num_classes=num_classes, 122 | min_lrn_rate=0.0001, 123 | lrn_rate=0.1, 124 | num_residual_units=5, 125 | use_bottleneck=False, 126 | weight_decay_rate=0.0002, 127 | relu_leakiness=0.1, 128 | optimizer='mom') 129 | 130 | with tf.device(dev): 131 | if FLAGS.mode == 'eval': 132 | evaluate(hps) 133 | 134 | 135 | if __name__ == '__main__': 136 | tf.logging.set_verbosity(tf.logging.INFO) 137 | tf.app.run() -------------------------------------------------------------------------------- /mainmodels/dataset/airplane_yolo_input.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import json 13 | import codecs 14 | import random 15 | import xml.dom.minidom 16 | import cv2 17 | 18 | from mainmodels.models.ssd.settings import g_SSDConfig 19 | 20 | # 解析XML文本得到JL1ST的目标数据 21 | dir_prefix = "/Volumes/projects/第三方数据下载/JL1ST/" 22 | JL1ST_NAME = "JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS" 23 | annotation_dir = dir_prefix + "SRC_" + JL1ST_NAME + "_annotation/" 24 | image_dir = dir_prefix + "SRC_" + JL1ST_NAME + "/" 25 | ssd_sample_dir = dir_prefix + "SRC_" + JL1ST_NAME + "_YOLOv2/" 26 | # SSD_IMG_W, SSD_IMG_H = g_SSDConfig.IMG_W, g_SSDConfig.IMG_H 27 | SSD_IMG_W, SSD_IMG_H = 608, 608 28 | RANDOM_SAMPLE_NUM = 500 # 随机采样500张原始图像用来生成训练样本 29 | 30 | 31 | # 采用蓄水池采样算法对序列进行采样 32 | def rand_selected_file(file_list): 33 | res = list() 34 | for i in range(0, len(file_list)): 35 | if i < RANDOM_SAMPLE_NUM: 36 | res.append(file_list[i]) 37 | else: 38 | M = random.randint(0, i) 39 | if M < RANDOM_SAMPLE_NUM: 40 | res[M] = file_list[i] 41 | return res 42 | 43 | # 给定一个标记文件,找到对应的目标的位置信息 44 | def extract_airplane_posinfo(filename): 45 | if not os.path.exists(filename): 46 | raise IOError(filename + " not exists !") 47 | # 使用minidom解析器打开 XML 文档 48 | DOMTree = xml.dom.minidom.parse(filename) 49 | collection = DOMTree.documentElement 50 | # 获取集合中所有的目标 51 | targets = collection.getElementsByTagName("object") 52 | res = [] 53 | for target in targets: 54 | target_name = target.getElementsByTagName('name')[0].childNodes[0].data 55 | bndbox = target.getElementsByTagName("bndbox")[0] 56 | xmin = bndbox.getElementsByTagName("xmin")[0].childNodes[0].data 57 | ymin = bndbox.getElementsByTagName("ymin")[0].childNodes[0].data 58 | xmax = bndbox.getElementsByTagName("xmax")[0].childNodes[0].data 59 | ymax = bndbox.getElementsByTagName("ymax")[0].childNodes[0].data 60 | res.append([int(xmin), int(ymin), int(xmax), int(ymax), target_name]) 61 | return res 62 | 63 | 64 | # 从样本中随机裁剪出制定的大小的候选样本,这其中必须要包含相应的目标 65 | def create_ssd_training_samples(): 66 | def _crop_valid(area, anno_targets): 67 | anno_res = [] 68 | for info in anno_targets: 69 | if ((info[0] >= area[0] and info[1] >= area[1]) and 70 | (info[2] <= area[2] and info[3] <= area[3])): 71 | anno_res.append([info[0] - area[0], info[1] - area[1], 72 | info[2] - area[0], info[3] - area[1]]) 73 | if (info[0] >= area[0] and info[1] >= area[1] and 74 | info[0] < area[2] and info[1] < area[3] and 75 | (not (info[2] <= area[2] and info[3] <= area[3]))): 76 | base = (info[2] - info[0]) * (info[3] - info[1]) 77 | x_max_min = min(info[2], area[2]) 78 | y_max_min = min(info[3], area[3]) 79 | new_square = (x_max_min - info[0]) * (y_max_min - info[1]) 80 | if new_square / base >= 0.75: 81 | anno_res.append([info[0] - area[0], info[1] - area[1], 82 | x_max_min - area[0], y_max_min - area[1]]) 83 | return anno_res 84 | 85 | def _random_crop(src_image, anno_targets, save_prefix): 86 | img_height, img_width = src_image.shape[:2] 87 | crop_list, anno_list = [], [] 88 | while len(crop_list) < 10: 89 | u_x = random.randint(0, img_width - SSD_IMG_W) 90 | u_y = random.randint(0, img_height - SSD_IMG_H) 91 | area = [u_x, u_y, u_x + SSD_IMG_W, u_y + SSD_IMG_H] 92 | # 检测当前的候选框中是否包含了目标,并算出目标在给定图像的位置 93 | trans_targets = _crop_valid(area, anno_targets) 94 | if trans_targets: 95 | crop_list.append(area) 96 | anno_list.append(trans_targets) 97 | # 保存裁剪出来的结果 98 | if not os.path.isdir(ssd_sample_dir): 99 | os.makedirs(ssd_sample_dir) 100 | if not os.path.isdir(ssd_sample_dir + "src"): 101 | os.makedirs(ssd_sample_dir + "src") 102 | if not os.path.isdir(ssd_sample_dir + "annotation"): 103 | os.makedirs(ssd_sample_dir + "annotation") 104 | 105 | for i in range(len(crop_list)): 106 | x0, y0, x1, y1 = crop_list[i] 107 | # roi = im[y1:y2, x1:x2] opencv中类似NUMPY的裁剪 108 | sub_img = src_img[y0:y1, x0:x1] 109 | f_name = save_prefix + "_%d_%d_%d_%d_%d.png" % (x0, y0, x1, y1, i) 110 | cv2.imwrite(ssd_sample_dir + "src/" + f_name, sub_img) 111 | f_name = save_prefix + "_%d_%d_%d_%d_%d.json" % (x0, y0, x1, y1, i) 112 | with codecs.open( 113 | ssd_sample_dir + "annotation/" + f_name, "w") as handle: 114 | handle.write(json.dumps(anno_list[i])) 115 | print("save %s" % f_name) 116 | 117 | 118 | if not (os.path.exists(annotation_dir) and os.path.isdir(annotation_dir)): 119 | raise IOError("%s Not Found !" % annotation_dir) 120 | annotation_lists = os.listdir(annotation_dir) 121 | 122 | annotation_lists = rand_selected_file(annotation_lists) 123 | for annotation_file in annotation_lists: 124 | abs_anno_path = annotation_dir + annotation_file 125 | anno_targets = extract_airplane_posinfo(abs_anno_path) 126 | abs_src_path = image_dir + ".".join( 127 | annotation_file.split(".")[:-1] + ["png"]) 128 | if not os.path.exists(abs_src_path): 129 | continue 130 | image_name = ".".join(annotation_file.split(".")[:-1]) 131 | src_img = cv2.imread(abs_src_path) 132 | _random_crop(src_image=src_img, anno_targets=anno_targets, 133 | save_prefix=image_name) 134 | # break 135 | 136 | 137 | if __name__ == "__main__": 138 | create_ssd_training_samples() -------------------------------------------------------------------------------- /mainmodels/models/ssd/settings.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | 13 | import tensorflow as tf 14 | 15 | proj_dir = "/".join(os.path.abspath(__file__).split("/")[:-4]) 16 | print(proj_dir) 17 | 18 | class SSDConfig(object): 19 | # Model selection and dependent parameters 20 | MODEL = "AlexNet" 21 | 22 | # Default boxes 23 | # DEFAULT_BOXES = ((x1_offset, y1_offset, x2_offset, y2_offset), (...), ...) 24 | # Offset is relative to upper-left-corner and lower-right-corner of the feature map cell 25 | if MODEL == "AlexNet": 26 | DEFAULT_BOXES = ( 27 | (-0.5, -0.5, 0.5, 0.5), (0.2, 0.2, -0.2, -0.2), 28 | (-0.8, -0.2, 0.8, 0.2), (-0.2, -0.8, 0.2, 0.8) 29 | ) 30 | elif MODEL == "NWPUNet": 31 | DEFAULT_BOXES = ( 32 | (-0.5, -0.5, 0.5, 0.5), (0.2, 0.2, -0.2, -0.2), 33 | (-0.8, -0.2, 0.8, 0.2), (-0.2, -0.8, 0.2, 0.8) 34 | ) 35 | elif MODEL == "ResAlexNet": 36 | DEFAULT_BOXES = ( 37 | (-0.5, -0.5, 0.5, 0.5), 38 | (0.3, 0.3, -0.3, -0.3), 39 | (-0.8, -0.3, 0.8, 0.3), 40 | (-0.3, -0.8, 0.3, 0.8), 41 | (-0.65, -0.5, 0.35, 0.5), 42 | (0.15, 0.3, -0.45, -0.3), 43 | (-0.95, -0.3, 0.65, 0.3), 44 | (-0.45, -0.8, 0.15, 0.8), 45 | (-0.35, -0.5, 0.65, 0.5), 46 | (0.45, 0.3, -0.15, -0.3), 47 | (-0.65, -0.3, 0.95, 0.3), 48 | (-0.15, -0.8, 0.45, 0.8), 49 | (-0.5, -0.65, 0.5, 0.65), 50 | (0.3, 0.15, -0.3, -0.15), 51 | (-0.8, -0.45, 0.8, 0.45), 52 | (-0.3, -0.95, 0.3, 0.95), 53 | (-0.5, -0.35, 0.5, 0.65), 54 | (0.3, 0.45, -0.3, -0.15), 55 | (-0.8, -0.15, 0.8, 0.45), 56 | (-0.3, -0.65, 0.3, 0.95) 57 | ) 58 | else: 59 | pass 60 | NUM_DEFAULT_BOXES = len(DEFAULT_BOXES) 61 | 62 | # Constants (TODO: Keep this updated as we go along) 63 | if MODEL == "AlexNet": 64 | NUM_CLASSES = 2 # 1 signs + 1 background class 65 | elif MODEL == "NWPUNet": 66 | NUM_CLASSES = 6 # 5 signs + 1 background class 67 | elif MODEL == "ResAlexNet": 68 | NUM_CLASSES = 2 # 1 airplane + 1 background class 69 | else: 70 | raise NotImplementedError('Model not implemented') 71 | NUM_CHANNELS = 3 # grayscale->1, RGB->3 72 | NUM_PRED_CONF = NUM_DEFAULT_BOXES * NUM_CLASSES # number of class predictions per feature map cell 73 | NUM_PRED_LOC = NUM_DEFAULT_BOXES * 4 # number of localization regression predictions per feature map cell 74 | 75 | # Bounding box parameters 76 | IOU_THRESH = 0.50 # match ground-truth box to default boxes exceeding 77 | # this IOU threshold, during data prep 78 | NMS_IOU_THRESH = 0.20 # IOU threshold for non-max suppression 79 | 80 | # Negatives-to-positives ratio used to filter training data 81 | NEG_POS_RATIO = 4 # negative:positive = NEG_POS_RATIO:1 82 | 83 | # Class confidence threshold to count as detection 84 | CONF_THRESH = 0.60 85 | 86 | if MODEL == 'AlexNet': 87 | IMG_H, IMG_W = 260, 400 88 | # feature map sizes for SSD hooks via TensorBoard visualization (HxW) 89 | FM_SIZES = [[31, 48], [15, 23], [8, 12], [4, 6]] 90 | elif MODEL == "NWPUNet": 91 | IMG_H, IMG_W = 400, 600 92 | # feature map sizes for SSD hooks via TensorBoard visualization (HxW) 93 | # FM_SIZES = [[100, 150], [50, 75], [25, 38], [13, 19]] 94 | FM_SIZES = [[50, 75], [25, 37], [13, 19]] 95 | elif MODEL == "ResAlexNet": 96 | IMG_H, IMG_W = 512, 512 97 | FM_SIZES = [[64, 64], [32, 32], [16, 16], [8, 8]] 98 | else: 99 | raise NotImplementedError('Model not implemented') 100 | 101 | # Model hyper-parameters 102 | OPT = tf.train.AdadeltaOptimizer() 103 | REG_SCALE = 1e-2 # L2 regularization strength 104 | LOC_LOSS_WEIGHT = 1. # weight of localization loss: loss = conf_loss + LOC_LOSS_WEIGHT * loc_loss 105 | 106 | INIT_LEARNING_RATE = 0.005 107 | NUM_EPOCHS_PER_DECAY = 350.0 108 | LEARNING_RATE_DECAY_FACTOR = 0.1 109 | MOVING_AVERAGE_DECAY = 0.9999 110 | 111 | # Training process 112 | RESUME = False # resume training from previously saved model? 113 | NUM_EPOCH = 10000 114 | BATCH_SIZE = 20 # batch size for training (relatively small) 115 | MODEL_SAVE_FREQ = 10 # 每隔多少隔epoch进行模型的存储 116 | VALID_FREQ = 50 # 每个多少个epoch进行验证的集的测试 117 | VALIDATION_SIZE = 0.05 # fraction of total training set to use as validation set 118 | SAVE_MODEL = True # save trained model to disk? 119 | 120 | if MODEL == "AlexNet": 121 | DATASET_BASE_DIR = "/home/ai-i-liuguiyang/repos_ssd/SRC_JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS_SSD_AlexNet" 122 | DATASET_BASE_DIR = "/Volumes/projects/第三方数据下载/JL1ST/SRC_JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS_SSD_AlexNet" 123 | elif MODEL == "NWPUNet": 124 | DATASET_BASE_DIR = "/Volumes/projects/NWPU-VHR-10-dataset" 125 | elif MODEL == "ResAlexNet": 126 | DATASET_BASE_DIR = "/home/ai-i-liuguiyang/repos_ssd/SRC_JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS_SSD" 127 | else: 128 | raise NotImplementedError('Model not implemented') 129 | 130 | MODEL_SAVE_PATH = DATASET_BASE_DIR + "/model/model.ckpt" 131 | LOSS_HISTORY_PATH = DATASET_BASE_DIR + "/loss_history.pkl" 132 | TENSORBOARD_SAVE_PATH = MODEL_SAVE_PATH 133 | PRETRAIN_MODEL_PATH = MODEL_SAVE_PATH 134 | 135 | TRAIN_DATA_PRE_PATH = "/".join( 136 | [DATASET_BASE_DIR, "ssd_prepare", "train_data_prep.pkl"]) 137 | TEST_DATA_PRE_PATH = "/".join( 138 | [DATASET_BASE_DIR, "ssd_prepare", "test_data_prep.pkl"]) 139 | 140 | TRAIN_DATA_SRC_DIR = "/".join([DATASET_BASE_DIR, "ssd_src"]) 141 | TEST_DATA_SRC_DIR = TRAIN_DATA_SRC_DIR 142 | RESIZED_IMAGES_DIR = TRAIN_DATA_SRC_DIR 143 | label_sign_path = "/".join( 144 | ["/Volumes/projects/第三方数据下载/JL1ST/SRC_JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS_SSD", "target.label.json"]) 145 | 146 | g_SSDConfig = SSDConfig() 147 | -------------------------------------------------------------------------------- /mainmodels/models/ssd/inference.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/6/14 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | import os 12 | import time 13 | import json 14 | 15 | import numpy as np 16 | import tensorflow as tf 17 | import cv2 18 | from PIL import Image 19 | 20 | # import __init 21 | 22 | from mainmodels.models.ssd.settings import g_SSDConfig 23 | from mainmodels.models.ssd.ssdmodel import SSDModel 24 | from mainmodels.models.ssd.tools.NMS import nms 25 | 26 | 27 | def run_inference(image, model, sess, sign_map): 28 | image = np.array(image) 29 | image_orig = np.copy(image) 30 | 31 | # Get relevant tensors 32 | x = model['x'] 33 | is_training = model['is_training'] 34 | preds_conf = model['preds_conf'] 35 | preds_loc = model['preds_loc'] 36 | probs = model['probs'] 37 | 38 | image = Image.fromarray(image) 39 | orig_w, orig_h = image.size 40 | if g_SSDConfig.NUM_CHANNELS == 1: 41 | image = image.convert('L') 42 | image = image.resize((g_SSDConfig.IMG_W, g_SSDConfig.IMG_H), Image.LANCZOS) 43 | image = np.asarray(image) 44 | 45 | images = np.array([image]) # create a "batch" of 1 image 46 | if g_SSDConfig.NUM_CHANNELS == 1: 47 | images = np.expand_dims(images, axis=-1) 48 | 49 | # Perform object detection 50 | t0 = time.time() # keep track of duration of object detection + NMS 51 | preds_conf_val, preds_loc_val, probs_val = sess.run( 52 | [preds_conf, preds_loc, probs], 53 | feed_dict={x: images, is_training: False}) 54 | print('Inference took %.1f ms (%.2f fps)' % ( 55 | (time.time() - t0) * 1000, 1 / (time.time() - t0))) 56 | 57 | # Gather class predictions and confidence values 58 | y_pred_conf = preds_conf_val[0] # batch size of 1, so just take [0] 59 | y_pred_conf = y_pred_conf.astype('float32') 60 | prob = probs_val[0] 61 | 62 | # Gather localization predictions 63 | y_pred_loc = preds_loc_val[0] 64 | 65 | # Perform NMS 66 | boxes = nms(y_pred_conf, y_pred_loc, prob) 67 | #boxes = classify(boxes) 68 | print('Inference + NMS took %.1f ms (%.2f fps)' % ( 69 | (time.time() - t0) * 1000, 1 / (time.time() - t0))) 70 | 71 | # Rescale boxes' coordinates back to original image's dimensions 72 | # Recall boxes = [[x1, y1, x2, y2, cls, cls_prob], [...], ...] 73 | scale = np.array( 74 | [orig_w / g_SSDConfig.IMG_W, 75 | orig_h / g_SSDConfig.IMG_H, 76 | orig_w / g_SSDConfig.IMG_W, 77 | orig_h / g_SSDConfig.IMG_H]) 78 | if len(boxes) > 0: 79 | boxes[:, :4] = boxes[:, :4] * scale 80 | 81 | # print("boxes: ", boxes) 82 | # Draw and annotate boxes over original image, and return annotated image 83 | image = image_orig 84 | for box in boxes[:20]: 85 | # Get box parameters 86 | box_coords = [int(round(x)) for x in box[:4]] 87 | cls = int(box[4]) 88 | cls_prob = box[5] 89 | 90 | # Annotate image 91 | image = cv2.rectangle(image, tuple(box_coords[:2]), 92 | tuple(box_coords[2:]), (0, 255, 0)) 93 | label_str = '%s %.2f' % (sign_map[cls], cls_prob) 94 | image = cv2.putText(image, label_str, (box_coords[0], box_coords[1]), 0, 95 | 0.5, (0, 255, 0), 1, cv2.LINE_AA) 96 | 97 | return image 98 | 99 | 100 | def generate_output(input_files, options): 101 | """ 102 | Generate annotated images, videos, or sample images, based on mode 103 | """ 104 | if not os.path.exists(options.sign_file_path): 105 | raise IOError(options.sign_file_path + " not found !") 106 | # First, load mapping from integer class ID to sign name string 107 | sign_map = dict() 108 | with open(options.sign_file_path, "r") as handle: 109 | r_sign_map = json.load(handle) 110 | for key, val in r_sign_map.items(): 111 | sign_map[val] = key 112 | sign_map[0] = 'bg' # class ID 0 reserved for background class 113 | 114 | # Create output directory 'inference_out/' if needed 115 | if not os.path.isdir(options.inference_out): 116 | os.mkdir(options.inference_out) 117 | 118 | # Launch the graph 119 | with tf.Graph().as_default(), tf.Session() as sess: 120 | # "Instantiate" neural network, get relevant tensors 121 | model = SSDModel() 122 | 123 | # Load trained model 124 | saver = tf.train.Saver() 125 | print('Restoring previously trained model at %s' % 126 | g_SSDConfig.PRETRAIN_MODEL_PATH) 127 | saver.restore(sess, g_SSDConfig.PRETRAIN_MODEL_PATH) 128 | 129 | if options.mode == 'image': 130 | for image_file in input_files: 131 | print('Running inference on %s' % image_file) 132 | image_orig = np.asarray(Image.open(image_file)) 133 | image = run_inference(image_orig, model, sess, sign_map) 134 | 135 | head, tail = os.path.split(image_file) 136 | cv2.imwrite('%s/%s' % (options.inference_out, tail), image) 137 | cv2.imshow("ssd res", image) 138 | cv2.waitKey() 139 | print('Output saved in %s' % options.inference_out) 140 | 141 | 142 | if __name__ == '__main__': 143 | class RunOption(object): 144 | proj_dir = "/Volumes/projects/第三方数据下载/JL1ST" \ 145 | "/SRC_JL101B_MSS_20160904180811_000013363_101_001_L1B_MSS_SSD_AlexNet" 146 | mode = "image" 147 | sign_file_path = proj_dir + "/target.label.json" 148 | inference_out = "/".join([proj_dir, "test", "output"]) 149 | sample_images_dir = "/".join([proj_dir, "test", "src"]) 150 | 151 | options = RunOption() 152 | if options.mode not in ["image", "demo"]: 153 | raise ValueError('Invalid mode: %s' % options.mode) 154 | 155 | demo_lists = os.listdir(options.sample_images_dir) 156 | 157 | input_files = [] 158 | for item in demo_lists: 159 | if item.startswith("._"): 160 | continue 161 | if item.endswith("png") or item.endswith("jpg"): 162 | input_files.append(options.sample_images_dir+"/"+item) 163 | generate_output(input_files, options) 164 | 165 | -------------------------------------------------------------------------------- /mainmodels/dataset/LSD10/evaluate_lsd10.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2009 IW. 2 | # All rights reserved. 3 | # 4 | # Author: liuguiyang 5 | # Date: 2017/9/30 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | 12 | import os 13 | import json 14 | 15 | import cv2 16 | import numpy as np 17 | import matplotlib.pyplot as plt 18 | 19 | from mainmodels.dataset.LSD10.lsd_config import sign_idx_dict, idx_sign_dict 20 | 21 | dir_prefix = "/Volumes/projects/repos/RSI/LSD10/" 22 | total_test_image_path = dir_prefix + "all_test.txt" 23 | model_res_prefix = dir_prefix + "results/" 24 | expect_res_prefix = dir_prefix + "std_results/" 25 | 26 | model_template = "comp4_det_test_{}.txt" 27 | expect_template = "comp4_det_std_{}.txt" 28 | 29 | all_test_samples = set() 30 | with open(total_test_image_path, "r") as test_sample_reader: 31 | for line in test_sample_reader: 32 | line = line.strip() 33 | all_test_samples.add(line) 34 | 35 | 36 | def rect_cross(rect1, rect2): 37 | rect = [max(rect1[0], rect2[0]), 38 | max(rect1[1], rect2[1]), 39 | min(rect1[2], rect2[2]), 40 | min(rect1[3], rect2[3])] 41 | rect[2] = max(rect[2], rect[0]) 42 | rect[3] = max(rect[3], rect[1]) 43 | return rect 44 | 45 | 46 | def rect_area(rect): 47 | return float(max(0.0, (rect[2] - rect[0]) * (rect[3] - rect[1]))) 48 | 49 | 50 | def calc_cover(rect1, rect2): 51 | crect = rect_cross(rect1, rect2) 52 | return rect_area(crect) / rect_area(rect2) 53 | 54 | 55 | def calc_iou(rect1, rect2): 56 | crect = rect_cross(rect1, rect2) 57 | ac = rect_area(crect) 58 | a1 = rect_area(rect1) 59 | a2 = rect_area(rect2) 60 | return ac / (a1 + a2 - ac) 61 | 62 | 63 | # 计算各个纬度的统计数据 64 | # False negative; Result should have been positive, but is negative. 65 | # False positive; Result should have been negative, but is positive. 66 | # True positive; Result should have been positive and is positive. 67 | # True negative; Result should have been negative and is negative. 68 | def calc_pntf_value(expect_res_dict, model_res_dict, IOU): 69 | """ 70 | :param expect_res_dict: 标准数据集的GT的所有结果 71 | :param model_res_dict: 模型检测出来的所有结果 72 | :param IOU: 用来衡量候选框是不是真正的结果 73 | """ 74 | tp_cnt, fn_cnt, fp_cnt = 0, 0, 0 75 | for image_name in model_res_dict.keys(): 76 | if image_name in expect_res_dict.keys(): 77 | # 是否需要考虑已经匹配过的框的位置信息? 78 | detected_box_idx = list() 79 | for i in range(len(expect_res_dict[image_name])): 80 | gt_box = expect_res_dict[image_name][i]["pos"] 81 | max_iou, max_idx, is_matched = 0.0, -1, False 82 | for j in range(len(model_res_dict[image_name])): 83 | if j not in detected_box_idx: 84 | md_box = model_res_dict[image_name][j]["pos"] 85 | md_iou = calc_iou(md_box, gt_box) 86 | if md_iou >= IOU: 87 | # print(md_iou) 88 | is_matched = True 89 | max_iou = max(max_iou, md_iou) 90 | if max_iou == md_iou: 91 | max_idx = j 92 | if is_matched: 93 | detected_box_idx.append(max_idx) 94 | tp_cnt += 1 95 | else: 96 | fn_cnt += 1 97 | fp_cnt += len(model_res_dict[image_name]) - len(detected_box_idx) 98 | # print("tp: {}, fp: {}, fn: {}".format( 99 | # tp_cnt, fp_cnt, fn_cnt)) 100 | # print("Precision=TP/(TP+FP)={}, Recall=TP/(TP+FN)={}".format( 101 | # 0 if tp_cnt == 0 else tp_cnt/(tp_cnt+fp_cnt), 102 | # 0 if tp_cnt == 0 else tp_cnt/(tp_cnt+fn_cnt))) 103 | precision = 0 if tp_cnt == 0 else tp_cnt/(tp_cnt+fp_cnt) 104 | recall = 0 if tp_cnt == 0 else tp_cnt/(tp_cnt+fn_cnt) 105 | return precision, recall 106 | 107 | 108 | # 根据模型产生的结果,使用相应的评价标准进行评估 109 | def evaluate_test_results(sign_name="ps", prob_limit=0.6, iou_limit=0.4): 110 | """ 111 | :param sign_name: 交通标志的名字 112 | :param prob_limit: 给出的识别概率的下限 113 | :param iou_limit: GT和Box之间的IOU的下限 114 | """ 115 | model_sign_name = model_template.format(sign_name) 116 | expect_sign_name = expect_template.format(sign_name) 117 | model_res_path = model_res_prefix + model_sign_name 118 | expect_res_path = expect_res_prefix + expect_sign_name 119 | # 开始组织模型的测试结果和GT结果 120 | model_res_dict = dict() 121 | with open(model_res_path, "r") as model_reader: 122 | for line in model_reader: 123 | line = line.strip().split(" ") 124 | prob = float(line[1]) 125 | if prob < prob_limit: 126 | continue 127 | if line[0] not in model_res_dict.keys(): 128 | model_res_dict[line[0]] = list() 129 | tmp_dict = { 130 | "prob": prob, 131 | "pos": [float(a) for a in line[2:]] 132 | } 133 | model_res_dict[line[0]].append(tmp_dict) 134 | expect_res_dict = dict() 135 | with open(expect_res_path, "r") as expect_reader: 136 | for line in expect_reader: 137 | line = line.strip().split(" ") 138 | if line[0] not in expect_res_dict.keys(): 139 | expect_res_dict[line[0]] = list() 140 | tmp_dict = { 141 | "prob": 1.0, 142 | "pos": [int(a) for a in line[2:]] 143 | } 144 | expect_res_dict[line[0]].append(tmp_dict) 145 | precision, recall = calc_pntf_value( 146 | expect_res_dict, model_res_dict, iou_limit) 147 | return precision, recall 148 | 149 | detail_dict = dict() 150 | for _, sign_idx in sign_idx_dict.items(): 151 | sign_name = idx_sign_dict[sign_idx] 152 | detail_dict[sign_name] = list() 153 | for iou in np.linspace(0, 1, 9): 154 | for prob in np.linspace(0, 1, 9): 155 | precision, recall = evaluate_test_results( 156 | sign_name=sign_name, prob_limit=prob, iou_limit=iou) 157 | if iou == 0.625 and prob == 0.75: 158 | print(sign_name, prob, iou, precision, recall) 159 | detail_dict[sign_name].append([iou, prob, precision, recall]) 160 | print(detail_dict) 161 | --------------------------------------------------------------------------------