Giter Club home page Giter Club logo

novelty-detection's Introduction

Latent Space Autoregression for Novelty Detection

This repository contains Pytorch code to replicate experiments in the CVPR19 paper "Latent Space Autoregression for Novelty Detection".

Please cite with the following BibTeX:

@inproceedings{abati2019latent,
  title={{Latent Space Autoregression for Novelty Detection}},
  author={Abati, Davide and Porrello, Angelo and Calderara, Simone and Cucchiara, Rita},
  booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision and Pattern Recognition},
  year={2019}
}

sample results

Specifically, performs:

  • one class classification on MNIST.
  • one class classification on CIFAR-10.
  • video anomaly detection on UCSD Ped2.
  • video anomaly detection on ShanghaiTech.

0 - Clone this repo

First things first, clone this repository locally via git.

git clone https://github.com/cvpr19-858/novelty-detection.git
cd novelty-detection

1 - Environment

This code runs on Python 3.6. The easiest way to set up the environment is via pip and the file requirements.txt:

pip install -r requirements.txt

2 - Datasets

MNIST and CIFAR-10 will be downloaded for you by torchvision.

You still need to download UCSD Ped and ShanghaiTech. After download, please unpack them into the data folder as follows

tar -xzvf <path-to-UCSD_Anomaly_Dataset.tar.gz> -C data
tar -xzvf <path-to-shanghaitech.tar.gz> -C data

3 - Model checkpoints

Checkpoints for all trained models are available here.

Please untar them into the checkpoints folder as follows:

tar -xzvf <path-to-tar.gz> -C checkpoints

4 - Run!

Once your setup is complete, running tests is as simple as running test.py.

Usage:

usage: test.py [-h]

positional arguments:
              The name of the dataset to perform tests on.Choose among
              `mnist`, `cifar10`, `ucsd-ped2`, `shanghaitech`

optional arguments:
  -h, --help  show this help message and exit

Example:

python test.py ucsd-ped2

novelty-detection's People

Contributors

angpo avatar cvpr19-858 avatar davidea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

novelty-detection's Issues

import errors?

from torch.utils.data.dataloader import _use_shared_memory
from torch.utils.data.dataloader import int_classes
from torch.utils.data.dataloader import numpy_type_map
from torch.utils.data.dataloader import string_classes

Details about training

Hi, I am replicating your experiment's results. Could you give more the following details about the training on mnist and cifar10 which will replicate the results on your paper?

  • lambda (trade-off between llk and reconstruction loss), is it fixed during training
  • batch size
  • optimizer, the learning rate
  • when to stop training? If I use the validate set, which needs to be monitored? reconstruction loss, or negative llk, or novelty score to monitor

Thanks a lot!
Best,
Gin

Why not providing a train.py

How can I train your method on a dataset you didn't consider?

I want to compare your method with mine. Is there a way to replicate exactly your training on my dataset?

Not be able to download the check point file

I am trying to download the check point file for a few times and it all gave me the errors for "curl: (56) Recv failure: Connection reset by peer". Are the check points files still available for download?

About trainning.

Hello, thank you for your work, can you share your training codes?

Loss exploding after few steps

