def test_resnet50_architecture(self): resnet50 = architectures.resnet50() self.assertEqual(resnet50.input_shape, (224, 224, 3)) self.assertEqual(resnet50.output_shape, (1000,)) random_input = asfloat(np.random.random((7, 224, 224, 3))) prediction = self.eval(resnet50.output(random_input)) self.assertEqual(prediction.shape, (7, 1000))
def test_resnet50_architecture(self): resnet50 = architectures.resnet50() self.assertEqual(resnet50.input_shape, (3, 224, 224)) self.assertEqual(resnet50.output_shape, (1000, )) resnet50_predict = resnet50.compile() random_input = asfloat(np.random.random((7, 3, 224, 224))) prediction = resnet50_predict(random_input) self.assertEqual(prediction.shape, (7, 1000))
def test_resnet50_spatial(self): resnet50 = architectures.resnet50( include_global_pool=False, in_out_ratio=8, ) self.assertEqual(resnet50.input_shape, (224, 224, 3)) self.assertEqual(resnet50.output_shape, (28, 28, 2048)) random_input = asfloat(np.random.random((7, 224, 224, 3))) prediction = self.eval(resnet50.output(random_input)) self.assertEqual(prediction.shape, (7, 28, 28, 2048))
def test_resnet50_dilation_rates(self): resnet50 = architectures.resnet50( include_global_pool=False, in_out_ratio=8, ) layer = resnet50.layer('res4d_branch2b') self.assertEqual(layer.dilation, (2, 2)) layer = resnet50.layer('res5a_branch2b') self.assertEqual(layer.dilation, (2, 2)) layer = resnet50.layer('res5b_branch2b') self.assertEqual(layer.dilation, (4, 4))
def prepare_image(fname): with open(IMAGENET_MEAN_FILE, 'rb') as f: # Mean values is the average image accros all training dataset. # if dataset (1000, 3, 224, 224) then mean image shape # is (3, 224, 224) and computes as data.mean(axis=0) mean_values = pickle.load(f) image = read_image(fname, image_size=(256, 256), crop_size=(224, 224)) # Convert RGB to BGR image[:, (0, 1, 2), :, :] = image[:, (2, 1, 0), :, :] return asfloat(image - mean_values) environment.speedup() resnet50 = architectures.resnet50() if not os.path.exists(RESNET50_WEIGHTS_FILE): download_file( url="http://neupy.s3.amazonaws.com/imagenet-models/resnet50.pickle", filepath=RESNET50_WEIGHTS_FILE, description='Downloading weights') storage.load(resnet50, RESNET50_WEIGHTS_FILE) predict = resnet50.compile() dog_image = prepare_image(DOG_IMAGE_PATH) output = predict(dog_image) print_top_n(output, n=5)
def test_resnet50_no_global_pooling(self): resnet50 = architectures.resnet50(include_global_pool=False) self.assertEqual(resnet50.input_shape, (224, 224, 3)) self.assertEqual(resnet50.output_shape, (7, 7, 2048))
def test_resnet50_exceptions(self): with self.assertRaises(ValueError): architectures.resnet50(in_out_ratio=2)
def create_deeplab_model(resnet50_weights=None, deeplab_weights=None, size=None): print("Initializing ResNet-50 architecture...") SamePadConv = partial(Convolution, bias=None, padding='same') resnet50 = architectures.resnet50( input_shape=(size, size, 3), include_global_pool=False, in_out_ratio=16, ) if resnet50_weights is not None: # Pre-trained ResNet-50 contains parameters for the final # classification layer. We don't use this layer and for this reason # we need to set ``ignore_missing=True`` print("Recovering ResNet-50 parameters...") storage.load(resnet50, resnet50_weights, ignore_missing=True) in_height, in_width, _ = resnet50.input_shape out_height, out_width, _ = resnet50.output_shape resnet50_input = resnet50.layers[0] deeplab_input = Input(resnet50.output_shape, name='deeplab-input') print("Initializing Deeplab architecture...") deeplab = join( deeplab_input, # Atrous Spatial Pyramid Pooling parallel( SamePadConv((1, 1, 256)) > BatchNorm(), SamePadConv((3, 3, 256), dilation=6) > BatchNorm(), SamePadConv((3, 3, 256), dilation=12) > BatchNorm(), SamePadConv((3, 3, 256), dilation=18) > BatchNorm(), [ GlobalPooling('avg'), Reshape((1, 1, -1)), SamePadConv((1, 1, 256)) > BatchNorm(), IncludeResidualInputs(deeplab_input), ResizeBilinear(), ]), Concatenate(), SamePadConv((1, 1, 256)) > BatchNorm(), # Convert to the classification maps Convolution((1, 1, 21), padding='same'), IncludeResidualInputs(resnet50_input), ResizeBilinear((in_height, in_width)), Softmax(name='segmentation-proba'), ) if deeplab_weights is not None: print("Recovering Deeplab parameters...") storage.load(deeplab, deeplab_weights, ignore_missing=True) print("Patching layers...") patches = { BatchNorm: { 'alpha': 1 - 0.997, 'epsion': 1e-5, } } patch_layers(deeplab, patches) patch_layers(resnet50, patches) return resnet50, deeplab