def run(): num_classes = 3 image_shape = IMG_SIZE data_dir = TRAINING_DIR runs_dir = './runs' epochs = 1 batch_size = 1 learning_rate=1e-5 net_input = tf.placeholder( tf.float32,shape=[None,image_shape[0], image_shape[1],3], name="net_input") net_output = tf.placeholder( tf.float32,shape=[None,image_shape[0], image_shape[1],num_classes], name="net_output") network = build_fc_densenet(net_input, preset_model = 'FC-DenseNet56', num_classes=num_classes) network = tf.reshape(network, (-1, num_classes), name='logits') loss = custom_loss(network, net_output) opt = tf.train.AdamOptimizer(1e-4).minimize(loss, var_list=[var for var in tf.trainable_variables()]) with tf.Session() as sess: # Create function to get batches get_batches_fn = helper.gen_batch_function(os.path.join(data_dir), RGB_DIR, SEG_DIR, image_shape) init_op = tf.global_variables_initializer() saver = tf.train.Saver() # Runs training sess.run(init_op) train_nn(sess, epochs, batch_size, get_batches_fn, opt, loss, net_input, net_output, learning_rate) # Save the trained model today = datetime.datetime.now().strftime("%Y-%m-%d-%H%M") save_dir = os.path.join(SAVE_MODEL_DIR, today) helper.save_model(sess, net_input, network, save_dir) print("SavedModel saved at {}".format(save_dir)) test_dir = TEST_DIR helper.save_inference_samples(runs_dir, test_dir, sess, image_shape, network, net_input)
def inference (self, net_input, num_classes, is_training): if FLAGS.patch_slim: fuck_slim.patch(is_training) network = None init_fn = None if FLAGS.net == "FC-DenseNet56" or FLAGS.net == "FC-DenseNet67" or FLAGS.net == "FC-DenseNet103": with slim.arg_scope(aardvark.default_argscope(is_training)): network = build_fc_densenet(net_input, preset_model = FLAGS.net, num_classes=num_classes) elif FLAGS.net == "RefineNet-Res50" or FLAGS.net == "RefineNet-Res101" or FLAGS.net == "RefineNet-Res152": with slim.arg_scope(aardvark.default_argscope(is_training)): # RefineNet requires pre-trained ResNet weights network, init_fn = build_refinenet(net_input, preset_model = FLAGS.net, num_classes=num_classes, is_training=is_training) elif FLAGS.net == "FRRN-A" or FLAGS.net == "FRRN-B": with slim.arg_scope(aardvark.default_argscope(is_training)): network = build_frrn(net_input, preset_model = FLAGS.net, num_classes=num_classes) elif FLAGS.net == "Encoder-Decoder" or FLAGS.net == "Encoder-Decoder-Skip": with slim.arg_scope(aardvark.default_argscope(is_training)): network = build_encoder_decoder(net_input, preset_model = FLAGS.net, num_classes=num_classes) elif FLAGS.net == "MobileUNet" or FLAGS.net == "MobileUNet-Skip": with slim.arg_scope(aardvark.default_argscope(is_training)): network = build_mobile_unet(net_input, preset_model = FLAGS.net, num_classes=num_classes) elif FLAGS.net == "PSPNet-Res50" or FLAGS.net == "PSPNet-Res101" or FLAGS.net == "PSPNet-Res152": with slim.arg_scope(aardvark.default_argscope(is_training)): # Image size is required for PSPNet # PSPNet requires pre-trained ResNet weights network, init_fn = build_pspnet(net_input, label_size=[args.crop_height, args.crop_width], preset_model = FLAGS.net, num_classes=num_classes, is_training=is_training) elif FLAGS.net == "GCN-Res50" or FLAGS.net == "GCN-Res101" or FLAGS.net == "GCN-Res152": with slim.arg_scope(aardvark.default_argscope(is_training)): # GCN requires pre-trained ResNet weights network, init_fn = build_gcn(net_input, preset_model = FLAGS.net, num_classes=num_classes, is_training=is_training) elif FLAGS.net == "DeepLabV3-Res50" or FLAGS.net == "DeepLabV3-Res101" or FLAGS.net == "DeepLabV3-Res152": with slim.arg_scope(aardvark.default_argscope(is_training)): # DeepLabV requires pre-trained ResNet weights network, init_fn = build_deeplabv3(net_input, preset_model = FLAGS.net, num_classes=num_classes, is_training=is_training) elif FLAGS.net == "DeepLabV3_plus-Res50" or FLAGS.net == "DeepLabV3_plus-Res101" or FLAGS.net == "DeepLabV3_plus-Res152": # DeepLabV3+ requires pre-trained ResNet weights with slim.arg_scope(aardvark.default_argscope(is_training)): network, init_fn = build_deeplabv3_plus(net_input, preset_model = FLAGS.net, num_classes=num_classes, is_training=is_training) elif FLAGS.net == "AdapNet": with slim.arg_scope(aardvark.default_argscope(is_training)): network = build_adaptnet(net_input, num_classes=num_classes) else: raise ValueError("Error: the model %d is not available. Try checking which models are available using the command python main.py --help") self.init_fn = init_fn return network
download_checkpoints("Res50") if "Res101" in args.model and not os.path.isfile("models/resnet_v2_101.ckpt"): download_checkpoints("Res101") if "Res152" in args.model and not os.path.isfile("models/resnet_v2_152.ckpt"): download_checkpoints("Res152") # Compute your softmax cross entropy loss print("Preparing the model ...") net_input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) net_output = tf.placeholder(tf.float32, shape=[None, None, None, num_classes]) network = None init_fn = None if args.model == "FC-DenseNet56" or args.model == "FC-DenseNet67" or args.model == "FC-DenseNet103": network = build_fc_densenet(net_input, preset_model=args.model, num_classes=num_classes) elif args.model == "RefineNet-Res50" or args.model == "RefineNet-Res101" or args.model == "RefineNet-Res152": # RefineNet requires pre-trained ResNet weights network, init_fn = build_refinenet(net_input, preset_model=args.model, num_classes=num_classes) elif args.model == "FRRN-A" or args.model == "FRRN-B": network = build_frrn(net_input, preset_model=args.model, num_classes=num_classes) elif args.model == "Encoder-Decoder" or args.model == "Encoder-Decoder-Skip": network = build_encoder_decoder(net_input, preset_model=args.model, num_classes=num_classes) elif args.model == "MobileUNet" or args.model == "MobileUNet-Skip":
if args.balanced_weight: b_weight = utils.median_frequency_balancing( args.dataset + "/train_labels/", num_classes) print("Preparing the model ...") input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, num_classes]) with tf.device('/gpu:' + str(args.gpu)): input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, num_classes]) if args.model == "FC-DenseNet56" or args.model == "FC-DenseNet67" or args.model == "FC-DenseNet103" or args.model == "FC-DenseNet158" or args.model == "FC-DenseNet232": network = build_fc_densenet(input, preset_model=args.model, num_classes=num_classes, dropout_p=0.5) elif args.model == "RefineNet-Res101" or args.model == "RefineNet-Res152": network = build_refinenet(input, preset_model=args.model, num_classes=num_classes) elif args.model == "Encoder-Decoder": network = build_encoder_decoder(input, num_classes) elif args.model == "Encoder-Decoder-Skip": network = build_encoder_decoder_skip(input, num_classes) elif args.model == "HF-FCN": network = build_hf_fcn(input, preset_model='FC-DenseNet103', num_classes=num_classes) elif args.model == "custom": network = build_custom(input, num_classes)
for file in os.listdir(dataset_dir + "/test_labels"): cwd = os.getcwd() test_output_names.append(cwd + "/" + dataset_dir + "/test_labels/" + file) return train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names # Load the data print("Loading the data ...") train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names = prepare_data( ) print("Setting up training procedure ...") input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, 12]) network = build_fc_densenet(input, preset_model='FC-DenseNet103') loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=network, labels=output)) opt = tf.train.RMSPropOptimizer(learning_rate=0.001, decay=0.995).minimize( loss, var_list=[var for var in tf.trainable_variables()]) is_training = True num_epochs = 250 continue_training = False class_names_string = "Sky, Building, Pole, Road, Pavement, Tree, SignSymbol, Fence, Car, Pedestrian, Bicyclist, Unlabelled" class_names_list = [ "Sky", "Building", "Pole", "Road", "Pavement", "Tree", "SignSymbol", "Fence", "Car", "Pedestrian", "Bicyclist", "Unlabelled" ]
# print(variable_parameters) total_parameters += variable_parameters print("This model has %d trainable parameters"% (total_parameters)) if __name__ == '__main__': img_dir = '../DL_DATA/mass_buildings/test/sat' channel = 1 device_id = 0 offset = 0 print("Start prediction ...") with tf.device('/gpu:0'): input = tf.placeholder(tf.float32,shape=[None,3,None,None]) output = tf.placeholder(tf.float32,shape=[None,2,None,None]) network, prob = build_fc_densenet(input, preset_model = 'FC-DenseNet56', num_classes=2,is_bottneck=False, compression_rate=1, data_format='NCHW') is_training = False continue_training = False class_names_string = "Building, Unlabelled" class_names_list = ["Building", "Unlabelled"] config = tf.ConfigProto() config.gpu_options.allow_growth = True sess=tf.Session(config=config) saver=tf.train.Saver(max_to_keep=1000) # sess.run(tf.global_variables_initializer()) count_params()
with tf.device('/gpu:%d' % gpu_id): print('using tower:%d...' % gpu_id) with tf.name_scope('tower_%d' % gpu_id) as scope: with tf.variable_scope('gpu_variables', reuse=gpu_id > 0): input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, num_classes]) if args.is_balanced_weight or args.is_edge_weight: weight = tf.placeholder(tf.float32, shape=[None, None, None]) if args.model == "FC-DenseNet56" or args.model == "FC-DenseNet67" or args.model == "FC-DenseNet103" or args.model == "FC-DenseNet158" or args.model == "FC-DenseNet232": if args.is_BC: network = build_fc_densenet(input, preset_model=args.model, num_classes=num_classes, is_bottneck=1, compression_rate=0.5) else: network = build_fc_densenet(input, preset_model=args.model, num_classes=num_classes, is_bottneck=False, compression_rate=1) elif args.model == "RefineNet-Res50" or args.model == "RefineNet-Res101" or args.model == "RefineNet-Res152": # RefineNet requires pre-trained ResNet weights network, init_fn = build_refinenet(input, preset_model=args.model, num_classes=num_classes) elif args.model == "FRRN-A" or args.model == "FRRN-B": network = build_frrn(input,
def buildNetwork(model, net_input, num_class): # Get the selected model. # Some of them require pre-trained ResNet if "Res50" in model and not os.path.isfile("models/resnet_v2_50.ckpt"): download_checkpoints("ResnetV2", "50") if "Res101" in model and not os.path.isfile("models/resnet_v2_101.ckpt"): utils.download_checkpoints("ResnetV2", "101") if "Res152" in model and not os.path.isfile("models/resnet_v2_152.ckpt"): utils.download_checkpoints("ResnetV2", "152") network = None init_fn = None if model == "FC-DenseNet56" or model == "FC-DenseNet67" or model == "FC-DenseNet103": network = build_fc_densenet(net_input, preset_model=model, num_classes=num_class) elif model == "RefineNet-Res50" or model == "RefineNet-Res101" or model == "RefineNet-Res152": # RefineNet requires pre-trained ResNet weights network, init_fn = build_refinenet(net_input, preset_model=model, num_classes=num_class) elif model == "FRRN-A" or model == "FRRN-B": network = build_frrn(net_input, preset_model=model, num_classes=num_class) elif model == "Encoder-Decoder" or model == "Encoder-Decoder-Skip": network = build_encoder_decoder(net_input, preset_model=model, num_classes=num_class) elif model == "MobileUNet" or model == "MobileUNet-Skip": network = build_mobile_unet(net_input, preset_model=model, num_classes=num_class) elif model == "PSPNet-Res50" or model == "PSPNet-Res101" or model == "PSPNet-Res152": # Image size is required for PSPNet # PSPNet requires pre-trained ResNet weights network, init_fn = build_pspnet( net_input, label_size=[args.crop_height, args.crop_width], preset_model=model, num_classes=num_class) elif model == "GCN-Res50" or model == "GCN-Res101" or model == "GCN-Res152": # GCN requires pre-trained ResNet weights network, init_fn = build_gcn(net_input, preset_model=model, num_classes=num_class) elif model == "DeepLabV3-Res50" or model == "DeepLabV3-Res101" or model == "DeepLabV3-Res152": # DeepLabV requires pre-trained ResNet weights network, init_fn = build_deeplabv3(net_input, preset_model=model, num_classes=num_class) elif model == "DeepLabV3_plus-Res50" or model == "DeepLabV3_plus-Res101" or model == "DeepLabV3_plus-Res152": # DeepLabV3+ requires pre-trained ResNet weights network, init_fn = build_deeplabv3_plus(net_input, preset_model=model, num_classes=num_class) elif model == "AdapNet": network = build_adaptnet(net_input, num_classes=num_class) elif model == "custom": network = build_custom(net_input, num_class) else: raise ValueError( "Error: the model %d is not available. Try checking which models are available using the command python main.py --help" ) return network, init_fn
# cwd = os.getcwd() test_output_names.append(dataset_dir + "/labels_test/" + file) return train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names # Load the data print("Loading the data ...") train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names = prepare_data( '/media/zhoun/Data/lx/caffe/DeepNetsForEO/ISPRS/Vaihingen/vaihingen_128_128_32_fold1' ) print("Setting up training procedure ...") input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, 6]) network = build_fc_densenet(input, preset_model='FC-DenseNet103', num_classes=6) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=network, labels=output)) opt = tf.train.RMSPropOptimizer(learning_rate=0.001, decay=0.995).minimize( loss, var_list=[var for var in tf.trainable_variables()]) is_training = False num_epochs = 500 continue_training = True class_names_string = "Impervious surfaces, Buildings, Low vegetation, Tree, Car, Clutter, Unclassified " class_names_list = [ "Impervious surfaces", "Buildings", "Low vegetation", "Tree", "Car", "Clutter", "Unclassified"
file) return train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names # Load the data print("Loading the data ...") train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names = prepare_data( './data/mass_buildings/patches') print("Setting up training procedure ...") with tf.device('/gpu:0'): input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, 2]) network = build_fc_densenet(input, preset_model='FC-DenseNet232', num_classes=2, dropout_p=0.4) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=network, labels=output)) opt = tf.train.RMSPropOptimizer(learning_rate=0.001, decay=0.995).minimize( loss, var_list=[var for var in tf.trainable_variables()]) is_training = True num_epochs = 300 continue_training = False class_names_string = "Building, Unlabelled" class_names_list = ["Building", "Unlabelled"] config = tf.ConfigProto()
network = None init_fn = None print("Preparing the model ...") # with tf.device('/gpu'): # with tf.name_scope('tower_%d' % args.gpu_ids) as scope: # z = tf.placeholder(tf.float32,shape=[None,None,None,3],name='input') # gt = tf.placeholder(tf.float32,shape=[None,None,None,num_classes],name='output') # if args.is_balanced_weight or args.is_edge_weight: # weight = tf.placeholder(tf.float32,shape=[None,None,None],name='weight') if args.model == "FC-DenseNet56" or args.model == "FC-DenseNet67" or args.model == "FC-DenseNet103" or args.model == "FC-DenseNet158" or args.model == "FC-DenseNet232": if args.is_BC: G_logist, G, G_var = build_fc_densenet(z, preset_model=args.model, num_classes=num_classes, is_bottneck=True, compression_rate=0.5) else: G_logist, G, G_var = build_fc_densenet(z, preset_model=args.model, num_classes=num_classes, is_bottneck=False, compression_rate=1) k_t = tf.Variable(0., trainable=False, name='k_t') step = tf.Variable(0, name='step', trainable=False) g_lr = tf.Variable(1e-3, name='g_lr') d_lr = tf.Variable(8e-5, name='d_lr')
if __name__ == '__main__': img_dir = './data/mass_buildings/test/sat' channel = 1 device_id = 0 offset = 0 print("Start prediction ...") with tf.device('/gpu:0'): input = tf.placeholder(tf.float32, shape=[None, None, None, 3]) output = tf.placeholder(tf.float32, shape=[None, None, None, 2]) network = build_fc_densenet(input, preset_model='FC-DenseNet158', num_classes=2, is_bottneck=True, compression_rate=0.5) prob = tf.nn.softmax(network) is_training = False continue_training = False class_names_string = "Building, Unlabelled" class_names_list = ["Building", "Unlabelled"] config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) saver = tf.train.Saver(max_to_keep=1000) # sess.run(tf.global_variables_initializer())
def build_model(model_name, net_input, num_classes, frontend="ResNet101", is_training=True): # Get the selected model. # Some of them require pre-trained ResNet print("Preparing the model ...") if model_name not in SUPPORTED_MODELS: raise ValueError( "The model you selelect is not supported. The following models are currently supported: {0}" .format(SUPPORTED_MODELS)) if frontend not in SUPPORTED_FRONTENDS: raise ValueError( "The frontend you selelect is not supported. The following models are currently supported: {0}" .format(SUPPORTED_FRONTENDS)) if "ResNet50" == frontend and not os.path.isfile( "models/resnet_v2_50.ckpt"): download_checkpoints("ResNet50") if "ResNet101" == frontend and not os.path.isfile( "models/resnet_v2_101.ckpt"): download_checkpoints("ResNet101") if "ResNet152" == frontend and not os.path.isfile( "models/resnet_v2_152.ckpt"): download_checkpoints("ResNet152") if "MobileNetV2" == frontend and not os.path.isfile( "models/mobilenet_v2_1.4_224.ckpt.data-00000-of-00001"): download_checkpoints("MobileNetV2") if "InceptionV4" == frontend and not os.path.isfile( "models/inception_v4.ckpt"): download_checkpoints("InceptionV4") network = None init_fn = None if model_name == "FC-DenseNet56" or model_name == "FC-DenseNet67" or model_name == "FC-DenseNet103": network = build_fc_densenet(net_input, preset_model=model_name, num_classes=num_classes) elif model_name == "RefineNet": # RefineNet requires pre-trained ResNet weights network, init_fn = build_refinenet(net_input, preset_model=model_name, frontend=frontend, num_classes=num_classes, is_training=is_training) elif model_name == "FRRN-A" or model_name == "FRRN-B": network = build_frrn(net_input, preset_model=model_name, num_classes=num_classes) elif model_name == "Encoder-Decoder" or model_name == "Encoder-Decoder-Skip": network = build_encoder_decoder(net_input, preset_model=model_name, num_classes=num_classes) elif model_name == "MobileUNet" or model_name == "MobileUNet-Skip": network = build_mobile_unet(net_input, preset_model=model_name, num_classes=num_classes) elif model_name == "PSPNet": # Image size is required for PSPNet # PSPNet requires pre-trained ResNet weights network, init_fn = build_pspnet( net_input, label_size=[args.crop_height, args.crop_width], preset_model=model_name, frontend=frontend, num_classes=num_classes, is_training=is_training) elif model_name == "GCN": # GCN requires pre-trained ResNet weights network, init_fn = build_gcn(net_input, preset_model=model_name, frontend=frontend, num_classes=num_classes, is_training=is_training) elif model_name == "DeepLabV3": # DeepLabV requires pre-trained ResNet weights network, init_fn = build_deeplabv3(net_input, preset_model=model_name, frontend=frontend, num_classes=num_classes, is_training=is_training) elif model_name == "DeepLabV3_plus": # DeepLabV3+ requires pre-trained ResNet weights network, init_fn = build_deeplabv3_plus(net_input, preset_model=model_name, frontend=frontend, num_classes=num_classes, is_training=is_training) elif model_name == "AdapNet": network = build_adaptnet(net_input, num_classes=num_classes) elif model_name == "custom": network = build_custom(net_input, num_classes) else: raise ValueError( "Error: the model %d is not available. Try checking which models are available using the command python main.py --help" ) return network, init_fn