def build(self,inputs,targets): n=inputs.shape[0] if self.distance=='euclidean': dist=flow.math.pow(inputs,2) dist=flow.math.reduce_sum(dist, axis=1, keepdims=True) dist=np.tile(dist,(n, n)) dist_t=flow.transpose(dist) dist=dist+dist_t inputs_t=flow.transpose(inputs) dist=addmm(dist,inputs,inputs_t,beta=1,alpha=-2) dist=flow.clamp(min_value=1e-12) dist=flow.math.sqrt(dist) elif self.distance == 'cosine': fnorm=np.linalg.norm(inputs,ord=2,axis=1,keepdims=True) l2norm=np.tile(inputs,(inputs.shape)) l2norm=inputs/l2norm l2norm_t=flow.transpose(l2norm) dist=-np.matmul(l2norm,l2norm_t) target_expand=np.tile(targets,(n,n)) target_expand_t=flow.transpose(target_expand) mask=flow.math.equal(target_expand,target_expand_t) dist_ap, dist_an = [], [] for i in range(n): temp=np.ndarray.max(dist[i][mask[i]]) temp=flow.expand_dims(temp,axis=0) dist_ap.append(temp) temp=np.ndarray.min(dist[i][mask[i]==0]) temp=flow.expand_dims(temp,axis=0) dist_an.append(temp) dist_ap=flow.concat(dist_ap) dist_an=flow.concat(dist_an) y=flow.ones_like(dist_an) loss=self.ranking_loss(dist_an, dist_ap, y,margin=self.margin) return loss
def YoloTrainLayer(in_blob, gt_bbox_blob, gt_label_blob, gt_valid_num_blob, i): global layer_number layer_name = 'yolo-layer' + str(layer_number) # placeholder for a reshape from (n,h,w,255)->(n,h,w*3,85) blob = flow.transpose(in_blob, name=layer_name + '-yolo_transpose', perm=[0, 2, 3, 1]) reshape_blob = flow.reshape(blob, shape=(blob.shape[0], -1, 85), name=layer_name + '-yolo_reshape') position = flow.slice(reshape_blob, [None, 0, 0], [None, -1, 4], name=layer_name + '-yolo_slice_pos') xy = flow.slice(position, [None, 0, 0], [None, -1, 2], name=layer_name + '-yolo_slice_xy') wh = flow.slice(position, [None, 0, 2], [None, -1, 2], name=layer_name + '-yolo_slice_wh') xy = logistic(xy, name=layer_name + '-yolo_ligistic_xy') # xy = flow.math.sigmoid(xy, name = layer_name + '-yolo_ligistic_xy') position = flow.concat([xy, wh], axis=2, name=layer_name + '-yolo_concat') confidence = flow.slice(reshape_blob, [None, 0, 4], [None, -1, 81], name=layer_name + '-yolo_slice_prob') confidence = logistic(confidence, name=layer_name + '-yolo_ligistic_prob') # confidence = flow.math.sigmoid(confidence, name = layer_name+ '-yolo_ligistic_prob') objness = flow.slice(confidence, [None, 0, 0], [None, -1, 1], name=layer_name + '-yolo_slice_objness') clsprob = flow.slice(confidence, [None, 0, 1], [None, -1, 80], name=layer_name + '-yolo_slice_clsprob') bbox_loc_diff, pos_inds, pos_cls_label, neg_inds, valid_num, statistics_info = yolo_box_diff( position, gt_bbox_blob, gt_label_blob, gt_valid_num_blob, image_height=yolo_box_diff_conf[i]['image_height'], image_width=yolo_box_diff_conf[i]['image_width'], layer_height=yolo_box_diff_conf[i]['layer_height'], layer_width=yolo_box_diff_conf[i]['layer_width'], ignore_thresh=yolo_box_diff_conf[i]['ignore_thresh'], truth_thresh=yolo_box_diff_conf[i]['truth_thresh'], box_mask=yolo_box_diff_conf[i]['box_mask'], anchor_boxes_size=yolo_box_diff_conf[i]['anchor_boxes_size'], name=layer_name + '-yolo_box_loss') # placeholder for yolobox layer bbox_objness_out, bbox_clsprob_out = yolo_prob_loss(objness, clsprob, pos_inds, pos_cls_label, neg_inds, valid_num, num_classes=80, name=layer_name + '-yolo_prob_loss') bbox_loss = flow.concat( [bbox_loc_diff, bbox_objness_out, bbox_clsprob_out], axis=2, name=layer_name + '-loss_concat') bbox_loss_reduce_sum = flow.math.reduce_sum(bbox_loss, axis=[1, 2], name=layer_name + '-bbox_loss_reduce_sum') return bbox_loss_reduce_sum, statistics_info
def build(self, inputs, targets): """ Args: inputs (torch.Tensor): feature matrix with shape (batch_size, feat_dim). targets (torch.LongTensor): ground truth labels with shape (num_classes). """ n = inputs.shape[0] dist = math.reduce_sum(math.pow( inputs, flow.constant_like(inputs, 2, dtype=flow.float32)), axis=1) shape_tensor = flow.constant(value=0.0, dtype=flow.float32, shape=(n, n)) dist = flow.broadcast_like(dist, like=shape_tensor, broadcast_axes=[1]) dist = math.add( dist, flow.transpose(dist, perm=(1, 0), batch_axis_non_change=True)) temp1 = math.multiply( -2, flow.matmul( inputs, flow.transpose(inputs, perm=(1, 0), batch_axis_non_change=True))) dist = math.add(dist, temp1) dist = math.sqrt(flow.clamp(dist, min_value=1e-12)) mask = math.equal( flow.broadcast_like(targets, like=shape_tensor, broadcast_axes=[1]), flow.transpose(flow.broadcast_like(targets, like=shape_tensor, broadcast_axes=[1]), perm=(1, 0), batch_axis_non_change=True)) mask_rev = math.not_equal( flow.broadcast_like(targets, like=shape_tensor, broadcast_axes=[1]), flow.transpose(flow.broadcast_like(targets, like=shape_tensor, broadcast_axes=[1]), perm=(1, 0), batch_axis_non_change=True)) dist_ap, dist_an = [], [] for i in range(n): temp_dist = flow.slice_v2(dist, [(i, i + 1, 1)]) temp_mask = flow.slice_v2(mask, [(i, i + 1, 1)]) temp_mask_rev = flow.slice_v2(mask_rev, [(i, i + 1, 1)]) dist_ap.append( math.reduce_max( flow.gather_nd(temp_dist, flow.where(temp_mask)))) dist_an.append( math.reduce_min( flow.gather_nd(temp_dist, flow.where(temp_mask_rev)))) dist_ap = flow.concat(dist_ap, 0) dist_an = flow.concat(dist_an, 0) y = flow.ones_like(dist_an) # return dist_an, dist_ap, y return self._MarginRankingLoss(dist_an, dist_ap, y)
def __call__(self, x, y, bias, cache=None): """Apply attention mechanism to x and y. Args: x: a tensor with shape [batch_size, length_x, hidden_size] y: a tensor with shape [batch_size, length_y, hidden_size] bias: attention bias that will be added to the result of the dot product. cache: (Used during prediction) dictionary with tensors containing results of previous attentions. The dictionary must have the items: {"k": tensor with shape [batch_size, i, key_channels], "v": tensor with shape [batch_size, i, value_channels]} where i is the current decoded length. Returns: Attention layer output with shape [batch_size, length_x, hidden_size] """ # Linearly project the query (q), key (k) and value (v) using different # learned projections. This is in preparation of splitting them into # multiple heads. Multi-head attention uses multiple queries, keys, and # values rather than regular attention (which uses a single q, k, v). q = self._build_dense(x, self.hidden_size, name="dense_q") k = self._build_dense(y, self.hidden_size, name="dense_k") v = self._build_dense(y, self.hidden_size, name="dense_v") if cache is not None: # Combine cached keys and values with new keys and values. k = flow.concat([cache["k"], k], axis=1) v = flow.concat([cache["v"], v], axis=1) # Update cache cache["k"] = k cache["v"] = v q = self.split_heads(q) k = self.split_heads(k) v = self.split_heads(v) depth = (self.hidden_size // self.num_heads) q *= depth**-0.5 logits = flow.matmul(q, k, transpose_b=True) logits += bias weights = flow.nn.softmax(logits, name="attention_weights") if self.train: weights = flow.nn.dropout(weights, self.attention_dropout) attention_output = flow.matmul(weights, v) # Recombine heads --> [batch_size, length, hidden_size] attention_output = self.combine_heads(attention_output) # Run the combined outputs through another linear projection layer. attention_output = self._build_dense(attention_output, unit=self.hidden_size, name="output_transform") return attention_output
def InceptionD(in_blob, index): with flow.scope.namespace("mixed_{}".format(index)): with flow.scope.namespace("branch3x3"): branch3x3_1 = _conv2d_layer( "conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME" ) branch3x3_2 = _conv2d_layer( "conv1", branch3x3_1, filters=320, kernel_size=3, strides=2, padding="VALID", ) with flow.scope.namespace("branch7x7x3"): branch7x7x3_1 = _conv2d_layer( "conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME" ) branch7x7x3_2 = _conv2d_layer( "conv1", branch7x7x3_1, filters=192, kernel_size=[1, 7], strides=1, padding="SAME", ) branch7x7x3_3 = _conv2d_layer( "conv2", branch7x7x3_2, filters=192, kernel_size=[7, 1], strides=1, padding="SAME", ) branch7x7x3_4 = _conv2d_layer( "conv3", branch7x7x3_3, filters=192, kernel_size=3, strides=2, padding="VALID", ) with flow.scope.namespace("branch_pool"): branch_pool = flow.nn.max_pool2d( in_blob, ksize=3, strides=2, padding="VALID", data_format="NCHW", name="pool", ) inceptionD_bn = [] inceptionD_bn.append(branch3x3_2) inceptionD_bn.append(branch7x7x3_4) inceptionD_bn.append(branch_pool) mixed_concat = flow.concat(values=inceptionD_bn, axis=1, name="concat") return mixed_concat
def predict(self, images, anchors_s, anchors_l): ''' :param images: [N, 3, 416, 416] :param anchors_s: [anchor_per_scale, 2] :param anchors_l: [anchor_per_scale, 2] :return: [N, -1, 4+1+class_num] pred_bbox: [N, -1, 4] pred_conf: [N, -1, 1] pred_pred: [N, -1, class_num] ''' conv_lbbox, conv_sbbox = self.network(images) conv_sbbox = flow.transpose(conv_sbbox, perm=[0, 2, 3, 1]) conv_lbbox = flow.transpose(conv_lbbox, perm=[0, 2, 3, 1]) pred_s, _ = self.decode(conv_sbbox, anchors_s, self.strides[0], prefix='decode_s') pred_l, _ = self.decode(conv_lbbox, anchors_l, self.strides[1], prefix='decode_l') pred_s = flow.reshape(pred_s, [pred_s.shape[0], -1, pred_s.shape[-1]]) pred_l = flow.reshape(pred_l, [pred_l.shape[0], -1, pred_l.shape[-1]]) pred = flow.concat([pred_s, pred_l], axis=-2) # pred_bbox = flow.slice(pred, begin=[None, None, 0], size=[None, None, 4]) # pred_conf = flow.slice(pred, begin=[None, None, 4], size=[None, None, 1]) # pred_pred = flow.slice(pred, begin=[None, None, 5], size=[None, None, pred.shape[-1]-5]) # return pred_bbox, pred_conf, pred_pred return pred
def test_concat_dim_equal_runtime_error(test_case): with test_case.assertRaises(Exception) as context: x1 = flow.ones((2, 2), dtype=flow.float32, requires_grad=True) x2 = flow.ones((2, 2, 2), dtype=flow.float32, requires_grad=True) y = flow.concat([x1, x2]) test_case.assertTrue("Tensors must have same number of dimensions" in str(context.exception))
def test_stack_index_error(test_case): with test_case.assertRaises(Exception) as context: x1 = flow.ones((2, 1), dtype=flow.float32, requires_grad=True) x2 = flow.ones((2, 1), dtype=flow.float32, requires_grad=True) y = flow.concat([x1, x2], dim=4) test_case.assertTrue( "Dimension out of range" in str(context.exception))
def ConcatJob(): with flow.scope.placement(device_type, "0:0"): x = flow.get_variable( "x", shape=x_shape, dtype=type_name_to_flow_type[dtype], initializer=flow.random_uniform_initializer(minval=-10, maxval=10), trainable=True, ) y = flow.get_variable( "y", shape=y_shape, dtype=type_name_to_flow_type[dtype], initializer=flow.random_uniform_initializer(minval=-10, maxval=10), trainable=True, ) x = flow.cast_to_current_logical_view(x) y = flow.cast_to_current_logical_view(y) loss = flow.concat([x, y], axis) flow.optimizer.SGD(flow.optimizer.PiecewiseConstantScheduler( [], [1e-4]), momentum=0).minimize(loss) flow.watch(x, test_global_storage.Setter("x")) flow.watch_diff(x, test_global_storage.Setter("x_diff")) flow.watch(y, test_global_storage.Setter("y")) flow.watch_diff(y, test_global_storage.Setter("y_diff")) flow.watch(loss, test_global_storage.Setter("loss")) flow.watch_diff(loss, test_global_storage.Setter("loss_diff")) return loss
def hybrid_concat_job( input_0_def: oft.ListNumpy.Placeholder(shape=static_shape, dtype=flow.float), input_1_def: oft.ListNumpy.Placeholder(shape=static_shape, dtype=flow.float), ): var = flow.get_variable( "var", shape=static_shape, dtype=flow.float, initializer=flow.random_uniform_initializer(), trainable=True, ) constant = flow.constant(1.0, dtype=flow.float, shape=rand_sub_shape) inputs = [ flow.cast_to_current_logical_view(input) for input in [var, input_0_def, input_1_def, constant] ] concated = flow.concat(inputs, axis=axis, max_dim_size=max_dim_size,) if verbose: print("concated static shape:", concated.shape) flow.optimizer.SGD( flow.optimizer.PiecewiseConstantScheduler([], [1e-3]), momentum=0 ).minimize(concated) flow.watch_diff(var, compare_var_diff) if max_dim_size is None: test_case.assertTrue( concated.shape[axis] == (static_shape[axis] * 3 + rand_sub_shape[axis]) ) else: test_case.assertTrue(concated.shape[axis] == max_dim_size) return var, concated
def build_network(self, inputs, kernel_size=1, dw_size=3, stride=1, relu=True, times=1): output1 = conv2d_layer_with_bn("conv_gm" + str(times) + "_0", inputs, self.init_channels, kernel_size=kernel_size, strides=stride, padding="same", activation=None, use_bias=False) if relu: output1 = flow.nn.relu(output1) # assert filters.shape[filter_in_axis] == inputs.shape[in_channel_axis] // groups output2 = conv2d_layer_with_bn("conv_gm" + str(times) + "_1", output1, self.new_channels, kernel_size=dw_size, strides=1, padding="same", groups=self.init_channels, activation=None, use_bias=False) if relu: output2 = flow.nn.relu(output2) output = flow.concat([output1, output2], axis=1) return output #[:,:self.oup,:,:]
def test_concat_match_size_runtime_error(test_case): with test_case.assertRaises(Exception) as context: x1 = flow.ones((2, 2), dtype=flow.float32, requires_grad=True) x2 = flow.ones((2, 3), dtype=flow.float32, requires_grad=True) y = flow.concat([x1, x2]) test_case.assertTrue("Sizes of tensors must match except in dimension" in str(context.exception))
def _model(dense_fields, wide_sparse_fields, deep_sparse_fields): wide_sparse_fields = flow.parallel_cast( wide_sparse_fields, distribute=flow.distribute.broadcast()) wide_embedding_table = flow.get_variable( name='wide_embedding', shape=(FLAGS.wide_vocab_size, 1), initializer=flow.random_uniform_initializer(minval=-0.05, maxval=0.05), distribute=flow.distribute.split(0), ) wide_embedding = flow.gather(params=wide_embedding_table, indices=wide_sparse_fields) wide_embedding = flow.reshape(wide_embedding, shape=(-1, wide_embedding.shape[-1] * wide_embedding.shape[-2])) wide_scores = flow.math.reduce_sum(wide_embedding, axis=[1], keepdims=True) wide_scores = flow.parallel_cast( wide_scores, distribute=flow.distribute.split(0), gradient_distribute=flow.distribute.broadcast()) deep_sparse_fields = flow.parallel_cast( deep_sparse_fields, distribute=flow.distribute.broadcast()) deep_embedding_table = flow.get_variable( name='deep_embedding', shape=(FLAGS.deep_vocab_size, FLAGS.deep_embedding_vec_size), initializer=flow.random_uniform_initializer(minval=-0.05, maxval=0.05), distribute=flow.distribute.split(1), ) deep_embedding = flow.gather(params=deep_embedding_table, indices=deep_sparse_fields) deep_embedding = flow.parallel_cast( deep_embedding, distribute=flow.distribute.split(0), gradient_distribute=flow.distribute.split(2)) deep_embedding = flow.reshape(deep_embedding, shape=(-1, deep_embedding.shape[-1] * deep_embedding.shape[-2])) deep_features = flow.concat([deep_embedding, dense_fields], axis=1) for idx, units in enumerate(DEEP_HIDDEN_UNITS): deep_features = flow.layers.dense( deep_features, units=units, kernel_initializer=flow.glorot_uniform_initializer(), bias_initializer=flow.constant_initializer(0.0), activation=flow.math.relu, name='fc' + str(idx + 1)) deep_features = flow.nn.dropout(deep_features, rate=FLAGS.deep_dropout_rate) deep_scores = flow.layers.dense( deep_features, units=1, kernel_initializer=flow.glorot_uniform_initializer(), bias_initializer=flow.constant_initializer(0.0), name='fc' + str(len(DEEP_HIDDEN_UNITS) + 1)) scores = wide_scores + deep_scores return scores
def CSFI3(x1, x2, x3, trainable=True): x12 = flow.layers.upsample_2d(x1, (2, 2), interpolation='bilinear', name='upsanple4') x12 = conv1x1(x12, 64, "CSFI3_0", trainable=trainable) x12 = flow.math.relu(x12) x13 = flow.layers.upsample_2d(x1, (4, 4), interpolation='bilinear', name='upsanple5') x13 = conv1x1(x13, 64, "CSFI3_1", trainable=trainable) x13 = flow.math.relu(x13) x21 = conv3x3(x2, 64, "CSFI3_2", 2, trainable) x13 = flow.math.relu(x13) x23 = flow.layers.upsample_2d(x2, (2, 2), interpolation='bilinear', name='upsanple6') x23 = conv1x1(x23, 64, "CSFI3_3", trainable=trainable) x23 = flow.math.relu(x23) x31 = conv3x3(x3, 64, "CSFI3_4", 2, trainable) x31 = flow.math.relu(x31) x31 = conv3x3(x31, 64, "CSFI3_5", 2, trainable) x31 = flow.math.relu(x31) x32 = conv3x3(x3, 64, "CSFI3_6", 2, trainable) x32 = flow.math.relu(x32) x1 = conv3x3(flow.concat((x1, x21, x31), axis=1), 64, "CSFI3_7", trainable=trainable) x1 = flow.math.relu(x1) x2 = conv3x3(flow.concat((x2, x12, x32), axis=1), 64, "CSFI3_8", trainable=trainable) x2 = flow.math.relu(x2) x3 = conv3x3(flow.concat((x3, x13, x23), axis=1), 64, "CSFI3_9", trainable=trainable) x3 = flow.math.relu(x3) return x1, x2, x3
def fuse_layer4(inputs, filters=32, name='final_fuse'): x1, x2, x3, x4 = inputs x11 = x1 x21 = _conv2d_layer(f'{name}_conv2_1' + str(time), x2, filters, 1, 1, use_bias=False) x21 = _batch_norm(x21, momentum=0.1, epsilon=1e-5, name=f'{name}_bn2_1' + str(time), training=training) x21 = flow.layers.upsample_2d(x=x21, size=(2, 2), data_format="NHMC", name=f'{name}_up2_1') x31 = _conv2d_layer(f'{name}_conv3_1' + str(time), x3, filters, 1, 1, use_bias=False) x31 = _batch_norm(x31, momentum=0.1, epsilon=1e-5, name=f'{name}_bn3_1' + str(time), training=training) x31 = flow.layers.upsample_2d(x=x31, size=(4, 4), data_format="NHMC", name=f'{name}_up3_1') x41 = _conv2d_layer(f'{name}_conv4_1' + str(time), x4, filters, 1, 1, use_bias=False) x41 = _batch_norm(x41, momentum=0.1, epsilon=1e-5, name=f'{name}_bn4_1' + str(time), training=training) x41 = flow.layers.upsample_2d(x=x41, size=(8, 8), data_format="NHMC", name=f'{name}_up4_1') x = flow.concat(inputs=[x11, x21, x31, x41], axis=-1, name=f'{name}_out') return x
def concat(): variables = [] for i in range(4): variables.append( flow.get_variable( name=str(i), shape=(2, 3), dtype=flow.float, initializer=flow.random_uniform_initializer(), )) return flow.concat(variables, axis=1)
def CSFI2(x1, x2, trainable=True): x12 = flow.layers.upsample_2d(x1, (2, 2), interpolation='bilinear', name='upsanple2') x12 = conv1x1(x12, 64, "CSFI2_0", trainable=trainable) x12 = flow.math.relu(x12) x21 = conv3x3(x2, 64, "CSFI2_1", 2, trainable) x21 = flow.math.relu(x21) x1 = conv3x3(flow.concat((x1, x21), axis=1), 64, "CSFI2_2", trainable=trainable) x1 = flow.math.relu(x1) x2 = conv3x3(flow.concat((x2, x12), axis=1), 64, "CSFI2_3", trainable=trainable) x2 = flow.math.relu(x2) return x1, x2
def bis(self, input, index): # batch index select # input: [N, ?, ?, ...] # dim: scalar > 0 # index: [N, idx] views = [input.shape[0] ] + [1 if i != 2 else -1 for i in range(1, len(input.shape))] index = flow.reshape(index, shape=views) index_list = [] for i in range(input.shape[1]): index_list.append(index) index = flow.concat(inputs=index_list, axis=1) return flow.dim_gather(input, 2, index)
def InceptionB(in_blob, index): with flow.scope.namespace("mixed_{}".format(index)): with flow.scope.namespace("branch3x3"): branch3x3 = conv2d_layer("conv0", in_blob, filters=384, kernel_size=3, strides=2, padding="VALID") with flow.scope.namespace("branch3x3dbl"): branch3x3dbl_1 = conv2d_layer("conv0", in_blob, filters=64, kernel_size=1, strides=1, padding="SAME") branch3x3dbl_2 = conv2d_layer( "conv1", branch3x3dbl_1, filters=96, kernel_size=3, strides=1, padding="SAME", ) branch3x3dbl_3 = conv2d_layer( "conv2", branch3x3dbl_2, filters=96, kernel_size=3, strides=2, padding="VALID", ) with flow.scope.namespace("branch_pool"): branch_pool = flow.nn.max_pool2d( in_blob, ksize=3, strides=2, padding="VALID", data_format="NCHW", name="pool0", ) inceptionB_bn = [] inceptionB_bn.append(branch3x3) inceptionB_bn.append(branch3x3dbl_3) inceptionB_bn.append(branch_pool) mixed_concat = flow.concat(values=inceptionB_bn, axis=1, name="concat") return mixed_concat
def static_concat_job( input_0_def: oft.Numpy.Placeholder(shape=shape, dtype=flow.float), input_1_def: oft.Numpy.Placeholder(shape=shape, dtype=flow.float), ): var = flow.get_variable( "var", shape=shape, dtype=flow.float, initializer=flow.random_uniform_initializer(), trainable=True, ) concated = flow.concat([input_0_def, input_1_def, var], axis=axis) test_case.assertTrue(not concated.is_dynamic) flow.losses.add_loss(concated) flow.watch_diff(var, compare_var_diff) return var, concated
def dynamic_concat_job( input_0_def: oft.ListNumpy.Placeholder( shape=input_static_shape, dtype=flow.float ), input_1_def: oft.ListNumpy.Placeholder( shape=input_static_shape, dtype=flow.float ), ): var_0 = flow.get_variable( "Var0", shape=(1,), dtype=flow.float, initializer=flow.constant_initializer(value=1, dtype=flow.float), trainable=True, ) var_1 = flow.get_variable( "Var1", shape=(1,), dtype=flow.float, initializer=flow.constant_initializer(value=1, dtype=flow.float), trainable=True, ) var_0 = flow.cast_to_current_logical_view(var_0) var_1 = flow.cast_to_current_logical_view(var_1) input_0_def = flow.cast_to_current_logical_view(input_0_def) input_1_def = flow.cast_to_current_logical_view(input_1_def) if callable(watch_cb): flow.watch(var_0, watch_cb) flow.watch(var_1, watch_cb) flow.watch(flow.identity(input_0_def), watch_cb) flow.watch(flow.identity(input_1_def), watch_cb) var_0 = var_0 * input_0_def var_1 = var_1 * input_1_def if callable(watch_cb): flow.watch(var_0, watch_cb) flow.watch(var_1, watch_cb) result = flow.concat( [var_0, var_1], axis=axis, max_dim_size=input_static_shape[axis] ) flow.optimizer.SGD( flow.optimizer.PiecewiseConstantScheduler([], [1e-4]), momentum=0 ).minimize(result) flow.watch_diff(var_0, make_watch_diff_cb(0)) flow.watch_diff(var_1, make_watch_diff_cb(1)) return result
def static_concat_job( input_0_def: oft.Numpy.Placeholder(shape=shape, dtype=flow.float), input_1_def: oft.Numpy.Placeholder(shape=shape, dtype=flow.float), ): var = flow.get_variable( "var", shape=shape, dtype=flow.float, initializer=flow.random_uniform_initializer(), trainable=True, ) concated = flow.concat([input_0_def, input_1_def, var], axis=axis) test_case.assertTrue(not concated.is_dynamic) flow.optimizer.SGD(flow.optimizer.PiecewiseConstantScheduler([], [1e-3]), momentum=0).minimize(concated) flow.watch_diff(var, compare_var_diff) return var, concated
def block8(in_blob): """Builds the 8x8 resnet block.""" with flow.scope.namespace("Block8"): with flow.scope.namespace("Branch_0"): tower_conv = conv2d_layer(in_blob, 192, 1, padding="SAME", name="Conv2d_1x1") with flow.scope.namespace("Branch_1"): tower_conv1_0 = conv2d_layer(in_blob, 192, 1, padding="SAME", name="Conv2d_0a_1x1") tower_conv1_1 = conv2d_layer(tower_conv1_0, 224, [1, 3], padding="SAME", name="Conv2d_0b_1x3") tower_conv1_2 = conv2d_layer(tower_conv1_1, 256, [3, 1], padding="SAME", name="Conv2d_0c_3x1") mixed_B8 = [] mixed_B8.append(tower_conv) mixed_B8.append(tower_conv1_2) mixed = flow.concat(values=mixed_B8, axis=1, name="concat3") up = conv2d_layer(mixed, in_blob.shape[1], 1, padding="SAME", name="Conv2d_1x1") scaled_up = up * 1.0 in_blob += scaled_up in_blob = flow.nn.relu(in_blob) return in_blob
def block17(in_blob): """Builds the 17x17 resnet block.""" with flow.scope.namespace("Block17"): with flow.scope.namespace("Branch_0"): tower_conv = conv2d_layer(in_blob, 192, 1, padding="SAME", name="Conv2d_1x1") with flow.scope.namespace("Branch_1"): tower_conv1_0 = conv2d_layer(in_blob, 128, 1, padding="SAME", name="Conv2d_0a_1x1") tower_conv1_1 = conv2d_layer(tower_conv1_0, 160, [1, 7], padding="SAME", name="Conv2d_0b_1x7") tower_conv1_2 = conv2d_layer(tower_conv1_1, 192, [7, 1], padding="SAME", name="Conv2d_0c_7x1") mixed_B17 = [] mixed_B17.append(tower_conv) mixed_B17.append(tower_conv1_2) mixed = flow.concat(values=mixed_B17, axis=1, name="concat2") up = conv2d_layer(mixed, in_blob.shape[1], 1, padding="SAME", name="Conv2d_1x1") scaled_up = up * 1.0 in_blob += scaled_up in_blob = flow.nn.relu(in_blob) return in_blob
def MergeTail(x1, x2, x3, trainable=True): x13 = flow.layers.upsample_2d(x1, (4, 4), interpolation='bilinear', name='upsanple7') x13 = conv1x1(x13, 64, "MergeTail_0", trainable=trainable) x13 = flow.math.relu(x13) x23 = flow.layers.upsample_2d(x2, (2, 2), interpolation='bilinear', name='upsanple8') x23 = conv1x1(x23, 64, "MergeTail_1", trainable=trainable) x23 = flow.math.relu(x23) x = conv3x3(flow.concat((x3, x13, x23), axis=1), 64, "MergeTail_2", trainable=trainable) x = flow.math.relu(x) x = conv3x3(x, 32, "MergeTail_3", trainable=trainable) x = conv1x1(x, 3, "MergeTail_4", trainable=trainable) x = flow.clamp(x, -1, 1) return x
def YoloPredictLayer(in_blob, origin_image_info, i, trainable): global layer_number layer_name = 'yolo-layer' + str(layer_number) #placeholder for a reshape from (n,h,w,255)->(n,h,w*3,85) blob = flow.transpose(in_blob, name=layer_name + '-yolo_transpose', perm=[0, 2, 3, 1]) reshape_blob = flow.reshape(blob, shape=(blob.shape[0], -1, 85), name=layer_name + '-yolo_reshape') position = flow.slice(reshape_blob, [None, 0, 0], [None, -1, 4], name=layer_name + '-yolo_slice_pos') xy = flow.slice(position, [None, 0, 0], [None, -1, 2], name=layer_name + '-yolo_slice_xy') wh = flow.slice(position, [None, 0, 2], [None, -1, 2], name=layer_name + '-yolo_slice_wh') xy = flow.math.sigmoid(xy, name=layer_name + '-yolo_ligistic_xy') position = flow.concat([xy, wh], axis=2, name=layer_name + '-yolo_concat') confidence = flow.slice(reshape_blob, [None, 0, 4], [None, -1, 81], name=layer_name + '-yolo_slice_prob') confidence = flow.math.sigmoid(confidence, name=layer_name + '-yolo_ligistic_prob') #[out_bbox, out_probs, valid_num] = flow.detection.yolo_detect(bbox=position, probs=confidence, origin_image_info=origin_image_info, image_height=608, image_width=608, layer_height=yolo_conf[i]['layer_height'], layer_width=yolo_conf[i]['layer_width'], prob_thresh=0.5, num_classes=80, max_out_boxes = max_out_boxes, anchor_boxes=yolo_conf[i]['anchor_boxes_size']) [out_bbox, out_probs, valid_num ] = flow.yolo_detect(bbox=position, probs=confidence, origin_image_info=origin_image_info, image_height=608, image_width=608, layer_height=yolo_conf[i]['layer_height'], layer_width=yolo_conf[i]['layer_width'], prob_thresh=0.5, num_classes=80, max_out_boxes=max_out_boxes, anchor_boxes=yolo_conf[i]['anchor_boxes_size'], name=str(layer_name) + "yolo_detect") #print("out_bbox.shape",out_bbox.shape) return out_bbox, out_probs, valid_num
def discriminator(self, inputs, targets, trainable=True, reuse=False, const_init=False): # (n, 6, 256, 256) d0 = flow.concat([inputs, targets], axis=1) # (n, 64, 128, 128) d1 = self._downsample( d0, 64, 4, name="d_d1", apply_batchnorm=False, reuse=reuse, const_init=const_init, trainable=trainable, ) # (n, 64, 64, 64) d2 = self._downsample(d1, 128, 4, name="d_d2", reuse=reuse, const_init=const_init) # (n, 256, 32, 32) d3 = self._downsample(d2, 256, 4, name="d_d3", reuse=reuse, const_init=const_init) # (n, 256, 34, 34) pad1 = flow.pad(d3, [[0, 0], [0, 0], [1, 1], [1, 1]]) # (n, 512, 31, 31) conv1 = layers.conv2d( pad1, 512, 4, strides=1, padding="valid", name="d_conv1", trainable=trainable, reuse=reuse, const_init=const_init, use_bias=False, ) bn1 = layers.batchnorm(conv1, name="d_bn", reuse=reuse, trainable=trainable) leaky_relu = flow.nn.leaky_relu(bn1, alpha=0.3) # (n, 512, 33, 33) pad2 = flow.pad(leaky_relu, [[0, 0], [0, 0], [1, 1], [1, 1]]) # (n, 1, 30, 30) conv2 = layers.conv2d( pad2, 1, 4, strides=1, padding="valid", name="d_conv2", trainable=trainable, reuse=reuse, const_init=const_init, ) return conv2
def InceptionE(in_blob, index, pooltype): with flow.scope.namespace("mixed_{}".format(index)): with flow.scope.namespace("branch1x1"): branch1x1 = conv2d_layer("conv0", in_blob, filters=320, kernel_size=1, strides=1, padding="SAME") with flow.scope.namespace("branch3x3"): branch3x3_1 = conv2d_layer("conv0", in_blob, filters=384, kernel_size=1, strides=1, padding="SAME") branch3x3_2 = conv2d_layer( "conv1", branch3x3_1, filters=384, kernel_size=[1, 3], strides=1, padding="SAME", ) branch3x3_3 = conv2d_layer( "conv2", branch3x3_1, filters=384, kernel_size=[3, 1], strides=[1, 1], padding="SAME", ) inceptionE_1_bn = [] inceptionE_1_bn.append(branch3x3_2) inceptionE_1_bn.append(branch3x3_3) concat_branch3x3 = flow.concat(values=inceptionE_1_bn, axis=1, name="concat") with flow.scope.namespace("branch3x3dbl"): branch3x3dbl_1 = conv2d_layer("conv0", in_blob, filters=448, kernel_size=1, strides=1, padding="SAME") branch3x3dbl_2 = conv2d_layer( "conv1", branch3x3dbl_1, filters=384, kernel_size=3, strides=1, padding="SAME", ) branch3x3dbl_3 = conv2d_layer( "conv2", branch3x3dbl_2, filters=384, kernel_size=[1, 3], strides=1, padding="SAME", ) branch3x3dbl_4 = conv2d_layer( "conv3", branch3x3dbl_2, filters=384, kernel_size=[3, 1], strides=1, padding="SAME", ) inceptionE_2_bn = [] inceptionE_2_bn.append(branch3x3dbl_3) inceptionE_2_bn.append(branch3x3dbl_4) concat_branch3x3dbl = flow.concat(values=inceptionE_2_bn, axis=1, name="concat") with flow.scope.namespace("branch_pool"): if pooltype == 'avg': branch_pool_1 = flow.nn.avg_pool2d( in_blob, ksize=3, strides=1, padding="SAME", data_format="NCHW", name="pool", ) elif pooltype == 'max': branch_pool_1 = flow.nn.max_pool2d( in_blob, ksize=3, strides=1, padding="SAME", data_format="NCHW", name="pool", ) branch_pool_2 = conv2d_layer( "conv", branch_pool_1, filters=192, kernel_size=[1, 1], strides=1, padding="SAME", ) inceptionE_total_bn = [] inceptionE_total_bn.append(branch1x1) inceptionE_total_bn.append(concat_branch3x3) inceptionE_total_bn.append(concat_branch3x3dbl) inceptionE_total_bn.append(branch_pool_2) concat_total = flow.concat(values=inceptionE_total_bn, axis=1, name="concat") return concat_total
def generator(self, inputs, trainable=True, const_init=False): if const_init: apply_dropout = False else: apply_dropout = True # (n, 64, 128, 128) d1 = self._downsample( inputs, 64, 4, const_init=const_init, trainable=trainable, apply_batchnorm=False, name="g_d1", ) # (n, 128, 64, 64) d2 = self._downsample(d1, 128, 4, const_init=const_init, trainable=trainable, name="g_d2") # (n, 256, 32, 32) d3 = self._downsample(d2, 256, 4, const_init=const_init, trainable=trainable, name="g_d3") # (n, 512, 16, 16) d4 = self._downsample(d3, 512, 4, const_init=const_init, trainable=trainable, name="g_d4") # (n, 512, 8, 8) d5 = self._downsample(d4, 512, 4, const_init=const_init, trainable=trainable, name="g_d5") # (n, 512, 4, 4) d6 = self._downsample(d5, 512, 4, const_init=const_init, trainable=trainable, name="g_d6") # (n, 512, 2, 2) d7 = self._downsample(d6, 512, 4, const_init=const_init, trainable=trainable, name="g_d7") # (n, 512, 1, 1) d8 = self._downsample(d7, 512, 4, const_init=const_init, trainable=trainable, name="g_d8") # (n, 1024, 2, 2) u7 = self._upsample( d8, 512, 4, const_init=const_init, trainable=trainable, apply_dropout=apply_dropout, name="g_u7", ) u7 = flow.concat([u7, d7], axis=1) # (n, 1024, 4, 4) u6 = self._upsample( u7, 512, 4, const_init=const_init, trainable=trainable, apply_dropout=apply_dropout, name="g_u6", ) u6 = flow.concat([u6, d6], axis=1) # (n, 1024, 8, 8) u5 = self._upsample( u6, 512, 4, const_init=const_init, trainable=trainable, apply_dropout=apply_dropout, name="g_u5", ) u5 = flow.concat([u5, d5], axis=1) # (n, 1024, 16, 16) u4 = self._upsample(u5, 512, 4, const_init=const_init, trainable=trainable, name="g_u4") u4 = flow.concat([u4, d4], axis=1) # (n, 512, 32, 32) u3 = self._upsample(u4, 256, 4, const_init=const_init, trainable=trainable, name="g_u3") u3 = flow.concat([u3, d3], axis=1) # (n, 256, 64, 64) u2 = self._upsample(u3, 128, 4, const_init=const_init, trainable=trainable, name="g_u2") u2 = flow.concat([u2, d2], axis=1) # (n, 128, 128, 128) u1 = self._upsample(u2, 64, 4, const_init=const_init, trainable=trainable, name="g_u1") u1 = flow.concat([u1, d1], axis=1) # (n, 3, 256, 256) u0 = layers.deconv2d( u1, self.out_channels, 4, name="g_u0_deconv", const_init=const_init, trainable=trainable, ) u0 = flow.math.tanh(u0) return u0
def InceptionC(in_blob, index, filters): with flow.scope.namespace("mixed_{}".format(index)): with flow.scope.namespace("branch1x1"): branch1x1 = conv2d_layer("conv0", in_blob, filters=192, kernel_size=1, strides=1, padding="SAME") with flow.scope.namespace("branch7x7"): branch7x7_1 = conv2d_layer( "conv0", in_blob, filters=filters, kernel_size=1, strides=1, padding="SAME", ) branch7x7_2 = conv2d_layer( "conv1", branch7x7_1, filters=filters, kernel_size=[1, 7], strides=1, padding="SAME", ) branch7x7_3 = conv2d_layer( "conv2", branch7x7_2, filters=192, kernel_size=[7, 1], strides=[1, 1], padding="SAME", ) with flow.scope.namespace("branch7x7dbl"): branch7x7dbl_1 = conv2d_layer( "conv0", in_blob, filters=filters, kernel_size=1, strides=1, padding="SAME", ) branch7x7dbl_2 = conv2d_layer( "conv1", branch7x7dbl_1, filters=filters, kernel_size=[7, 1], strides=1, padding="SAME", ) branch7x7dbl_3 = conv2d_layer( "conv2", branch7x7dbl_2, filters=filters, kernel_size=[1, 7], strides=1, padding="SAME", ) branch7x7dbl_4 = conv2d_layer( "conv3", branch7x7dbl_3, filters=filters, kernel_size=[7, 1], strides=1, padding="SAME", ) branch7x7dbl_5 = conv2d_layer( "conv4", branch7x7dbl_4, filters=192, kernel_size=[1, 7], strides=1, padding="SAME", ) with flow.scope.namespace("branch_pool"): branch_pool_1 = flow.nn.avg_pool2d( in_blob, ksize=3, strides=1, padding="SAME", data_format="NCHW", name="pool", ) branch_pool_2 = conv2d_layer( "conv", branch_pool_1, filters=192, kernel_size=[1, 1], strides=1, padding="SAME", ) inceptionC_bn = [] inceptionC_bn.append(branch1x1) inceptionC_bn.append(branch7x7_3) inceptionC_bn.append(branch7x7dbl_5) inceptionC_bn.append(branch_pool_2) mixed_concat = flow.concat(values=inceptionC_bn, axis=1, name="concat") return mixed_concat