def main(): fcnxs = symbol_fcnxs.get_fcn32s_symbol(numclass=2, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN32s_VGG16" num_epoch = 1 learning_rate = 1e-3 if args.model == "fcn16s": fcnxs = symbol_fcnxs.get_fcn16s_symbol(numclass=2, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN16s_VGG16" learning_rate = 1e-5 elif args.model == "fcn8s": fcnxs = symbol_fcnxs.get_fcn8s_symbol(numclass=2, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN8s_VGG16" num_epoch = 30 learning_rate = 1e-7 arg_names = fcnxs.list_arguments() _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(args.prefix, args.epoch) if not args.retrain: if args.init_type == "vgg16": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_vgg16(ctx, fcnxs, fcnxs_args, fcnxs_auxs) elif args.init_type == "fcnxs": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_fcnxs(ctx, fcnxs, fcnxs_args, fcnxs_auxs) train_dataiter = FileIter( root_dir = ".", flist_name = "train.lst", # cut_off_size = 400, rgb_mean = (123.68, 116.779, 103.939), ) # val_dataiter = FileIter( # root_dir = "/home/zw/dataset/VOC2012Segmentation/VOC2012", # flist_name = "val.lst", # rgb_mean = (123.68, 116.779, 103.939), # ) model = Solver( ctx = ctx, symbol = fcnxs, begin_epoch = 0, num_epoch = num_epoch, arg_params = fcnxs_args, aux_params = fcnxs_auxs, learning_rate = learning_rate, momentum = 0.9, wd = 0.0001) model.fit( train_data = train_dataiter, # eval_data = val_dataiter, batch_end_callback = mx.callback.Speedometer(1, 10), epoch_end_callback = mx.callback.do_checkpoint(fcnxs_model_prefix))
def main(): fcnxs = symbol_fcnxs.get_fcn32s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN32s_VGG16" if args.model == "fcn16s": fcnxs = symbol_fcnxs.get_fcn16s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN16s_VGG16" elif args.model == "fcn8s": fcnxs = symbol_fcnxs.get_fcn8s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN8s_VGG16" arg_names = fcnxs.list_arguments() _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(args.prefix, args.epoch) # print arg_names if not args.retrain: if args.init_type == "vgg16": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_vgg16(ctx, fcnxs, fcnxs_args, fcnxs_auxs) elif args.init_type == "fcnxs": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_fcnxs(ctx, fcnxs, fcnxs_args, fcnxs_auxs) train_dataiter = FileIter( root_dir = "./VOC2012", flist_name = "train.lst", # cut_off_size = 400, rgb_mean = (123.68, 116.779, 103.939), ) val_dataiter = FileIter( root_dir = "./VOC2012", flist_name = "val.lst", rgb_mean = (123.68, 116.779, 103.939), ) model = Solver( ctx = ctx, symbol = fcnxs, begin_epoch = 0, num_epoch = 50, arg_params = fcnxs_args, aux_params = fcnxs_auxs, learning_rate = 1e-10, momentum = 0.99, wd = 0.0005) model.fit( train_data = train_dataiter, eval_data = val_dataiter, batch_end_callback = mx.callback.Speedometer(1, 10), epoch_end_callback = mx.callback.do_checkpoint(fcnxs_model_prefix))
def main(): symbol = symbol_fcnxs.get_fcn8s_symbol(numclass=21, workspace_default=1536) _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(model_previx, epoch) mod = mx.mod.Module(symbol, context=ctx) #data_shape = fcnxs_args["data"].shape data_shape = (1, 3, 800, 800) mod.bind(data_shapes=[('data', (1, 3, data_shape[2], data_shape[3]))]) mod.set_params(fcnxs_args, fcnxs_auxs) start = timer() data = get_data(img, data_shape[2], data_shape[3]) mod.forward(Batch([mx.nd.array(data)])) output = mod.get_outputs()[0] out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1))) time_elapsed = timer() - start out_img = Image.fromarray(out_img) out_img.putpalette(pallete) out_img.save(seg) print "Detection time: {:.6f} sec".format(time_elapsed)
def main(): ctx = mx.cpu() if not args.gpu else mx.gpu(args.gpu) fcnxs = symbol_fcnxs.get_fcn32s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN32s_VGG16" if args.model == "fcn16s": fcnxs = symbol_fcnxs.get_fcn16s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN16s_VGG16" elif args.model == "fcn8s": fcnxs = symbol_fcnxs.get_fcn8s_symbol(numclass=21, workspace_default=1536) fcnxs_model_prefix = "model_pascal/FCN8s_VGG16" arg_names = fcnxs.list_arguments() _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(args.prefix, args.epoch) if not args.retrain: if args.init_type == "vgg16": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_vgg16(ctx, fcnxs, fcnxs_args, fcnxs_auxs) elif args.init_type == "fcnxs": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_fcnxs(ctx, fcnxs, fcnxs_args, fcnxs_auxs) train_dataiter = FileIter( root_dir = "./VOC2012", flist_name = "train.lst", # cut_off_size = 400, rgb_mean = (123.68, 116.779, 103.939), ) val_dataiter = FileIter( root_dir = "./VOC2012", flist_name = "val.lst", rgb_mean = (123.68, 116.779, 103.939), ) model = Solver( ctx = ctx, symbol = fcnxs, begin_epoch = 0, num_epoch = 50, arg_params = fcnxs_args, aux_params = fcnxs_auxs, learning_rate = 1e-10, momentum = 0.99, wd = 0.0005) model.fit( train_data = train_dataiter, eval_data = val_dataiter, batch_end_callback = mx.callback.Speedometer(1, 10), epoch_end_callback = mx.callback.do_checkpoint(fcnxs_model_prefix))
def main(): symbol = symbol_fcnxs.get_fcn8s_symbol(numclass=21, workspace_default=1536) _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(model_previx, epoch) mod = mx.mod.Module(symbol, context=ctx) #data_shape = fcnxs_args["data"].shape #data_shape = (1, 3, 800, 800) # get size of video capture video = cv2.VideoCapture(0) width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5) height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5) if width<800 and height<800: data_shape = (height, width) elif width<height: data_shape = (800, int(800.0*width/height)) else: data_shape = (int(800.0*height/width), 800) print data_shape mod.bind(data_shapes=[('data', (1,3,data_shape[0], data_shape[1]))]) mod.set_params(fcnxs_args, fcnxs_auxs) count = 0 while count<1000: _, img = video.read() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) start = timer() data = get_data(img, data_shape[0], data_shape[1]) mod.forward(Batch([mx.nd.array(data)])) output = mod.get_outputs()[0] out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1))) time_elapsed = timer() - start out_img = Image.fromarray(out_img) out_img.putpalette(pallete) fname = 'data/'+str(count).zfill(7)+'.png' out_img.save(fname) print "Detection time: {:.6f} sec".format(time_elapsed) count += 1
def main(): devs = mx.cpu() if args.gpus is None or args.gpus is '' else [ mx.gpu(int(i)) for i in args.gpus.split(',') ] carvn_root = '' num_classes = 2 cutoff = None if args.cutoff == 0 else args.cutoff epochs = [74, 2, 1, 1] if not os.path.exists(args.model_dir): os.mkdir(args.model_dir) model_prefixes = [ 'VGG_FC_ILSVRC_16_layers', args.model_dir + "/FCN32s_VGG16", args.model_dir + "/FCN16s_VGG16", args.model_dir + "/FCN8s_VGG16" ] if args.model == "fcn16s": fcnxs = symbol_fcnxs.get_fcn16s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[2] load_prefix = model_prefixes[1] lr = 1e-5 run_epochs = epochs[2] load_epoch = epochs[1] elif args.model == "fcn8s": fcnxs = symbol_fcnxs.get_fcn8s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[3] load_prefix = model_prefixes[2] lr = 1e-6 run_epochs = epochs[3] load_epoch = epochs[2] else: fcnxs = symbol_fcnxs.get_fcn32s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[1] load_prefix = model_prefixes[0] lr = 1e-4 run_epochs = epochs[1] load_epoch = epochs[0] arg_names = fcnxs.list_arguments() print('loading', load_prefix, load_epoch) print('lr', lr) print('model_prefix', fcnxs_model_prefix) print('running epochs', run_epochs) print('cutoff size', cutoff) args.batch_size = len(devs) _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint( load_prefix, load_epoch) ctx = mx.cpu() if not args.retrain: if args.model == "fcn16s" or args.model == "fcn8s": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_fcnxs( ctx, fcnxs, fcnxs_args, fcnxs_auxs) else: fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_vgg16( ctx, fcnxs, fcnxs_args, fcnxs_auxs) train_dataiter = BatchFileIter( path_imglist="../data/train.lst", cut_off_size=cutoff, rgb_mean=(123.68, 116.779, 103.939), batch_size=args.batch_size, ) val_dataiter = BatchFileIter( path_imglist="../data/val.lst", cut_off_size=cutoff, rgb_mean=(123.68, 116.779, 103.939), batch_size=args.batch_size, ) # learning rate kv = mx.kvstore.create('local') # create model model = mx.mod.Module( context=devs, symbol=fcnxs, #label_names = ['softmax_label', 'softmax2_label'] ) optimizer_params = {'learning_rate': lr, 'momentum': 0.99, 'wd': 0.0005} _dice = DiceMetric() _acc = AccMetric() eval_metrics = [mx.metric.create(_dice), mx.metric.create(_acc)] initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2) model.fit( train_dataiter, begin_epoch=0, num_epoch=run_epochs, #eval_data = val_dataiter, eval_metric=eval_metrics, kvstore=kv, optimizer='sgd', optimizer_params=optimizer_params, initializer=initializer, arg_params=fcnxs_args, aux_params=fcnxs_auxs, batch_end_callback=mx.callback.Speedometer(args.batch_size, 10), epoch_end_callback=mx.callback.do_checkpoint(fcnxs_model_prefix), allow_missing=True)
def main(): gpu_list = [] _gpus = args.gpu.split(',') for _gpu in _gpus: _gpu = _gpu.strip() if len(_gpu)==0: continue gpu_list.append(int(_gpu)) assert len(gpu_list)>0 ctx = mx.gpu(gpu_list[0]) carvn_root = '' num_classes = 2 cutoff = None if args.cutoff==0 else args.cutoff epochs = [74,31,27,19] model_prefixes = ['VGG_FC_ILSVRC_16_layers', args.model_dir+"/FCN32s_VGG16", args.model_dir+"/FCN16s_VGG16", args.model_dir+"/FCN8s_VGG16"] if args.model == "fcn16s": fcnxs = symbol_fcnxs.get_fcn16s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[2] load_prefix = model_prefixes[1] lr = 1e-5 run_epochs = epochs[2] load_epoch = epochs[1] elif args.model == "fcn8s": fcnxs = symbol_fcnxs.get_fcn8s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[3] load_prefix = model_prefixes[2] lr = 1e-6 run_epochs = epochs[3] load_epoch = epochs[2] else: fcnxs = symbol_fcnxs.get_fcn32s_symbol(numclass=num_classes, workspace_default=1536) fcnxs_model_prefix = model_prefixes[1] load_prefix = model_prefixes[0] lr = 1e-4 run_epochs = epochs[1] load_epoch = epochs[0] arg_names = fcnxs.list_arguments() print('loading', load_prefix, load_epoch) print('lr', lr) print('model_prefix', fcnxs_model_prefix) print('running epochs', run_epochs) _, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(load_prefix, load_epoch) if not args.retrain: if args.model == "fcn16s" or args.model == "fcn8s": fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_fcnxs(ctx, fcnxs, fcnxs_args, fcnxs_auxs) else: fcnxs_args, fcnxs_auxs = init_fcnxs.init_from_vgg16(ctx, fcnxs, fcnxs_args, fcnxs_auxs) train_dataiter = FileIter( root_dir = carvn_root, flist_name = "../data/train.lst", cut_off_size = cutoff, rgb_mean = (123.68, 116.779, 103.939), ) val_dataiter = FileIter( root_dir = carvn_root, flist_name = "../data/val.lst", cut_off_size = cutoff, rgb_mean = (123.68, 116.779, 103.939), ) model = Solver( ctx = ctx, symbol = fcnxs, begin_epoch = 0, num_epoch = run_epochs, arg_params = fcnxs_args, aux_params = fcnxs_auxs, learning_rate = lr, momentum = 0.99, wd = 0.0005) _metric = DiceMetric() model.fit( train_data = train_dataiter, eval_data = val_dataiter, eval_metric = _metric, batch_end_callback = mx.callback.Speedometer(1, 10), epoch_end_callback = mx.callback.do_checkpoint(fcnxs_model_prefix))