`from functools import reduce
from operator import mul
from typing import Tuple
import numpy as np
import torch
import torchvision
import torch.nn as nn
from models.loss_functions.lsaloss import LSALoss
from models.base import BaseModule
from models.blocks_2d import DownsampleBlock
from models.blocks_2d import ResidualBlock
from models.blocks_2d import UpsampleBlock
from models.estimator_1D import Estimator1D
import cv2

class Encoder(BaseModule):
"""
CIFAR10 model encoder.
"""
def init(self, input_shape, code_length):
# type: (Tuple[int, int, int], int) -> None
"""
Class constructor:

    :param input_shape: the shape of CIFAR10 samples.
    :param code_length: the dimensionality of latent vectors.
    """
    super(Encoder, self).__init__()

    self.input_shape = input_shape
    self.code_length = code_length

    c, h, w = input_shape

    print (c,h,w)

    activation_fn = nn.LeakyReLU()

    # Convolutional network
    self.conv = nn.Sequential(
        nn.Conv2d(in_channels=c, out_channels=32, kernel_size=3, bias=False),
        activation_fn,
        ResidualBlock(channel_in=32, channel_out=32, activation_fn=activation_fn),
        DownsampleBlock(channel_in=32, channel_out=64, activation_fn=activation_fn),
        DownsampleBlock(channel_in=64, channel_out=128, activation_fn=activation_fn),
        DownsampleBlock(channel_in=128, channel_out=256, activation_fn=activation_fn),
    )
    self.deepest_shape = (256, h // 8, w // 8)

    # FC network
    self.fc = nn.Sequential(
        nn.Linear(in_features=reduce(mul, self.deepest_shape), out_features=256),
        nn.BatchNorm1d(num_features=256),
        activation_fn,
        nn.Linear(in_features=256, out_features=code_length),
        nn.Sigmoid()
    )

def forward(self, x):
    # types: (torch.Tensor) -> torch.Tensor
    """
    Forward propagation.

    :param x: the input batch of images.
    :return: the batch of latent vectors.
    """
    h = x
    print (type(h))
    h = self.conv(h)
    h = h.view(len(h), -1)
    o = self.fc(h)

    return o

class Decoder(BaseModule):
"""
CIFAR10 model decoder.
"""
def init(self, code_length, deepest_shape, output_shape):
# type: (int, Tuple[int, int, int], Tuple[int, int, int]) -> None
"""
Class constructor.

    :param code_length: the dimensionality of latent vectors.
    :param deepest_shape: the dimensionality of the encoder's deepest convolutional map.
    :param output_shape: the shape of CIFAR10 samples.
    """
    super(Decoder, self).__init__()

    self.code_length = code_length
    self.deepest_shape = deepest_shape
    self.output_shape = output_shape

    print (self.output_shape,"--")

    activation_fn = nn.LeakyReLU()

    # FC network
    self.fc = nn.Sequential(
        nn.Linear(in_features=code_length, out_features=256),
        nn.BatchNorm1d(num_features=256),
        activation_fn,
        nn.Linear(in_features=256, out_features=reduce(mul, deepest_shape)),
        nn.BatchNorm1d(num_features=reduce(mul, deepest_shape)),
        activation_fn
    )

    # Convolutional network
    self.conv = nn.Sequential(
        UpsampleBlock(channel_in=256, channel_out=128, activation_fn=activation_fn),
        UpsampleBlock(channel_in=128, channel_out=64, activation_fn=activation_fn),
        UpsampleBlock(channel_in=64, channel_out=32, activation_fn=activation_fn),
        ResidualBlock(channel_in=32, channel_out=32, activation_fn=activation_fn),
        nn.Conv2d(in_channels=32, out_channels=3, kernel_size=1, bias=False)
    )

def forward(self, x):
    # types: (torch.Tensor) -> torch.Tensor
    """
    Forward propagation.

    :param x: the batch of latent vectors.
    :return: the batch of reconstructions.
    """
    h = x
    h = self.fc(h)
    h = h.view(len(h), *self.deepest_shape)
    h = self.conv(h)
    o = h

    return o

class LSACIFAR10(BaseModule):
"""
LSA model for CIFAR10 one-class classification.
"""
def init(self, input_shape, code_length, cpd_channels):
# type: (Tuple[int, int, int], int, int) -> None
"""
Class constructor.

    :param input_shape: the shape of CIFAR10 samples.
    :param code_length: the dimensionality of latent vectors.
    :param cpd_channels: number of bins in which the multinomial works.
    """
    super(LSACIFAR10, self).__init__()

    self.input_shape = input_shape
    self.code_length = code_length

    # Build encoder
    self.encoder = Encoder(
        input_shape=input_shape,
        code_length=code_length
    )

    # Build decoder
    self.decoder = Decoder(
        code_length=code_length,
        deepest_shape=self.encoder.deepest_shape,
        output_shape=input_shape
    )

    # Build estimator
    self.estimator = Estimator1D(
        code_length=code_length,
        fm_list=[32, 32, 32, 32],
        cpd_channels=cpd_channels
    )

def forward(self, x):
    # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
    """
    Forward propagation.

    :param x: the input batch of images.
    :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
    """
    h = x

    # Produce representations
    z = self.encoder(h)

    # Estimate CPDs with autoregression
    z_dist = self.estimator(z)

    # Reconstruct x
    x_r = self.decoder(z)
    # print (x_r.shape)
    x_r = x_r.view(-1, *self.input_shape)

    return x_r, z, z_dist

def load_dataset(data_path="/home/jbmai/Downloads/Defect Images-20190705T133320Z-001"):
# data_path = 'data/train/'

torchvision.transforms.Grayscale(num_output_channels=1)

trainTransform  = torchvision.transforms.Compose([
                                torchvision.transforms.Resize(size=(128,128), interpolation=2),
                                torchvision.transforms.ToTensor(), 
                                ])




train_dataset = torchvision.datasets.ImageFolder(
    root=data_path,
    transform=trainTransform)


train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size=64,
    num_workers=0,
    shuffle=True
)
return train_loader

net = LSACIFAR10(input_shape=[3,128,128],code_length = 32,cpd_channels =100)
lossFunction = LSALoss(cpd_channels=100)
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

try:

checkpoint = torch.load("savedWeights/enc.pth")
net.encoder.load_state_dict(checkpoint)

checkpoint = torch.load("savedWeights/est.pth")
net.estimator.load_state_dict(checkpoint)

checkpoint = torch.load("savedWeights/dec.pth")
net.decoder.load_state_dict(checkpoint)

except Exception as e:
print (e)

for epoch in range(1000): # loop over the dataset multiple times

running_loss = 0.0
d = load_dataset()
for i, (data,l) in enumerate(d):
    # get the inputs; data is a list of [inputs, labels]
    
    # print (data.shape)

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    # print (data)
    x_r,z,z_dist = net.forward(data)
    # print (x_r.shape)
    # print(data.shape)
    loss = lossFunction(data,x_r,z,z_dist)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 5 == 0:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 5))
        running_loss = 0.0
if (epoch % 5)== 0 :
    # print ("--------------------{} epoch-----------".format(epoch))
    # net.encoder.eval()
    # net.estimator.eval()
    # net.decoder.eval()

    # z = net.encoder(data)
    # z_dist = net.estimator(z)
    # x_r = net.decoder(z).permute(0,2,3,1).detach().numpy()
    # out =x_r
    # print (type(out))
    # for i in range(out.shape[0]):
    #     # print (out.shape)
    #     # # out.permute(0,2,3,1)
    #     # print (out.shape)
    #     cv2.imwrite("constructedImages/outDec{}_{}.jpg".format(epoch,i),out[i,:,:,:]*255)
    #     # cv2.waitKey(0)
       
    #     # cv2.
    # net.encoder.train()
    # net.estimator.train()
    # net.decoder.eval()
    torch.save(net.encoder.state_dict(),("savedWeights/enc.pth"))
    torch.save(net.estimator.state_dict(),("savedWeights/est.pth"))
    torch.save(net.decoder.state_dict(),("savedWeights/dec.pth"))

print('Finished Training')`

