示例#1
0
def triplet_img_gen(BASE_DIR,
                    IMG_ROW,
                    IMG_COL,
                    kindlist,
                    kindimgpathlist,
                    triplet_num=40):
    tripletlist = []

    for i in range(len(kindimgpathlist)):
        print(i)
        for j in range(triplet_num):
            negativeindex = random.choice(
                [num for num in range(len(kindimgpathlist)) if num not in [i]])
            pair = np.random.randint(0, len(kindimgpathlist[i]), 2)
            anchorpath = kindimgpathlist[i][pair[0]]
            anchor = imgarr(BASE_DIR + anchorpath)
            positivepath = kindimgpathlist[i][pair[1]]
            positive = imgarr(BASE_DIR + positivepath)
            rnd_id = random.randint(0, len(kindimgpathlist[negativeindex]) - 1)
            negativepath = kindimgpathlist[negativeindex][rnd_id]
            negative = imgarr(BASE_DIR + negativepath)
            anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
            positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
            negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
            triplet = [anchor, positive, negative]
            tripletlist.append(triplet)
    return np.array(tripletlist)
示例#2
0
def siamese_train_gen(BASE_DIR,
                      IMG_ROW,
                      IMG_COL,
                      pathdata,
                      labeldata,
                      batch_size=50):
    while (True):
        imglist1 = []
        imglist2 = []
        labellist = []
        for i in range(batch_size):
            rndid = random.randint(0, len(pathdata) - 1)
            imgpath = BASE_DIR + pathdata[pathdata.index[rndid]]
            img1 = imgarr(imgpath)
            if (i % 2 == 0):
                kind = 0
                labellist.append(0)
            else:
                kind = 1
                labellist.append(1)
            img2 = siamese_fetch_img(BASE_DIR,
                                     labeldata[labeldata.index[rndid]],
                                     labeldata,
                                     pathdata,
                                     kind=kind)
            img1 = cv2.resize(img1, (IMG_ROW, IMG_COL))

            img2 = cv2.resize(img2, (IMG_ROW, IMG_COL))
            imglist1.append(img1)
            imglist2.append(img2)
        yield ([np.asarray(imglist1),
                np.asarray(imglist2)], np.asarray(labellist))
示例#3
0
def triplet_train_gen(BASE_DIR,
                      IMG_ROW,
                      IMG_COL,
                      pathdata,
                      labeldata,
                      batch_size=50):
    while (True):
        anchorlist = []
        positivelist = []
        negativelist = []
        for i in range(batch_size):
            rndid = random.randint(0, len(pathdata) - 1)
            imgpath = BASE_DIR + pathdata[pathdata.index[rndid]]
            anchor = imgarr(imgpath)
            anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
            anchorlist.append(anchor)
            positive = siamese_fetch_img(BASE_DIR,
                                         labeldata[labeldata.index[rndid]],
                                         labeldata,
                                         pathdata,
                                         kind=1)
            negative = siamese_fetch_img(BASE_DIR,
                                         labeldata[labeldata.index[rndid]],
                                         labeldata,
                                         pathdata,
                                         kind=0)
            positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
            negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
            positivelist.append(positive)
            negativelist.append(negative)
        yield ([
            np.asarray(anchorlist),
            np.asarray(positivelist),
            np.asarray(negativelist)
        ], None)
示例#4
0
def triplet_train_gen1(BASE_DIR,
                       IMG_ROW,
                       IMG_COL,
                       pathdata,
                       labeldata,
                       batch_size=50):
    while (True):
        imglist = []
        for i in range(batch_size):
            rndid = random.randint(0, len(pathdata) - 1)
            imgpath = BASE_DIR + pathdata[pathdata.index[rndid]]
            anchor = imgarr(imgpath)
            anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
            positive = siamese_fetch_img(BASE_DIR,
                                         labeldata[labeldata.index[rndid]],
                                         labeldata,
                                         pathdata,
                                         kind=1)
            negative = siamese_fetch_img(BASE_DIR,
                                         labeldata[labeldata.index[rndid]],
                                         labeldata,
                                         pathdata,
                                         kind=0)
            positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
            negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
            imglist.append([anchor, positive, negative])
        imglist = np.asarray(imglist)
        yield ([imglist[:, 0], imglist[:, 1], imglist[:, 2]])
