def dataset(self, config, debug=False): # #### load dataset train_dataset = coco.CocoDataSet('./COCO2017', 'train', flip_ratio=0., pad_mode='fixed', config=config, debug=False) print(train_dataset.get_categories()) assert config.NUM_CLASSES == len( train_dataset.get_categories() ), f"NUM_CLASSES must be compare with dataset, set:{config.NUM_CLASSES} != {len(train_dataset.get_categories())}" train_generator = data_generator.DataGenerator(train_dataset) train_tf_dataset = tf.data.Dataset.from_generator( train_generator, (tf.float32, tf.float32, tf.float32, tf.int32, tf.float32, tf.float32)) self.train_tf_dataset = train_tf_dataset.padded_batch( config.IMAGES_PER_GPU, padded_shapes=( [None, None, None], # img [None], #img_meta [None, None], #bboxes [None], #labels [None, None, None], #masks [None, None, 1])) # global_mask eval_dataset = coco.CocoDataSet('./COCO2017', 'val', flip_ratio=0., pad_mode='fixed', config=config, debug=False) eval_generator = data_generator.DataGenerator(eval_dataset) eval_tf_dataset = tf.data.Dataset.from_generator( eval_generator, (tf.float32, tf.float32, tf.float32, tf.int32, tf.float32, tf.float32)) self.eval_tf_dataset = eval_tf_dataset.padded_batch( config.IMAGES_PER_GPU, padded_shapes=( [None, None, None], # img [None], #img_meta [None, None], #bboxes [None], #labels [None, None, None], #masks [None, None, 1])) # global_mask if debug: idx = np.random.choice(range(len(train_dataset))) img, img_meta, bboxes, labels, masks, global_mask = train_dataset[ idx] rgb_img = np.round(img + config.MEAN_PIXEL) ori_img = utils.get_original_image(img, img_meta, config.MEAN_PIXEL) visualize.display_instances(rgb_img, bboxes, labels, train_dataset.get_categories()) self.train_dataset = train_dataset
from detection.models.detectors import faster_rcnn print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) for gpu in tf.config.experimental.list_physical_devices('GPU'): tf.config.experimental.set_memory_growth(gpu, True) # load dataset from detection.datasets import coco, data_generator img_mean = (123.675, 116.28, 103.53) # img_std = (58.395, 57.12, 57.375) img_std = (1., 1., 1.) train_dataset = coco.CocoDataSet('./COCO2017/', 'val', flip_ratio=0.5, pad_mode='fixed', mean=img_mean, std=img_std, scale=(640, 832), debug=True) train_generator = data_generator.DataGenerator(train_dataset) # load model from detection.models.detectors import faster_rcnn strategy = tf.distribute.MirroredStrategy() with strategy.scope(): model = faster_rcnn.FasterRCNN( num_classes=len(train_dataset.get_categories()))
# # Currently, memory growth needs to be the same across GPUs # for gpu in gpus: # tf.config.experimental.set_memory_growth(gpu, True) # logical_gpus = tf.config.experimental.list_logical_devices('GPU') # print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") # except RuntimeError as e: # # Memory growth must be set before GPUs have been initialized # print(e) img_mean = (123.675, 116.28, 103.53) # img_std = (58.395, 57.12, 57.375) img_std = (1., 1., 1.) val_dataset = coco.CocoDataSet('./COCO2017/', 'val', flip_ratio=0, pad_mode='fixed', mean=img_mean, std=img_std, scale=(800, 1344)) print(len(val_dataset)) model = faster_rcnn.FasterRCNN( num_classes=len(val_dataset.get_categories())) img, img_meta, bboxes, labels = val_dataset[0] batch_imgs = tf.Variable(np.expand_dims(img, 0)) batch_metas = tf.Variable(np.expand_dims(img_meta, 0)) _ = model((batch_imgs, batch_metas), training=False)
# memory region needed by the TensorFlow process os.environ['CUDA_VISIBLE_DEVICES'] = '0' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # load dataset from detection.datasets import coco img_mean = (123.675, 116.28, 103.53) # img_std = (58.395, 57.12, 57.375) img_std = (1., 1., 1.) train_dataset = coco.CocoDataSet('dataset', 'train', flip_ratio=0, pad_mode='fixed', mean=img_mean, std=img_std, scale=(800, 1024)) img, img_meta, bboxes, labels = train_dataset[0] rgb_img = np.round(img + img_mean) print('origin shape:', img_meta[:3]) print('scale shape:', img_meta[3:6]) print('pad shape:', img_meta[6:9]) print('scale factor:', img_meta[9:10]) print('flip :', img_meta[10:11]) print('newimg shape:', img.shape) print('bboxes:', bboxes.shape) print('labels:', labels)
os.environ['CUDA_VISIBLE_DEVICES'] = '0' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' print(tf.__version__) assert tf.__version__.startswith('2.') tf.random.set_seed(22) np.random.seed(22) img_mean = (123.675, 116.28, 103.53) # img_std = (58.395, 57.12, 57.375) img_std = (1., 1., 1.) batch_size = 8 train_dataset = coco.CocoDataSet('/scratch/llong/datasets/coco2017/', 'train', flip_ratio=0.5, pad_mode='fixed', mean=img_mean, std=img_std, scale=(800, 1216)) num_classes = len(train_dataset.get_categories()) train_generator = data_generator.DataGenerator(train_dataset) train_tf_dataset = tf.data.Dataset.from_generator( train_generator, (tf.float32, tf.float32, tf.float32, tf.int32)) train_tf_dataset = train_tf_dataset.batch(batch_size).prefetch(100).shuffle( 100) # train_tf_dataset = train_tf_dataset.padded_batch( # batch_size, padded_shapes=([None, None, None], [None], [None, None], [None])) model = faster_rcnn.FasterRCNN(num_classes=num_classes) optimizer = keras.optimizers.SGD(1e-3, momentum=0.9, nesterov=True)
# When you are finetuning with some pretrained model, # you need to follow the mean and std values used for pretraining. img_mean = (123.675, 116.28, 103.53) img_std = (58.395, 57.12, 57.375) epochs = 5 batch_size = 2 flip_ratio = 0 learning_rate = 1e-4 finetune = False train_dataset = coco.CocoDataSet( dataset_dir='./val2014', subset='train', annotation_dir='./annotations/sampled_ann_train.json', flip_ratio=flip_ratio, pad_mode='fixed', mean=img_mean, std=img_std, scale=(800, 1216)) test_dataset = coco.CocoDataSet( dataset_dir='./val2014', subset='val', annotation_dir='./annotations/sampled_ann_test.json', flip_ratio=flip_ratio, pad_mode='non-fixed', mean=img_mean, std=img_std, scale=(800, 1216)) train_generator = data_generator.DataGenerator(train_dataset)
epochs = int(arg) elif opt == '-c': checkpoint = int(arg) elif opt == '-n': if int(arg) == 0: img_mean = (0., 0., 0.) img_std = (1., 1., 1.) elif int(arg) == 1: # Company Articles Dataset img_mean = (0.9684, 0.9683, 0.9683) img_std = (0.1502, 0.1505, 0.1505) train_dataset = coco.CocoDataSet(dataset_dir='dataset', subset='train', flip_ratio=flip_ratio, pad_mode='fixed', mean=img_mean, std=img_std, scale=(800, 1216)) test_dataset = coco.CocoDataSet(dataset_dir='dataset', subset='val', flip_ratio=flip_ratio, pad_mode='non-fixed', mean=img_mean, std=img_std, scale=(800, 1216)) train_generator = data_generator.DataGenerator(train_dataset) train_tf_dataset = tf.data.Dataset.from_generator( train_generator, (tf.float32, tf.float32, tf.float32, tf.int32)) train_tf_dataset = train_tf_dataset.batch(batch_size).prefetch(100).shuffle(
# session = tf.Session(config=config) print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) for gpu in tf.config.experimental.list_physical_devices('GPU'): tf.config.experimental.set_memory_growth(gpu, True) # load dataset img_mean = (123.675, 116.28, 103.53) # img_std = (58.395, 57.12, 57.375) img_std = (1., 1., 1.) train_dataset = coco.CocoDataSet('./COCO2017/', 'val', flip_ratio=0.5, pad_mode='fixed', mean=img_mean, std=img_std, scale=(400, 512)) train_generator = data_generator.DataGenerator(train_dataset) # display a sample img, img_meta, bboxes, labels = train_dataset[0] rgb_img = np.round(img + img_mean) ori_img = get_original_image(img, img_meta, img_mean) visualize.display_instances(rgb_img, bboxes, labels, train_dataset.get_categories()) # load model model = faster_rcnn.FasterRCNN(num_classes=len(train_dataset.get_categories()))