Output :

<class 'torch.Tensor'>
[1, 1] loss: 727109273.600
<class 'torch.Tensor'>
[2, 1] loss: 2495627954514337382531072.000

Hi can you help me rectify the issue.

Loss increases during the traning

I am trying to do training but the loss function increases rather than decrease

I have attached the following training part
___________________________________code start
for cl_idx, video_id in enumerate(dataset.train_videos):

        # Run the train video 
        dataset.train(video_id)
        loader = DataLoader(dataset, collate_fn=dataset.collate_fn)

        # Build score containers
        #sample_llk = np.zeros(shape=(len(loader) + t - 1,))
        #sample_rec = np.zeros(shape=(len(loader) + t - 1,))
        ##uploading the ground truth
        #sample_y = dataset.load_test_sequence_gt(video_id)
        for i, (x, y) in tqdm(enumerate(loader), desc=f'Computing scores for {dataset}'):
            optimizer.zero_grad()
            x = x.to('cuda')
            # Forward pass, get our logits then backward pass, then update weights
            x_r, z, z_dist = model(x)
             # Calculate the joint loss 
            loss=criterion(x, x_r, z, z_dist)
            #print(loss)
            ## do backward and the update
            loss.backward()
            optimizer.step()
            running_loss += loss.item()  
            print (running_loss)

how to training?

hello, thank you for your work, how to training the model with your code?

train ucsd

Hi,thanks for your work。When I train it ,I get this error。

Traceback (most recent call last):
File "train.py", line 201, in
main()
File "train.py", line 192, in main
train_ucsdped2()
File "train.py", line 128, in train_ucsdped2
x_r, z, z_dist = model(x)
File "/home/dl/VSST/dm/novelty-detection-master/models/base.py", line 33, in call
return super(BaseModule, self).call(*args, **kwargs)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/dl/VSST/dm/novelty-detection-master/models/LSA_ucsd.py", line 189, in forward
z = self.encoder(h)
File "/home/dl/VSST/dm/novelty-detection-master/models/base.py", line 33, in call
return super(BaseModule, self).call(*args, **kwargs)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/dl/VSST/dm/novelty-detection-master/models/LSA_ucsd.py", line 62, in forward
h = self.conv(h)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/container.py", line 91, in forward
input = module(input)
File "/home/dl/VSST/dm/novelty-detection-master/models/base.py", line 33, in call
return super(BaseModule, self).call(*args, **kwargs)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/dl/VSST/dm/novelty-detection-master/models/blocks_3d.py", line 133, in forward
activation_fn=self._activation_fn
File "/home/dl/VSST/dm/novelty-detection-master/models/blocks_3d.py", line 33, in residual_op
ha = f1(ha)
File "/home/dl/VSST/dm/novelty-detection-master/models/base.py", line 33, in call
return super(BaseModule, self).call(*args, **kwargs)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/dl/VSST/dm/novelty-detection-master/models/layers/mconv3d.py", line 29, in forward
return super(MaskedConv3d, self).forward(x)
File "/home/dl/anaconda3/envs/pytorch0.4/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 421, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Expected 5-dimensional input for 5-dimensional weight [8, 1, 3, 3, 3], but got input of size [105, 690, 1, 8, 32, 32] instead

checkpoints link is not working

Hi, I am unable to download checkpoints, the link is not working. Could you please check and update the link for download

Sampling

Hi, thank you for sharing your code about the LSA model. Having trained the full model, I'm wondering how I can sample from the LSA? Can I just draw a uniform random vector as z and put it into the CPD estimator?

Train own dataset

Hey,

thanks for this great work and the release of the code.
I would like to train on my own data, but I can't find a train.py file. So I would like to ask, how can I train on my own data?

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.