$CODE$

1.

import numpy as np
import os
from PIL import Image
from matplotlib import pyplot as plt
import scipy.misc as msic

data_dir="E:/experiment/项目记录/2019-1-10-实验四/datasets"
save_dir="E:/experiment/项目记录/2019-1-10-实验四/datasets-argumentation"

data_image=data_dir+"/image"
data_depth=data_dir+"/depth"
data_label=data_dir+"/label"

save_image=save_dir+"/image"
save_depth=save_dir+"/depth"
save_SV=save_dir+"/SV"
save_UV=save_dir+"/UV"

lists=[]
dicts={}

images=os.listdir(data_image)
depths=os.listdir(data_depth)
labels=os.listdir(data_label)

for img in images:
    img_num=img.split(".")[0]
    dicts["image"]=img
    dicts["depth"]="depth_"+img_num.split("_")[0]+img_num.split("_")[1]+".jpg"
    dicts["SV"]=img_num+"_SV_1.npy"
    dicts["UV"]=img_num+"_UV_1.npy"
    lists.append(dicts.copy())

for dict in lists:
    img_path=data_image+"/"+dict["image"]
    depth_path=data_depth+"/"+dict["depth"]
    SV_path=data_label+"/"+dict["SV"]
    UV_path=data_label+"/"+dict["UV"]
    #source
    image=Image.open(img_path).resize([500, 500], Image.ANTIALIAS)
    depth=Image.open(depth_path).resize([500, 500], Image.ANTIALIAS)
    SV=Image.fromarray(np.load(SV_path)).resize([500, 500], Image.ANTIALIAS)
    UV=Image.fromarray(np.load(UV_path)).resize([500, 500], Image.ANTIALIAS)

    image_30=image.rotate(30)
    depth_30=depth.rotate(30)
    SV_30=SV.rotate(30)
    UV_30=UV.rotate(30)
    #msic.imsave("C:/Users/zy/Desktop/1111.jpg", np.array(UV_30))

    image_60=image.rotate(60)
    depth_60=depth.rotate(60)
    SV_60=SV.rotate(60)
    UV_60=UV.rotate(60)


   #L-R
    LR_image=image.transpose(Image.FLIP_LEFT_RIGHT)
    LR_depth=depth.transpose(Image.FLIP_LEFT_RIGHT)
    LR_SV=SV.transpose(Image.FLIP_LEFT_RIGHT)
    LR_UV=UV.transpose(Image.FLIP_LEFT_RIGHT)

    LR_image_30=LR_image.rotate(30)
    LR_depth_30=LR_depth.rotate(30)
    LR_SV_30=LR_SV.rotate(30)
    LR_UV_30=LR_UV.rotate(30)

    LR_image_60=LR_image.rotate(60)
    LR_depth_60=LR_depth.rotate(60)
    LR_SV_60=LR_SV.rotate(60)
    LR_UV_60=LR_UV.rotate(60)


    #T-D
    TD_image=image.transpose(Image.FLIP_TOP_BOTTOM)
    TD_depth=depth.transpose(Image.FLIP_TOP_BOTTOM)
    TD_SV=SV.transpose(Image.FLIP_TOP_BOTTOM)
    TD_UV=UV.transpose(Image.FLIP_TOP_BOTTOM)

    TD_image_30=TD_image.rotate(30)
    TD_depth_30=TD_depth.rotate(30)
    TD_SV_30=TD_SV.rotate(30)
    TD_UV_30=TD_UV.rotate(30)

    TD_image_60=TD_image.rotate(60)
    TD_depth_60=TD_depth.rotate(60)
    TD_SV_60=TD_SV.rotate(60)
    TD_UV_60=TD_UV.rotate(60)

    #**save
    #print(dict["image"], "\n",dict["depth"], "\n",dict["SV"], "\n",dict["UV"])
    #source
    image_30_name=save_image+"/30_"+dict["image"]
    depth_30_name=save_depth+"/30_"+dict["depth"]
    SV_30_name=save_SV+"/30_"+dict["SV"].split(".")[0]+".jpg"
    UV_30_name=save_UV+"/30_"+dict["UV"].split(".")[0]+".jpg"

    image_60_name=save_image+"/60_"+dict["image"]
    depth_60_name=save_depth+"/60_"+dict["depth"]
    SV_60_name=save_SV+"/60_"+dict["SV"].split(".")[0]+".jpg"
    UV_60_name=save_UV+"/60_"+dict["UV"].split(".")[0]+".jpg"


    #L-R
    LR_image_name=save_image+"/LR_"+dict["image"]
    LR_depth_name=save_depth+"/LR_"+dict["depth"]
    LR_SV_name=save_SV+"/LR_"+dict["SV"].split(".")[0]+".jpg"
    LR_UV_name=save_UV+"/LR_"+dict["UV"].split(".")[0]+".jpg"

    LR_image_30_name=save_image+"/LR_30_"+dict["image"]
    LR_depth_30_name=save_depth+"/LR_30_"+dict["depth"]
    LR_SV_30_name=save_SV+"/LR_30_"+dict["SV"].split(".")[0]+".jpg"
    LR_UV_30_name=save_UV+"/LR_30_"+dict["UV"].split(".")[0]+".jpg"

    LR_image_60_name=save_image+"/LR_60_"+dict["image"]
    LR_depth_60_name=save_depth+"/LR_60_"+dict["depth"]
    LR_SV_60_name=save_SV+"/LR_60_"+dict["SV"].split(".")[0]+".jpg"
    LR_UV_60_name=save_UV+"/LR_60_"+dict["UV"].split(".")[0]+".jpg"

    #T-D
    TD_image_name=save_image+"/TD_"+dict["image"]
    TD_depth_name=save_depth+"/TD_"+dict["depth"]
    TD_SV_name=save_SV+"/TD_"+dict["SV"].split(".")[0]+".jpg"
    TD_UV_name=save_UV+"/TD_"+dict["UV"].split(".")[0]+".jpg"

    TD_image_30_name=save_image+"/TD_30_"+dict["image"]
    TD_depth_30_name=save_depth+"/TD_30_"+dict["depth"]
    TD_SV_30_name=save_SV+"/TD_30_"+dict["SV"].split(".")[0]+".jpg"
    TD_UV_30_name=save_UV+"/TD_30_"+dict["UV"].split(".")[0]+".jpg"

    TD_image_60_name=save_image+"/TD_60_"+dict["image"]
    TD_depth_60_name=save_depth+"/TD_60_"+dict["depth"]
    TD_SV_60_name=save_SV+"/TD_60_"+dict["SV"].split(".")[0]+".jpg"
    TD_UV_60_name=save_UV+"/TD_60_"+dict["UV"].split(".")[0]+".jpg"

