Exemplo n.º 1
0
def De_test(myDeconvNN_CS, with_new_csphi, myflag):

    test_data_list = "./Training_Data_L/validation1/"
    testImgList = tdp.eachFile(test_data_list)
    testImgArrayList_ori = tdp.crop_img(testImgList,
                                        crop_strides=tdp.crops_width)
    testImgArrayList = tdp.up_dim(testImgArrayList_ori)

    #myflag.model = "test"
    #myflag.check_batch_size()
    #print("In test, myflag.batch_size:", myflag.batch_size)
    CS_Phi = ge_csphi(with_new_csphi)
    # test数据集的总损失
    total_test_cost = 0.
    batch_num = int(len(testImgArrayList) / myflag.batch_size)
    #print("testImgArrayList[0].shape:", testImgArrayList[0].shape)
    for i in range(batch_num):
        img_batch = get_block_from_data(testImgArrayList, myflag.batch_size, i)
        test_cost_batch = myDeconvNN_CS.calc_cost(X_2D=img_batch,
                                                  CS_Phi=CS_Phi)
        total_test_cost += test_cost_batch
    print("In test phase, batch_size:", myflag.batch_size)
    #print("De_test_batch_cost=", "{:.9f}".format(total_test_cost / batch_num))
    print("De_one_test_cost=",
          "{:.9f}".format(total_test_cost / (batch_num * myflag.batch_size)))
Exemplo n.º 2
0
def cal_each_mean_PSNR(oriImgDir, reconImgDir):

    oriImgFullPaths = tdp.eachFile(oriImgDir)
    reconImgFullPaths = tdp.eachFile(reconImgDir)
    #oriImgList = []
    #reconImgList = []
    oridict = {}
    redict = {}
    for oriImgFullPath in oriImgFullPaths:
        oriImgName = os.path.split(oriImgFullPath)[1].split(".")[0]
        oridict[oriImgName] = oriImgFullPath
        '''
         a.append(os.path.split(oriImgFullPath)[1])
        oriImg = Image.open(oriImgFullPath)
        oriImgList.append(oriImg)
        '''
    for reconImgFullPath in reconImgFullPaths:
        reconImgName = os.path.split(reconImgFullPath)[1].split('.')[0]
        redict[reconImgName] = reconImgFullPath
        '''
        b.append(os.path.split(reconImgFullPath)[1])
        reconImg = Image.open(reconImgFullPath)
        reconImgList.append(reconImg)
        '''
    '''
    print("ori:", a)
    print("re:", b)
    '''
    PSNR_dict = {}
    totalPSNR = 0
    #for i in range(len(oriImgList)):
    for i in oridict:
        #print(i)
        oriImg = Image.open(oridict[i])
        reImg = Image.open(redict[i])
        somePSNR = sR.calc_PSNR(generate_img=reImg, original_img=oriImg)
        #somePSNR = sR.calc_PSNR(generate_img=reconImgList[i], original_img=oriImgList[i])
        #PSNR_dict[os.path.split(oriImgFullPaths[i])[1]] = somePSNR
        PSNR_dict[i] = somePSNR
        totalPSNR += somePSNR
    #meanPSNR = totalPSNR/len(oriImgList)
    meanPSNR = totalPSNR / len(oridict)
    print("PSNR of each pair:", PSNR_dict)
    print("meanPSNR:", meanPSNR)
    #print("without flinstone, meanPSNR:", (totalPSNR-PSNR_dict['flinstones'])/(len(oridict)-1))
    return
Exemplo n.º 3
0
def show_from_De1():
    Decnn = De1.myDeconvNN_CS
    dirs = "./Training_Data_L/Test/Set1/"
    imgList = tdp.eachFile(dirs)
    img = Image.open(imgList[0])
    img.show()
    imgArrayList = tdp.crop_img(imgList)
    imgArrayList_up = tdp.up_dim(imgArrayList)
    print("len(imgArrayList):", len(imgArrayList))
    result_img = get_Result_img(imgArrayList=imgArrayList_up,
                                original_img=img,
                                nn=Decnn)
    result_img.show()
    PSNR = calc_PSNR(result_img, img)
    print("PSNR:", PSNR)
