def get_fused_P_feature(p_features, stage, dim_reduced, init, norm): prefix = "S{}_".format(stage) with mx.name.Prefix(prefix): P3_0 = p_features['S{}_P3'.format(stage - 1)] # s8 P4_0 = p_features['S{}_P4'.format(stage - 1)] # s16 P5_0 = p_features['S{}_P5'.format(stage - 1)] # s32 P6_0 = p_features['S{}_P6'.format(stage - 1)] # s64 P7_0 = p_features['S{}_P7'.format(stage - 1)] # s128 # P4_1 = gp(P6_0, P4_0) P6_0_to_P4 = mx.sym.UpSampling(P6_0, scale=4, sample_type='nearest', name="P6_0_to_P4", num_args=1) P6_0_to_P4 = mx.sym.slice_like(P6_0_to_P4, P4_0) P4_1 = merge_gp(P6_0_to_P4, P4_0, name="gp_P6_0_P4_0") P4_1 = X.reluconvbn(P4_1, dim_reduced, init, norm, name="P4_1", prefix=prefix) # P4_2 = sum(P4_0, P4_1) P4_2 = X.merge_sum([P4_0, P4_1], name="sum_P4_0_P4_1") P4_2 = X.reluconvbn(P4_2, dim_reduced, init, norm, name="P4_2", prefix=prefix) # P3_3 = sum(P4_2, P3_0) end node P4_2_to_P3 = mx.sym.UpSampling(P4_2, scale=2, sample_type='nearest', name="P4_2_to_P3", num_args=1) P4_2_to_P3 = mx.sym.slice_like(P4_2_to_P3, P3_0) P3_3 = X.merge_sum([P4_2_to_P3, P3_0], name="sum_P4_2_P3_0") P3_3 = X.reluconvbn(P3_3, dim_reduced, init, norm, name="P3_3", prefix=prefix) P3 = P3_3 # P4_4 = sum(P4_2, P3_3) end node P3_3_to_P4 = X.pool(P3_3, name="P3_3_to_P4", kernel=2, stride=2, pad=0) P3_3_to_P4 = mx.sym.slice_like(P3_3_to_P4, P4_2) P4_4 = X.merge_sum([P4_2, P3_3_to_P4], name="sum_P4_4_P3_3") P4_4 = X.reluconvbn(P4_4, dim_reduced, init, norm, name="P4_4", prefix=prefix) P4 = P4_4 # P5_5 = sum(gp(P4_4, P3_3), P5_0) end node P4_4_to_P5 = X.pool(P4_4, kernel=2, stride=2, name="P4_4_to_P5", pad=0) P4_4_to_P5 = mx.sym.slice_like(P4_4_to_P5, P5_0) P3_3_to_P5 = X.pool(P3_3, kernel=4, stride=4, name="P3_3_to_P5", pad=0) P3_3_to_P5 = mx.sym.slice_like(P3_3_to_P5, P5_0) gp_P4_4_P3_3 = merge_gp(P4_4_to_P5, P3_3_to_P5, name="gp_P4_4_P3_3") P5_5 = X.merge_sum([gp_P4_4_P3_3, P5_0], name="sum_[gp_P4_4_P3_3]_P5_0") P5_5 = X.reluconvbn(P5_5, dim_reduced, init, norm, name="P5_5", prefix=prefix) P5 = P5_5 # P7_6 = sum(gp(P5_5, P4_2), P7_0) end node P4_2_to_P7 = X.pool(P4_2, name="P4_2_to_P7", kernel=8, stride=8, pad=0) P4_2_to_P7 = mx.sym.slice_like(P4_2_to_P7, P7_0) P5_5_to_P7 = X.pool(P5_5, name="P5_5_to_P7", kernel=4, stride=4, pad=0) P5_5_to_P7 = mx.sym.slice_like(P5_5_to_P7, P7_0) gp_P5_5_P4_2 = merge_gp(P5_5_to_P7, P4_2_to_P7, name="gp_P5_5_P4_2") P7_6 = X.merge_sum([gp_P5_5_P4_2, P7_0], name="sum_[gp_P5_5_P4_2]_P7_0") P7_6 = X.reluconvbn(P7_6, dim_reduced, init, norm, name="P7_6", prefix=prefix) P7 = P7_6 # P6_7 = gp(P7_6, P5_5) end node P7_6_to_P6 = mx.sym.UpSampling(P7_6, scale=2, sample_type='nearest', name="P7_6_to_P6", num_args=1) P7_6_to_P6 = mx.sym.slice_like(P7_6_to_P6, P6_0) P5_5_to_P6 = X.pool(P5_5, name="p5_5_to_P6", kernel=2, stride=2, pad=0) P5_5_to_P6 = mx.sym.slice_like(P5_5_to_P6, P6_0) P6_7 = merge_gp(P7_6_to_P6, P5_5_to_P6, name="gp_P7_6_to_P6_P5_5_to_P6") P6_7 = X.reluconvbn(P6_7, dim_reduced, init, norm, name="P6_7", prefix=prefix) P6 = P6_7 return { 'S{}_P3'.format(stage): P3, 'S{}_P4'.format(stage): P4, 'S{}_P5'.format(stage): P5, 'S{}_P6'.format(stage): P6, 'S{}_P7'.format(stage): P7 }
def get_fused_P_feature(self, stage, dim_reduced, init, norm): prefix = "S{}_".format(stage) level_list = [2, 3, 4, 5, 6] feature_list = [] self.feature_grids.append(feature_list) with mx.name.Prefix(prefix): for index, level in enumerate(level_list): fusion_list = [] # bottom feature (Same Up) bottom_ind_x = stage bottom_ind_y = index - 1 if bottom_ind_x >= 0 and bottom_ind_y >= 0 and bottom_ind_y < len( level_list): fusion_list.append( self.same_up(bottom_ind_x, bottom_ind_y, dim_reduced, init, norm)) # bottom left feature (Across Up) bottom_ind_x = stage - 1 bottom_ind_y = index - 1 if bottom_ind_x >= 0 and bottom_ind_y >= 0 and bottom_ind_y < len( level_list): fusion_list.append( self.across_up(bottom_ind_x, bottom_ind_y, dim_reduced, init, norm)) # left feature (Across Same) bottom_ind_x = stage - 1 bottom_ind_y = index if bottom_ind_x >= 0 and bottom_ind_y >= 0 and bottom_ind_y < len( level_list): fusion_list.append( self.across_same(bottom_ind_x, bottom_ind_y, dim_reduced, init, norm)) # upper left feature (Across Down) bottom_ind_x = stage - 1 bottom_ind_y = index + 1 if bottom_ind_x >= 0 and bottom_ind_y >= 0 and bottom_ind_y < len( level_list): fusion_list.append( self.across_down(bottom_ind_x, bottom_ind_y, dim_reduced, init, norm)) # stage 0 feature (Across Skip) bottom_ind_x = 0 bottom_ind_y = index if bottom_ind_x >= 0 and bottom_ind_y >= 0 and bottom_ind_y < len( level_list): fusion_list.append( self.across_skip(bottom_ind_x, bottom_ind_y, stage, dim_reduced, init, norm)) name = "S%s_P%s" % (stage, level) fusion_feature = X.merge_sum(fusion_list, name='sum_' + name) feature_grid = X.conv( data=fusion_feature, filter=dim_reduced, kernel=3, pad=1, stride=1, no_bias=False, weight=X.var(name=name + "_weight", init=init), bias=X.var(name=name + "_bias", init=X.zero_init()), name=name + '_conv', ) self.feature_grids[stage].append(feature_grid)
def get_fused_P_feature(p_features, stage, dim_reduced, init, norm): prefix = "S{}_".format(stage) with mx.name.Prefix(prefix): P3_0 = p_features['S{}_P3'.format(stage - 1)] # s8 P4_0 = p_features['S{}_P4'.format(stage - 1)] # s16 P5_0 = p_features['S{}_P5'.format(stage - 1)] # s32 P6_0 = p_features['S{}_P6'.format(stage - 1)] # s64 P7_0 = p_features['S{}_P7'.format(stage - 1)] # s128 # P7_1 P7_1 = P7_0 # P6_1 = sum(P6_0, P7_1) P7_1_to_P6 = mx.sym.UpSampling(P7_1, scale=2, sample_type='nearest', name="P7_1_to_P6", num_args=1) P6_1 = X.merge_sum([P6_0, P7_1_to_P6], name="sum_P6_0_P7_1") P6_1 = X.reluconvbn(P6_1, dim_reduced, init, norm, name="P6_1", prefix=prefix) # P5_1 = sum(P5_0, P6_1) P6_1_to_P5 = mx.sym.UpSampling(P6_1, scale=2, sample_type='nearest', name="P6_1_to_P5", num_args=1) P5_1 = X.merge_sum([P5_0, P6_1_to_P5], name="sum_P5_0_P6_1") P5_1 = X.reluconvbn(P5_1, dim_reduced, init, norm, name="P5_1", prefix=prefix) # P4_1 = sum(P4_0, P5_1) P5_1_to_P4 = mx.sym.UpSampling(P5_1, scale=2, sample_type='nearest', name="P5_1_to_P4", num_args=1) P4_1 = X.merge_sum([P4_0, P5_1_to_P4], name="sum_P4_0_P5_1") P4_1 = X.reluconvbn(P4_1, dim_reduced, init, norm, name="P4_1", prefix=prefix) # P3_1 = sum(P3_0, P4_1) P4_1_to_P3 = mx.sym.UpSampling(P4_1, scale=2, sample_type='nearest', name="P4_1_to_P3", num_args=1) P3_1 = X.merge_sum([P3_0, P4_1_to_P3], name="sum_P3_0_P4_1") P3_1 = X.reluconvbn(P3_1, dim_reduced, init, norm, name="P3_1", prefix=prefix) P3_2 = P3_1 P3 = P3_2 # P4_2 = sum(P3_2, P4_1) P3_2_to_P4 = X.pool(P3_2, name="P3_2_to_P4", kernel=2, stride=2, pad=0) P4_2 = X.merge_sum([P4_1, P3_2_to_P4], name="sum_P4_1_P3_2") P4_2 = X.reluconvbn(P4_2, dim_reduced, init, norm, name="P4_2", prefix=prefix) P4 = P4_2 # P5_2 = sum(P4_2, P5_1) P4_2_to_P5 = X.pool(P4_2, name="P4_2_to_P5", kernel=2, stride=2, pad=0) P5_2 = X.merge_sum([P5_1, P4_2_to_P5], name="sum_P5_1_P4_2") P5_2 = X.reluconvbn(P5_2, dim_reduced, init, norm, name="P5_2", prefix=prefix) P5 = P5_2 # P6_2 = sum(P5_2, P6_1) P5_2_to_P6 = X.pool(P5_2, name="P5_2_to_P6", kernel=2, stride=2, pad=0) P6_2 = X.merge_sum([P6_1, P5_2_to_P6], name="sum_P6_1_P5_2") P6_2 = X.reluconvbn(P6_2, dim_reduced, init, norm, name="P6_2", prefix=prefix) P6 = P6_2 # P7_2 = sum(P6_2, P7_1) P6_2_to_P7 = X.pool(P6_2, name="P6_2_to_P7", kernel=2, stride=2, pad=0) P7_2 = X.merge_sum([P7_1, P6_2_to_P7], name="sum_P7_1_P6_2") P7_2 = X.reluconvbn(P7_2, dim_reduced, init, norm, name="P7_2", prefix=prefix) P7 = P7_2 return { 'S{}_P3'.format(stage): P3, 'S{}_P4'.format(stage): P4, 'S{}_P5'.format(stage): P5, 'S{}_P6'.format(stage): P6, 'S{}_P7'.format(stage): P7 }
def get_fused_P_feature(p_features, stage, dim_reduced, init, norm): prefix = "S{}_".format(stage) with mx.name.Prefix(prefix): P2_0 = p_features['S{}_P2'.format(stage - 1)] # s4 P3_0 = p_features['S{}_P3'.format(stage - 1)] # s8 P4_0 = p_features['S{}_P4'.format(stage - 1)] # s16 P5_0 = p_features['S{}_P5'.format(stage - 1)] # s32 P6_0 = p_features['S{}_P6'.format(stage - 1)] # s64 P6_1 = P6_0 P6_1_to_P5 = mx.sym.UpSampling(P6_1, scale=2, sample_type='nearest', name="P6_1_to_P5", num_args=1) P6_1_to_P5 = mx.sym.slice_like(P6_1_to_P5, P5_0) P5_1 = X.merge_sum([P5_0, P6_1_to_P5], name="sum_P5_0_P6_1") P5_1 = X.reluconvbn(P5_1, dim_reduced, init, norm, name="P5_1", prefix=prefix) # P4_1 = sum(P4_0, P5_1) P5_1_to_P4 = mx.sym.UpSampling(P5_1, scale=2, sample_type='nearest', name="P5_1_to_P4", num_args=1) P5_1_to_P4 = mx.sym.slice_like(P5_1_to_P4, P4_0) P4_1 = X.merge_sum([P4_0, P5_1_to_P4], name="sum_P4_0_P5_1") P4_1 = X.reluconvbn(P4_1, dim_reduced, init, norm, name="P4_1", prefix=prefix) P4_1_to_P3 = mx.sym.UpSampling(P4_1, scale=2, sample_type='nearest', name="P4_1_to_P3", num_args=1) P4_1_to_P3 = mx.sym.slice_like(P4_1_to_P3, P3_0) P3_1 = X.merge_sum([P3_0, P4_1_to_P3], name="sum_P3_0_P4_1") P3_1 = X.reluconvbn(P3_1, dim_reduced, init, norm, name="P3_1", prefix=prefix) P3_1_to_P2 = mx.sym.UpSampling(P3_1, scale=2, sample_type='nearest', name="P3_1_to_P2", num_args=1) P3_1_to_P2 = mx.sym.slice_like(P3_1_to_P2, P2_0) P2_1 = X.merge_sum([P2_0, P3_1_to_P2], name="sum_P2_0_P3_1") P2_1 = X.reluconvbn(P2_1, dim_reduced, init, norm, name="P2_1", prefix=prefix) P2_2 = P2_1 P2 = P2_2 P2_2_to_P3 = X.pool(P2_2, name="P2_2_to_P3", kernel=3, stride=2, pad=1) P3_2 = X.merge_sum([P3_1, P2_2_to_P3], name="sum_P3_1_P2_2") P3_2 = X.reluconvbn(P3_2, dim_reduced, init, norm, name="P3_2", prefix=prefix) P3 = P3_2 P3_2_to_P4 = X.pool(P3_2, name="P3_2_to_P4", kernel=3, stride=2, pad=1) P4_2 = X.merge_sum([P4_1, P3_2_to_P4], name="sum_P4_1_P3_2") P4_2 = X.reluconvbn(P4_2, dim_reduced, init, norm, name="P4_2", prefix=prefix) P4 = P4_2 P4_2_to_P5 = X.pool(P4_2, name="P4_2_to_P5", kernel=3, stride=2, pad=1) P5_2 = X.merge_sum([P5_1, P4_2_to_P5], name="sum_P5_1_P4_2") P5_2 = X.reluconvbn(P5_2, dim_reduced, init, norm, name="P5_2", prefix=prefix) P5 = P5_2 P5_2_to_P6 = X.pool(P5_2, name="P5_2_to_P6", kernel=3, stride=2, pad=1) P6_2 = X.merge_sum([P6_1, P5_2_to_P6], name="sum_P6_1_P5_2") P6_2 = X.reluconvbn(P6_2, dim_reduced, init, norm, name="P6_2", prefix=prefix) P6 = P6_2 return { 'S{}_P2'.format(stage): P2, 'S{}_P3'.format(stage): P3, 'S{}_P4'.format(stage): P4, 'S{}_P5'.format(stage): P5, 'S{}_P6'.format(stage): P6, }