#-----------
    image_30.save(image_30_name)
    depth_30.save(depth_30_name)
    msic.imsave(SV_30_name, np.array(SV_30))
    msic.imsave(UV_30_name, np.array(UV_30))

    image_60.save(image_60_name)
    depth_60.save(depth_60_name)
    msic.imsave(SV_60_name, np.array(SV_60))
    msic.imsave(UV_60_name, np.array(UV_60))


    LR_image.save(LR_image_name)
    LR_depth.save(LR_depth_name)
    msic.imsave(LR_SV_name, np.array(LR_SV))
    msic.imsave(LR_UV_name, np.array(LR_UV))

    LR_image_30.save(LR_image_30_name)
    LR_depth_30.save(LR_depth_30_name)
    msic.imsave(LR_SV_30_name, np.array(LR_SV_30))
    msic.imsave(LR_UV_30_name, np.array(LR_UV_30))

    LR_image_60.save(LR_image_60_name)
    LR_depth_60.save(LR_depth_60_name)
    msic.imsave(LR_SV_60_name, np.array(LR_SV_60))
    msic.imsave(LR_UV_60_name, np.array(LR_UV_60))

    TD_image.save(TD_image_name)
    TD_depth.save(TD_depth_name)
    msic.imsave(TD_SV_name, np.array(TD_SV))
    msic.imsave(TD_UV_name, np.array(TD_UV))

    TD_image_30.save(TD_image_30_name)
    TD_depth_30.save(TD_depth_30_name)
    msic.imsave(TD_SV_30_name, np.array(TD_SV_30))
    msic.imsave(TD_UV_30_name, np.array(TD_UV_30))

    TD_image_60.save(TD_image_60_name)
    TD_depth_60.save(TD_depth_60_name)
    msic.imsave(TD_SV_60_name, np.array(TD_SV_60))
    msic.imsave(TD_UV_60_name, np.array(TD_UV_60))



print("***finsh***")

2.


from PIL import  Image
import numpy as np
from matplotlib import pyplot as plt
import os

def mapping(x):
    if x<=37:
        x=0
    else:
        x=1
    return x



uv_path="E:/experiment/项目记录/2019-1-10-实验四/datasets-argumentation/UV"
sv_path="E:/experiment/项目记录/2019-1-10-实验四/datasets-argumentation/SV"
save_path="E:/experiment/项目记录/2019-1-10-实验四/datasets-argumentation/sv-uv"

uvs=os.listdir(uv_path)
svs=os.listdir(sv_path)

for lab_u in uvs:
    I_uvs = []
    I_svs = []

    list_us=lab_u.split("_")
    lab_u_num="_".join(list_us[:-2])

    I_uv=Image.open(uv_path+"/"+lab_u)
    I_uv=np.array(I_uv)
    for li in I_uv:
        li=list(map(mapping, li))
        I_uvs.append(li)
    I_uvs=np.array(I_uvs)[:, :, np.newaxis]
    for lab_s in svs:
        list_sv=lab_s.split("_")
        lab_s_num="_".join(list_sv[:-2])
        if lab_s_num == lab_u_num:
            I_sv=Image.open(sv_path+"/"+lab_s)
            I_sv=np.array(I_sv)
            for li in I_sv:
                li=list(map(mapping, li))
                I_svs.append(li)
    I_svs=np.array(I_svs)[:, :, np.newaxis]
    concat=np.concatenate([I_svs, I_uvs], axis=2)
    np.save(save_path+"/"+lab_u_num+".npy", concat)