Exemplo n.º 4
0
def save_reconImg_from_nn(nn):
    dirs = "./Training_Data_L/test_images/noiseless/"
    saveDir = './Training_Data_L/re_test_images/noiseless/v1/mr_0_01_e1000/'
    imgList = tdp.eachFile(dirs)
    #print("last one:", imgList[len(imgList)-1])
    crop_strides = tdp.crops_width
    imgArrayList = tdp.crop_img(imgList, crop_strides=crop_strides)
    imgArrayList_up = tdp.up_dim(imgArrayList)
    start = time.clock()
    save_Result_img(imgArrayList=imgArrayList_up,
                    oriFileDir=dirs,
                    saveDir=saveDir,
                    nn=nn,
                    crop_strides=crop_strides,
                    start=start)
    return
Exemplo n.º 5
0
def show_img_from_nn(nn):
    dirs = "./Training_Data_L/Test/Set1/"
    imgList = tdp.eachFile(dirs)
    img = Image.open(imgList[0])
    img.show()
    crop_strides = tdp.crops_width
    imgArrayList = tdp.crop_img(imgList, crop_strides=crop_strides)
    imgArrayList_up = tdp.up_dim(imgArrayList)
    result_img = get_Result_img(imgArrayList=imgArrayList_up,
                                original_img=img,
                                nn=nn,
                                crop_strides=crop_strides)
    result_img.show()
    #denoised_img_array = BM3D_denoise.ge_final(result_img)
    #print("enoised_img_array.shape:", denoised_img_array.shape)
    #print("denoised_img_array:", denoised_img_array)
    #denoised_img = Image.fromarray(denoised_img_array)
    #print("show the denoised image.")
    #denoised_img.show()
    PSNR_mine1 = calc_PSNR(result_img, img)
    #PSNR_mine2 = calc_PSNR(denoised_img, img)
    print("PSNR_mine1 between nondenoised_img and ori_img:", PSNR_mine1)
Exemplo n.º 6
0
def De_train(myDeconvNN_CS, with_new_csphi, myflag):
    train_data_dirs = "./Training_Data_L/Train/"
    trainImgList = tdp.eachFile(train_data_dirs)
    trainImgArrayList_ori = tdp.crop_img(trainImgList)
    trainImgArrayList = tdp.up_dim(trainImgArrayList_ori)

    myflag.model = "train"
    myflag.check_batch_size()
    CS_Phi = ge_csphi(with_new_csphi)
    # train中平均每个batch的总损失
    batch_num = int(len(trainImgArrayList) / myflag.batch_size)
    for epoch in range(training_epochs):
        total_train_cost = 0.
        for i in range(batch_num):
            image_batch = get_block_from_data(trainImgArrayList,
                                              myflag.batch_size, i)
            train_cost_batch = myDeconvNN_CS.partial_fit(X_2D=image_batch,
                                                         CS_Phi=CS_Phi)
            total_train_cost += train_cost_batch
        if epoch % display_step == 0:
            print(
                "Epoch:", '%04d' % (epoch + 1), "De_one_train_cost=",
                "{:.9f}".format(total_train_cost /
                                (myflag.batch_size * batch_num)))
            '''
            print("Epoch:", '%04d' % (epoch + 1), "De_train_batch_cost=",
                  "{:.9f}".format((total_train_cost / batch_num)))
            '''
        if (epoch + 1) % 10 == 0:
            De_test(myDeconvNN_CS=myDeconvNN_CS,
                    with_new_csphi=False,
                    myflag=myflag)

    print("In train phase, batch_size:", myflag.batch_size)
    print("De_one_train_cost=",
          "{:.9f}".format(total_train_cost / (myflag.batch_size * batch_num)))