示例#5
0
def triplet_valid_gen(BASE_DIR, IMG_ROW, IMG_COL, pathdata, labeldata):
    anchorlist = []
    positivelist = []
    negativelist = []
    for i in range(len(pathdata)):
        imgpath = BASE_DIR + pathdata[pathdata.index[i]]
        print(imgpath)
        print(os.path.exists(imgpath))
        anchor = imgarr(imgpath)
        anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
        positive = siamese_fetch_img(BASE_DIR,
                                     labeldata[labeldata.index[i]],
                                     labeldata,
                                     pathdata,
                                     kind=1)
        negative = siamese_fetch_img(BASE_DIR,
                                     labeldata[labeldata.index[i]],
                                     labeldata,
                                     pathdata,
                                     kind=0)
        positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
        negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
        anchorlist.append(anchor)
        positivelist.append(positive)
        negativelist.append(negative)
    return ([
        np.asarray(anchorlist),
        np.asarray(positivelist),
        np.asarray(negativelist)
    ], None)
示例#6
0
def img_all_list(BASE_DIR, IMG_ROW, IMG_COL, imgpathlist):
    imglist = []
    for path in imgpathlist:
        imgpath = BASE_DIR + path
        img = imgarr(imgpath)
        img = cv2.resize(img, (IMG_ROW, IMG_COL))
        imglist.append(img)
    return np.asarray(imglist)
示例#7
0
def extract_imgarr(BASE_DIR, pathlist, IMG_ROW, IMG_COL):
    pathpair = np.random.randint(0, len(pathlist), 2)
    imglist = []
    for i in range(2):
        path = BASE_DIR + pathlist[pathpair[i]]
        img = imgarr(path)
        img = cv2.resize(img, (IMG_ROW, IMG_COL))
        imglist.append(img)
    return imglist
示例#8
0
def triplet_img_gen1(BASE_DIR,
                     IMG_ROW,
                     IMG_COL,
                     kindlist,
                     kindimgpathlist,
                     times_kind=40,
                     triplet_num=10,
                     batch_size=10):
    while (True):
        anchorlist = []
        positivelist = []
        negativelist = []
        for i in range(batch_size):
            print('batch_size:' + str(i))
            for j in range(times_kind):
                print('kind_num:' + str(j))
                for k in range(triplet_num):
                    rndid = random.randint(0, len(kindimgpathlist) - 1)
                    negativeindex = random.choice([
                        num for num in range(len(kindimgpathlist))
                        if num not in [rndid]
                    ])
                    pair = np.random.randint(0, len(kindimgpathlist[rndid]), 2)
                    anchorpath = kindimgpathlist[rndid][pair[0]]
                    positivepath = kindimgpathlist[rndid][pair[1]]
                    rndid1 = random.randint(
                        0,
                        len(kindimgpathlist[negativeindex]) - 1)
                    negativepath = kindimgpathlist[negativeindex][rndid1]
                    anchor = imgarr(BASE_DIR + anchorpath)
                    positive = imgarr(BASE_DIR + positivepath)
                    negative = imgarr(BASE_DIR + negativepath)
                    anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
                    positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
                    negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
                    anchorlist.append(anchor)
                    positivelist.append(positive)
                    negativelist.append(negative)
        yield ([
            np.asarray(anchorlist),
            np.asarray(positivelist),
            np.asarray(negativelist)
        ], None)
示例#9
0
def pretrain_valid_gen(BASE_DIR, IMG_ROW, IMG_COL, pathdata, labeldata):
    imglist = []
    labellist = []
    for i in range(len(pathdata)):
        imgpath = BASE_DIR + pathdata[pathdata.index[i]]
        img = imgarr(imgpath)
        img = cv2.resize(img, (IMG_ROW, IMG_COL))
        imglist.append(img)
        if (labeldata[labeldata.index[i]] == 'new_whale'):
            labellist.append(0)
        else:
            labellist.append(1)
    return (np.asarray(imglist), np.asarray(labellist))
