X = tf.placeholder(tf.float32, [None, 299, 299, 3])
    elif args.model == 'squeezenet':
        X = tf.placeholder(tf.float32, [None, 227, 227, 3])
    else:
        raise ValueError("Do not support {}".format(args.model))

    Y = tf.placeholder(tf.float32, [None, 1000])

    dg = DataGenerator('./data/val224_compressed.pkl', model=args.model, dtype='float32')
    with tf.device('/cpu:0'):
        if args.model == 'resnet':
            logits = resnet50.ResNet50(X, weights)
        elif args.model == 'inception':
            logits = inception_v3.InceptionV3(X, weights)
        elif args.model == 'vgg':
            logits = vgg16.VGG16(X, weights)
        elif args.model == 'mobilenet':
            logits = mobilenet.MobileNet(X, weights, args.alpha)
        elif args.model == 'xception':
            logits = xception.Xception(X, weights)
        else:
            logits = squeezenet.SqueezeNet(X, weights)
        prediction = tf.nn.softmax(logits)
        pred = tf.argmax(prediction, 1)

    acc = 0.
    acc_top5 = 0.
    print('Start evaluating {}'.format(args.model))
    with tf.Session() as sess:
        for im, label in dg.generator():
            t1, t5 = sess.run([pred, prediction], feed_dict={X: im})
Exemplo n.º 2
0
    elif args.prune_type == 'chunk':
        args.q = 0.75
    elif args.prune_type == 'cascade':
        args.q = 0.75

#device = 'cuda' if torch.cuda.is_available() else 'cpu'

# --------------------------------------- #
# --- Full precision model load/train --- #
# --------------------------------------- #

if args.model == "vgg16":
    if args.dataset == "ImageNet":
        model = vgg_in.vgg16(pretrained=args.scratch)
    else:
        model = vgg16.VGG16()
elif args.model == "resnet50":
    if args.dataset == "ImageNet":
        model = resnet_in.resnet50(pretrained=args.scratch)
    else:
        model = resnet.ResNet50()
elif args.model == "resnet152":
    if args.dataset == "ImageNet":
        model = resnet_in.resnet152(pretrained=args.scratch)
    else:
        print("Model {} not supported!".format(args.model))
        sys.exit(0)
elif args.model == "inception_v3":
    if args.dataset == "ImageNet":
        model = inception_v3.gluon_inception_v3(pretrained=args.scratch)
    else:
Exemplo n.º 3
0
def loadCoeffIdx(pathName, modelName):
    skipLinear = False
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    if modelName == "vgg16_c10":
        model = vgg16.VGG16()
    elif modelName == "vgg16_c10_old":
        model = vgg16_old.VGG16()
    elif modelName == "resnet50_c10":
        model = resnet.ResNet50()
    elif modelName == "inceptionv3_c10":
        model = inception_v3_c10.inception_v3()
    elif modelName == "alexnet_in":
        #model = torch.hub.load('pytorch/vision', 'alexnet', pretrained=True)
        model = alexnet.AlexNet()
        skipLinear = True
    elif modelName == "vgg16_in":
        model = vgg_in.vgg16()
    elif modelName == "resnet50_in":
        model = resnet_in.resnet50()
    elif modelName == "transformer_wmt":
        translator = transformer_translate.create_model(
        )  # returns Transformer()
        translator = translator.to(device)
        model = translator.model
    else:
        print("Model not supported")
        exit(1)

    model = model.to(device)

    if ("vgg16" in modelName or "alexnet" in modelName):
        model = replace_vgg16(model, skipLinear)
    elif "transformer" in modelName:
        #transformer_train.replace_with_pruned(model, "model", prune_attention=True, prune_only_attention=False)
        pass
    else:
        replace_with_pruned(model, "model", skipLinear)

    if "_in" in modelName:
        if not torch.distributed.is_initialized():
            port = np.random.randint(10000, 65536)
            torch.distributed.init_process_group(
                backend='nccl',
                init_method='tcp://127.0.0.1:%d' % port,
                rank=0,
                world_size=1)
        model = torch.nn.parallel.DistributedDataParallel(model)

    if not ("transformer" in modelName):
        model.load_state_dict(torch.load(pathName))
    model.eval()

    #prune(model, method="cascade", q=1.0)

    layers = []
    widths = [3]
    numConv = 0
    assert isinstance(model, nn.Module)
    for n, m in model.named_modules():
        print(type(m))
        if isinstance(m, torch.nn.Conv2d):
            numConv += 1
        if isinstance(m, torch.nn.Linear):
            numConv += 1
        if isinstance(m, PrunedConv):
            #print(m.mask)
            #layer = m.mask.view((m.out_channels, -1)).detach().cpu().numpy()
            layer = m.conv.weight.view(
                (m.out_channels, -1)).detach().cpu().numpy()
        elif isinstance(m, PrunedLinear) or isinstance(
                m, CSP.pruned_layers.PrunedLinear) and (not skipLinear):
            #print(m.mask)
            #layer = m.mask.view((m.out_features, -1)).detach().cpu().numpy()
            layer = m.linear.weight.view(
                (m.out_features, -1)).detach().cpu().numpy()
        else:
            continue

        #print(n)
        #print(layer.shape)

        #layer = layer > 0
        widths.append(len(layer))
        layers.append(layer)

        #layerMask = layer > 0
        #layerMask = np.transpose(layerMask)
    #print("NUM CONV: {}".format(numConv))
    #print("NUM EXTRACTED LAYERS: {}".format(len(layers)))

    if "transformer" in modelName:
        for i in range(11):
            for j in range(36, 97):
                layers.append(layers[j])

    return layers
