Ejemplo n.º 1
0
 def request_schema(self, key, val):
     """
     Converter for request schema.
         Args:
         . key - key name in the schema.
         . val - the key value.
     """
     logging.debug(f'request schema key => {key} , val => {val}')
     res = ""
     if key in ["style", "process_query", "apm_stats_query"]:
         res += block(key, val, assignmentString)
     elif key in ["metadata", "conditional_formats"]:
         res += block_list(key, val, assignmentString)
     elif key in [
             "log_query", "rum_query", "apm_query", "security_query",
             "network_query"
     ]:
         res += block(key, val, self.request_nested_schema)
     elif key in ["fill", "size"]:
         res += block(key, val, self.request_schema)
     elif key in ["profile_metrics_query"]:  #unsupported keys
         pass
     else:
         res += assignmentString(key, val)
     return res
Ejemplo n.º 2
0
def searchForColorPoints(im, criteria):
    points = []
    pointColors = []
    hsvIm = cv2.cvtColor(im, cv2.COLOR_BGR2HSV_FULL)
    for i in range(11, im.shape[1] - 11, 10):
        for j in range(11, im.shape[0] - 11, 10):
            b = block(hsvIm, (i, j), 8)
            if b[:, :, 0].std() > 25:
                continue

            color = (b[:, :, 0].mean(), b[:, :, 1].mean(), b[:, :, 2].mean())
            matchedColor = matchColor(color, criteria)
            if matchedColor >= 0:
                points.append((i, j))
                pointColors.append(matchedColor)

    points = np.array(points, np.float16)
    cluster = fclusterdata(points, 10, "distance")

    centroids = []
    for i in range(len(criteria)):
        centroids.append([])

    for i in range(1, cluster.max() + 1):
        b = cluster == i

        c = np.zeros((1, 2), np.int16)
        for p in points[b.argsort()[len(b) - sum(b) :]]:
            c = c + p / sum(b)

        centroids[pointColors[b.argsort()[len(b) - sum(b)]]].append(c[0])

    return centroids
Ejemplo n.º 3
0
 def group_by_schema(self, key, val):
     """
     Converter for group_by schema.
         Args:
         . key - key name in the schema. 
         . val - the key value.
     """
     logging.debug(f'group_by schema key => {key} , val => {val}')
     res = ""
     if key in ["sort"]:
         res += block("sort_query", val, assignmentString)
     elif key in ["sort_query"]:
         res += block("sort_query", val, assignmentString)
     else:
         res += assignmentString(key, val)
     return res
Ejemplo n.º 4
0
def i3conf_bar():
    return lines(
        "# Bar",
        block(
            "bar",
            # row("status_command", "i3status"),
            row("output", "LVDS1"),
            row("status_command", "i3status"),
            row("position", "bottom"),
            "font xft:{} 7".format(font),
            row("separator_symbol", quotes(" · ")),
            block("colors", row("background", color.bg),
                  row("statusline", color.fg), row("separator", color.main),
                  row("focused_workspace", color.main, color.bg, color.main),
                  row("active_workspace", color.bg, color.bg, "#5f676a"),
                  row("inactive_workspace", color.bg, color.bg, color.fg),
                  row("urgent_workspace", color.alert, color.bg, color.alert),
                  "")),
        "")
Ejemplo n.º 5
0
 def widget_definition_schema(self, content):
     """
     Converter for widget dfinition schema.
         Args:
         . content - the content dict of the widget definition.
     """
     logging.debug(f'widget definition schema content => \n {content}')
     res = ""
     for key, val in content.items():
         if key in [
                 "type", "legend_layout", "legend_columns",
                 "global_time_target", "reflow_type"
         ]:
             pass
         elif key == "custom_links":
             res += block_list("custom_link", val, assignmentString)
         elif key == "requests":
             res += block_list("request", val,
                               self.request_schema) if isinstance(
                                   val, list) else block(
                                       "request", val, self.request_schema)
         elif key == "widgets":
             res += block_list("widget", val, self.widget_schema)
         elif key == "events":
             res += block_list("event", val, assignmentString)
         elif key == "markers":
             res += block_list("marker", val, assignmentString)
         elif key == "sort":
             res += assignmentString("sort", val) if isinstance(
                 content, str) else block_list("sort", val,
                                               assignmentString)
         elif key in [
                 "event", "right_yaxis", "widget_layout", "xaxis", "yaxis",
                 "style"
         ]:
             res += block(key, val, assignmentString)
         elif key == "time":
             res += assignmentString("live_span", val["live_span"])
         else:
             res += assignmentString(key, val)
     definition_type = "service_level_objective" if content[
         "type"] == "slo" else content["type"]
     return f'\n {definition_type}_definition {{ {res} \n }}'