print("***finsh***")

3.IOU-AP

#IOU-version-1
import  matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import tensorflow as tf
from collections import Counter

def mapp(x):
    if x <38:
        x=0
    else:
        x=1
    return x

def mapping(x):
    images=[]
    for li in x:
        li=list(map(mapp, li))
        images.append(li)
    return np.array(images)

full=Image.open("E:/experiment/项目记录/2019-1-14/结果/code-mask/DYCE/val_datasets/data-2/full-mask/8_279_28_amodal.png")
full=full.resize([500, 500], Image.ANTIALIAS)

full_pred=Image.open("E:/experiment/项目记录/2019-1-14/结果/code-mask/DYCE/results/results_2/uv+vs/pred_8_279.jpg")

full=np.array(full)
full_pred=np.array(full_pred)

full=np.reshape(full, [-1,])
full_pred=np.reshape(full_pred, [-1,])

full_where=np.where(full>0)[0]
full_pred_where=np.where(full_pred>0)[0]



inters = []
unois=[]
for i in full_where:
    if i in full_pred_where:
        inters.append(i)

for i in full_where:
    unois.append(i)
for i in full_pred_where:
    unois.append(i)
unois=np.unique(unois)

print(len(inters)/len(unois))

#============================================
#IOU-version-2
import numpy as np
from PIL import Image


def IOU(x, y):
    """
    :param x: the path to the  prediction
    :param y: the path to the groundtruth
    :return:  IOU
    """
#pre-processing
    pred=Image.open(x)
    gt=Image.open(y)
    pred=np.array(pred)
    gt=np.array(gt)
    pred=np.reshape(pred, [-1,])
    gt=np.reshape(gt, [-1,])

    index_pred=np.where(pred>0)[0]
    index_gt=np.where(gt>0)[0]

    intersection=[]
    union=[]

    for ind in index_pred:
        if ind in index_gt:
            intersection.append(ind)
    for ind in index_gt:
        union.append(ind)
    for ind in index_pred:
        union.append(ind)
    union=np.unique(union)

    iou=np.float32(len(intersection))/np.float32(len(union))
    return iou
dicts={}
lists=[]

values=[]
#inserting you address
data_dir=""

prediction=data_dir+"prediction/"
groundtruth=data_dir+"groundtruth/"

pred_files=os.listdir(prediction)

for file in pred_files:
    pred_path=prediction+file
    ###change in your facts
    gt_path=groundtruth+file
    iou=IOU(pred_path, gt_path)

    dict[file]=iou
    lists.append(dict)

#compute mIOU

for dict in lists:
    for value in dict.values():
        values.append(value)

mIOU=np.mean(np.array(values))
print("mIOU=", mIOU)
#==========================
#iou
intersection = np.logical_and(target, prediction)
union = np.logical_or(target, prediction)
iou_score = np.sum(intersection) / np.sum(union)

#===========================
#AP
def AP(x, y):
    '''
    :param x:the path to the prediction
    :param y: the path to the groundtruth
    :return: a value of accuracy of AP
    '''
    prediction=np.array(Image.open(x))
    ground_truth=np.array(Image.open(y))
    bool_matrix=prediction==ground_truth
    bool_matrix=np.array(bool_matrix)
    #bool_matrix=np.cast(bool_matrix, np.int32)
    real_sum=np.sum(bool_matrix)
    sum=np.shape(bool_matrix)[0]*np.shape(bool_matrix)[1]
    accuracy=np.float32(real_sum)/np.float32(sum)
    return accuracy

4.deeplab-resnet里面的decode_label函数:

# colour map
label_colours = [(0,0,0)
                # 0=background
                ,(128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128)
                # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
                ,(0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0)
                # 6=bus, 7=car, 8=cat, 9=chair, 10=cow
                ,(192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128)
                # 11=diningtable, 12=dog, 13=horse, 14=motorbike, 15=person
                ,(0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)]
                # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor

def decode_labels(mask, num_images=1, num_classes=21):
    """Decode batch of segmentation masks.
    
    Args:
      mask: result of inference after taking argmax.
      num_images: number of images to decode from the batch.
      num_classes: number of classes to predict (including background).
    
    Returns:
      A batch with num_images RGB images of the same size as the input. 
    """
    n, h, w, c = mask.shape
    assert(n >= num_images), 'Batch size %d should be greater or equal than number of images to save %d.' % (n, num_images)
    outputs = np.zeros((num_images, h, w, 3), dtype=np.uint8)
    for i in range(num_images):
      img = Image.new('RGB', (len(mask[i, 0]), len(mask[i])))
      pixels = img.load()
      for j_, j in enumerate(mask[i, :, :, 0]):
          for k_, k in enumerate(j):
              if k < num_classes:
                  pixels[k_,j_] = label_colours[k]
      outputs[i] = np.array(img)
    return outputs

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值