def forward(self, inputs, weights): # reuse=False, scope='' ? """Call Resnet functionally and return output tensor for given input and given weights.""" datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array( (0.2023, 0.1994, 0.2010)) inputs = tf_standardize(inputs, datamean, datastd) output = self.resnet(self.num_blocks, weights, img_input=inputs, classes=self.classes, training=True) return output
def forward(self, x, weights, buffers, training=True): stride, no_stride = [1, 2, 2, 1], [1, 1, 1, 1] newbuffers = {} datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array( (0.2023, 0.1994, 0.2010)) x = tf_standardize(x, datamean, datastd) # Input layer in_planes = 32 x, newbuffers[f'mean_input'], newbuffers[ f'var_input'] = self.forward_layer(x, weights, buffers, 'input', training) # 1) Deep layers for idx, (expansion, out_planes, num_blocks, stride) in enumerate(self.cfg): strides = [stride] + [1] * (num_blocks - 1) for ids, stride in enumerate(strides): # print(f'{idx}:: This is stride {ids}, cfg: {expansion}:{out_planes}:{num_blocks}:{stride}') y, newbuffers[f'mean_{idx}_{ids}_1'], newbuffers[f'var_{idx}_{ids}_1'] = \ self.forward_layer(x, weights, buffers, f'{idx}_{ids}_1', training) y, newbuffers[f'mean_{idx}_{ids}_2'], newbuffers[f'var_{idx}_{ids}_2'] = \ self.forward_layer(y, weights, buffers, f'{idx}_{ids}_2', training, stride=stride, groups=out_planes) y, newbuffers[f'mean_{idx}_{ids}_3'], newbuffers[f'var_{idx}_{ids}_3'] = \ self.forward_layer(y, weights, buffers, f'{idx}_{ids}_3', training) if stride == 1 and in_planes != out_planes: shortcut, newbuffers[f'mean_{idx}_{ids}_r'], newbuffers[f'var_{idx}_{ids}_r'] = \ self.forward_layer(x, weights, buffers, f'{idx}_{ids}_r', training) x = y + shortcut else: x = y in_planes = out_planes # Output layer: x, newbuffers[f'mean_output'], newbuffers[ f'var_output'] = self.forward_layer(x, weights, buffers, 'output', training) # Dense layers x = tf.nn.avg_pool(x, [1, x.shape[1], x.shape[2], 1], [1, 1, 1, 1], 'VALID') features = tf.reshape( x, [-1, np.prod([int(dim) for dim in x.get_shape()[1:]])]) logits = tf.matmul( features, weights['classifier_weights']) + weights['classifier_bias'] if self.batchnorm: returnbuffers = newbuffers else: returnbuffers = {} return logits, features, returnbuffers
def forward(self, input, weights, buffers, training=True): newbuffers = {} datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array((0.2023, 0.1994, 0.2010)) y = tf_standardize(input, datamean, datastd) x, newbuffers[f'mean_input'], newbuffers[f'var_input'] = \ self.res_layer(y, weights['conv_input'], weights.get('scale_input'), weights.get('offset_input'), buffers.get('mean_input'), buffers.get('var_input'), training, stride=1) for idx, stage in enumerate(self.structure): for block in range(stage): stride = 1 if idx > 0 and block == 0: stride = 2 # downsample y, newbuffers[f'mean_{idx}_{block}_1'], newbuffers[f'var_{idx}_{block}_1'] = \ self.res_layer(x, weights[f'conv_{idx}_{block}_1'], weights.get(f'scale_{idx}_{block}_1'), weights.get(f'offset_{idx}_{block}_1'), buffers.get(f'mean_{idx}_{block}_1'), buffers.get(f'var_{idx}_{block}_1'), training, stride=stride) y, newbuffers[f'mean_{idx}_{block}_2'], newbuffers[f'var_{idx}_{block}_2'] = \ self.res_layer(y, weights[f'conv_{idx}_{block}_2'], weights.get(f'scale_{idx}_{block}_2'), weights.get(f'offset_{idx}_{block}_2'), buffers.get(f'mean_{idx}_{block}_2'), buffers.get(f'var_{idx}_{block}_2'), training, stride=1, activation=tf.identity) if idx > 0 and block == 0: x, newbuffers[f'mean_{idx}_{block}_r'], newbuffers[f'var_{idx}_{block}_r'] = \ self.res_layer(x, weights[f'conv_{idx}_{block}_r'], weights.get(f'scale_{idx}_{block}_r'), weights.get(f'offset_{idx}_{block}_r'), buffers.get(f'mean_{idx}_{block}_r'), buffers.get(f'var_{idx}_{block}_r'), training, stride=stride, activation=tf.identity) x = x + y x = tf.nn.relu(x) # Final pooling for head # print(x.shape) x = tf.nn.avg_pool(x, [1, x.shape[1], x.shape[2], 1], [1, 1, 1, 1], 'VALID') # print(x.shape) features = tf.reshape(x, [-1, np.prod([int(dim) for dim in x.get_shape()[1:]])]) logits = tf.matmul(features, weights['classifier_weights']) + weights['classifier_bias'] if self.batchnorm: returnbuffers = newbuffers else: returnbuffers = {} return logits, features, returnbuffers
def forward(self, inp, weights, buffers, training=True): newbuffers = {} hiddens = [] datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array( (0.2023, 0.1994, 0.2010)) hidden = tf_standardize(inp, datamean, datastd) for l in range(len(self.dim_hidden)): if not self.batchnorm: hidden = self.conv_block(hidden, weights[f'conv{l + 1}'], weights[f'b{l + 1}']) else: hidden, buffermean, buffervar = self.convbn_block( hidden, weights[f'conv{l + 1}'], weights[f'scale{l + 1}'], weights[f'offset{l + 1}'], buffers[f'mean{l + 1}'], buffers[f'var{l + 1}'], training) newbuffers[f'mean{l + 1}'] = buffermean newbuffers[f'var{l + 1}'] = buffervar hiddens.append(hidden) hidden = tf.reshape( hidden, [-1, np.prod([int(dim) for dim in hidden.get_shape()[1:]])]) logits = tf.matmul(hidden, weights['w6']) + weights['b6'] return logits, hiddens, newbuffers
def forward(self, inp, weights, reuse=False, scope=''): # reuse is for the normalization parameters. datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array( (0.2023, 0.1994, 0.2010)) inp = tf_standardize(inp, datamean, datastd) hidden1 = self.conv_block(inp, weights['conv1'], weights['b1'], reuse, scope + '0') hidden2 = self.conv_block(hidden1, weights['conv2'], weights['b2'], reuse, scope + '1') hidden3 = self.conv_block(hidden2, weights['conv3'], weights['b3'], reuse, scope + '2') hidden4 = self.conv_block(hidden3, weights['conv4'], weights['b4'], reuse, scope + '3') hidden5 = self.conv_block(hidden4, weights['conv5'], weights['b5'], reuse, scope + '4') hidden5 = tf.reshape( hidden5, [-1, np.prod([int(dim) for dim in hidden5.get_shape()[1:]])]) logits = tf.matmul(hidden5, weights['w6']) + weights['b6'] # nparam = count_params_in_scope() return logits, hidden5
def forward(self, x, weights, buffers, training=True): stride, no_stride = [1, 2, 2, 1], [1, 1, 1, 1] newbuffers = {} datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array((0.2023, 0.1994, 0.2010)) x = tf_standardize(x, datamean, datastd) for idx, block in enumerate(self.blocks): for layer in range(block): if self.batchnorm: x, newbuffers[f'mean_{idx}_{layer}'], newbuffers[f'var_{idx}_{layer}'] = \ self.bn_layer(x, weights[f'conv_{idx}_{layer}'], weights.get(f'scale_{idx}_{layer}'), weights.get(f'offset_{idx}_{layer}'), buffers.get(f'mean_{idx}_{layer}'), buffers.get(f'var_{idx}_{layer}'), training, stride=1, activation=tf.nn.relu) else: x, newbuffers[f'mean_{idx}_{layer}'], newbuffers[f'var_{idx}_{layer}'] = \ self.conv_layer(x, weights[f'conv_{idx}_{layer}'], weights[f'bias_{idx}_{layer}'], training, stride=1, activation=tf.nn.relu) x = tf.nn.max_pool(x, stride, stride, 'VALID') # Flatten: x = tf.reshape(x, [-1, np.prod([int(dim) for dim in x.get_shape()[1:]])]) # Dense layers x = tf.nn.relu(tf.matmul(x, weights['dense_weights_1']) + weights['dense_bias_1']) features = tf.nn.relu(tf.matmul(x, weights['dense_weights_2']) + weights['dense_bias_2']) logits = tf.matmul(features, weights['classifier_weights']) + weights['classifier_bias'] if self.batchnorm: returnbuffers = newbuffers else: returnbuffers = {} return logits, features, returnbuffers
def call(self, inputs=None, params=defaultdict(lambda: None), preprocessing=True): if preprocessing: data = self.data else: data = None if data == 'CIFAR10': datamean, datastd = np.array((0.4914, 0.4822, 0.4465)), np.array( (0.2023, 0.1994, 0.2010)) inputs = tf_standardize(inputs, datamean, datastd) elif data == 'ImageNet': pass # depends on the network arch and will be handled separately if self.architecture in ['ResNet50', 'ResNet101', 'ResNet152']: if data == 'ImageNet': inputs = keras.applications.resnet.preprocess_input(inputs) x, _ = getattr(keras_applications.resnet, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['ResNet50V2', 'ResNet101V2', 'ResNet152V2']: if data == 'ImageNet': inputs = keras_applications.resnet_v2.preprocess_input(inputs) x, _ = getattr(keras_applications.resnet_v2, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['ResNeXt50', 'ResNeXt101']: if data == 'ImageNet': inputs = keras_applications.resnext.preprocess_input(inputs) x, _ = getattr(keras_applications.resnext, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in [ 'DenseNet40', 'DenseNet121', 'DenseNet169', 'DenseNet201' ]: if data == 'ImageNet': inputs = keras_applications.densenet.preprocess_input(inputs) x, _ = getattr(keras_applications.densenet, self.architecture)(include_top=False, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['NASNetMobile', 'NASNetLarge']: if data == 'ImageNet': inputs = keras_applications.nasnet.preprocess_input(inputs) x, _ = getattr(keras_applications.nasnet, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['InceptionResNetV2']: if data == 'ImageNet': inputs = keras_applications.inception_resnet_v2.preprocess_input( inputs) x, _ = getattr(keras_applications.inception_resnet_v2, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['InceptionV3']: if data == 'ImageNet': inputs = keras_applications.inception_v3.preprocess_input( inputs) x, _ = getattr(keras_applications.inception_v3, self.architecture)(include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['VGG19']: if data == 'ImageNet': inputs = keras_applications.vgg19.preprocess_input(inputs) x, _ = keras_applications.vgg19.VGG19( include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='max', classes=self.num_classes, params=params) elif self.architecture in ['VGG16']: if data == 'ImageNet': inputs = keras_applications.vgg16.preprocess_input(inputs) x, _ = keras_applications.vgg16.VGG16( include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='max', classes=self.num_classes, params=params) elif self.architecture in ['VGG13']: if data == 'ImageNet': inputs = keras_applications.vgg13.preprocess_input(inputs) x, _ = keras_applications.vgg13.VGG13( include_top=False, input_tensor=inputs, input_shape=self.expected_shape, pooling='max', classes=self.num_classes, params=params) elif self.architecture in ['VGG11']: if data == 'ImageNet': inputs = keras_applications.vgg11.preprocess_input(inputs) x, _ = keras_applications.vgg11.VGG11( include_top=False, input_tensor=inputs, input_shape=self.expected_shape, pooling='max', classes=self.num_classes, params=params) elif self.architecture in ['Xception']: if data == 'ImageNet': inputs = keras_applications.xception.preprocess_input(inputs) x, _ = keras_applications.xception.Xception( include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['MobileNet']: if data == 'ImageNet': inputs = keras_applications.mobilenet.preprocess_input(inputs) x, _ = keras_applications.mobilenet.MobileNet( include_top=False, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) elif self.architecture in ['MobileNetV2']: if data == 'ImageNet': inputs = keras_applications.mobilenet_v2.preprocess_input( inputs) x, _ = keras_applications.mobilenet_v2.MobileNetV2( include_top=False, alpha=1.0, weights=None, input_tensor=inputs, input_shape=self.expected_shape, pooling='avg', classes=self.num_classes, params=params) else: raise NotImplementedError('Unknown architecture.') features = self.flatten(x) logits = self.dense(features, params=params) return logits, features