Beispiel #1
0
 def val_reader():
     traindataset = myreader.myreader_classify(val_datasetfile,
                                               val_labelfile,
                                               'val',
                                               doshuffle=False)
     for image, label in traindataset:
         yield image, label
def debug_patchloss():
    #debug loss NaN problem
    from losses import SoftmaxLoss
    from losses import ArcMarginLoss
    traindataset = myreader.myreader_classify(train_datasetfile,
                                              train_labelfile,
                                              'train',
                                              iscolor=0,
                                              doshuffle=True,
                                              preprocessfunc=preprocess)

    embedding_size = 64
    class_dim = 50000
    batchsize = 128
    model = l2net.L2Net()
    #model = resnet18.ResNet18()
    image = fluid.layers.data(name='image', shape=[1, 32, 32], dtype='uint8')
    labelvar = fluid.layers.data(name='label', shape=[1], dtype='int64')
    inputdata = trainmodule.preprocessimg(image)
    out = model.net(input=inputdata, embedding_size=embedding_size)
    metricloss = ArcMarginLoss(class_dim=class_dim,
                               margin=0.5,
                               scale=64,
                               easy_margin=False)

    #metricloss = SoftmaxLoss(class_dim=class_dim)

    cost, logit = metricloss.loss(out, labelvar)
    avg_cost = fluid.layers.mean(x=cost)

    imgs = []
    labels = []
    for img, label in traindataset:
        if label < class_dim:
            imgs.append(img.reshape(1, 1, 32, 32))
            labels.append(label)
            if len(imgs) == batchsize:
                break
    imgs = np.vstack(imgs)
    labels = np.array(labels, np.int64).reshape((-1, 1))

    optimizer = fluid.optimizer.SGD(learning_rate=0.1)
    opts = optimizer.minimize(avg_cost)

    place = fluid.CPUPlace()
    #place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    loss_value, output_value, label_value = exe.run(
        fluid.default_main_program(),
        feed={
            'image': imgs,
            'label': labels
        },
        fetch_list=[cost.name, out.name, labelvar.name])

    for x, y, z in zip(loss_value, output_value, label_value):
        print list(x.flat), np.sum(y.flat), list(z.flat)
 def val_reader():
     traindataset = myreader.myreader_classify(val_datasetfile,
                                               val_labelfile,
                                               'val',
                                               doshuffle=False,
                                               iscolor=0,
                                               preprocessfunc=preprocess)
     for image, label in traindataset:
         yield image, label
 def val_reader():
     traindataset = myreader.myreader_classify(args.val_datasetfile,
                                               args.val_labelfile, 'val')
     for image, label in traindataset:
         yield image, label
def debug_faceloss():
    initrandom()
    #debug loss NaN problem
    from losses import SoftmaxLoss
    from losses import ArcMarginLoss

    train_datasetfile = 'dataset/face_ms1m_small/train.data'
    train_labelfile = 'dataset/face_ms1m_small/train.label'
    traindataset = myreader.myreader_classify(
        train_datasetfile,
        train_labelfile,
        'train',
        iscolor=1,
        doshuffle=False,  #disable shuffle for debug
    )

    embedding_size = 256
    class_dim = 1000
    batchsize = 128
    imgshape = [3, 112, 112]
    model = resnet18.ResNet18()
    #model = SimpleNet()
    image = fluid.layers.data(name='image', shape=imgshape, dtype='uint8')
    labelvar = fluid.layers.data(name='label', shape=[1], dtype='int64')
    inputdata = trainmodule.preprocessimg(image,
                                          mean=[127.5, 127.5, 127.5],
                                          std=[128.0, 128.0, 128.0])
    out = model.net(input=inputdata, embedding_size=embedding_size)

    metricloss = ArcMarginLoss(class_dim=class_dim,
                               margin=0.5,
                               scale=64,
                               easy_margin=False)

    #metricloss = SoftmaxLoss(class_dim=class_dim)

    cost, logit = metricloss.loss(out, labelvar)
    avg_cost = fluid.layers.mean(x=cost)

    if os.path.exists('debugbatch.npz'):
        loaddata = np.load('debugbatch.npz')
        imgs, labels = loaddata['imgs'], loaddata['labels']
        print('img,label shape', imgs.shape, labels.shape, 'save mat sum',
              np.sum(imgs), np.sum(labels))
    else:
        imgs = []
        labels = []
        for img, label in traindataset:
            if label < class_dim:
                imgs.append(img.reshape([1] + imgshape))
                labels.append(label)
                if len(imgs) == batchsize:
                    break
        imgs = np.vstack(imgs)
        labels = np.array(labels, np.int64).reshape((-1, 1))
        np.savez('debugbatch.npz', imgs=imgs, labels=labels)
        print('save mat sum', np.sum(imgs), np.sum(labels))

    #place = fluid.CPUPlace()
    place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if not os.path.exists('debugmodel'):
        fluid.io.save_persistables(exe,
                                   'debugmodel',
                                   main_program=fluid.default_main_program())
    else:
        fluid.io.load_persistables(exe,
                                   'debugmodel',
                                   main_program=fluid.default_main_program())

    fcvars = getvariable_value('embedded.w_0')
    assert (len(fcvars) == 1)
    fcname = fcvars[0].name

    #optimizer = fluid.optimizer.SGD(learning_rate=0.1)
    #opts = optimizer.minimize(avg_cost)

    fetch_list = [cost.name, inputdata.name, logit.name]
    for i in range(1):
        outputlist = exe.run(fluid.default_main_program(),
                             feed={
                                 'image': imgs,
                                 'label': labels
                             },
                             fetch_list=fetch_list)

        for row in range(len(outputlist[0])):
            outsum = [(fetch_list[col], list(outputlist[col][row].flat[:3]))
                      for col in range(len(fetch_list))]
            out = outputlist[2][row]
            normout = np.sum(out * out)
            print row, outsum, normout, out.shape