Exemplo n.º 4
0
def exportData(pathName, modelName):
    global skipLinear
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    if modelName == "vgg16_c10":
        model = vgg16.VGG16()
    elif modelName == "vgg16_c10_old":
        model = vgg16_old.VGG16()
    elif modelName == "resnet50_c10":
        model = resnet.ResNet50()
    elif modelName == "inceptionv3_c10":
        model = inception_v3_c10.inception_v3()
    elif modelName == "alexnet_in":
        #model = torch.hub.load('pytorch/vision', 'alexnet', pretrained=True)
        model = alexnet.AlexNet()
        skipLinear = True
    elif modelName == "vgg16_in":
        model = vgg_in.vgg16()
        skipLinear = True
    elif modelName == "resnet50_in":
        model = resnet_in.resnet50()
    elif modelName == "transformer_wmt":
        translator = transformer_translate.create_model(
        )  # returns Transformer()
        translator = translator.to(device)
        model = translator.model
    else:
        print("Model not supported")
        exit(1)

    model = model.to(device)

    if ("vgg16" in modelName or "alexnet" in modelName):
        model = replace_vgg16(model, skipLinear)
    elif "transformer" in modelName:
        #transformer_train.replace_with_pruned(model, "model", prune_attention=True, prune_only_attention=False)
        pass
    else:
        replace_with_pruned(model, "model", skipLinear)

    if "_in" in modelName:
        if not torch.distributed.is_initialized():
            port = np.random.randint(10000, 65536)
            torch.distributed.init_process_group(
                backend='nccl',
                init_method='tcp://127.0.0.1:%d' % port,
                rank=0,
                world_size=1)
        model = torch.nn.parallel.DistributedDataParallel(model,
                                                          device_ids=[0],
                                                          output_device=0)

    if not ("transformer" in modelName):
        model.load_state_dict(torch.load(pathName))
    model.eval()

    #prune(model, method="cascade", q=1.0)

    #layer_num = 0
    #save_dir = "/root/hostCurUser/reproduce/DNNsim/net_traces/ResNet50_ImageNet_CSP/"
    #lstModules = list( model.named_modules())
    #for n, m in model.named_modules():
    #for i in range(len(lstModules)):
    #    if isinstance(lstModules[i], nn.Conv2d) or isinstance(lstModules[i], nn.Linear):
    #        model[i] = DataExporter(lstModules[i], save_dir, layer_num)
    #        layer_num += 1

    f = open(MODEL_PATH + "model.csv", "w")
    fscale = open(SCALE_PATH, "w")
    fscale.write(
        "Layer name, IFMAP Height, IFMAP Width, Filter Height, Filter Width, Channels, Num Filter, Strides,\n"
    )
    layer_idx = 0

    models = []  # for SparTen
    layers = []
    acts = []
    weights = []
    paddings = []
    strides = []
    idxs = []

    def extract(module, input):
        #if module.extracted:
        #    return
        if len(input[0].shape) < 4:
            if not ("transformer" in modelName):
                return
            try:
                a = input[0].detach().cpu().reshape(1, module.in_features, 1,
                                                    1)
            except:
                a = input[0].detach().cpu().reshape(-1, 1, 1)
                a = a[:module.in_features]
                a = a.reshape(1, module.in_features, 1, 1)
        else:
            a = input[0].detach().cpu()
        acts.append(a)

        if isinstance(module, torch.nn.Conv2d):
            layer = module.weight.view(
                (module.out_channels, -1)).detach().cpu().numpy()
            weight = module.weight.detach().cpu().numpy()
            tp = "conv"
            stride = str(max(module.stride[0], module.stride[1]))
            padding = str(max(module.padding[0], module.padding[1]))
            in_channels = module.in_channels
            out_channels = module.out_channels
            kernel_size = module.kernel_size
            padding_st = module.padding
            stride_st = module.stride
        elif isinstance(module, torch.nn.Linear) and (not skipLinearExport):
            layer = module.weight.view(
                (module.out_features, -1)).detach().cpu().numpy()
            weight = module.weight.detach().cpu().reshape(
                module.weight.shape[0], module.weight.shape[1], 1, 1).numpy()
            tp = "fc"
            stride = str(1)
            padding = str(0)
            in_channels = module.in_features
            out_channels = module.out_features
            kernel_size = (1, 1)
            padding_st = (0, 0)
            stride_st = (1, 1)
        else:
            print("{} does not exist".format(module))
            exit(1)
        name = str(module.layer_idx)
        weights.append(weight)
        paddings.append(int(padding))
        strides.append(int(stride))
        f.write(name + "," + tp + "," + stride + "," + padding + ",\n")
        layers.append(layer)
        models.append({
            'in_channels': in_channels,
            'out_channels': out_channels,
            'kernel': kernel_size,
            'name': tp + name,
            'padding': padding_st,
            'weights': weight,
            'IFM': a.cpu().numpy(),
            'stride': stride_st
        })
        idxs.append(module.layer_idx)
        #module.extracted = True

    #replace_with_exporter(model, "model", f, fscale)
    for n, m in model.named_modules():
        if isinstance(m, torch.nn.Conv2d):
            weight = m.weight.detach().cpu().numpy()
            if weight.shape[2] != weight.shape[3]:
                continue
            #weights.append(weight)
            #m.extracted = False
            m.register_forward_pre_hook(extract)
            #name = str(layer_idx)
            #tp = "conv"
            #stride = str(max(m.stride[0], m.stride[1]))
            #padding = str(max(m.padding[0], m.padding[1]))
            #paddings.append(int(padding))
            #f.write(name+"," + tp+"," + stride+"," + padding + ",\n")
            m.layer_idx = layer_idx
            layer_idx += 1
        elif isinstance(m, torch.nn.Linear):
            if not ("transformer" in modelName):
                continue
            #weight = m.weight.detach().cpu().reshape(m.weight.shape[0], m.weight.shape[1], 1, 1).numpy()
            #weights.append(weight)
            #m.extracted = False
            m.register_forward_pre_hook(extract)
            #name = str(layer_idx)
            #tp = "fc"
            #stride = str(1)
            #padding = str(0)
            #paddings.append(int(padding))
            #f.write(name+"," + tp+"," + stride+"," + padding + ",\n")
            m.layer_idx = layer_idx
            layer_idx += 1

    if "_in" in modelName:
        IFM = torch.rand(1, 3, 224, 224).cuda()
        model(IFM)
    elif "_c10" in modelName:
        IFM = torch.rand(1, 3, 32, 32).cuda()
        model(IFM)
    elif "_wmt" in modelName:
        src_seq = [4556, 4562, 4560, 4557, 4712, 1894, 15, 4564, 4620, 0, 5]
        pred_seq = translator.translate_sentence(
            torch.LongTensor([src_seq]).to(device))
        print(pred_seq)
    else:
        print("Dataset not supported")
        exit(1)

    print(len(acts))
    print(len(weights))

    #layers = loadCoeffIdx(pathName, modelName)

    with open(ST_PATH + modelName + ".h5", "wb") as f:
        pickle.dump(models, f)

    sq_ptrs = []
    out_channels = []
    for layer in layers:
        sp, oc = squeezeCoeffIdxTotal(layer, len(layer))
        out_channels.append(oc)
    #"""
    i = 0
    for idx in range(len(acts)):
        x_save = acts[idx]
        weight_save = weights[idx]
        np.save(EXPORT_PATH + "act-" + str(idx) + "-0.npy", x_save)
        np.save(EXPORT_PATH + "wgt-" + str(idx) + ".npy", weight_save)

        x_save[x_save == 0] = int(0)
        x_save[x_save != 0] = int(1)
        np.savetxt(CX_PATH + "act" + str(idx) + ".csv",
                   x_save.reshape(-1),
                   delimiter=",",
                   fmt="%d")
        weight_save[weight_save == 0] = int(0)
        weight_save[weight_save != 0] = int(1)
        np.savetxt(CX_PATH + "Conv2D_" + str(idx) + ".csv",
                   weight_save.reshape(weight_save.shape[0], -1),
                   delimiter=",",
                   fmt="%d")

        if x_save.shape[2] > 1:
            name = "Conv" + str(idx)  #str(idxs[idx])
            IFM_height = str(x_save.shape[2] + (2 * paddings[idx]))
            IFM_width = str(x_save.shape[3] + (2 * paddings[idx]))
            filt_height = str(weight_save.shape[2])
            filt_width = str(weight_save.shape[3])
        else:
            name = "FC" + str(idx)  #str(idxs[idx])
            IFM_height = str(1)
            IFM_width = str(1)
            filt_height = str(1)
            filt_width = str(1)
        channels = str(weight_save.shape[1])
        if ("resnet50" in modelName) and (idx == 4 or idx == 15 or idx == 29
                                          or idx == 49):
            num_filt = str(weight_save.shape[0])
        else:
            num_filt = str(out_channels[i])
            i += 1
        fscale.write(name + ',\t' + IFM_height + ',\t' + IFM_width + ',\t' +
                     filt_height + ',\t' + filt_width + ',\t' + channels +
                     ',\t' + num_filt + ',\t' + str(strides[idx]) + ',\n')

    fscale.close()
    f.close()
    #"""
    cxf = open(CX_PATH + "ModelShape.txt", "w")
    cxf.write("LayerName\tLayerID\tInputShape\tOutputShape\tKernelShape\n")
    cx_layers = []
    layer_idx = 0
    for n, m in model.named_modules():
        if isinstance(m, torch.nn.Conv2d):
            if m.weight.shape[2] != m.weight.shape[3]:
                continue
            curr = "Conv\t" + str(layer_idx) + "\t" + str(
                tuple(acts[layer_idx].shape)).replace('(', '').replace(
                    ')', '').replace(' ', '') + "\t" + str(
                        tuple(acts[layer_idx].shape)).replace('(', '').replace(
                            ')', '').replace(' ', '') + "\t" + str(
                                tuple(
                                    m.weight.shape)).replace('(', '').replace(
                                        ')', '').replace(' ', '') + "\n"
            cxf.write(curr)
            cx_layers.append(curr)
            layer_idx += 1
        if isinstance(m, torch.nn.Linear) and (not skipLinear):
            curr = "FC\t" + str(idxs[layer_idx]) + "\t" + str(
                tuple(acts[layer_idx].shape)).replace('(', '').replace(
                    ')', '').replace(' ', '') + "\t" + str(
                        tuple(acts[layer_idx].shape)).replace('(', '').replace(
                            ')', '').replace(' ', '') + "\t" + str(
                                tuple(
                                    m.weight.shape)).replace('(', '').replace(
                                        ')', '').replace(' ', '') + ",1,1\n"
            cxf.write(curr)
            cx_layers.append(curr)
            layer_idx += 1
    if "transformer" in modelName:
        for i in range(11):
            for j in range(36, 97):
                cxf.write(cx_layers[j])
    cxf.close()

    return
