class TestFeaturePredictorPredict(unittest.TestCase): def setUp(self): self.link = FeaturePredictor(DummyFeatureExtractor( self.in_channels, self.shape_0, self.shape_1), crop_size=5, crop=self.crop) self.x = np.random.uniform(size=(3, self.in_channels, 32, 32)).astype(np.float32) self.one_output = self.shape_1 is None def check(self, x): out = self.link.predict(x) if self.one_output: self.assertEqual(out.shape, (self.x.shape[0], ) + self.shape_0) self.assertIsInstance(out, np.ndarray) else: out_0, out_1 = out self.assertEqual(out_0.shape, (self.x.shape[0], ) + self.shape_0) self.assertEqual(out_1.shape, (self.x.shape[0], ) + self.shape_1) self.assertIsInstance(out_0, np.ndarray) self.assertIsInstance(out_1, np.ndarray) def test_cpu(self): self.check(self.x) @attr.gpu def test_gpu(self): self.link.to_gpu() self.check(self.x)
class TestFeaturePredictorPredict(unittest.TestCase): def setUp(self): self.link = FeaturePredictor( DummyFeatureExtractor( self.in_channels, self.shape_0, self.shape_1), crop_size=5, crop=self.crop) self.x = np.random.uniform( size=(3, self.in_channels, 32, 32)).astype(np.float32) self.one_output = self.shape_1 is None def check(self, x): out = self.link.predict(x) if self.one_output: self.assertEqual(out.shape, (self.x.shape[0],) + self.shape_0) self.assertIsInstance(out, np.ndarray) else: out_0, out_1 = out self.assertEqual(out_0.shape, (self.x.shape[0],) + self.shape_0) self.assertEqual(out_1.shape, (self.x.shape[0],) + self.shape_1) self.assertIsInstance(out_0, np.ndarray) self.assertIsInstance(out_1, np.ndarray) def test_cpu(self): self.check(self.x) @attr.gpu def test_gpu(self): self.link.to_gpu() self.check(self.x)
def setUp(self): self.link = FeaturePredictor(DummyFeatureExtractor( self.in_channels, self.shape_0, self.shape_1), crop_size=5, crop=self.crop) self.x = np.random.uniform(size=(3, self.in_channels, 32, 32)).astype(np.float32) self.one_output = self.shape_1 is None
def main(): parser = argparse.ArgumentParser( description='Learning convnet from ILSVRC2012 dataset') parser.add_argument('val', help='Path to root of the validation dataset') parser.add_argument( '--model', choices=('vgg16', 'resnet50', 'resnet101', 'resnet152')) parser.add_argument('--pretrained_model', default='imagenet') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--batchsize', type=int, default=32) parser.add_argument('--crop', choices=('center', '10'), default='center') parser.add_argument('--resnet_mode', default='he') args = parser.parse_args() dataset = DirectoryParsingLabelDataset(args.val) label_names = directory_parsing_label_names(args.val) n_class = len(label_names) iterator = iterators.MultiprocessIterator( dataset, args.batchsize, repeat=False, shuffle=False, n_processes=6, shared_mem=300000000) if args.model == 'vgg16': extractor = VGG16(n_class, args.pretrained_model) elif args.model == 'resnet50': extractor = ResNet50( n_class, args.pretrained_model, mode=args.resnet_mode) elif args.model == 'resnet101': extractor = ResNet101( n_class, args.pretrained_model, mode=args.resnet_mode) elif args.model == 'resnet152': extractor = ResNet152( n_class, args.pretrained_model, mode=args.resnet_mode) model = FeaturePredictor( extractor, crop_size=224, scale_size=256, crop=args.crop) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() model.to_gpu() print('Model has been prepared. Evaluation starts.') in_values, out_values, rest_values = apply_to_iterator( model.predict, iterator, hook=ProgressHook(len(dataset))) del in_values pred_probs, = out_values gt_labels, = rest_values accuracy = F.accuracy( np.array(list(pred_probs)), np.array(list(gt_labels))).data print() print('Top 1 Error {}'.format(1. - accuracy))
def setUp(self): self.link = FeaturePredictor( DummyFeatureExtractor(self.in_channels, (1,), None), crop_size=self.crop_size, scale_size=self.scale_size, crop=self.crop, mean=self.mean) if isinstance(self.crop_size, int): hw = (self.crop_size, self.crop_size) else: hw = self.crop_size if self.crop == 'center': self.expected_shape = (1, self.in_channels) + hw elif self.crop == '10': self.expected_shape = (10, self.in_channels) + hw
class TestFeaturePredictor(unittest.TestCase): def setUp(self): self.link = FeaturePredictor( DummyFeatureExtractor(self.in_channels, (1,), None), crop_size=self.crop_size, scale_size=self.scale_size, crop=self.crop, mean=self.mean) if isinstance(self.crop_size, int): hw = (self.crop_size, self.crop_size) else: hw = self.crop_size if self.crop == 'center': self.expected_shape = (1, self.in_channels) + hw elif self.crop == '10': self.expected_shape = (10, self.in_channels) + hw def test_prepare(self): out = self.link._prepare( np.random.uniform(size=(self.in_channels, 286, 286))) self.assertEqual(out.shape, self.expected_shape) def test_mean(self): if self.mean is None: np.testing.assert_equal(self.link.mean, self.link.extractor.mean) else: np.testing.assert_equal(self.link.mean, self.mean)
def setUp(self): self.link = FeaturePredictor( DummyFeatureExtractor( self.in_channels, self.shape_0, self.shape_1), crop_size=5, crop=self.crop) self.x = np.random.uniform( size=(3, self.in_channels, 32, 32)).astype(np.float32) self.one_output = self.shape_1 is None
def setup(dataset, model, pretrained_model, batchsize, val, crop, resnet_arch): dataset_name = dataset if dataset_name == 'imagenet': dataset = DirectoryParsingLabelDataset(val) label_names = directory_parsing_label_names(val) def eval_(out_values, rest_values): pred_probs, = out_values gt_labels, = rest_values accuracy = F.accuracy(np.array(list(pred_probs)), np.array(list(gt_labels))).data print() print('Top 1 Error {}'.format(1. - accuracy)) cls, pretrained_models, default_batchsize = models[model][:3] if pretrained_model is None: pretrained_model = pretrained_models.get(dataset_name, dataset_name) if crop is None: crop = models[model][3] kwargs = { 'n_class': len(label_names), 'pretrained_model': pretrained_model, } if model in ['resnet50', 'resnet101', 'resnet152']: if resnet_arch is None: resnet_arch = models[model][4] kwargs.update({'arch': resnet_arch}) extractor = cls(**kwargs) model = FeaturePredictor(extractor, crop_size=224, scale_size=256, crop=crop) if batchsize is None: batchsize = default_batchsize return dataset, eval_, model, batchsize