示例#10
0
def siamese_img_gen(BASE_DIR,
                    IMG_ROW,
                    IMG_COL,
                    kindimgpathlist,
                    contrast_times=10,
                    batch_size=50):
    while (True):
        imglist1 = []
        imglist2 = []
        labellist = []
        for i in range(batch_size):
            for j in range(contrast_times):
                rndid = random.randint(0, len(kindimgpathlist) - 1)
                if (i % 2 == 0):
                    pair = np.random.randint(0, len(kindimgpathlist[rndid]), 2)
                    imgpath1 = kindimgpathlist[rndid][pair[0]]
                    imgpath2 = kindimgpathlist[rndid][pair[1]]
                    labellist.append(0)
                else:
                    rndid1 = random.randint(0, len(kindimgpathlist[rndid]) - 1)
                    imgpath1 = kindimgpathlist[rndid][rndid1]
                    index1 = random.choice([
                        num for num in range(len(kindimgpathlist))
                        if num not in [rndid]
                    ])
                    rndid2 = random.randint(0,
                                            len(kindimgpathlist[index1]) - 1)
                    imgpath2 = kindimgpathlist[index1][rndid2]
                    labellist.append(1)
                img1 = imgarr(BASE_DIR + imgpath1)
                img2 = imgarr(BASE_DIR + imgpath2)
                img1 = cv2.resize(img1, (IMG_ROW, IMG_COL))
                img2 = cv2.resize(img2, (IMG_ROW, IMG_COL))
                imglist1.append(img1)
                imglist2.append(img2)
        yield ([np.asarray(imglist1),
                np.asarray(imglist2)], np.asarray(labellist))
示例#11
0
def pretrain_train_gen(BASE_DIR,
                       IMG_ROW,
                       IMG_COL,
                       pathdata,
                       labeldata,
                       batch_size=50):
    while (True):
        imglist = []
        labellist = []
        for i in range(batch_size):
            rnd_id = random.randint(0, len(pathdata) - 1)
            imgpath = BASE_DIR + pathdata[pathdata.index[rnd_id]]
            img = imgarr(imgpath)
            img = cv2.resize(img, (IMG_ROW, IMG_COL))
            imglist.append(img)
            if (labeldata[labeldata.index[rnd_id]] == 'new_whale'):
                labellist.append(0)
            else:
                labellist.append(1)
        yield (np.asarray(imglist), np.asarray(labellist))
示例#12
0
def triplet_valid_gen1(BASE_DIR, IMG_ROW, IMG_COL, pathdata, labeldata):
    imglist = []
    for i in range(len(pathdata)):
        imgpath = BASE_DIR + pathdata[pathdata.index[i]]
        anchor = imgarr(imgpath)
        anchor = cv2.resize(anchor, (IMG_ROW, IMG_COL))
        positive = siamese_fetch_img(BASE_DIR,
                                     labeldata[labeldata.index[i]],
                                     labeldata,
                                     pathdata,
                                     kind=1)
        negative = siamese_fetch_img(BASE_DIR,
                                     labeldata[labeldata.index[i]],
                                     labeldata,
                                     pathdata,
                                     kind=0)
        positive = cv2.resize(positive, (IMG_ROW, IMG_COL))
        negative = cv2.resize(negative, (IMG_ROW, IMG_COL))
        imglist.append([anchor, positive, negative])
    imglist = np.asarray(imglist)
    return ([imglist[:, 0], imglist[:, 1], imglist[:, 2]])
示例#13
0
def siamese_valid_gen(BASE_DIR, IMG_ROW, IMG_COL, pathdata, labeldata):
    imglist1 = []
    imglist2 = []
    labellist = []
    for i in range(len(pathdata)):
        imgpath = BASE_DIR + pathdata[pathdata.index[i]]
        img1 = imgarr(imgpath)
        if (i % 2 == 0):
            kind = 0
            labellist.append(0)
        else:
            kind = 1
            labellist.append(1)
        img2 = siamese_fetch_img(BASE_DIR,
                                 labeldata[labeldata.index[i]],
                                 labeldata,
                                 pathdata,
                                 kind=kind)
        img1 = cv2.resize(img1, (IMG_ROW, IMG_COL))
        img2 = cv2.resize(img2, (IMG_ROW, IMG_COL))
        imglist1.append(img1)
        imglist2.append(img2)
    return ([np.asarray(imglist1),
             np.asarray(imglist2)], np.asarray(labellist))