Exemplo n.º 5
0
    return results


if __name__ == "__main__":
    parse = argparse.ArgumentParser(description='command for testing keras model with fp16 and fp32')
    parse.add_argument('--model', type=str, default='mobilenet', help='support vgg16, resnet50, densenet121, \
     inceptionv3, inception_resnet, xception, mobilenet, squeezenet')
    parse.add_argument('--dtype', type=str, default='float32')
    parse.add_argument('--alpha', type=float, default=0.5, help='alpha for mobilenet')
    args = parse.parse_args()

    K.set_floatx(args.dtype)

    # create model
    if args.model == 'vgg':
        model = vgg16.VGG16(input_shape=(224, 224, 3))
    elif args.model == 'inception':
        model = inception_v3.InceptionV3(input_shape=(299, 299, 3))
    elif args.model == 'inception_resnet':
        model = inception_resnet_v2.InceptionResNetV2(input_shape=(299, 299, 3))
    elif args.model == 'xception':
        model = xception.Xception(input_shape=(299, 299, 3))
    elif args.model == 'resnet':
        model = resnet_50.ResNet50(input_shape=(224, 224, 3))
    elif args.model == 'densenet':
        model = densenet_121.DenseNet(reduction=0.5, classes=1000)
    elif args.model == 'squeezenet':
        model = squeezenet.SqueezeNet(input_shape=(227, 227, 3), classes=1000)
    elif args.model == 'mobilenet':
        model = mobilenet.MobileNet(input_shape=(224, 224, 3), alpha=args.alpha)
    else:
