Git Product home page Git Product logo

Comments (2)

gasharper avatar gasharper commented on June 10, 2024
# 前面是加载flow模型(略)

# 计算模板feat_mean
flow.eval()
feat_sum, cnt = [0 for _ in range(L)], 0
for val_dict in val_loader:
    image = val_dict['images'].to(device)
    with torch.no_grad():
        pyramid2= flow(image) 
        cnt += 1
    feat_sum = [p0+p for p0, p in zip(feat_sum, pyramid2)]
feat_mean = [p/cnt for p in feat_sum]

# 预处理
img2tensor = transforms.Compose([transforms.ToPILImage(),
                                        transforms.Resize((x_size,x_size)),
                                        transforms.ToTensor(),
                                        transforms.Normalize(img_mean, img_std)])

for datapath in glob(osp.join(datadir, cls_name, 'test', '*', '*.png')): # 这里改成你需要的
    save_dir = osp.join('diff', '/'.join(datapath.split('/')[-4:-1]) ) # 这里改成你需要的
    os.makedirs(save_dir, exist_ok=True)
    img = np.array(Image.open(datapath).convert('RGB'))
    img = img2tensor(img).unsqueeze(0).to(device) # h,w,c
    with torch.no_grad():
        pyramid2 = flow(img)
        pyramid_diff = [(feat2 - template).abs() for feat2, template in zip(pyramid2, feat_mean)]
        diff = flow.pyramid.compose_pyramid(pyramid_diff).mean(1) # b,h,w
        np_diff = diff[0].cpu().detach().numpy()
        plt.imshow(np_diff, vmax=0.9*np_diff.max(),) # 绘制anomaly map
        plt.gca().xaxis.set_major_locator(plt.NullLocator())
        plt.gca().yaxis.set_major_locator(plt.NullLocator())
        save_path = osp.join(save_dir, datapath.split('/')[-1] )
        plt.savefig(save_path,  bbox_inches='tight', pad_inches=0)

from pyramidflow.

leejq666 avatar leejq666 commented on June 10, 2024

from pyramidflow.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.