Ejemplo n.º 6
0
    def __init__(self, k_shots, num_classes, win_len, model=cnn):
        super(Net, self).__init__()
        self.fuse = Fusion()
        self.model = model
        if cfg.data == 'WIFI':
            self.fc = nn.Sequential(*utils.block('BN', 60 * 2, cfg.hid_dim))
            self.lstm_time = nn.LSTM(60, cfg.hid_dim // 2)
            self.lstm_freq = nn.LSTM(60, cfg.hid_dim // 2)

        elif cfg.data == 'UWB':
            self.fc = nn.Sequential(*utils.block('BN', 138 * 2, cfg.hid_dim))
            self.lstm_time = nn.LSTM(138, cfg.hid_dim // 2)
            self.lstm_freq = nn.LSTM(138, cfg.hid_dim // 2)
        elif cfg.data == 'FMCW':
            self.fc = nn.Sequential(*utils.block('BN', 253 * 2, cfg.hid_dim))
            self.lstm_time = nn.LSTM(253, cfg.hid_dim // 2)
            self.lstm_freq = nn.LSTM(253, cfg.hid_dim // 2)

        self.classifier = nn.Linear(output_dim, cfg.num_class)
        self.attention = weight_norm(BiAttention(
            time_features=cfg.hid_dim // 2,
            freq_features=cfg.hid_dim // 2,
            mid_features=cfg.hid_dim,
            glimpses=1,
            drop=0.5,
        ),
                                     name='h_weight',
                                     dim=None)

        self.apply_attention = ApplyAttention(
            time_features=cfg.hid_dim // 2,
            freq_features=cfg.hid_dim // 2,
            mid_features=cfg.hid_dim // 2,
            glimpses=1,
            num_obj=512,
            drop=0.2,
        )
        self.cnn1 = torch.nn.Conv2d(2, 3, kernel_size=3, stride=1, padding=1)
        self.fc1 = FCNet(cfg.hid_dim // 2, cfg.hid_dim // 2, 'relu', 0.4)
        self.fc2 = FCNet(cfg.hid_dim // 2, cfg.hid_dim // 2, 'relu', 0.4)
        self.fc3 = FCNet(cfg.hid_dim, cfg.hid_dim, drop=0.4)
Ejemplo n.º 7
0
 def request_nested_schema(self, key, val):
     """
     Converter for request nested schema.
         Args:
         . key - key name in the schema.
         . val - the key value.
     """
     logging.debug(f'request nested schema key => {key} , val => {val}')
     res = ""
     if key in ["search"]:
         res += assignmentString("search_query", val["query"])
     elif key in ["compute"]:
         res += block("compute_query", val, assignmentString)
     elif key in ["compute_query"]:
         res += block("compute_query", val, assignmentString)
     elif key in ["multi_compute"]:
         pass
     elif key in ["group_by"]:
         res += block_list("group_by", val, self.group_by_schema)
     else:
         res += assignmentString(key, val)
     return res
Ejemplo n.º 8
0
def i3conf_bar():
    return lines("# Bar",
                 block("bar",
                       # row("status_command", "i3status"),
                       row("output", "LVDS1"),
                       row("status_command", "i3status"),
                       row("position", "bottom"),
                       "font xft:{} 7".format(font),
                       row("separator_symbol", quotes(" · ")),
                       block("colors",
                             row("background", color.bg),
                             row("statusline", color.fg),
                             row("separator", color.main),
                             row("focused_workspace",
                                 color.main, color.bg, color.main),
                             row("active_workspace",
                                 color.bg, color.bg, "#5f676a"),
                             row("inactive_workspace",
                                 color.bg, color.bg, color.fg),
                             row("urgent_workspace",
                                 color.alert, color.bg, color.alert),
                             "")
                       ),
                 "")
Ejemplo n.º 9
0
 def widget_schema(self, key, val):
     """
     Converter for widget schema.
         Args:
         . key - key name in the schema. 
         . val - the key value.
     """
     logging.debug(f'widget schema key => {key} , val => {val}')
     res = ""
     if key in ["id"]:
         pass
     elif key == "definition":
         res += self.widget_definition_schema(val)
     elif key == "layout":
         res += block("widget_layout", val, assignmentString)
     return res
Ejemplo n.º 10
0
 def options_schema(self, content):
     """
     Converter of options block.
         Args:
         . content - options dict.
     """
     logging.debug(f'Monitor options schema content  => \n {content}')
     res = ""
     for key, val in content.items():
         if key in ["thresholds"]:
             # res += self.block("monitor_thresholds", val, assignmentString)
             res += block("monitor_thresholds", val, assignmentString)
         elif key in ["silenced"]:
             # res += self.block(key, val, assignmentString)
             # res += block(key, val, assignmentString)
             pass  # unsupported block tyep.
         else:
             res += assignmentString(key, val)
     return res
Ejemplo n.º 11
0
def recall_command(request):
    data = request.form
    team_id, user_id, text = data['team_id'], data['user_id'], data['text']
    items = recall(team_id, user_id, text)
    items_text = "\n".join(["%s. %s" % (i, x) for i, x in enumerate(items, 1)])

    block_args = [
        ('mrkdwn', "Recalling `{}`".format(text)),
        ('divider',),
        ('mrkdwn', items_text),
        ('divider',),
        ('mrkdwn', "_Trying filtering by tag and date_ `/recall last 7 days #mgmt`."),
        ('mrkdwn', "_Share your response with the room by adding `public` anywhere in your response_"),
    ]
    resp = {
        "text": items_text,
        "response_type": "in_channel" if "public" in text else "ephemeral",
        "blocks": [block(*args) for args in block_args]
    }
    return jsonify(resp)
Ejemplo n.º 12
0
def app_home_opened_event(request):
    parsed = request.json
    user_id = parsed['event']['user']
    team_id = parsed['team_id']
    items = recall(team_id, user_id, "last 14 days")
    items_text = "\n".join(["%s. %s" % (i, x) for i, x in enumerate(items, 1)])
    blocks_spec = [
        ('mrkdwn', "Your home tab for Reflect"),
        ('divider', ),
        ('mrkdwn', items_text),
        ('divider', ),
        ('mrkdwn', "Some more stuff here"),
    ]
    blocks = [block(*x) for x in blocks_spec]
    msg = {
        "user_id": user_id,
        "view": {
            "type": "home",
            "blocks": blocks,
        }
    }
    resp = slack_api(team_id, "views.publish", msg)
    return "OK"
Ejemplo n.º 13
0
    def __init__(self,
                 args,
                 d_model,
                 frefine_layers,
                 brefine_layers,
                 drefine_layers,
                 dcrefine_layers,
                 dense_fusion=False,
                 conv_fusion=False,
                 flatten=False):
        super().__init__()

        self.args = args
        self.d_model = d_model
        self.dim = d_model
        self.fusion_strategy = self.args.view_fusion_strategy
        self.dense_fusion = dense_fusion
        self.conv_fusion = conv_fusion

        # self.params = []
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
        self.drefine_nlayers = drefine_layers
        self.brefine_nlayers = brefine_layers
        self.frefine_nlayers = frefine_layers
        self.dcrefine_nlayers = dcrefine_layers

        # print(self.dense_fusion, self.conv_fusion)

        if self.drefine_nlayers > 0:

            drefine_layers = []
            for i in range(self.drefine_nlayers):
                drefine_layers.append(dblock(self.dim, self.dim))
                drefine_layers.append(nn.Dropout(0.5))

            self.dense_project_layers = nn.Sequential(*drefine_layers)
            # self.params += list(self.dense_project_layers.parameters())

        if self.dense_fusion:
            if self.frefine_nlayers > 0:
                frefine_layers = []
                for i in range(self.frefine_nlayers):
                    frefine_layers.append(dblock(6 * self.dim, 6 * self.dim))
                    frefine_layers.append(nn.Dropout(0.5))

                self.refine_before_fuse = nn.Sequential(*frefine_layers)

            if "concat" in self.fusion_strategy:
                reduce_layers = []
                reduce_layers.append(dblock(int(6 * self.dim), int(self.dim)))
                reduce_layers.append(nn.Dropout(0.5))
                self.reduce_views = nn.Sequential(*reduce_layers)

            if self.brefine_nlayers > 0:
                brefine_layers = []
                for i in range(self.brefine_nlayers):
                    brefine_layers.append(dblock(self.dim, self.dim))
                    brefine_layers.append(nn.Dropout(0.5))

                self.refine_after_fuse = nn.Sequential(*brefine_layers)

        if self.conv_fusion:
            if self.drefine_nlayers > 0:
                self.reshape = dblock(self.dim, 16 * 16 * 32)
                self.conv_refine = nn.Sequential(block(32, 64, 3, 1, 1),
                                                 Resblock(64, 64, 3, 1, 1))
                self.dim = 64

            if self.frefine_nlayers > 0:
                frefine_layers = []
                for i in range(self.frefine_nlayers):
                    frefine_layers.append(
                        Resblock(int(6 * self.dim), int(6 * self.dim), 3, 1,
                                 1))

                self.refine_before_fuse = nn.Sequential(*frefine_layers)

            if "concat" in self.fusion_strategy:
                self.reduce_views = block(int(6 * self.dim), int(self.dim), 3,
                                          1, 1)

            if self.brefine_nlayers > 0:
                brefine_layers = []
                for i in range(self.brefine_nlayers):
                    brefine_layers.append(
                        Resblock(int(self.dim), int(self.dim), 3, 1, 1))

                self.refine_after_fuse = nn.Sequential(*brefine_layers)

        if self.dcrefine_nlayers > 0:

            dcrefine_layers = []
            for i in range(self.dcrefine_nlayers):
                if i == 0 and "cond" in self.args.finetune_obj:
                    dcrefine_layers.append(
                        dblock(self.dim + self.args.latent_dim, self.dim))
                else:
                    dcrefine_layers.append(dblock(self.dim, self.dim))
                dcrefine_layers.append(nn.Dropout(0.5))

            dcrefine_layers.append(dblock(self.dim, 16 * 16 * 32))
            self.refine = Resblock(32, 32, 3, 1, 1)

            self.dense_reduce_layers = nn.Sequential(*dcrefine_layers)

        else:
            if self.dense_fusion:

                self.reshape = dblock(self.dim, 16 * 16 * 32)
                self.refine = Resblock(32, 32, 3, 1, 1)

        # self.reduce = []

        # if self.frefine_nlayers > 0:
        #     self.params += list(self.refine_before_fuse.parameters())
        #     # self.reduce.append(self.refine_before_fuse)

        # # self.reduce.append(self.reduce_views)
        # if "concat" in self.fusion_strategy:
        #     self.params += list(self.reduce_views.parameters())

        # if self.brefine_nlayers > 0:
        #     self.params += list(self.refine_after_fuse.parameters())
        # self.reduce.append(self.refine_after_fuse)

        self.dropout = torch.nn.Dropout(p=0.5, inplace=False)
Ejemplo n.º 14
0
    def __init__(self,
                 args,
                 init_layer_dim,
                 init_channel_dim,
                 max_f,
                 d_model,
                 add_convs_before_decoding=False,
                 add_initial_upsample_conv=False):
        super().__init__()

        decoder_network_layers = []

        self.args = args
        self.max_f = max_f
        self.init_layer_dim = init_layer_dim
        self.init_channel_dim = init_channel_dim
        self.d_model = d_model
        self.input_dim = 256

        if add_initial_upsample_conv:
            decoder_network_layers.append(
                block(int(init_channel_dim // 2),
                      int(init_channel_dim),
                      3,
                      1,
                      1,
                      upsample=True))

            decoder_network_layers.append(
                block(int(init_channel_dim),
                      int(init_channel_dim),
                      3,
                      1,
                      1,
                      activation="identity"))

        if add_convs_before_decoding:
            # print("add_convs_before_decoding",add_convs_before_decoding)

            decoder_network_layers.append(
                block((self.d_model), int(self.init_channel_dim), 3, 1, 1))
            #

            decoder_network_layers.append(
                block(int(self.d_model // 4),
                      int(self.init_channel_dim),
                      3,
                      1,
                      1,
                      activation='identity'))

        while self.init_layer_dim < self.input_dim:
            decoder_network_layers.append(
                block(int(self.init_channel_dim),
                      int(self.init_channel_dim // 2),
                      3,
                      1,
                      1,
                      upsample=True))

            decoder_network_layers.append(
                block(int(self.init_channel_dim // 2),
                      int(self.init_channel_dim // 2),
                      3,
                      1,
                      1,
                      activation="identity"))

            self.init_layer_dim *= 2
            self.init_channel_dim = self.init_channel_dim / 2

#                 decoder_network_layers.append(
#                     nn.ZeroPad2d((1,0,1,0))
#                 )

        if self.args.gen_road_map:
            outdim = 1
        elif self.args.gen_semantic_map:
            outdim = 11
        else:
            outdim = 3

        decoder_network_layers.append(
            block(int(self.init_channel_dim),
                  outdim,
                  3,
                  1,
                  1,
                  activation="identity",
                  norm=False), )

        self.decoder_network = nn.Sequential(*decoder_network_layers)
Ejemplo n.º 15
0
def ShuffleNetV2(include_top=True,
                 input_tensor=None,
                 scale_factor=1.0,
                 pooling='max',
                 input_shape=(224,224,3),
                 load_model=None,
                 num_shuffle_units=[3,7,3],
                 bottleneck_ratio=1,
                 classes=1000):
    if K.backend() != 'tensorflow':
        raise RuntimeError('Only tensorflow supported for now')
    name = 'ShuffleNetV2_{}_{}_{}'.format(scale_factor, bottleneck_ratio, "".join([str(x) for x in num_shuffle_units]))
    input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=28, require_flatten=include_top,
                                      data_format=K.image_data_format())
    out_dim_stage_two = {0.5:48, 1:116, 1.5:176, 2:244}

    if pooling not in ['max', 'avg']:
        raise ValueError('Invalid value for pooling')
    if not (float(scale_factor)*4).is_integer():
        raise ValueError('Invalid value for scale_factor, should be x over 4')
    exp = np.insert(np.arange(len(num_shuffle_units), dtype=np.float32), 0, 0)  # [0., 0., 1., 2.]
    out_channels_in_stage = 2**exp
    out_channels_in_stage *= out_dim_stage_two[bottleneck_ratio]  #  calculate output channels for each stage
    out_channels_in_stage[0] = 24  # first stage has always 24 output channels
    out_channels_in_stage *= scale_factor
    out_channels_in_stage = out_channels_in_stage.astype(int)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    # create shufflenet architecture
    x = Conv2D(filters=out_channels_in_stage[0], kernel_size=(3, 3), padding='same', use_bias=False, strides=(2, 2),
               activation='relu', name='conv1')(img_input)
    x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='maxpool1')(x)

    # create stages containing shufflenet units beginning at stage 2
    for stage in range(len(num_shuffle_units)):
        repeat = num_shuffle_units[stage]
        x = block(x, out_channels_in_stage,
                   repeat=repeat,
                   bottleneck_ratio=bottleneck_ratio,
                   stage=stage + 2)

    if bottleneck_ratio < 2:
        k = 1024
    else:
        k = 2048
    x = Conv2D(k, kernel_size=1, padding='same', strides=1, name='1x1conv5_out', activation='relu')(x)

    if pooling == 'avg':
        x = GlobalAveragePooling2D(name='global_avg_pool')(x)
    elif pooling == 'max':
        x = GlobalMaxPooling2D(name='global_max_pool')(x)

    if include_top:
        x = Dense(classes, name='fc')(x)
        x = Activation('softmax', name='softmax')(x)

    if input_tensor:
        inputs = get_source_inputs(input_tensor)

    else:
        inputs = img_input

    model = Model(inputs, x, name=name)

    if load_model:
        model.load_weights('', by_name=True)

    return model
Ejemplo n.º 16
0
    def __init__(self, args):
        super().__init__(args)

        self.args = args
        self.num_patches = args.num_patches
        self.d_model = OUT_BLOCK4_DIMENSION_DICT[self.args.network_base]

        full_resnet = get_base_model(self.args.network_base)

        self.init_layers = nn.Sequential(
            full_resnet.conv1,
            full_resnet.bn1,
            full_resnet.relu,
            full_resnet.maxpool,
        )
        self.block1 = full_resnet.layer1
        self.block2 = full_resnet.layer2
        self.block3 = full_resnet.layer3
        self.block4 = full_resnet.layer4

        self.image_network = nn.Sequential(
            self.init_layers,
            self.block1,
            self.block2,
            self.block3,
            self.block4,
        )

        if self.d_model > 1024:
            self.max_f = 1024
        else:
            self.max_f = self.d_model

        if self.args.view_pretrain_obj != "none":
            self.model_type = self.args.view_pretrain_obj.split("_")[0]
        else:
            self.model_type = "var"

        self.input_dim = 256

        self.latent_dim = self.args.latent_dim
        self.mask_ninps = 1

        self.reduce = nn.Conv2d(6 * self.d_model,
                                self.d_model,
                                kernel_size=1,
                                stride=1)

        if self.model_type == "det":

            decoder_network_layers = []

            decoder_network_layers.append(
                block(int(self.d_model), int(self.max_f), 2, 2, 0))

            init_layer_dim = int(self.input_dim // 16)
            init_channel_dim = self.max_f

            while init_layer_dim < self.input_dim:
                decoder_network_layers.append(
                    block(int(init_channel_dim), int(init_channel_dim // 2), 2,
                          2, 0), )
                init_layer_dim *= 2
                init_channel_dim = init_channel_dim / 2

            if self.stage != "pretrain":
                out_dim = 1
            else:
                out_dim = 3

            decoder_network_layers.append(
                block(int(init_channel_dim), out_dim, 3, 1, 1, "sigmoid",
                      False, False), )

            self.decoder_network = nn.Sequential(*decoder_network_layers)

            self.decoding = nn.Sequential(self.decoder_network)

            self.loss_type = "bce"

        else:

            decoder_network_layers = []
            decoder_network_layers.append(
                block(int(self.latent_dim), int(self.max_f), 4, 1, 0,
                      "leakyrelu"), )

            init_layer_dim = 4
            init_channel_dim = self.max_f
            while init_layer_dim < self.input_dim:
                decoder_network_layers.append(
                    block(int(init_channel_dim), int(init_channel_dim // 2), 4,
                          2, 1, "leakyrelu"), )
                init_layer_dim *= 2
                init_channel_dim = init_channel_dim / 2

            if self.stage == "pretrain":
                decoder_network_layers.append(
                    block(int(init_channel_dim), 3, 3, 1, 1, "tanh", False,
                          False), )
            else:
                decoder_network_layers.append(
                    block(int(init_channel_dim), 1, 3, 1, 1, "sigmoid", False,
                          False), )

            self.decoder_network = nn.Sequential(*decoder_network_layers)

            self.z_project = nn.Linear(self.d_model, 2 * self.latent_dim)
            # self.reduce = nn.Linear((6-self.mask_ninps) * self.d_model, self.d_model)
            self.decoding = nn.Sequential(self.decoder_network, self.z_project)

            self.loss_type = "mse"

        if self.loss_type == "mse":
            self.criterion = torch.nn.MSELoss()
        elif self.loss_type == "bce":
            self.criterion = torch.nn.BCELoss()

        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))

        self.n_synthesize = self.args.synthesizer_nlayers

        if self.n_synthesize > 0:
            synthesizer_layers = []
            for i in range(self.n_synthesize):
                synthesizer_layers.append(
                    Resblock(self.d_model, self.d_model, 3, 1, 1))

            self.synthesizer = nn.Sequential(*synthesizer_layers)

        self.fusion = self.args.view_fusion_strategy

        self.dropout = torch.nn.Dropout(p=0.5, inplace=False)

        self.det_fusion_strategy = "concat_fuse"
        assert self.det_fusion_strategy in ["concat_fuse", "mean"]

        self.sigmoid = nn.Sigmoid()
        self.shared_params = list(self.image_network.parameters())
        self.pretrain_params = list(self.reduce.parameters()) + list(
            self.decoding.parameters()) + list(self.synthesizer.parameters())
def shufflenet(images, is_training, num_classes=1000, depth_multiplier='1.0'):
    """
    This is an implementation of ShuffleNet v2:
    https://arxiv.org/abs/1807.11164

    Arguments:
        images: a float tensor with shape [batch_size, image_height, image_width, 3],
            a batch of RGB images with pixel values in the range [0, 1].
        is_training: a boolean.
        num_classes: an integer.
        depth_multiplier: a string, possible values are '0.5', '1.0', '1.5', and '2.0'.
    Returns:
        a float tensor with shape [batch_size, num_classes].
    """
    possibilities = {'0.5': 48, '1.0': 116, '1.5': 176, '2.0': 224}
    # 116 is not divisible by 8,
    # so maybe you need to change it when training on TPUs
    initial_depth = possibilities[depth_multiplier]

    def batch_norm(x):
        x = tf.layers.batch_normalization(x,
                                          axis=3,
                                          center=True,
                                          scale=True,
                                          training=is_training,
                                          momentum=BATCH_NORM_MOMENTUM,
                                          epsilon=BATCH_NORM_EPSILON,
                                          fused=True,
                                          name='batch_norm')
        return x

    with tf.name_scope('standardize_input'):
        x = (2.0 * images) - 1.0

    with tf.variable_scope('ShuffleNetV2'):
        params = {
            'padding': 'SAME',
            'activation_fn': tf.nn.relu,
            'normalizer_fn': batch_norm,
            'data_format': 'NHWC',
            'weights_initializer': tf.variance_scaling_initializer()
        }
        with slim.arg_scope([slim.conv2d, slim.separable_conv2d], **params):

            x = slim.conv2d(x, 24, (3, 3), stride=2, scope='Conv1')
            x = slim.max_pool2d(x, (3, 3),
                                stride=2,
                                padding='SAME',
                                scope='MaxPool')

            x = block(x,
                      num_units=4,
                      out_channels=initial_depth,
                      scope='Stage2')
            x = block(x, num_units=8, scope='Stage3')
            x = block(x, num_units=4, scope='Stage4')

            final_channels = 1024 if depth_multiplier != '2.0' else 2048
            x = slim.conv2d(x, final_channels, (1, 1), stride=1, scope='Conv5')

    # global average pooling
    x = tf.reduce_mean(x, axis=[1, 2])

    logits = slim.fully_connected(
        x,
        num_classes,
        activation_fn=None,
        scope='classifier',
        weights_initializer=tf.truncated_normal_initializer(stddev=0.01))
    return logits
Ejemplo n.º 18
0
def large_shufflenet(images, is_training, num_classes=1000):
    """
    This is an implementation of ShuffleNet v2-50:
    https://arxiv.org/abs/1807.11164
    It is a more fast/accurate alternative to ResNet-50.

    Arguments:
        images: a float tensor with shape [batch_size, image_height, image_width, 3],
            a batch of RGB images with pixel values in the range [0, 1].
        is_training: a boolean.
        num_classes: an integer.
    Returns:
        a float tensor with shape [batch_size, num_classes].
    """
    initial_depth = 240

    # in the original paper 244, but i want it to be divisible by 8.
    # it is recommended for TPUs

    def batch_norm(x):
        x = tf.layers.batch_normalization(x,
                                          axis=3,
                                          center=True,
                                          scale=True,
                                          training=is_training,
                                          momentum=BATCH_NORM_MOMENTUM,
                                          epsilon=BATCH_NORM_EPSILON,
                                          fused=True,
                                          name='batch_norm')
        return x

    with tf.name_scope('standardize_input'):
        x = (2.0 * images) - 1.0

    with tf.variable_scope('ShuffleNetV2-50'):
        params = {
            'padding': 'SAME',
            'activation_fn': tf.nn.relu,
            'normalizer_fn': batch_norm,
            'data_format': 'NHWC',
            'weights_initializer': tf.variance_scaling_initializer()
        }
        with slim.arg_scope([slim.conv2d, slim.separable_conv2d], **params):

            x = slim.conv2d(x, 64, (3, 3), stride=2, scope='Conv1')
            x = slim.max_pool2d(x, (3, 3),
                                stride=2,
                                padding='SAME',
                                scope='MaxPool')

            x = block(x,
                      num_units=3,
                      out_channels=initial_depth,
                      downsample=False,
                      scope='Stage2')
            x = block(x, num_units=4, scope='Stage3')
            x = block(x, num_units=6, scope='Stage4')
            x = block(x, num_units=3, scope='Stage5')

            x = slim.conv2d(x, 2048, (1, 1), stride=1, scope='Conv6')

            # number of conv layers in the network:
            # 1 + (3 + 4 + 6 + 3)*3 + 1 = 50

    # global average pooling
    x = tf.reduce_mean(x, axis=[1, 2])

    logits = slim.fully_connected(
        x,
        num_classes,
        activation_fn=None,
        scope='classifier',
        weights_initializer=tf.truncated_normal_initializer(stddev=0.01))
    return logits
Ejemplo n.º 19
0
    def __init__(self, args):
        super().__init__()
        self.args = args
        self.stage = None
        self.disc_finetune_params = []

        self.type = self.args.disc_type

        if "patch" in self.type:
            channels = [16, 32, 64, 128, 1]
        else:
            channels = [64, 128, 256, 256, 512, 512, 512]

        network_layers = []

        if self.args.gen_road_map:
            initc = 1
        elif self.args.gen_semantic_map:
            initc = 11
        else:
            initc = 3

        for i, channel in enumerate(channels):
            if i == len(channels) - 1 and "patch" in self.type:
                network_layers.append(
                    block(initc,
                          channel,
                          3,
                          1,
                          1,
                          activation="sigmoid",
                          norm=False))
            elif i == 0:
                network_layers.append(
                    block(initc,
                          channel,
                          4,
                          2,
                          1,
                          activation="leakyrelu",
                          norm=False))
            else:
                network_layers.append(
                    block(initc, channel, 4, 2, 1, activation="leakyrelu"))

            initc = channel

        self.network = nn.Sequential(*network_layers)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

        self.disc_finetune_params += list(self.network.parameters())

        if "patch" not in self.type:
            self.final = dblock(2048, 1, activation="sigmoid")
            self.disc_finetune_params += list(self.final.parameters())
Ejemplo n.º 20
0
optimizer = pargs.optimizer(model.parameters())
optimizer = pargs.optimizer([{'params': model.flow.parameters()},
                             {'params': model.parameters(recurse=False), 'lr': 0.01}] +
                             ([{'params': model.A.parameters(), 'lr': 0.01}] if hasattr(model, 'A') else []))
trainer = pargs.trainer(_logname=pargs.name)
scheduler = pargs.scheduler(optimizer)

if pargs.cuda:
 #model = torch.nn.DataParallel(model)
  #model_obj = model.module
  model.cuda()
  trainer.cuda()

if len(pargs.resume) > 0:
  with utils.block('Resume', exit_on_error=True) as b:
    path = trainer.resume(model, optimizer, pargs.resume, uid=pargs.resume_uid, unique=False)
    b.print('Successfully Resumed: {}'.format(path))
    b.print(trainer.state_dict)
    
with utils.block('Command') as b:
  b.print(pargs.command())
  b.print('Stub: ' + pargs.stub())
  trainer.log(0, **{':command': pargs.command()})

with utils.block('Arguments') as b:
  b.print(pargs, indent=False)
  trainer.log(0, **{':arguments': repr(pargs)})

with utils.block('Parameters') as b:
  b.print(utils.pytorch_summary(model, verbose=pargs.print_model), indent=False)