Exemplo n.º 6
0
def get_model():
    if model_index == 0:
        return mobilenet_v1.MobileNetV1()
    elif model_index == 1:
        return mobilenet_v2.MobileNetV2()
    elif model_index == 2:
        return mobilenet_v3_large.MobileNetV3Large()
    elif model_index == 3:
        return mobilenet_v3_small.MobileNetV3Small()
    elif model_index == 4:
        return efficientnet.efficient_net_b0()
    elif model_index == 5:
        return efficientnet.efficient_net_b1()
    elif model_index == 6:
        return efficientnet.efficient_net_b2()
    elif model_index == 7:
        return efficientnet.efficient_net_b3()
    elif model_index == 8:
        return efficientnet.efficient_net_b4()
    elif model_index == 9:
        return efficientnet.efficient_net_b5()
    elif model_index == 10:
        return efficientnet.efficient_net_b6()
    elif model_index == 11:
        return efficientnet.efficient_net_b7()
    elif model_index == 12:
        return resnext.ResNeXt50()
    elif model_index == 13:
        return resnext.ResNeXt101()
    elif model_index == 14:
        return inception_v4.InceptionV4()
    elif model_index == 15:
        return inception_resnet_v1.InceptionResNetV1()
    elif model_index == 16:
        return inception_resnet_v2.InceptionResNetV2()
    elif model_index == 17:
        return se_resnet.se_resnet_50()
    elif model_index == 18:
        return se_resnet.se_resnet_101()
    elif model_index == 19:
        return se_resnet.se_resnet_152()
    elif model_index == 20:
        return squeezenet.SqueezeNet()
    elif model_index == 21:
        return densenet.densenet_121()
    elif model_index == 22:
        return densenet.densenet_169()
    elif model_index == 23:
        return densenet.densenet_201()
    elif model_index == 24:
        return densenet.densenet_264()
    elif model_index == 25:
        return shufflenet_v2.shufflenet_0_5x()
    elif model_index == 26:
        return shufflenet_v2.shufflenet_1_0x()
    elif model_index == 27:
        return shufflenet_v2.shufflenet_1_5x()
    elif model_index == 28:
        return shufflenet_v2.shufflenet_2_0x()
    elif model_index == 29:
        return resnet.resnet_18()
    elif model_index == 30:
        return resnet.resnet_34()
    elif model_index == 31:
        return resnet.resnet_50()
    elif model_index == 32:
        return resnet.resnet_101()
    elif model_index == 33:
        return resnet.resnet_152()
    elif model_index == 34:
        return vgg16.VGG16()
    elif model_index == 35:
        return vgg16_mini.VGG16()
    elif model_index == 36:
        return VGG16_self.VGG16()
    elif model_index == 10086:
        return diy_resnet.resnet_50()
    else:
        raise ValueError("The model_index does not exist.")