def main(): args = Parameters().parse() np.random.seed(args.random_seed) torch.manual_seed(args.random_seed) torch.cuda.manual_seed_all(args.random_seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = True torch.backends.cudnn.enabled = True # Dataset datasets = create_datasets(args) # Network net = create_network(args) # Loss Function criterion = create_lossfunc(args, net) # optimizer and parameters optim_params = create_params(args, net) optimizer = create_optimizer(args, optim_params) # learning rate scheduler scheduler = create_scheduler(args, optimizer, datasets) if args.mode == 'train': train(args, net, datasets, criterion, optimizer, scheduler) return if args.mode == 'test': test(args, net, datasets) return
def main(): t1 = time() # load hyperparams params = Parameters() # load dataset print('loading dataset') dataset = Sub_COCO(params) print('data num:{}'.format(len(dataset))) # x:[b, 3, 224, 224] # y:[b,] dataloader = DataLoader(dataset, batch_size=params.batch_size, shuffle=False) # load model model = load_model(params).cuda().eval() # run model correct_num = 0.0 total_num = 0.0 print('starting') for (x, y) in dataloader: x, y = x.cuda(), y.cuda() logits = model(x) correct_num += y.int().eq(torch.argmax(logits, dim=1)).sum().float() total_num += y.size(0) accuracy = correct_num / total_num print(accuracy) t2 = time() print('it costs {} s'.format(t2 - t1))
def test(): params = Parameters() print('reading logs') log_path = os.path.join(params.model_logs_dir, 'cider_log_' + str(params.topic_num) + '.txt') print(os.path.exists(log_path)) log = np.loadtxt(log_path) index = np.argmax(log[:, 1].tolist()) print('loading model') checkpoint = os.path.join(params.model_dir, str(index) + '.pkl') print(os.path.exists(checkpoint))
def initGui(self): """Create the menu entries and toolbar icons inside the QGIS GUI.""" self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockWidgetAnnotation) icon_path = ':/plugins/CoconutTreesDetection/icon.png' self.add_action(icon_path, text=self.tr(u'coconutTreesDetection'), callback=self.run, parent=self.iface.mainWindow()) # imgFilename = self.iface.activeLayer().dataProvider().dataSourceUri() self.imgFilename = Parameters.rgb_image_clipped_tif self.layer = self.getLayerByName(Parameters.rgb_image_layername) self.windowArrayList = list() # self.imgArray = cv2.imread(self.imgFilename) self.imgArray = gdal.Open(self.imgFilename).ReadAsArray().astype( np.uint8) self.imgArray = np.transpose(self.imgArray, (1, 2, 0)) self.bovwTrainingFeatures = None self.labelTrainingArray = None self.predicted_probs = None self.pred_test_labels = None self.windowsCentersList = list() self.windowPositiveIndexList = list() self.windowNegativeIndexList = list() self.config = Parameters(self.layer) self.config.readRasterConfig() self.canvasClicked = ClickTool(self.config, self.canvas, self.layer, self.imgArray) self.canvas.setMapTool(self.canvasClicked) self.uiDockWidgetAnnotation.btnLoadAnnotationFile.clicked.connect( self.loadAnnotationsAndDisplay) self.uiDockWidgetAnnotation.btnSaveAnnotationFile.clicked.connect( self.saveAnnotationFile) self.uiDockWidgetAnnotation.btnAddAnnotationCoco.clicked.connect( self.addAnnotationsCoco) self.uiDockWidgetAnnotation.btnDeleteAnnotation.clicked.connect( self.deleteAnnotation) self.uiDockWidgetAnnotation.btnClassify.clicked.connect(self.classify) self.uiDockWidgetAnnotation.btnPreprocess.clicked.connect( self.preprocess) self.uiDockWidgetAnnotation.btnAddAnnotationNoncoco.clicked.connect( self.addAnnotationsNoncoco) self.uiDockWidgetAnnotation.btnDeleteAllAnnotation.clicked.connect( self.deleteAllAnnnotaions) self.uiDockWidgetAnnotation.btnVisualize.clicked.connect( self.tsneVisualization) self.uiDockWidgetAnnotation.btnTest.clicked.connect(self.calRecall) self.uiDockWidgetAnnotation.btnValidate.clicked.connect(self.validate)
from dataset import get_segmentation_dataset from network import get_segmentation_model from config import Parameters import random import timeit import logging import pdb from tqdm import tqdm from tensorboardX import SummaryWriter from utils.criterion import CriterionCrossEntropy, CriterionDSN, CriterionOhemDSN, CriterionOhemDSN_single from utils.parallel import DataParallelModel, DataParallelCriterion start = timeit.default_timer() args = Parameters().parse() # file_log = open(args.log_file, "w") # sys.stdout = sys.stderr = file_log def lr_poly(base_lr, iter, max_iter, power): return base_lr*((1-float(iter)/max_iter)**(power)) def adjust_learning_rate(optimizer, i_iter): lr = lr_poly(args.learning_rate, i_iter, args.num_steps, args.power) optimizer.param_groups[0]['lr'] = lr return lr def main():
import tensorflow as tf from glob import glob from processors import SimpleDataGenerator from readers import KittiDataReader from config import Parameters from network import build_point_pillar_graph DATA_ROOT = "../validation_small" MODEL_ROOT = "./logs" # os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # os.environ["CUDA_VISIBLE_DEVICES"]="0" if __name__ == "__main__": params = Parameters() pillar_net = build_point_pillar_graph(params) pillar_net.load_weights(os.path.join(MODEL_ROOT, "model.h5")) pillar_net.summary() data_reader = KittiDataReader() lidar_files = sorted(glob(os.path.join(DATA_ROOT, "velodyne", "*.bin"))) label_files = sorted(glob(os.path.join(DATA_ROOT, "label_2", "*.txt"))) calibration_files = sorted(glob(os.path.join(DATA_ROOT, "calib", "*.txt"))) eval_gen = SimpleDataGenerator(data_reader, params.batch_size, lidar_files, label_files, calibration_files) pred = pillar_net.predict(eval_gen)
def main(): """Create the model and start the evaluation process.""" args = Parameters().parse() # # # args.method = 'student_res18_pre' args.method = 'student_esp_d' args.dataset = 'camvid_light' args.data_list = "/ssd/yifan/SegNet/CamVid/test.txt" args.data_dir = "/ssd/yifan/" args.num_classes = 11 # args.method='psp_dsn_floor' args.restore_from = "./checkpoint/Camvid/ESP/base_57.8.pth" # args.restore_from="/teamscratch/msravcshare/v-yifan/ESPNet/train/0.4results_enc_01_enc_2_8/model_298.pth" # args.restore_from = "/teamscratch/msravcshare/v-yifacd n/sd_pytorch0.5/checkpoint/snapshots_psp_dsn_floor_1e-2_40000_TEACHER864/CS_scenes_40000.pth" # args.restore_from = "/teamscratch/msravcshare/v-yifan/sd_pytorch0.5/checkpoint/snapshots_psp_dsn_floor_1e-2_40000_TEACHER5121024_esp/CS_scenes_40000.pth" # args.data_list = '/teamscratch/msravcshare/v-yifan/deeplab_v3/dataset/list/cityscapes/train.lst' args.batch_size = 1 print("Input arguments:") for key, val in vars(args).items(): print("{:16} {}".format(key, val)) h, w = map(int, args.input_size.split(',')) input_size = (h, w) print(args) output_path = args.output_path if not os.path.exists(output_path): os.makedirs(output_path) # args.method='psp_dsn' deeplab = get_segmentation_model(args.method, num_classes=args.num_classes) ignore_label = 255 id_to_trainid = { -1: ignore_label, 0: ignore_label, 1: ignore_label, 2: ignore_label, 3: ignore_label, 4: ignore_label, 5: ignore_label, 6: ignore_label, 7: 0, 8: 1, 9: ignore_label, 10: ignore_label, 11: 2, 12: 3, 13: 4, 14: ignore_label, 15: ignore_label, 16: ignore_label, 17: 5, 18: ignore_label, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14, 28: 15, 29: ignore_label, 30: ignore_label, 31: 16, 32: 17, 33: 18 } os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu # args.restore_from="/teamscratch/msravcshare/v-yifan/sd_pytorch0.3/checkpoint/snapshots_resnet_psp_dsn_1e-4_5e-4_8_20000_DSN_0.4_769light/CS_scenes_20000.pth" # if 'dense' in args.method: # if args.restore_from is not None: saved_state_dict = torch.load(args.restore_from) c_keys = saved_state_dict.keys() for i in c_keys: flag = i.split('.')[0] if 'module' in flag: deeplab = nn.DataParallel(deeplab) deeplab.load_state_dict(saved_state_dict) if 'module' not in flag: deeplab = nn.DataParallel(deeplab) # if 'dense' not in args.method: # deeplab = nn.DataParallel(deeplab) model = deeplab model.eval() model.cuda() # args.dataset='cityscapes_light' testloader = data.DataLoader(get_segmentation_dataset( args.dataset, root=args.data_dir, list_path=args.data_list, crop_size=(360, 480), mean=IMG_MEAN, scale=False, mirror=False), batch_size=args.batch_size, shuffle=False, pin_memory=True) data_list = [] confusion_matrix = np.zeros((args.num_classes, args.num_classes)) palette = get_palette(20) image_id = 0 for index, batch in enumerate(testloader): if index % 100 == 0: print('%d processd' % (index)) if args.side: image, label, _, size, name = batch elif 'sd' in args.dataset: _, image, label, size, name = batch else: image, label, size, name = batch # print('image name: {}'.format(name)) size = size[0].numpy() output = predict_esp(model, image) # seg_pred = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) result = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) # result=cv2.resize(result, (1024, 1024), interpolation=cv2.INTER_NEAREST) m_seg_pred = ma.masked_array(result, mask=torch.eq(label, 255)) ma.set_fill_value(m_seg_pred, 20) seg_pred = m_seg_pred for i in range(image.size(0)): image_id += 1 print('%d th segmentation map generated ...' % (image_id)) args.store_output = 'True' output_path = './esp_camvid_base/' if not os.path.exists(output_path): os.mkdir(output_path) if args.store_output == 'True': # print('a') output_im = PILImage.fromarray(seg_pred[i]) output_im.putpalette(palette) output_im.save(output_path + '/' + name[i] + '.png') seg_gt = np.asarray(label.numpy()[:, :size[0], :size[1]], dtype=np.int) ignore_index = seg_gt != 255 seg_gt = seg_gt[ignore_index] seg_pred = seg_pred[ignore_index] confusion_matrix += get_confusion_matrix(seg_gt, seg_pred, args.num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) IU_array = (tp / np.maximum(1.0, pos + res - tp)) mean_IU = IU_array.mean() print({'meanIU': mean_IU, 'IU_array': IU_array}) print("confusion matrix\n") print(confusion_matrix)
def main(): """Create the model and start the evaluation process.""" args = Parameters().parse() # file_log = open(args.log_file, "w") # sys.stdout = sys.stderr = file_log print("Input arguments:") sys.stdout.flush() for key, val in vars(args).items(): print("{:16} {}".format(key, val)) h, w = map(int, args.input_size.split(',')) input_size = (h, w) output_path = args.output_path if not os.path.exists(output_path): os.makedirs(output_path) deeplab = get_segmentation_model("_".join([args.network, args.method]), num_classes=args.num_classes) ignore_label = 255 id_to_trainid = { -1: ignore_label, 0: ignore_label, 1: ignore_label, 2: ignore_label, 3: ignore_label, 4: ignore_label, 5: ignore_label, 6: ignore_label, 7: 0, 8: 1, 9: ignore_label, 10: ignore_label, 11: 2, 12: 3, 13: 4, 14: ignore_label, 15: ignore_label, 16: ignore_label, 17: 5, 18: ignore_label, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14, 28: 15, 29: ignore_label, 30: ignore_label, 31: 16, 32: 17, 33: 18 } os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu import urllib.request local_checkpoint, _ = urllib.request.urlretrieve( 'http://sceneparsing.csail.mit.edu/model/pretrained_resnet/resnet101-imagenet.pth', 'resnet101-imagenet.pth') saved_state_dict = torch.load(local_checkpoint) deeplab.load_state_dict(saved_state_dict) model = nn.DataParallel(deeplab) model.eval() model.cuda() testloader = data.DataLoader(get_segmentation_dataset( args.dataset, root=args.data_dir, list_path=args.data_list, crop_size=(1024, 2048), scale=False, mirror=False, network=args.network), batch_size=args.batch_size, shuffle=False, pin_memory=True) data_list = [] confusion_matrix = np.zeros((args.num_classes, args.num_classes)) palette = get_palette(20) image_id = 0 for index, batch in enumerate(testloader): if index % 100 == 0: print('%d processd' % (index)) sys.stdout.flush() image, label, size, name = batch size = size[0].numpy() if torch_ver == '0.3': if args.use_ms == 'True': output = predict_multi_scale(model, image.numpy(), ([0.75, 1, 1.25]), input_size, args.num_classes, args.use_flip, args.method) else: if args.use_flip == 'True': output = predict_multi_scale(model, image.numpy(), ([args.whole_scale]), input_size, args.num_classes, args.use_flip, args.method) else: if 'gt' in args.method: label = Variable(label.long().cuda()) output = predict_whole_img_w_label( model, image.numpy(), args.num_classes, args.method, scale=float(args.whole_scale), label=label) else: output = predict_whole_img(model, image.numpy(), args.num_classes, args.method, scale=float( args.whole_scale)) else: with torch.no_grad(): if args.use_ms == 'True': output = predict_multi_scale(model, image.numpy(), ([0.75, 1, 1.25]), input_size, args.num_classes, args.use_flip, args.method) else: if args.use_flip == 'True': output = predict_multi_scale(model, image.numpy(), ([args.whole_scale]), input_size, args.num_classes, args.use_flip, args.method) else: if 'gt' in args.method: output = predict_whole_img_w_label( model, image.numpy(), args.num_classes, args.method, scale=float(args.whole_scale), label=Variable(label.long().cuda())) else: output = predict_whole_img(model, image.numpy(), args.num_classes, args.method, scale=float( args.whole_scale)) seg_pred = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) m_seg_pred = ma.masked_array(seg_pred, mask=torch.eq(label, 255)) ma.set_fill_value(m_seg_pred, 20) seg_pred = m_seg_pred for i in range(image.size(0)): image_id += 1 print('%d th segmentation map generated ...' % (image_id)) sys.stdout.flush() if args.store_output == 'True': output_im = PILImage.fromarray(seg_pred[i]) output_im.putpalette(palette) output_im.save(output_path + '/' + name[i] + '.png') seg_gt = np.asarray(label.numpy()[:, :size[0], :size[1]], dtype=np.int) ignore_index = seg_gt != 255 seg_gt = seg_gt[ignore_index] seg_pred = seg_pred[ignore_index] confusion_matrix += get_confusion_matrix(seg_gt, seg_pred, args.num_classes) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) tp = np.diag(confusion_matrix) IU_array = (tp / np.maximum(1.0, pos + res - tp)) mean_IU = IU_array.mean() print({'meanIU': mean_IU, 'IU_array': IU_array}) print("confusion matrix\n") print(confusion_matrix) sys.stdout.flush()
def main(): """Create the model and start the evaluation process.""" args = Parameters().parse() # file_log = open(args.log_file, "w") # sys.stdout = sys.stderr = file_log print("Input arguments:") for key, val in vars(args).items(): print("{:16} {}".format(key, val)) sys.stdout.flush() h, w = map(int, args.input_size.split(',')) input_size = (h, w) ignore_label = args.ignore_label output_path = args.output_path if not os.path.exists(output_path): os.makedirs(output_path) deeplab = get_segmentation_model("_".join([args.network, args.method]), num_classes=args.num_classes) os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu saved_state_dict = torch.load(args.restore_from) deeplab.load_state_dict(saved_state_dict) model = nn.DataParallel(deeplab) model.eval() model.cuda() testloader = data.DataLoader(get_segmentation_dataset( args.dataset, root=args.data_dir, list_path=args.data_list, crop_size=(1024, 2048), scale=False, mirror=False, network=args.network), batch_size=args.batch_size, shuffle=False, pin_memory=True) data_list = [] confusion_matrix = np.zeros((args.num_classes, args.num_classes)) palette = get_palette(256) id_to_trainid = { -1: ignore_label, 0: ignore_label, 1: ignore_label, 2: ignore_label, 3: ignore_label, 4: ignore_label, 5: ignore_label, 6: ignore_label, 7: 0, 8: 1, 9: ignore_label, 10: ignore_label, 11: 2, 12: 3, 13: 4, 14: ignore_label, 15: ignore_label, 16: ignore_label, 17: 5, 18: ignore_label, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14, 28: 15, 29: ignore_label, 30: ignore_label, 31: 16, 32: 17, 33: 18 } image_id = 0 for index, batch in enumerate(testloader): if index % 100 == 0: print('%d processd' % (index)) image, size, name = batch size = size[0].numpy() if torch_ver == '0.3': if args.use_ms == 'True': output = predict_multi_scale(model, image.numpy(), ([0.75, 1, 1.25]), args.num_classes, args.use_flip, args.method) else: output = predict_whole_img(model, image.numpy(), args.num_classes, args.method, scale=float(args.whole_scale)) else: with torch.no_grad(): if args.use_ms == 'True': output = predict_multi_scale(model, image.numpy(), ([0.75, 1, 1.25]), args.num_classes, args.use_flip, args.method) else: output = predict_whole_img(model, image.numpy(), args.num_classes, args.method, scale=float(args.whole_scale)) seg_pred = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) seg_pred = id2trainId(seg_pred, id_to_trainid, reverse=True) for i in range(image.size(0)): image_id += 1 print('%d th segmentation map generated ...' % (image_id)) sys.stdout.flush() if args.store_output == 'True': output_im = PILImage.fromarray(seg_pred[i]) output_im.putpalette(palette) output_im.save(output_path + '/' + name[i] + '.png')
def main(): args = Parameters().parse() model = get_segmentation_model("_".join([args.network, args.method]), num_classes=20) cost_summary(model=model.cuda(), input_size=(3, 1024, 2048))
from torch.autograd import Variable import torch.optim as optim import numpy as np import os import math import datetime from tqdm import tqdm import sys sys.path.append(os.getcwd()) from data_process import DataProcess sys.path.append(os.path.abspath('..')) from config import Parameters param = Parameters() """ B batch_size L max_len D dimension = d_mode d dimension = d_q, d_k, d_v H heads """ # 一些必须的工具函数类 class Tools(): def __init__(self): pass def str_2_list(self, tgt_seq):