def load_network_from_json_and_h5(self, json_fname, h5_fname): with open(json_fname, "r") as f: json_structure = json.load(f) with h5py.File(h5_fname, "r") as f: self.name = json_structure["name"] del json_structure['name'] for layer_name in json_structure.keys(): l_type = f[layer_name + "/layer_info"].attrs["type"] if l_type == "SoftmaxWithCrossEntropy": l = SoftmaxWithCrossEntropy(layer_name) l.load_from_h5(f) self.loss_layer = l continue elif l_type == "ConvLayer": l = ConvLayer(layer_name) elif l_type == "BatchNormLayer": l = BatchNormLayer(layer_name) elif l_type == "ReLu": l = ReLu(layer_name) elif l_type == "DepthwiseConvLayer": l = DepthwiseConvLayer(layer_name) elif l_type == "PointwiseConvLayer": l = PointwiseConvLayer(layer_name) elif l_type == "GlobalAveragePoolingLayer": l = GlobalAveragePoolingLayer(layer_name) elif l_type == "DenseLayer": l = DenseLayer(layer_name) elif l_type == "ResidualBlock": l = ResidualBlock(layer_name) l.load_from_h5(f) self.layers.append(l)
def build(self, input_shape): input_shape = tf.TensorShape(input_shape) self.dense = tf.layers.Dense((self._bottom_w ** 2) * self._channel * 16, use_bias=False, activation=None, kernel_initializer=tf.initializers.random_normal()) self.block1 = ResidualBlock(out_c=self._channel * 16, activation=self._activation, category=self._category, upsampling=True) self.block2 = ResidualBlock(out_c=self._channel * 8, activation=self._activation, category=self._category, upsampling=True) self.block3 = ResidualBlock(out_c=self._channel * 4, activation=self._activation, category=self._category, upsampling=True) self.block4 = ResidualBlock(out_c=self._channel * 2, activation=self._activation, category=self._category, upsampling=True) self.block5 = ResidualBlock(out_c=self._channel, activation=self._activation, category=self._category, upsampling=True) self.bn = tf.layers.BatchNormalization() self.conv = tf.layers.Conv2D(3, 3, padding='SAME', use_bias=False, activation=None, kernel_initializer=tf.initializers.random_normal()) self._layers += [self.dense, self.block1, self.block2, self.block3, self.block4, self.block5, self.bn, self.conv]
def build(self, input_shape): input_shape = tf.TensorShape(input_shape) self.block1 = ResidualBlock(out_c=self._channel, activation=self._activation, is_use_bn=False, is_use_sn=True) self.block2 = ResidualBlock(out_c=self._channel * 2, activation=self._activation, is_use_bn=False, downsampling=True, is_use_sn=True) self.block3 = ResidualBlock(out_c=self._channel * 4, activation=self._activation, is_use_bn=False, downsampling=True, is_use_sn=True) self.block4 = ResidualBlock(out_c=self._channel * 8, activation=self._activation, is_use_bn=False, downsampling=True, is_use_sn=True) self.block5 = ResidualBlock(out_c=self._channel * 16, activation=self._activation, is_use_bn=False, downsampling=True, is_use_sn=True) self.block6 = ResidualBlock(out_c=self._channel * 16, activation=self._activation, is_use_bn=False, downsampling=True, is_use_sn=True) self.dense = tf.layers.Dense(1, use_bias=False, activation=None, kernel_initializer=tf.initializers.random_normal()) self.dense_u = None self._layers += [self.block1, self.block2, self.block3, self.block4, self.block5, self.block6, self.dense] if self._category != 0: self.embed_y = self.add_weight( 'embeddings', shape=[self._category, self._channel * 16], initializer=tf.initializers.random_normal(), regularizer=None, constraint=None, trainable=True) self.embed_u = tf.get_variable( name="u", shape=(1, self._category), initializer=tf.initializers.random_normal(), trainable=False)
def testTrain(self): N = 5 W = 10 H = 10 C = 3 hidden_c = 5 x = tf.ones((N, H, W, C)) rb = ResidualBlock(hidden_c=hidden_c) with tf.GradientTape() as tape: outputs = rb(x) loss = tf.reduce_mean(tf.square(1 - outputs)) grads = tape.gradient(loss, rb.variables) optimizer = tf.train.GradientDescentOptimizer(0.001) optimizer.apply_gradients(zip(grads, rb.variables)) self.assertEqual(type(rb.bn1), tf.layers.BatchNormalization)
def testBuildAndRun(self): N = 5 W = 10 H = 10 C = 3 hidden_c = 5 x = tf.ones((N, H, W, C)) rb = ResidualBlock(hidden_c=hidden_c) outputs = rb(x) self.assertEqual(rb.in_c, C) self.assertEqual(rb.out_c, C) self.assertEqual(rb.hidden_c, hidden_c) self.assertEqual((N, H, W, C), outputs.shape) self.assertEqual((rb.ksize, rb.ksize, C, hidden_c), rb.conv1.kernel.shape) self.assertEqual(rb.conv1_u, None) self.assertEqual(rb.conv2_u, None)
def testTrainCategory(self): N = 5 W = 10 H = 10 C = 3 hidden_c = 5 x = tf.ones((N, H, W, C)) y = [[3], [1], [3], [1], [3]] rb = ResidualBlock(hidden_c=hidden_c, category=4) with tf.GradientTape() as tape: outputs = rb(x, labels=y) loss = tf.reduce_mean(tf.square(1 - outputs)) grads = tape.gradient(loss, rb.variables) optimizer = tf.train.GradientDescentOptimizer(0.001) optimizer.apply_gradients(zip(grads, rb.variables)) self.assertEqual(type(rb.bn1), ConditionalBatchNormalization)
def testBuildAndRunWithDownsampling(self): N = 5 W = 10 H = 10 C = 3 hidden_c = 5 x = tf.ones((N, H, W, C)) rb = ResidualBlock(hidden_c=hidden_c, is_use_bn=False, downsampling=True) outputs = rb(x) self.assertEqual(rb.in_c, C) self.assertEqual(rb.out_c, C) self.assertEqual(rb.hidden_c, hidden_c) self.assertEqual((N, H / 2, W / 2, C), outputs.shape) self.assertEqual((rb.ksize, rb.ksize, C, hidden_c), rb.conv1.kernel.shape) self.assertEqual((1, 1, C, C), rb.conv_shortcut.kernel.shape) self.assertFalse(hasattr(rb, 'bn1'))
def testBuildAndRunWithUpsampling(self): N = 5 W = 10 H = 10 C = 3 hidden_c = 5 x = tf.ones((N, H, W, C)) rb = ResidualBlock(hidden_c=hidden_c, upsampling=True) outputs = rb(x) self.assertEqual(rb.in_c, C) self.assertEqual(rb.out_c, C) self.assertEqual(rb.hidden_c, hidden_c) self.assertEqual((N, H * 2, W * 2, C), outputs.shape) self.assertEqual((rb.ksize, rb.ksize, C, hidden_c), rb.conv1.kernel.shape) self.assertEqual((1, 1, C, C), rb.conv_shortcut.kernel.shape) self.assertTrue(hasattr(rb, 'bn1')) self.assertEqual(rb.conv1_u, None) self.assertEqual(rb.conv2_u, None) self.assertEqual(rb.conv_shortcut_u, None)
def add_res_block(self, layer_name, first_filter_block_shape, downsample=False, weight_regulariser_strength=0.0001, depthwise_sep=False): num_filters, incoming_chans, f_rows, f_cols = first_filter_block_shape layer_list = [] if depthwise_sep: layer_list += self.depthwise_sep_layer(layer_name + "_dw1", incoming_chans, first_filter_block_shape, stride=2 if downsample else 1, padding=1, depthwise_weight_regulariser=None, pointwise_weight_regulariser=l2(strength=weight_regulariser_strength), final_relu=True, add_layers=False) else: layer_list.append(ConvLayer(layer_name + "_conv1", filter_block_shape=first_filter_block_shape, stride=2 if downsample else 1, padding=1, with_bias=False, weight_regulariser=l2(strength=weight_regulariser_strength))) layer_list.append(BatchNormLayer(layer_name + "_bn1", input_dimension=4, incoming_chans=num_filters)) layer_list.append(ReLu(layer_name + "_relu1")) if depthwise_sep: layer_list += self.depthwise_sep_layer(layer_name + "_dw2", num_filters, (num_filters,num_filters,f_rows,f_cols), stride=1, padding=1, depthwise_weight_regulariser=None, pointwise_weight_regulariser=l2(strength=weight_regulariser_strength), final_relu=False, add_layers=False) else: layer_list.append(ConvLayer(layer_name + "_conv2", filter_block_shape=(num_filters,num_filters,f_rows,f_cols), stride=1, padding=1, with_bias=False, weight_regulariser=l2(strength=weight_regulariser_strength))) layer_list.append(BatchNormLayer(layer_name + "_bn2", input_dimension=4, incoming_chans=num_filters)) if downsample: skip_proj = PointwiseConvLayer(layer_name + "_pw_skip", filter_block_shape=(num_filters,incoming_chans), stride=2, with_bias=False, weight_regulariser=l2(strength=weight_regulariser_strength)) else: skip_proj = None relu2 = ReLu(layer_name + "_relu2") self.add_layer(ResidualBlock(layer_name, layer_list=layer_list, skip_projection=skip_proj, post_skip_activation=relu2))
def testInit(self): ResidualBlock()