def init_resnet(stack_fn, preact, use_bias, model_name, input_shape, block_name_to_hyperparameters_dict, preprocess_input_mode): # Downloads the weights file_name = model_name + "_weights_tf_dim_ordering_tf_kernels_notop.h5" file_hash = WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir="models", file_hash=file_hash) # Define and initialize the first block first_block = ResNet(stack_fn=lambda x: x, preact=preact, use_bias=use_bias, include_top=False, weights=None, input_shape=input_shape) first_block.load_weights(weights_path, by_name=True) submodel_list = [first_block] # Define and initialize each block for block_name, (filters, blocks, stride1) in block_name_to_hyperparameters_dict.items(): input_tensor = Input(shape=K.int_shape(submodel_list[-1].output)[1:]) output_tensor = stack_fn(input_tensor, filters=filters, blocks=blocks, stride1=stride1, name=block_name) submodel = Model(inputs=input_tensor, outputs=output_tensor, name="{}_block".format(block_name)) submodel.load_weights(weights_path, by_name=True) submodel_list.append(submodel) return submodel_list, lambda x: preprocess_input(x, mode=preprocess_input_mode)
def ResNeXt50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): def stack_fn(x): x = stack3(x, 128, 3, stride1=1, name='conv2') x = stack3(x, 256, 4, name='conv3') x = stack3(x, 512, 6, name='conv4') x = stack3(x, 1024, 3, name='conv5') return x return ResNet(stack_fn, False, False, 'resnext50', include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
def ResNet34(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet34 architecture.""" def stack_fn(x): x = basicstack1(x, 64, 3, stride1=1, name='conv2') x = basicstack1(x, 128, 4, name='conv3') x = basicstack1(x, 256, 6, name='conv4') return basicstack1(x, 512, 3, name='conv5') return ResNet(stack_fn, False, True, 'resnet34', include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
def ResNet34Bottleneck(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet34Bottleneck architecture. Warning: this is actually the same with ResNet50. """ def stack_fn(x): x = stack1(x, 64, 3, stride1=1, name='conv2') x = stack1(x, 128, 4, name='conv3') x = stack1(x, 256, 6, name='conv4') return stack1(x, 512, 3, name='conv5') return ResNet(stack_fn, False, True, 'resnet34_bottleneck', include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
def ResNet18Bottleneck(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet18Bottleneck architecture. Warning: this would be inexistent ResNet26. """ def stack_fn(x): x = stack1(x, 64, 2, stride1=1, name='conv2') x = stack1(x, 128, 2, name='conv3') x = stack1(x, 256, 2, name='conv4') return stack1(x, 512, 2, name='conv5') return ResNet(stack_fn, False, True, 'resnet18_bottleneck', include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
def stacks(x): x = resnet.stack1(x, 64, 3, stride1=1, name='conv2') x = resnet.stack1(x, 128, 4, name='conv3') x = resnet.stack1(x, 256, 6, name='conv4') x = resnet.stack1(x, 512, 3, name='conv5') return x input_name = 'my_input' inp = tf.keras.layers.Input(shape=(img_dim, img_dim, 3), name=input_name) model = ResNet(stacks, preact=False, use_bias=True, model_name='resnet50', weights='imagenet', input_tensor=inp, pooling='avg', input_shape=(img_dim, img_dim, 3)) y = model(inp) print(tf.shape(y)) print(len(model.layers)) names = {} def show_layers(): for i, layer in enumerate(model.layers): print(i, layer.name)